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, VkDescriptorUpdateTemplateEntry const &entry, char const *src);
108 static void WriteTextureLevelInfo(sw::Texture *texture, int level, int width, int height, int depth, int pitchP, int sliceP, int samplePitchP, int sampleMax);
109
110 void initialize(DescriptorSet *descriptorSet);
111
112 // Returns the total size of the descriptor set in bytes.
113 size_t getDescriptorSetAllocationSize() const;
114
115 // Returns the byte offset from the base address of the descriptor set for
116 // the given binding number.
117 uint32_t getBindingOffset(uint32_t bindingNumber) const;
118
119 // Returns the number of descriptors for the given binding number.
120 uint32_t getDescriptorCount(uint32_t bindingNumber) const;
121
122 // Returns the number of descriptors across all bindings that are dynamic.
123 uint32_t getDynamicDescriptorCount() const;
124
125 // Returns the relative index into the pipeline's dynamic offsets array for
126 // the given binding number. This index should be added to the base index
127 // returned by PipelineLayout::getDynamicOffsetBase() to produce the
128 // starting index for dynamic descriptors.
129 uint32_t getDynamicOffsetIndex(uint32_t bindingNumber) const;
130
131 // Returns the descriptor type for the given binding number.
132 VkDescriptorType getDescriptorType(uint32_t bindingNumber) const;
133
134 // Returns the number of entries in the direct-indexed array of bindings.
135 // It equals the highest binding number + 1.
getBindingsArraySize() const136 uint32_t getBindingsArraySize() const { return bindingsArraySize; }
137
138 private:
139 uint8_t *getDescriptorPointer(DescriptorSet *descriptorSet, uint32_t bindingNumber, uint32_t arrayElement, uint32_t count, size_t *typeSize) const;
140 size_t getDescriptorSetDataSize() const;
141 static bool isDynamic(VkDescriptorType type);
142
143 const VkDescriptorSetLayoutCreateFlags flags;
144 uint32_t bindingsArraySize = 0;
145 Binding *const bindings; // Direct-indexed array of bindings.
146 };
147
Cast(VkDescriptorSetLayout object)148 static inline DescriptorSetLayout *Cast(VkDescriptorSetLayout object)
149 {
150 return DescriptorSetLayout::Cast(object);
151 }
152
153 } // namespace vk
154
155 #endif // VK_DESCRIPTOR_SET_LAYOUT_HPP_
156