Code clean up in swizzle math operations script am: fc24d7d28c
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / robustness / vktRobustnessUtil.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  * Copyright (c) 2016 Imagination Technologies Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Robustness Utilities
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktRobustnessUtil.hpp"
26 #include "vkDefs.hpp"
27 #include "vkImageUtil.hpp"
28 #include "vkPrograms.hpp"
29 #include "vkQueryUtil.hpp"
30 #include "vkRefUtil.hpp"
31 #include "deMath.h"
32 #include <iomanip>
33 #include <limits>
34 #include <sstream>
35
36 namespace vkt
37 {
38 namespace robustness
39 {
40
41 using namespace vk;
42
43 Move<VkDevice> createRobustBufferAccessDevice (Context& context)
44 {
45         const float queuePriority = 1.0f;
46
47         // Create a universal queue that supports graphics and compute
48         const VkDeviceQueueCreateInfo queueParams =
49         {
50                 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,     // VkStructureType                              sType;
51                 DE_NULL,                                                                        // const void*                                  pNext;
52                 0u,                                                                                     // VkDeviceQueueCreateFlags             flags;
53                 context.getUniversalQueueFamilyIndex(),         // deUint32                                             queueFamilyIndex;
54                 1u,                                                                                     // deUint32                                             queueCount;
55                 &queuePriority                                                          // const float*                                 pQueuePriorities;
56         };
57
58         VkPhysicalDeviceFeatures enabledFeatures = context.getDeviceFeatures();
59         enabledFeatures.robustBufferAccess = true;
60
61         const VkDeviceCreateInfo deviceParams =
62         {
63                 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,   // VkStructureType                                      sType;
64                 DE_NULL,                                                                // const void*                                          pNext;
65                 0u,                                                                             // VkDeviceCreateFlags                          flags;
66                 1u,                                                                             // deUint32                                                     queueCreateInfoCount;
67                 &queueParams,                                                   // const VkDeviceQueueCreateInfo*       pQueueCreateInfos;
68                 0u,                                                                             // deUint32                                                     enabledLayerCount;
69                 DE_NULL,                                                                // const char* const*                           ppEnabledLayerNames;
70                 0u,                                                                             // deUint32                                                     enabledExtensionCount;
71                 DE_NULL,                                                                // const char* const*                           ppEnabledExtensionNames;
72                 &enabledFeatures                                                // const VkPhysicalDeviceFeatures*      pEnabledFeatures;
73         };
74
75         return createDevice(context.getInstanceInterface(), context.getPhysicalDevice(), &deviceParams);
76 }
77
78 bool areEqual (float a, float b)
79 {
80         return deFloatAbs(a - b) <= 0.001f;
81 }
82
83 bool isValueZero (const void* valuePtr, size_t valueSizeInBytes)
84 {
85         const deUint8* bytePtr = reinterpret_cast<const deUint8*>(valuePtr);
86
87         for (size_t i = 0; i < valueSizeInBytes; i++)
88         {
89                 if (bytePtr[i] != 0)
90                         return false;
91         }
92
93         return true;
94 }
95
96 bool isValueWithinBuffer (const void* buffer, VkDeviceSize bufferSize, const void* valuePtr, size_t valueSizeInBytes)
97 {
98         const deUint8* byteBuffer = reinterpret_cast<const deUint8*>(buffer);
99
100         if (bufferSize < ((VkDeviceSize)valueSizeInBytes))
101                 return false;
102
103         for (VkDeviceSize i = 0; i <= (bufferSize - valueSizeInBytes); i++)
104         {
105                 if (!deMemCmp(&byteBuffer[i], valuePtr, valueSizeInBytes))
106                         return true;
107         }
108
109         return false;
110 }
111
112 bool isValueWithinBufferOrZero (const void* buffer, VkDeviceSize bufferSize, const void* valuePtr, size_t valueSizeInBytes)
113 {
114         return isValueWithinBuffer(buffer, bufferSize, valuePtr, valueSizeInBytes) || isValueZero(valuePtr, valueSizeInBytes);
115 }
116
117 bool verifyOutOfBoundsVec4 (const void* vecPtr, VkFormat bufferFormat)
118 {
119         if (isUintFormat(bufferFormat))
120         {
121                 const deUint32* data = (deUint32*)vecPtr;
122
123                 return data[0] == 0u
124                         && data[1] == 0u
125                         && data[2] == 0u
126                         && (data[3] == 0u || data[3] == 1u || data[3] == std::numeric_limits<deUint32>::max());
127         }
128         else if (isIntFormat(bufferFormat))
129         {
130                 const deInt32* data = (deInt32*)vecPtr;
131
132                 return data[0] == 0
133                         && data[1] == 0
134                         && data[2] == 0
135                         && (data[3] == 0 || data[3] == 1 || data[3] == std::numeric_limits<deInt32>::max());
136         }
137         else if (isFloatFormat(bufferFormat))
138         {
139                 const float* data = (float*)vecPtr;
140
141                 return areEqual(data[0], 0.0f)
142                         && areEqual(data[1], 0.0f)
143                         && areEqual(data[2], 0.0f)
144                         && (areEqual(data[3], 0.0f) || areEqual(data[3], 1.0f));
145         }
146         else if (bufferFormat == VK_FORMAT_A2B10G10R10_UNORM_PACK32)
147         {
148                 return *((deUint32*)vecPtr) == 0xc0000000u;
149         }
150
151         DE_ASSERT(false);
152         return false;
153 }
154
155 void populateBufferWithTestValues (void* buffer, VkDeviceSize size, VkFormat format)
156 {
157         // Assign a sequence of 32-bit values
158         for (VkDeviceSize scalarNdx = 0; scalarNdx < size / 4; scalarNdx++)
159         {
160                 const deUint32 valueIndex = (deUint32)(2 + scalarNdx); // Do not use 0 or 1
161
162                 if (isUintFormat(format))
163                 {
164                         reinterpret_cast<deUint32*>(buffer)[scalarNdx] = valueIndex;
165                 }
166                 else if (isIntFormat(format))
167                 {
168                         reinterpret_cast<deInt32*>(buffer)[scalarNdx] = -deInt32(valueIndex);
169                 }
170                 else if (isFloatFormat(format))
171                 {
172                         reinterpret_cast<float*>(buffer)[scalarNdx] = float(valueIndex);
173                 }
174                 else if (format == VK_FORMAT_A2B10G10R10_UNORM_PACK32)
175                 {
176                         const deUint32  r       = ((valueIndex + 0) & ((2u << 10) - 1u));
177                         const deUint32  g       = ((valueIndex + 1) & ((2u << 10) - 1u));
178                         const deUint32  b       = ((valueIndex + 2) & ((2u << 10) - 1u));
179                         const deUint32  a       = ((valueIndex + 0) & ((2u << 2) - 1u));
180
181                         reinterpret_cast<deUint32*>(buffer)[scalarNdx] = (a << 30) | (b << 20) | (g << 10) | r;
182                 }
183                 else
184                 {
185                         DE_ASSERT(false);
186                 }
187         }
188 }
189
190 void logValue (std::ostringstream& logMsg, const void* valuePtr, VkFormat valueFormat, size_t valueSize)
191 {
192         if (isUintFormat(valueFormat))
193         {
194                 logMsg << *reinterpret_cast<const deUint32*>(valuePtr);
195         }
196         else if (isIntFormat(valueFormat))
197         {
198                 logMsg << *reinterpret_cast<const deInt32*>(valuePtr);
199         }
200         else if (isFloatFormat(valueFormat))
201         {
202                 logMsg << *reinterpret_cast<const float*>(valuePtr);
203         }
204         else
205         {
206                 const deUint8*                          bytePtr         = reinterpret_cast<const deUint8*>(valuePtr);
207                 const std::ios::fmtflags        streamFlags     = logMsg.flags();
208
209                 logMsg << std::hex;
210                 for (size_t i = 0; i < valueSize; i++)
211                 {
212                         logMsg << " " << (deUint32)bytePtr[i];
213                 }
214                 logMsg.flags(streamFlags);
215         }
216 }
217
218 // TestEnvironment
219
220 TestEnvironment::TestEnvironment (Context&                              context,
221                                                                   VkDevice                              device,
222                                                                   VkDescriptorSetLayout descriptorSetLayout,
223                                                                   VkDescriptorSet               descriptorSet)
224         : m_context                             (context)
225         , m_device                              (device)
226         , m_descriptorSetLayout (descriptorSetLayout)
227         , m_descriptorSet               (descriptorSet)
228 {
229         const DeviceInterface& vk = context.getDeviceInterface();
230
231         // Create command pool
232         {
233                 const VkCommandPoolCreateInfo commandPoolParams =
234                 {
235                         VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,             // VkStructureType                      sType;
236                         DE_NULL,                                                                                // const void*                          pNext;
237                         VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,                   // VkCommandPoolCreateFlags     flags;
238                         context.getUniversalQueueFamilyIndex()                  // deUint32                                     queueFamilyIndex;
239                 };
240
241                 m_commandPool = createCommandPool(vk, m_device, &commandPoolParams);
242         }
243
244         // Create command buffer
245         {
246                 const VkCommandBufferAllocateInfo commandBufferAllocateInfo =
247                 {
248                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
249                         DE_NULL,                                                                                // const void*                          pNext;
250                         *m_commandPool,                                                                         // VkCommandPool                        commandPool;
251                         VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel         level;
252                         1u,                                                                                             // deUint32                                     bufferCount;
253                 };
254
255                 m_commandBuffer = allocateCommandBuffer(vk, m_device, &commandBufferAllocateInfo);
256         }
257 }
258
259 VkCommandBuffer TestEnvironment::getCommandBuffer (void)
260 {
261         return *m_commandBuffer;
262 }
263
264 // GraphicsEnvironment
265
266 GraphicsEnvironment::GraphicsEnvironment (Context&                                      context,
267                                                                                   VkDevice                                      device,
268                                                                                   VkDescriptorSetLayout         descriptorSetLayout,
269                                                                                   VkDescriptorSet                       descriptorSet,
270                                                                                   const VertexBindings&         vertexBindings,
271                                                                                   const VertexAttributes&       vertexAttributes,
272                                                                                   const DrawConfig&                     drawConfig)
273
274         : TestEnvironment               (context, device, descriptorSetLayout, descriptorSet)
275         , m_renderSize                  (16, 16)
276         , m_colorFormat                 (VK_FORMAT_R8G8B8A8_UNORM)
277 {
278         const DeviceInterface&          vk                                              = context.getDeviceInterface();
279         const deUint32                          queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
280         const VkComponentMapping        componentMappingRGBA    = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
281         SimpleAllocator                         memAlloc                                (vk, m_device, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice()));
282
283         // Create color image and view
284         {
285                 const VkImageCreateInfo colorImageParams =
286                 {
287                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                            // VkStructureType                      sType;
288                         DE_NULL,                                                                                                                                        // const void*                          pNext;
289                         0u,                                                                                                                                                     // VkImageCreateFlags           flags;
290                         VK_IMAGE_TYPE_2D,                                                                                                                       // VkImageType                          imageType;
291                         m_colorFormat,                                                                                                                          // VkFormat                                     format;
292                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y(), 1u },                         // VkExtent3D                           extent;
293                         1u,                                                                                                                                                     // deUint32                                     mipLevels;
294                         1u,                                                                                                                                                     // deUint32                                     arrayLayers;
295                         VK_SAMPLE_COUNT_1_BIT,                                                                                                          // VkSampleCountFlagBits        samples;
296                         VK_IMAGE_TILING_OPTIMAL,                                                                                                        // VkImageTiling                        tiling;
297                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,          // VkImageUsageFlags            usage;
298                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                      // VkSharingMode                        sharingMode;
299                         1u,                                                                                                                                                     // deUint32                                     queueFamilyIndexCount;
300                         &queueFamilyIndex,                                                                                                                      // const deUint32*                      pQueueFamilyIndices;
301                         VK_IMAGE_LAYOUT_UNDEFINED                                                                                                       // VkImageLayout                        initialLayout;
302                 };
303
304                 m_colorImage                    = createImage(vk, m_device, &colorImageParams);
305                 m_colorImageAlloc               = memAlloc.allocate(getImageMemoryRequirements(vk, m_device, *m_colorImage), MemoryRequirement::Any);
306                 VK_CHECK(vk.bindImageMemory(m_device, *m_colorImage, m_colorImageAlloc->getMemory(), m_colorImageAlloc->getOffset()));
307
308                 const VkImageViewCreateInfo colorAttachmentViewParams =
309                 {
310                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,                       // VkStructureType                      sType;
311                         DE_NULL,                                                                                        // const void*                          pNext;
312                         0u,                                                                                                     // VkImageViewCreateFlags       flags;
313                         *m_colorImage,                                                                          // VkImage                                      image;
314                         VK_IMAGE_VIEW_TYPE_2D,                                                          // VkImageViewType                      viewType;
315                         m_colorFormat,                                                                          // VkFormat                                     format;
316                         componentMappingRGBA,                                                           // VkComponentMapping           components;
317                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }           // VkImageSubresourceRange      subresourceRange;
318                 };
319
320                 m_colorAttachmentView = createImageView(vk, m_device, &colorAttachmentViewParams);
321         }
322
323         // Create render pass
324         {
325                 const VkAttachmentDescription colorAttachmentDescription =
326                 {
327                         0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
328                         m_colorFormat,                                                                          // VkFormat                                                     format;
329                         VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
330                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
331                         VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
332                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
333                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
334                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
335                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
336                 };
337
338                 const VkAttachmentReference colorAttachmentReference =
339                 {
340                         0u,                                                                                                     // deUint32                     attachment;
341                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
342                 };
343
344                 const VkSubpassDescription subpassDescription =
345                 {
346                         0u,                                                                                                     // VkSubpassDescriptionFlags            flags;
347                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                        // VkPipelineBindPoint                          pipelineBindPoint;
348                         0u,                                                                                                     // deUint32                                                     inputAttachmentCount;
349                         DE_NULL,                                                                                        // const VkAttachmentReference*         pInputAttachments;
350                         1u,                                                                                                     // deUint32                                                     colorAttachmentCount;
351                         &colorAttachmentReference,                                                      // const VkAttachmentReference*         pColorAttachments;
352                         DE_NULL,                                                                                        // const VkAttachmentReference*         pResolveAttachments;
353                         DE_NULL,                                                                                        // const VkAttachmentReference*         pDepthStencilAttachment;
354                         0u,                                                                                                     // deUint32                                                     preserveAttachmentCount;
355                         DE_NULL                                                                                         // const VkAttachmentReference*         pPreserveAttachments;
356                 };
357
358                 const VkRenderPassCreateInfo renderPassParams =
359                 {
360                         VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
361                         DE_NULL,                                                                                        // const void*                                          pNext;
362                         0u,                                                                                                     // VkRenderPassCreateFlags                      flags;
363                         1u,                                                                                                     // deUint32                                                     attachmentCount;
364                         &colorAttachmentDescription,                                            // const VkAttachmentDescription*       pAttachments;
365                         1u,                                                                                                     // deUint32                                                     subpassCount;
366                         &subpassDescription,                                                            // const VkSubpassDescription*          pSubpasses;
367                         0u,                                                                                                     // deUint32                                                     dependencyCount;
368                         DE_NULL                                                                                         // const VkSubpassDependency*           pDependencies;
369                 };
370
371                 m_renderPass = createRenderPass(vk, m_device, &renderPassParams);
372         }
373
374         // Create framebuffer
375         {
376                 const VkFramebufferCreateInfo framebufferParams =
377                 {
378                         VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                      // VkStructureType                      sType;
379                         DE_NULL,                                                                                        // const void*                          pNext;
380                         0u,                                                                                                     // VkFramebufferCreateFlags     flags;
381                         *m_renderPass,                                                                          // VkRenderPass                         renderPass;
382                         1u,                                                                                                     // deUint32                                     attachmentCount;
383                         &m_colorAttachmentView.get(),                                           // const VkImageView*           pAttachments;
384                         (deUint32)m_renderSize.x(),                                                     // deUint32                                     width;
385                         (deUint32)m_renderSize.y(),                                                     // deUint32                                     height;
386                         1u                                                                                                      // deUint32                                     layers;
387                 };
388
389                 m_framebuffer = createFramebuffer(vk, m_device, &framebufferParams);
390         }
391
392         // Create pipeline layout
393         {
394                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
395                 {
396                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                              sType;
397                         DE_NULL,                                                                                        // const void*                                  pNext;
398                         0u,                                                                                                     // VkPipelineLayoutCreateFlags  flags;
399                         1u,                                                                                                     // deUint32                                             setLayoutCount;
400                         &m_descriptorSetLayout,                                                         // const VkDescriptorSetLayout* pSetLayouts;
401                         0u,                                                                                                     // deUint32                                             pushConstantRangeCount;
402                         DE_NULL                                                                                         // const VkPushConstantRange*   pPushConstantRanges;
403                 };
404
405                 m_pipelineLayout = createPipelineLayout(vk, m_device, &pipelineLayoutParams);
406         }
407
408         m_vertexShaderModule    = createShaderModule(vk, m_device, m_context.getBinaryCollection().get("vertex"), 0);
409         m_fragmentShaderModule  = createShaderModule(vk, m_device, m_context.getBinaryCollection().get("fragment"), 0);
410
411         // Create pipeline
412         {
413                 const VkPipelineShaderStageCreateInfo renderShaderStages[2] =
414                 {
415                         {
416                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
417                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
418                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
419                                 VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                stage;
420                                 *m_vertexShaderModule,                                                                          // VkShaderModule                                               module;
421                                 "main",                                                                                                         // const char*                                                  pName;
422                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
423                         },
424                         {
425                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
426                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
427                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
428                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                stage;
429                                 *m_fragmentShaderModule,                                                                        // VkShaderModule                                               module;
430                                 "main",                                                                                                         // const char*                                                  pName;
431                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
432                         }
433                 };
434
435                 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
436                 {
437                         VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
438                         DE_NULL,                                                                                                                // const void*                                                          pNext;
439                         0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
440                         (deUint32)vertexBindings.size(),                                                                // deUint32                                                                     vertexBindingDescriptionCount;
441                         vertexBindings.data(),                                                                                  // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
442                         (deUint32)vertexAttributes.size(),                                                              // deUint32                                                                     vertexAttributeDescriptionCount;
443                         vertexAttributes.data()                                                                                 // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
444                 };
445
446                 const VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
447                 {
448                         VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
449                         DE_NULL,                                                                                                                // const void*                                                          pNext;
450                         0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
451                         VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,                                                   // VkPrimitiveTopology                                          topology;
452                         false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
453                 };
454
455                 const VkViewport viewport =
456                 {
457                         0.0f,                                           // float        x;
458                         0.0f,                                           // float        y;
459                         (float)m_renderSize.x(),        // float        width;
460                         (float)m_renderSize.y(),        // float        height;
461                         0.0f,                                           // float        minDepth;
462                         1.0f                                            // float        maxDepth;
463                 };
464
465                 const VkRect2D scissor = { { 0, 0 }, { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y() } };
466
467                 const VkPipelineViewportStateCreateInfo viewportStateParams =
468                 {
469                         VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                              sType;
470                         DE_NULL,                                                                                                                // const void*                                                  pNext;
471                         0u,                                                                                                                             // VkPipelineViewportStateCreateFlags   flags;
472                         1u,                                                                                                                             // deUint32                                                             viewportCount;
473                         &viewport,                                                                                                              // const VkViewport*                                    pViewports;
474                         1u,                                                                                                                             // deUint32                                                             scissorCount;
475                         &scissor                                                                                                                // const VkRect2D*                                              pScissors;
476                 };
477
478                 const VkPipelineRasterizationStateCreateInfo rasterStateParams =
479                 {
480                         VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
481                         DE_NULL,                                                                                                                // const void*                                                          pNext;
482                         0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
483                         false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
484                         false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
485                         VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
486                         VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
487                         VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
488                         false,                                                                                                                  // VkBool32                                                                     depthBiasEnable;
489                         0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
490                         0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
491                         0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
492                         1.0f                                                                                                                    // float                                                                        lineWidth;
493                 };
494
495                 const VkPipelineColorBlendAttachmentState colorBlendAttachmentState =
496                 {
497                         false,                                                                                                          // VkBool32                                     blendEnable;
498                         VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcColorBlendFactor;
499                         VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstColorBlendFactor;
500                         VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            colorBlendOp;
501                         VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcAlphaBlendFactor;
502                         VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstAlphaBlendFactor;
503                         VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            alphaBlendOp;
504                         VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |           // VkColorComponentFlags        colorWriteMask;
505                                 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
506                 };
507
508                 const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
509                 {
510                         VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
511                         DE_NULL,                                                                                                        // const void*                                                                  pNext;
512                         0u,                                                                                                                     // VkPipelineColorBlendStateCreateFlags                 flags;
513                         false,                                                                                                          // VkBool32                                                                             logicOpEnable;
514                         VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
515                         1u,                                                                                                                     // deUint32                                                                             attachmentCount;
516                         &colorBlendAttachmentState,                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
517                         { 0.0f, 0.0f, 0.0f, 0.0f }                                                                      // float                                                                                blendConstants[4];
518                 };
519
520                 const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
521                 {
522                         VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
523                         DE_NULL,                                                                                                        // const void*                                                          pNext;
524                         0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
525                         VK_SAMPLE_COUNT_1_BIT,                                                                          // VkSampleCountFlagBits                                        rasterizationSamples;
526                         false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
527                         0.0f,                                                                                                           // float                                                                        minSampleShading;
528                         DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
529                         false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
530                         false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
531                 };
532
533                 VkPipelineDepthStencilStateCreateInfo depthStencilStateParams =
534                 {
535                         VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // VkStructureType                                                      sType;
536                         DE_NULL,                                                                                                        // const void*                                                          pNext;
537                         0u,                                                                                                                     // VkPipelineDepthStencilStateCreateFlags       flags;
538                         false,                                                                                                          // VkBool32                                                                     depthTestEnable;
539                         false,                                                                                                          // VkBool32                                                                     depthWriteEnable;
540                         VK_COMPARE_OP_LESS,                                                                                     // VkCompareOp                                                          depthCompareOp;
541                         false,                                                                                                          // VkBool32                                                                     depthBoundsTestEnable;
542                         false,                                                                                                          // VkBool32                                                                     stencilTestEnable;
543                         {                                                                                                                       // VkStencilOpState                                                     front;
544                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  failOp;
545                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  passOp;
546                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  depthFailOp;
547                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
548                                 0u,                                             // deUint32             compareMask;
549                                 0u,                                             // deUint32             writeMask;
550                                 0u                                              // deUint32             reference;
551                         },
552                         {                                                                                                                       // VkStencilOpState     back;
553                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  failOp;
554                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  passOp;
555                                 VK_STENCIL_OP_ZERO,             // VkStencilOp  depthFailOp;
556                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
557                                 0u,                                             // deUint32             compareMask;
558                                 0u,                                             // deUint32             writeMask;
559                                 0u                                              // deUint32             reference;
560                         },
561                         -1.0f,                                                                                                          // float                        minDepthBounds;
562                         +1.0f                                                                                                           // float                        maxDepthBounds;
563                 };
564
565                 const VkGraphicsPipelineCreateInfo graphicsPipelineParams =
566                 {
567                         VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
568                         DE_NULL,                                                                                        // const void*                                                                          pNext;
569                         0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
570                         2u,                                                                                                     // deUint32                                                                                     stageCount;
571                         renderShaderStages,                                                                     // const VkPipelineShaderStageCreateInfo*                       pStages;
572                         &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
573                         &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
574                         DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
575                         &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
576                         &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
577                         &multisampleStateParams,                                                        // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
578                         &depthStencilStateParams,                                                       // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
579                         &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
580                         DE_NULL,                                                                                        // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
581                         *m_pipelineLayout,                                                                      // VkPipelineLayout                                                                     layout;
582                         *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
583                         0u,                                                                                                     // deUint32                                                                                     subpass;
584                         0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
585                         0u                                                                                                      // deInt32                                                                                      basePipelineIndex;
586                 };
587
588                 m_graphicsPipeline      = createGraphicsPipeline(vk, m_device, DE_NULL, &graphicsPipelineParams);
589         }
590
591         // Record commands
592         {
593                 const VkCommandBufferBeginInfo commandBufferBeginInfo =
594                 {
595                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                              sType;
596                         DE_NULL,                                                                                // const void*                                                  pNext;
597                         0u,                                                                                             // VkCommandBufferUsageFlags                    flags;
598                         (const VkCommandBufferInheritanceInfo*)DE_NULL, // const VkCommandBufferInheritanceInfo *pInheritanceInfo;
599                 };
600
601                 VkClearValue attachmentClearValue;
602                 attachmentClearValue.color.float32[0] = 0.0f;
603                 attachmentClearValue.color.float32[1] = 0.0f;
604                 attachmentClearValue.color.float32[2] = 0.0f;
605                 attachmentClearValue.color.float32[3] = 0.0f;
606
607                 const VkRenderPassBeginInfo renderPassBeginInfo =
608                 {
609                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
610                         DE_NULL,                                                                                                // const void*                  pNext;
611                         *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
612                         *m_framebuffer,                                                                                 // VkFramebuffer                framebuffer;
613                         {
614                                 { 0, 0 },
615                                 { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y() }
616                         },                                                                                                              // VkRect2D                             renderArea;
617                         1,                                                                                                              // deUint32                             clearValueCount;
618                         &attachmentClearValue                                                                   // const VkClearValue*  pClearValues;
619                 };
620
621                 const VkImageMemoryBarrier imageLayoutBarrier =
622                 {
623                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                 // VkStructureType                      sType;
624                         DE_NULL,                                                                                                // const void*                          pNext;
625                         (VkAccessFlags)0,                                                                               // VkAccessFlags                        srcAccessMask;
626                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                                   // VkAccessFlags                        dstAccessMask;
627                         VK_IMAGE_LAYOUT_UNDEFINED,                                                              // VkImageLayout                        oldLayout;
628                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                               // VkImageLayout                        newLayout;
629                         VK_QUEUE_FAMILY_IGNORED,                                                                // uint32_t                                     srcQueueFamilyIndex;
630                         VK_QUEUE_FAMILY_IGNORED,                                                                // uint32_t                                     dstQueueFamilyIndex;
631                         *m_colorImage,                                                                                  // VkImage                                      image;
632                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }                   // VkImageSubresourceRange      subresourceRange;
633                 };
634
635                 VK_CHECK(vk.beginCommandBuffer(*m_commandBuffer, &commandBufferBeginInfo));
636                 {
637                         vk.cmdPipelineBarrier(*m_commandBuffer,
638                                                                   VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
639                                                                   VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
640                                                                   (VkDependencyFlags)0,
641                                                                   0u, DE_NULL,
642                                                                   0u, DE_NULL,
643                                                                   1u, &imageLayoutBarrier);
644
645                         vk.cmdBeginRenderPass(*m_commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
646                         {
647                                 const std::vector<VkDeviceSize> vertexBufferOffsets(drawConfig.vertexBuffers.size(), 0ull);
648
649                                 vk.cmdBindPipeline(*m_commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_graphicsPipeline);
650                                 vk.cmdBindDescriptorSets(*m_commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipelineLayout, 0, 1, &m_descriptorSet, 0, DE_NULL);
651                                 vk.cmdBindVertexBuffers(*m_commandBuffer, 0, (deUint32)drawConfig.vertexBuffers.size(), drawConfig.vertexBuffers.data(), vertexBufferOffsets.data());
652
653                                 if (drawConfig.indexBuffer == DE_NULL || drawConfig.indexCount == 0)
654                                 {
655                                         vk.cmdDraw(*m_commandBuffer, drawConfig.vertexCount, drawConfig.instanceCount, 0, 0);
656                                 }
657                                 else
658                                 {
659                                         vk.cmdBindIndexBuffer(*m_commandBuffer, drawConfig.indexBuffer, 0, VK_INDEX_TYPE_UINT32);
660                                         vk.cmdDrawIndexed(*m_commandBuffer, drawConfig.indexCount, drawConfig.instanceCount, 0, 0, 0);
661                                 }
662                         }
663                         vk.cmdEndRenderPass(*m_commandBuffer);
664                 }
665                 VK_CHECK(vk.endCommandBuffer(*m_commandBuffer));
666         }
667 }
668
669 // ComputeEnvironment
670
671 ComputeEnvironment::ComputeEnvironment (Context&                                context,
672                                                                                 VkDevice                                device,
673                                                                                 VkDescriptorSetLayout   descriptorSetLayout,
674                                                                                 VkDescriptorSet                 descriptorSet)
675
676         : TestEnvironment       (context, device, descriptorSetLayout, descriptorSet)
677 {
678         const DeviceInterface& vk = context.getDeviceInterface();
679
680         // Create pipeline layout
681         {
682                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
683                 {
684                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,                  // VkStructureType                                      sType;
685                         DE_NULL,                                                                                                // const void*                                          pNext;
686                         0u,                                                                                                             // VkPipelineLayoutCreateFlags          flags;
687                         1u,                                                                                                             // deUint32                                                     setLayoutCount;
688                         &m_descriptorSetLayout,                                                                 // const VkDescriptorSetLayout*         pSetLayouts;
689                         0u,                                                                                                             // deUint32                                                     pushConstantRangeCount;
690                         DE_NULL                                                                                                 // const VkPushConstantRange*           pPushConstantRanges;
691                 };
692
693                 m_pipelineLayout = createPipelineLayout(vk, m_device, &pipelineLayoutParams);
694         }
695
696         // Create compute pipeline
697         {
698                 m_computeShaderModule = createShaderModule(vk, m_device, m_context.getBinaryCollection().get("compute"), 0);
699
700                 const VkPipelineShaderStageCreateInfo computeStageParams =
701                 {
702                         VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    // VkStructureType                                              sType;
703                         DE_NULL,                                                                                                // const void*                                                  pNext;
704                         0u,                                                                                                             // VkPipelineShaderStageCreateFlags             flags;
705                         VK_SHADER_STAGE_COMPUTE_BIT,                                                    // VkShaderStageFlagBits                                stage;
706                         *m_computeShaderModule,                                                                 // VkShaderModule                                               module;
707                         "main",                                                                                                 // const char*                                                  pName;
708                         DE_NULL,                                                                                                // const VkSpecializationInfo*                  pSpecializationInfo;
709                 };
710
711                 const VkComputePipelineCreateInfo computePipelineParams =
712                 {
713                         VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,                 // VkStructureType                                              sType;
714                         DE_NULL,                                                                                                // const void*                                                  pNext;
715                         0u,                                                                                                             // VkPipelineCreateFlags                                flags;
716                         computeStageParams,                                                                             // VkPipelineShaderStageCreateInfo              stage;
717                         *m_pipelineLayout,                                                                              // VkPipelineLayout                                             layout;
718                         DE_NULL,                                                                                                // VkPipeline                                                   basePipelineHandle;
719                         0u                                                                                                              // deInt32                                                              basePipelineIndex;
720                 };
721
722                 m_computePipeline = createComputePipeline(vk, m_device, DE_NULL, &computePipelineParams);
723         }
724
725         // Record commands
726         {
727                 const VkCommandBufferBeginInfo commandBufferBeginInfo =
728                 {
729                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                              sType;
730                         DE_NULL,                                                                                // const void*                                                  pNext;
731                         0u,                                                                                             // VkCommandBufferUsageFlags                    flags;
732                         (const VkCommandBufferInheritanceInfo*)DE_NULL, // const VkCommandBufferInheritanceInfo *pInheritanceInfo;
733                 };
734
735                 VK_CHECK(vk.beginCommandBuffer(*m_commandBuffer, &commandBufferBeginInfo));
736                 vk.cmdBindPipeline(*m_commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *m_computePipeline);
737                 vk.cmdBindDescriptorSets(*m_commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *m_pipelineLayout, 0, 1, &m_descriptorSet, 0, DE_NULL);
738                 vk.cmdDispatch(*m_commandBuffer, 32, 32, 1);
739                 VK_CHECK(vk.endCommandBuffer(*m_commandBuffer));
740         }
741 }
742
743 } // robustness
744 } // vkt