Extend device feature and limit validation in dEQP-VK.api.info
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / compute / vktComputeTestsUtil.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 Mobica Ltd.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and/or associated documentation files (the
9  * "Materials"), to deal in the Materials without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Materials, and to
12  * permit persons to whom the Materials are furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice(s) and this permission notice shall be included
16  * in all copies or substantial portions of the Materials.
17  *
18  * The Materials are Confidential Information as defined by the
19  * Khronos Membership Agreement until designated non-confidential by Khronos,
20  * at which point this condition clause shall be removed.
21  *
22  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
29  *
30  *//*!
31  * \file
32  * \brief Compute tests utility classes
33  *//*--------------------------------------------------------------------*/
34
35 #include "vktComputeTestsUtil.hpp"
36 #include "vkQueryUtil.hpp"
37 #include "vkTypeUtil.hpp"
38
39 using namespace vk;
40
41 namespace vkt
42 {
43 namespace compute
44 {
45
46 Buffer::Buffer (const DeviceInterface&          vk,
47                                 const VkDevice                          device,
48                                 Allocator&                                      allocator,
49                                 const VkBufferCreateInfo&       bufferCreateInfo,
50                                 const MemoryRequirement         memoryRequirement)
51 {
52         m_buffer = createBuffer(vk, device, &bufferCreateInfo);
53         m_allocation = allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement);
54         VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
55 }
56
57 Image::Image (const DeviceInterface&    vk,
58                           const VkDevice                        device,
59                           Allocator&                            allocator,
60                           const VkImageCreateInfo&      imageCreateInfo,
61                           const MemoryRequirement       memoryRequirement)
62 {
63         m_image = createImage(vk, device, &imageCreateInfo);
64         m_allocation = allocator.allocate(getImageMemoryRequirements(vk, device, *m_image), memoryRequirement);
65         VK_CHECK(vk.bindImageMemory(device, *m_image, m_allocation->getMemory(), m_allocation->getOffset()));
66 }
67
68 VkBufferCreateInfo makeBufferCreateInfo (const VkDeviceSize                     bufferSize,
69                                                                                  const VkBufferUsageFlags       usage)
70 {
71         const VkBufferCreateInfo bufferCreateInfo =
72         {
73                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,   // VkStructureType              sType;
74                 DE_NULL,                                                                // const void*                  pNext;
75                 0u,                                                                             // VkBufferCreateFlags  flags;
76                 bufferSize,                                                             // VkDeviceSize                 size;
77                 usage,                                                                  // VkBufferUsageFlags   usage;
78                 VK_SHARING_MODE_EXCLUSIVE,                              // VkSharingMode                sharingMode;
79                 0u,                                                                             // deUint32                             queueFamilyIndexCount;
80                 DE_NULL,                                                                // const deUint32*              pQueueFamilyIndices;
81         };
82         return bufferCreateInfo;
83 }
84
85 VkBufferImageCopy makeBufferImageCopy (const VkExtent3D extent,
86                                                                            const deUint32       arraySize)
87 {
88         const VkBufferImageCopy copyParams =
89         {
90                 0ull,                                                                                                                                           //      VkDeviceSize                            bufferOffset;
91                 0u,                                                                                                                                                     //      deUint32                                        bufferRowLength;
92                 0u,                                                                                                                                                     //      deUint32                                        bufferImageHeight;
93                 makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, arraySize),       //      VkImageSubresourceLayers        imageSubresource;
94                 makeOffset3D(0, 0, 0),                                                                                                          //      VkOffset3D                                      imageOffset;
95                 extent,                                                                                                                                         //      VkExtent3D                                      imageExtent;
96         };
97         return copyParams;
98 }
99
100 Move<VkCommandPool> makeCommandPool (const DeviceInterface& vk, const VkDevice device, const deUint32 queueFamilyIndex)
101 {
102         const VkCommandPoolCreateInfo commandPoolParams =
103         {
104                 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,                     // VkStructureType                      sType;
105                 DE_NULL,                                                                                        // const void*                          pNext;
106                 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,        // VkCommandPoolCreateFlags     flags;
107                 queueFamilyIndex,                                                                       // deUint32                                     queueFamilyIndex;
108         };
109         return createCommandPool(vk, device, &commandPoolParams);
110 }
111
112 Move<VkCommandBuffer> makeCommandBuffer (const DeviceInterface& vk, const VkDevice device, const VkCommandPool commandPool)
113 {
114         const VkCommandBufferAllocateInfo bufferAllocateParams =
115         {
116                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,         // VkStructureType                      sType;
117                 DE_NULL,                                                                                        // const void*                          pNext;
118                 commandPool,                                                                            // VkCommandPool                        commandPool;
119                 VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                        // VkCommandBufferLevel         level;
120                 1u,                                                                                                     // deUint32                                     bufferCount;
121         };
122         return allocateCommandBuffer(vk, device, &bufferAllocateParams);
123 }
124
125 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface&               vk,
126                                                                                    const VkDevice                               device)
127 {
128         const VkPipelineLayoutCreateInfo pipelineLayoutParams =
129         {
130                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
131                 DE_NULL,                                                                                        // const void*                                          pNext;
132                 0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
133                 0u,                                                                                                     // deUint32                                                     setLayoutCount;
134                 DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
135                 0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
136                 DE_NULL,                                                                                        // const VkPushConstantRange*           pPushConstantRanges;
137         };
138         return createPipelineLayout(vk, device, &pipelineLayoutParams);
139 }
140
141 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface&               vk,
142                                                                                    const VkDevice                               device,
143                                                                                    const VkDescriptorSetLayout  descriptorSetLayout)
144 {
145         const VkPipelineLayoutCreateInfo pipelineLayoutParams =
146         {
147                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
148                 DE_NULL,                                                                                        // const void*                                          pNext;
149                 0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
150                 1u,                                                                                                     // deUint32                                                     setLayoutCount;
151                 &descriptorSetLayout,                                                           // const VkDescriptorSetLayout*         pSetLayouts;
152                 0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
153                 DE_NULL,                                                                                        // const VkPushConstantRange*           pPushConstantRanges;
154         };
155         return createPipelineLayout(vk, device, &pipelineLayoutParams);
156 }
157
158 Move<VkPipeline> makeComputePipeline (const DeviceInterface&    vk,
159                                                                           const VkDevice                        device,
160                                                                           const VkPipelineLayout        pipelineLayout,
161                                                                           const VkShaderModule          shaderModule)
162 {
163         const VkPipelineShaderStageCreateInfo pipelineShaderStageParams =
164         {
165                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    // VkStructureType                                              sType;
166                 DE_NULL,                                                                                                // const void*                                                  pNext;
167                 0u,                                                                                                             // VkPipelineShaderStageCreateFlags             flags;
168                 VK_SHADER_STAGE_COMPUTE_BIT,                                                    // VkShaderStageFlagBits                                stage;
169                 shaderModule,                                                                                   // VkShaderModule                                               module;
170                 "main",                                                                                                 // const char*                                                  pName;
171                 DE_NULL,                                                                                                // const VkSpecializationInfo*                  pSpecializationInfo;
172         };
173         const VkComputePipelineCreateInfo pipelineCreateInfo =
174         {
175                 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,         // VkStructureType                                      sType;
176                 DE_NULL,                                                                                        // const void*                                          pNext;
177                 0u,                                                                                                     // VkPipelineCreateFlags                        flags;
178                 pipelineShaderStageParams,                                                      // VkPipelineShaderStageCreateInfo      stage;
179                 pipelineLayout,                                                                         // VkPipelineLayout                                     layout;
180                 DE_NULL,                                                                                        // VkPipeline                                           basePipelineHandle;
181                 0,                                                                                                      // deInt32                                                      basePipelineIndex;
182         };
183         return createComputePipeline(vk, device, DE_NULL , &pipelineCreateInfo);
184 }
185
186 Move<VkBufferView> makeBufferView (const DeviceInterface&       vk,
187                                                                    const VkDevice                       vkDevice,
188                                                                    const VkBuffer                       buffer,
189                                                                    const VkFormat                       format,
190                                                                    const VkDeviceSize           offset,
191                                                                    const VkDeviceSize           size)
192 {
193         const VkBufferViewCreateInfo bufferViewParams =
194         {
195                 VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,      // VkStructureType                      sType;
196                 DE_NULL,                                                                        // const void*                          pNext;
197                 0u,                                                                                     // VkBufferViewCreateFlags      flags;
198                 buffer,                                                                         // VkBuffer                                     buffer;
199                 format,                                                                         // VkFormat                                     format;
200                 offset,                                                                         // VkDeviceSize                         offset;
201                 size,                                                                           // VkDeviceSize                         range;
202         };
203         return createBufferView(vk, vkDevice, &bufferViewParams);
204 }
205
206 Move<VkImageView> makeImageView (const DeviceInterface&                 vk,
207                                                                  const VkDevice                                 vkDevice,
208                                                                  const VkImage                                  image,
209                                                                  const VkImageViewType                  imageViewType,
210                                                                  const VkFormat                                 format,
211                                                                  const VkImageSubresourceRange  subresourceRange)
212 {
213         const VkImageViewCreateInfo imageViewParams =
214         {
215                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
216                 DE_NULL,                                                                                // const void*                          pNext;
217                 0u,                                                                                             // VkImageViewCreateFlags       flags;
218                 image,                                                                                  // VkImage                                      image;
219                 imageViewType,                                                                  // VkImageViewType                      viewType;
220                 format,                                                                                 // VkFormat                                     format;
221                 makeComponentMappingRGBA(),                                             // VkComponentMapping           components;
222                 subresourceRange,                                                               // VkImageSubresourceRange      subresourceRange;
223         };
224         return createImageView(vk, vkDevice, &imageViewParams);
225 }
226
227 Move<VkDescriptorSet> makeDescriptorSet (const DeviceInterface&                 vk,
228                                                                                  const VkDevice                                 device,
229                                                                                  const VkDescriptorPool                 descriptorPool,
230                                                                                  const VkDescriptorSetLayout    setLayout)
231 {
232         const VkDescriptorSetAllocateInfo allocateParams =
233         {
234                 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,         // VkStructureType                              sType;
235                 DE_NULL,                                                                                        // const void*                                  pNext;
236                 descriptorPool,                                                                         // VkDescriptorPool                             descriptorPool;
237                 1u,                                                                                                     // deUint32                                             setLayoutCount;
238                 &setLayout,                                                                                     // const VkDescriptorSetLayout* pSetLayouts;
239         };
240         return allocateDescriptorSet(vk, device, &allocateParams);
241 }
242
243 VkBufferMemoryBarrier makeBufferMemoryBarrier (const VkAccessFlags      srcAccessMask,
244                                                                                            const VkAccessFlags  dstAccessMask,
245                                                                                            const VkBuffer               buffer,
246                                                                                            const VkDeviceSize   offset,
247                                                                                            const VkDeviceSize   bufferSizeBytes)
248 {
249         const VkBufferMemoryBarrier barrier =
250         {
251                 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,        // VkStructureType      sType;
252                 DE_NULL,                                                                        // const void*          pNext;
253                 srcAccessMask,                                                          // VkAccessFlags        srcAccessMask;
254                 dstAccessMask,                                                          // VkAccessFlags        dstAccessMask;
255                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     srcQueueFamilyIndex;
256                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     destQueueFamilyIndex;
257                 buffer,                                                                         // VkBuffer                     buffer;
258                 offset,                                                                         // VkDeviceSize         offset;
259                 bufferSizeBytes,                                                        // VkDeviceSize         size;
260         };
261         return barrier;
262 }
263
264 VkImageMemoryBarrier makeImageMemoryBarrier     (const VkAccessFlags                    srcAccessMask,
265                                                                                          const VkAccessFlags                    dstAccessMask,
266                                                                                          const VkImageLayout                    oldLayout,
267                                                                                          const VkImageLayout                    newLayout,
268                                                                                          const VkImage                                  image,
269                                                                                          const VkImageSubresourceRange  subresourceRange)
270 {
271         const VkImageMemoryBarrier barrier =
272         {
273                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // VkStructureType                      sType;
274                 DE_NULL,                                                                                // const void*                          pNext;
275                 srcAccessMask,                                                                  // VkAccessFlags                        outputMask;
276                 dstAccessMask,                                                                  // VkAccessFlags                        inputMask;
277                 oldLayout,                                                                              // VkImageLayout                        oldLayout;
278                 newLayout,                                                                              // VkImageLayout                        newLayout;
279                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     srcQueueFamilyIndex;
280                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     destQueueFamilyIndex;
281                 image,                                                                                  // VkImage                                      image;
282                 subresourceRange,                                                               // VkImageSubresourceRange      subresourceRange;
283         };
284         return barrier;
285 }
286
287 void beginCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
288 {
289         const VkCommandBufferBeginInfo commandBufBeginParams =
290         {
291                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
292                 DE_NULL,                                                                                // const void*                                          pNext;
293                 0u,                                                                                             // VkCommandBufferUsageFlags            flags;
294                 (const VkCommandBufferInheritanceInfo*)DE_NULL,
295         };
296         VK_CHECK(vk.beginCommandBuffer(commandBuffer, &commandBufBeginParams));
297 }
298
299 void endCommandBuffer (const DeviceInterface& vk, const VkCommandBuffer commandBuffer)
300 {
301         VK_CHECK(vk.endCommandBuffer(commandBuffer));
302 }
303
304 void submitCommandsAndWait (const DeviceInterface&      vk,
305                                                         const VkDevice                  device,
306                                                         const VkQueue                   queue,
307                                                         const VkCommandBuffer   commandBuffer)
308 {
309         const VkFenceCreateInfo fenceParams =
310         {
311                 VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,    // VkStructureType              sType;
312                 DE_NULL,                                                                // const void*                  pNext;
313                 0u,                                                                             // VkFenceCreateFlags   flags;
314         };
315         const Unique<VkFence> fence(createFence(vk, device, &fenceParams));
316
317         const VkSubmitInfo submitInfo =
318         {
319                 VK_STRUCTURE_TYPE_SUBMIT_INFO,          // VkStructureType                      sType;
320                 DE_NULL,                                                        // const void*                          pNext;
321                 0u,                                                                     // deUint32                                     waitSemaphoreCount;
322                 DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
323                 (const VkPipelineStageFlags*)DE_NULL,
324                 1u,                                                                     // deUint32                                     commandBufferCount;
325                 &commandBuffer,                                         // const VkCommandBuffer*       pCommandBuffers;
326                 0u,                                                                     // deUint32                                     signalSemaphoreCount;
327                 DE_NULL,                                                        // const VkSemaphore*           pSignalSemaphores;
328         };
329
330         VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, *fence));
331         VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), DE_TRUE, ~0ull));
332 }
333
334 } // compute
335 } // vkt