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 namespace vk {
25
26 class DescriptorSet;
27 class Device;
28
29 // TODO(b/129523279): Move to the Device or Pipeline layer.
30 struct alignas(16) SampledImageDescriptor
31 {
32 ~SampledImageDescriptor() = delete;
33
34 void updateSampler(VkSampler sampler);
35
36 // TODO(b/129523279): Minimize to the data actually needed.
37 vk::Sampler sampler;
38 vk::Device *device;
39
40 uint32_t imageViewId;
41 VkImageViewType type;
42 VkFormat format;
43 VkComponentMapping swizzle;
44 alignas(16) sw::Texture texture;
45 VkExtent3D extent; // Of base mip-level.
46 int arrayLayers;
47 int mipLevels;
48 int sampleCount;
49 };
50
51 struct alignas(16) StorageImageDescriptor
52 {
53 ~StorageImageDescriptor() = delete;
54
55 void *ptr;
56 VkExtent3D extent;
57 int rowPitchBytes;
58 int slicePitchBytes;
59 int samplePitchBytes;
60 int arrayLayers;
61 int sampleCount;
62 int sizeInBytes;
63
64 void *stencilPtr;
65 int stencilRowPitchBytes;
66 int stencilSlicePitchBytes;
67 int stencilSamplePitchBytes;
68 };
69
70 struct alignas(16) BufferDescriptor
71 {
72 ~BufferDescriptor() = delete;
73
74 void *ptr;
75 int sizeInBytes; // intended size of the bound region -- slides along with dynamic offsets
76 int robustnessSize; // total accessible size from static offset -- does not move with dynamic offset
77 };
78
79 class DescriptorSetLayout : public Object<DescriptorSetLayout, VkDescriptorSetLayout>
80 {
81 public:
82 DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *pCreateInfo, void *mem);
83 void destroy(const VkAllocationCallbacks *pAllocator);
84
85 static size_t ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo *pCreateInfo);
86
87 static size_t GetDescriptorSize(VkDescriptorType type);
88 static void WriteDescriptorSet(Device *device, const VkWriteDescriptorSet &descriptorWrites);
89 static void CopyDescriptorSet(const VkCopyDescriptorSet &descriptorCopies);
90
91 static void WriteDescriptorSet(Device *device, DescriptorSet *dstSet, VkDescriptorUpdateTemplateEntry const &entry, char const *src);
92 static void WriteTextureLevelInfo(sw::Texture *texture, int level, int width, int height, int depth, int pitchP, int sliceP, int samplePitchP, int sampleMax);
93
94 void initialize(DescriptorSet *descriptorSet);
95
96 // Returns the total size of the descriptor set in bytes.
97 size_t getDescriptorSetAllocationSize() const;
98
99 // Returns the number of bindings in the descriptor set.
100 size_t getBindingCount() const;
101
102 // Returns true iff the given binding exists.
103 bool hasBinding(uint32_t binding) const;
104
105 // Returns the byte offset from the base address of the descriptor set for
106 // the given binding and array element within that binding.
107 size_t getBindingOffset(uint32_t binding, size_t arrayElement) const;
108
109 // Returns the stride of an array of descriptors
110 size_t getBindingStride(uint32_t binding) const;
111
112 // Returns the number of descriptors across all bindings that are dynamic
113 // (see isBindingDynamic).
114 uint32_t getDynamicDescriptorCount() const;
115
116 // Returns the relative offset into the pipeline's dynamic offsets array for
117 // the given binding. This offset should be added to the base offset
118 // returned by PipelineLayout::getDynamicOffsetBase() to produce the
119 // starting index for dynamic descriptors.
120 uint32_t getDynamicDescriptorOffset(uint32_t binding) const;
121
122 // Returns true if the given binding is of type:
123 // VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC or
124 // VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
125 bool isBindingDynamic(uint32_t binding) const;
126
127 // Returns the VkDescriptorSetLayoutBinding for the given binding.
128 VkDescriptorSetLayoutBinding const &getBindingLayout(uint32_t binding) const;
129
130 uint8_t *getOffsetPointer(DescriptorSet *descriptorSet, uint32_t binding, uint32_t arrayElement, uint32_t count, size_t *typeSize) const;
131
132 private:
133 size_t getDescriptorSetDataSize() const;
134 uint32_t getBindingIndex(uint32_t binding) const;
135 static bool isDynamic(VkDescriptorType type);
136
137 VkDescriptorSetLayoutCreateFlags flags;
138 uint32_t bindingCount;
139 VkDescriptorSetLayoutBinding *bindings;
140 size_t *bindingOffsets;
141 };
142
Cast(VkDescriptorSetLayout object)143 static inline DescriptorSetLayout *Cast(VkDescriptorSetLayout object)
144 {
145 return DescriptorSetLayout::Cast(object);
146 }
147
148 } // namespace vk
149
150 #endif // VK_DESCRIPTOR_SET_LAYOUT_HPP_
151