xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkDescriptorSetLayout.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker //    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker 
15*03ce13f7SAndroid Build Coastguard Worker #ifndef VK_DESCRIPTOR_SET_LAYOUT_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define VK_DESCRIPTOR_SET_LAYOUT_HPP_
17*03ce13f7SAndroid Build Coastguard Worker 
18*03ce13f7SAndroid Build Coastguard Worker #include "VkObject.hpp"
19*03ce13f7SAndroid Build Coastguard Worker 
20*03ce13f7SAndroid Build Coastguard Worker #include "Device/Sampler.hpp"
21*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkImageView.hpp"
22*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkSampler.hpp"
23*03ce13f7SAndroid Build Coastguard Worker 
24*03ce13f7SAndroid Build Coastguard Worker #include <cstdint>
25*03ce13f7SAndroid Build Coastguard Worker 
26*03ce13f7SAndroid Build Coastguard Worker namespace vk {
27*03ce13f7SAndroid Build Coastguard Worker 
28*03ce13f7SAndroid Build Coastguard Worker class DescriptorSet;
29*03ce13f7SAndroid Build Coastguard Worker class Device;
30*03ce13f7SAndroid Build Coastguard Worker 
31*03ce13f7SAndroid Build Coastguard Worker // TODO(b/129523279): Move to the Device or Pipeline layer.
32*03ce13f7SAndroid Build Coastguard Worker struct alignas(16) ImageDescriptor
33*03ce13f7SAndroid Build Coastguard Worker {
34*03ce13f7SAndroid Build Coastguard Worker 	uint32_t imageViewId;
35*03ce13f7SAndroid Build Coastguard Worker };
36*03ce13f7SAndroid Build Coastguard Worker 
37*03ce13f7SAndroid Build Coastguard Worker struct alignas(16) SampledImageDescriptor : ImageDescriptor
38*03ce13f7SAndroid Build Coastguard Worker {
39*03ce13f7SAndroid Build Coastguard Worker 	~SampledImageDescriptor() = delete;
40*03ce13f7SAndroid Build Coastguard Worker 
41*03ce13f7SAndroid Build Coastguard Worker 	uint32_t samplerId;
42*03ce13f7SAndroid Build Coastguard Worker 
43*03ce13f7SAndroid Build Coastguard Worker 	alignas(16) sw::Texture texture;
44*03ce13f7SAndroid Build Coastguard Worker 	int width;  // Of base mip-level.
45*03ce13f7SAndroid Build Coastguard Worker 	int height;
46*03ce13f7SAndroid Build Coastguard Worker 	int depth;  // Layer/cube count for arrayed images
47*03ce13f7SAndroid Build Coastguard Worker 	int mipLevels;
48*03ce13f7SAndroid Build Coastguard Worker 	int sampleCount;
49*03ce13f7SAndroid Build Coastguard Worker 
50*03ce13f7SAndroid Build Coastguard Worker 	ImageView *memoryOwner;  // Pointer to the view which owns the memory used by the descriptor set
51*03ce13f7SAndroid Build Coastguard Worker };
52*03ce13f7SAndroid Build Coastguard Worker 
53*03ce13f7SAndroid Build Coastguard Worker struct alignas(16) StorageImageDescriptor : ImageDescriptor
54*03ce13f7SAndroid Build Coastguard Worker {
55*03ce13f7SAndroid Build Coastguard Worker 	~StorageImageDescriptor() = delete;
56*03ce13f7SAndroid Build Coastguard Worker 
57*03ce13f7SAndroid Build Coastguard Worker 	void *ptr;
58*03ce13f7SAndroid Build Coastguard Worker 	int width;
59*03ce13f7SAndroid Build Coastguard Worker 	int height;
60*03ce13f7SAndroid Build Coastguard Worker 	int depth;  // Layer/cube count for arrayed images
61*03ce13f7SAndroid Build Coastguard Worker 	int rowPitchBytes;
62*03ce13f7SAndroid Build Coastguard Worker 	int slicePitchBytes;  // Layer pitch in case of array image
63*03ce13f7SAndroid Build Coastguard Worker 	int samplePitchBytes;
64*03ce13f7SAndroid Build Coastguard Worker 	int sampleCount;
65*03ce13f7SAndroid Build Coastguard Worker 	int sizeInBytes;
66*03ce13f7SAndroid Build Coastguard Worker 
67*03ce13f7SAndroid Build Coastguard Worker 	void *stencilPtr;
68*03ce13f7SAndroid Build Coastguard Worker 	int stencilRowPitchBytes;
69*03ce13f7SAndroid Build Coastguard Worker 	int stencilSlicePitchBytes;  // Layer pitch in case of array image
70*03ce13f7SAndroid Build Coastguard Worker 	int stencilSamplePitchBytes;
71*03ce13f7SAndroid Build Coastguard Worker 
72*03ce13f7SAndroid Build Coastguard Worker 	ImageView *memoryOwner;  // Pointer to the view which owns the memory used by the descriptor set
73*03ce13f7SAndroid Build Coastguard Worker };
74*03ce13f7SAndroid Build Coastguard Worker 
75*03ce13f7SAndroid Build Coastguard Worker struct alignas(16) BufferDescriptor
76*03ce13f7SAndroid Build Coastguard Worker {
77*03ce13f7SAndroid Build Coastguard Worker 	~BufferDescriptor() = delete;
78*03ce13f7SAndroid Build Coastguard Worker 
79*03ce13f7SAndroid Build Coastguard Worker 	void *ptr;
80*03ce13f7SAndroid Build Coastguard Worker 	int sizeInBytes;     // intended size of the bound region -- slides along with dynamic offsets
81*03ce13f7SAndroid Build Coastguard Worker 	int robustnessSize;  // total accessible size from static offset -- does not move with dynamic offset
82*03ce13f7SAndroid Build Coastguard Worker };
83*03ce13f7SAndroid Build Coastguard Worker 
84*03ce13f7SAndroid Build Coastguard Worker class DescriptorSetLayout : public Object<DescriptorSetLayout, VkDescriptorSetLayout>
85*03ce13f7SAndroid Build Coastguard Worker {
86*03ce13f7SAndroid Build Coastguard Worker 	struct Binding
87*03ce13f7SAndroid Build Coastguard Worker 	{
88*03ce13f7SAndroid Build Coastguard Worker 		VkDescriptorType descriptorType;
89*03ce13f7SAndroid Build Coastguard Worker 		uint32_t descriptorCount;
90*03ce13f7SAndroid Build Coastguard Worker 		const vk::Sampler **immutableSamplers;
91*03ce13f7SAndroid Build Coastguard Worker 
92*03ce13f7SAndroid Build Coastguard Worker 		uint32_t offset;  // Offset in bytes in the descriptor set data.
93*03ce13f7SAndroid Build Coastguard Worker 	};
94*03ce13f7SAndroid Build Coastguard Worker 
95*03ce13f7SAndroid Build Coastguard Worker public:
96*03ce13f7SAndroid Build Coastguard Worker 	DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *pCreateInfo, void *mem);
97*03ce13f7SAndroid Build Coastguard Worker 	void destroy(const VkAllocationCallbacks *pAllocator);
98*03ce13f7SAndroid Build Coastguard Worker 
99*03ce13f7SAndroid Build Coastguard Worker 	static size_t ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo *pCreateInfo);
100*03ce13f7SAndroid Build Coastguard Worker 
101*03ce13f7SAndroid Build Coastguard Worker 	static uint32_t GetDescriptorSize(VkDescriptorType type);
102*03ce13f7SAndroid Build Coastguard Worker 	static bool IsDescriptorDynamic(VkDescriptorType type);
103*03ce13f7SAndroid Build Coastguard Worker 
104*03ce13f7SAndroid Build Coastguard Worker 	static void WriteDescriptorSet(Device *device, const VkWriteDescriptorSet &descriptorWrites);
105*03ce13f7SAndroid Build Coastguard Worker 	static void CopyDescriptorSet(const VkCopyDescriptorSet &descriptorCopies);
106*03ce13f7SAndroid Build Coastguard Worker 
107*03ce13f7SAndroid Build Coastguard Worker 	static void WriteDescriptorSet(Device *device, DescriptorSet *dstSet, const VkDescriptorUpdateTemplateEntry &entry, const char *src);
108*03ce13f7SAndroid Build Coastguard Worker 
109*03ce13f7SAndroid Build Coastguard Worker 	void initialize(DescriptorSet *descriptorSet, uint32_t variableDescriptorCount);
110*03ce13f7SAndroid Build Coastguard Worker 
111*03ce13f7SAndroid Build Coastguard Worker 	// Returns the total size of the descriptor set in bytes.
112*03ce13f7SAndroid Build Coastguard Worker 	size_t getDescriptorSetAllocationSize(uint32_t variableDescriptorCount) const;
113*03ce13f7SAndroid Build Coastguard Worker 
114*03ce13f7SAndroid Build Coastguard Worker 	// Returns the byte offset from the base address of the descriptor set for
115*03ce13f7SAndroid Build Coastguard Worker 	// the given binding number.
116*03ce13f7SAndroid Build Coastguard Worker 	uint32_t getBindingOffset(uint32_t bindingNumber) const;
117*03ce13f7SAndroid Build Coastguard Worker 
118*03ce13f7SAndroid Build Coastguard Worker 	// Returns the number of descriptors for the given binding number.
119*03ce13f7SAndroid Build Coastguard Worker 	uint32_t getDescriptorCount(uint32_t bindingNumber) const;
120*03ce13f7SAndroid Build Coastguard Worker 
121*03ce13f7SAndroid Build Coastguard Worker 	// Returns the number of descriptors across all bindings that are dynamic.
122*03ce13f7SAndroid Build Coastguard Worker 	uint32_t getDynamicDescriptorCount() const;
123*03ce13f7SAndroid Build Coastguard Worker 
124*03ce13f7SAndroid Build Coastguard Worker 	// Returns the relative index into the pipeline's dynamic offsets array for
125*03ce13f7SAndroid Build Coastguard Worker 	// the given binding number. This index should be added to the base index
126*03ce13f7SAndroid Build Coastguard Worker 	// returned by PipelineLayout::getDynamicOffsetBase() to produce the
127*03ce13f7SAndroid Build Coastguard Worker 	// starting index for dynamic descriptors.
128*03ce13f7SAndroid Build Coastguard Worker 	uint32_t getDynamicOffsetIndex(uint32_t bindingNumber) const;
129*03ce13f7SAndroid Build Coastguard Worker 
130*03ce13f7SAndroid Build Coastguard Worker 	// Returns the descriptor type for the given binding number.
131*03ce13f7SAndroid Build Coastguard Worker 	VkDescriptorType getDescriptorType(uint32_t bindingNumber) const;
132*03ce13f7SAndroid Build Coastguard Worker 
133*03ce13f7SAndroid Build Coastguard Worker 	// Returns the number of entries in the direct-indexed array of bindings.
134*03ce13f7SAndroid Build Coastguard Worker 	// It equals the highest binding number + 1.
getBindingsArraySize() const135*03ce13f7SAndroid Build Coastguard Worker 	uint32_t getBindingsArraySize() const { return bindingsArraySize; }
136*03ce13f7SAndroid Build Coastguard Worker 
137*03ce13f7SAndroid Build Coastguard Worker private:
138*03ce13f7SAndroid Build Coastguard Worker 	uint8_t *getDescriptorPointer(DescriptorSet *descriptorSet, uint32_t bindingNumber, uint32_t arrayElement, uint32_t count, size_t *typeSize) const;
139*03ce13f7SAndroid Build Coastguard Worker 	size_t getDescriptorSetDataSize(uint32_t variableDescriptorCount) const;
140*03ce13f7SAndroid Build Coastguard Worker 	static bool isDynamic(VkDescriptorType type);
141*03ce13f7SAndroid Build Coastguard Worker 
142*03ce13f7SAndroid Build Coastguard Worker 	const VkDescriptorSetLayoutCreateFlags flags;
143*03ce13f7SAndroid Build Coastguard Worker 	uint32_t bindingsArraySize = 0;
144*03ce13f7SAndroid Build Coastguard Worker 	Binding *const bindings;  // Direct-indexed array of bindings.
145*03ce13f7SAndroid Build Coastguard Worker };
146*03ce13f7SAndroid Build Coastguard Worker 
Cast(VkDescriptorSetLayout object)147*03ce13f7SAndroid Build Coastguard Worker static inline DescriptorSetLayout *Cast(VkDescriptorSetLayout object)
148*03ce13f7SAndroid Build Coastguard Worker {
149*03ce13f7SAndroid Build Coastguard Worker 	return DescriptorSetLayout::Cast(object);
150*03ce13f7SAndroid Build Coastguard Worker }
151*03ce13f7SAndroid Build Coastguard Worker 
152*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
153*03ce13f7SAndroid Build Coastguard Worker 
154*03ce13f7SAndroid Build Coastguard Worker #endif  // VK_DESCRIPTOR_SET_LAYOUT_HPP_
155