1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrVkDescriptorSetManager_DEFINED 9 #define GrVkDescriptorSetManager_DEFINED 10 11 #include "GrResourceHandle.h" 12 #include "GrVkDescriptorPool.h" 13 #include "SkRefCnt.h" 14 #include "SkTArray.h" 15 #include "vk/GrVkDefines.h" 16 17 class GrVkDescriptorSet; 18 class GrVkGpu; 19 class GrVkUniformHandler; 20 21 /** 22 * This class handles the allocation of descriptor sets for a given VkDescriptorSetLayout. It will 23 * try to reuse previously allocated descriptor sets if they are no longer in use by other objects. 24 */ 25 class GrVkDescriptorSetManager { 26 public: 27 GR_DEFINE_RESOURCE_HANDLE_CLASS(Handle); 28 29 GrVkDescriptorSetManager(GrVkGpu* gpu, 30 VkDescriptorType, 31 const GrVkUniformHandler* handler = nullptr); 32 33 GrVkDescriptorSetManager(GrVkGpu* gpu, 34 VkDescriptorType, 35 const SkTArray<uint32_t>& visibilities); 36 ~GrVkDescriptorSetManager()37 ~GrVkDescriptorSetManager() {} 38 39 void abandon(); 40 void release(const GrVkGpu* gpu); 41 layout()42 VkDescriptorSetLayout layout() const { return fPoolManager.fDescLayout; } 43 44 const GrVkDescriptorSet* getDescriptorSet(GrVkGpu* gpu, const Handle& handle); 45 46 void recycleDescriptorSet(const GrVkDescriptorSet*); 47 48 bool isCompatible(VkDescriptorType type, const GrVkUniformHandler*) const; 49 bool isCompatible(VkDescriptorType type, 50 const SkTArray<uint32_t>& visibilities) const; 51 52 private: 53 struct DescriptorPoolManager { 54 DescriptorPoolManager(VkDescriptorType type, GrVkGpu* gpu, 55 const GrVkUniformHandler* handler = nullptr); 56 DescriptorPoolManager(VkDescriptorType type, GrVkGpu* gpu, 57 const SkTArray<uint32_t>& visibilities); 58 59 ~DescriptorPoolManagerDescriptorPoolManager60 ~DescriptorPoolManager() { 61 SkASSERT(!fDescLayout); 62 SkASSERT(!fPool); 63 } 64 65 void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds); 66 67 void freeGPUResources(const GrVkGpu* gpu); 68 void abandonGPUResources(); 69 70 VkDescriptorSetLayout fDescLayout; 71 VkDescriptorType fDescType; 72 uint32_t fDescCountPerSet; 73 uint32_t fMaxDescriptors; 74 uint32_t fCurrentDescriptorCount; 75 GrVkDescriptorPool* fPool; 76 77 private: 78 enum { 79 kUniformDescPerSet = 2, 80 kMaxDescriptors = 1024, 81 kStartNumDescriptors = 16, // must be less than kMaxUniformDescriptors 82 }; 83 84 void init(GrVkGpu* gpu, VkDescriptorType type, const GrVkUniformHandler* uniformHandler, 85 const SkTArray<uint32_t>* visibilities); 86 87 void getNewPool(GrVkGpu* gpu); 88 }; 89 90 DescriptorPoolManager fPoolManager; 91 SkTArray<const GrVkDescriptorSet*, true> fFreeSets; 92 SkSTArray<4, uint32_t> fBindingVisibilities; 93 }; 94 95 #endif 96