dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / pipeline / vktPipelineMultisampleTests.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 Multisample Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktPipelineMultisampleTests.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 #include <map>
57
58 namespace vkt
59 {
60 namespace pipeline
61 {
62
63 using namespace vk;
64
65 namespace
66 {
67 enum GeometryType
68 {
69         GEOMETRY_TYPE_OPAQUE_TRIANGLE,
70         GEOMETRY_TYPE_OPAQUE_LINE,
71         GEOMETRY_TYPE_OPAQUE_POINT,
72         GEOMETRY_TYPE_OPAQUE_QUAD,
73         GEOMETRY_TYPE_TRANSLUCENT_QUAD,
74         GEOMETRY_TYPE_INVISIBLE_QUAD,
75         GEOMETRY_TYPE_GRADIENT_QUAD
76 };
77
78
79 bool                                                                    isSupportedSampleCount                          (const InstanceInterface& instanceInterface, VkPhysicalDevice physicalDevice, VkSampleCountFlagBits rasterizationSamples);
80 VkPipelineColorBlendAttachmentState             getDefaultColorBlendAttachmentState     (void);
81 deUint32                                                                getUniqueColorsCount                            (const tcu::ConstPixelBufferAccess& image);
82 void                                                                    initMultisamplePrograms                         (SourceCollections& sources, GeometryType geometryType);
83
84 class MultisampleTest : public vkt::TestCase
85 {
86 public:
87
88                                                                                                 MultisampleTest                                         (tcu::TestContext&                                                              testContext,
89                                                                                                                                                                          const std::string&                                                             name,
90                                                                                                                                                                          const std::string&                                                             description,
91                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
92                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
93                                                                                                                                                                          GeometryType                                                                   geometryType);
94         virtual                                                                         ~MultisampleTest                                        (void);
95
96         virtual void                                                            initPrograms                                            (SourceCollections& programCollection) const;
97         virtual TestInstance*                                           createInstance                                          (Context& context) const;
98
99 protected:
100         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
101                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
102                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
103                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
104                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const = 0;
105         VkPipelineMultisampleStateCreateInfo            m_multisampleStateParams;
106         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
107         const GeometryType                                                      m_geometryType;
108         std::vector<VkSampleMask>                                       m_sampleMask;
109 };
110
111 class RasterizationSamplesTest : public MultisampleTest
112 {
113 public:
114                                                                                                 RasterizationSamplesTest                        (tcu::TestContext&              testContext,
115                                                                                                                                                                          const std::string&             name,
116                                                                                                                                                                          const std::string&             description,
117                                                                                                                                                                          VkSampleCountFlagBits  rasterizationSamples,
118                                                                                                                                                                          GeometryType                   geometryType);
119         virtual                                                                         ~RasterizationSamplesTest                       (void) {}
120
121 protected:
122         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
123                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
124                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
125                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
126                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
127
128         static VkPipelineMultisampleStateCreateInfo     getRasterizationSamplesStateParams      (VkSampleCountFlagBits rasterizationSamples);
129 };
130
131 class MinSampleShadingTest : public MultisampleTest
132 {
133 public:
134                                                                                                 MinSampleShadingTest                            (tcu::TestContext&              testContext,
135                                                                                                                                                                          const std::string&             name,
136                                                                                                                                                                          const std::string&             description,
137                                                                                                                                                                          VkSampleCountFlagBits  rasterizationSamples,
138                                                                                                                                                                          float                                  minSampleShading,
139                                                                                                                                                                          GeometryType                   geometryType);
140         virtual                                                                         ~MinSampleShadingTest                           (void) {}
141
142 protected:
143         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
144                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
145                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
146                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
147                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
148
149         static VkPipelineMultisampleStateCreateInfo     getMinSampleShadingStateParams          (VkSampleCountFlagBits rasterizationSamples, float minSampleShading);
150 };
151
152 class SampleMaskTest : public MultisampleTest
153 {
154 public:
155                                                                                                 SampleMaskTest                                          (tcu::TestContext&                                      testContext,
156                                                                                                                                                                          const std::string&                                     name,
157                                                                                                                                                                          const std::string&                                     description,
158                                                                                                                                                                          VkSampleCountFlagBits                          rasterizationSamples,
159                                                                                                                                                                          const std::vector<VkSampleMask>&       sampleMask,
160                                                                                                                                                                          GeometryType                                           geometryType);
161
162         virtual                                                                         ~SampleMaskTest                                         (void) {}
163
164 protected:
165         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
166                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
167                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
168                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
169                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
170
171         static VkPipelineMultisampleStateCreateInfo     getSampleMaskStateParams                        (VkSampleCountFlagBits rasterizationSamples, const std::vector<VkSampleMask>& sampleMask);
172 };
173
174 class AlphaToOneTest : public MultisampleTest
175 {
176 public:
177                                                                                                 AlphaToOneTest                                  (tcu::TestContext&                                      testContext,
178                                                                                                                                                                  const std::string&                                     name,
179                                                                                                                                                                  const std::string&                                     description,
180                                                                                                                                                                  VkSampleCountFlagBits                          rasterizationSamples);
181
182         virtual                                                                         ~AlphaToOneTest                                 (void) {}
183
184 protected:
185         virtual TestInstance*                                           createMultisampleTestInstance   (Context&                                                                               context,
186                                                                                                                                                                  VkPrimitiveTopology                                                    topology,
187                                                                                                                                                                  const std::vector<Vertex4RGBA>&                                vertices,
188                                                                                                                                                                  const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
189                                                                                                                                                                  const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
190
191         static VkPipelineMultisampleStateCreateInfo     getAlphaToOneStateParams                (VkSampleCountFlagBits rasterizationSamples);
192         static VkPipelineColorBlendAttachmentState      getAlphaToOneBlendState                 (void);
193 };
194
195 class AlphaToCoverageTest : public MultisampleTest
196 {
197 public:
198                                                                                                 AlphaToCoverageTest                             (tcu::TestContext&              testContext,
199                                                                                                                                                                  const std::string&             name,
200                                                                                                                                                                  const std::string&             description,
201                                                                                                                                                                  VkSampleCountFlagBits  rasterizationSamples,
202                                                                                                                                                                  GeometryType                   geometryType);
203
204         virtual                                                                         ~AlphaToCoverageTest                    (void) {}
205
206 protected:
207         virtual TestInstance*                                           createMultisampleTestInstance   (Context&                                                                               context,
208                                                                                                                                                                  VkPrimitiveTopology                                                    topology,
209                                                                                                                                                                  const std::vector<Vertex4RGBA>&                                vertices,
210                                                                                                                                                                  const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
211                                                                                                                                                                  const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
212
213         static VkPipelineMultisampleStateCreateInfo     getAlphaToCoverageStateParams   (VkSampleCountFlagBits rasterizationSamples);
214
215         GeometryType                                                            m_geometryType;
216 };
217
218 class MultisampleRenderer
219 {
220 public:
221                                                                                                 MultisampleRenderer                     (Context&                                                                               context,
222                                                                                                                                                          VkFormat                                                                               colorFormat,
223                                                                                                                                                          const tcu::IVec2&                                                              renderSize,
224                                                                                                                                                          VkPrimitiveTopology                                                    topology,
225                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
226                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
227                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState);
228
229         virtual                                                                         ~MultisampleRenderer            (void);
230
231         de::MovePtr<tcu::TextureLevel>                          render                                          (void);
232
233 protected:
234         Context&                                                                        m_context;
235
236         const VkFormat                                                          m_colorFormat;
237         tcu::IVec2                                                                      m_renderSize;
238
239         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
240         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
241
242         Move<VkImage>                                                           m_colorImage;
243         de::MovePtr<Allocation>                                         m_colorImageAlloc;
244         Move<VkImageView>                                                       m_colorAttachmentView;
245
246         Move<VkImage>                                                           m_resolveImage;
247         de::MovePtr<Allocation>                                         m_resolveImageAlloc;
248         Move<VkImageView>                                                       m_resolveAttachmentView;
249
250         Move<VkRenderPass>                                                      m_renderPass;
251         Move<VkFramebuffer>                                                     m_framebuffer;
252
253         Move<VkShaderModule>                                            m_vertexShaderModule;
254         Move<VkShaderModule>                                            m_fragmentShaderModule;
255
256         Move<VkBuffer>                                                          m_vertexBuffer;
257         de::MovePtr<Allocation>                                         m_vertexBufferAlloc;
258
259         Move<VkPipelineLayout>                                          m_pipelineLayout;
260         Move<VkPipeline>                                                        m_graphicsPipeline;
261
262         Move<VkCommandPool>                                                     m_cmdPool;
263         Move<VkCommandBuffer>                                           m_cmdBuffer;
264
265         Move<VkFence>                                                           m_fence;
266 };
267
268 class RasterizationSamplesInstance : public vkt::TestInstance
269 {
270 public:
271                                                                         RasterizationSamplesInstance    (Context&                                                                               context,
272                                                                                                                                          VkPrimitiveTopology                                                    topology,
273                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
274                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
275                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState);
276         virtual                                                 ~RasterizationSamplesInstance   (void) {}
277
278         virtual tcu::TestStatus                 iterate                                                 (void);
279
280 protected:
281         virtual tcu::TestStatus                 verifyImage                                             (const tcu::ConstPixelBufferAccess& result);
282
283         const VkFormat                                  m_colorFormat;
284         const tcu::IVec2                                m_renderSize;
285         const VkPrimitiveTopology               m_primitiveTopology;
286         const std::vector<Vertex4RGBA>  m_vertices;
287         MultisampleRenderer                             m_multisampleRenderer;
288 };
289
290 class MinSampleShadingInstance : public vkt::TestInstance
291 {
292 public:
293                                                                                                 MinSampleShadingInstance        (Context&                                                                               context,
294                                                                                                                                                          VkPrimitiveTopology                                                    topology,
295                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
296                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
297                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState);
298         virtual                                                                         ~MinSampleShadingInstance       (void) {}
299
300         virtual tcu::TestStatus                                         iterate                                         (void);
301
302 protected:
303         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& testShadingImage,
304                                                                                                                                                          const tcu::ConstPixelBufferAccess& minShadingImage,
305                                                                                                                                                          const tcu::ConstPixelBufferAccess& maxShadingImage);
306         const VkFormat                                                          m_colorFormat;
307         const tcu::IVec2                                                        m_renderSize;
308         const VkPrimitiveTopology                                       m_primitiveTopology;
309         const std::vector<Vertex4RGBA>                          m_vertices;
310         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
311         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
312 };
313
314 class SampleMaskInstance : public vkt::TestInstance
315 {
316 public:
317                                                                                                 SampleMaskInstance                      (Context&                                                                               context,
318                                                                                                                                                          VkPrimitiveTopology                                                    topology,
319                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
320                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
321                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState);
322         virtual                                                                         ~SampleMaskInstance                     (void) {}
323
324         virtual tcu::TestStatus                                         iterate                                         (void);
325
326 protected:
327         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& testShadingImage,
328                                                                                                                                                          const tcu::ConstPixelBufferAccess& minShadingImage,
329                                                                                                                                                          const tcu::ConstPixelBufferAccess& maxShadingImage);
330         const VkFormat                                                          m_colorFormat;
331         const tcu::IVec2                                                        m_renderSize;
332         const VkPrimitiveTopology                                       m_primitiveTopology;
333         const std::vector<Vertex4RGBA>                          m_vertices;
334         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
335         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
336 };
337
338 class AlphaToOneInstance : public vkt::TestInstance
339 {
340 public:
341                                                                                                 AlphaToOneInstance                      (Context&                                                                               context,
342                                                                                                                                                          VkPrimitiveTopology                                                    topology,
343                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
344                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
345                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState);
346         virtual                                                                         ~AlphaToOneInstance                     (void) {}
347
348         virtual tcu::TestStatus                                         iterate                                         (void);
349
350 protected:
351         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& alphaOneImage,
352                                                                                                                                                          const tcu::ConstPixelBufferAccess& noAlphaOneImage);
353         const VkFormat                                                          m_colorFormat;
354         const tcu::IVec2                                                        m_renderSize;
355         const VkPrimitiveTopology                                       m_primitiveTopology;
356         const std::vector<Vertex4RGBA>                          m_vertices;
357         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
358         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
359 };
360
361 class AlphaToCoverageInstance : public vkt::TestInstance
362 {
363 public:
364                                                                                                 AlphaToCoverageInstance         (Context&                                                                               context,
365                                                                                                                                                          VkPrimitiveTopology                                                    topology,
366                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
367                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
368                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
369                                                                                                                                                          GeometryType                                                                   geometryType);
370         virtual                                                                         ~AlphaToCoverageInstance        (void) {}
371
372         virtual tcu::TestStatus                                         iterate                                         (void);
373
374 protected:
375         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& result);
376         const VkFormat                                                          m_colorFormat;
377         const tcu::IVec2                                                        m_renderSize;
378         const VkPrimitiveTopology                                       m_primitiveTopology;
379         const std::vector<Vertex4RGBA>                          m_vertices;
380         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
381         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
382         const GeometryType                      m_geometryType;
383 };
384
385
386 // Helper functions
387
388 void initMultisamplePrograms (SourceCollections& sources, GeometryType geometryType)
389 {
390         std::ostringstream vertexSource;
391
392         vertexSource <<
393                 "#version 310 es\n"
394                 "layout(location = 0) in vec4 position;\n"
395                 "layout(location = 1) in vec4 color;\n"
396                 "layout(location = 0) out highp vec4 vtxColor;\n"
397                 "void main (void)\n"
398                 "{\n"
399                 "       gl_Position = position;\n"
400                 "       vtxColor = color;\n"
401                 << (geometryType == GEOMETRY_TYPE_OPAQUE_POINT ? "      gl_PointSize = 3.0f;\n"
402                                                                                                                  : "" )
403                 << "}\n";
404
405         static const char* fragmentSource =
406                 "#version 310 es\n"
407                 "layout(location = 0) in highp vec4 vtxColor;\n"
408                 "layout(location = 0) out highp vec4 fragColor;\n"
409                 "void main (void)\n"
410                 "{\n"
411                 "       fragColor = vtxColor;\n"
412                 "}\n";
413
414         sources.glslSources.add("color_vert") << glu::VertexSource(vertexSource.str());
415         sources.glslSources.add("color_frag") << glu::FragmentSource(fragmentSource);
416 }
417
418 bool isSupportedSampleCount (const InstanceInterface& instanceInterface, VkPhysicalDevice physicalDevice, VkSampleCountFlagBits rasterizationSamples)
419 {
420         VkPhysicalDeviceProperties deviceProperties;
421
422         instanceInterface.getPhysicalDeviceProperties(physicalDevice, &deviceProperties);
423
424         return !!(deviceProperties.limits.framebufferColorSampleCounts & rasterizationSamples);
425 }
426
427 VkPipelineColorBlendAttachmentState getDefaultColorBlendAttachmentState (void)
428 {
429         const VkPipelineColorBlendAttachmentState colorBlendState =
430         {
431                 false,                                                                                                          // VkBool32                                     blendEnable;
432                 VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcColorBlendFactor;
433                 VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstColorBlendFactor;
434                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            colorBlendOp;
435                 VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcAlphaBlendFactor;
436                 VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstAlphaBlendFactor;
437                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            alphaBlendOp;
438                 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |           // VkColorComponentFlags        colorWriteMask;
439                         VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
440         };
441
442         return colorBlendState;
443 }
444
445 deUint32 getUniqueColorsCount (const tcu::ConstPixelBufferAccess& image)
446 {
447         DE_ASSERT(image.getFormat().getPixelSize() == 4);
448
449         std::map<deUint32, deUint32>    histogram; // map<pixel value, number of occurrences>
450         const deUint32                                  pixelCount      = image.getWidth() * image.getHeight() * image.getDepth();
451
452         for (deUint32 pixelNdx = 0; pixelNdx < pixelCount; pixelNdx++)
453         {
454                 const deUint32 pixelValue = *((const deUint32*)image.getDataPtr() + pixelNdx);
455
456                 if (histogram.find(pixelValue) != histogram.end())
457                         histogram[pixelValue]++;
458                 else
459                         histogram[pixelValue] = 1;
460         }
461
462         return (deUint32)histogram.size();
463 }
464
465
466 // MultisampleTest
467
468 MultisampleTest::MultisampleTest (tcu::TestContext&                                                             testContext,
469                                                                   const std::string&                                                    name,
470                                                                   const std::string&                                                    description,
471                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
472                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
473                                                                   GeometryType                                                                  geometryType)
474         : vkt::TestCase                         (testContext, name, description)
475         , m_multisampleStateParams      (multisampleStateParams)
476         , m_colorBlendState                     (blendState)
477         , m_geometryType                        (geometryType)
478 {
479         if (m_multisampleStateParams.pSampleMask)
480         {
481                 // Copy pSampleMask to avoid dependencies with other classes
482
483                 const deUint32 maskCount = deCeilFloatToInt32(float(m_multisampleStateParams.rasterizationSamples) / 32);
484
485                 for (deUint32 maskNdx = 0; maskNdx < maskCount; maskNdx++)
486                         m_sampleMask.push_back(m_multisampleStateParams.pSampleMask[maskNdx]);
487
488                 m_multisampleStateParams.pSampleMask = m_sampleMask.data();
489         }
490 }
491
492 MultisampleTest::~MultisampleTest (void)
493 {
494 }
495
496 void MultisampleTest::initPrograms (SourceCollections& programCollection) const
497 {
498         initMultisamplePrograms(programCollection, m_geometryType);
499 }
500
501 TestInstance* MultisampleTest::createInstance (Context& context) const
502 {
503         VkPrimitiveTopology                     topology;
504         std::vector<Vertex4RGBA>        vertices;
505
506         switch (m_geometryType)
507         {
508                 case GEOMETRY_TYPE_OPAQUE_TRIANGLE:
509                 {
510                         topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
511                         const Vertex4RGBA vertexData[3] =
512                         {
513                                 {
514                                         tcu::Vec4(-0.75f, 0.0f, 0.0f, 1.0f),
515                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
516                                 },
517                                 {
518                                         tcu::Vec4(0.75f, 0.125f, 0.0f, 1.0f),
519                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
520                                 },
521                                 {
522                                         tcu::Vec4(0.75f, -0.125f, 0.0f, 1.0f),
523                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
524                                 }
525                         };
526
527                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 3);
528                         break;
529                 }
530
531                 case GEOMETRY_TYPE_OPAQUE_LINE:
532                 {
533                         topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
534
535                         const Vertex4RGBA vertexData[2] =
536                         {
537                                 {
538                                         tcu::Vec4(-0.75f, 0.25f, 0.0f, 1.0f),
539                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
540                                 },
541                                 {
542                                         tcu::Vec4(0.75f, -0.25f, 0.0f, 1.0f),
543                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
544                                 }
545                         };
546
547                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 2);
548                         break;
549                 }
550
551                 case GEOMETRY_TYPE_OPAQUE_POINT:
552                 {
553                         topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
554
555                         const Vertex4RGBA vertex =
556                         {
557                                 tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f),
558                                 tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
559                         };
560
561                         vertices = std::vector<Vertex4RGBA>(1, vertex);
562                         break;
563                 }
564
565                 case GEOMETRY_TYPE_OPAQUE_QUAD:
566                 case GEOMETRY_TYPE_TRANSLUCENT_QUAD:
567                 case GEOMETRY_TYPE_INVISIBLE_QUAD:
568                 case GEOMETRY_TYPE_GRADIENT_QUAD:
569                 {
570                         topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
571
572                         Vertex4RGBA vertexData[4] =
573                         {
574                                 {
575                                         tcu::Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
576                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
577                                 },
578                                 {
579                                         tcu::Vec4(1.0f, -1.0f, 0.0f, 1.0f),
580                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
581                                 },
582                                 {
583                                         tcu::Vec4(-1.0f, 1.0f, 0.0f, 1.0f),
584                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
585                                 },
586                                 {
587                                         tcu::Vec4(1.0f, 1.0f, 0.0f, 1.0f),
588                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
589                                 }
590                         };
591
592                         if (m_geometryType == GEOMETRY_TYPE_TRANSLUCENT_QUAD)
593                         {
594                                 for (int i = 0; i < 4; i++)
595                                         vertexData[i].color.w() = 0.25f;
596                         }
597                         else if (m_geometryType == GEOMETRY_TYPE_INVISIBLE_QUAD)
598                         {
599                                 for (int i = 0; i < 4; i++)
600                                         vertexData[i].color.w() = 0.0f;
601                         }
602                         else if (m_geometryType == GEOMETRY_TYPE_GRADIENT_QUAD)
603                         {
604                                 vertexData[0].color.w() = 0.0f;
605                                 vertexData[2].color.w() = 0.0f;
606                         }
607
608                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 4);
609                         break;
610                 }
611
612                 default:
613                         topology = VK_PRIMITIVE_TOPOLOGY_LAST;
614                         DE_ASSERT(false);
615         }
616
617         return createMultisampleTestInstance(context, topology, vertices, m_multisampleStateParams, m_colorBlendState);
618 }
619
620
621 // RasterizationSamplesTest
622
623 RasterizationSamplesTest::RasterizationSamplesTest (tcu::TestContext&           testContext,
624                                                                                                         const std::string&              name,
625                                                                                                         const std::string&              description,
626                                                                                                         VkSampleCountFlagBits   rasterizationSamples,
627                                                                                                         GeometryType                    geometryType)
628         : MultisampleTest       (testContext, name, description, getRasterizationSamplesStateParams(rasterizationSamples), getDefaultColorBlendAttachmentState(), geometryType)
629 {
630 }
631
632 VkPipelineMultisampleStateCreateInfo RasterizationSamplesTest::getRasterizationSamplesStateParams (VkSampleCountFlagBits rasterizationSamples)
633 {
634         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
635         {
636                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
637                 DE_NULL,                                                                                                        // const void*                                                          pNext;
638                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
639                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
640                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
641                 0.0f,                                                                                                           // float                                                                        minSampleShading;
642                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
643                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
644                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
645         };
646
647         return multisampleStateParams;
648 }
649
650 TestInstance* RasterizationSamplesTest::createMultisampleTestInstance (Context&                                                                         context,
651                                                                                                                                            VkPrimitiveTopology                                                  topology,
652                                                                                                                                            const std::vector<Vertex4RGBA>&                              vertices,
653                                                                                                                                            const VkPipelineMultisampleStateCreateInfo&  multisampleStateParams,
654                                                                                                                                            const VkPipelineColorBlendAttachmentState&   colorBlendState) const
655 {
656         return new RasterizationSamplesInstance(context, topology, vertices, multisampleStateParams, colorBlendState);
657 }
658
659
660 // MinSampleShadingTest
661
662 MinSampleShadingTest::MinSampleShadingTest (tcu::TestContext&           testContext,
663                                                                                         const std::string&              name,
664                                                                                         const std::string&              description,
665                                                                                         VkSampleCountFlagBits   rasterizationSamples,
666                                                                                         float                                   minSampleShading,
667                                                                                         GeometryType                    geometryType)
668         : MultisampleTest       (testContext, name, description, getMinSampleShadingStateParams(rasterizationSamples, minSampleShading), getDefaultColorBlendAttachmentState(), geometryType)
669 {
670 }
671
672 TestInstance* MinSampleShadingTest::createMultisampleTestInstance (Context&                                                                             context,
673                                                                                                                                    VkPrimitiveTopology                                                  topology,
674                                                                                                                                    const std::vector<Vertex4RGBA>&                              vertices,
675                                                                                                                                    const VkPipelineMultisampleStateCreateInfo&  multisampleStateParams,
676                                                                                                                                    const VkPipelineColorBlendAttachmentState&   colorBlendState) const
677 {
678         return new MinSampleShadingInstance(context, topology, vertices, multisampleStateParams, colorBlendState);
679 }
680
681 VkPipelineMultisampleStateCreateInfo MinSampleShadingTest::getMinSampleShadingStateParams (VkSampleCountFlagBits rasterizationSamples, float minSampleShading)
682 {
683         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
684         {
685                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
686                 DE_NULL,                                                                                                        // const void*                                                          pNext;
687                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
688                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
689                 true,                                                                                                           // VkBool32                                                                     sampleShadingEnable;
690                 minSampleShading,                                                                                       // float                                                                        minSampleShading;
691                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
692                 false,                                                                                                          //  VkBool32                                                            alphaToCoverageEnable;
693                 false                                                                                                           //  VkBool32                                                            alphaToOneEnable;
694         };
695
696         return multisampleStateParams;
697 }
698
699
700 // SampleMaskTest
701
702 SampleMaskTest::SampleMaskTest (tcu::TestContext&                                       testContext,
703                                                                 const std::string&                                      name,
704                                                                 const std::string&                                      description,
705                                                                 VkSampleCountFlagBits                           rasterizationSamples,
706                                                                 const std::vector<VkSampleMask>&        sampleMask,
707                                                                 GeometryType                                            geometryType)
708         : MultisampleTest       (testContext, name, description, getSampleMaskStateParams(rasterizationSamples, sampleMask), getDefaultColorBlendAttachmentState(), geometryType)
709 {
710 }
711
712 TestInstance* SampleMaskTest::createMultisampleTestInstance (Context&                                                                           context,
713                                                                                                                          VkPrimitiveTopology                                                    topology,
714                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
715                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
716                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const
717 {
718         return new SampleMaskInstance(context, topology,vertices, multisampleStateParams, colorBlendState);
719 }
720
721 VkPipelineMultisampleStateCreateInfo SampleMaskTest::getSampleMaskStateParams (VkSampleCountFlagBits rasterizationSamples, const std::vector<VkSampleMask>& sampleMask)
722 {
723         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
724         {
725                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
726                 DE_NULL,                                                                                                        // const void*                                                          pNext;
727                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
728                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
729                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
730                 0.0f,                                                                                                           // float                                                                        minSampleShading;
731                 sampleMask.data(),                                                                                      // const VkSampleMask*                                          pSampleMask;
732                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
733                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
734         };
735
736         return multisampleStateParams;
737 }
738
739
740 // AlphaToOneTest
741
742 AlphaToOneTest::AlphaToOneTest (tcu::TestContext&               testContext,
743                                                                 const std::string&              name,
744                                                                 const std::string&              description,
745                                                                 VkSampleCountFlagBits   rasterizationSamples)
746         : MultisampleTest       (testContext, name, description, getAlphaToOneStateParams(rasterizationSamples), getAlphaToOneBlendState(), GEOMETRY_TYPE_GRADIENT_QUAD)
747 {
748 }
749
750 TestInstance* AlphaToOneTest::createMultisampleTestInstance (Context&                                                                           context,
751                                                                                                                          VkPrimitiveTopology                                                    topology,
752                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
753                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
754                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const
755 {
756         return new AlphaToOneInstance(context, topology, vertices, multisampleStateParams, colorBlendState);
757 }
758
759 VkPipelineMultisampleStateCreateInfo AlphaToOneTest::getAlphaToOneStateParams (VkSampleCountFlagBits rasterizationSamples)
760 {
761         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
762         {
763                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
764                 DE_NULL,                                                                                                        // const void*                                                          pNext;
765                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
766                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
767                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
768                 0.0f,                                                                                                           // float                                                                        minSampleShading;
769                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
770                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
771                 true                                                                                                            // VkBool32                                                                     alphaToOneEnable;
772         };
773
774         return multisampleStateParams;
775 }
776
777 VkPipelineColorBlendAttachmentState AlphaToOneTest::getAlphaToOneBlendState (void)
778 {
779         const VkPipelineColorBlendAttachmentState colorBlendState =
780         {
781                 true,                                                                                                           // VkBool32                                     blendEnable;
782                 VK_BLEND_FACTOR_SRC_ALPHA,                                                                      // VkBlendFactor                        srcColorBlendFactor;
783                 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,                                            // VkBlendFactor                        dstColorBlendFactor;
784                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            colorBlendOp;
785                 VK_BLEND_FACTOR_SRC_ALPHA,                                                                      // VkBlendFactor                        srcAlphaBlendFactor;
786                 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,                                            // VkBlendFactor                        dstAlphaBlendFactor;
787                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            alphaBlendOp;
788                 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |           // VkColorComponentFlags        colorWriteMask;
789                         VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
790         };
791
792         return colorBlendState;
793 }
794
795
796 // AlphaToCoverageTest
797
798 AlphaToCoverageTest::AlphaToCoverageTest (tcu::TestContext&                     testContext,
799                                                                                   const std::string&            name,
800                                                                                   const std::string&            description,
801                                                                                   VkSampleCountFlagBits         rasterizationSamples,
802                                                                                   GeometryType                          geometryType)
803         : MultisampleTest       (testContext, name, description, getAlphaToCoverageStateParams(rasterizationSamples), getDefaultColorBlendAttachmentState(), geometryType)
804         , m_geometryType        (geometryType)
805 {
806 }
807
808 TestInstance* AlphaToCoverageTest::createMultisampleTestInstance (Context&                                                                              context,
809                                                                                                                                   VkPrimitiveTopology                                                   topology,
810                                                                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
811                                                                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
812                                                                                                                                   const VkPipelineColorBlendAttachmentState&    colorBlendState) const
813 {
814         return new AlphaToCoverageInstance(context, topology, vertices, multisampleStateParams, colorBlendState, m_geometryType);
815 }
816
817 VkPipelineMultisampleStateCreateInfo AlphaToCoverageTest::getAlphaToCoverageStateParams (VkSampleCountFlagBits rasterizationSamples)
818 {
819         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
820         {
821                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
822                 DE_NULL,                                                                                                        // const void*                                                          pNext;
823                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
824                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
825                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
826                 0.0f,                                                                                                           // float                                                                        minSampleShading;
827                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
828                 true,                                                                                                           // VkBool32                                                                     alphaToCoverageEnable;
829                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
830         };
831
832         return multisampleStateParams;
833 }
834
835 // RasterizationSamplesInstance
836
837 RasterizationSamplesInstance::RasterizationSamplesInstance (Context&                                                                            context,
838                                                                                                                         VkPrimitiveTopology                                                             topology,
839                                                                                                                         const std::vector<Vertex4RGBA>&                                 vertices,
840                                                                                                                         const VkPipelineMultisampleStateCreateInfo&             multisampleStateParams,
841                                                                                                                         const VkPipelineColorBlendAttachmentState&              blendState)
842         : vkt::TestInstance             (context)
843         , m_colorFormat                 (VK_FORMAT_R8G8B8A8_UNORM)
844         , m_renderSize                  (32, 32)
845         , m_primitiveTopology   (topology)
846         , m_vertices                    (vertices)
847         , m_multisampleRenderer (context, m_colorFormat, m_renderSize, topology, vertices, multisampleStateParams, blendState)
848 {
849 }
850
851 tcu::TestStatus RasterizationSamplesInstance::iterate (void)
852 {
853         de::MovePtr<tcu::TextureLevel> level(m_multisampleRenderer.render());
854         return verifyImage(level->getAccess());
855 }
856
857 tcu::TestStatus RasterizationSamplesInstance::verifyImage (const tcu::ConstPixelBufferAccess& result)
858 {
859         // Verify range of unique pixels
860         {
861                 const deUint32  numUniqueColors = getUniqueColorsCount(result);
862                 const deUint32  minUniqueColors = 3;
863
864                 tcu::TestLog& log = m_context.getTestContext().getLog();
865
866                 log << tcu::TestLog::Message
867                         << "\nMin. unique colors expected: " << minUniqueColors << "\n"
868                         << "Unique colors found: " << numUniqueColors << "\n"
869                         << tcu::TestLog::EndMessage;
870
871                 if (numUniqueColors < minUniqueColors)
872                         return tcu::TestStatus::fail("Unique colors out of expected bounds");
873         }
874
875         // Verify shape of the rendered primitive (fuzzy-compare)
876         {
877                 const tcu::TextureFormat        tcuColorFormat  = mapVkFormat(m_colorFormat);
878                 const tcu::TextureFormat        tcuDepthFormat  = tcu::TextureFormat();
879                 const ColorVertexShader         vertexShader;
880                 const ColorFragmentShader       fragmentShader  (tcuColorFormat, tcuDepthFormat);
881                 const rr::Program                       program                 (&vertexShader, &fragmentShader);
882                 ReferenceRenderer                       refRenderer             (m_renderSize.x(), m_renderSize.y(), 1, tcuColorFormat, tcuDepthFormat, &program);
883                 rr::RenderState                         renderState             (refRenderer.getViewportState());
884
885                 if (m_primitiveTopology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
886                 {
887                         VkPhysicalDeviceProperties deviceProperties;
888
889                         m_context.getInstanceInterface().getPhysicalDeviceProperties(m_context.getPhysicalDevice(), &deviceProperties);
890
891                         // gl_PointSize is clamped to pointSizeRange
892                         renderState.point.pointSize = deFloatMin(3.0f, deviceProperties.limits.pointSizeRange[1]);
893                 }
894
895                 refRenderer.colorClear(tcu::Vec4(0.0f));
896                 refRenderer.draw(renderState, mapVkPrimitiveTopology(m_primitiveTopology), m_vertices);
897
898                 if (!tcu::fuzzyCompare(m_context.getTestContext().getLog(), "FuzzyImageCompare", "Image comparison", refRenderer.getAccess(), result, 0.05f, tcu::COMPARE_LOG_RESULT))
899                         return tcu::TestStatus::fail("Primitive has unexpected shape");
900
901         }
902
903         return tcu::TestStatus::pass("Primitive rendered, unique colors within expected bounds");
904 }
905
906
907 // MinSampleShadingInstance
908
909 MinSampleShadingInstance::MinSampleShadingInstance (Context&                                                                    context,
910                                                                                                         VkPrimitiveTopology                                                     topology,
911                                                                                                         const std::vector<Vertex4RGBA>&                         vertices,
912                                                                                                         const VkPipelineMultisampleStateCreateInfo&     multisampleStateParams,
913                                                                                                         const VkPipelineColorBlendAttachmentState&      colorBlendState)
914         : vkt::TestInstance                     (context)
915         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
916         , m_renderSize                          (32, 32)
917         , m_primitiveTopology           (topology)
918         , m_vertices                            (vertices)
919         , m_multisampleStateParams      (multisampleStateParams)
920         , m_colorBlendState                     (colorBlendState)
921 {
922         VkPhysicalDeviceFeatures deviceFeatures;
923
924         m_context.getInstanceInterface().getPhysicalDeviceFeatures(m_context.getPhysicalDevice(), &deviceFeatures);
925
926         if (!deviceFeatures.sampleRateShading)
927                 throw tcu::NotSupportedError("Sample shading is not supported");
928 }
929
930 tcu::TestStatus MinSampleShadingInstance::iterate (void)
931 {
932         de::MovePtr<tcu::TextureLevel>                          testShadingImage;
933         de::MovePtr<tcu::TextureLevel>                          minShadingImage;
934         de::MovePtr<tcu::TextureLevel>                          maxShadingImage;
935
936         // Render with test minSampleShading
937         {
938                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState);
939                 testShadingImage = renderer.render();
940         }
941
942         // Render with minSampleShading = 0.0f
943         {
944                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
945                 multisampleParams.minSampleShading = 0.0f;
946
947                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState);
948                 minShadingImage = renderer.render();
949         }
950
951         // Render with minSampleShading = 1.0f
952         {
953                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
954                 multisampleParams.minSampleShading = 1.0f;
955
956                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState);
957                 maxShadingImage = renderer.render();
958         }
959
960         return verifyImage(testShadingImage->getAccess(), minShadingImage->getAccess(), maxShadingImage->getAccess());
961 }
962
963 tcu::TestStatus MinSampleShadingInstance::verifyImage (const tcu::ConstPixelBufferAccess& testShadingImage, const tcu::ConstPixelBufferAccess& minShadingImage, const tcu::ConstPixelBufferAccess& maxShadingImage)
964 {
965         const deUint32  testColorCount  = getUniqueColorsCount(testShadingImage);
966         const deUint32  minColorCount   = getUniqueColorsCount(minShadingImage);
967         const deUint32  maxColorCount   = getUniqueColorsCount(maxShadingImage);
968
969         tcu::TestLog& log = m_context.getTestContext().getLog();
970
971         log << tcu::TestLog::Message
972                 << "\nColors found: " << testColorCount << "\n"
973                 << "Min. colors expected: " << minColorCount << "\n"
974                 << "Max. colors expected: " << maxColorCount << "\n"
975                 << tcu::TestLog::EndMessage;
976
977         if (minColorCount > testColorCount || testColorCount > maxColorCount)
978                 return tcu::TestStatus::fail("Unique colors out of expected bounds");
979         else
980                 return tcu::TestStatus::pass("Unique colors within expected bounds");
981 }
982
983 SampleMaskInstance::SampleMaskInstance (Context&                                                                                context,
984                                                                                 VkPrimitiveTopology                                                             topology,
985                                                                                 const std::vector<Vertex4RGBA>&                                 vertices,
986                                                                                 const VkPipelineMultisampleStateCreateInfo&             multisampleStateParams,
987                                                                                 const VkPipelineColorBlendAttachmentState&              blendState)
988         : vkt::TestInstance                     (context)
989         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
990         , m_renderSize                          (32, 32)
991         , m_primitiveTopology           (topology)
992         , m_vertices                            (vertices)
993         , m_multisampleStateParams      (multisampleStateParams)
994         , m_colorBlendState                     (blendState)
995 {
996 }
997
998 tcu::TestStatus SampleMaskInstance::iterate (void)
999 {
1000         de::MovePtr<tcu::TextureLevel>                          testSampleMaskImage;
1001         de::MovePtr<tcu::TextureLevel>                          minSampleMaskImage;
1002         de::MovePtr<tcu::TextureLevel>                          maxSampleMaskImage;
1003
1004         // Render with test flags
1005         {
1006                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState);
1007                 testSampleMaskImage = renderer.render();
1008         }
1009
1010         // Render with all flags off
1011         {
1012                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1013                 const std::vector<VkSampleMask>                 sampleMask                      (multisampleParams.rasterizationSamples / 32, (VkSampleMask)0);
1014
1015                 multisampleParams.pSampleMask = sampleMask.data();
1016
1017                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState);
1018                 minSampleMaskImage = renderer.render();
1019         }
1020
1021         // Render with all flags on
1022         {
1023                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1024                 const std::vector<VkSampleMask>                 sampleMask                      (multisampleParams.rasterizationSamples / 32, ~((VkSampleMask)0));
1025
1026                 multisampleParams.pSampleMask = sampleMask.data();
1027
1028                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState);
1029                 maxSampleMaskImage = renderer.render();
1030         }
1031
1032         return verifyImage(testSampleMaskImage->getAccess(), minSampleMaskImage->getAccess(), maxSampleMaskImage->getAccess());
1033 }
1034
1035 tcu::TestStatus SampleMaskInstance::verifyImage (const tcu::ConstPixelBufferAccess& testSampleMaskImage,
1036                                                                                                  const tcu::ConstPixelBufferAccess& minSampleMaskImage,
1037                                                                                                  const tcu::ConstPixelBufferAccess& maxSampleMaskImage)
1038 {
1039         const deUint32  testColorCount  = getUniqueColorsCount(testSampleMaskImage);
1040         const deUint32  minColorCount   = getUniqueColorsCount(minSampleMaskImage);
1041         const deUint32  maxColorCount   = getUniqueColorsCount(maxSampleMaskImage);
1042
1043         tcu::TestLog& log = m_context.getTestContext().getLog();
1044
1045         log << tcu::TestLog::Message
1046                 << "\nColors found: " << testColorCount << "\n"
1047                 << "Min. colors expected: " << minColorCount << "\n"
1048                 << "Max. colors expected: " << maxColorCount << "\n"
1049                 << tcu::TestLog::EndMessage;
1050
1051         if (minColorCount > testColorCount || testColorCount > maxColorCount)
1052                 return tcu::TestStatus::fail("Unique colors out of expected bounds");
1053         else
1054                 return tcu::TestStatus::pass("Unique colors within expected bounds");
1055 }
1056
1057 tcu::TestStatus testRasterSamplesConsistency (Context& context, GeometryType geometryType)
1058 {
1059         // Use triangle only.
1060         DE_UNREF(geometryType);
1061
1062         const VkSampleCountFlagBits samples[] =
1063         {
1064                 VK_SAMPLE_COUNT_1_BIT,
1065                 VK_SAMPLE_COUNT_2_BIT,
1066                 VK_SAMPLE_COUNT_4_BIT,
1067                 VK_SAMPLE_COUNT_8_BIT,
1068                 VK_SAMPLE_COUNT_16_BIT,
1069                 VK_SAMPLE_COUNT_32_BIT,
1070                 VK_SAMPLE_COUNT_64_BIT
1071         };
1072
1073         const Vertex4RGBA vertexData[3] =
1074         {
1075                 {
1076                         tcu::Vec4(-0.75f, 0.0f, 0.0f, 1.0f),
1077                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1078                 },
1079                 {
1080                         tcu::Vec4(0.75f, 0.125f, 0.0f, 1.0f),
1081                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1082                 },
1083                 {
1084                         tcu::Vec4(0.75f, -0.125f, 0.0f, 1.0f),
1085                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1086                 }
1087         };
1088
1089         const std::vector<Vertex4RGBA>  vertices                        (vertexData, vertexData + 3);
1090         deUint32                                                prevUniqueColors        = 2;
1091         int                                                             renderCount                     = 0;
1092
1093         // Do not render with 1 sample (start with samplesNdx = 1).
1094         for (int samplesNdx = 1; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1095         {
1096                 if (!isSupportedSampleCount(context.getInstanceInterface(), context.getPhysicalDevice(), samples[samplesNdx]))
1097                         continue;
1098
1099                 const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
1100                 {
1101                         VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
1102                         DE_NULL,                                                                                                        // const void*                                                          pNext;
1103                         0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
1104                         samples[samplesNdx],                                                                            // VkSampleCountFlagBits                                        rasterizationSamples;
1105                         false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
1106                         0.0f,                                                                                                           // float                                                                        minSampleShading;
1107                         DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
1108                         false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
1109                         false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
1110                 };
1111
1112                 MultisampleRenderer                             renderer                (context, VK_FORMAT_R8G8B8A8_UNORM, tcu::IVec2(32, 32), VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, vertices, multisampleStateParams, getDefaultColorBlendAttachmentState());
1113                 de::MovePtr<tcu::TextureLevel>  result                  = renderer.render();
1114                 const deUint32                                  uniqueColors    = getUniqueColorsCount(result->getAccess());
1115
1116                 renderCount++;
1117
1118                 if (prevUniqueColors > uniqueColors)
1119                 {
1120                         std::ostringstream message;
1121
1122                         message << "More unique colors generated with " << samples[samplesNdx - 1] << " than with " << samples[samplesNdx];
1123                         return tcu::TestStatus::fail(message.str());
1124                 }
1125
1126                 prevUniqueColors = uniqueColors;
1127         }
1128
1129         if (renderCount == 0)
1130                 throw tcu::NotSupportedError("Multisampling is unsupported");
1131
1132         return tcu::TestStatus::pass("Number of unique colors increases as the sample count increases");
1133 }
1134
1135
1136 // AlphaToOneInstance
1137
1138 AlphaToOneInstance::AlphaToOneInstance (Context&                                                                        context,
1139                                                                                 VkPrimitiveTopology                                                     topology,
1140                                                                                 const std::vector<Vertex4RGBA>&                         vertices,
1141                                                                                 const VkPipelineMultisampleStateCreateInfo&     multisampleStateParams,
1142                                                                                 const VkPipelineColorBlendAttachmentState&      blendState)
1143         : vkt::TestInstance                     (context)
1144         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1145         , m_renderSize                          (32, 32)
1146         , m_primitiveTopology           (topology)
1147         , m_vertices                            (vertices)
1148         , m_multisampleStateParams      (multisampleStateParams)
1149         , m_colorBlendState                     (blendState)
1150 {
1151         VkPhysicalDeviceFeatures deviceFeatures;
1152
1153         context.getInstanceInterface().getPhysicalDeviceFeatures(context.getPhysicalDevice(), &deviceFeatures);
1154
1155         if (!deviceFeatures.alphaToOne)
1156                 throw tcu::NotSupportedError("Alpha-to-one is not supported");
1157 }
1158
1159 tcu::TestStatus AlphaToOneInstance::iterate     (void)
1160 {
1161         DE_ASSERT(m_multisampleStateParams.alphaToOneEnable);
1162         DE_ASSERT(m_colorBlendState.blendEnable);
1163
1164         de::MovePtr<tcu::TextureLevel>  alphaOneImage;
1165         de::MovePtr<tcu::TextureLevel>  noAlphaOneImage;
1166
1167         // Render with blend enabled and alpha to one on
1168         {
1169                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState);
1170                 alphaOneImage = renderer.render();
1171         }
1172
1173         // Render with blend enabled and alpha to one off
1174         {
1175                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1176                 multisampleParams.alphaToOneEnable = false;
1177
1178                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState);
1179                 noAlphaOneImage = renderer.render();
1180         }
1181
1182         return verifyImage(alphaOneImage->getAccess(), noAlphaOneImage->getAccess());
1183 }
1184
1185 tcu::TestStatus AlphaToOneInstance::verifyImage (const tcu::ConstPixelBufferAccess&     alphaOneImage,
1186                                                                                                  const tcu::ConstPixelBufferAccess&     noAlphaOneImage)
1187 {
1188         for (int y = 0; y < m_renderSize.y(); y++)
1189         {
1190                 for (int x = 0; x < m_renderSize.x(); x++)
1191                 {
1192                         if (!tcu::boolAll(tcu::greaterThanEqual(alphaOneImage.getPixel(x, y), noAlphaOneImage.getPixel(x, y))))
1193                         {
1194                                 std::ostringstream message;
1195                                 message << "Unsatisfied condition: " << alphaOneImage.getPixel(x, y) << " >= " << noAlphaOneImage.getPixel(x, y);
1196                                 return tcu::TestStatus::fail(message.str());
1197                         }
1198                 }
1199         }
1200
1201         return tcu::TestStatus::pass("Image rendered with alpha-to-one contains pixels of image rendered with no alpha-to-one");
1202 }
1203
1204
1205 // AlphaToCoverageInstance
1206
1207 AlphaToCoverageInstance::AlphaToCoverageInstance (Context&                                                                              context,
1208                                                                                                   VkPrimitiveTopology                                                   topology,
1209                                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
1210                                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1211                                                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
1212                                                                                                   GeometryType                                                                  geometryType)
1213         : vkt::TestInstance                     (context)
1214         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1215         , m_renderSize                          (32, 32)
1216         , m_primitiveTopology           (topology)
1217         , m_vertices                            (vertices)
1218         , m_multisampleStateParams      (multisampleStateParams)
1219         , m_colorBlendState                     (blendState)
1220         , m_geometryType                        (geometryType)
1221 {
1222 }
1223
1224 tcu::TestStatus AlphaToCoverageInstance::iterate (void)
1225 {
1226         DE_ASSERT(m_multisampleStateParams.alphaToCoverageEnable);
1227
1228         de::MovePtr<tcu::TextureLevel>  result;
1229         MultisampleRenderer                             renderer        (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState);
1230
1231         result = renderer.render();
1232
1233         return verifyImage(result->getAccess());
1234 }
1235
1236 tcu::TestStatus AlphaToCoverageInstance::verifyImage (const tcu::ConstPixelBufferAccess&        result)
1237 {
1238         float maxColorValue;
1239
1240         switch (m_geometryType)
1241         {
1242                 case GEOMETRY_TYPE_OPAQUE_QUAD:
1243                         maxColorValue = 1.01f;
1244                         break;
1245
1246                 case GEOMETRY_TYPE_TRANSLUCENT_QUAD:
1247                         maxColorValue = 0.5f;
1248                         break;
1249
1250                 case GEOMETRY_TYPE_INVISIBLE_QUAD:
1251                         maxColorValue = 0.01f;
1252                         break;
1253
1254                 default:
1255                         maxColorValue = 0.0f; // Garbage
1256                         DE_ASSERT(false);
1257         }
1258
1259         for (int y = 0; y < m_renderSize.y(); y++)
1260         {
1261                 for (int x = 0; x < m_renderSize.x(); x++)
1262                 {
1263                         if (result.getPixel(x, y).x() > maxColorValue)
1264                         {
1265                                 std::ostringstream message;
1266                                 message << "Pixel is not below the threshold value (" << result.getPixel(x, y).x() << " > " << maxColorValue << ")";
1267                                 return tcu::TestStatus::fail(message.str());
1268                         }
1269                 }
1270         }
1271
1272         return tcu::TestStatus::pass("Image matches reference value");
1273 }
1274
1275
1276 // MultisampleRenderer
1277
1278 MultisampleRenderer::MultisampleRenderer (Context&                                                                              context,
1279                                                                                   VkFormat                                                                              colorFormat,
1280                                                                                   const tcu::IVec2&                                                             renderSize,
1281                                                                                   VkPrimitiveTopology                                                   topology,
1282                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
1283                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1284                                                                                   const VkPipelineColorBlendAttachmentState&    blendState)
1285
1286         : m_context                                     (context)
1287         , m_colorFormat                         (colorFormat)
1288         , m_renderSize                          (renderSize)
1289         , m_multisampleStateParams      (multisampleStateParams)
1290         , m_colorBlendState                     (blendState)
1291 {
1292         if (!isSupportedSampleCount(context.getInstanceInterface(), context.getPhysicalDevice(), multisampleStateParams.rasterizationSamples))
1293                 throw tcu::NotSupportedError("Unsupported number of rasterization samples");
1294
1295         const DeviceInterface&          vk                                              = context.getDeviceInterface();
1296         const VkDevice                          vkDevice                                = context.getDevice();
1297         const deUint32                          queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
1298         SimpleAllocator                         memAlloc                                (vk, vkDevice, getPhysicalDeviceMemoryProperties(context.getInstanceInterface(), context.getPhysicalDevice()));
1299         const VkComponentMapping        componentMappingRGBA    = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
1300
1301         // Create color image
1302         {
1303                 const VkImageCreateInfo colorImageParams =
1304                 {
1305                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                            // VkStructureType                      sType;
1306                         DE_NULL,                                                                                                                                        // const void*                          pNext;
1307                         0u,                                                                                                                                                     // VkImageCreateFlags           flags;
1308                         VK_IMAGE_TYPE_2D,                                                                                                                       // VkImageType                          imageType;
1309                         m_colorFormat,                                                                                                                          // VkFormat                                     format;
1310                         { m_renderSize.x(), m_renderSize.y(), 1u },                                                                     // VkExtent3D                           extent;
1311                         1u,                                                                                                                                                     // deUint32                                     mipLevels;
1312                         1u,                                                                                                                                                     // deUint32                                     arrayLayers;
1313                         m_multisampleStateParams.rasterizationSamples,                                                          // VkSampleCountFlagBits        samples;
1314                         VK_IMAGE_TILING_OPTIMAL,                                                                                                        // VkImageTiling                        tiling;
1315                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,          // VkImageUsageFlags            usage;
1316                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                      // VkSharingMode                        sharingMode;
1317                         1u,                                                                                                                                                     // deUint32                                     queueFamilyIndexCount;
1318                         &queueFamilyIndex,                                                                                                                      // const deUint32*                      pQueueFamilyIndices;
1319                         VK_IMAGE_LAYOUT_UNDEFINED,                                                                                                      // VkImageLayout                        initialLayout;
1320                 };
1321
1322                 m_colorImage                    = createImage(vk, vkDevice, &colorImageParams);
1323
1324                 // Allocate and bind color image memory
1325                 m_colorImageAlloc               = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_colorImage), MemoryRequirement::Any);
1326                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_colorImage, m_colorImageAlloc->getMemory(), m_colorImageAlloc->getOffset()));
1327         }
1328
1329         // Create resolve image
1330         {
1331                 const VkImageCreateInfo resolveImageParams =
1332                 {
1333                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                                    // VkStructureType                      sType;
1334                         DE_NULL,                                                                                                                                                // const void*                          pNext;
1335                         0u,                                                                                                                                                             // VkImageCreateFlags           flags;
1336                         VK_IMAGE_TYPE_2D,                                                                                                                               // VkImageType                          imageType;
1337                         m_colorFormat,                                                                                                                                  // VkFormat                                     format;
1338                         { m_renderSize.x(), m_renderSize.y(), 1u },                                                                             // VkExtent3D                           extent;
1339                         1u,                                                                                                                                                             // deUint32                                     mipLevels;
1340                         1u,                                                                                                                                                             // deUint32                                     arrayLayers;
1341                         VK_SAMPLE_COUNT_1_BIT,                                                                                                                  // VkSampleCountFlagBits        samples;
1342                         VK_IMAGE_TILING_OPTIMAL,                                                                                                                // VkImageTiling                        tiling;
1343                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |                 // VkImageUsageFlags            usage;
1344                                 VK_IMAGE_USAGE_TRANSFER_DST_BIT,
1345                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                              // VkSharingMode                        sharingMode;
1346                         1u,                                                                                                                                                             // deUint32                                     queueFamilyIndexCount;
1347                         &queueFamilyIndex,                                                                                                                              // const deUint32*                      pQueueFamilyIndices;
1348                         VK_IMAGE_LAYOUT_UNDEFINED                                                                                                               // VkImageLayout                        initialLayout;
1349                 };
1350
1351                 m_resolveImage = createImage(vk, vkDevice, &resolveImageParams);
1352
1353                 // Allocate and bind resolve image memory
1354                 m_resolveImageAlloc = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_resolveImage), MemoryRequirement::Any);
1355                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_resolveImage, m_resolveImageAlloc->getMemory(), m_resolveImageAlloc->getOffset()));
1356         }
1357
1358         // Create color attachment view
1359         {
1360                 const VkImageViewCreateInfo colorAttachmentViewParams =
1361                 {
1362                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
1363                         DE_NULL,                                                                                // const void*                          pNext;
1364                         0u,                                                                                             // VkImageViewCreateFlags       flags;
1365                         *m_colorImage,                                                                  // VkImage                                      image;
1366                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
1367                         m_colorFormat,                                                                  // VkFormat                                     format;
1368                         componentMappingRGBA,                                                   // VkComponentMapping           components;
1369                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
1370                 };
1371
1372                 m_colorAttachmentView = createImageView(vk, vkDevice, &colorAttachmentViewParams);
1373         }
1374
1375         // Create resolve attachment view
1376         {
1377                 const VkImageViewCreateInfo resolveAttachmentViewParams =
1378                 {
1379                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
1380                         DE_NULL,                                                                                // const void*                          pNext;
1381                         0u,                                                                                             // VkImageViewCreateFlags       flags;
1382                         *m_resolveImage,                                                                // VkImage                                      image;
1383                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
1384                         m_colorFormat,                                                                  // VkFormat                                     format;
1385                         componentMappingRGBA,                                                   // VkComponentMapping           components;
1386                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
1387                 };
1388
1389                 m_resolveAttachmentView = createImageView(vk, vkDevice, &resolveAttachmentViewParams);
1390         }
1391
1392         // Create render pass
1393         {
1394                 const VkAttachmentDescription attachmentDescriptions[2] =
1395                 {
1396                         {
1397                                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
1398                                 m_colorFormat,                                                                          // VkFormat                                                     format;
1399                                 m_multisampleStateParams.rasterizationSamples,          // VkSampleCountFlagBits                        samples;
1400                                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
1401                                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
1402                                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
1403                                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
1404                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
1405                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
1406                         },
1407                         {
1408                                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
1409                                 m_colorFormat,                                                                          // VkFormat                                                     format;
1410                                 VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
1411                                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
1412                                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
1413                                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
1414                                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
1415                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
1416                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
1417                         }
1418                 };
1419
1420                 const VkAttachmentReference colorAttachmentReference =
1421                 {
1422                         0u,                                                                                                     // deUint32                     attachment;
1423                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
1424                 };
1425
1426                 const VkAttachmentReference resolveAttachmentReference =
1427                 {
1428                         1u,                                                                                                     // deUint32                     attachment;
1429                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
1430                 };
1431
1432                 const VkSubpassDescription subpassDescription =
1433                 {
1434                         0u,                                                                                                             // VkSubpassDescriptionFlags    flags;
1435                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                                // VkPipelineBindPoint                  pipelineBindPoint;
1436                         0u,                                                                                                             // deUint32                                             inputAttachmentCount;
1437                         DE_NULL,                                                                                                // const VkAttachmentReference* pInputAttachments;
1438                         1u,                                                                                                             // deUint32                                             colorAttachmentCount;
1439                         &colorAttachmentReference,                                                              // const VkAttachmentReference* pColorAttachments;
1440                         &resolveAttachmentReference,                                                    // const VkAttachmentReference* pResolveAttachments;
1441                         DE_NULL,                                                                                                // const VkAttachmentReference* pDepthStencilAttachment;
1442                         0u,                                                                                                             // deUint32                                             preserveAttachmentCount;
1443                         DE_NULL                                                                                                 // const VkAttachmentReference* pPreserveAttachments;
1444                 };
1445
1446                 const VkRenderPassCreateInfo renderPassParams =
1447                 {
1448                         VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
1449                         DE_NULL,                                                                                        // const void*                                          pNext;
1450                         0u,                                                                                                     // VkRenderPassCreateFlags                      flags;
1451                         2u,                                                                                                     // deUint32                                                     attachmentCount;
1452                         attachmentDescriptions,                                                         // const VkAttachmentDescription*       pAttachments;
1453                         1u,                                                                                                     // deUint32                                                     subpassCount;
1454                         &subpassDescription,                                                            // const VkSubpassDescription*          pSubpasses;
1455                         0u,                                                                                                     // deUint32                                                     dependencyCount;
1456                         DE_NULL                                                                                         // const VkSubpassDependency*           pDependencies;
1457                 };
1458
1459                 m_renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
1460         }
1461
1462         // Create framebuffer
1463         {
1464                 const VkImageView attachments[2] =
1465                 {
1466                         *m_colorAttachmentView,
1467                         *m_resolveAttachmentView
1468                 };
1469
1470                 const VkFramebufferCreateInfo framebufferParams =
1471                 {
1472                         VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                      // VkStructureType                              sType;
1473                         DE_NULL,                                                                                        // const void*                                  pNext;
1474                         0u,                                                                                                     // VkFramebufferCreateFlags             flags;
1475                         *m_renderPass,                                                                          // VkRenderPass                                 renderPass;
1476                         2u,                                                                                                     // deUint32                                             attachmentCount;
1477                         attachments,                                                                            // const VkImageView*                   pAttachments;
1478                         (deUint32)m_renderSize.x(),                                                     // deUint32                                             width;
1479                         (deUint32)m_renderSize.y(),                                                     // deUint32                                             height;
1480                         1u                                                                                                      // deUint32                                             layers;
1481                 };
1482
1483                 m_framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
1484         }
1485
1486         // Create pipeline layout
1487         {
1488                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
1489                 {
1490                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
1491                         DE_NULL,                                                                                        // const void*                                          pNext;
1492                         0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
1493                         0u,                                                                                                     // deUint32                                                     setLayoutCount;
1494                         DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
1495                         0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
1496                         DE_NULL                                                                                         // const VkPushConstantRange*           pPushConstantRanges;
1497                 };
1498
1499                 m_pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
1500         }
1501
1502         m_vertexShaderModule    = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_vert"), 0);
1503         m_fragmentShaderModule  = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_frag"), 0);
1504
1505         // Create pipeline
1506         {
1507                 const VkPipelineShaderStageCreateInfo shaderStageParams[2] =
1508                 {
1509                         {
1510                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
1511                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
1512                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
1513                                 VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                stage;
1514                                 *m_vertexShaderModule,                                                                          // VkShaderModule                                               module;
1515                                 "main",                                                                                                         // const char*                                                  pName;
1516                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
1517                         },
1518                         {
1519                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
1520                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
1521                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
1522                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                stage;
1523                                 *m_fragmentShaderModule,                                                                        // VkShaderModule                                               module;
1524                                 "main",                                                                                                         // const char*                                                  pName;
1525                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
1526                         }
1527                 };
1528
1529                 const VkVertexInputBindingDescription vertexInputBindingDescription =
1530                 {
1531                         0u,                                                                     // deUint32                             binding;
1532                         sizeof(Vertex4RGBA),                            // deUint32                             stride;
1533                         VK_VERTEX_INPUT_RATE_VERTEX                     // VkVertexInputRate    inputRate;
1534                 };
1535
1536                 const VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
1537                 {
1538                         {
1539                                 0u,                                                                     // deUint32     location;
1540                                 0u,                                                                     // deUint32     binding;
1541                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
1542                                 0u                                                                      // deUint32     offset;
1543                         },
1544                         {
1545                                 1u,                                                                     // deUint32     location;
1546                                 0u,                                                                     // deUint32     binding;
1547                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
1548                                 DE_OFFSET_OF(Vertex4RGBA, color),       // deUint32     offset;
1549                         }
1550                 };
1551
1552                 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
1553                 {
1554                         VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
1555                         DE_NULL,                                                                                                                // const void*                                                          pNext;
1556                         0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
1557                         1u,                                                                                                                             // deUint32                                                                     vertexBindingDescriptionCount;
1558                         &vertexInputBindingDescription,                                                                 // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
1559                         2u,                                                                                                                             // deUint32                                                                     vertexAttributeDescriptionCount;
1560                         vertexInputAttributeDescriptions                                                                // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
1561                 };
1562
1563                 const VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
1564                 {
1565                         VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
1566                         DE_NULL,                                                                                                                // const void*                                                          pNext;
1567                         0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
1568                         topology,                                                                                                               // VkPrimitiveTopology                                          topology;
1569                         false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
1570                 };
1571
1572                 const VkViewport viewport =
1573                 {
1574                         0.0f,                                           // float        x;
1575                         0.0f,                                           // float        y;
1576                         (float)m_renderSize.x(),        // float        width;
1577                         (float)m_renderSize.y(),        // float        height;
1578                         0.0f,                                           // float        minDepth;
1579                         1.0f                                            // float        maxDepth;
1580                 };
1581
1582                 const VkRect2D scissor =
1583                 {
1584                         { 0, 0 },                                                                                               // VkOffset2D  offset;
1585                         { m_renderSize.x(), m_renderSize.y() }                                  // VkExtent2D  extent;
1586                 };
1587
1588                 const VkPipelineViewportStateCreateInfo viewportStateParams =
1589                 {
1590                         VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                              sType;
1591                         DE_NULL,                                                                                                                // const void*                                                  pNext;
1592                         0u,                                                                                                                             // VkPipelineViewportStateCreateFlags   flags;
1593                         1u,                                                                                                                             // deUint32                                                             viewportCount;
1594                         &viewport,                                                                                                              // const VkViewport*                                    pViewports;
1595                         1u,                                                                                                                             // deUint32                                                             scissorCount;
1596                         &scissor                                                                                                                // const VkRect2D*                                              pScissors;
1597                 };
1598
1599                 const VkPipelineRasterizationStateCreateInfo rasterStateParams =
1600                 {
1601                         VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
1602                         DE_NULL,                                                                                                                // const void*                                                          pNext;
1603                         0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
1604                         false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
1605                         false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
1606                         VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
1607                         VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
1608                         VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
1609                         VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
1610                         0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
1611                         0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
1612                         0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
1613                         1.0f                                                                                                                    // float                                                                        lineWidth;
1614                 };
1615
1616                 const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
1617                 {
1618                         VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
1619                         DE_NULL,                                                                                                        // const void*                                                                  pNext;
1620                         0u,                                                                                                                     // VkPipelineColorBlendStateCreateFlags                 flags;
1621                         false,                                                                                                          // VkBool32                                                                             logicOpEnable;
1622                         VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
1623                         1u,                                                                                                                     // deUint32                                                                             attachmentCount;
1624                         &m_colorBlendState,                                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
1625                         { 0.0f, 0.0f, 0.0f, 0.0f }                                                                      // float                                                                                blendConstants[4];
1626                 };
1627
1628                 const VkPipelineDynamicStateCreateInfo  dynamicStateParams              =
1629                 {
1630                         VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,           // VkStructureType                                              sType;
1631                         DE_NULL,                                                                                                        // const void*                                                  pNext;
1632                         0u,                                                                                                                     // VkPipelineDynamicStateCreateFlags    flags;
1633                         0u,                                                                                                                     // deUint32                                                             dynamicStateCount;
1634                         DE_NULL                                                                                                         // const VkDynamicState*                                pDynamicStates;
1635                 };
1636
1637                 const VkPipelineDepthStencilStateCreateInfo depthStencilStateParams =
1638                 {
1639                         VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // VkStructureType                                                      sType;
1640                         DE_NULL,                                                                                                        // const void*                                                          pNext;
1641                         0u,                                                                                                                     // VkPipelineDepthStencilStateCreateFlags       flags;
1642                         false,                                                                                                          // VkBool32                                                                     depthTestEnable;
1643                         false,                                                                                                          // VkBool32                                                                     depthWriteEnable;
1644                         VK_COMPARE_OP_LESS,                                                                                     // VkCompareOp                                                          depthCompareOp;
1645                         false,                                                                                                          // VkBool32                                                                     depthBoundsTestEnable;
1646                         false,                                                                                                          // VkBool32                                                                     stencilTestEnable;
1647                         // VkStencilOpState     front;
1648                         {
1649                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
1650                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
1651                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
1652                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
1653                                 0u,                                             // deUint32             compareMask;
1654                                 0u,                                             // deUint32             writeMask;
1655                                 0u,                                             // deUint32             reference;
1656                         },
1657                         // VkStencilOpState     back;
1658                         {
1659                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  failOp;
1660                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  passOp;
1661                                 VK_STENCIL_OP_KEEP,             // VkStencilOp  depthFailOp;
1662                                 VK_COMPARE_OP_NEVER,    // VkCompareOp  compareOp;
1663                                 0u,                                             // deUint32             compareMask;
1664                                 0u,                                             // deUint32             writeMask;
1665                                 0u,                                             // deUint32             reference;
1666                         },
1667                         -1.0f,                                                                                                          // float                        minDepthBounds;
1668                         +1.0f,                                                                                                          // float                        maxDepthBounds;
1669                 };
1670
1671                 const VkGraphicsPipelineCreateInfo graphicsPipelineParams =
1672                 {
1673                         VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
1674                         DE_NULL,                                                                                        // const void*                                                                          pNext;
1675                         0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
1676                         2u,                                                                                                     // deUint32                                                                                     stageCount;
1677                         shaderStageParams,                                                                      // const VkPipelineShaderStageCreateInfo*                       pStages;
1678                         &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
1679                         &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
1680                         DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
1681                         &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
1682                         &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
1683                         &m_multisampleStateParams,                                                      // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
1684                         &depthStencilStateParams,                                                       // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
1685                         &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
1686                         &dynamicStateParams,                                                            // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
1687                         *m_pipelineLayout,                                                                      // VkPipelineLayout                                                                     layout;
1688                         *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
1689                         0u,                                                                                                     // deUint32                                                                                     subpass;
1690                         0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
1691                         0u                                                                                                      // deInt32                                                                                      basePipelineIndex;
1692                 };
1693
1694                 m_graphicsPipeline      = createGraphicsPipeline(vk, vkDevice, DE_NULL, &graphicsPipelineParams);
1695         }
1696
1697         // Create vertex buffer
1698         {
1699                 const VkBufferCreateInfo vertexBufferParams =
1700                 {
1701                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           // VkStructureType              sType;
1702                         DE_NULL,                                                                        // const void*                  pNext;
1703                         0u,                                                                                     // VkBufferCreateFlags  flags;
1704                         1024u,                                                                          // VkDeviceSize                 size;
1705                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,                      // VkBufferUsageFlags   usage;
1706                         VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                sharingMode;
1707                         1u,                                                                                     // deUint32                             queueFamilyIndexCount;
1708                         &queueFamilyIndex                                                       // const deUint32*              pQueueFamilyIndices;
1709                 };
1710
1711                 m_vertexBuffer          = createBuffer(vk, vkDevice, &vertexBufferParams);
1712                 m_vertexBufferAlloc     = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *m_vertexBuffer), MemoryRequirement::HostVisible);
1713
1714                 VK_CHECK(vk.bindBufferMemory(vkDevice, *m_vertexBuffer, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset()));
1715
1716                 // Load vertices into vertex buffer
1717                 deMemcpy(m_vertexBufferAlloc->getHostPtr(), vertices.data(), vertices.size() * sizeof(Vertex4RGBA));
1718                 flushMappedMemoryRange(vk, vkDevice, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferParams.size);
1719         }
1720
1721         // Create command pool
1722         {
1723                 const VkCommandPoolCreateInfo cmdPoolParams =
1724                 {
1725                         VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,             // VkStructureType                              sType;
1726                         DE_NULL,                                                                                // const void*                                  pNext;
1727                         VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,                   // VkCommandPoolCreateFlags             flags;
1728                         queueFamilyIndex,                                                               // deUint32                                             queueFamilyIndex;
1729                 };
1730
1731                 m_cmdPool = createCommandPool(vk, vkDevice, &cmdPoolParams);
1732         }
1733
1734         // Create command buffer
1735         {
1736                 const VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
1737                 {
1738                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType                      sType;
1739                         DE_NULL,                                                                                // const void*                          pNext;
1740                         *m_cmdPool,                                                                             // VkCommandPool                        commandPool;
1741                         VK_COMMAND_BUFFER_LEVEL_PRIMARY,                                // VkCommandBufferLevel level;
1742                         1u                                                                                              // deUint32                             bufferCount;
1743                 };
1744
1745                 const VkCommandBufferBeginInfo cmdBufferBeginInfo =
1746                 {
1747                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
1748                         DE_NULL,                                                                                // const void*                                          pNext;
1749                         0u,                                                                                             // VkCommandBufferUsageFlags            flags;
1750                         DE_NULL,                                                                                // VkRenderPass                                         renderPass;
1751                         0u,                                                                                             // deUint32                                                     subpass;
1752                         DE_NULL,                                                                                // VkFramebuffer                                        framebuffer;
1753                         false,                                                                                  // VkBool32                                                     occlusionQueryEnable;
1754                         0u,                                                                                             // VkQueryControlFlags                          queryFlags;
1755                         0u                                                                                              // VkQueryPipelineStatisticFlags        pipelineStatistics;
1756                 };
1757
1758                 VkClearValue colorClearValue;
1759                 colorClearValue.color.float32[0] = 0.0f;
1760                 colorClearValue.color.float32[1] = 0.0f;
1761                 colorClearValue.color.float32[2] = 0.0f;
1762                 colorClearValue.color.float32[3] = 0.0f;
1763
1764                 const VkClearValue clearValues[2] =
1765                 {
1766                         colorClearValue,
1767                         colorClearValue
1768                 };
1769
1770                 const VkRenderPassBeginInfo renderPassBeginInfo =
1771                 {
1772                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
1773                         DE_NULL,                                                                                                // const void*                  pNext;
1774                         *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
1775                         *m_framebuffer,                                                                                 // VkFramebuffer                framebuffer;
1776                         { { 0, 0 }, { m_renderSize.x(), m_renderSize.y() } },   // VkRect2D                             renderArea;
1777                         2,                                                                                                              // deUint32                             clearValueCount;
1778                         clearValues                                                                                             // const VkClearValue*  pClearValues;
1779                 };
1780
1781                 m_cmdBuffer = allocateCommandBuffer(vk, vkDevice, &cmdBufferAllocateInfo);
1782
1783                 VK_CHECK(vk.beginCommandBuffer(*m_cmdBuffer, &cmdBufferBeginInfo));
1784                 vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
1785
1786                 VkDeviceSize vertexBufferOffset = 0u;
1787
1788                 vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_graphicsPipeline);
1789                 vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &m_vertexBuffer.get(), &vertexBufferOffset);
1790                 vk.cmdDraw(*m_cmdBuffer, (deUint32)vertices.size(), 1, 0, 0);
1791
1792                 vk.cmdEndRenderPass(*m_cmdBuffer);
1793
1794                 VK_CHECK(vk.endCommandBuffer(*m_cmdBuffer));
1795         }
1796
1797         // Create fence
1798         {
1799                 const VkFenceCreateInfo fenceParams =
1800                 {
1801                         VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,    // VkStructureType              sType;
1802                         DE_NULL,                                                                // const void*                  pNext;
1803                         0u                                                                              // VkFenceCreateFlags   flags;
1804                 };
1805
1806                 m_fence = createFence(vk, vkDevice, &fenceParams);
1807         }
1808 }
1809
1810 MultisampleRenderer::~MultisampleRenderer (void)
1811 {
1812 }
1813
1814 de::MovePtr<tcu::TextureLevel> MultisampleRenderer::render (void)
1815 {
1816         const DeviceInterface&          vk                                      = m_context.getDeviceInterface();
1817         const VkDevice                          vkDevice                        = m_context.getDevice();
1818         const VkQueue                           queue                           = m_context.getUniversalQueue();
1819         const deUint32                          queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
1820         SimpleAllocator                         allocator                       (vk, vkDevice, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice()));
1821         const VkSubmitInfo                      submitInfo      =
1822         {
1823                 VK_STRUCTURE_TYPE_SUBMIT_INFO,  // VkStructureType                      sType;
1824                 DE_NULL,                                                // const void*                          pNext;
1825                 0u,                                                             // deUint32                                     waitSemaphoreCount;
1826                 DE_NULL,                                                // const VkSemaphore*           pWaitSemaphores;
1827                 1u,                                                             // deUint32                                     commandBufferCount;
1828                 &m_cmdBuffer.get(),                             // const VkCommandBuffer*       pCommandBuffers;
1829                 0u,                                                             // deUint32                                     signalSemaphoreCount;
1830                 DE_NULL                                                 // const VkSemaphore*           pSignalSemaphores;
1831         };
1832
1833         VK_CHECK(vk.resetFences(vkDevice, 1, &m_fence.get()));
1834         VK_CHECK(vk.queueSubmit(queue, 1, &submitInfo, *m_fence));
1835         VK_CHECK(vk.waitForFences(vkDevice, 1, &m_fence.get(), true, ~(0ull) /* infinity*/));
1836
1837         return readColorAttachment(vk, vkDevice, queue, queueFamilyIndex, allocator, *m_resolveImage, m_colorFormat, m_renderSize);
1838 }
1839
1840 } // anonymous
1841
1842 tcu::TestCaseGroup* createMultisampleTests (tcu::TestContext& testCtx)
1843 {
1844         const VkSampleCountFlagBits samples[] =
1845         {
1846                 VK_SAMPLE_COUNT_2_BIT,
1847                 VK_SAMPLE_COUNT_4_BIT,
1848                 VK_SAMPLE_COUNT_8_BIT,
1849                 VK_SAMPLE_COUNT_16_BIT,
1850                 VK_SAMPLE_COUNT_32_BIT,
1851                 VK_SAMPLE_COUNT_64_BIT
1852         };
1853
1854         de::MovePtr<tcu::TestCaseGroup> multisampleTests (new tcu::TestCaseGroup(testCtx, "multisample", ""));
1855
1856         // Rasterization samples tests
1857         {
1858                 de::MovePtr<tcu::TestCaseGroup> rasterizationSamplesTests(new tcu::TestCaseGroup(testCtx, "raster_samples", ""));
1859
1860                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1861                 {
1862                         std::ostringstream caseName;
1863                         caseName << "samples_" << samples[samplesNdx];
1864
1865                         de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
1866
1867                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_triangle", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_TRIANGLE));
1868                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_line", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_LINE));
1869                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_point", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_POINT));
1870
1871                         rasterizationSamplesTests->addChild(samplesTests.release());
1872                 }
1873
1874                 multisampleTests->addChild(rasterizationSamplesTests.release());
1875         }
1876
1877         // Raster samples consistency check
1878         {
1879                 de::MovePtr<tcu::TestCaseGroup> rasterSamplesConsistencyTests(new tcu::TestCaseGroup(testCtx, "raster_samples_consistency", ""));
1880
1881                 addFunctionCaseWithPrograms(rasterSamplesConsistencyTests.get(),
1882                                                                         "unique_colors_check",
1883                                                                         "",
1884                                                                         initMultisamplePrograms,
1885                                                                         testRasterSamplesConsistency,
1886                                                                         GEOMETRY_TYPE_OPAQUE_TRIANGLE);
1887
1888                 multisampleTests->addChild(rasterSamplesConsistencyTests.release());
1889         }
1890
1891         // minSampleShading tests
1892         {
1893                 struct TestConfig
1894                 {
1895                         const char*     name;
1896                         float           minSampleShading;
1897                 };
1898
1899                 const TestConfig testConfigs[] =
1900                 {
1901                         { "min_0_0",    0.0f },
1902                         { "min_0_25",   0.25f },
1903                         { "min_0_5",    0.5f },
1904                         { "min_0_75",   0.75f },
1905                         { "min_1_0",    1.0f }
1906                 };
1907
1908                 de::MovePtr<tcu::TestCaseGroup> minSampleShadingTests(new tcu::TestCaseGroup(testCtx, "min_sample_shading", ""));
1909
1910                 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(testConfigs); configNdx++)
1911                 {
1912                         const TestConfig&                               testConfig                              = testConfigs[configNdx];
1913                         de::MovePtr<tcu::TestCaseGroup> minShadingValueTests    (new tcu::TestCaseGroup(testCtx, testConfigs[configNdx].name, ""));
1914
1915                         for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1916                         {
1917                                 std::ostringstream caseName;
1918                                 caseName << "samples_" << samples[samplesNdx];
1919
1920                                 de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
1921
1922                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_triangle", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_TRIANGLE));
1923                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_line", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_LINE));
1924                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_point", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_POINT));
1925
1926                                 minShadingValueTests->addChild(samplesTests.release());
1927                         }
1928
1929                         minSampleShadingTests->addChild(minShadingValueTests.release());
1930                 }
1931
1932                 multisampleTests->addChild(minSampleShadingTests.release());
1933         }
1934
1935         // pSampleMask tests
1936         {
1937                 struct TestConfig
1938                 {
1939                         const char*             name;
1940                         const char*             description;
1941                         VkSampleMask    sampleMask;
1942                 };
1943
1944                 const TestConfig testConfigs[] =
1945                 {
1946                         { "mask_all_on",        "All mask bits are off",                        0x0 },
1947                         { "mask_all_off",       "All mask bits are on",                         0xFFFFFFFF },
1948                         { "mask_one",           "All mask elements are 0x1",            0x1},
1949                         { "mask_random",        "All mask elements are 0xAAAAAAAA",     0xAAAAAAAA },
1950                 };
1951
1952                 de::MovePtr<tcu::TestCaseGroup> sampleMaskTests(new tcu::TestCaseGroup(testCtx, "sample_mask", ""));
1953
1954                 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(testConfigs); configNdx++)
1955                 {
1956                         const TestConfig&                               testConfig                              = testConfigs[configNdx];
1957                         de::MovePtr<tcu::TestCaseGroup> sampleMaskValueTests    (new tcu::TestCaseGroup(testCtx, testConfig.name, testConfig.description));
1958
1959                         for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1960                         {
1961                                 std::ostringstream caseName;
1962                                 caseName << "samples_" << samples[samplesNdx];
1963
1964                                 const deUint32                                  sampleMaskCount = samples[samplesNdx] / 32;
1965                                 de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
1966
1967                                 std::vector<VkSampleMask> mask;
1968                                 for (deUint32 maskNdx = 0; maskNdx < sampleMaskCount; maskNdx++)
1969                                         mask.push_back(testConfig.sampleMask);
1970
1971                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_triangle", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_TRIANGLE));
1972                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_line", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_LINE));
1973                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_point", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_POINT));
1974
1975                                 sampleMaskValueTests->addChild(samplesTests.release());
1976                         }
1977
1978                         sampleMaskTests->addChild(sampleMaskValueTests.release());
1979                 }
1980
1981                 multisampleTests->addChild(sampleMaskTests.release());
1982
1983         }
1984
1985         // AlphaToOne tests
1986         {
1987                 de::MovePtr<tcu::TestCaseGroup> alphaToOneTests(new tcu::TestCaseGroup(testCtx, "alpha_to_one", ""));
1988
1989                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1990                 {
1991                         std::ostringstream caseName;
1992                         caseName << "samples_" << samples[samplesNdx];
1993
1994                         alphaToOneTests->addChild(new AlphaToOneTest(testCtx, caseName.str(), "", samples[samplesNdx]));
1995                 }
1996
1997                 multisampleTests->addChild(alphaToOneTests.release());
1998         }
1999
2000         // AlphaToCoverageEnable tests
2001         {
2002                 de::MovePtr<tcu::TestCaseGroup> alphaToCoverageTests (new tcu::TestCaseGroup(testCtx, "alpha_to_coverage", ""));
2003
2004                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
2005                 {
2006                         std::ostringstream caseName;
2007                         caseName << "samples_" << samples[samplesNdx];
2008
2009                         de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
2010
2011                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_opaque", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_QUAD));
2012                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_translucent", "", samples[samplesNdx], GEOMETRY_TYPE_TRANSLUCENT_QUAD));
2013                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_invisible", "", samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_QUAD));
2014
2015                         alphaToCoverageTests->addChild(samplesTests.release());
2016                 }
2017                 multisampleTests->addChild(alphaToCoverageTests.release());
2018         }
2019
2020         return multisampleTests.release();
2021 }
2022
2023 } // pipeline
2024 } // vkt