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