1 #ifndef _VKBUILDERUTIL_HPP
2 #define _VKBUILDERUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2015 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Vulkan object builder utilities.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "vkDefs.hpp"
27 #include "vkRef.hpp"
28 
29 #include <vector>
30 
31 namespace vk
32 {
33 
34 class DescriptorSetLayoutBuilder
35 {
36 public:
37 												DescriptorSetLayoutBuilder		(void);
38 
39 	DescriptorSetLayoutBuilder&					addBinding						(VkDescriptorType	descriptorType,
40 																				 deUint32			descriptorCount,
41 																				 VkShaderStageFlags	stageFlags,
42 																				 const VkSampler*	pImmutableSamplers);
43 
44 	DescriptorSetLayoutBuilder&					addIndexedBinding				(VkDescriptorType	descriptorType,
45 																				 deUint32			descriptorCount,
46 																				 VkShaderStageFlags	stageFlags,
47 																				 deUint32			dstBinding,
48 																				 const VkSampler*	pImmutableSamplers);
49 
50 	Move<VkDescriptorSetLayout>					build							(const DeviceInterface& vk, VkDevice device, VkDescriptorSetLayoutCreateFlags extraFlags = 0) const;
51 
52 	// helpers
53 
addSingleBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags)54 	inline DescriptorSetLayoutBuilder&			addSingleBinding				(VkDescriptorType	descriptorType,
55 																				 VkShaderStageFlags	stageFlags)
56 	{
57 		return addBinding(descriptorType, 1u, stageFlags, (VkSampler*)DE_NULL);
58 	}
addSingleIndexedBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,deUint32 dstBinding)59 	inline DescriptorSetLayoutBuilder&			addSingleIndexedBinding			(VkDescriptorType	descriptorType,
60 																				 VkShaderStageFlags	stageFlags,
61 																				 deUint32			dstBinding)
62 	{
63 		return addIndexedBinding(descriptorType, 1u, stageFlags, dstBinding, (VkSampler*)DE_NULL);
64 	}
addArrayBinding(VkDescriptorType descriptorType,deUint32 descriptorCount,VkShaderStageFlags stageFlags)65 	inline DescriptorSetLayoutBuilder&			addArrayBinding					(VkDescriptorType	descriptorType,
66 																				 deUint32			descriptorCount,
67 																				 VkShaderStageFlags	stageFlags)
68 	{
69 		return addBinding(descriptorType, descriptorCount, stageFlags, (VkSampler*)DE_NULL);
70 	}
addSingleSamplerBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,const VkSampler * immutableSampler)71 	inline DescriptorSetLayoutBuilder&			addSingleSamplerBinding			(VkDescriptorType	descriptorType,
72 																				 VkShaderStageFlags	stageFlags,
73 																				 const VkSampler*	immutableSampler)	//!< \note: Using pointer to sampler to clarify that handle is not
74 																														//!<        copied and argument lifetime is expected to cover build()
75 																														//!<        call.
76 	{
77 		return addBinding(descriptorType, 1u, stageFlags, immutableSampler);
78 	}
addSingleIndexedSamplerBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,deUint32 dstBinding,const VkSampler * immutableSampler)79 	inline DescriptorSetLayoutBuilder&			addSingleIndexedSamplerBinding	(VkDescriptorType	descriptorType,
80 																				 VkShaderStageFlags	stageFlags,
81 																				 deUint32			dstBinding,
82 																				 const VkSampler*	immutableSampler)	//!< \note: Using pointer to sampler to clarify that handle is not
83 																														//!<        copied and argument lifetime is expected to cover build()
84 																														//!<        call.
85 	{
86 		return addIndexedBinding(descriptorType, 1u, stageFlags, dstBinding, immutableSampler);
87 	}
addArraySamplerBinding(VkDescriptorType descriptorType,deUint32 descriptorCount,VkShaderStageFlags stageFlags,const VkSampler * pImmutableSamplers)88 	inline DescriptorSetLayoutBuilder&			addArraySamplerBinding			(VkDescriptorType	descriptorType,
89 																				 deUint32			descriptorCount,
90 																				 VkShaderStageFlags	stageFlags,
91 																				 const VkSampler*	pImmutableSamplers)
92 	{
93 		return addBinding(descriptorType, descriptorCount, stageFlags, pImmutableSamplers);
94 	}
95 
96 private:
97 												DescriptorSetLayoutBuilder		(const DescriptorSetLayoutBuilder&); // delete
98 	DescriptorSetLayoutBuilder&					operator=						(const DescriptorSetLayoutBuilder&); // delete
99 
100 	std::vector<VkDescriptorSetLayoutBinding>	m_bindings;
101 
102 	struct ImmutableSamplerInfo
103 	{
104 		deUint32 bindingIndex;
105 		deUint32 samplerBaseIndex;
106 	};
107 
108 	std::vector<ImmutableSamplerInfo>			m_immutableSamplerInfos;
109 	std::vector<VkSampler>						m_immutableSamplers;
110 };
111 
112 class DescriptorPoolBuilder
113 {
114 public:
115 										DescriptorPoolBuilder	(void);
116 
117 	DescriptorPoolBuilder&				addType					(VkDescriptorType type, deUint32 numDescriptors = 1u);
118 	Move<VkDescriptorPool>				build					(const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets, const void *pNext = DE_NULL) const;
119 
120 private:
121 										DescriptorPoolBuilder	(const DescriptorPoolBuilder&); // delete
122 	DescriptorPoolBuilder&				operator=				(const DescriptorPoolBuilder&); // delete
123 
124 	std::vector<VkDescriptorPoolSize>	m_counts;
125 };
126 
127 class DescriptorSetUpdateBuilder
128 {
129 public:
130 	class Location
131 	{
132 	public:
binding(deUint32 binding_)133 		static inline Location	binding				(deUint32 binding_)
134 		{
135 			return Location(binding_, 0u);
136 		}
bindingArrayElement(deUint32 binding_,deUint32 arrayElement)137 		static inline Location	bindingArrayElement	(deUint32 binding_, deUint32 arrayElement)
138 		{
139 			return Location(binding_, arrayElement);
140 		}
141 
142 	private:
143 		// \note private to force use of factory methods that have more descriptive names
Location(deUint32 binding_,deUint32 arrayElement)144 		inline					Location			(deUint32 binding_, deUint32 arrayElement)
145 			: m_binding			(binding_)
146 			, m_arrayElement	(arrayElement)
147 		{
148 		}
149 
150 		friend class DescriptorSetUpdateBuilder;
151 
152 		const deUint32			m_binding;
153 		const deUint32			m_arrayElement;
154 	};
155 
156 										DescriptorSetUpdateBuilder	(void);
157 										/* DescriptorSetUpdateBuilder	(const DescriptorSetUpdateBuilder&); // do not delete */
158 
159 	DescriptorSetUpdateBuilder&			write						(VkDescriptorSet				destSet,
160 																	 deUint32						destBinding,
161 																	 deUint32						destArrayElement,
162 																	 deUint32						count,
163 																	 VkDescriptorType				descriptorType,
164 																	 const VkDescriptorImageInfo*	pImageInfo,
165 																	 const VkDescriptorBufferInfo*	pBufferInfo,
166 																	 const VkBufferView*			pTexelBufferView,
167 																	 const void*					pNext = DE_NULL);
168 
169 	DescriptorSetUpdateBuilder&			copy						(VkDescriptorSet	srcSet,
170 																	 deUint32			srcBinding,
171 																	 deUint32			srcArrayElement,
172 																	 VkDescriptorSet	destSet,
173 																	 deUint32			destBinding,
174 																	 deUint32			destArrayElement,
175 																	 deUint32			count);
176 
177 	void								update						(const DeviceInterface& vk, VkDevice device) const;
178 	void								updateWithPush				(const DeviceInterface& vk, VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, deUint32 setIdx, deUint32 descriptorIdx = 0, deUint32 numDescriptors = 0) const;
179 	void								clear						(void);
180 
181 	// helpers
182 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorImageInfo * pImageInfo)183 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
184 																	 const Location&				destLocation,
185 																	 VkDescriptorType				descriptorType,
186 																	 const VkDescriptorImageInfo*	pImageInfo)
187 	{
188 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, pImageInfo, DE_NULL, DE_NULL);
189 	}
190 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorBufferInfo * pBufferInfo)191 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
192 																	 const Location&				destLocation,
193 																	 VkDescriptorType				descriptorType,
194 																	 const VkDescriptorBufferInfo*	pBufferInfo)
195 	{
196 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
197 	}
198 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkBufferView * pTexelBufferView)199 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
200 																	 const Location&				destLocation,
201 																	 VkDescriptorType				descriptorType,
202 																	 const VkBufferView*			pTexelBufferView)
203 	{
204 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
205 	}
206 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkWriteDescriptorSetAccelerationStructureKHR * pAccelerationStructure)207 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet										destSet,
208 																	 const Location&										destLocation,
209 																	 VkDescriptorType										descriptorType,
210 																	 const VkWriteDescriptorSetAccelerationStructureKHR*	pAccelerationStructure)
211 	{
212 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, DE_NULL, pAccelerationStructure);
213 	}
214 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorImageInfo * pImageInfo)215 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
216 																	 const Location&				destLocation,
217 																	 VkDescriptorType				descriptorType,
218 																	 deUint32						numDescriptors,
219 																	 const VkDescriptorImageInfo*	pImageInfo)
220 	{
221 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, pImageInfo, DE_NULL, DE_NULL);
222 	}
223 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorBufferInfo * pBufferInfo)224 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
225 																	 const Location&				destLocation,
226 																	 VkDescriptorType				descriptorType,
227 																	 deUint32						numDescriptors,
228 																	 const VkDescriptorBufferInfo*	pBufferInfo)
229 	{
230 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
231 	}
232 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkBufferView * pTexelBufferView)233 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
234 																	 const Location&				destLocation,
235 																	 VkDescriptorType				descriptorType,
236 																	 deUint32						numDescriptors,
237 																	 const VkBufferView*			pTexelBufferView)
238 	{
239 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
240 	}
241 
copySingle(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation)242 	inline DescriptorSetUpdateBuilder&	copySingle					(VkDescriptorSet	srcSet,
243 																	 const Location&	srcLocation,
244 																	 VkDescriptorSet	destSet,
245 																	 const Location&	destLocation)
246 	{
247 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u);
248 	}
249 
copyArray(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation,deUint32 count)250 	inline DescriptorSetUpdateBuilder&	copyArray					(VkDescriptorSet	srcSet,
251 																	 const Location&	srcLocation,
252 																	 VkDescriptorSet	destSet,
253 																	 const Location&	destLocation,
254 																	 deUint32			count)
255 	{
256 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, count);
257 	}
258 
259 private:
260 	DescriptorSetUpdateBuilder&			operator=					(const DescriptorSetUpdateBuilder&); // delete
261 
262 	struct WriteDescriptorInfo
263 	{
264 		std::vector<VkDescriptorImageInfo>	imageInfos;
265 		std::vector<VkDescriptorBufferInfo>	bufferInfos;
266 		std::vector<VkBufferView>			texelBufferViews;
267 	};
268 
269 	std::vector<WriteDescriptorInfo>	m_writeDescriptorInfos;
270 
271 	std::vector<VkWriteDescriptorSet>	m_writes;
272 	std::vector<VkCopyDescriptorSet>	m_copies;
273 };
274 
275 } // vk
276 
277 #endif // _VKBUILDERUTIL_HPP
278