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