dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / pipeline / vktPipelineDepthTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Imagination Technologies Ltd.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Depth Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktPipelineDepthTests.hpp"
37 #include "vktPipelineClearUtil.hpp"
38 #include "vktPipelineImageUtil.hpp"
39 #include "vktPipelineVertexUtil.hpp"
40 #include "vktPipelineReferenceRenderer.hpp"
41 #include "vktTestCase.hpp"
42 #include "vktTestCaseUtil.hpp"
43 #include "vkImageUtil.hpp"
44 #include "vkMemUtil.hpp"
45 #include "vkPrograms.hpp"
46 #include "vkQueryUtil.hpp"
47 #include "vkRef.hpp"
48 #include "vkRefUtil.hpp"
49 #include "tcuImageCompare.hpp"
50 #include "deUniquePtr.hpp"
51 #include "deStringUtil.hpp"
52 #include "deMemory.h"
53
54 #include <sstream>
55 #include <vector>
56
57 namespace vkt
58 {
59 namespace pipeline
60 {
61
62 using namespace vk;
63
64 namespace
65 {
66
67 bool isSupportedDepthStencilFormat (const InstanceInterface& instanceInterface, VkPhysicalDevice device, VkFormat format)
68 {
69         VkFormatProperties formatProps;
70
71         instanceInterface.getPhysicalDeviceFormatProperties(device, format, &formatProps);
72
73         return (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0u;
74 }
75
76 tcu::TestStatus testSupportsDepthStencilFormat (Context& context, VkFormat format)
77 {
78         DE_ASSERT(vk::isDepthStencilFormat(format));
79
80         if (isSupportedDepthStencilFormat(context.getInstanceInterface(), context.getPhysicalDevice(), format))
81                 return tcu::TestStatus::pass("Format can be used in depth/stencil attachment");
82         else
83                 return tcu::TestStatus::fail("Unsupported depth/stencil attachment format");
84 }
85
86 tcu::TestStatus testSupportsAtLeastOneDepthStencilFormat (Context& context, const std::vector<VkFormat> formats)
87 {
88         std::ostringstream      supportedFormatsMsg;
89         bool                            pass                                    = false;
90
91         DE_ASSERT(!formats.empty());
92
93         for (size_t formatNdx = 0; formatNdx < formats.size(); formatNdx++)
94         {
95                 const VkFormat format = formats[formatNdx];
96
97                 DE_ASSERT(vk::isDepthStencilFormat(format));
98
99                 if (isSupportedDepthStencilFormat(context.getInstanceInterface(), context.getPhysicalDevice(), format))
100                 {
101                         pass = true;
102                         supportedFormatsMsg << vk::getFormatName(format);
103
104                         if (formatNdx < formats.size() - 1)
105                                 supportedFormatsMsg << ", ";
106                 }
107         }
108
109         if (pass)
110                 return tcu::TestStatus::pass(std::string("Supported depth/stencil formats: ") + supportedFormatsMsg.str());
111         else
112                 return tcu::TestStatus::fail("All depth/stencil formats are unsupported");
113 }
114
115 class DepthTest : public vkt::TestCase
116 {
117 public:
118         enum
119         {
120                 QUAD_COUNT = 4
121         };
122
123         static const float                                      quadDepths[QUAD_COUNT];
124
125                                                                                 DepthTest                               (tcu::TestContext&              testContext,
126                                                                                                                                  const std::string&             name,
127                                                                                                                                  const std::string&             description,
128                                                                                                                                  const VkFormat                 depthFormat,
129                                                                                                                                  const VkCompareOp              depthCompareOps[QUAD_COUNT]);
130         virtual                                                         ~DepthTest                              (void);
131         virtual void                                            initPrograms                    (SourceCollections& programCollection) const;
132         virtual TestInstance*                           createInstance                  (Context& context) const;
133
134 private:
135         const VkFormat                                          m_depthFormat;
136         VkCompareOp                                                     m_depthCompareOps[QUAD_COUNT];
137 };
138
139 class DepthTestInstance : public vkt::TestInstance
140 {
141 public:
142                                                                                 DepthTestInstance               (Context& context, const VkFormat depthFormat, const VkCompareOp depthCompareOps[DepthTest::QUAD_COUNT]);
143         virtual                                                         ~DepthTestInstance              (void);
144         virtual tcu::TestStatus                         iterate                                 (void);
145
146 private:
147         tcu::TestStatus                                         verifyImage                             (void);
148
149 private:
150         VkCompareOp                                                     m_depthCompareOps[DepthTest::QUAD_COUNT];
151         const tcu::IVec2                                        m_renderSize;
152         const VkFormat                                          m_colorFormat;
153         const VkFormat                                          m_depthFormat;
154
155         Move<VkImage>                                           m_colorImage;
156         de::MovePtr<Allocation>                         m_colorImageAlloc;
157         Move<VkImage>                                           m_depthImage;
158         de::MovePtr<Allocation>                         m_depthImageAlloc;
159         Move<VkImageView>                                       m_colorAttachmentView;
160         Move<VkImageView>                                       m_depthAttachmentView;
161         Move<VkRenderPass>                                      m_renderPass;
162         Move<VkFramebuffer>                                     m_framebuffer;
163
164         Move<VkShaderModule>                            m_vertexShaderModule;
165         Move<VkShaderModule>                            m_fragmentShaderModule;
166
167         Move<VkBuffer>                                          m_vertexBuffer;
168         std::vector<Vertex4RGBA>                        m_vertices;
169         de::MovePtr<Allocation>                         m_vertexBufferAlloc;
170
171         Move<VkPipelineLayout>                          m_pipelineLayout;
172         Move<VkPipeline>                                        m_graphicsPipelines[DepthTest::QUAD_COUNT];
173
174         Move<VkCommandPool>                                     m_cmdPool;
175         Move<VkCommandBuffer>                           m_cmdBuffer;
176
177         Move<VkFence>                                           m_fence;
178 };
179
180 const float DepthTest::quadDepths[QUAD_COUNT] =
181 {
182         0.1f,
183         0.0f,
184         0.3f,
185         0.2f
186 };
187
188 DepthTest::DepthTest (tcu::TestContext&         testContext,
189                                           const std::string&    name,
190                                           const std::string&    description,
191                                           const VkFormat                depthFormat,
192                                           const VkCompareOp             depthCompareOps[QUAD_COUNT])
193         : vkt::TestCase (testContext, name, description)
194         , m_depthFormat (depthFormat)
195 {
196         deMemcpy(m_depthCompareOps, depthCompareOps, sizeof(VkCompareOp) * QUAD_COUNT);
197 }
198
199 DepthTest::~DepthTest (void)
200 {
201 }
202
203 TestInstance* DepthTest::createInstance (Context& context) const
204 {
205         return new DepthTestInstance(context, m_depthFormat, m_depthCompareOps);
206 }
207
208 void DepthTest::initPrograms (SourceCollections& programCollection) const
209 {
210         programCollection.glslSources.add("color_vert") << glu::VertexSource(
211                 "#version 310 es\n"
212                 "layout(location = 0) in vec4 position;\n"
213                 "layout(location = 1) in vec4 color;\n"
214                 "layout(location = 0) out highp vec4 vtxColor;\n"
215                 "void main (void)\n"
216                 "{\n"
217                 "       gl_Position = position;\n"
218                 "       vtxColor = color;\n"
219                 "}\n");
220
221         programCollection.glslSources.add("color_frag") << glu::FragmentSource(
222                 "#version 310 es\n"
223                 "layout(location = 0) in highp vec4 vtxColor;\n"
224                 "layout(location = 0) out highp vec4 fragColor;\n"
225                 "void main (void)\n"
226                 "{\n"
227                 "       fragColor = vtxColor;\n"
228                 "}\n");
229 }
230
231 DepthTestInstance::DepthTestInstance (Context&                          context,
232                                                                           const VkFormat                depthFormat,
233                                                                           const VkCompareOp             depthCompareOps[DepthTest::QUAD_COUNT])
234         : vkt::TestInstance     (context)
235         , m_renderSize          (32, 32)
236         , m_colorFormat         (VK_FORMAT_R8G8B8A8_UNORM)
237         , m_depthFormat         (depthFormat)
238 {
239         const DeviceInterface&          vk                                              = context.getDeviceInterface();
240         const VkDevice                          vkDevice                                = context.getDevice();
241         const deUint32                          queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
242         SimpleAllocator                         memAlloc                                (vk, vkDevice, getPhysicalDeviceMemoryProperties(context.getInstanceInterface(), context.getPhysicalDevice()));
243         const VkComponentMapping        componentMappingRGBA    = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
244
245         // Copy depth operators
246         deMemcpy(m_depthCompareOps, depthCompareOps, sizeof(VkCompareOp) * DepthTest::QUAD_COUNT);
247
248         // Create color image
249         {
250                 const VkImageCreateInfo colorImageParams =
251                 {
252                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                            // VkStructureType                      sType;
253                         DE_NULL,                                                                                                                                        // const void*                          pNext;
254                         0u,                                                                                                                                                     // VkImageCreateFlags           flags;
255                         VK_IMAGE_TYPE_2D,                                                                                                                       // VkImageType                          imageType;
256                         m_colorFormat,                                                                                                                          // VkFormat                                     format;
257                         { m_renderSize.x(), m_renderSize.y(), 1u },                                                                     // VkExtent3D                           extent;
258                         1u,                                                                                                                                                     // deUint32                                     mipLevels;
259                         1u,                                                                                                                                                     // deUint32                                     arrayLayers;
260                         VK_SAMPLE_COUNT_1_BIT,                                                                                                          // VkSampleCountFlagBits        samples;
261                         VK_IMAGE_TILING_OPTIMAL,                                                                                                        // VkImageTiling                        tiling;
262                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,          // VkImageUsageFlags            usage;
263                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                      // VkSharingMode                        sharingMode;
264                         1u,                                                                                                                                                     // deUint32                                     queueFamilyIndexCount;
265                         &queueFamilyIndex,                                                                                                                      // const deUint32*                      pQueueFamilyIndices;
266                         VK_IMAGE_LAYOUT_UNDEFINED,                                                                                                      // VkImageLayout                        initialLayout;
267                 };
268
269                 m_colorImage                    = createImage(vk, vkDevice, &colorImageParams);
270
271                 // Allocate and bind color image memory
272                 m_colorImageAlloc               = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_colorImage), MemoryRequirement::Any);
273                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_colorImage, m_colorImageAlloc->getMemory(), m_colorImageAlloc->getOffset()));
274         }
275
276         // Create depth image
277         {
278                 // Check format support
279                 if (!isSupportedDepthStencilFormat(context.getInstanceInterface(), context.getPhysicalDevice(), m_depthFormat))
280                         throw tcu::NotSupportedError(std::string("Unsupported depth/stencil format: ") + getFormatName(m_depthFormat));
281
282                 const VkImageCreateInfo depthImageParams =
283                 {
284                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                    // VkStructureType                      sType;
285                         DE_NULL,                                                                                // const void*                          pNext;
286                         0u,                                                                                             // VkImageCreateFlags           flags;
287                         VK_IMAGE_TYPE_2D,                                                               // VkImageType                          imageType;
288                         m_depthFormat,                                                                  // VkFormat                                     format;
289                         { m_renderSize.x(), m_renderSize.y(), 1u },             // VkExtent3D                           extent;
290                         1u,                                                                                             // deUint32                                     mipLevels;
291                         1u,                                                                                             // deUint32                                     arrayLayers;
292                         VK_SAMPLE_COUNT_1_BIT,                                                  // VkSampleCountFlagBits        samples;
293                         VK_IMAGE_TILING_OPTIMAL,                                                // VkImageTiling                        tiling;
294                         VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,    // VkImageUsageFlags            usage;
295                         VK_SHARING_MODE_EXCLUSIVE,                                              // VkSharingMode                        sharingMode;
296                         1u,                                                                                             // deUint32                                     queueFamilyIndexCount;
297                         &queueFamilyIndex,                                                              // const deUint32*                      pQueueFamilyIndices;
298                         VK_IMAGE_LAYOUT_UNDEFINED,                                              // VkImageLayout                        initialLayout;
299                 };
300
301                 m_depthImage = createImage(vk, vkDevice, &depthImageParams);
302
303                 // Allocate and bind depth image memory
304                 m_depthImageAlloc = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_depthImage), MemoryRequirement::Any);
305                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_depthImage, m_depthImageAlloc->getMemory(), m_depthImageAlloc->getOffset()));
306         }
307
308         // Create color attachment view
309         {
310                 const VkImageViewCreateInfo colorAttachmentViewParams =
311                 {
312                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
313                         DE_NULL,                                                                                // const void*                          pNext;
314                         0u,                                                                                             // VkImageViewCreateFlags       flags;
315                         *m_colorImage,                                                                  // VkImage                                      image;
316                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
317                         m_colorFormat,                                                                  // VkFormat                                     format;
318                         componentMappingRGBA,                                                   // VkComponentMapping           components;
319                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
320                 };
321
322                 m_colorAttachmentView = createImageView(vk, vkDevice, &colorAttachmentViewParams);
323         }
324
325         // Create depth attachment view
326         {
327                 const VkImageViewCreateInfo depthAttachmentViewParams =
328                 {
329                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
330                         DE_NULL,                                                                                // const void*                          pNext;
331                         0u,                                                                                             // VkImageViewCreateFlags       flags;
332                         *m_depthImage,                                                                  // VkImage                                      image;
333                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
334                         m_depthFormat,                                                                  // VkFormat                                     format;
335                         componentMappingRGBA,                                                   // VkComponentMapping           components;
336                         { VK_IMAGE_ASPECT_DEPTH_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
337                 };
338
339                 m_depthAttachmentView = createImageView(vk, vkDevice, &depthAttachmentViewParams);
340         }
341
342         // Create render pass
343         {
344                 const VkAttachmentDescription colorAttachmentDescription =
345                 {
346                         0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
347                         m_colorFormat,                                                                          // VkFormat                                                     format;
348                         VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
349                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
350                         VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
351                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
352                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
353                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
354                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
355                 };
356
357                 const VkAttachmentDescription depthAttachmentDescription =
358                 {
359                         0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
360                         m_depthFormat,                                                                          // VkFormat                                                     format;
361                         VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
362                         VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
363                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          storeOp;
364                         VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
365                         VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
366                         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,       // VkImageLayout                                        initialLayout;
367                         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,       // VkImageLayout                                        finalLayout;
368                 };
369
370                 const VkAttachmentDescription attachments[2] =
371                 {
372                         colorAttachmentDescription,
373                         depthAttachmentDescription
374                 };
375
376                 const VkAttachmentReference colorAttachmentReference =
377                 {
378                         0u,                                                                                                     // deUint32                     attachment;
379                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
380                 };
381
382                 const VkAttachmentReference depthAttachmentReference =
383                 {
384                         1u,                                                                                                     // deUint32                     attachment;
385                         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL        // VkImageLayout        layout;
386                 };
387
388                 const VkSubpassDescription subpassDescription =
389                 {
390                         0u,                                                                                                     // VkSubpassDescriptionFlags            flags;
391                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                        // VkPipelineBindPoint                          pipelineBindPoint;
392                         0u,                                                                                                     // deUint32                                                     inputAttachmentCount;
393                         DE_NULL,                                                                                        // const VkAttachmentReference*         pInputAttachments;
394                         1u,                                                                                                     // deUint32                                                     colorAttachmentCount;
395                         &colorAttachmentReference,                                                      // const VkAttachmentReference*         pColorAttachments;
396                         DE_NULL,                                                                                        // const VkAttachmentReference*         pResolveAttachments;
397                         &depthAttachmentReference,                                                      // const VkAttachmentReference*         pDepthStencilAttachment;
398                         0u,                                                                                                     // deUint32                                                     preserveAttachmentCount;
399                         DE_NULL                                                                                         // const VkAttachmentReference*         pPreserveAttachments;
400                 };
401
402                 const VkRenderPassCreateInfo renderPassParams =
403                 {
404                         VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
405                         DE_NULL,                                                                                        // const void*                                          pNext;
406                         0u,                                                                                                     // VkRenderPassCreateFlags                      flags;
407                         2u,                                                                                                     // deUint32                                                     attachmentCount;
408                         attachments,                                                                            // const VkAttachmentDescription*       pAttachments;
409                         1u,                                                                                                     // deUint32                                                     subpassCount;
410                         &subpassDescription,                                                            // const VkSubpassDescription*          pSubpasses;
411                         0u,                                                                                                     // deUint32                                                     dependencyCount;
412                         DE_NULL                                                                                         // const VkSubpassDependency*           pDependencies;
413                 };
414
415                 m_renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
416         }
417
418         // Create framebuffer
419         {
420                 const VkImageView attachmentBindInfos[2] =
421                 {
422                         *m_colorAttachmentView,
423                         *m_depthAttachmentView,
424                 };
425
426                 const VkFramebufferCreateInfo framebufferParams =
427                 {
428                         VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                      // VkStructureType                              sType;
429                         DE_NULL,                                                                                        // const void*                                  pNext;
430                         0u,                                                                                                     // VkFramebufferCreateFlags             flags;
431                         *m_renderPass,                                                                          // VkRenderPass                                 renderPass;
432                         2u,                                                                                                     // deUint32                                             attachmentCount;
433                         attachmentBindInfos,                                                            // const VkImageView*                   pAttachments;
434                         (deUint32)m_renderSize.x(),                                                     // deUint32                                             width;
435                         (deUint32)m_renderSize.y(),                                                     // deUint32                                             height;
436                         1u                                                                                                      // deUint32                                             layers;
437                 };
438
439                 m_framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
440         }
441
442         // Create pipeline layout
443         {
444                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
445                 {
446                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
447                         DE_NULL,                                                                                        // const void*                                          pNext;
448                         0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
449                         0u,                                                                                                     // deUint32                                                     setLayoutCount;
450                         DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
451                         0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
452                         DE_NULL                                                                                         // const VkPushConstantRange*           pPushConstantRanges;
453                 };
454
455                 m_pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
456         }
457
458         // Shader modules
459         m_vertexShaderModule    = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_vert"), 0);
460         m_fragmentShaderModule  = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_frag"), 0);
461
462         // Create pipeline
463         {
464                 const VkPipelineShaderStageCreateInfo shaderStages[2] =
465                 {
466                         {
467                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    // VkStructureType                                              sType;
468                                 DE_NULL,                                                                                                // const void*                                                  pNext;
469                                 0u,                                                                                                             // VkPipelineShaderStageCreateFlags             flags;
470                                 VK_SHADER_STAGE_VERTEX_BIT,                                                             // VkShaderStageFlagBits                                stage;
471                                 *m_vertexShaderModule,                                                                  // VkShaderModule                                               module;
472                                 "main",                                                                                                 // const char*                                                  pName;
473                                 DE_NULL                                                                                                 // const VkSpecializationInfo*                  pSpecializationInfo;
474                         },
475                         {
476                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,    // VkStructureType                                              sType;
477                                 DE_NULL,                                                                                                // const void*                                                  pNext;
478                                 0u,                                                                                                             // VkPipelineShaderStageCreateFlags             flags;
479                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                   // VkShaderStageFlagBits                                stage;
480                                 *m_fragmentShaderModule,                                                                // VkShaderModule                                               module;
481                                 "main",                                                                                                 // const char*                                                  pName;
482                                 DE_NULL                                                                                                 // const VkSpecializationInfo*                  pSpecializationInfo;
483                         }
484                 };
485
486                 const VkVertexInputBindingDescription vertexInputBindingDescription =
487                 {
488                         0u,                                                                     // deUint32                                     binding;
489                         sizeof(Vertex4RGBA),                            // deUint32                                     strideInBytes;
490                         VK_VERTEX_INPUT_RATE_VERTEX                     // VkVertexInputStepRate        inputRate;
491                 };
492
493                 const VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
494                 {
495                         {
496                                 0u,                                                                     // deUint32     location;
497                                 0u,                                                                     // deUint32     binding;
498                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
499                                 0u                                                                      // deUint32     offset;
500                         },
501                         {
502                                 1u,                                                                     // deUint32     location;
503                                 0u,                                                                     // deUint32     binding;
504                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
505                                 DE_OFFSET_OF(Vertex4RGBA, color),       // deUint32     offset;
506                         }
507                 };
508
509                 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
510                 {
511                         VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
512                         DE_NULL,                                                                                                                // const void*                                                          pNext;
513                         0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
514                         1u,                                                                                                                             // deUint32                                                                     vertexBindingDescriptionCount;
515                         &vertexInputBindingDescription,                                                                 // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
516                         2u,                                                                                                                             // deUint32                                                                     vertexAttributeDescriptionCount;
517                         vertexInputAttributeDescriptions                                                                // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
518                 };
519
520                 const VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
521                 {
522                         VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
523                         DE_NULL,                                                                                                                // const void*                                                          pNext;
524                         0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
525                         VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,                                                    // VkPrimitiveTopology                                          topology;
526                         false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
527                 };
528
529                 const VkViewport viewport =
530                 {
531                         0.0f,                                           // float        x;
532                         0.0f,                                           // float        y;
533                         (float)m_renderSize.x(),        // float        width;
534                         (float)m_renderSize.y(),        // float        height;
535                         0.0f,                                           // float        minDepth;
536                         1.0f                                            // float        maxDepth;
537                 };
538                 const VkRect2D scissor =
539                 {
540                         { 0, 0 },                                                                                               // VkOffset2D  offset;
541                         { m_renderSize.x(), m_renderSize.y() }                                  // VkExtent2D  extent;
542                 };
543                 const VkPipelineViewportStateCreateInfo viewportStateParams =
544                 {
545                         VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                              sType;
546                         DE_NULL,                                                                                                                // const void*                                                  pNext;
547                         0u,                                                                                                                             // VkPipelineViewportStateCreateFlags   flags;
548                         1u,                                                                                                                             // deUint32                                                             viewportCount;
549                         &viewport,                                                                                                              // const VkViewport*                                    pViewports;
550                         1u,                                                                                                                             // deUint32                                                             scissorCount;
551                         &scissor                                                                                                                // const VkRect2D*                                              pScissors;
552                 };
553
554                 const VkPipelineRasterizationStateCreateInfo rasterStateParams =
555                 {
556                         VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
557                         DE_NULL,                                                                                                                // const void*                                                          pNext;
558                         0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
559                         false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
560                         false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
561                         VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
562                         VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
563                         VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
564                         VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
565                         0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
566                         0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
567                         0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
568                         1.0f,                                                                                                                   // float                                                                        lineWidth;
569                 };
570
571                 const VkPipelineColorBlendAttachmentState colorBlendAttachmentState =
572                 {
573                         false,                                                                                                                                          // VkBool32                                     blendEnable;
574                         VK_BLEND_FACTOR_ONE,                                                                                                            // VkBlendFactor                        srcColorBlendFactor;
575                         VK_BLEND_FACTOR_ZERO,                                                                                                           // VkBlendFactor                        dstColorBlendFactor;
576                         VK_BLEND_OP_ADD,                                                                                                                        // VkBlendOp                            colorBlendOp;
577                         VK_BLEND_FACTOR_ONE,                                                                                                            // VkBlendFactor                        srcAlphaBlendFactor;
578                         VK_BLEND_FACTOR_ZERO,                                                                                                           // VkBlendFactor                        dstAlphaBlendFactor;
579                         VK_BLEND_OP_ADD,                                                                                                                        // VkBlendOp                            alphaBlendOp;
580                         VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |                                           // VkColorComponentFlags        colorWriteMask;
581                                 VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
582                 };
583
584                 const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
585                 {
586                         VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
587                         DE_NULL,                                                                                                        // const void*                                                                  pNext;
588                         0,                                                                                                                      // VkPipelineColorBlendStateCreateFlags                 flags;
589                         false,                                                                                                          // VkBool32                                                                             logicOpEnable;
590                         VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
591                         1u,                                                                                                                     // deUint32                                                                             attachmentCount;
592                         &colorBlendAttachmentState,                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
593                         { 0.0f, 0.0f, 0.0f, 0.0f },                                                                     // float                                                                                blendConstants[4];
594                 };
595
596                 const VkPipelineMultisampleStateCreateInfo      multisampleStateParams  =
597                 {
598                         VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
599                         DE_NULL,                                                                                                        // const void*                                                          pNext;
600                         0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
601                         VK_SAMPLE_COUNT_1_BIT,                                                                          // VkSampleCountFlagBits                                        rasterizationSamples;
602                         false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
603                         0.0f,                                                                                                           // float                                                                        minSampleShading;
604                         DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
605                         false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
606                         false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
607                 };
608
609                 const VkPipelineDynamicStateCreateInfo dynamicStateParams               =
610                 {
611                         VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,           // VkStructureType                                              sType;
612                         DE_NULL,                                                                                                        // const void*                                                  pNext;
613                         0u,                                                                                                                     // VkPipelineDynamicStateCreateFlags    flags;
614                         0u,                                                                                                                     // deUint32                                                             dynamicStateCount;
615                         DE_NULL                                                                                                         // const VkDynamicState*                                pDynamicStates;
616                 };
617
618                 VkPipelineDepthStencilStateCreateInfo depthStencilStateParams =
619                 {
620                         VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // VkStructureType                                                      sType;
621                         DE_NULL,                                                                                                        // const void*                                                          pNext;
622                         0u,                                                                                                                     // VkPipelineDepthStencilStateCreateFlags       flags;
623                         true,                                                                                                           // VkBool32                                                                     depthTestEnable;
624                         true,                                                                                                           // VkBool32                                                                     depthWriteEnable;
625                         VK_COMPARE_OP_LESS,                                                                                     // VkCompareOp                                                          depthCompareOp;
626                         false,                                                                                                          // VkBool32                                                                     depthBoundsTestEnable;
627                         false,                                                                                                          // VkBool32                                                                     stencilTestEnable;
628                         // VkStencilOpState     front;
629                         {
630                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
631                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
632                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
633                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
634                                 0u,                                             // deUint32             compareMask;
635                                 0u,                                             // deUint32             writeMask;
636                                 0u,                                             // deUint32             reference;
637                         },
638                         // VkStencilOpState     back;
639                         {
640                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
641                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
642                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
643                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
644                                 0u,                                             // deUint32             compareMask;
645                                 0u,                                             // deUint32             writeMask;
646                                 0u,                                             // deUint32             reference;
647                         },
648                         -1.0f,                                                                                                          // float                        minDepthBounds;
649                         +1.0f,                                                                                                          // float                        maxDepthBounds;
650                 };
651
652                 const VkGraphicsPipelineCreateInfo graphicsPipelineParams =
653                 {
654                         VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
655                         DE_NULL,                                                                                        // const void*                                                                          pNext;
656                         0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
657                         2u,                                                                                                     // deUint32                                                                                     stageCount;
658                         shaderStages,                                                                           // const VkPipelineShaderStageCreateInfo*                       pStages;
659                         &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
660                         &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
661                         DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
662                         &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
663                         &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
664                         &multisampleStateParams,                                                        // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
665                         &depthStencilStateParams,                                                       // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
666                         &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
667                         &dynamicStateParams,                                                            // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
668                         *m_pipelineLayout,                                                                      // VkPipelineLayout                                                                     layout;
669                         *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
670                         0u,                                                                                                     // deUint32                                                                                     subpass;
671                         0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
672                         0u,                                                                                                     // deInt32                                                                                      basePipelineIndex;
673                 };
674
675                 for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
676                 {
677                         depthStencilStateParams.depthCompareOp  = depthCompareOps[quadNdx];
678                         m_graphicsPipelines[quadNdx]                    = createGraphicsPipeline(vk, vkDevice, DE_NULL, &graphicsPipelineParams);
679                 }
680         }
681
682         // Create vertex buffer
683         {
684                 const VkBufferCreateInfo vertexBufferParams =
685                 {
686                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           // VkStructureType              sType;
687                         DE_NULL,                                                                        // const void*                  pNext;
688                         0u,                                                                                     // VkBufferCreateFlags  flags;
689                         1024u,                                                                          // VkDeviceSize                 size;
690                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,                      // VkBufferUsageFlags   usage;
691                         VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                sharingMode;
692                         1u,                                                                                     // deUint32                             queueFamilyIndexCount;
693                         &queueFamilyIndex                                                       // const deUint32*              pQueueFamilyIndices;
694                 };
695
696                 m_vertices                      = createOverlappingQuads();
697                 m_vertexBuffer          = createBuffer(vk, vkDevice, &vertexBufferParams);
698                 m_vertexBufferAlloc     = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *m_vertexBuffer), MemoryRequirement::HostVisible);
699
700                 VK_CHECK(vk.bindBufferMemory(vkDevice, *m_vertexBuffer, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset()));
701
702                 // Adjust depths
703                 for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
704                         for (int vertexNdx = 0; vertexNdx < 6; vertexNdx++)
705                                 m_vertices[quadNdx * 6 + vertexNdx].position.z() = DepthTest::quadDepths[quadNdx];
706
707                 // Load vertices into vertex buffer
708                 deMemcpy(m_vertexBufferAlloc->getHostPtr(), m_vertices.data(), m_vertices.size() * sizeof(Vertex4RGBA));
709                 flushMappedMemoryRange(vk, vkDevice, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferParams.size);
710         }
711
712         // Create command pool
713         {
714                 const VkCommandPoolCreateInfo cmdPoolParams =
715                 {
716                         VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,     // VkStructureType              sType;
717                         DE_NULL,                                                                        // const void*                  pNext;
718                         VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,           // VkCmdPoolCreateFlags flags;
719                         queueFamilyIndex                                                        // deUint32                             queueFamilyIndex;
720                 };
721
722                 m_cmdPool = createCommandPool(vk, vkDevice, &cmdPoolParams);
723         }
724
725         // Create command buffer
726         {
727                 const VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
728                 {
729                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
730                         DE_NULL,                                                                                // const void*                          pNext;
731                         *m_cmdPool,                                                                             // VkCommandPool                        commandPool;
732                         VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel         level;
733                         1u                                                                                              // deUint32                                     bufferCount;
734                 };
735
736                 const VkCommandBufferBeginInfo cmdBufferBeginInfo =
737                 {
738                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
739                         DE_NULL,                                                                                // const void*                                          pNext;
740                         0u,                                                                                             // VkCommandBufferUsageFlags            flags;
741                         DE_NULL,                                                                                // VkRenderPass                                         renderPass;
742                         0u,                                                                                             // deUint32                                                     subpass;
743                         DE_NULL,                                                                                // VkFramebuffer                                        framebuffer;
744                         false,                                                                                  // VkBool32                                                     occlusionQueryEnable;
745                         0u,                                                                                             // VkQueryControlFlags                          queryFlags;
746                         0u                                                                                              // VkQueryPipelineStatisticFlags        pipelineStatistics;
747                 };
748
749                 const VkClearValue attachmentClearValues[2] =
750                 {
751                         defaultClearValue(m_colorFormat),
752                         defaultClearValue(m_depthFormat),
753                 };
754
755                 const VkRenderPassBeginInfo renderPassBeginInfo =
756                 {
757                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
758                         DE_NULL,                                                                                                // const void*                  pNext;
759                         *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
760                         *m_framebuffer,                                                                                 // VkFramebuffer                framebuffer;
761                         { { 0, 0 }, { m_renderSize.x(), m_renderSize.y() } },   // VkRect2D                             renderArea;
762                         2,                                                                                                              // deUint32                             clearValueCount;
763                         attachmentClearValues                                                                   // const VkClearValue*  pClearValues;
764                 };
765
766                 m_cmdBuffer = allocateCommandBuffer(vk, vkDevice, &cmdBufferAllocateInfo);
767
768                 VK_CHECK(vk.beginCommandBuffer(*m_cmdBuffer, &cmdBufferBeginInfo));
769                 vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
770
771                 const VkDeviceSize              quadOffset              = (m_vertices.size() / DepthTest::QUAD_COUNT) * sizeof(Vertex4RGBA);
772
773                 for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
774                 {
775                         VkDeviceSize vertexBufferOffset = quadOffset * quadNdx;
776
777                         vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_graphicsPipelines[quadNdx]);
778                         vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &m_vertexBuffer.get(), &vertexBufferOffset);
779                         vk.cmdDraw(*m_cmdBuffer, (deUint32)(m_vertices.size() / DepthTest::QUAD_COUNT), 1, 0, 0);
780                 }
781
782                 vk.cmdEndRenderPass(*m_cmdBuffer);
783                 VK_CHECK(vk.endCommandBuffer(*m_cmdBuffer));
784         }
785
786         // Create fence
787         {
788                 const VkFenceCreateInfo fenceParams =
789                 {
790                         VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,    // VkStructureType              sType;
791                         DE_NULL,                                                                // const void*                  pNext;
792                         0u                                                                              // VkFenceCreateFlags   flags;
793                 };
794
795                 m_fence = createFence(vk, vkDevice, &fenceParams);
796         }
797 }
798
799 DepthTestInstance::~DepthTestInstance (void)
800 {
801 }
802
803 tcu::TestStatus DepthTestInstance::iterate (void)
804 {
805         const DeviceInterface&          vk                      = m_context.getDeviceInterface();
806         const VkDevice                          vkDevice        = m_context.getDevice();
807         const VkQueue                           queue           = m_context.getUniversalQueue();
808         const VkSubmitInfo                      submitInfo      =
809         {
810                 VK_STRUCTURE_TYPE_SUBMIT_INFO,  // VkStructureType                      sType;
811                 DE_NULL,                                                // const void*                          pNext;
812                 0u,                                                             // deUint32                                     waitSemaphoreCount;
813                 DE_NULL,                                                // const VkSemaphore*           pWaitSemaphores;
814                 1u,                                                             // deUint32                                     commandBufferCount;
815                 &m_cmdBuffer.get(),                             // const VkCommandBuffer*       pCommandBuffers;
816                 0u,                                                             // deUint32                                     signalSemaphoreCount;
817                 DE_NULL                                                 // const VkSemaphore*           pSignalSemaphores;
818         };
819
820         VK_CHECK(vk.resetFences(vkDevice, 1, &m_fence.get()));
821         VK_CHECK(vk.queueSubmit(queue, 1, &submitInfo, *m_fence));
822         VK_CHECK(vk.waitForFences(vkDevice, 1, &m_fence.get(), true, ~(0ull) /* infinity*/));
823
824         return verifyImage();
825 }
826
827 tcu::TestStatus DepthTestInstance::verifyImage (void)
828 {
829         const tcu::TextureFormat        tcuColorFormat  = mapVkFormat(m_colorFormat);
830         const tcu::TextureFormat        tcuDepthFormat  = mapVkFormat(m_depthFormat);
831         const ColorVertexShader         vertexShader;
832         const ColorFragmentShader       fragmentShader  (tcuColorFormat, tcuDepthFormat);
833         const rr::Program                       program                 (&vertexShader, &fragmentShader);
834         ReferenceRenderer                       refRenderer             (m_renderSize.x(), m_renderSize.y(), 1, tcuColorFormat, tcuDepthFormat, &program);
835         bool                                            compareOk               = false;
836
837         // Render reference image
838         {
839                 for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
840                 {
841                         // Set depth state
842                         rr::RenderState renderState(refRenderer.getViewportState());
843                         renderState.fragOps.depthTestEnabled = true;
844                         renderState.fragOps.depthFunc = mapVkCompareOp(m_depthCompareOps[quadNdx]);
845
846                         refRenderer.draw(renderState,
847                                                          rr::PRIMITIVETYPE_TRIANGLES,
848                                                          std::vector<Vertex4RGBA>(m_vertices.begin() + quadNdx * 6,
849                                                                                                           m_vertices.begin() + (quadNdx + 1) * 6));
850                 }
851         }
852
853         // Compare result with reference image
854         {
855                 const DeviceInterface&                  vk                                      = m_context.getDeviceInterface();
856                 const VkDevice                                  vkDevice                        = m_context.getDevice();
857                 const VkQueue                                   queue                           = m_context.getUniversalQueue();
858                 const deUint32                                  queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
859                 SimpleAllocator                                 allocator                       (vk, vkDevice, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice()));
860                 de::MovePtr<tcu::TextureLevel>  result                          = readColorAttachment(vk, vkDevice, queue, queueFamilyIndex, allocator, *m_colorImage, m_colorFormat, m_renderSize);
861
862                 compareOk = tcu::intThresholdPositionDeviationCompare(m_context.getTestContext().getLog(),
863                                                                                                                           "IntImageCompare",
864                                                                                                                           "Image comparison",
865                                                                                                                           refRenderer.getAccess(),
866                                                                                                                           result->getAccess(),
867                                                                                                                           tcu::UVec4(2, 2, 2, 2),
868                                                                                                                           tcu::IVec3(1, 1, 0),
869                                                                                                                           true,
870                                                                                                                           tcu::COMPARE_LOG_RESULT);
871         }
872
873         if (compareOk)
874                 return tcu::TestStatus::pass("Result image matches reference");
875         else
876                 return tcu::TestStatus::fail("Image mismatch");
877 }
878
879 std::string getFormatCaseName (const VkFormat format)
880 {
881         const std::string       fullName        = getFormatName(format);
882
883         DE_ASSERT(de::beginsWith(fullName, "VK_FORMAT_"));
884
885         return de::toLower(fullName.substr(10));
886 }
887
888 std::string     getCompareOpsName (const VkCompareOp quadDepthOps[DepthTest::QUAD_COUNT])
889 {
890         std::ostringstream name;
891
892         for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
893         {
894                 const std::string       fullOpName      = getCompareOpName(quadDepthOps[quadNdx]);
895
896                 DE_ASSERT(de::beginsWith(fullOpName, "VK_COMPARE_OP_"));
897
898                 name << de::toLower(fullOpName.substr(14));
899
900                 if (quadNdx < DepthTest::QUAD_COUNT - 1)
901                         name << "_";
902         }
903
904         return name.str();
905 }
906
907 std::string     getCompareOpsDescription (const VkCompareOp quadDepthOps[DepthTest::QUAD_COUNT])
908 {
909         std::ostringstream desc;
910         desc << "Draws " << DepthTest::QUAD_COUNT << " quads with depth compare ops: ";
911
912         for (int quadNdx = 0; quadNdx < DepthTest::QUAD_COUNT; quadNdx++)
913         {
914                 desc << getCompareOpName(quadDepthOps[quadNdx]) << " at depth " << DepthTest::quadDepths[quadNdx];
915
916                 if (quadNdx < DepthTest::QUAD_COUNT - 1)
917                         desc << ", ";
918         }
919         return desc.str();
920 }
921
922
923 } // anonymous
924
925 tcu::TestCaseGroup* createDepthTests (tcu::TestContext& testCtx)
926 {
927         const VkFormat depthFormats[] =
928         {
929                 VK_FORMAT_D16_UNORM,
930                 VK_FORMAT_X8_D24_UNORM_PACK32,
931                 VK_FORMAT_D32_SFLOAT,
932                 VK_FORMAT_D16_UNORM_S8_UINT,
933                 VK_FORMAT_D24_UNORM_S8_UINT,
934                 VK_FORMAT_D32_SFLOAT_S8_UINT
935         };
936
937         // Each entry configures the depth compare operators of QUAD_COUNT quads.
938         // All entries cover pair-wise combinations of compare operators.
939         const VkCompareOp depthOps[][DepthTest::QUAD_COUNT] =
940         {
941                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_NOT_EQUAL },
942                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_GREATER },
943                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_LESS_OR_EQUAL },
944                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_GREATER_OR_EQUAL },
945                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_ALWAYS },
946                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_LESS },
947                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_NEVER },
948                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_EQUAL },
949                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_LESS },
950                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_NOT_EQUAL },
951                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_GREATER },
952                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_LESS_OR_EQUAL },
953                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_EQUAL },
954                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_ALWAYS },
955                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_GREATER_OR_EQUAL },
956                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_LESS },
957                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_ALWAYS },
958                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER },
959                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_NOT_EQUAL },
960                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_GREATER_OR_EQUAL },
961                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_NEVER },
962                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_GREATER },
963                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_LESS_OR_EQUAL },
964                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_NOT_EQUAL },
965                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_GREATER_OR_EQUAL },
966                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_LESS_OR_EQUAL },
967                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_LESS },
968                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_EQUAL },
969                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_NEVER },
970                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_LESS_OR_EQUAL },
971                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_EQUAL },
972                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_LESS },
973                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_ALWAYS },
974                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER_OR_EQUAL },
975                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_NEVER },
976                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_GREATER_OR_EQUAL },
977                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_LESS_OR_EQUAL },
978                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_NEVER },
979                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_EQUAL },
980                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_NOT_EQUAL },
981                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_ALWAYS },
982                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_ALWAYS },
983                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS },
984                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_EQUAL },
985                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_GREATER },
986                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_NOT_EQUAL },
987                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS_OR_EQUAL },
988                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_GREATER_OR_EQUAL },
989                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_NEVER },
990                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_LESS },
991                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_ALWAYS },
992                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_GREATER },
993                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_EQUAL },
994                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_NOT_EQUAL },
995                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_LESS },
996                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_NEVER },
997                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_NOT_EQUAL },
998                 { VK_COMPARE_OP_GREATER,                        VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_EQUAL },
999                 { VK_COMPARE_OP_EQUAL,                          VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_LESS_OR_EQUAL },
1000                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_GREATER },
1001                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_NEVER },
1002                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_GREATER },
1003                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_NOT_EQUAL },
1004                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_ALWAYS },
1005                 { VK_COMPARE_OP_LESS_OR_EQUAL,          VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_GREATER },
1006                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS,                             VK_COMPARE_OP_GREATER },
1007                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_GREATER_OR_EQUAL },
1008                 { VK_COMPARE_OP_ALWAYS,                         VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS_OR_EQUAL },
1009                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_LESS },
1010                 { VK_COMPARE_OP_GREATER_OR_EQUAL,       VK_COMPARE_OP_NEVER,                    VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_NEVER },
1011                 { VK_COMPARE_OP_LESS,                           VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_EQUAL,                    VK_COMPARE_OP_EQUAL },
1012                 { VK_COMPARE_OP_NEVER,                          VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_ALWAYS,                   VK_COMPARE_OP_GREATER_OR_EQUAL },
1013                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER,                  VK_COMPARE_OP_ALWAYS },
1014                 { VK_COMPARE_OP_NOT_EQUAL,                      VK_COMPARE_OP_LESS_OR_EQUAL,    VK_COMPARE_OP_NOT_EQUAL,                VK_COMPARE_OP_GREATER }
1015         };
1016
1017         de::MovePtr<tcu::TestCaseGroup> depthTests (new tcu::TestCaseGroup(testCtx, "depth", "Depth tests"));
1018
1019         // Tests for format features
1020         {
1021                 de::MovePtr<tcu::TestCaseGroup> formatFeaturesTests (new tcu::TestCaseGroup(testCtx, "format_features", "Checks depth format features"));
1022
1023                 // Formats that must be supported in all implementations
1024                 addFunctionCase(formatFeaturesTests.get(),
1025                                                 "support_d16_unorm",
1026                                                 "Tests if VK_FORMAT_D16_UNORM is supported as depth/stencil attachment format",
1027                                                 testSupportsDepthStencilFormat,
1028                                                 VK_FORMAT_D16_UNORM);
1029
1030                 // Sets where at least one of the formats must be supported
1031                 const VkFormat  depthOnlyFormats[]              = { VK_FORMAT_X8_D24_UNORM_PACK32, VK_FORMAT_D32_SFLOAT };
1032                 const VkFormat  depthStencilFormats[]   = { VK_FORMAT_D24_UNORM_S8_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT };
1033
1034                 addFunctionCase(formatFeaturesTests.get(),
1035                                                 "support_d24_unorm_or_d32_sfloat",
1036                                                 "Tests if any of VK_FORMAT_D24_UNORM_X8 or VK_FORMAT_D32_SFLOAT are supported as depth/stencil attachment format",
1037                                                 testSupportsAtLeastOneDepthStencilFormat,
1038                                                 std::vector<VkFormat>(depthOnlyFormats, depthOnlyFormats + DE_LENGTH_OF_ARRAY(depthOnlyFormats)));
1039
1040                 addFunctionCase(formatFeaturesTests.get(),
1041                                                 "support_d24_unorm_s8_uint_or_d32_sfloat_s8_uint",
1042                                                 "Tests if any of VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT are supported as depth/stencil attachment format",
1043                                                 testSupportsAtLeastOneDepthStencilFormat,
1044                                                 std::vector<VkFormat>(depthStencilFormats, depthStencilFormats + DE_LENGTH_OF_ARRAY(depthStencilFormats)));
1045
1046                 depthTests->addChild(formatFeaturesTests.release());
1047         }
1048
1049         // Tests for format and compare operators
1050         {
1051                 de::MovePtr<tcu::TestCaseGroup> formatTests (new tcu::TestCaseGroup(testCtx, "format", "Uses different depth formats"));
1052
1053                 for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(depthFormats); formatNdx++)
1054                 {
1055                         de::MovePtr<tcu::TestCaseGroup> formatTest              (new tcu::TestCaseGroup(testCtx,
1056                                                                                                                                                                         getFormatCaseName(depthFormats[formatNdx]).c_str(),
1057                                                                                                                                                                         (std::string("Uses format ") + getFormatName(depthFormats[formatNdx])).c_str()));
1058                         de::MovePtr<tcu::TestCaseGroup> compareOpsTests (new tcu::TestCaseGroup(testCtx, "compare_ops", "Combines depth compare operators"));
1059
1060                         for (size_t opsNdx = 0; opsNdx < DE_LENGTH_OF_ARRAY(depthOps); opsNdx++)
1061                         {
1062                                 compareOpsTests->addChild(new DepthTest(testCtx,
1063                                                                                                                 getCompareOpsName(depthOps[opsNdx]),
1064                                                                                                                 getCompareOpsDescription(depthOps[opsNdx]),
1065                                                                                                                 depthFormats[formatNdx],
1066                                                                                                                 depthOps[opsNdx]));
1067                         }
1068                         formatTest->addChild(compareOpsTests.release());
1069                         formatTests->addChild(formatTest.release());
1070                 }
1071                 depthTests->addChild(formatTests.release());
1072         }
1073
1074         return depthTests.release();
1075 }
1076
1077 } // pipeline
1078 } // vkt