• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2018 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Utilities for commonly used command tasks
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vkCmdUtil.hpp"
25 #include "vkDefs.hpp"
26 #include "vkRefUtil.hpp"
27 #include "vkTypeUtil.hpp"
28 
29 namespace vk
30 {
31 
beginCommandBuffer(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,VkCommandBufferUsageFlags flags)32 void beginCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer, VkCommandBufferUsageFlags flags)
33 {
34 	const VkCommandBufferBeginInfo commandBufBeginParams =
35 	{
36 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,	// VkStructureType                  sType;
37 		DE_NULL,										// const void*                      pNext;
38 		flags,											// VkCommandBufferUsageFlags        flags;
39 		(const VkCommandBufferInheritanceInfo*)DE_NULL,
40 	};
41 	VK_CHECK(vk.beginCommandBuffer(commandBuffer, &commandBufBeginParams));
42 }
43 
endCommandBuffer(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)44 void endCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
45 {
46 	VK_CHECK(vk.endCommandBuffer(commandBuffer));
47 }
48 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const deUint32 clearValueCount,const VkClearValue * clearValues,const VkSubpassContents contents,const void * pNext)49 void beginRenderPass (const DeviceInterface&	vk,
50 					  const VkCommandBuffer		commandBuffer,
51 					  const VkRenderPass		renderPass,
52 					  const VkFramebuffer		framebuffer,
53 					  const VkRect2D&			renderArea,
54 					  const deUint32			clearValueCount,
55 					  const VkClearValue*		clearValues,
56 					  const VkSubpassContents	contents,
57 					  const void*				pNext)
58 {
59 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
60 	{
61 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
62 		pNext,										// const void*             pNext;
63 		renderPass,									// VkRenderPass            renderPass;
64 		framebuffer,								// VkFramebuffer           framebuffer;
65 		renderArea,									// VkRect2D                renderArea;
66 		clearValueCount,							// deUint32                clearValueCount;
67 		clearValues,								// const VkClearValue*     pClearValues;
68 	};
69 
70 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
71 }
72 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkClearValue & clearValue,const VkSubpassContents contents)73 void beginRenderPass (const DeviceInterface&	vk,
74 					  const VkCommandBuffer		commandBuffer,
75 					  const VkRenderPass		renderPass,
76 					  const VkFramebuffer		framebuffer,
77 					  const VkRect2D&			renderArea,
78 					  const VkClearValue&		clearValue,
79 					  const VkSubpassContents	contents)
80 {
81 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, 1u, &clearValue, contents);
82 }
83 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const VkSubpassContents contents)84 void beginRenderPass (const DeviceInterface&	vk,
85 					  const VkCommandBuffer		commandBuffer,
86 					  const VkRenderPass		renderPass,
87 					  const VkFramebuffer		framebuffer,
88 					  const VkRect2D&			renderArea,
89 					  const tcu::Vec4&			clearColor,
90 					  const VkSubpassContents	contents)
91 {
92 	const VkClearValue clearValue = makeClearValueColor(clearColor);
93 
94 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
95 }
96 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const void * pNext,const VkSubpassContents contents)97 void beginRenderPass (const DeviceInterface&	vk,
98 					  const VkCommandBuffer		commandBuffer,
99 					  const VkRenderPass		renderPass,
100 					  const VkFramebuffer		framebuffer,
101 					  const VkRect2D&			renderArea,
102 					  const tcu::Vec4&			clearColor,
103 					  const void*				pNext,
104 					  const VkSubpassContents	contents)
105 {
106 	const VkClearValue clearValue = makeClearValueColor(clearColor);
107 
108 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, 1u, &clearValue, contents, pNext);
109 }
110 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const float clearDepth,const deUint32 clearStencil,const void * pNext,const VkSubpassContents contents)111 void beginRenderPass (const DeviceInterface&	vk,
112 					  const VkCommandBuffer		commandBuffer,
113 					  const VkRenderPass		renderPass,
114 					  const VkFramebuffer		framebuffer,
115 					  const VkRect2D&			renderArea,
116 					  const tcu::Vec4&			clearColor,
117 					  const float				clearDepth,
118 					  const deUint32			clearStencil,
119 					  const void*				pNext,
120 					  const VkSubpassContents	contents)
121 {
122 	const VkClearValue			clearValues[]		=
123 	{
124 		makeClearValueColor(clearColor),						// attachment 0
125 		makeClearValueDepthStencil(clearDepth, clearStencil),	// attachment 1
126 	};
127 
128 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, DE_LENGTH_OF_ARRAY(clearValues), clearValues, contents, pNext);
129 }
130 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::UVec4 & clearColor,const VkSubpassContents contents)131 void beginRenderPass (const DeviceInterface&	vk,
132 					  const VkCommandBuffer		commandBuffer,
133 					  const VkRenderPass		renderPass,
134 					  const VkFramebuffer		framebuffer,
135 					  const VkRect2D&			renderArea,
136 					  const tcu::UVec4&			clearColor,
137 					  const VkSubpassContents	contents)
138 {
139 	const VkClearValue clearValue = makeClearValueColorU32(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
140 
141 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
142 }
143 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkSubpassContents contents)144 void beginRenderPass (const DeviceInterface&	vk,
145 					  const VkCommandBuffer		commandBuffer,
146 					  const VkRenderPass		renderPass,
147 					  const VkFramebuffer		framebuffer,
148 					  const VkRect2D&			renderArea,
149 					  const VkSubpassContents	contents)
150 {
151 	const VkRenderPassBeginInfo renderPassBeginInfo =
152 	{
153 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
154 		DE_NULL,									// const void*             pNext;
155 		renderPass,									// VkRenderPass            renderPass;
156 		framebuffer,								// VkFramebuffer           framebuffer;
157 		renderArea,									// VkRect2D                renderArea;
158 		0u,											// deUint32                clearValueCount;
159 		DE_NULL,									// const VkClearValue*     pClearValues;
160 	};
161 
162 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
163 }
164 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const float clearDepth,const deUint32 clearStencil,const VkSubpassContents contents)165 void beginRenderPass (const DeviceInterface&	vk,
166 					  const VkCommandBuffer		commandBuffer,
167 					  const VkRenderPass		renderPass,
168 					  const VkFramebuffer		framebuffer,
169 					  const VkRect2D&			renderArea,
170 					  const tcu::Vec4&			clearColor,
171 					  const float				clearDepth,
172 					  const deUint32			clearStencil,
173 					  const VkSubpassContents	contents)
174 {
175 	const VkClearValue			clearValues[]		=
176 	{
177 		makeClearValueColor(clearColor),						// attachment 0
178 		makeClearValueDepthStencil(clearDepth, clearStencil),	// attachment 1
179 	};
180 
181 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
182 	{
183 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
184 		DE_NULL,									// const void*             pNext;
185 		renderPass,									// VkRenderPass            renderPass;
186 		framebuffer,								// VkFramebuffer           framebuffer;
187 		renderArea,									// VkRect2D                renderArea;
188 		DE_LENGTH_OF_ARRAY(clearValues),			// deUint32                clearValueCount;
189 		clearValues,								// const VkClearValue*     pClearValues;
190 	};
191 
192 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
193 }
194 
endRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)195 void endRenderPass (const DeviceInterface&	vk,
196 					const VkCommandBuffer	commandBuffer)
197 {
198 	vk.cmdEndRenderPass(commandBuffer);
199 }
200 
beginRendering(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkImageView colorImageView,const VkRect2D & renderArea,const VkClearValue & clearValue,const VkImageLayout imageLayout,const VkAttachmentLoadOp loadOperation,VkRenderingFlagsKHR renderingFlags,const deUint32 layerCount,const deUint32 viewMask)201 void beginRendering(const DeviceInterface&		vk,
202 					const VkCommandBuffer		commandBuffer,
203 					const VkImageView			colorImageView,
204 					const VkRect2D&				renderArea,
205 					const VkClearValue&			clearValue,
206 					const VkImageLayout			imageLayout,
207 					const VkAttachmentLoadOp	loadOperation,
208 					VkRenderingFlagsKHR			renderingFlags,
209 					const deUint32				layerCount,
210 					const deUint32				viewMask)
211 {
212 	VkRenderingAttachmentInfoKHR colorAttachment
213 	{
214 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
215 		DE_NULL,												// const void*							pNext;
216 		colorImageView,											// VkImageView							imageView;
217 		imageLayout,											// VkImageLayout						imageLayout;
218 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
219 		DE_NULL,												// VkImageView							resolveImageView;
220 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
221 		loadOperation,											// VkAttachmentLoadOp					loadOp;
222 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
223 		clearValue												// VkClearValue							clearValue;
224 	};
225 
226 	VkRenderingInfoKHR renderingInfo
227 	{
228 		VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
229 		DE_NULL,
230 		renderingFlags,											// VkRenderingFlagsKHR					flags;
231 		renderArea,												// VkRect2D								renderArea;
232 		layerCount,												// deUint32								layerCount;
233 		viewMask,												// deUint32								viewMask;
234 		1u,														// deUint32								colorAttachmentCount;
235 		&colorAttachment,										// const VkRenderingAttachmentInfoKHR*	pColorAttachments;
236 		DE_NULL,												// const VkRenderingAttachmentInfoKHR*	pDepthAttachment;
237 		DE_NULL,												// const VkRenderingAttachmentInfoKHR*	pStencilAttachment;
238 	};
239 
240 	vk.cmdBeginRenderingKHR(commandBuffer, &renderingInfo);
241 }
242 
beginRendering(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkImageView colorImageView,const VkImageView depthStencilImageView,const bool useStencilAttachment,const VkRect2D & renderArea,const VkClearValue & clearColorValue,const VkClearValue & clearDepthValue,const VkImageLayout colorImageLayout,const VkImageLayout depthImageLayout,const VkAttachmentLoadOp loadOperation,VkRenderingFlagsKHR renderingFlags,const deUint32 layerCount,const deUint32 viewMask)243 void beginRendering(const DeviceInterface&		vk,
244 					const VkCommandBuffer		commandBuffer,
245 					const VkImageView			colorImageView,
246 					const VkImageView			depthStencilImageView,
247 					const bool					useStencilAttachment,
248 					const VkRect2D&				renderArea,
249 					const VkClearValue&			clearColorValue,
250 					const VkClearValue&			clearDepthValue,
251 					const VkImageLayout			colorImageLayout,
252 					const VkImageLayout			depthImageLayout,
253 					const VkAttachmentLoadOp	loadOperation,
254 					VkRenderingFlagsKHR			renderingFlags,
255 					const deUint32				layerCount,
256 					const deUint32				viewMask)
257 {
258 	VkRenderingAttachmentInfoKHR colorAttachment
259 	{
260 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
261 		DE_NULL,												// const void*							pNext;
262 		colorImageView,											// VkImageView							imageView;
263 		colorImageLayout,										// VkImageLayout						imageLayout;
264 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
265 		DE_NULL,												// VkImageView							resolveImageView;
266 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
267 		loadOperation,											// VkAttachmentLoadOp					loadOp;
268 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
269 		clearColorValue											// VkClearValue							clearValue;
270 	};
271 
272 	VkRenderingAttachmentInfoKHR depthStencilAttachment
273 	{
274 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
275 		DE_NULL,												// const void*							pNext;
276 		depthStencilImageView,									// VkImageView							imageView;
277 		depthImageLayout,										// VkImageLayout						imageLayout;
278 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
279 		DE_NULL,												// VkImageView							resolveImageView;
280 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
281 		loadOperation,											// VkAttachmentLoadOp					loadOp;
282 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
283 		clearDepthValue											// VkClearValue							clearValue;
284 	};
285 
286 	VkRenderingInfoKHR renderingInfo
287 	{
288 		VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
289 		DE_NULL,
290 		renderingFlags,												// VkRenderingFlagsKHR					flags;
291 		renderArea,													// VkRect2D								renderArea;
292 		layerCount,													// deUint32								layerCount;
293 		viewMask,													// deUint32								viewMask;
294 		1u,															// deUint32								colorAttachmentCount;
295 		&colorAttachment,											// const VkRenderingAttachmentInfoKHR*	pColorAttachments;
296 		&depthStencilAttachment,									// const VkRenderingAttachmentInfoKHR*	pDepthAttachment;
297 		useStencilAttachment ? &depthStencilAttachment : DE_NULL,	// const VkRenderingAttachmentInfoKHR*	pStencilAttachment;
298 	};
299 
300 	vk.cmdBeginRenderingKHR(commandBuffer, &renderingInfo);
301 }
302 
endRendering(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)303 void endRendering(const DeviceInterface&	vk,
304 				  const VkCommandBuffer		commandBuffer)
305 {
306 	vk.cmdEndRenderingKHR(commandBuffer);
307 }
308 
submitCommandsAndWait(const DeviceInterface & vk,const VkDevice device,const VkQueue queue,const VkCommandBuffer commandBuffer,const bool useDeviceGroups,const deUint32 deviceMask,const deUint32 waitSemaphoreCount,const VkSemaphore * waitSemaphores,const VkPipelineStageFlags * waitStages)309 void submitCommandsAndWait (const DeviceInterface&		vk,
310 							const VkDevice				device,
311 							const VkQueue				queue,
312 							const VkCommandBuffer		commandBuffer,
313 							const bool					useDeviceGroups,
314 							const deUint32				deviceMask,
315 							const deUint32				waitSemaphoreCount,
316 							const VkSemaphore*			waitSemaphores,
317 							const VkPipelineStageFlags*	waitStages)
318 {
319 	// For simplicity. A more complete approach can be found in vkt::sparse::submitCommandsAndWait().
320 	DE_ASSERT(!(useDeviceGroups && waitSemaphoreCount > 0u));
321 
322 	if (waitSemaphoreCount > 0u)
323 	{
324 		DE_ASSERT(waitSemaphores != nullptr);
325 		DE_ASSERT(waitStages != nullptr);
326 	}
327 
328 	const Unique<VkFence>	fence					(createFence(vk, device));
329 
330 	VkDeviceGroupSubmitInfo	deviceGroupSubmitInfo	=
331 	{
332 		VK_STRUCTURE_TYPE_DEVICE_GROUP_SUBMIT_INFO,		//	VkStructureType		sType;
333 		DE_NULL,										//	const void*			pNext;
334 		0u,												//	deUint32			waitSemaphoreCount;
335 		DE_NULL,										//	const deUint32*		pWaitSemaphoreDeviceIndices;
336 		1u,												//	deUint32			commandBufferCount;
337 		&deviceMask,									//	const deUint32*		pCommandBufferDeviceMasks;
338 		0u,												//	deUint32			signalSemaphoreCount;
339 		DE_NULL,										//	const deUint32*		pSignalSemaphoreDeviceIndices;
340 	};
341 
342 	const VkSubmitInfo		submitInfo				=
343 	{
344 		VK_STRUCTURE_TYPE_SUBMIT_INFO,						// VkStructureType				sType;
345 		useDeviceGroups ? &deviceGroupSubmitInfo : DE_NULL,	// const void*					pNext;
346 		waitSemaphoreCount,									// deUint32						waitSemaphoreCount;
347 		waitSemaphores,										// const VkSemaphore*			pWaitSemaphores;
348 		waitStages,											// const VkPipelineStageFlags*	pWaitDstStageMask;
349 		1u,													// deUint32						commandBufferCount;
350 		&commandBuffer,										// const VkCommandBuffer*		pCommandBuffers;
351 		0u,													// deUint32						signalSemaphoreCount;
352 		nullptr,											// const VkSemaphore*			pSignalSemaphores;
353 	};
354 
355 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
356 	VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
357 }
358 
359 } // vk
360