1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
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 #include "VkDescriptorSetLayout.hpp"
16 #include <cstring>
17
18 namespace
19 {
20
UsesImmutableSamplers(const VkDescriptorSetLayoutBinding & binding)21 static bool UsesImmutableSamplers(const VkDescriptorSetLayoutBinding& binding)
22 {
23 return (((binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) ||
24 (binding.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) &&
25 (binding.pImmutableSamplers != nullptr));
26 }
27
28 }
29
30 namespace vk
31 {
32
DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo * pCreateInfo,void * mem)33 DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo* pCreateInfo, void* mem) :
34 flags(pCreateInfo->flags), bindingCount(pCreateInfo->bindingCount), bindings(reinterpret_cast<VkDescriptorSetLayoutBinding*>(mem))
35 {
36 char* host_memory = static_cast<char*>(mem) + bindingCount * sizeof(VkDescriptorSetLayoutBinding);
37
38 for(uint32_t i = 0; i < bindingCount; i++)
39 {
40 bindings[i] = pCreateInfo->pBindings[i];
41 if(UsesImmutableSamplers(bindings[i]))
42 {
43 size_t immutableSamplersSize = bindings[i].descriptorCount * sizeof(VkSampler);
44 bindings[i].pImmutableSamplers = reinterpret_cast<const VkSampler*>(host_memory);
45 host_memory += immutableSamplersSize;
46 memcpy(const_cast<VkSampler*>(bindings[i].pImmutableSamplers),
47 pCreateInfo->pBindings[i].pImmutableSamplers,
48 immutableSamplersSize);
49 }
50 else
51 {
52 bindings[i].pImmutableSamplers = nullptr;
53 }
54 }
55 }
56
destroy(const VkAllocationCallbacks * pAllocator)57 void DescriptorSetLayout::destroy(const VkAllocationCallbacks* pAllocator)
58 {
59 for(uint32_t i = 0; i < bindingCount; i++)
60 {
61 if(UsesImmutableSamplers(bindings[i]))
62 {
63 // A single allocation is used for all immutable samplers, so only a single deallocation is needed.
64 vk::deallocate(const_cast<VkSampler*>(bindings[i].pImmutableSamplers), pAllocator);
65 break;
66 }
67 }
68
69 vk::deallocate(bindings, pAllocator);
70 }
71
ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo * pCreateInfo)72 size_t DescriptorSetLayout::ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo* pCreateInfo)
73 {
74 size_t allocationSize = pCreateInfo->bindingCount * sizeof(VkDescriptorSetLayoutBinding);
75
76 for(uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
77 {
78 if(UsesImmutableSamplers(pCreateInfo->pBindings[i]))
79 {
80 allocationSize += pCreateInfo->pBindings[i].descriptorCount * sizeof(VkSampler);
81 }
82 }
83
84 return allocationSize;
85 }
86
GetDescriptorSize(VkDescriptorType type)87 size_t DescriptorSetLayout::GetDescriptorSize(VkDescriptorType type)
88 {
89 switch(type)
90 {
91 case VK_DESCRIPTOR_TYPE_SAMPLER:
92 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
93 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
94 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
95 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
96 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
97 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
98 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
99 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
100 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
101 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
102 return sizeof(void*); // FIXME(b/123244275) : Compute actual required size for each desciptor type
103 default:
104 UNIMPLEMENTED("Unsupported Descriptor Type");
105 }
106
107 return 0;
108 }
109
getSize() const110 size_t DescriptorSetLayout::getSize() const
111 {
112 size_t size = 0;
113 for(uint32_t i = 0; i < bindingCount; i++)
114 {
115 size += bindings[i].descriptorCount * GetDescriptorSize(bindings[i].descriptorType);
116 }
117 return size;
118 }
119
120 } // namespace vk