egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 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 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 11 | #include "GrVkVulkan.h" |
| 12 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 13 | #include "GrResourceHandle.h" |
| 14 | #include "GrVkDescriptorPool.h" |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 15 | #include "GrVkSampler.h" |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 16 | #include "SkRefCnt.h" |
| 17 | #include "SkTArray.h" |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 18 | |
| 19 | class GrVkDescriptorSet; |
| 20 | class GrVkGpu; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 21 | class GrVkUniformHandler; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * This class handles the allocation of descriptor sets for a given VkDescriptorSetLayout. It will |
| 25 | * try to reuse previously allocated descriptor sets if they are no longer in use by other objects. |
| 26 | */ |
| 27 | class GrVkDescriptorSetManager { |
| 28 | public: |
| 29 | GR_DEFINE_RESOURCE_HANDLE_CLASS(Handle); |
| 30 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 31 | static GrVkDescriptorSetManager* CreateUniformManager(GrVkGpu* gpu); |
| 32 | static GrVkDescriptorSetManager* CreateSamplerManager(GrVkGpu* gpu, VkDescriptorType type, |
| 33 | const GrVkUniformHandler&); |
| 34 | static GrVkDescriptorSetManager* CreateSamplerManager(GrVkGpu* gpu, VkDescriptorType type, |
| 35 | const SkTArray<uint32_t>& visibilities); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 36 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 37 | ~GrVkDescriptorSetManager() {} |
| 38 | |
| 39 | void abandon(); |
| 40 | void release(const GrVkGpu* gpu); |
| 41 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 42 | VkDescriptorSetLayout layout() const { return fPoolManager.fDescLayout; } |
| 43 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 44 | const GrVkDescriptorSet* getDescriptorSet(GrVkGpu* gpu, const Handle& handle); |
| 45 | |
| 46 | void recycleDescriptorSet(const GrVkDescriptorSet*); |
| 47 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 48 | bool isCompatible(VkDescriptorType type, const GrVkUniformHandler*) const; |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 49 | bool isCompatible(VkDescriptorType type, |
| 50 | const SkTArray<uint32_t>& visibilities) const; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | struct DescriptorPoolManager { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 54 | DescriptorPoolManager(VkDescriptorType type, GrVkGpu* gpu, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 55 | const SkTArray<uint32_t>& visibilities, |
| 56 | const SkTArray<const GrVkSampler*>& immutableSamplers); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 57 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 58 | |
| 59 | ~DescriptorPoolManager() { |
| 60 | SkASSERT(!fDescLayout); |
| 61 | SkASSERT(!fPool); |
| 62 | } |
| 63 | |
| 64 | void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds); |
| 65 | |
| 66 | void freeGPUResources(const GrVkGpu* gpu); |
| 67 | void abandonGPUResources(); |
| 68 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 69 | VkDescriptorSetLayout fDescLayout; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 70 | VkDescriptorType fDescType; |
| 71 | uint32_t fDescCountPerSet; |
| 72 | uint32_t fMaxDescriptors; |
| 73 | uint32_t fCurrentDescriptorCount; |
| 74 | GrVkDescriptorPool* fPool; |
| 75 | |
| 76 | private: |
| 77 | enum { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 78 | kUniformDescPerSet = 2, |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 79 | kMaxDescriptors = 1024, |
| 80 | kStartNumDescriptors = 16, // must be less than kMaxUniformDescriptors |
| 81 | }; |
| 82 | |
| 83 | void getNewPool(GrVkGpu* gpu); |
| 84 | }; |
| 85 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 86 | GrVkDescriptorSetManager(GrVkGpu* gpu, |
| 87 | VkDescriptorType, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 88 | const SkTArray<uint32_t>& visibilities, |
| 89 | const SkTArray<const GrVkSampler*>& immutableSamplers); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 90 | |
| 91 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 92 | DescriptorPoolManager fPoolManager; |
| 93 | SkTArray<const GrVkDescriptorSet*, true> fFreeSets; |
| 94 | SkSTArray<4, uint32_t> fBindingVisibilities; |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame^] | 95 | SkSTArray<4, const GrVkSampler*> fImmutableSamplers; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | #endif |