1 // Copyright 2018 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_BINDGROUPLAYOUTVK_H_ 16 #define DAWNNATIVE_VULKAN_BINDGROUPLAYOUTVK_H_ 17 18 #include "dawn_native/BindGroupLayout.h" 19 20 #include "common/SlabAllocator.h" 21 #include "common/vulkan_platform.h" 22 23 #include <vector> 24 25 namespace dawn_native { namespace vulkan { 26 27 class BindGroup; 28 struct DescriptorSetAllocation; 29 class DescriptorSetAllocator; 30 class Device; 31 32 VkDescriptorType VulkanDescriptorType(const BindingInfo& bindingInfo); 33 34 // In Vulkan descriptor pools have to be sized to an exact number of descriptors. This means 35 // it's hard to have something where we can mix different types of descriptor sets because 36 // we don't know if their vector of number of descriptors will be similar. 37 // 38 // That's why that in addition to containing the VkDescriptorSetLayout to create 39 // VkDescriptorSets for its bindgroups, the layout also acts as an allocator for the descriptor 40 // sets. 41 // 42 // The allocations is done with one pool per descriptor set, which is inefficient, but at least 43 // the pools are reused when no longer used. Minimizing the number of descriptor pool allocation 44 // is important because creating them can incur GPU memory allocation which is usually an 45 // expensive syscall. 46 class BindGroupLayout final : public BindGroupLayoutBase { 47 public: 48 static ResultOrError<Ref<BindGroupLayout>> Create( 49 Device* device, 50 const BindGroupLayoutDescriptor* descriptor, 51 PipelineCompatibilityToken pipelineCompatibilityToken); 52 53 BindGroupLayout(DeviceBase* device, 54 const BindGroupLayoutDescriptor* descriptor, 55 PipelineCompatibilityToken pipelineCompatibilityToken); 56 57 VkDescriptorSetLayout GetHandle() const; 58 59 ResultOrError<Ref<BindGroup>> AllocateBindGroup(Device* device, 60 const BindGroupDescriptor* descriptor); 61 void DeallocateBindGroup(BindGroup* bindGroup, 62 DescriptorSetAllocation* descriptorSetAllocation); 63 64 private: 65 ~BindGroupLayout() override; 66 MaybeError Initialize(); 67 void DestroyImpl() override; 68 69 // Dawn API 70 void SetLabelImpl() override; 71 72 VkDescriptorSetLayout mHandle = VK_NULL_HANDLE; 73 74 SlabAllocator<BindGroup> mBindGroupAllocator; 75 Ref<DescriptorSetAllocator> mDescriptorSetAllocator; 76 }; 77 78 }} // namespace dawn_native::vulkan 79 80 #endif // DAWNNATIVE_VULKAN_BINDGROUPLAYOUTVK_H_ 81