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)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 {
58 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
59 	{
60 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
61 		DE_NULL,									// const void*             pNext;
62 		renderPass,									// VkRenderPass            renderPass;
63 		framebuffer,								// VkFramebuffer           framebuffer;
64 		renderArea,									// VkRect2D                renderArea;
65 		clearValueCount,							// deUint32                clearValueCount;
66 		clearValues,								// const VkClearValue*     pClearValues;
67 	};
68 
69 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
70 }
71 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkClearValue & clearValue,const VkSubpassContents contents)72 void beginRenderPass (const DeviceInterface&	vk,
73 					  const VkCommandBuffer		commandBuffer,
74 					  const VkRenderPass		renderPass,
75 					  const VkFramebuffer		framebuffer,
76 					  const VkRect2D&			renderArea,
77 					  const VkClearValue&		clearValue,
78 					  const VkSubpassContents	contents)
79 {
80 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, 1u, &clearValue, contents);
81 }
82 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::Vec4 & clearColor,const VkSubpassContents contents)83 void beginRenderPass (const DeviceInterface&	vk,
84 					  const VkCommandBuffer		commandBuffer,
85 					  const VkRenderPass		renderPass,
86 					  const VkFramebuffer		framebuffer,
87 					  const VkRect2D&			renderArea,
88 					  const tcu::Vec4&			clearColor,
89 					  const VkSubpassContents	contents)
90 {
91 	const VkClearValue clearValue = makeClearValueColor(clearColor);
92 
93 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
94 }
95 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const tcu::UVec4 & clearColor,const VkSubpassContents contents)96 void beginRenderPass (const DeviceInterface&	vk,
97 					  const VkCommandBuffer		commandBuffer,
98 					  const VkRenderPass		renderPass,
99 					  const VkFramebuffer		framebuffer,
100 					  const VkRect2D&			renderArea,
101 					  const tcu::UVec4&			clearColor,
102 					  const VkSubpassContents	contents)
103 {
104 	const VkClearValue clearValue = makeClearValueColorU32(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
105 
106 	beginRenderPass(vk, commandBuffer, renderPass, framebuffer, renderArea, clearValue, contents);
107 }
108 
beginRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer,const VkRenderPass renderPass,const VkFramebuffer framebuffer,const VkRect2D & renderArea,const VkSubpassContents contents)109 void beginRenderPass (const DeviceInterface&	vk,
110 					  const VkCommandBuffer		commandBuffer,
111 					  const VkRenderPass		renderPass,
112 					  const VkFramebuffer		framebuffer,
113 					  const VkRect2D&			renderArea,
114 					  const VkSubpassContents	contents)
115 {
116 	const VkRenderPassBeginInfo renderPassBeginInfo =
117 	{
118 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
119 		DE_NULL,									// const void*             pNext;
120 		renderPass,									// VkRenderPass            renderPass;
121 		framebuffer,								// VkFramebuffer           framebuffer;
122 		renderArea,									// VkRect2D                renderArea;
123 		0u,											// deUint32                clearValueCount;
124 		DE_NULL,									// const VkClearValue*     pClearValues;
125 	};
126 
127 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
128 }
129 
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)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 float				clearDepth,
137 					  const deUint32			clearStencil,
138 					  const VkSubpassContents	contents)
139 {
140 	const VkClearValue			clearValues[]		=
141 	{
142 		makeClearValueColor(clearColor),						// attachment 0
143 		makeClearValueDepthStencil(clearDepth, clearStencil),	// attachment 1
144 	};
145 
146 	const VkRenderPassBeginInfo	renderPassBeginInfo	=
147 	{
148 		VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,	// VkStructureType         sType;
149 		DE_NULL,									// const void*             pNext;
150 		renderPass,									// VkRenderPass            renderPass;
151 		framebuffer,								// VkFramebuffer           framebuffer;
152 		renderArea,									// VkRect2D                renderArea;
153 		DE_LENGTH_OF_ARRAY(clearValues),			// deUint32                clearValueCount;
154 		clearValues,								// const VkClearValue*     pClearValues;
155 	};
156 
157 	vk.cmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, contents);
158 }
159 
endRenderPass(const DeviceInterface & vk,const VkCommandBuffer commandBuffer)160 void endRenderPass (const DeviceInterface&	vk,
161 					const VkCommandBuffer	commandBuffer)
162 {
163 	vk.cmdEndRenderPass(commandBuffer);
164 }
165 
submitCommandsAndWait(const DeviceInterface & vk,const VkDevice device,const VkQueue queue,const VkCommandBuffer commandBuffer,const bool useDeviceGroups,const deUint32 deviceMask)166 void submitCommandsAndWait (const DeviceInterface&	vk,
167 							const VkDevice			device,
168 							const VkQueue			queue,
169 							const VkCommandBuffer	commandBuffer,
170 							const bool				useDeviceGroups,
171 							const deUint32			deviceMask)
172 {
173 	const Unique<VkFence>	fence					(createFence(vk, device));
174 
175 	VkDeviceGroupSubmitInfo	deviceGroupSubmitInfo	=
176 	{
177 		VK_STRUCTURE_TYPE_DEVICE_GROUP_SUBMIT_INFO_KHR,	//	VkStructureType		sType;
178 		DE_NULL,										//	const void*			pNext;
179 		0u,												//	deUint32			waitSemaphoreCount;
180 		DE_NULL,										//	const deUint32*		pWaitSemaphoreDeviceIndices;
181 		1u,												//	deUint32			commandBufferCount;
182 		&deviceMask,									//	const deUint32*		pCommandBufferDeviceMasks;
183 		0u,												//	deUint32			signalSemaphoreCount;
184 		DE_NULL,										//	const deUint32*		pSignalSemaphoreDeviceIndices;
185 	};
186 
187 	const VkSubmitInfo		submitInfo				=
188 	{
189 		VK_STRUCTURE_TYPE_SUBMIT_INFO,						// VkStructureType				sType;
190 		useDeviceGroups ? &deviceGroupSubmitInfo : DE_NULL,	// const void*					pNext;
191 		0u,													// deUint32						waitSemaphoreCount;
192 		DE_NULL,											// const VkSemaphore*			pWaitSemaphores;
193 		(const VkPipelineStageFlags*)DE_NULL,				// const VkPipelineStageFlags*	pWaitDstStageMask;
194 		1u,													// deUint32						commandBufferCount;
195 		&commandBuffer,										// const VkCommandBuffer*		pCommandBuffers;
196 		0u,													// deUint32						signalSemaphoreCount;
197 		DE_NULL,											// const VkSemaphore*			pSignalSemaphores;
198 	};
199 
200 	VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
201 	VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
202 }
203 
204 } // vk
205