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