• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // See GrVkResourceProvider::getZeroSamplerDescriptorSetHandle() for more info on what the zero
34     // sampler is for.
35     static GrVkDescriptorSetManager* CreateZeroSamplerManager(GrVkGpu* gpu);
36     static GrVkDescriptorSetManager* CreateInputManager(GrVkGpu* gpu);
37 
~GrVkDescriptorSetManager()38     ~GrVkDescriptorSetManager() {}
39 
40     void release(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 
50     bool isZeroSampler() const;
51 
52 private:
53     struct DescriptorPoolManager {
54         DescriptorPoolManager(VkDescriptorSetLayout, VkDescriptorType type,
55                               uint32_t descCountPerSet);
56 
~DescriptorPoolManagerDescriptorPoolManager57         ~DescriptorPoolManager() {
58             SkASSERT(!fDescLayout);
59             SkASSERT(!fPool);
60         }
61 
62         bool getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds);
63 
64         void freeGPUResources(GrVkGpu* gpu);
65 
66         VkDescriptorSetLayout  fDescLayout;
67         VkDescriptorType       fDescType;
68         uint32_t               fDescCountPerSet;
69         uint32_t               fMaxDescriptors;
70         uint32_t               fCurrentDescriptorCount;
71         GrVkDescriptorPool*    fPool;
72 
73     private:
74         enum {
75             kMaxDescriptors = 1024,
76             kStartNumDescriptors = 16, // must be less than kMaxUniformDescriptors
77         };
78 
79         bool getNewPool(GrVkGpu* gpu);
80     };
81 
82     static GrVkDescriptorSetManager* Create(GrVkGpu* gpu,
83                                             VkDescriptorType,
84                                             const SkTArray<uint32_t>& visibilities,
85                                             const SkTArray<const GrVkSampler*>& immutableSamplers);
86 
87     GrVkDescriptorSetManager(GrVkGpu* gpu,
88                              VkDescriptorType, VkDescriptorSetLayout, uint32_t descCountPerSet,
89                              const SkTArray<uint32_t>& visibilities,
90                              const SkTArray<const GrVkSampler*>& immutableSamplers);
91 
92 
93     DescriptorPoolManager                    fPoolManager;
94     SkTArray<const GrVkDescriptorSet*, true> fFreeSets;
95     SkSTArray<4, uint32_t>                   fBindingVisibilities;
96     SkSTArray<4, const GrVkSampler*>         fImmutableSamplers;
97 };
98 
99 #endif
100