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 	Move<VkDescriptorSetLayout>					build						(const DeviceInterface& vk, VkDevice device) const;
45 
46 	// helpers
47 
addSingleBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags)48 	inline DescriptorSetLayoutBuilder&			addSingleBinding			(VkDescriptorType	descriptorType,
49 																			 VkShaderStageFlags	stageFlags)
50 	{
51 		return addBinding(descriptorType, 1u, stageFlags, (VkSampler*)DE_NULL);
52 	}
addArrayBinding(VkDescriptorType descriptorType,deUint32 descriptorCount,VkShaderStageFlags stageFlags)53 	inline DescriptorSetLayoutBuilder&			addArrayBinding				(VkDescriptorType	descriptorType,
54 																			 deUint32			descriptorCount,
55 																			 VkShaderStageFlags	stageFlags)
56 	{
57 		return addBinding(descriptorType, descriptorCount, stageFlags, (VkSampler*)DE_NULL);
58 	}
addSingleSamplerBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,const VkSampler * immutableSampler)59 	inline DescriptorSetLayoutBuilder&			addSingleSamplerBinding		(VkDescriptorType	descriptorType,
60 																			 VkShaderStageFlags	stageFlags,
61 																			 const VkSampler*	immutableSampler)	//!< \note: Using pointer to sampler to clarify that handle is not
62 																													//!<        copied and argument lifetime is expected to cover build()
63 																													//!<        call.
64 	{
65 		return addBinding(descriptorType, 1u, stageFlags, immutableSampler);
66 	}
addArraySamplerBinding(VkDescriptorType descriptorType,deUint32 descriptorCount,VkShaderStageFlags stageFlags,const VkSampler * pImmutableSamplers)67 	inline DescriptorSetLayoutBuilder&			addArraySamplerBinding		(VkDescriptorType	descriptorType,
68 																			 deUint32			descriptorCount,
69 																			 VkShaderStageFlags	stageFlags,
70 																			 const VkSampler*	pImmutableSamplers)
71 	{
72 		return addBinding(descriptorType, descriptorCount, stageFlags, pImmutableSamplers);
73 	}
74 
75 private:
76 												DescriptorSetLayoutBuilder	(const DescriptorSetLayoutBuilder&); // delete
77 	DescriptorSetLayoutBuilder&					operator=					(const DescriptorSetLayoutBuilder&); // delete
78 
79 	std::vector<VkDescriptorSetLayoutBinding>	m_bindings;
80 
81 	struct ImmutableSamplerInfo
82 	{
83 		deUint32 bindingIndex;
84 		deUint32 samplerBaseIndex;
85 	};
86 
87 	std::vector<ImmutableSamplerInfo>			m_immutableSamplerInfos;
88 	std::vector<VkSampler>						m_immutableSamplers;
89 };
90 
91 class DescriptorPoolBuilder
92 {
93 public:
94 										DescriptorPoolBuilder	(void);
95 
96 	DescriptorPoolBuilder&				addType					(VkDescriptorType type, deUint32 numDescriptors = 1u);
97 	Move<VkDescriptorPool>				build					(const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const;
98 
99 private:
100 										DescriptorPoolBuilder	(const DescriptorPoolBuilder&); // delete
101 	DescriptorPoolBuilder&				operator=				(const DescriptorPoolBuilder&); // delete
102 
103 	std::vector<VkDescriptorPoolSize>	m_counts;
104 };
105 
106 class DescriptorSetUpdateBuilder
107 {
108 public:
109 	class Location
110 	{
111 	public:
binding(deUint32 binding_)112 		static inline Location	binding				(deUint32 binding_)
113 		{
114 			return Location(binding_, 0u);
115 		}
bindingArrayElement(deUint32 binding_,deUint32 arrayElement)116 		static inline Location	bindingArrayElement	(deUint32 binding_, deUint32 arrayElement)
117 		{
118 			return Location(binding_, arrayElement);
119 		}
120 
121 	private:
122 		// \note private to force use of factory methods that have more descriptive names
Location(deUint32 binding_,deUint32 arrayElement)123 		inline					Location			(deUint32 binding_, deUint32 arrayElement)
124 			: m_binding			(binding_)
125 			, m_arrayElement	(arrayElement)
126 		{
127 		}
128 
129 		friend class DescriptorSetUpdateBuilder;
130 
131 		const deUint32			m_binding;
132 		const deUint32			m_arrayElement;
133 	};
134 
135 										DescriptorSetUpdateBuilder	(void);
136 
137 	DescriptorSetUpdateBuilder&			write						(VkDescriptorSet				destSet,
138 																	 deUint32						destBinding,
139 																	 deUint32						destArrayElement,
140 																	 deUint32						count,
141 																	 VkDescriptorType				descriptorType,
142 																	 const VkDescriptorImageInfo*	pImageInfo,
143 																	 const VkDescriptorBufferInfo*	pBufferInfo,
144 																	 const VkBufferView*			pTexelBufferView);
145 
146 	DescriptorSetUpdateBuilder&			copy						(VkDescriptorSet	srcSet,
147 																	 deUint32			srcBinding,
148 																	 deUint32			srcArrayElement,
149 																	 VkDescriptorSet	destSet,
150 																	 deUint32			destBinding,
151 																	 deUint32			destArrayElement,
152 																	 deUint32			count);
153 
154 	void								update						(const DeviceInterface& vk, VkDevice device) const;
155 
156 	// helpers
157 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorImageInfo * pImageInfo)158 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
159 																	 const Location&				destLocation,
160 																	 VkDescriptorType				descriptorType,
161 																	 const VkDescriptorImageInfo*	pImageInfo)
162 	{
163 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, pImageInfo, DE_NULL, DE_NULL);
164 	}
165 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorBufferInfo * pBufferInfo)166 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
167 																	 const Location&				destLocation,
168 																	 VkDescriptorType				descriptorType,
169 																	 const VkDescriptorBufferInfo*	pBufferInfo)
170 	{
171 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
172 	}
173 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkBufferView * pTexelBufferView)174 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
175 																	 const Location&				destLocation,
176 																	 VkDescriptorType				descriptorType,
177 																	 const VkBufferView*			pTexelBufferView)
178 	{
179 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
180 	}
181 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorImageInfo * pImageInfo)182 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
183 																	 const Location&				destLocation,
184 																	 VkDescriptorType				descriptorType,
185 																	 deUint32						numDescriptors,
186 																	 const VkDescriptorImageInfo*	pImageInfo)
187 	{
188 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, pImageInfo, DE_NULL, DE_NULL);
189 	}
190 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorBufferInfo * pBufferInfo)191 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
192 																	 const Location&				destLocation,
193 																	 VkDescriptorType				descriptorType,
194 																	 deUint32						numDescriptors,
195 																	 const VkDescriptorBufferInfo*	pBufferInfo)
196 	{
197 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
198 	}
199 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkBufferView * pTexelBufferView)200 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
201 																	 const Location&				destLocation,
202 																	 VkDescriptorType				descriptorType,
203 																	 deUint32						numDescriptors,
204 																	 const VkBufferView*			pTexelBufferView)
205 	{
206 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
207 	}
208 
copySingle(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation)209 	inline DescriptorSetUpdateBuilder&	copySingle					(VkDescriptorSet	srcSet,
210 																	 const Location&	srcLocation,
211 																	 VkDescriptorSet	destSet,
212 																	 const Location&	destLocation)
213 	{
214 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u);
215 	}
216 
copyArray(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation,deUint32 count)217 	inline DescriptorSetUpdateBuilder&	copyArray					(VkDescriptorSet	srcSet,
218 																	 const Location&	srcLocation,
219 																	 VkDescriptorSet	destSet,
220 																	 const Location&	destLocation,
221 																	 deUint32			count)
222 	{
223 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, count);
224 	}
225 
226 private:
227 										DescriptorSetUpdateBuilder	(const DescriptorSetUpdateBuilder&); // delete
228 	DescriptorSetUpdateBuilder&			operator=					(const DescriptorSetUpdateBuilder&); // delete
229 
230 	struct WriteDescriptorInfo
231 	{
232 		std::vector<VkDescriptorImageInfo>	imageInfos;
233 		std::vector<VkDescriptorBufferInfo>	bufferInfos;
234 		std::vector<VkBufferView>			texelBufferViews;
235 	};
236 
237 	std::vector<WriteDescriptorInfo>	m_writeDescriptorInfos;
238 
239 	std::vector<VkWriteDescriptorSet>	m_writes;
240 	std::vector<VkCopyDescriptorSet>	m_copies;
241 };
242 
243 } // vk
244 
245 #endif // _VKBUILDERUTIL_HPP
246