• 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 
beginSecondaryCommandBuffer(const DeviceInterface & vkd,const VkCommandBuffer cmdBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkCommandBufferUsageFlags flags)44 void beginSecondaryCommandBuffer	(const DeviceInterface&				vkd,
45 									 const VkCommandBuffer				cmdBuffer,
46 									 const VkRenderPass					renderPass,
47 									 const VkFramebuffer				framebuffer,
48 									 const VkCommandBufferUsageFlags	flags)
49 {
50 	const VkCommandBufferInheritanceInfo inheritanceInfo =
51 	{
52 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,	//	VkStructureType					sType;
53 		nullptr,											//	const void*						pNext;
54 		renderPass,											//	VkRenderPass					renderPass;
55 		0u,													//	deUint32						subpass;
56 		framebuffer,										//	VkFramebuffer					framebuffer;
57 		VK_FALSE,											//	VkBool32						occlusionQueryEnable;
58 		0u,													//	VkQueryControlFlags				queryFlags;
59 		0u,													//	VkQueryPipelineStatisticFlags	pipelineStatistics;
60 	};
61 
62 	const VkCommandBufferUsageFlags	extraFlags	= ((renderPass == DE_NULL)
63 												? static_cast<VkCommandBufferUsageFlagBits>(0)
64 												: VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT);
65 	const VkCommandBufferUsageFlags	usageFlags	= (flags | extraFlags);
66 	const VkCommandBufferBeginInfo	beginInfo	=
67 	{
68 		VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,	//	VkStructureType							sType;
69 		nullptr,										//	const void*								pNext;
70 		usageFlags,										//	VkCommandBufferUsageFlags				flags;
71 		&inheritanceInfo,								//	const VkCommandBufferInheritanceInfo*	pInheritanceInfo;
72 	};
73 
74 	vkd.beginCommandBuffer(cmdBuffer, &beginInfo);
75 }
76 
endCommandBuffer(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)77 void endCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
78 {
79 	VK_CHECK(vk.endCommandBuffer(commandBuffer));
80 }
81 
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)82 void beginRenderPass (const DeviceInterface&	vk,
83 					  const VkCommandBuffer		commandBuffer,
84 					  const VkRenderPass		renderPass,
85 					  const VkFramebuffer		framebuffer,
86 					  const VkRect2D&			renderArea,
87 					  const deUint32			clearValueCount,
88 					  const VkClearValue*		clearValues,
89 					  const VkSubpassContents	contents,
90 					  const void*				pNext)
91 {
92 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
93 	{
94 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
95 		pNext,										// const void*             pNext;
96 		renderPass,									// VkRenderPass            renderPass;
97 		framebuffer,								// VkFramebuffer           framebuffer;
98 		renderArea,									// VkRect2D                renderArea;
99 		clearValueCount,							// deUint32                clearValueCount;
100 		clearValues,								// const VkClearValue*     pClearValues;
101 	};
102 
103 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
104 }
105 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkClearValue & clearValue,const VkSubpassContents contents)106 void beginRenderPass (const DeviceInterface&	vk,
107 					  const VkCommandBuffer		commandBuffer,
108 					  const VkRenderPass		renderPass,
109 					  const VkFramebuffer		framebuffer,
110 					  const VkRect2D&			renderArea,
111 					  const VkClearValue&		clearValue,
112 					  const VkSubpassContents	contents)
113 {
114 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, 1u, &clearValue, contents);
115 }
116 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const VkSubpassContents contents)117 void beginRenderPass (const DeviceInterface&	vk,
118 					  const VkCommandBuffer		commandBuffer,
119 					  const VkRenderPass		renderPass,
120 					  const VkFramebuffer		framebuffer,
121 					  const VkRect2D&			renderArea,
122 					  const tcu::Vec4&			clearColor,
123 					  const VkSubpassContents	contents)
124 {
125 	const VkClearValue clearValue = makeClearValueColor(clearColor);
126 
127 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
128 }
129 
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)130 void beginRenderPass (const DeviceInterface&	vk,
131 					  const VkCommandBuffer		commandBuffer,
132 					  const VkRenderPass		renderPass,
133 					  const VkFramebuffer		framebuffer,
134 					  const VkRect2D&			renderArea,
135 					  const tcu::Vec4&			clearColor,
136 					  const void*				pNext,
137 					  const VkSubpassContents	contents)
138 {
139 	const VkClearValue clearValue = makeClearValueColor(clearColor);
140 
141 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, 1u, &clearValue, contents, pNext);
142 }
143 
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)144 void beginRenderPass (const DeviceInterface&	vk,
145 					  const VkCommandBuffer		commandBuffer,
146 					  const VkRenderPass		renderPass,
147 					  const VkFramebuffer		framebuffer,
148 					  const VkRect2D&			renderArea,
149 					  const tcu::Vec4&			clearColor,
150 					  const float				clearDepth,
151 					  const deUint32			clearStencil,
152 					  const void*				pNext,
153 					  const VkSubpassContents	contents)
154 {
155 	const VkClearValue			clearValues[]		=
156 	{
157 		makeClearValueColor(clearColor),						// attachment 0
158 		makeClearValueDepthStencil(clearDepth, clearStencil),	// attachment 1
159 	};
160 
161 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, DE_LENGTH_OF_ARRAY(clearValues), clearValues, contents, pNext);
162 }
163 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::UVec4 & clearColor,const VkSubpassContents contents)164 void beginRenderPass (const DeviceInterface&	vk,
165 					  const VkCommandBuffer		commandBuffer,
166 					  const VkRenderPass		renderPass,
167 					  const VkFramebuffer		framebuffer,
168 					  const VkRect2D&			renderArea,
169 					  const tcu::UVec4&			clearColor,
170 					  const VkSubpassContents	contents)
171 {
172 	const VkClearValue clearValue = makeClearValueColorU32(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
173 
174 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
175 }
176 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkSubpassContents contents)177 void beginRenderPass (const DeviceInterface&	vk,
178 					  const VkCommandBuffer		commandBuffer,
179 					  const VkRenderPass		renderPass,
180 					  const VkFramebuffer		framebuffer,
181 					  const VkRect2D&			renderArea,
182 					  const VkSubpassContents	contents)
183 {
184 	const VkRenderPassBeginInfo renderPassBeginInfo =
185 	{
186 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
187 		DE_NULL,									// const void*             pNext;
188 		renderPass,									// VkRenderPass            renderPass;
189 		framebuffer,								// VkFramebuffer           framebuffer;
190 		renderArea,									// VkRect2D                renderArea;
191 		0u,											// deUint32                clearValueCount;
192 		DE_NULL,									// const VkClearValue*     pClearValues;
193 	};
194 
195 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
196 }
197 
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)198 void beginRenderPass (const DeviceInterface&	vk,
199 					  const VkCommandBuffer		commandBuffer,
200 					  const VkRenderPass		renderPass,
201 					  const VkFramebuffer		framebuffer,
202 					  const VkRect2D&			renderArea,
203 					  const tcu::Vec4&			clearColor,
204 					  const float				clearDepth,
205 					  const deUint32			clearStencil,
206 					  const VkSubpassContents	contents)
207 {
208 	const VkClearValue			clearValues[]		=
209 	{
210 		makeClearValueColor(clearColor),						// attachment 0
211 		makeClearValueDepthStencil(clearDepth, clearStencil),	// attachment 1
212 	};
213 
214 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
215 	{
216 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
217 		DE_NULL,									// const void*             pNext;
218 		renderPass,									// VkRenderPass            renderPass;
219 		framebuffer,								// VkFramebuffer           framebuffer;
220 		renderArea,									// VkRect2D                renderArea;
221 		DE_LENGTH_OF_ARRAY(clearValues),			// deUint32                clearValueCount;
222 		clearValues,								// const VkClearValue*     pClearValues;
223 	};
224 
225 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
226 }
227 
endRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)228 void endRenderPass (const DeviceInterface&	vk,
229 					const VkCommandBuffer	commandBuffer)
230 {
231 	vk.cmdEndRenderPass(commandBuffer);
232 }
233 
234 #ifndef CTS_USES_VULKANSC
235 
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)236 void beginRendering(const DeviceInterface&		vk,
237 					const VkCommandBuffer		commandBuffer,
238 					const VkImageView			colorImageView,
239 					const VkRect2D&				renderArea,
240 					const VkClearValue&			clearValue,
241 					const VkImageLayout			imageLayout,
242 					const VkAttachmentLoadOp	loadOperation,
243 					VkRenderingFlagsKHR			renderingFlags,
244 					const deUint32				layerCount,
245 					const deUint32				viewMask)
246 {
247 	VkRenderingAttachmentInfoKHR colorAttachment
248 	{
249 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
250 		DE_NULL,												// const void*							pNext;
251 		colorImageView,											// VkImageView							imageView;
252 		imageLayout,											// VkImageLayout						imageLayout;
253 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
254 		DE_NULL,												// VkImageView							resolveImageView;
255 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
256 		loadOperation,											// VkAttachmentLoadOp					loadOp;
257 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
258 		clearValue												// VkClearValue							clearValue;
259 	};
260 
261 	VkRenderingInfoKHR renderingInfo
262 	{
263 		VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
264 		DE_NULL,
265 		renderingFlags,											// VkRenderingFlagsKHR					flags;
266 		renderArea,												// VkRect2D								renderArea;
267 		layerCount,												// deUint32								layerCount;
268 		viewMask,												// deUint32								viewMask;
269 		1u,														// deUint32								colorAttachmentCount;
270 		&colorAttachment,										// const VkRenderingAttachmentInfoKHR*	pColorAttachments;
271 		DE_NULL,												// const VkRenderingAttachmentInfoKHR*	pDepthAttachment;
272 		DE_NULL,												// const VkRenderingAttachmentInfoKHR*	pStencilAttachment;
273 	};
274 
275 	vk.cmdBeginRendering(commandBuffer, &renderingInfo);
276 }
277 
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)278 void beginRendering(const DeviceInterface&		vk,
279 					const VkCommandBuffer		commandBuffer,
280 					const VkImageView			colorImageView,
281 					const VkImageView			depthStencilImageView,
282 					const bool					useStencilAttachment,
283 					const VkRect2D&				renderArea,
284 					const VkClearValue&			clearColorValue,
285 					const VkClearValue&			clearDepthValue,
286 					const VkImageLayout			colorImageLayout,
287 					const VkImageLayout			depthImageLayout,
288 					const VkAttachmentLoadOp	loadOperation,
289 					VkRenderingFlagsKHR			renderingFlags,
290 					const deUint32				layerCount,
291 					const deUint32				viewMask)
292 {
293 	VkRenderingAttachmentInfoKHR colorAttachment
294 	{
295 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
296 		DE_NULL,												// const void*							pNext;
297 		colorImageView,											// VkImageView							imageView;
298 		colorImageLayout,										// VkImageLayout						imageLayout;
299 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
300 		DE_NULL,												// VkImageView							resolveImageView;
301 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
302 		loadOperation,											// VkAttachmentLoadOp					loadOp;
303 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
304 		clearColorValue											// VkClearValue							clearValue;
305 	};
306 
307 	VkRenderingAttachmentInfoKHR depthStencilAttachment
308 	{
309 		VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,		// VkStructureType						sType;
310 		DE_NULL,												// const void*							pNext;
311 		depthStencilImageView,									// VkImageView							imageView;
312 		depthImageLayout,										// VkImageLayout						imageLayout;
313 		VK_RESOLVE_MODE_NONE,									// VkResolveModeFlagBits				resolveMode;
314 		DE_NULL,												// VkImageView							resolveImageView;
315 		VK_IMAGE_LAYOUT_UNDEFINED,								// VkImageLayout						resolveImageLayout;
316 		loadOperation,											// VkAttachmentLoadOp					loadOp;
317 		VK_ATTACHMENT_STORE_OP_STORE,							// VkAttachmentStoreOp					storeOp;
318 		clearDepthValue											// VkClearValue							clearValue;
319 	};
320 
321 	VkRenderingInfoKHR renderingInfo
322 	{
323 		VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
324 		DE_NULL,
325 		renderingFlags,												// VkRenderingFlagsKHR					flags;
326 		renderArea,													// VkRect2D								renderArea;
327 		layerCount,													// deUint32								layerCount;
328 		viewMask,													// deUint32								viewMask;
329 		1u,															// deUint32								colorAttachmentCount;
330 		&colorAttachment,											// const VkRenderingAttachmentInfoKHR*	pColorAttachments;
331 		&depthStencilAttachment,									// const VkRenderingAttachmentInfoKHR*	pDepthAttachment;
332 		useStencilAttachment ? &depthStencilAttachment : DE_NULL,	// const VkRenderingAttachmentInfoKHR*	pStencilAttachment;
333 	};
334 
335 	vk.cmdBeginRendering(commandBuffer, &renderingInfo);
336 }
337 
endRendering(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)338 void endRendering(const DeviceInterface&	vk,
339 				  const VkCommandBuffer		commandBuffer)
340 {
341 	vk.cmdEndRendering(commandBuffer);
342 }
343 
344 #endif // CTS_USES_VULKANSC
345 
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)346 void submitCommandsAndWait (const DeviceInterface&		vk,
347 							const VkDevice				device,
348 							const VkQueue				queue,
349 							const VkCommandBuffer		commandBuffer,
350 							const bool					useDeviceGroups,
351 							const deUint32				deviceMask,
352 							const deUint32				waitSemaphoreCount,
353 							const VkSemaphore*			waitSemaphores,
354 							const VkPipelineStageFlags*	waitStages)
355 {
356 	// For simplicity. A more complete approach can be found in vkt::sparse::submitCommandsAndWait().
357 	DE_ASSERT(!(useDeviceGroups && waitSemaphoreCount > 0u));
358 
359 	if (waitSemaphoreCount > 0u)
360 	{
361 		DE_ASSERT(waitSemaphores != nullptr);
362 		DE_ASSERT(waitStages != nullptr);
363 	}
364 
365 	const Unique<VkFence>	fence					(createFence(vk, device));
366 
367 	VkDeviceGroupSubmitInfo	deviceGroupSubmitInfo	=
368 	{
369 		VK_STRUCTURE_TYPE_DEVICE_GROUP_SUBMIT_INFO,		//	VkStructureType		sType;
370 		DE_NULL,										//	const void*			pNext;
371 		0u,												//	deUint32			waitSemaphoreCount;
372 		DE_NULL,										//	const deUint32*		pWaitSemaphoreDeviceIndices;
373 		1u,												//	deUint32			commandBufferCount;
374 		&deviceMask,									//	const deUint32*		pCommandBufferDeviceMasks;
375 		0u,												//	deUint32			signalSemaphoreCount;
376 		DE_NULL,										//	const deUint32*		pSignalSemaphoreDeviceIndices;
377 	};
378 
379 	const VkSubmitInfo		submitInfo				=
380 	{
381 		VK_STRUCTURE_TYPE_SUBMIT_INFO,						// VkStructureType				sType;
382 		useDeviceGroups ? &deviceGroupSubmitInfo : DE_NULL,	// const void*					pNext;
383 		waitSemaphoreCount,									// deUint32						waitSemaphoreCount;
384 		waitSemaphores,										// const VkSemaphore*			pWaitSemaphores;
385 		waitStages,											// const VkPipelineStageFlags*	pWaitDstStageMask;
386 		1u,													// deUint32						commandBufferCount;
387 		&commandBuffer,										// const VkCommandBuffer*		pCommandBuffers;
388 		0u,													// deUint32						signalSemaphoreCount;
389 		nullptr,											// const VkSemaphore*			pSignalSemaphores;
390 	};
391 
392 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
393 	VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
394 }
395 
396 } // vk
397