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 "include/core/SkRefCnt.h"
12 #include "include/gpu/vk/GrVkTypes.h"
13 #include "include/private/SkTArray.h"
14 #include "src/gpu/GrResourceHandle.h"
15 #include "src/gpu/vk/GrVkDescriptorPool.h"
16 #include "src/gpu/vk/GrVkSampler.h"
17 
18 class GrVkDescriptorSet;
19 class GrVkGpu;
20 class GrVkUniformHandler;
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 
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);
35 
~GrVkDescriptorSetManager()36     ~GrVkDescriptorSetManager() {}
37 
38     void abandon();
39     void release(GrVkGpu* gpu);
40 
layout()41     VkDescriptorSetLayout layout() const { return fPoolManager.fDescLayout; }
42 
43     const GrVkDescriptorSet* getDescriptorSet(GrVkGpu* gpu, const Handle& handle);
44 
45     void recycleDescriptorSet(const GrVkDescriptorSet*);
46 
47     bool isCompatible(VkDescriptorType type, const GrVkUniformHandler*) const;
48     bool isCompatible(VkDescriptorType type,
49                       const SkTArray<uint32_t>& visibilities) const;
50 
51 private:
52     struct DescriptorPoolManager {
53         DescriptorPoolManager(VkDescriptorType type, GrVkGpu* gpu,
54                               const SkTArray<uint32_t>& visibilities,
55                               const SkTArray<const GrVkSampler*>& immutableSamplers);
56 
57 
~DescriptorPoolManagerDescriptorPoolManager58         ~DescriptorPoolManager() {
59             SkASSERT(!fDescLayout);
60             SkASSERT(!fPool);
61         }
62 
63         void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds);
64 
65         void freeGPUResources(GrVkGpu* gpu);
66         void abandonGPUResources();
67 
68         VkDescriptorSetLayout  fDescLayout;
69         VkDescriptorType       fDescType;
70         uint32_t               fDescCountPerSet;
71         uint32_t               fMaxDescriptors;
72         uint32_t               fCurrentDescriptorCount;
73         GrVkDescriptorPool*    fPool;
74 
75     private:
76         enum {
77             kUniformDescPerSet = 1,
78             kMaxDescriptors = 1024,
79             kStartNumDescriptors = 16, // must be less than kMaxUniformDescriptors
80         };
81 
82         void getNewPool(GrVkGpu* gpu);
83     };
84 
85     GrVkDescriptorSetManager(GrVkGpu* gpu,
86                              VkDescriptorType,
87                              const SkTArray<uint32_t>& visibilities,
88                              const SkTArray<const GrVkSampler*>& immutableSamplers);
89 
90 
91     DescriptorPoolManager                    fPoolManager;
92     SkTArray<const GrVkDescriptorSet*, true> fFreeSets;
93     SkSTArray<4, uint32_t>                   fBindingVisibilities;
94     SkSTArray<4, const GrVkSampler*>         fImmutableSamplers;
95 };
96 
97 #endif
98