Ignore A channel in EGL RGB5_A1 clear color tests am: 9da1b45a8e am: 4e33c51c88 am...
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / compute / vktComputeTestsUtil.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group 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 Compute tests utility classes
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktComputeTestsUtil.hpp"
25 #include "vkQueryUtil.hpp"
26 #include "vkTypeUtil.hpp"
27
28 using namespace vk;
29
30 namespace vkt
31 {
32 namespace compute
33 {
34
35 Buffer::Buffer (const DeviceInterface&          vk,
36                                 const VkDevice                          device,
37                                 Allocator&                                      allocator,
38                                 const VkBufferCreateInfo&       bufferCreateInfo,
39                                 const MemoryRequirement         memoryRequirement)
40 {
41         m_buffer = createBuffer(vk, device, &bufferCreateInfo);
42         m_allocation = allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement);
43         VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
44 }
45
46 Image::Image (const DeviceInterface&    vk,
47                           const VkDevice                        device,
48                           Allocator&                            allocator,
49                           const VkImageCreateInfo&      imageCreateInfo,
50                           const MemoryRequirement       memoryRequirement)
51 {
52         m_image = createImage(vk, device, &imageCreateInfo);
53         m_allocation = allocator.allocate(getImageMemoryRequirements(vk, device, *m_image), memoryRequirement);
54         VK_CHECK(vk.bindImageMemory(device, *m_image, m_allocation->getMemory(), m_allocation->getOffset()));
55 }
56
57 VkBufferCreateInfo makeBufferCreateInfo (const VkDeviceSize                     bufferSize,
58                                                                                  const VkBufferUsageFlags       usage)
59 {
60         const VkBufferCreateInfo bufferCreateInfo =
61         {
62                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,   // VkStructureType              sType;
63                 DE_NULL,                                                                // const void*                  pNext;
64                 0u,                                                                             // VkBufferCreateFlags  flags;
65                 bufferSize,                                                             // VkDeviceSize                 size;
66                 usage,                                                                  // VkBufferUsageFlags   usage;
67                 VK_SHARING_MODE_EXCLUSIVE,                              // VkSharingMode                sharingMode;
68                 0u,                                                                             // deUint32                             queueFamilyIndexCount;
69                 DE_NULL,                                                                // const deUint32*              pQueueFamilyIndices;
70         };
71         return bufferCreateInfo;
72 }
73
74 VkBufferImageCopy makeBufferImageCopy (const VkExtent3D extent,
75                                                                            const deUint32       arraySize)
76 {
77         const VkBufferImageCopy copyParams =
78         {
79                 0ull,                                                                                                                                           //      VkDeviceSize                            bufferOffset;
80                 0u,                                                                                                                                                     //      deUint32                                        bufferRowLength;
81                 0u,                                                                                                                                                     //      deUint32                                        bufferImageHeight;
82                 makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, arraySize),       //      VkImageSubresourceLayers        imageSubresource;
83                 makeOffset3D(0, 0, 0),                                                                                                          //      VkOffset3D                                      imageOffset;
84                 extent,                                                                                                                                         //      VkExtent3D                                      imageExtent;
85         };
86         return copyParams;
87 }
88
89 Move<VkCommandPool> makeCommandPool (const DeviceInterface& vk, const VkDevice device, const deUint32 queueFamilyIndex)
90 {
91         const VkCommandPoolCreateInfo commandPoolParams =
92         {
93                 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,                     // VkStructureType                      sType;
94                 DE_NULL,                                                                                        // const void*                          pNext;
95                 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,        // VkCommandPoolCreateFlags     flags;
96                 queueFamilyIndex,                                                                       // deUint32                                     queueFamilyIndex;
97         };
98         return createCommandPool(vk, device, &commandPoolParams);
99 }
100
101 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface&               vk,
102                                                                                    const VkDevice                               device)
103 {
104         const VkPipelineLayoutCreateInfo pipelineLayoutParams =
105         {
106                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
107                 DE_NULL,                                                                                        // const void*                                          pNext;
108                 0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
109                 0u,                                                                                                     // deUint32                                                     setLayoutCount;
110                 DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
111                 0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
112                 DE_NULL,                                                                                        // const VkPushConstantRange*           pPushConstantRanges;
113         };
114         return createPipelineLayout(vk, device, &pipelineLayoutParams);
115 }
116
117 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface&               vk,
118                                                                                    const VkDevice                               device,
119                                                                                    const VkDescriptorSetLayout  descriptorSetLayout)
120 {
121         const VkPipelineLayoutCreateInfo pipelineLayoutParams =
122         {
123                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
124                 DE_NULL,                                                                                        // const void*                                          pNext;
125                 0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
126                 1u,                                                                                                     // deUint32                                                     setLayoutCount;
127                 &descriptorSetLayout,                                                           // const VkDescriptorSetLayout*         pSetLayouts;
128                 0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
129                 DE_NULL,                                                                                        // const VkPushConstantRange*           pPushConstantRanges;
130         };
131         return createPipelineLayout(vk, device, &pipelineLayoutParams);
132 }
133
134 Move<VkPipeline> makeComputePipeline (const DeviceInterface&    vk,
135                                                                           const VkDevice                        device,
136                                                                           const VkPipelineLayout        pipelineLayout,
137                                                                           const VkShaderModule          shaderModule)
138 {
139         const VkPipelineShaderStageCreateInfo pipelineShaderStageParams =
140         {
141                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    // VkStructureType                                              sType;
142                 DE_NULL,                                                                                                // const void*                                                  pNext;
143                 0u,                                                                                                             // VkPipelineShaderStageCreateFlags             flags;
144                 VK_SHADER_STAGE_COMPUTE_BIT,                                                    // VkShaderStageFlagBits                                stage;
145                 shaderModule,                                                                                   // VkShaderModule                                               module;
146                 "main",                                                                                                 // const char*                                                  pName;
147                 DE_NULL,                                                                                                // const VkSpecializationInfo*                  pSpecializationInfo;
148         };
149         const VkComputePipelineCreateInfo pipelineCreateInfo =
150         {
151                 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,         // VkStructureType                                      sType;
152                 DE_NULL,                                                                                        // const void*                                          pNext;
153                 0u,                                                                                                     // VkPipelineCreateFlags                        flags;
154                 pipelineShaderStageParams,                                                      // VkPipelineShaderStageCreateInfo      stage;
155                 pipelineLayout,                                                                         // VkPipelineLayout                                     layout;
156                 DE_NULL,                                                                                        // VkPipeline                                           basePipelineHandle;
157                 0,                                                                                                      // deInt32                                                      basePipelineIndex;
158         };
159         return createComputePipeline(vk, device, DE_NULL , &pipelineCreateInfo);
160 }
161
162 Move<VkBufferView> makeBufferView (const DeviceInterface&       vk,
163                                                                    const VkDevice                       vkDevice,
164                                                                    const VkBuffer                       buffer,
165                                                                    const VkFormat                       format,
166                                                                    const VkDeviceSize           offset,
167                                                                    const VkDeviceSize           size)
168 {
169         const VkBufferViewCreateInfo bufferViewParams =
170         {
171                 VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      // VkStructureType                      sType;
172                 DE_NULL,                                                                        // const void*                          pNext;
173                 0u,                                                                                     // VkBufferViewCreateFlags      flags;
174                 buffer,                                                                         // VkBuffer                                     buffer;
175                 format,                                                                         // VkFormat                                     format;
176                 offset,                                                                         // VkDeviceSize                         offset;
177                 size,                                                                           // VkDeviceSize                         range;
178         };
179         return createBufferView(vk, vkDevice, &bufferViewParams);
180 }
181
182 Move<VkImageView> makeImageView (const DeviceInterface&                 vk,
183                                                                  const VkDevice                                 vkDevice,
184                                                                  const VkImage                                  image,
185                                                                  const VkImageViewType                  imageViewType,
186                                                                  const VkFormat                                 format,
187                                                                  const VkImageSubresourceRange  subresourceRange)
188 {
189         const VkImageViewCreateInfo imageViewParams =
190         {
191                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
192                 DE_NULL,                                                                                // const void*                          pNext;
193                 0u,                                                                                             // VkImageViewCreateFlags       flags;
194                 image,                                                                                  // VkImage                                      image;
195                 imageViewType,                                                                  // VkImageViewType                      viewType;
196                 format,                                                                                 // VkFormat                                     format;
197                 makeComponentMappingRGBA(),                                             // VkComponentMapping           components;
198                 subresourceRange,                                                               // VkImageSubresourceRange      subresourceRange;
199         };
200         return createImageView(vk, vkDevice, &imageViewParams);
201 }
202
203 Move<VkDescriptorSet> makeDescriptorSet (const DeviceInterface&                 vk,
204                                                                                  const VkDevice                                 device,
205                                                                                  const VkDescriptorPool                 descriptorPool,
206                                                                                  const VkDescriptorSetLayout    setLayout)
207 {
208         const VkDescriptorSetAllocateInfo allocateParams =
209         {
210                 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,         // VkStructureType                              sType;
211                 DE_NULL,                                                                                        // const void*                                  pNext;
212                 descriptorPool,                                                                         // VkDescriptorPool                             descriptorPool;
213                 1u,                                                                                                     // deUint32                                             setLayoutCount;
214                 &setLayout,                                                                                     // const VkDescriptorSetLayout* pSetLayouts;
215         };
216         return allocateDescriptorSet(vk, device, &allocateParams);
217 }
218
219 VkBufferMemoryBarrier makeBufferMemoryBarrier (const VkAccessFlags      srcAccessMask,
220                                                                                            const VkAccessFlags  dstAccessMask,
221                                                                                            const VkBuffer               buffer,
222                                                                                            const VkDeviceSize   offset,
223                                                                                            const VkDeviceSize   bufferSizeBytes)
224 {
225         const VkBufferMemoryBarrier barrier =
226         {
227                 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,        // VkStructureType      sType;
228                 DE_NULL,                                                                        // const void*          pNext;
229                 srcAccessMask,                                                          // VkAccessFlags        srcAccessMask;
230                 dstAccessMask,                                                          // VkAccessFlags        dstAccessMask;
231                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     srcQueueFamilyIndex;
232                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     destQueueFamilyIndex;
233                 buffer,                                                                         // VkBuffer                     buffer;
234                 offset,                                                                         // VkDeviceSize         offset;
235                 bufferSizeBytes,                                                        // VkDeviceSize         size;
236         };
237         return barrier;
238 }
239
240 VkImageMemoryBarrier makeImageMemoryBarrier     (const VkAccessFlags                    srcAccessMask,
241                                                                                          const VkAccessFlags                    dstAccessMask,
242                                                                                          const VkImageLayout                    oldLayout,
243                                                                                          const VkImageLayout                    newLayout,
244                                                                                          const VkImage                                  image,
245                                                                                          const VkImageSubresourceRange  subresourceRange)
246 {
247         const VkImageMemoryBarrier barrier =
248         {
249                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // VkStructureType                      sType;
250                 DE_NULL,                                                                                // const void*                          pNext;
251                 srcAccessMask,                                                                  // VkAccessFlags                        outputMask;
252                 dstAccessMask,                                                                  // VkAccessFlags                        inputMask;
253                 oldLayout,                                                                              // VkImageLayout                        oldLayout;
254                 newLayout,                                                                              // VkImageLayout                        newLayout;
255                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     srcQueueFamilyIndex;
256                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     destQueueFamilyIndex;
257                 image,                                                                                  // VkImage                                      image;
258                 subresourceRange,                                                               // VkImageSubresourceRange      subresourceRange;
259         };
260         return barrier;
261 }
262
263 void beginCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
264 {
265         const VkCommandBufferBeginInfo commandBufBeginParams =
266         {
267                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
268                 DE_NULL,                                                                                // const void*                                          pNext;
269                 0u,                                                                                             // VkCommandBufferUsageFlags            flags;
270                 (const VkCommandBufferInheritanceInfo*)DE_NULL,
271         };
272         VK_CHECK(vk.beginCommandBuffer(commandBuffer, &commandBufBeginParams));
273 }
274
275 void endCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
276 {
277         VK_CHECK(vk.endCommandBuffer(commandBuffer));
278 }
279
280 void submitCommandsAndWait (const DeviceInterface&      vk,
281                                                         const VkDevice                  device,
282                                                         const VkQueue                   queue,
283                                                         const VkCommandBuffer   commandBuffer)
284 {
285         const Unique<VkFence> fence(createFence(vk, device));
286
287         const VkSubmitInfo submitInfo =
288         {
289                 VK_STRUCTURE_TYPE_SUBMIT_INFO,          // VkStructureType                      sType;
290                 DE_NULL,                                                        // const void*                          pNext;
291                 0u,                                                                     // deUint32                                     waitSemaphoreCount;
292                 DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
293                 (const VkPipelineStageFlags*)DE_NULL,
294                 1u,                                                                     // deUint32                                     commandBufferCount;
295                 &commandBuffer,                                         // const VkCommandBuffer*       pCommandBuffers;
296                 0u,                                                                     // deUint32                                     signalSemaphoreCount;
297                 DE_NULL,                                                        // const VkSemaphore*           pSignalSemaphores;
298         };
299
300         VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
301         VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
302 }
303
304 } // compute
305 } // vkt