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