• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, VkDescriptorSetLayoutCreateFlags extraFlags = 0) 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 										/* DescriptorSetUpdateBuilder	(const DescriptorSetUpdateBuilder&); // do not delete */
137 
138 	DescriptorSetUpdateBuilder&			write						(VkDescriptorSet				destSet,
139 																	 deUint32						destBinding,
140 																	 deUint32						destArrayElement,
141 																	 deUint32						count,
142 																	 VkDescriptorType				descriptorType,
143 																	 const VkDescriptorImageInfo*	pImageInfo,
144 																	 const VkDescriptorBufferInfo*	pBufferInfo,
145 																	 const VkBufferView*			pTexelBufferView);
146 
147 	DescriptorSetUpdateBuilder&			copy						(VkDescriptorSet	srcSet,
148 																	 deUint32			srcBinding,
149 																	 deUint32			srcArrayElement,
150 																	 VkDescriptorSet	destSet,
151 																	 deUint32			destBinding,
152 																	 deUint32			destArrayElement,
153 																	 deUint32			count);
154 
155 	void								update						(const DeviceInterface& vk, VkDevice device) const;
156 	void								updateWithPush				(const DeviceInterface& vk, VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, deUint32 setIdx) const;
157 
158 	// helpers
159 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorImageInfo * pImageInfo)160 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
161 																	 const Location&				destLocation,
162 																	 VkDescriptorType				descriptorType,
163 																	 const VkDescriptorImageInfo*	pImageInfo)
164 	{
165 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, pImageInfo, DE_NULL, DE_NULL);
166 	}
167 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorBufferInfo * pBufferInfo)168 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
169 																	 const Location&				destLocation,
170 																	 VkDescriptorType				descriptorType,
171 																	 const VkDescriptorBufferInfo*	pBufferInfo)
172 	{
173 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
174 	}
175 
writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkBufferView * pTexelBufferView)176 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
177 																	 const Location&				destLocation,
178 																	 VkDescriptorType				descriptorType,
179 																	 const VkBufferView*			pTexelBufferView)
180 	{
181 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
182 	}
183 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorImageInfo * pImageInfo)184 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
185 																	 const Location&				destLocation,
186 																	 VkDescriptorType				descriptorType,
187 																	 deUint32						numDescriptors,
188 																	 const VkDescriptorImageInfo*	pImageInfo)
189 	{
190 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, pImageInfo, DE_NULL, DE_NULL);
191 	}
192 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkDescriptorBufferInfo * pBufferInfo)193 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
194 																	 const Location&				destLocation,
195 																	 VkDescriptorType				descriptorType,
196 																	 deUint32						numDescriptors,
197 																	 const VkDescriptorBufferInfo*	pBufferInfo)
198 	{
199 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
200 	}
201 
writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,deUint32 numDescriptors,const VkBufferView * pTexelBufferView)202 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
203 																	 const Location&				destLocation,
204 																	 VkDescriptorType				descriptorType,
205 																	 deUint32						numDescriptors,
206 																	 const VkBufferView*			pTexelBufferView)
207 	{
208 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
209 	}
210 
copySingle(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation)211 	inline DescriptorSetUpdateBuilder&	copySingle					(VkDescriptorSet	srcSet,
212 																	 const Location&	srcLocation,
213 																	 VkDescriptorSet	destSet,
214 																	 const Location&	destLocation)
215 	{
216 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u);
217 	}
218 
copyArray(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation,deUint32 count)219 	inline DescriptorSetUpdateBuilder&	copyArray					(VkDescriptorSet	srcSet,
220 																	 const Location&	srcLocation,
221 																	 VkDescriptorSet	destSet,
222 																	 const Location&	destLocation,
223 																	 deUint32			count)
224 	{
225 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, count);
226 	}
227 
228 private:
229 	DescriptorSetUpdateBuilder&			operator=					(const DescriptorSetUpdateBuilder&); // delete
230 
231 	struct WriteDescriptorInfo
232 	{
233 		std::vector<VkDescriptorImageInfo>	imageInfos;
234 		std::vector<VkDescriptorBufferInfo>	bufferInfos;
235 		std::vector<VkBufferView>			texelBufferViews;
236 	};
237 
238 	std::vector<WriteDescriptorInfo>	m_writeDescriptorInfos;
239 
240 	std::vector<VkWriteDescriptorSet>	m_writes;
241 	std::vector<VkCopyDescriptorSet>	m_copies;
242 };
243 
244 } // vk
245 
246 #endif // _VKBUILDERUTIL_HPP
247