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 #ifndef VK_DESCRIPTOR_SET_LAYOUT_HPP_
16 #define VK_DESCRIPTOR_SET_LAYOUT_HPP_
17
18 #include "VkObject.hpp"
19
20 #include "Device/Sampler.hpp"
21 #include "Vulkan/VkImageView.hpp"
22 #include "Vulkan/VkSampler.hpp"
23
24 #include <cstdint>
25
26 namespace vk {
27
28 class DescriptorSet;
29 class Device;
30
31 // TODO(b/129523279): Move to the Device or Pipeline layer.
32 struct alignas(16) ImageDescriptor
33 {
34 uint32_t imageViewId;
35 };
36
37 struct alignas(16) SampledImageDescriptor : ImageDescriptor
38 {
39 ~SampledImageDescriptor() = delete;
40
41 uint32_t samplerId;
42
43 alignas(16) sw::Texture texture;
44 int width; // Of base mip-level.
45 int height;
46 int depth; // Layer/cube count for arrayed images
47 int mipLevels;
48 int sampleCount;
49
50 ImageView *memoryOwner; // Pointer to the view which owns the memory used by the descriptor set
51 };
52
53 struct alignas(16) StorageImageDescriptor : ImageDescriptor
54 {
55 ~StorageImageDescriptor() = delete;
56
57 void *ptr;
58 int width;
59 int height;
60 int depth; // Layer/cube count for arrayed images
61 int rowPitchBytes;
62 int slicePitchBytes; // Layer pitch in case of array image
63 int samplePitchBytes;
64 int sampleCount;
65 int sizeInBytes;
66
67 void *stencilPtr;
68 int stencilRowPitchBytes;
69 int stencilSlicePitchBytes; // Layer pitch in case of array image
70 int stencilSamplePitchBytes;
71
72 ImageView *memoryOwner; // Pointer to the view which owns the memory used by the descriptor set
73 };
74
75 struct alignas(16) BufferDescriptor
76 {
77 ~BufferDescriptor() = delete;
78
79 void *ptr;
80 int sizeInBytes; // intended size of the bound region -- slides along with dynamic offsets
81 int robustnessSize; // total accessible size from static offset -- does not move with dynamic offset
82 };
83
84 class DescriptorSetLayout : public Object<DescriptorSetLayout, VkDescriptorSetLayout>
85 {
86 struct Binding
87 {
88 VkDescriptorType descriptorType;
89 uint32_t descriptorCount;
90 const vk::Sampler **immutableSamplers;
91
92 uint32_t offset; // Offset in bytes in the descriptor set data.
93 };
94
95 public:
96 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *pCreateInfo, void *mem);
97 void destroy(const VkAllocationCallbacks *pAllocator);
98
99 static size_t ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo *pCreateInfo);
100
101 static uint32_t GetDescriptorSize(VkDescriptorType type);
102 static bool IsDescriptorDynamic(VkDescriptorType type);
103
104 static void WriteDescriptorSet(Device *device, const VkWriteDescriptorSet &descriptorWrites);
105 static void CopyDescriptorSet(const VkCopyDescriptorSet &descriptorCopies);
106
107 static void WriteDescriptorSet(Device *device, DescriptorSet *dstSet, const VkDescriptorUpdateTemplateEntry &entry, const char *src);
108
109 void initialize(DescriptorSet *descriptorSet, uint32_t variableDescriptorCount);
110
111 // Returns the total size of the descriptor set in bytes.
112 size_t getDescriptorSetAllocationSize(uint32_t variableDescriptorCount) const;
113
114 // Returns the byte offset from the base address of the descriptor set for
115 // the given binding number.
116 uint32_t getBindingOffset(uint32_t bindingNumber) const;
117
118 // Returns the number of descriptors for the given binding number.
119 uint32_t getDescriptorCount(uint32_t bindingNumber) const;
120
121 // Returns the number of descriptors across all bindings that are dynamic.
122 uint32_t getDynamicDescriptorCount() const;
123
124 // Returns the relative index into the pipeline's dynamic offsets array for
125 // the given binding number. This index should be added to the base index
126 // returned by PipelineLayout::getDynamicOffsetBase() to produce the
127 // starting index for dynamic descriptors.
128 uint32_t getDynamicOffsetIndex(uint32_t bindingNumber) const;
129
130 // Returns the descriptor type for the given binding number.
131 VkDescriptorType getDescriptorType(uint32_t bindingNumber) const;
132
133 // Returns the number of entries in the direct-indexed array of bindings.
134 // It equals the highest binding number + 1.
getBindingsArraySize() const135 uint32_t getBindingsArraySize() const { return bindingsArraySize; }
136
137 private:
138 uint8_t *getDescriptorPointer(DescriptorSet *descriptorSet, uint32_t bindingNumber, uint32_t arrayElement, uint32_t count, size_t *typeSize) const;
139 size_t getDescriptorSetDataSize(uint32_t variableDescriptorCount) const;
140 static bool isDynamic(VkDescriptorType type);
141
142 const VkDescriptorSetLayoutCreateFlags flags;
143 uint32_t bindingsArraySize = 0;
144 Binding *const bindings; // Direct-indexed array of bindings.
145 };
146
Cast(VkDescriptorSetLayout object)147 static inline DescriptorSetLayout *Cast(VkDescriptorSetLayout object)
148 {
149 return DescriptorSetLayout::Cast(object);
150 }
151
152 } // namespace vk
153
154 #endif // VK_DESCRIPTOR_SET_LAYOUT_HPP_
155