Home | History | Annotate | Download | only in vk
      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     static GrVkDescriptorSetManager* CreateUniformManager(GrVkGpu* gpu);
     30     static GrVkDescriptorSetManager* CreateSamplerManager(GrVkGpu* gpu, VkDescriptorType type,
     31                                                           const GrVkUniformHandler&);
     32     static GrVkDescriptorSetManager* CreateSamplerManager(GrVkGpu* gpu, VkDescriptorType type,
     33                                                           const SkTArray<uint32_t>& visibilities);
     34 
     35     ~GrVkDescriptorSetManager() {}
     36 
     37     void abandon();
     38     void release(const GrVkGpu* gpu);
     39 
     40     VkDescriptorSetLayout layout() const { return fPoolManager.fDescLayout; }
     41 
     42     const GrVkDescriptorSet* getDescriptorSet(GrVkGpu* gpu, const Handle& handle);
     43 
     44     void recycleDescriptorSet(const GrVkDescriptorSet*);
     45 
     46     bool isCompatible(VkDescriptorType type, const GrVkUniformHandler*) const;
     47     bool isCompatible(VkDescriptorType type,
     48                       const SkTArray<uint32_t>& visibilities) const;
     49 
     50 private:
     51     struct DescriptorPoolManager {
     52         DescriptorPoolManager(VkDescriptorType type, GrVkGpu* gpu,
     53                               const SkTArray<uint32_t>& visibilities);
     54 
     55 
     56         ~DescriptorPoolManager() {
     57             SkASSERT(!fDescLayout);
     58             SkASSERT(!fPool);
     59         }
     60 
     61         void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds);
     62 
     63         void freeGPUResources(const GrVkGpu* gpu);
     64         void abandonGPUResources();
     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             kUniformDescPerSet = 2,
     76             kMaxDescriptors = 1024,
     77             kStartNumDescriptors = 16, // must be less than kMaxUniformDescriptors
     78         };
     79 
     80         void getNewPool(GrVkGpu* gpu);
     81     };
     82 
     83     GrVkDescriptorSetManager(GrVkGpu* gpu,
     84                              VkDescriptorType,
     85                              const SkTArray<uint32_t>& visibilities);
     86 
     87 
     88     DescriptorPoolManager                    fPoolManager;
     89     SkTArray<const GrVkDescriptorSet*, true> fFreeSets;
     90     SkSTArray<4, uint32_t>                   fBindingVisibilities;
     91 };
     92 
     93 #endif
     94