• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_VULKAN_DESCRIPTORSETALLOCATOR_H_
16 #define DAWNNATIVE_VULKAN_DESCRIPTORSETALLOCATOR_H_
17 
18 #include "common/SerialQueue.h"
19 #include "common/vulkan_platform.h"
20 #include "dawn_native/Error.h"
21 #include "dawn_native/IntegerTypes.h"
22 #include "dawn_native/ObjectBase.h"
23 #include "dawn_native/vulkan/DescriptorSetAllocation.h"
24 
25 #include <map>
26 #include <vector>
27 
28 namespace dawn_native { namespace vulkan {
29 
30     class BindGroupLayout;
31 
32     class DescriptorSetAllocator : public ObjectBase {
33         using PoolIndex = uint32_t;
34         using SetIndex = uint16_t;
35 
36       public:
37         static Ref<DescriptorSetAllocator> Create(
38             BindGroupLayout* layout,
39             std::map<VkDescriptorType, uint32_t> descriptorCountPerType);
40 
41         ResultOrError<DescriptorSetAllocation> Allocate();
42         void Deallocate(DescriptorSetAllocation* allocationInfo);
43         void FinishDeallocation(ExecutionSerial completedSerial);
44 
45       private:
46         DescriptorSetAllocator(BindGroupLayout* layout,
47                                std::map<VkDescriptorType, uint32_t> descriptorCountPerType);
48         ~DescriptorSetAllocator();
49 
50         MaybeError AllocateDescriptorPool();
51 
52         BindGroupLayout* mLayout;
53 
54         std::vector<VkDescriptorPoolSize> mPoolSizes;
55         SetIndex mMaxSets;
56 
57         struct DescriptorPool {
58             VkDescriptorPool vkPool;
59             std::vector<VkDescriptorSet> sets;
60             std::vector<SetIndex> freeSetIndices;
61         };
62 
63         std::vector<PoolIndex> mAvailableDescriptorPoolIndices;
64         std::vector<DescriptorPool> mDescriptorPools;
65 
66         struct Deallocation {
67             PoolIndex poolIndex;
68             SetIndex setIndex;
69         };
70         SerialQueue<ExecutionSerial, Deallocation> mPendingDeallocations;
71         ExecutionSerial mLastDeallocationSerial = ExecutionSerial(0);
72     };
73 
74 }}  // namespace dawn_native::vulkan
75 
76 #endif  // DAWNNATIVE_VULKAN_DESCRIPTORSETALLOCATOR_H_
77