Merge vk-gl-cts/master into vk-gl-cts/vulkan-cts-next-dev
[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  * Copyright (c) 2017 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Multisample Tests
24  *//*--------------------------------------------------------------------*/
25
26 #include "vktPipelineMultisampleTests.hpp"
27 #include "vktPipelineMultisampleImageTests.hpp"
28 #include "vktPipelineMultisampleSampleLocationsExtTests.hpp"
29 #include "vktPipelineClearUtil.hpp"
30 #include "vktPipelineImageUtil.hpp"
31 #include "vktPipelineVertexUtil.hpp"
32 #include "vktPipelineReferenceRenderer.hpp"
33 #include "vktTestCase.hpp"
34 #include "vktTestCaseUtil.hpp"
35 #include "vkImageUtil.hpp"
36 #include "vkMemUtil.hpp"
37 #include "vkPrograms.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "vkRef.hpp"
40 #include "vkRefUtil.hpp"
41 #include "tcuImageCompare.hpp"
42 #include "tcuTestLog.hpp"
43 #include "deUniquePtr.hpp"
44 #include "deSharedPtr.hpp"
45 #include "deStringUtil.hpp"
46 #include "deMemory.h"
47
48 #include <sstream>
49 #include <vector>
50 #include <map>
51
52 namespace vkt
53 {
54 namespace pipeline
55 {
56
57 using namespace vk;
58
59 namespace
60 {
61 enum GeometryType
62 {
63         GEOMETRY_TYPE_OPAQUE_TRIANGLE,
64         GEOMETRY_TYPE_OPAQUE_LINE,
65         GEOMETRY_TYPE_OPAQUE_POINT,
66         GEOMETRY_TYPE_OPAQUE_QUAD,
67         GEOMETRY_TYPE_OPAQUE_QUAD_NONZERO_DEPTH,        //!< placed at z = 0.5
68         GEOMETRY_TYPE_TRANSLUCENT_QUAD,
69         GEOMETRY_TYPE_INVISIBLE_TRIANGLE,
70         GEOMETRY_TYPE_INVISIBLE_QUAD,
71         GEOMETRY_TYPE_GRADIENT_QUAD
72 };
73
74 enum TestModeBits
75 {
76         TEST_MODE_DEPTH_BIT             = 1u,
77         TEST_MODE_STENCIL_BIT   = 2u,
78 };
79 typedef deUint32 TestModeFlags;
80
81 enum RenderType
82 {
83         // resolve multisample rendering to single sampled image
84         RENDER_TYPE_RESOLVE             = 0u,
85
86         // copy samples to an array of single sampled images
87         RENDER_TYPE_COPY_SAMPLES
88 };
89
90 enum ImageBackingMode
91 {
92         IMAGE_BACKING_MODE_REGULAR      = 0u,
93         IMAGE_BACKING_MODE_SPARSE
94 };
95
96 struct MultisampleTestParams
97 {
98         GeometryType            geometryType;
99         ImageBackingMode        backingMode;
100 };
101
102 void                                                                    initMultisamplePrograms                         (SourceCollections& sources, MultisampleTestParams params);
103 bool                                                                    isSupportedSampleCount                          (const InstanceInterface& instanceInterface, VkPhysicalDevice physicalDevice, VkSampleCountFlagBits rasterizationSamples);
104 bool                                                                    isSupportedDepthStencilFormat           (const InstanceInterface& vki, const VkPhysicalDevice physDevice, const VkFormat format);
105 VkPipelineColorBlendAttachmentState             getDefaultColorBlendAttachmentState     (void);
106 deUint32                                                                getUniqueColorsCount                            (const tcu::ConstPixelBufferAccess& image);
107 VkImageAspectFlags                                              getImageAspectFlags                                     (const VkFormat format);
108 VkPrimitiveTopology                                             getPrimitiveTopology                            (const GeometryType geometryType);
109 std::vector<Vertex4RGBA>                                generateVertices                                        (const GeometryType geometryType);
110 VkFormat                                                                findSupportedDepthStencilFormat         (Context& context, const bool useDepth, const bool useStencil);
111
112 class MultisampleTest : public vkt::TestCase
113 {
114 public:
115
116                                                                                                 MultisampleTest                                         (tcu::TestContext&                                                              testContext,
117                                                                                                                                                                          const std::string&                                                             name,
118                                                                                                                                                                          const std::string&                                                             description,
119                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
120                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
121                                                                                                                                                                          GeometryType                                                                   geometryType,
122                                                                                                                                                                          ImageBackingMode                                                               backingMode);
123         virtual                                                                         ~MultisampleTest                                        (void) {}
124
125         virtual void                                                            initPrograms                                            (SourceCollections& programCollection) const;
126         virtual TestInstance*                                           createInstance                                          (Context& context) const;
127
128 protected:
129         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
130                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
131                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
132                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
133                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const = 0;
134         VkPipelineMultisampleStateCreateInfo            m_multisampleStateParams;
135         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
136         const GeometryType                                                      m_geometryType;
137         const ImageBackingMode                                          m_backingMode;
138         std::vector<VkSampleMask>                                       m_sampleMask;
139 };
140
141 class RasterizationSamplesTest : public MultisampleTest
142 {
143 public:
144                                                                                                 RasterizationSamplesTest                        (tcu::TestContext&              testContext,
145                                                                                                                                                                          const std::string&             name,
146                                                                                                                                                                          const std::string&             description,
147                                                                                                                                                                          VkSampleCountFlagBits  rasterizationSamples,
148                                                                                                                                                                          GeometryType                   geometryType,
149                                                                                                                                                                          ImageBackingMode               backingMode,
150                                                                                                                                                                          TestModeFlags                  modeFlags                               = 0u);
151         virtual                                                                         ~RasterizationSamplesTest                       (void) {}
152
153 protected:
154         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
155                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
156                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
157                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
158                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
159
160         static VkPipelineMultisampleStateCreateInfo     getRasterizationSamplesStateParams      (VkSampleCountFlagBits rasterizationSamples);
161
162         const ImageBackingMode                                          m_backingMode;
163         const TestModeFlags                                                     m_modeFlags;
164 };
165
166 class MinSampleShadingTest : public MultisampleTest
167 {
168 public:
169                                                                                                 MinSampleShadingTest                            (tcu::TestContext&              testContext,
170                                                                                                                                                                          const std::string&             name,
171                                                                                                                                                                          const std::string&             description,
172                                                                                                                                                                          VkSampleCountFlagBits  rasterizationSamples,
173                                                                                                                                                                          float                                  minSampleShading,
174                                                                                                                                                                          GeometryType                   geometryType,
175                                                                                                                                                                          ImageBackingMode               backingMode);
176         virtual                                                                         ~MinSampleShadingTest                           (void) {}
177
178 protected:
179         virtual void                                                            initPrograms                                            (SourceCollections& programCollection) const;
180         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
181                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
182                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
183                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
184                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
185
186         static VkPipelineMultisampleStateCreateInfo     getMinSampleShadingStateParams          (VkSampleCountFlagBits rasterizationSamples, float minSampleShading);
187
188         const ImageBackingMode                                          m_backingMode;
189 };
190
191 class SampleMaskTest : public MultisampleTest
192 {
193 public:
194                                                                                                 SampleMaskTest                                          (tcu::TestContext&                                      testContext,
195                                                                                                                                                                          const std::string&                                     name,
196                                                                                                                                                                          const std::string&                                     description,
197                                                                                                                                                                          VkSampleCountFlagBits                          rasterizationSamples,
198                                                                                                                                                                          const std::vector<VkSampleMask>&       sampleMask,
199                                                                                                                                                                          GeometryType                                           geometryType,
200                                                                                                                                                                          ImageBackingMode                                       backingMode);
201
202         virtual                                                                         ~SampleMaskTest                                         (void) {}
203
204 protected:
205         virtual TestInstance*                                           createMultisampleTestInstance           (Context&                                                                               context,
206                                                                                                                                                                          VkPrimitiveTopology                                                    topology,
207                                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
208                                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
209                                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
210
211         static VkPipelineMultisampleStateCreateInfo     getSampleMaskStateParams                        (VkSampleCountFlagBits rasterizationSamples, const std::vector<VkSampleMask>& sampleMask);
212
213         const ImageBackingMode                                          m_backingMode;
214 };
215
216 class AlphaToOneTest : public MultisampleTest
217 {
218 public:
219                                                                                                 AlphaToOneTest                                  (tcu::TestContext&                                      testContext,
220                                                                                                                                                                  const std::string&                                     name,
221                                                                                                                                                                  const std::string&                                     description,
222                                                                                                                                                                  VkSampleCountFlagBits                          rasterizationSamples,
223                                                                                                                                                                  ImageBackingMode                                       backingMode);
224
225         virtual                                                                         ~AlphaToOneTest                                 (void) {}
226
227 protected:
228         virtual TestInstance*                                           createMultisampleTestInstance   (Context&                                                                               context,
229                                                                                                                                                                  VkPrimitiveTopology                                                    topology,
230                                                                                                                                                                  const std::vector<Vertex4RGBA>&                                vertices,
231                                                                                                                                                                  const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
232                                                                                                                                                                  const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
233
234         static VkPipelineMultisampleStateCreateInfo     getAlphaToOneStateParams                (VkSampleCountFlagBits rasterizationSamples);
235         static VkPipelineColorBlendAttachmentState      getAlphaToOneBlendState                 (void);
236
237         const ImageBackingMode                                          m_backingMode;
238 };
239
240 class AlphaToCoverageTest : public MultisampleTest
241 {
242 public:
243                                                                                                 AlphaToCoverageTest                             (tcu::TestContext&              testContext,
244                                                                                                                                                                  const std::string&             name,
245                                                                                                                                                                  const std::string&             description,
246                                                                                                                                                                  VkSampleCountFlagBits  rasterizationSamples,
247                                                                                                                                                                  GeometryType                   geometryType,
248                                                                                                                                                                  ImageBackingMode               backingMode);
249
250         virtual                                                                         ~AlphaToCoverageTest                    (void) {}
251
252 protected:
253         virtual TestInstance*                                           createMultisampleTestInstance   (Context&                                                                               context,
254                                                                                                                                                                  VkPrimitiveTopology                                                    topology,
255                                                                                                                                                                  const std::vector<Vertex4RGBA>&                                vertices,
256                                                                                                                                                                  const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
257                                                                                                                                                                  const VkPipelineColorBlendAttachmentState&             colorBlendState) const;
258
259         static VkPipelineMultisampleStateCreateInfo     getAlphaToCoverageStateParams   (VkSampleCountFlagBits rasterizationSamples);
260
261         GeometryType                                                            m_geometryType;
262         const ImageBackingMode                                          m_backingMode;
263 };
264
265 typedef de::SharedPtr<Unique<VkPipeline> > VkPipelineSp;
266
267 class MultisampleRenderer
268 {
269 public:
270                                                                                                 MultisampleRenderer                     (Context&                                                                               context,
271                                                                                                                                                          const VkFormat                                                                 colorFormat,
272                                                                                                                                                          const tcu::IVec2&                                                              renderSize,
273                                                                                                                                                          const VkPrimitiveTopology                                              topology,
274                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
275                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
276                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
277                                                                                                                                                          const RenderType                                                               renderType,
278                                                                                                                                                          const ImageBackingMode                                                 backingMode);
279
280                                                                                                 MultisampleRenderer                     (Context&                                                                               context,
281                                                                                                                                                          const VkFormat                                                                 colorFormat,
282                                                                                                                                                          const VkFormat                                                                 depthStencilFormat,
283                                                                                                                                                          const tcu::IVec2&                                                              renderSize,
284                                                                                                                                                          const bool                                                                             useDepth,
285                                                                                                                                                          const bool                                                                             useStencil,
286                                                                                                                                                          const deUint32                                                                 numTopologies,
287                                                                                                                                                          const VkPrimitiveTopology*                                             pTopology,
288                                                                                                                                                          const std::vector<Vertex4RGBA>*                                pVertices,
289                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
290                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
291                                                                                                                                                      const RenderType                                                           renderType,
292                                                                                                                                                          const ImageBackingMode                                                 backingMode);
293
294         virtual                                                                         ~MultisampleRenderer            (void);
295
296         de::MovePtr<tcu::TextureLevel>                          render                                          (void);
297         de::MovePtr<tcu::TextureLevel>                          getSingleSampledImage           (deUint32 sampleId);
298
299 protected:
300         void                                                                            initialize                                      (Context&                                                                               context,
301                                                                                                                                                          const deUint32                                                                 numTopologies,
302                                                                                                                                                          const VkPrimitiveTopology*                                             pTopology,
303                                                                                                                                                          const std::vector<Vertex4RGBA>*                                pVertices);
304
305         Context&                                                                        m_context;
306
307         const Unique<VkSemaphore>                                       m_bindSemaphore;
308
309         const VkFormat                                                          m_colorFormat;
310         const VkFormat                                                          m_depthStencilFormat;
311         tcu::IVec2                                                                      m_renderSize;
312         const bool                                                                      m_useDepth;
313         const bool                                                                      m_useStencil;
314
315         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
316         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
317
318         const RenderType                                                        m_renderType;
319
320         Move<VkImage>                                                           m_colorImage;
321         de::MovePtr<Allocation>                                         m_colorImageAlloc;
322         Move<VkImageView>                                                       m_colorAttachmentView;
323
324         Move<VkImage>                                                           m_resolveImage;
325         de::MovePtr<Allocation>                                         m_resolveImageAlloc;
326         Move<VkImageView>                                                       m_resolveAttachmentView;
327
328         struct PerSampleImage
329         {
330                 Move<VkImage>                                                           m_image;
331                 de::MovePtr<Allocation>                                         m_imageAlloc;
332                 Move<VkImageView>                                                       m_attachmentView;
333         };
334         std::vector<de::SharedPtr<PerSampleImage> >     m_perSampleImages;
335
336         Move<VkImage>                                                           m_depthStencilImage;
337         de::MovePtr<Allocation>                                         m_depthStencilImageAlloc;
338         Move<VkImageView>                                                       m_depthStencilAttachmentView;
339
340         Move<VkRenderPass>                                                      m_renderPass;
341         Move<VkFramebuffer>                                                     m_framebuffer;
342
343         Move<VkShaderModule>                                            m_vertexShaderModule;
344         Move<VkShaderModule>                                            m_fragmentShaderModule;
345
346         Move<VkShaderModule>                                            m_copySampleVertexShaderModule;
347         Move<VkShaderModule>                                            m_copySampleFragmentShaderModule;
348
349         Move<VkBuffer>                                                          m_vertexBuffer;
350         de::MovePtr<Allocation>                                         m_vertexBufferAlloc;
351
352         Move<VkPipelineLayout>                                          m_pipelineLayout;
353         std::vector<VkPipelineSp>                                       m_graphicsPipelines;
354
355         Move<VkDescriptorSetLayout>                                     m_copySampleDesciptorLayout;
356         Move<VkDescriptorPool>                                          m_copySampleDesciptorPool;
357         Move<VkDescriptorSet>                                           m_copySampleDesciptorSet;
358
359         Move<VkPipelineLayout>                                          m_copySamplePipelineLayout;
360         std::vector<VkPipelineSp>                                       m_copySamplePipelines;
361
362         Move<VkCommandPool>                                                     m_cmdPool;
363         Move<VkCommandBuffer>                                           m_cmdBuffer;
364
365         Move<VkFence>                                                           m_fence;
366
367         std::vector<de::SharedPtr<Allocation> >         m_allocations;
368
369         ImageBackingMode                                                        m_backingMode;
370 };
371
372 class RasterizationSamplesInstance : public vkt::TestInstance
373 {
374 public:
375                                                                                 RasterizationSamplesInstance    (Context&                                                                               context,
376                                                                                                                                                  VkPrimitiveTopology                                                    topology,
377                                                                                                                                                  const std::vector<Vertex4RGBA>&                                vertices,
378                                                                                                                                                  const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
379                                                                                                                                                  const VkPipelineColorBlendAttachmentState&             blendState,
380                                                                                                                                                  const TestModeFlags                                                    modeFlags,
381                                                                                                                                                  ImageBackingMode                                                               backingMode);
382         virtual                                                         ~RasterizationSamplesInstance   (void) {}
383
384         virtual tcu::TestStatus                         iterate                                                 (void);
385
386 protected:
387         virtual tcu::TestStatus                         verifyImage                                             (const tcu::ConstPixelBufferAccess& result);
388
389         const VkFormat                                          m_colorFormat;
390         const tcu::IVec2                                        m_renderSize;
391         const VkPrimitiveTopology                       m_primitiveTopology;
392         const std::vector<Vertex4RGBA>          m_vertices;
393         const std::vector<Vertex4RGBA>          m_fullQuadVertices;                     //!< used by depth/stencil case
394         const TestModeFlags                                     m_modeFlags;
395         de::MovePtr<MultisampleRenderer>        m_multisampleRenderer;
396 };
397
398 class MinSampleShadingInstance : public vkt::TestInstance
399 {
400 public:
401                                                                                                 MinSampleShadingInstance        (Context&                                                                               context,
402                                                                                                                                                          VkPrimitiveTopology                                                    topology,
403                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
404                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
405                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
406                                                                                                                                                          ImageBackingMode                                                               backingMode);
407         virtual                                                                         ~MinSampleShadingInstance       (void) {}
408
409         virtual tcu::TestStatus                                         iterate                                         (void);
410
411 protected:
412         virtual tcu::TestStatus                                         verifySampleShadedImage         (const std::vector<tcu::TextureLevel>& testShadingImages,
413                                                                                                                                                          const tcu::ConstPixelBufferAccess& noSampleshadingImage);
414
415         const VkFormat                                                          m_colorFormat;
416         const tcu::IVec2                                                        m_renderSize;
417         const VkPrimitiveTopology                                       m_primitiveTopology;
418         const std::vector<Vertex4RGBA>                          m_vertices;
419         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
420         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
421         const ImageBackingMode                                          m_backingMode;
422 };
423
424 class SampleMaskInstance : public vkt::TestInstance
425 {
426 public:
427                                                                                                 SampleMaskInstance                      (Context&                                                                               context,
428                                                                                                                                                          VkPrimitiveTopology                                                    topology,
429                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
430                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
431                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
432                                                                                                                                                          ImageBackingMode                                                               backingMode);
433         virtual                                                                         ~SampleMaskInstance                     (void) {}
434
435         virtual tcu::TestStatus                                         iterate                                         (void);
436
437 protected:
438         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& testShadingImage,
439                                                                                                                                                          const tcu::ConstPixelBufferAccess& minShadingImage,
440                                                                                                                                                          const tcu::ConstPixelBufferAccess& maxShadingImage);
441         const VkFormat                                                          m_colorFormat;
442         const tcu::IVec2                                                        m_renderSize;
443         const VkPrimitiveTopology                                       m_primitiveTopology;
444         const std::vector<Vertex4RGBA>                          m_vertices;
445         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
446         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
447         const ImageBackingMode                                          m_backingMode;
448 };
449
450 class AlphaToOneInstance : public vkt::TestInstance
451 {
452 public:
453                                                                                                 AlphaToOneInstance                      (Context&                                                                               context,
454                                                                                                                                                          VkPrimitiveTopology                                                    topology,
455                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
456                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
457                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
458                                                                                                                                                          ImageBackingMode                                                               backingMode);
459         virtual                                                                         ~AlphaToOneInstance                     (void) {}
460
461         virtual tcu::TestStatus                                         iterate                                         (void);
462
463 protected:
464         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& alphaOneImage,
465                                                                                                                                                          const tcu::ConstPixelBufferAccess& noAlphaOneImage);
466         const VkFormat                                                          m_colorFormat;
467         const tcu::IVec2                                                        m_renderSize;
468         const VkPrimitiveTopology                                       m_primitiveTopology;
469         const std::vector<Vertex4RGBA>                          m_vertices;
470         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
471         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
472         const ImageBackingMode                                          m_backingMode;
473 };
474
475 class AlphaToCoverageInstance : public vkt::TestInstance
476 {
477 public:
478                                                                                                 AlphaToCoverageInstance         (Context&                                                                               context,
479                                                                                                                                                          VkPrimitiveTopology                                                    topology,
480                                                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
481                                                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
482                                                                                                                                                          const VkPipelineColorBlendAttachmentState&             blendState,
483                                                                                                                                                          GeometryType                                                                   geometryType,
484                                                                                                                                                          ImageBackingMode                                                               backingMode);
485         virtual                                                                         ~AlphaToCoverageInstance        (void) {}
486
487         virtual tcu::TestStatus                                         iterate                                         (void);
488
489 protected:
490         virtual tcu::TestStatus                                         verifyImage                                     (const tcu::ConstPixelBufferAccess& result);
491         const VkFormat                                                          m_colorFormat;
492         const tcu::IVec2                                                        m_renderSize;
493         const VkPrimitiveTopology                                       m_primitiveTopology;
494         const std::vector<Vertex4RGBA>                          m_vertices;
495         const VkPipelineMultisampleStateCreateInfo      m_multisampleStateParams;
496         const VkPipelineColorBlendAttachmentState       m_colorBlendState;
497         const GeometryType                                                      m_geometryType;
498         const ImageBackingMode                                          m_backingMode;
499 };
500
501
502 // Helper functions
503
504 void initMultisamplePrograms (SourceCollections& sources, MultisampleTestParams params)
505 {
506         std::ostringstream vertexSource;
507
508         vertexSource <<
509                 "#version 310 es\n"
510                 "layout(location = 0) in vec4 position;\n"
511                 "layout(location = 1) in vec4 color;\n"
512                 "layout(location = 0) out highp vec4 vtxColor;\n"
513                 "void main (void)\n"
514                 "{\n"
515                 "       gl_Position = position;\n"
516                 "       vtxColor = color;\n"
517                         << (params.geometryType == GEOMETRY_TYPE_OPAQUE_POINT ? "       gl_PointSize = 3.0f;\n"
518                         : "")
519                 << "}\n";
520
521         static const char* fragmentSource =
522                 "#version 310 es\n"
523                 "layout(location = 0) in highp vec4 vtxColor;\n"
524                 "layout(location = 0) out highp vec4 fragColor;\n"
525                 "void main (void)\n"
526                 "{\n"
527                 "       fragColor = vtxColor;\n"
528                 "}\n";
529
530         sources.glslSources.add("color_vert") << glu::VertexSource(vertexSource.str());
531         sources.glslSources.add("color_frag") << glu::FragmentSource(fragmentSource);
532 }
533
534 void initSampleShadingPrograms (SourceCollections& sources, GeometryType geometryType)
535 {
536         {
537                 std::ostringstream vertexSource;
538
539                 vertexSource <<
540                         "#version 440\n"
541                         "layout(location = 0) in vec4 position;\n"
542                         "layout(location = 1) in vec4 color;\n"
543                         "void main (void)\n"
544                         "{\n"
545                         "       gl_Position = position;\n"
546                         << (geometryType == GEOMETRY_TYPE_OPAQUE_POINT ? "      gl_PointSize = 3.0f;\n"
547                                 : "")
548                         << "}\n";
549
550                 static const char* fragmentSource =
551                         "#version 440\n"
552                         "layout(location = 0) out highp vec4 fragColor;\n"
553                         "void main (void)\n"
554                         "{\n"
555                         "       fragColor = vec4(fract(gl_FragCoord.xy), 0.0, 1.0);\n"
556                         "}\n";
557
558                 sources.glslSources.add("color_vert") << glu::VertexSource(vertexSource.str());
559                 sources.glslSources.add("color_frag") << glu::FragmentSource(fragmentSource);
560         }
561
562         {
563                 static const char*  vertexSource =
564                         "#version 440\n"
565                         "void main (void)\n"
566                         "{\n"
567                         "       const vec4 positions[4] = vec4[4](\n"
568                         "               vec4(-1.0, -1.0, 0.0, 1.0),\n"
569                         "               vec4(-1.0,  1.0, 0.0, 1.0),\n"
570                         "               vec4( 1.0, -1.0, 0.0, 1.0),\n"
571                         "               vec4( 1.0,  1.0, 0.0, 1.0)\n"
572                         "       );\n"
573                         "       gl_Position = positions[gl_VertexIndex];\n"
574                         "}\n";
575
576                 static const char* fragmentSource =
577                         "#version 440\n"
578                         "precision highp float;\n"
579                         "layout(location = 0) out highp vec4 fragColor;\n"
580                         "layout(set = 0, binding = 0, input_attachment_index = 0) uniform subpassInputMS imageMS;\n"
581                         "layout(push_constant) uniform PushConstantsBlock\n"
582                         "{\n"
583                         "       int sampleId;\n"
584                         "} pushConstants;\n"
585                         "void main (void)\n"
586                         "{\n"
587                         "       fragColor = subpassLoad(imageMS, pushConstants.sampleId);\n"
588                         "}\n";
589
590                 sources.glslSources.add("quad_vert") << glu::VertexSource(vertexSource);
591                 sources.glslSources.add("copy_sample_frag") << glu::FragmentSource(fragmentSource);
592         }
593 }
594
595 bool isSupportedSampleCount (const InstanceInterface& instanceInterface, VkPhysicalDevice physicalDevice, VkSampleCountFlagBits rasterizationSamples)
596 {
597         VkPhysicalDeviceProperties deviceProperties;
598
599         instanceInterface.getPhysicalDeviceProperties(physicalDevice, &deviceProperties);
600
601         return !!(deviceProperties.limits.framebufferColorSampleCounts & rasterizationSamples);
602 }
603
604 VkPipelineColorBlendAttachmentState getDefaultColorBlendAttachmentState (void)
605 {
606         const VkPipelineColorBlendAttachmentState colorBlendState =
607         {
608                 false,                                                                                                          // VkBool32                                     blendEnable;
609                 VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcColorBlendFactor;
610                 VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstColorBlendFactor;
611                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            colorBlendOp;
612                 VK_BLEND_FACTOR_ONE,                                                                            // VkBlendFactor                        srcAlphaBlendFactor;
613                 VK_BLEND_FACTOR_ZERO,                                                                           // VkBlendFactor                        dstAlphaBlendFactor;
614                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            alphaBlendOp;
615                 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |           // VkColorComponentFlags        colorWriteMask;
616                         VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
617         };
618
619         return colorBlendState;
620 }
621
622 deUint32 getUniqueColorsCount (const tcu::ConstPixelBufferAccess& image)
623 {
624         DE_ASSERT(image.getFormat().getPixelSize() == 4);
625
626         std::map<deUint32, deUint32>    histogram; // map<pixel value, number of occurrences>
627         const deUint32                                  pixelCount      = image.getWidth() * image.getHeight() * image.getDepth();
628
629         for (deUint32 pixelNdx = 0; pixelNdx < pixelCount; pixelNdx++)
630         {
631                 const deUint32 pixelValue = *((const deUint32*)image.getDataPtr() + pixelNdx);
632
633                 if (histogram.find(pixelValue) != histogram.end())
634                         histogram[pixelValue]++;
635                 else
636                         histogram[pixelValue] = 1;
637         }
638
639         return (deUint32)histogram.size();
640 }
641
642 VkImageAspectFlags getImageAspectFlags (const VkFormat format)
643 {
644         const tcu::TextureFormat tcuFormat = mapVkFormat(format);
645
646         if      (tcuFormat.order == tcu::TextureFormat::DS)             return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
647         else if (tcuFormat.order == tcu::TextureFormat::D)              return VK_IMAGE_ASPECT_DEPTH_BIT;
648         else if (tcuFormat.order == tcu::TextureFormat::S)              return VK_IMAGE_ASPECT_STENCIL_BIT;
649
650         DE_ASSERT(false);
651         return 0u;
652 }
653
654 std::vector<Vertex4RGBA> generateVertices (const GeometryType geometryType)
655 {
656         std::vector<Vertex4RGBA> vertices;
657
658         switch (geometryType)
659         {
660                 case GEOMETRY_TYPE_OPAQUE_TRIANGLE:
661                 case GEOMETRY_TYPE_INVISIBLE_TRIANGLE:
662                 {
663                         Vertex4RGBA vertexData[3] =
664                         {
665                                 {
666                                         tcu::Vec4(-0.75f, 0.0f, 0.0f, 1.0f),
667                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
668                                 },
669                                 {
670                                         tcu::Vec4(0.75f, 0.125f, 0.0f, 1.0f),
671                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
672                                 },
673                                 {
674                                         tcu::Vec4(0.75f, -0.125f, 0.0f, 1.0f),
675                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
676                                 }
677                         };
678
679                         if (geometryType == GEOMETRY_TYPE_INVISIBLE_TRIANGLE)
680                         {
681                                 for (int i = 0; i < 3; i++)
682                                         vertexData[i].color = tcu::Vec4();
683                         }
684
685                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 3);
686                         break;
687                 }
688
689                 case GEOMETRY_TYPE_OPAQUE_LINE:
690                 {
691                         const Vertex4RGBA vertexData[2] =
692                         {
693                                 {
694                                         tcu::Vec4(-0.75f, 0.25f, 0.0f, 1.0f),
695                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
696                                 },
697                                 {
698                                         tcu::Vec4(0.75f, -0.25f, 0.0f, 1.0f),
699                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
700                                 }
701                         };
702
703                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 2);
704                         break;
705                 }
706
707                 case GEOMETRY_TYPE_OPAQUE_POINT:
708                 {
709                         const Vertex4RGBA vertex =
710                         {
711                                 tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f),
712                                 tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
713                         };
714
715                         vertices = std::vector<Vertex4RGBA>(1, vertex);
716                         break;
717                 }
718
719                 case GEOMETRY_TYPE_OPAQUE_QUAD:
720                 case GEOMETRY_TYPE_OPAQUE_QUAD_NONZERO_DEPTH:
721                 case GEOMETRY_TYPE_TRANSLUCENT_QUAD:
722                 case GEOMETRY_TYPE_INVISIBLE_QUAD:
723                 case GEOMETRY_TYPE_GRADIENT_QUAD:
724                 {
725                         Vertex4RGBA vertexData[4] =
726                         {
727                                 {
728                                         tcu::Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
729                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
730                                 },
731                                 {
732                                         tcu::Vec4(1.0f, -1.0f, 0.0f, 1.0f),
733                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
734                                 },
735                                 {
736                                         tcu::Vec4(-1.0f, 1.0f, 0.0f, 1.0f),
737                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
738                                 },
739                                 {
740                                         tcu::Vec4(1.0f, 1.0f, 0.0f, 1.0f),
741                                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
742                                 }
743                         };
744
745                         if (geometryType == GEOMETRY_TYPE_TRANSLUCENT_QUAD)
746                         {
747                                 for (int i = 0; i < 4; i++)
748                                         vertexData[i].color.w() = 0.25f;
749                         }
750                         else if (geometryType == GEOMETRY_TYPE_INVISIBLE_QUAD)
751                         {
752                                 for (int i = 0; i < 4; i++)
753                                         vertexData[i].color.w() = 0.0f;
754                         }
755                         else if (geometryType == GEOMETRY_TYPE_GRADIENT_QUAD)
756                         {
757                                 vertexData[0].color.w() = 0.0f;
758                                 vertexData[2].color.w() = 0.0f;
759                         }
760                         else if (geometryType == GEOMETRY_TYPE_OPAQUE_QUAD_NONZERO_DEPTH)
761                         {
762                                 for (int i = 0; i < 4; i++)
763                                         vertexData[i].position.z() = 0.5f;
764                         }
765
766                         vertices = std::vector<Vertex4RGBA>(vertexData, vertexData + 4);
767                         break;
768                 }
769
770                 default:
771                         DE_ASSERT(false);
772         }
773         return vertices;
774 }
775
776 VkPrimitiveTopology getPrimitiveTopology (const GeometryType geometryType)
777 {
778         switch (geometryType)
779         {
780                 case GEOMETRY_TYPE_OPAQUE_TRIANGLE:
781                 case GEOMETRY_TYPE_INVISIBLE_TRIANGLE:                  return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
782
783                 case GEOMETRY_TYPE_OPAQUE_LINE:                                 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
784                 case GEOMETRY_TYPE_OPAQUE_POINT:                                return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
785
786                 case GEOMETRY_TYPE_OPAQUE_QUAD:
787                 case GEOMETRY_TYPE_OPAQUE_QUAD_NONZERO_DEPTH:
788                 case GEOMETRY_TYPE_TRANSLUCENT_QUAD:
789                 case GEOMETRY_TYPE_INVISIBLE_QUAD:
790                 case GEOMETRY_TYPE_GRADIENT_QUAD:                               return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
791
792                 default:
793                         DE_ASSERT(false);
794                         return VK_PRIMITIVE_TOPOLOGY_LAST;
795         }
796 }
797
798 bool isSupportedDepthStencilFormat (const InstanceInterface& vki, const VkPhysicalDevice physDevice, const VkFormat format)
799 {
800         VkFormatProperties formatProps;
801         vki.getPhysicalDeviceFormatProperties(physDevice, format, &formatProps);
802         return (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0;
803 }
804
805 VkFormat findSupportedDepthStencilFormat (Context& context, const bool useDepth, const bool useStencil)
806 {
807         if (useDepth && !useStencil)
808                 return VK_FORMAT_D16_UNORM;             // must be supported
809
810         const InstanceInterface&        vki                     = context.getInstanceInterface();
811         const VkPhysicalDevice          physDevice      = context.getPhysicalDevice();
812
813         // One of these formats must be supported.
814
815         if (isSupportedDepthStencilFormat(vki, physDevice, VK_FORMAT_D24_UNORM_S8_UINT))
816                 return VK_FORMAT_D24_UNORM_S8_UINT;
817
818         if (isSupportedDepthStencilFormat(vki, physDevice, VK_FORMAT_D32_SFLOAT_S8_UINT))
819                 return VK_FORMAT_D32_SFLOAT_S8_UINT;
820
821         return VK_FORMAT_UNDEFINED;
822 }
823
824
825 // MultisampleTest
826
827 MultisampleTest::MultisampleTest (tcu::TestContext&                                                             testContext,
828                                                                   const std::string&                                                    name,
829                                                                   const std::string&                                                    description,
830                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
831                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
832                                                                   GeometryType                                                                  geometryType,
833                                                                   ImageBackingMode                                                              backingMode)
834         : vkt::TestCase                         (testContext, name, description)
835         , m_multisampleStateParams      (multisampleStateParams)
836         , m_colorBlendState                     (blendState)
837         , m_geometryType                        (geometryType)
838         , m_backingMode                         (backingMode)
839 {
840         if (m_multisampleStateParams.pSampleMask)
841         {
842                 // Copy pSampleMask to avoid dependencies with other classes
843
844                 const deUint32 maskCount = deCeilFloatToInt32(float(m_multisampleStateParams.rasterizationSamples) / 32);
845
846                 for (deUint32 maskNdx = 0; maskNdx < maskCount; maskNdx++)
847                         m_sampleMask.push_back(m_multisampleStateParams.pSampleMask[maskNdx]);
848
849                 m_multisampleStateParams.pSampleMask = m_sampleMask.data();
850         }
851 }
852
853 void MultisampleTest::initPrograms (SourceCollections& programCollection) const
854 {
855         MultisampleTestParams params = {m_geometryType, m_backingMode};
856         initMultisamplePrograms(programCollection, params);
857 }
858
859 TestInstance* MultisampleTest::createInstance (Context& context) const
860 {
861         return createMultisampleTestInstance(context, getPrimitiveTopology(m_geometryType), generateVertices(m_geometryType), m_multisampleStateParams, m_colorBlendState);
862 }
863
864
865 // RasterizationSamplesTest
866
867 RasterizationSamplesTest::RasterizationSamplesTest (tcu::TestContext&           testContext,
868                                                                                                         const std::string&              name,
869                                                                                                         const std::string&              description,
870                                                                                                         VkSampleCountFlagBits   rasterizationSamples,
871                                                                                                         GeometryType                    geometryType,
872                                                                                                         ImageBackingMode                backingMode,
873                                                                                                         TestModeFlags                   modeFlags)
874         : MultisampleTest       (testContext, name, description, getRasterizationSamplesStateParams(rasterizationSamples), getDefaultColorBlendAttachmentState(), geometryType, backingMode)
875         , m_backingMode         (backingMode)
876         , m_modeFlags           (modeFlags)
877 {
878 }
879
880 VkPipelineMultisampleStateCreateInfo RasterizationSamplesTest::getRasterizationSamplesStateParams (VkSampleCountFlagBits rasterizationSamples)
881 {
882         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
883         {
884                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
885                 DE_NULL,                                                                                                        // const void*                                                          pNext;
886                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
887                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
888                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
889                 0.0f,                                                                                                           // float                                                                        minSampleShading;
890                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
891                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
892                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
893         };
894
895         return multisampleStateParams;
896 }
897
898 TestInstance* RasterizationSamplesTest::createMultisampleTestInstance (Context&                                                                         context,
899                                                                                                                                            VkPrimitiveTopology                                                  topology,
900                                                                                                                                            const std::vector<Vertex4RGBA>&                              vertices,
901                                                                                                                                            const VkPipelineMultisampleStateCreateInfo&  multisampleStateParams,
902                                                                                                                                            const VkPipelineColorBlendAttachmentState&   colorBlendState) const
903 {
904         return new RasterizationSamplesInstance(context, topology, vertices, multisampleStateParams, colorBlendState, m_modeFlags, m_backingMode);
905 }
906
907
908 // MinSampleShadingTest
909
910 MinSampleShadingTest::MinSampleShadingTest (tcu::TestContext&           testContext,
911                                                                                         const std::string&              name,
912                                                                                         const std::string&              description,
913                                                                                         VkSampleCountFlagBits   rasterizationSamples,
914                                                                                         float                                   minSampleShading,
915                                                                                         GeometryType                    geometryType,
916                                                                                         ImageBackingMode                backingMode)
917         : MultisampleTest       (testContext, name, description, getMinSampleShadingStateParams(rasterizationSamples, minSampleShading), getDefaultColorBlendAttachmentState(), geometryType, backingMode)
918         , m_backingMode         (backingMode)
919 {
920 }
921
922 void MinSampleShadingTest::initPrograms (SourceCollections& programCollection) const
923 {
924         initSampleShadingPrograms(programCollection, m_geometryType);
925 }
926
927 TestInstance* MinSampleShadingTest::createMultisampleTestInstance (Context&                                                                             context,
928                                                                                                                                    VkPrimitiveTopology                                                  topology,
929                                                                                                                                    const std::vector<Vertex4RGBA>&                              vertices,
930                                                                                                                                    const VkPipelineMultisampleStateCreateInfo&  multisampleStateParams,
931                                                                                                                                    const VkPipelineColorBlendAttachmentState&   colorBlendState) const
932 {
933         return new MinSampleShadingInstance(context, topology, vertices, multisampleStateParams, colorBlendState, m_backingMode);
934 }
935
936 VkPipelineMultisampleStateCreateInfo MinSampleShadingTest::getMinSampleShadingStateParams (VkSampleCountFlagBits rasterizationSamples, float minSampleShading)
937 {
938         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
939         {
940                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
941                 DE_NULL,                                                                                                        // const void*                                                          pNext;
942                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
943                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
944                 true,                                                                                                           // VkBool32                                                                     sampleShadingEnable;
945                 minSampleShading,                                                                                       // float                                                                        minSampleShading;
946                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
947                 false,                                                                                                          //  VkBool32                                                            alphaToCoverageEnable;
948                 false                                                                                                           //  VkBool32                                                            alphaToOneEnable;
949         };
950
951         return multisampleStateParams;
952 }
953
954
955 // SampleMaskTest
956
957 SampleMaskTest::SampleMaskTest (tcu::TestContext&                                       testContext,
958                                                                 const std::string&                                      name,
959                                                                 const std::string&                                      description,
960                                                                 VkSampleCountFlagBits                           rasterizationSamples,
961                                                                 const std::vector<VkSampleMask>&        sampleMask,
962                                                                 GeometryType                                            geometryType,
963                                                                 ImageBackingMode                                        backingMode)
964         : MultisampleTest       (testContext, name, description, getSampleMaskStateParams(rasterizationSamples, sampleMask), getDefaultColorBlendAttachmentState(), geometryType, backingMode)
965         , m_backingMode         (backingMode)
966 {
967 }
968
969 TestInstance* SampleMaskTest::createMultisampleTestInstance (Context&                                                                           context,
970                                                                                                                          VkPrimitiveTopology                                                    topology,
971                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
972                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
973                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const
974 {
975         return new SampleMaskInstance(context, topology,vertices, multisampleStateParams, colorBlendState, m_backingMode);
976 }
977
978 VkPipelineMultisampleStateCreateInfo SampleMaskTest::getSampleMaskStateParams (VkSampleCountFlagBits rasterizationSamples, const std::vector<VkSampleMask>& sampleMask)
979 {
980         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
981         {
982                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
983                 DE_NULL,                                                                                                        // const void*                                                          pNext;
984                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
985                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
986                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
987                 0.0f,                                                                                                           // float                                                                        minSampleShading;
988                 sampleMask.data(),                                                                                      // const VkSampleMask*                                          pSampleMask;
989                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
990                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
991         };
992
993         return multisampleStateParams;
994 }
995
996
997 // AlphaToOneTest
998
999 AlphaToOneTest::AlphaToOneTest (tcu::TestContext&               testContext,
1000                                                                 const std::string&              name,
1001                                                                 const std::string&              description,
1002                                                                 VkSampleCountFlagBits   rasterizationSamples,
1003                                                                 ImageBackingMode                backingMode)
1004         : MultisampleTest       (testContext, name, description, getAlphaToOneStateParams(rasterizationSamples), getAlphaToOneBlendState(), GEOMETRY_TYPE_GRADIENT_QUAD, backingMode)
1005         , m_backingMode(backingMode)
1006 {
1007 }
1008
1009 TestInstance* AlphaToOneTest::createMultisampleTestInstance (Context&                                                                           context,
1010                                                                                                                          VkPrimitiveTopology                                                    topology,
1011                                                                                                                          const std::vector<Vertex4RGBA>&                                vertices,
1012                                                                                                                          const VkPipelineMultisampleStateCreateInfo&    multisampleStateParams,
1013                                                                                                                          const VkPipelineColorBlendAttachmentState&             colorBlendState) const
1014 {
1015         return new AlphaToOneInstance(context, topology, vertices, multisampleStateParams, colorBlendState, m_backingMode);
1016 }
1017
1018 VkPipelineMultisampleStateCreateInfo AlphaToOneTest::getAlphaToOneStateParams (VkSampleCountFlagBits rasterizationSamples)
1019 {
1020         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
1021         {
1022                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
1023                 DE_NULL,                                                                                                        // const void*                                                          pNext;
1024                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
1025                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
1026                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
1027                 0.0f,                                                                                                           // float                                                                        minSampleShading;
1028                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
1029                 false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
1030                 true                                                                                                            // VkBool32                                                                     alphaToOneEnable;
1031         };
1032
1033         return multisampleStateParams;
1034 }
1035
1036 VkPipelineColorBlendAttachmentState AlphaToOneTest::getAlphaToOneBlendState (void)
1037 {
1038         const VkPipelineColorBlendAttachmentState colorBlendState =
1039         {
1040                 true,                                                                                                           // VkBool32                                     blendEnable;
1041                 VK_BLEND_FACTOR_SRC_ALPHA,                                                                      // VkBlendFactor                        srcColorBlendFactor;
1042                 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,                                            // VkBlendFactor                        dstColorBlendFactor;
1043                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            colorBlendOp;
1044                 VK_BLEND_FACTOR_SRC_ALPHA,                                                                      // VkBlendFactor                        srcAlphaBlendFactor;
1045                 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,                                            // VkBlendFactor                        dstAlphaBlendFactor;
1046                 VK_BLEND_OP_ADD,                                                                                        // VkBlendOp                            alphaBlendOp;
1047                 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |           // VkColorComponentFlags        colorWriteMask;
1048                         VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
1049         };
1050
1051         return colorBlendState;
1052 }
1053
1054
1055 // AlphaToCoverageTest
1056
1057 AlphaToCoverageTest::AlphaToCoverageTest (tcu::TestContext&                     testContext,
1058                                                                                   const std::string&            name,
1059                                                                                   const std::string&            description,
1060                                                                                   VkSampleCountFlagBits         rasterizationSamples,
1061                                                                                   GeometryType                          geometryType,
1062                                                                                   ImageBackingMode                      backingMode)
1063         : MultisampleTest       (testContext, name, description, getAlphaToCoverageStateParams(rasterizationSamples), getDefaultColorBlendAttachmentState(), geometryType, backingMode)
1064         , m_geometryType        (geometryType)
1065         , m_backingMode         (backingMode)
1066 {
1067 }
1068
1069 TestInstance* AlphaToCoverageTest::createMultisampleTestInstance (Context&                                                                              context,
1070                                                                                                                                   VkPrimitiveTopology                                                   topology,
1071                                                                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
1072                                                                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1073                                                                                                                                   const VkPipelineColorBlendAttachmentState&    colorBlendState) const
1074 {
1075         return new AlphaToCoverageInstance(context, topology, vertices, multisampleStateParams, colorBlendState, m_geometryType, m_backingMode);
1076 }
1077
1078 VkPipelineMultisampleStateCreateInfo AlphaToCoverageTest::getAlphaToCoverageStateParams (VkSampleCountFlagBits rasterizationSamples)
1079 {
1080         const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
1081         {
1082                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
1083                 DE_NULL,                                                                                                        // const void*                                                          pNext;
1084                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
1085                 rasterizationSamples,                                                                           // VkSampleCountFlagBits                                        rasterizationSamples;
1086                 false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
1087                 0.0f,                                                                                                           // float                                                                        minSampleShading;
1088                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
1089                 true,                                                                                                           // VkBool32                                                                     alphaToCoverageEnable;
1090                 false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
1091         };
1092
1093         return multisampleStateParams;
1094 }
1095
1096 // RasterizationSamplesInstance
1097
1098 RasterizationSamplesInstance::RasterizationSamplesInstance (Context&                                                                            context,
1099                                                                                                                         VkPrimitiveTopology                                                             topology,
1100                                                                                                                         const std::vector<Vertex4RGBA>&                                 vertices,
1101                                                                                                                         const VkPipelineMultisampleStateCreateInfo&             multisampleStateParams,
1102                                                                                                                         const VkPipelineColorBlendAttachmentState&              blendState,
1103                                                                                                                         const TestModeFlags                                                             modeFlags,
1104                                                                                                                         ImageBackingMode                                                                backingMode)
1105         : vkt::TestInstance             (context)
1106         , m_colorFormat                 (VK_FORMAT_R8G8B8A8_UNORM)
1107         , m_renderSize                  (32, 32)
1108         , m_primitiveTopology   (topology)
1109         , m_vertices                    (vertices)
1110         , m_fullQuadVertices    (generateVertices(GEOMETRY_TYPE_OPAQUE_QUAD_NONZERO_DEPTH))
1111         , m_modeFlags                   (modeFlags)
1112 {
1113         if (m_modeFlags != 0)
1114         {
1115                 const bool              useDepth                        = (m_modeFlags & TEST_MODE_DEPTH_BIT) != 0;
1116                 const bool              useStencil                      = (m_modeFlags & TEST_MODE_STENCIL_BIT) != 0;
1117                 const VkFormat  depthStencilFormat      = findSupportedDepthStencilFormat(context, useDepth, useStencil);
1118
1119                 if (depthStencilFormat == VK_FORMAT_UNDEFINED)
1120                         TCU_THROW(NotSupportedError, "Required depth/stencil format is not supported");
1121
1122                 const VkPrimitiveTopology               pTopology[2] = { m_primitiveTopology, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP };
1123                 const std::vector<Vertex4RGBA>  pVertices[2] = { m_vertices, m_fullQuadVertices };
1124
1125                 m_multisampleRenderer = de::MovePtr<MultisampleRenderer>(
1126                         new MultisampleRenderer(
1127                                 context, m_colorFormat, depthStencilFormat, m_renderSize, useDepth, useStencil, 2u, pTopology, pVertices, multisampleStateParams, blendState, RENDER_TYPE_RESOLVE, backingMode));
1128         }
1129         else
1130         {
1131                 m_multisampleRenderer = de::MovePtr<MultisampleRenderer>(
1132                         new MultisampleRenderer(context, m_colorFormat, m_renderSize, topology, vertices, multisampleStateParams, blendState, RENDER_TYPE_RESOLVE, backingMode));
1133         }
1134 }
1135
1136 tcu::TestStatus RasterizationSamplesInstance::iterate (void)
1137 {
1138         de::MovePtr<tcu::TextureLevel> level(m_multisampleRenderer->render());
1139         return verifyImage(level->getAccess());
1140 }
1141
1142 tcu::TestStatus RasterizationSamplesInstance::verifyImage (const tcu::ConstPixelBufferAccess& result)
1143 {
1144         // Verify range of unique pixels
1145         {
1146                 const deUint32  numUniqueColors = getUniqueColorsCount(result);
1147                 const deUint32  minUniqueColors = 3;
1148
1149                 tcu::TestLog& log = m_context.getTestContext().getLog();
1150
1151                 log << tcu::TestLog::Message
1152                         << "\nMin. unique colors expected: " << minUniqueColors << "\n"
1153                         << "Unique colors found: " << numUniqueColors << "\n"
1154                         << tcu::TestLog::EndMessage;
1155
1156                 if (numUniqueColors < minUniqueColors)
1157                         return tcu::TestStatus::fail("Unique colors out of expected bounds");
1158         }
1159
1160         // Verify shape of the rendered primitive (fuzzy-compare)
1161         {
1162                 const tcu::TextureFormat        tcuColorFormat  = mapVkFormat(m_colorFormat);
1163                 const tcu::TextureFormat        tcuDepthFormat  = tcu::TextureFormat();
1164                 const ColorVertexShader         vertexShader;
1165                 const ColorFragmentShader       fragmentShader  (tcuColorFormat, tcuDepthFormat);
1166                 const rr::Program                       program                 (&vertexShader, &fragmentShader);
1167                 ReferenceRenderer                       refRenderer             (m_renderSize.x(), m_renderSize.y(), 1, tcuColorFormat, tcuDepthFormat, &program);
1168                 rr::RenderState                         renderState             (refRenderer.getViewportState());
1169
1170                 if (m_primitiveTopology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
1171                 {
1172                         VkPhysicalDeviceProperties deviceProperties;
1173
1174                         m_context.getInstanceInterface().getPhysicalDeviceProperties(m_context.getPhysicalDevice(), &deviceProperties);
1175
1176                         // gl_PointSize is clamped to pointSizeRange
1177                         renderState.point.pointSize = deFloatMin(3.0f, deviceProperties.limits.pointSizeRange[1]);
1178                 }
1179
1180                 if (m_modeFlags == 0)
1181                 {
1182                         refRenderer.colorClear(tcu::Vec4(0.0f));
1183                         refRenderer.draw(renderState, mapVkPrimitiveTopology(m_primitiveTopology), m_vertices);
1184                 }
1185                 else
1186                 {
1187                         // For depth/stencil case the primitive is invisible and the surroundings are filled red.
1188                         refRenderer.colorClear(tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
1189                         refRenderer.draw(renderState, mapVkPrimitiveTopology(m_primitiveTopology), m_vertices);
1190                 }
1191
1192                 if (!tcu::fuzzyCompare(m_context.getTestContext().getLog(), "FuzzyImageCompare", "Image comparison", refRenderer.getAccess(), result, 0.05f, tcu::COMPARE_LOG_RESULT))
1193                         return tcu::TestStatus::fail("Primitive has unexpected shape");
1194         }
1195
1196         return tcu::TestStatus::pass("Primitive rendered, unique colors within expected bounds");
1197 }
1198
1199
1200 // MinSampleShadingInstance
1201
1202 MinSampleShadingInstance::MinSampleShadingInstance (Context&                                                                    context,
1203                                                                                                         VkPrimitiveTopology                                                     topology,
1204                                                                                                         const std::vector<Vertex4RGBA>&                         vertices,
1205                                                                                                         const VkPipelineMultisampleStateCreateInfo&     multisampleStateParams,
1206                                                                                                         const VkPipelineColorBlendAttachmentState&      colorBlendState,
1207                                                                                                         ImageBackingMode                                                        backingMode)
1208         : vkt::TestInstance                     (context)
1209         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1210         , m_renderSize                          (32, 32)
1211         , m_primitiveTopology           (topology)
1212         , m_vertices                            (vertices)
1213         , m_multisampleStateParams      (multisampleStateParams)
1214         , m_colorBlendState                     (colorBlendState)
1215         , m_backingMode                         (backingMode)
1216 {
1217         VkPhysicalDeviceFeatures deviceFeatures;
1218
1219         m_context.getInstanceInterface().getPhysicalDeviceFeatures(m_context.getPhysicalDevice(), &deviceFeatures);
1220
1221         if (!deviceFeatures.sampleRateShading)
1222                 throw tcu::NotSupportedError("Sample shading is not supported");
1223 }
1224
1225 tcu::TestStatus MinSampleShadingInstance::iterate (void)
1226 {
1227         de::MovePtr<tcu::TextureLevel>  noSampleshadingImage;
1228         std::vector<tcu::TextureLevel>  sampleShadedImages;
1229
1230         // Render and resolve without sample shading
1231         {
1232                 VkPipelineMultisampleStateCreateInfo multisampleStateParms = m_multisampleStateParams;
1233                 multisampleStateParms.sampleShadingEnable       = VK_FALSE;
1234                 multisampleStateParms.minSampleShading          = 0.0;
1235
1236                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleStateParms, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1237                 noSampleshadingImage  = renderer.render();
1238         }
1239
1240         // Render with test minSampleShading and collect per-sample images
1241         {
1242                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState, RENDER_TYPE_COPY_SAMPLES, m_backingMode);
1243                 renderer.render();
1244
1245                 sampleShadedImages.resize(m_multisampleStateParams.rasterizationSamples);
1246                 for (deUint32 sampleId = 0; sampleId < sampleShadedImages.size(); sampleId++)
1247                 {
1248                         sampleShadedImages[sampleId] = *renderer.getSingleSampledImage(sampleId);
1249                 }
1250         }
1251
1252         // Log images
1253         {
1254                 tcu::TestLog& testLog   = m_context.getTestContext().getLog();
1255
1256                 testLog << tcu::TestLog::ImageSet("Images", "Images")
1257                                 << tcu::TestLog::Image("noSampleshadingImage", "Image rendered without sample shading", noSampleshadingImage->getAccess());
1258
1259                 for (deUint32 sampleId = 0; sampleId < sampleShadedImages.size(); sampleId++)
1260                 {
1261                         testLog << tcu::TestLog::Image("sampleShadedImage", "One sample of sample shaded image", sampleShadedImages[sampleId].getAccess());
1262                 }
1263                 testLog << tcu::TestLog::EndImageSet;
1264         }
1265
1266         return verifySampleShadedImage(sampleShadedImages, noSampleshadingImage->getAccess());
1267 }
1268
1269 tcu::TestStatus MinSampleShadingInstance::verifySampleShadedImage (const std::vector<tcu::TextureLevel>& sampleShadedImages, const tcu::ConstPixelBufferAccess& noSampleshadingImage)
1270 {
1271         const deUint32  pixelCount      = noSampleshadingImage.getWidth() * noSampleshadingImage.getHeight() * noSampleshadingImage.getDepth();
1272
1273         bool anyPixelCovered            = false;
1274
1275         for (deUint32 pixelNdx = 0; pixelNdx < pixelCount; pixelNdx++)
1276         {
1277                 const deUint32 noSampleShadingValue = *((const deUint32*)noSampleshadingImage.getDataPtr() + pixelNdx);
1278
1279                 if (noSampleShadingValue == 0)
1280                 {
1281                         // non-covered pixel, continue
1282                         continue;
1283                 }
1284                 else
1285                 {
1286                         anyPixelCovered = true;
1287                 }
1288
1289                 int numNotCoveredSamples = 0;
1290
1291                 std::map<deUint32, deUint32>    histogram; // map<pixel value, number of occurrences>
1292
1293                 // Collect histogram of occurrences or each pixel across all samples
1294                 for (size_t i = 0; i < sampleShadedImages.size(); ++i)
1295                 {
1296                         const deUint32 sampleShadedValue = *((const deUint32*)sampleShadedImages[i].getAccess().getDataPtr() + pixelNdx);
1297
1298                         if (sampleShadedValue == 0)
1299                         {
1300                                 numNotCoveredSamples++;
1301                                 continue;
1302                         }
1303
1304                         if (histogram.find(sampleShadedValue) != histogram.end())
1305                                 histogram[sampleShadedValue]++;
1306                         else
1307                                 histogram[sampleShadedValue] = 1;
1308                 }
1309
1310                 if (numNotCoveredSamples == static_cast<int>(sampleShadedImages.size()))
1311                 {
1312                         return tcu::TestStatus::fail("Got uncovered pixel, where covered samples were expected");
1313                 }
1314
1315                 const int uniqueColorsCount                             = (int)histogram.size();
1316                 const int expectedUniqueSamplesCount    = static_cast<int>(m_multisampleStateParams.minSampleShading * static_cast<float>(sampleShadedImages.size()) + 0.5f);
1317
1318                 if (uniqueColorsCount + numNotCoveredSamples < expectedUniqueSamplesCount)
1319                 {
1320                         return tcu::TestStatus::fail("Got less unique colors than requested through minSampleShading");
1321                 }
1322         }
1323
1324         if (!anyPixelCovered)
1325         {
1326                 return tcu::TestStatus::fail("Did not get any covered pixel, cannot test minSampleShading");
1327         }
1328
1329         return tcu::TestStatus::pass("Got proper count of unique colors");
1330 }
1331
1332 SampleMaskInstance::SampleMaskInstance (Context&                                                                                context,
1333                                                                                 VkPrimitiveTopology                                                             topology,
1334                                                                                 const std::vector<Vertex4RGBA>&                                 vertices,
1335                                                                                 const VkPipelineMultisampleStateCreateInfo&             multisampleStateParams,
1336                                                                                 const VkPipelineColorBlendAttachmentState&              blendState,
1337                                                                                 ImageBackingMode                                                                backingMode)
1338         : vkt::TestInstance                     (context)
1339         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1340         , m_renderSize                          (32, 32)
1341         , m_primitiveTopology           (topology)
1342         , m_vertices                            (vertices)
1343         , m_multisampleStateParams      (multisampleStateParams)
1344         , m_colorBlendState                     (blendState)
1345         , m_backingMode                         (backingMode)
1346 {
1347 }
1348
1349 tcu::TestStatus SampleMaskInstance::iterate (void)
1350 {
1351         de::MovePtr<tcu::TextureLevel>                          testSampleMaskImage;
1352         de::MovePtr<tcu::TextureLevel>                          minSampleMaskImage;
1353         de::MovePtr<tcu::TextureLevel>                          maxSampleMaskImage;
1354
1355         // Render with test flags
1356         {
1357                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1358                 testSampleMaskImage = renderer.render();
1359         }
1360
1361         // Render with all flags off
1362         {
1363                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1364                 const std::vector<VkSampleMask>                 sampleMask                      (multisampleParams.rasterizationSamples / 32, (VkSampleMask)0);
1365
1366                 multisampleParams.pSampleMask = sampleMask.data();
1367
1368                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1369                 minSampleMaskImage = renderer.render();
1370         }
1371
1372         // Render with all flags on
1373         {
1374                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1375                 const std::vector<VkSampleMask>                 sampleMask                      (multisampleParams.rasterizationSamples / 32, ~((VkSampleMask)0));
1376
1377                 multisampleParams.pSampleMask = sampleMask.data();
1378
1379                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1380                 maxSampleMaskImage = renderer.render();
1381         }
1382
1383         return verifyImage(testSampleMaskImage->getAccess(), minSampleMaskImage->getAccess(), maxSampleMaskImage->getAccess());
1384 }
1385
1386 tcu::TestStatus SampleMaskInstance::verifyImage (const tcu::ConstPixelBufferAccess& testSampleMaskImage,
1387                                                                                                  const tcu::ConstPixelBufferAccess& minSampleMaskImage,
1388                                                                                                  const tcu::ConstPixelBufferAccess& maxSampleMaskImage)
1389 {
1390         const deUint32  testColorCount  = getUniqueColorsCount(testSampleMaskImage);
1391         const deUint32  minColorCount   = getUniqueColorsCount(minSampleMaskImage);
1392         const deUint32  maxColorCount   = getUniqueColorsCount(maxSampleMaskImage);
1393
1394         tcu::TestLog& log = m_context.getTestContext().getLog();
1395
1396         log << tcu::TestLog::Message
1397                 << "\nColors found: " << testColorCount << "\n"
1398                 << "Min. colors expected: " << minColorCount << "\n"
1399                 << "Max. colors expected: " << maxColorCount << "\n"
1400                 << tcu::TestLog::EndMessage;
1401
1402         if (minColorCount > testColorCount || testColorCount > maxColorCount)
1403                 return tcu::TestStatus::fail("Unique colors out of expected bounds");
1404         else
1405                 return tcu::TestStatus::pass("Unique colors within expected bounds");
1406 }
1407
1408 tcu::TestStatus testRasterSamplesConsistency (Context& context, MultisampleTestParams params)
1409 {
1410         const VkSampleCountFlagBits samples[] =
1411         {
1412                 VK_SAMPLE_COUNT_1_BIT,
1413                 VK_SAMPLE_COUNT_2_BIT,
1414                 VK_SAMPLE_COUNT_4_BIT,
1415                 VK_SAMPLE_COUNT_8_BIT,
1416                 VK_SAMPLE_COUNT_16_BIT,
1417                 VK_SAMPLE_COUNT_32_BIT,
1418                 VK_SAMPLE_COUNT_64_BIT
1419         };
1420
1421         const Vertex4RGBA vertexData[3] =
1422         {
1423                 {
1424                         tcu::Vec4(-0.75f, 0.0f, 0.0f, 1.0f),
1425                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1426                 },
1427                 {
1428                         tcu::Vec4(0.75f, 0.125f, 0.0f, 1.0f),
1429                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1430                 },
1431                 {
1432                         tcu::Vec4(0.75f, -0.125f, 0.0f, 1.0f),
1433                         tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f)
1434                 }
1435         };
1436
1437         const std::vector<Vertex4RGBA>  vertices                        (vertexData, vertexData + 3);
1438         deUint32                                                prevUniqueColors        = 2;
1439         int                                                             renderCount                     = 0;
1440
1441         // Do not render with 1 sample (start with samplesNdx = 1).
1442         for (int samplesNdx = 1; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
1443         {
1444                 if (!isSupportedSampleCount(context.getInstanceInterface(), context.getPhysicalDevice(), samples[samplesNdx]))
1445                         continue;
1446
1447                 const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
1448                 {
1449                         VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType;
1450                         DE_NULL,                                                                                                        // const void*                                                          pNext;
1451                         0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags;
1452                         samples[samplesNdx],                                                                            // VkSampleCountFlagBits                                        rasterizationSamples;
1453                         false,                                                                                                          // VkBool32                                                                     sampleShadingEnable;
1454                         0.0f,                                                                                                           // float                                                                        minSampleShading;
1455                         DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask;
1456                         false,                                                                                                          // VkBool32                                                                     alphaToCoverageEnable;
1457                         false                                                                                                           // VkBool32                                                                     alphaToOneEnable;
1458                 };
1459
1460                 MultisampleRenderer                             renderer                (context, VK_FORMAT_R8G8B8A8_UNORM, tcu::IVec2(32, 32), VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, vertices, multisampleStateParams, getDefaultColorBlendAttachmentState(), RENDER_TYPE_RESOLVE, params.backingMode);
1461                 de::MovePtr<tcu::TextureLevel>  result                  = renderer.render();
1462                 const deUint32                                  uniqueColors    = getUniqueColorsCount(result->getAccess());
1463
1464                 renderCount++;
1465
1466                 if (prevUniqueColors > uniqueColors)
1467                 {
1468                         std::ostringstream message;
1469
1470                         message << "More unique colors generated with " << samples[samplesNdx - 1] << " than with " << samples[samplesNdx];
1471                         return tcu::TestStatus::fail(message.str());
1472                 }
1473
1474                 prevUniqueColors = uniqueColors;
1475         }
1476
1477         if (renderCount == 0)
1478                 throw tcu::NotSupportedError("Multisampling is unsupported");
1479
1480         return tcu::TestStatus::pass("Number of unique colors increases as the sample count increases");
1481 }
1482
1483
1484 // AlphaToOneInstance
1485
1486 AlphaToOneInstance::AlphaToOneInstance (Context&                                                                        context,
1487                                                                                 VkPrimitiveTopology                                                     topology,
1488                                                                                 const std::vector<Vertex4RGBA>&                         vertices,
1489                                                                                 const VkPipelineMultisampleStateCreateInfo&     multisampleStateParams,
1490                                                                                 const VkPipelineColorBlendAttachmentState&      blendState,
1491                                                                                 ImageBackingMode                                                        backingMode)
1492         : vkt::TestInstance                     (context)
1493         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1494         , m_renderSize                          (32, 32)
1495         , m_primitiveTopology           (topology)
1496         , m_vertices                            (vertices)
1497         , m_multisampleStateParams      (multisampleStateParams)
1498         , m_colorBlendState                     (blendState)
1499         , m_backingMode                         (backingMode)
1500 {
1501         VkPhysicalDeviceFeatures deviceFeatures;
1502
1503         context.getInstanceInterface().getPhysicalDeviceFeatures(context.getPhysicalDevice(), &deviceFeatures);
1504
1505         if (!deviceFeatures.alphaToOne)
1506                 throw tcu::NotSupportedError("Alpha-to-one is not supported");
1507 }
1508
1509 tcu::TestStatus AlphaToOneInstance::iterate     (void)
1510 {
1511         DE_ASSERT(m_multisampleStateParams.alphaToOneEnable);
1512         DE_ASSERT(m_colorBlendState.blendEnable);
1513
1514         de::MovePtr<tcu::TextureLevel>  alphaOneImage;
1515         de::MovePtr<tcu::TextureLevel>  noAlphaOneImage;
1516
1517         // Render with blend enabled and alpha to one on
1518         {
1519                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1520                 alphaOneImage = renderer.render();
1521         }
1522
1523         // Render with blend enabled and alpha to one off
1524         {
1525                 VkPipelineMultisampleStateCreateInfo    multisampleParams       = m_multisampleStateParams;
1526                 multisampleParams.alphaToOneEnable = false;
1527
1528                 MultisampleRenderer renderer (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, multisampleParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1529                 noAlphaOneImage = renderer.render();
1530         }
1531
1532         return verifyImage(alphaOneImage->getAccess(), noAlphaOneImage->getAccess());
1533 }
1534
1535 tcu::TestStatus AlphaToOneInstance::verifyImage (const tcu::ConstPixelBufferAccess&     alphaOneImage,
1536                                                                                                  const tcu::ConstPixelBufferAccess&     noAlphaOneImage)
1537 {
1538         for (int y = 0; y < m_renderSize.y(); y++)
1539         {
1540                 for (int x = 0; x < m_renderSize.x(); x++)
1541                 {
1542                         if (!tcu::boolAll(tcu::greaterThanEqual(alphaOneImage.getPixel(x, y), noAlphaOneImage.getPixel(x, y))))
1543                         {
1544                                 std::ostringstream message;
1545                                 message << "Unsatisfied condition: " << alphaOneImage.getPixel(x, y) << " >= " << noAlphaOneImage.getPixel(x, y);
1546                                 return tcu::TestStatus::fail(message.str());
1547                         }
1548                 }
1549         }
1550
1551         return tcu::TestStatus::pass("Image rendered with alpha-to-one contains pixels of image rendered with no alpha-to-one");
1552 }
1553
1554
1555 // AlphaToCoverageInstance
1556
1557 AlphaToCoverageInstance::AlphaToCoverageInstance (Context&                                                                              context,
1558                                                                                                   VkPrimitiveTopology                                                   topology,
1559                                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
1560                                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1561                                                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
1562                                                                                                   GeometryType                                                                  geometryType,
1563                                                                                                   ImageBackingMode                                                              backingMode)
1564         : vkt::TestInstance                     (context)
1565         , m_colorFormat                         (VK_FORMAT_R8G8B8A8_UNORM)
1566         , m_renderSize                          (32, 32)
1567         , m_primitiveTopology           (topology)
1568         , m_vertices                            (vertices)
1569         , m_multisampleStateParams      (multisampleStateParams)
1570         , m_colorBlendState                     (blendState)
1571         , m_geometryType                        (geometryType)
1572         , m_backingMode                         (backingMode)
1573 {
1574 }
1575
1576 tcu::TestStatus AlphaToCoverageInstance::iterate (void)
1577 {
1578         DE_ASSERT(m_multisampleStateParams.alphaToCoverageEnable);
1579
1580         de::MovePtr<tcu::TextureLevel>  result;
1581         MultisampleRenderer                             renderer        (m_context, m_colorFormat, m_renderSize, m_primitiveTopology, m_vertices, m_multisampleStateParams, m_colorBlendState, RENDER_TYPE_RESOLVE, m_backingMode);
1582
1583         result = renderer.render();
1584
1585         return verifyImage(result->getAccess());
1586 }
1587
1588 tcu::TestStatus AlphaToCoverageInstance::verifyImage (const tcu::ConstPixelBufferAccess&        result)
1589 {
1590         float maxColorValue;
1591
1592         switch (m_geometryType)
1593         {
1594                 case GEOMETRY_TYPE_OPAQUE_QUAD:
1595                         maxColorValue = 1.01f;
1596                         break;
1597
1598                 case GEOMETRY_TYPE_TRANSLUCENT_QUAD:
1599                         maxColorValue = 0.52f;
1600                         break;
1601
1602                 case GEOMETRY_TYPE_INVISIBLE_QUAD:
1603                         maxColorValue = 0.01f;
1604                         break;
1605
1606                 default:
1607                         maxColorValue = 0.0f;
1608                         DE_ASSERT(false);
1609         }
1610
1611         for (int y = 0; y < m_renderSize.y(); y++)
1612         {
1613                 for (int x = 0; x < m_renderSize.x(); x++)
1614                 {
1615                         if (result.getPixel(x, y).x() > maxColorValue)
1616                         {
1617                                 std::ostringstream message;
1618                                 message << "Pixel is not below the threshold value (" << result.getPixel(x, y).x() << " > " << maxColorValue << ")";
1619                                 return tcu::TestStatus::fail(message.str());
1620                         }
1621                 }
1622         }
1623
1624         return tcu::TestStatus::pass("Image matches reference value");
1625 }
1626
1627
1628 // MultisampleRenderer
1629
1630 MultisampleRenderer::MultisampleRenderer (Context&                                                                              context,
1631                                                                                   const VkFormat                                                                colorFormat,
1632                                                                                   const tcu::IVec2&                                                             renderSize,
1633                                                                                   const VkPrimitiveTopology                                             topology,
1634                                                                                   const std::vector<Vertex4RGBA>&                               vertices,
1635                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1636                                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
1637                                                                                   const RenderType                                                              renderType,
1638                                                                                   const ImageBackingMode                                                backingMode)
1639         : m_context                                     (context)
1640         , m_bindSemaphore                       (createSemaphore(context.getDeviceInterface(), context.getDevice()))
1641         , m_colorFormat                         (colorFormat)
1642         , m_depthStencilFormat          (VK_FORMAT_UNDEFINED)
1643         , m_renderSize                          (renderSize)
1644         , m_useDepth                            (false)
1645         , m_useStencil                          (false)
1646         , m_multisampleStateParams      (multisampleStateParams)
1647         , m_colorBlendState                     (blendState)
1648         , m_renderType                          (renderType)
1649         , m_backingMode                         (backingMode)
1650 {
1651         initialize(context, 1u, &topology, &vertices);
1652 }
1653
1654 MultisampleRenderer::MultisampleRenderer (Context&                                                                              context,
1655                                                                                   const VkFormat                                                                colorFormat,
1656                                                                                   const VkFormat                                                                depthStencilFormat,
1657                                                                                   const tcu::IVec2&                                                             renderSize,
1658                                                                                   const bool                                                                    useDepth,
1659                                                                                   const bool                                                                    useStencil,
1660                                                                                   const deUint32                                                                numTopologies,
1661                                                                                   const VkPrimitiveTopology*                                    pTopology,
1662                                                                                   const std::vector<Vertex4RGBA>*                               pVertices,
1663                                                                                   const VkPipelineMultisampleStateCreateInfo&   multisampleStateParams,
1664                                                                                   const VkPipelineColorBlendAttachmentState&    blendState,
1665                                                                                   const RenderType                                                              renderType,
1666                                                                                   const ImageBackingMode                                                backingMode)
1667         : m_context                                     (context)
1668         , m_bindSemaphore                       (createSemaphore(context.getDeviceInterface(), context.getDevice()))
1669         , m_colorFormat                         (colorFormat)
1670         , m_depthStencilFormat          (depthStencilFormat)
1671         , m_renderSize                          (renderSize)
1672         , m_useDepth                            (useDepth)
1673         , m_useStencil                          (useStencil)
1674         , m_multisampleStateParams      (multisampleStateParams)
1675         , m_colorBlendState                     (blendState)
1676         , m_renderType                          (renderType)
1677         , m_backingMode                         (backingMode)
1678 {
1679         initialize(context, numTopologies, pTopology, pVertices);
1680 }
1681
1682 void MultisampleRenderer::initialize (Context&                                                                  context,
1683                                                                           const deUint32                                                        numTopologies,
1684                                                                           const VkPrimitiveTopology*                            pTopology,
1685                                                                           const std::vector<Vertex4RGBA>*                       pVertices)
1686 {
1687         if (!isSupportedSampleCount(context.getInstanceInterface(), context.getPhysicalDevice(), m_multisampleStateParams.rasterizationSamples))
1688                 throw tcu::NotSupportedError("Unsupported number of rasterization samples");
1689
1690         const DeviceInterface&          vk                                              = context.getDeviceInterface();
1691         const VkDevice                          vkDevice                                = context.getDevice();
1692         const deUint32                          queueFamilyIndices[]    = { context.getUniversalQueueFamilyIndex(), context.getSparseQueueFamilyIndex() };
1693         const bool                                      sparse                                  = m_backingMode == IMAGE_BACKING_MODE_SPARSE;
1694         const VkComponentMapping        componentMappingRGBA    = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
1695         const VkImageCreateFlags        imageCreateFlags                = sparse ? (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) : 0u;
1696         const VkSharingMode                     sharingMode                             = (sparse && context.getUniversalQueueFamilyIndex() != context.getSparseQueueFamilyIndex()) ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
1697         Allocator&                                      memAlloc                                = m_context.getDefaultAllocator();
1698
1699         // Create color image
1700         {
1701                 const VkImageUsageFlags imageUsageFlags         = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1702                         (m_renderType == RENDER_TYPE_COPY_SAMPLES ? VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT : (VkImageUsageFlagBits)0u);
1703
1704                 const VkImageCreateInfo colorImageParams        =
1705                 {
1706                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                            // VkStructureType                      sType;
1707                         DE_NULL,                                                                                                                                        // const void*                          pNext;
1708                         imageCreateFlags,                                                                                                                       // VkImageCreateFlags           flags;
1709                         VK_IMAGE_TYPE_2D,                                                                                                                       // VkImageType                          imageType;
1710                         m_colorFormat,                                                                                                                          // VkFormat                                     format;
1711                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y(), 1u },                         // VkExtent3D                           extent;
1712                         1u,                                                                                                                                                     // deUint32                                     mipLevels;
1713                         1u,                                                                                                                                                     // deUint32                                     arrayLayers;
1714                         m_multisampleStateParams.rasterizationSamples,                                                          // VkSampleCountFlagBits        samples;
1715                         VK_IMAGE_TILING_OPTIMAL,                                                                                                        // VkImageTiling                        tiling;
1716                         imageUsageFlags,                                                                                                                        // VkImageUsageFlags            usage;
1717                         sharingMode,                                                                                                                            // VkSharingMode                        sharingMode;
1718                         sharingMode == VK_SHARING_MODE_CONCURRENT ? 2u : 1u,                                            // deUint32                                     queueFamilyIndexCount;
1719                         queueFamilyIndices,                                                                                                                     // const deUint32*                      pQueueFamilyIndices;
1720                         VK_IMAGE_LAYOUT_UNDEFINED,                                                                                                      // VkImageLayout                        initialLayout;
1721                 };
1722
1723                 if (sparse && !checkSparseImageFormatSupport(context.getPhysicalDevice(), context.getInstanceInterface(), colorImageParams))
1724                         TCU_THROW(NotSupportedError, "The image format does not support sparse operations.");
1725
1726                 m_colorImage = createImage(vk, vkDevice, &colorImageParams);
1727
1728                 // Allocate and bind color image memory
1729                 if (sparse)
1730                 {
1731                         allocateAndBindSparseImage(vk, vkDevice, context.getPhysicalDevice(), context.getInstanceInterface(), colorImageParams, *m_bindSemaphore, context.getSparseQueue(), memAlloc, m_allocations, mapVkFormat(m_colorFormat), *m_colorImage);
1732                 }
1733                 else
1734                 {
1735                         m_colorImageAlloc = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_colorImage), MemoryRequirement::Any);
1736                         VK_CHECK(vk.bindImageMemory(vkDevice, *m_colorImage, m_colorImageAlloc->getMemory(), m_colorImageAlloc->getOffset()));
1737                 }
1738         }
1739
1740         // Create resolve image
1741         if (m_renderType == RENDER_TYPE_RESOLVE)
1742         {
1743                 const VkImageCreateInfo resolveImageParams =
1744                 {
1745                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                                    // VkStructureType                      sType;
1746                         DE_NULL,                                                                                                                                                // const void*                          pNext;
1747                         0u,                                                                                                                                                             // VkImageCreateFlags           flags;
1748                         VK_IMAGE_TYPE_2D,                                                                                                                               // VkImageType                          imageType;
1749                         m_colorFormat,                                                                                                                                  // VkFormat                                     format;
1750                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y(), 1u },                                 // VkExtent3D                           extent;
1751                         1u,                                                                                                                                                             // deUint32                                     mipLevels;
1752                         1u,                                                                                                                                                             // deUint32                                     arrayLayers;
1753                         VK_SAMPLE_COUNT_1_BIT,                                                                                                                  // VkSampleCountFlagBits        samples;
1754                         VK_IMAGE_TILING_OPTIMAL,                                                                                                                // VkImageTiling                        tiling;
1755                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |                 // VkImageUsageFlags            usage;
1756                                 VK_IMAGE_USAGE_TRANSFER_DST_BIT,
1757                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                              // VkSharingMode                        sharingMode;
1758                         1u,                                                                                                                                                             // deUint32                                     queueFamilyIndexCount;
1759                         queueFamilyIndices,                                                                                                                             // const deUint32*                      pQueueFamilyIndices;
1760                         VK_IMAGE_LAYOUT_UNDEFINED                                                                                                               // VkImageLayout                        initialLayout;
1761                 };
1762
1763                 m_resolveImage = createImage(vk, vkDevice, &resolveImageParams);
1764
1765                 // Allocate and bind resolve image memory
1766                 m_resolveImageAlloc = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_resolveImage), MemoryRequirement::Any);
1767                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_resolveImage, m_resolveImageAlloc->getMemory(), m_resolveImageAlloc->getOffset()));
1768
1769                 // Create resolve attachment view
1770                 {
1771                         const VkImageViewCreateInfo resolveAttachmentViewParams =
1772                         {
1773                                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
1774                                 DE_NULL,                                                                                // const void*                          pNext;
1775                                 0u,                                                                                             // VkImageViewCreateFlags       flags;
1776                                 *m_resolveImage,                                                                // VkImage                                      image;
1777                                 VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
1778                                 m_colorFormat,                                                                  // VkFormat                                     format;
1779                                 componentMappingRGBA,                                                   // VkComponentMapping           components;
1780                                 { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
1781                         };
1782
1783                         m_resolveAttachmentView = createImageView(vk, vkDevice, &resolveAttachmentViewParams);
1784                 }
1785         }
1786
1787         // Create per-sample output images
1788         if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
1789         {
1790                 const VkImageCreateInfo perSampleImageParams =
1791                 {
1792                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                                    // VkStructureType                      sType;
1793                         DE_NULL,                                                                                                                                                // const void*                          pNext;
1794                         0u,                                                                                                                                                             // VkImageCreateFlags           flags;
1795                         VK_IMAGE_TYPE_2D,                                                                                                                               // VkImageType                          imageType;
1796                         m_colorFormat,                                                                                                                                  // VkFormat                                     format;
1797                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y(), 1u },                                 // VkExtent3D                           extent;
1798                         1u,                                                                                                                                                             // deUint32                                     mipLevels;
1799                         1u,                                                                                                                                                             // deUint32                                     arrayLayers;
1800                         VK_SAMPLE_COUNT_1_BIT,                                                                                                                  // VkSampleCountFlagBits        samples;
1801                         VK_IMAGE_TILING_OPTIMAL,                                                                                                                // VkImageTiling                        tiling;
1802                         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |                 // VkImageUsageFlags            usage;
1803                         VK_IMAGE_USAGE_TRANSFER_DST_BIT,
1804                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                              // VkSharingMode                        sharingMode;
1805                         1u,                                                                                                                                                             // deUint32                                     queueFamilyIndexCount;
1806                         queueFamilyIndices,                                                                                                                             // const deUint32*                      pQueueFamilyIndices;
1807                         VK_IMAGE_LAYOUT_UNDEFINED                                                                                                               // VkImageLayout                        initialLayout;
1808                 };
1809
1810                 m_perSampleImages.resize(static_cast<size_t>(m_multisampleStateParams.rasterizationSamples));
1811
1812                 for (size_t i = 0; i < m_perSampleImages.size(); ++i)
1813                 {
1814                         m_perSampleImages[i]    = de::SharedPtr<PerSampleImage>(new PerSampleImage);
1815                         PerSampleImage& image   = *m_perSampleImages[i];
1816
1817                         image.m_image                   = createImage(vk, vkDevice, &perSampleImageParams);
1818
1819                         // Allocate and bind image memory
1820                         image.m_imageAlloc              = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *image.m_image), MemoryRequirement::Any);
1821                         VK_CHECK(vk.bindImageMemory(vkDevice, *image.m_image, image.m_imageAlloc->getMemory(), image.m_imageAlloc->getOffset()));
1822
1823                         // Create per-sample attachment view
1824                         {
1825                                 const VkImageViewCreateInfo perSampleAttachmentViewParams =
1826                                 {
1827                                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
1828                                         DE_NULL,                                                                                // const void*                          pNext;
1829                                         0u,                                                                                             // VkImageViewCreateFlags       flags;
1830                                         *image.m_image,                                                                 // VkImage                                      image;
1831                                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
1832                                         m_colorFormat,                                                                  // VkFormat                                     format;
1833                                         componentMappingRGBA,                                                   // VkComponentMapping           components;
1834                                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
1835                                 };
1836
1837                                 image.m_attachmentView = createImageView(vk, vkDevice, &perSampleAttachmentViewParams);
1838                         }
1839                 }
1840         }
1841
1842         // Create a depth/stencil image
1843         if (m_useDepth || m_useStencil)
1844         {
1845                 const VkImageCreateInfo depthStencilImageParams =
1846                 {
1847                         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                                                                                    // VkStructureType                      sType;
1848                         DE_NULL,                                                                                                                                                // const void*                          pNext;
1849                         0u,                                                                                                                                                             // VkImageCreateFlags           flags;
1850                         VK_IMAGE_TYPE_2D,                                                                                                                               // VkImageType                          imageType;
1851                         m_depthStencilFormat,                                                                                                                   // VkFormat                                     format;
1852                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y(), 1u },                                 // VkExtent3D                           extent;
1853                         1u,                                                                                                                                                             // deUint32                                     mipLevels;
1854                         1u,                                                                                                                                                             // deUint32                                     arrayLayers;
1855                         m_multisampleStateParams.rasterizationSamples,                                                                  // VkSampleCountFlagBits        samples;
1856                         VK_IMAGE_TILING_OPTIMAL,                                                                                                                // VkImageTiling                        tiling;
1857                         VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,                                                                    // VkImageUsageFlags            usage;
1858                         VK_SHARING_MODE_EXCLUSIVE,                                                                                                              // VkSharingMode                        sharingMode;
1859                         1u,                                                                                                                                                             // deUint32                                     queueFamilyIndexCount;
1860                         queueFamilyIndices,                                                                                                                             // const deUint32*                      pQueueFamilyIndices;
1861                         VK_IMAGE_LAYOUT_UNDEFINED                                                                                                               // VkImageLayout                        initialLayout;
1862                 };
1863
1864                 m_depthStencilImage = createImage(vk, vkDevice, &depthStencilImageParams);
1865
1866                 // Allocate and bind depth/stencil image memory
1867                 m_depthStencilImageAlloc = memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *m_depthStencilImage), MemoryRequirement::Any);
1868                 VK_CHECK(vk.bindImageMemory(vkDevice, *m_depthStencilImage, m_depthStencilImageAlloc->getMemory(), m_depthStencilImageAlloc->getOffset()));
1869         }
1870
1871         // Create color attachment view
1872         {
1873                 const VkImageViewCreateInfo colorAttachmentViewParams =
1874                 {
1875                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,               // VkStructureType                      sType;
1876                         DE_NULL,                                                                                // const void*                          pNext;
1877                         0u,                                                                                             // VkImageViewCreateFlags       flags;
1878                         *m_colorImage,                                                                  // VkImage                                      image;
1879                         VK_IMAGE_VIEW_TYPE_2D,                                                  // VkImageViewType                      viewType;
1880                         m_colorFormat,                                                                  // VkFormat                                     format;
1881                         componentMappingRGBA,                                                   // VkComponentMapping           components;
1882                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }   // VkImageSubresourceRange      subresourceRange;
1883                 };
1884
1885                 m_colorAttachmentView = createImageView(vk, vkDevice, &colorAttachmentViewParams);
1886         }
1887
1888         VkImageAspectFlags      depthStencilAttachmentAspect    = (VkImageAspectFlagBits)0;
1889
1890         // Create depth/stencil attachment view
1891         if (m_useDepth || m_useStencil)
1892         {
1893                 depthStencilAttachmentAspect = getImageAspectFlags(m_depthStencilFormat);
1894
1895                 const VkImageViewCreateInfo depthStencilAttachmentViewParams =
1896                 {
1897                         VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,                       // VkStructureType                      sType;
1898                         DE_NULL,                                                                                        // const void*                          pNext;
1899                         0u,                                                                                                     // VkImageViewCreateFlags       flags;
1900                         *m_depthStencilImage,                                                           // VkImage                                      image;
1901                         VK_IMAGE_VIEW_TYPE_2D,                                                          // VkImageViewType                      viewType;
1902                         m_depthStencilFormat,                                                           // VkFormat                                     format;
1903                         componentMappingRGBA,                                                           // VkComponentMapping           components;
1904                         { depthStencilAttachmentAspect, 0u, 1u, 0u, 1u }        // VkImageSubresourceRange      subresourceRange;
1905                 };
1906
1907                 m_depthStencilAttachmentView = createImageView(vk, vkDevice, &depthStencilAttachmentViewParams);
1908         }
1909
1910         // Create render pass
1911         {
1912                 std::vector<VkAttachmentDescription> attachmentDescriptions;
1913                 {
1914                         const VkAttachmentDescription colorAttachmentDescription =
1915                         {
1916                                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
1917                                 m_colorFormat,                                                                          // VkFormat                                                     format;
1918                                 m_multisampleStateParams.rasterizationSamples,          // VkSampleCountFlagBits                        samples;
1919                                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
1920                                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
1921                                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
1922                                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
1923                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
1924                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
1925                         };
1926                         attachmentDescriptions.push_back(colorAttachmentDescription);
1927                 }
1928
1929                 deUint32 resolveAttachmentIndex = VK_ATTACHMENT_UNUSED;
1930
1931                 if (m_renderType == RENDER_TYPE_RESOLVE)
1932                 {
1933                         resolveAttachmentIndex = static_cast<deUint32>(attachmentDescriptions.size());
1934
1935                         const VkAttachmentDescription resolveAttachmentDescription =
1936                         {
1937                                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
1938                                 m_colorFormat,                                                                          // VkFormat                                                     format;
1939                                 VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
1940                                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
1941                                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
1942                                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
1943                                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
1944                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
1945                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
1946                         };
1947                         attachmentDescriptions.push_back(resolveAttachmentDescription);
1948                 }
1949
1950                 deUint32 perSampleAttachmentIndex = VK_ATTACHMENT_UNUSED;
1951
1952                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
1953                 {
1954                         perSampleAttachmentIndex = static_cast<deUint32>(attachmentDescriptions.size());
1955
1956                         const VkAttachmentDescription perSampleAttachmentDescription =
1957                         {
1958                                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
1959                                 m_colorFormat,                                                                          // VkFormat                                                     format;
1960                                 VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
1961                                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
1962                                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
1963                                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
1964                                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
1965                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        initialLayout;
1966                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout                                        finalLayout;
1967                         };
1968
1969                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
1970                         {
1971                                 attachmentDescriptions.push_back(perSampleAttachmentDescription);
1972                         }
1973                 }
1974
1975                 deUint32 depthStencilAttachmentIndex = VK_ATTACHMENT_UNUSED;
1976
1977                 if (m_useDepth || m_useStencil)
1978                 {
1979                         depthStencilAttachmentIndex = static_cast<deUint32>(attachmentDescriptions.size());
1980
1981                         const VkAttachmentDescription depthStencilAttachmentDescription =
1982                         {
1983                                 0u,                                                                                                                                                                     // VkAttachmentDescriptionFlags         flags;
1984                                 m_depthStencilFormat,                                                                                                                           // VkFormat                                                     format;
1985                                 m_multisampleStateParams.rasterizationSamples,                                                                          // VkSampleCountFlagBits                        samples;
1986                                 (m_useDepth ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE),           // VkAttachmentLoadOp                           loadOp;
1987                                 (m_useDepth ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE),         // VkAttachmentStoreOp                          storeOp;
1988                                 (m_useStencil ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE),         // VkAttachmentStoreOp                          stencilLoadOp;
1989                                 (m_useStencil ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE),       // VkAttachmentStoreOp                          stencilStoreOp;
1990                                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,                                                                       // VkImageLayout                                        initialLayout;
1991                                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL                                                                        // VkImageLayout                                        finalLayout;
1992                         };
1993                         attachmentDescriptions.push_back(depthStencilAttachmentDescription);
1994                 };
1995
1996                 const VkAttachmentReference colorAttachmentReference =
1997                 {
1998                         0u,                                                                                                     // deUint32                     attachment;
1999                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
2000                 };
2001
2002                 const VkAttachmentReference inputAttachmentReference =
2003                 {
2004                         0u,                                                                                                     // deUint32                     attachment;
2005                         VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL                        // VkImageLayout        layout;
2006                 };
2007
2008                 const VkAttachmentReference resolveAttachmentReference =
2009                 {
2010                         resolveAttachmentIndex,                                                         // deUint32                     attachment;
2011                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
2012                 };
2013
2014                 std::vector<VkAttachmentReference> perSampleAttachmentReferences(m_perSampleImages.size());
2015                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2016                 {
2017                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2018                         {
2019                                 const VkAttachmentReference perSampleAttachmentReference =
2020                                 {
2021                                         perSampleAttachmentIndex + static_cast<deUint32>(i),    // deUint32                     attachment;
2022                                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                                // VkImageLayout        layout;
2023                                 };
2024                                 perSampleAttachmentReferences[i] = perSampleAttachmentReference;
2025                         }
2026                 }
2027
2028                 const VkAttachmentReference depthStencilAttachmentReference =
2029                 {
2030                         depthStencilAttachmentIndex,                                            // deUint32                     attachment;
2031                         VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL        // VkImageLayout        layout;
2032                 };
2033
2034                 std::vector<VkSubpassDescription>       subpassDescriptions;
2035                 std::vector<VkSubpassDependency>        subpassDependencies;
2036
2037                 {
2038                         const VkSubpassDescription renderSubpassDescription =
2039                         {
2040                                 0u,                                                                                                                                                             // VkSubpassDescriptionFlags    flags;
2041                                 VK_PIPELINE_BIND_POINT_GRAPHICS,                                                                                                // VkPipelineBindPoint                  pipelineBindPoint;
2042                                 0u,                                                                                                                                                             // deUint32                                             inputAttachmentCount;
2043                                 DE_NULL,                                                                                                                                                // const VkAttachmentReference* pInputAttachments;
2044                                 1u,                                                                                                                                                             // deUint32                                             colorAttachmentCount;
2045                                 &colorAttachmentReference,                                                                                                              // const VkAttachmentReference* pColorAttachments;
2046                                 (m_renderType == RENDER_TYPE_RESOLVE) ? &resolveAttachmentReference : DE_NULL,  // const VkAttachmentReference* pResolveAttachments;
2047                                 (m_useDepth || m_useStencil ? &depthStencilAttachmentReference : DE_NULL),              // const VkAttachmentReference* pDepthStencilAttachment;
2048                                 0u,                                                                                                                                                             // deUint32                                             preserveAttachmentCount;
2049                                 DE_NULL                                                                                                                                                 // const VkAttachmentReference* pPreserveAttachments;
2050                         };
2051                         subpassDescriptions.push_back(renderSubpassDescription);
2052                 }
2053
2054                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2055                 {
2056
2057                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2058                         {
2059                                 const VkSubpassDescription copySampleSubpassDescription =
2060                                 {
2061                                         0u,                                                                                                     // VkSubpassDescriptionFlags            flags;
2062                                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                        // VkPipelineBindPoint                          pipelineBindPoint;
2063                                         1u,                                                                                                     // deUint32                                                     inputAttachmentCount;
2064                                         &inputAttachmentReference,                                                      // const VkAttachmentReference*         pInputAttachments;
2065                                         1u,                                                                                                     // deUint32                                                     colorAttachmentCount;
2066                                         &perSampleAttachmentReferences[i],                                      // const VkAttachmentReference*         pColorAttachments;
2067                                         DE_NULL,                                                                                        // const VkAttachmentReference*         pResolveAttachments;
2068                                         DE_NULL,                                                                                        // const VkAttachmentReference*         pDepthStencilAttachment;
2069                                         0u,                                                                                                     // deUint32                                                     preserveAttachmentCount;
2070                                         DE_NULL                                                                                         // const VkAttachmentReference*         pPreserveAttachments;
2071                                 };
2072                                 subpassDescriptions.push_back(copySampleSubpassDescription);
2073
2074                                 const VkSubpassDependency copySampleSubpassDependency =
2075                                 {
2076                                         0u,                                                                                                     // deUint32                                                     srcSubpass
2077                                         1u + static_cast<deUint32>(i),                                          // deUint32                                                     dstSubpass
2078                                         VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,                                      // VkPipelineStageFlags                         srcStageMask
2079                                         VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,                          // VkPipelineStageFlags                         dstStageMask
2080                                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                           // VkAccessFlags                                        srcAccessMask
2081                                         VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,                            // VkAccessFlags                                        dstAccessMask
2082                                         0u,                                                                                                     // VkDependencyFlags                            dependencyFlags
2083                                 };
2084                                 subpassDependencies.push_back(copySampleSubpassDependency);
2085                         }
2086                 }
2087
2088                 const VkRenderPassCreateInfo renderPassParams =
2089                 {
2090                         VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                                      // VkStructureType                                      sType;
2091                         DE_NULL,                                                                                                        // const void*                                          pNext;
2092                         0u,                                                                                                                     // VkRenderPassCreateFlags                      flags;
2093                         (deUint32)attachmentDescriptions.size(),                                        // deUint32                                                     attachmentCount;
2094                         &attachmentDescriptions[0],                                                                     // const VkAttachmentDescription*       pAttachments;
2095                         (deUint32)subpassDescriptions.size(),                                           // deUint32                                                     subpassCount;
2096                         &subpassDescriptions[0],                                                                        // const VkSubpassDescription*          pSubpasses;
2097                         (deUint32)subpassDependencies.size(),                                           // deUint32                                                     dependencyCount;
2098                         subpassDependencies.size() != 0 ? &subpassDependencies[0] : DE_NULL
2099                 };
2100
2101                 m_renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
2102         }
2103
2104         // Create framebuffer
2105         {
2106                 std::vector<VkImageView> attachments;
2107                 attachments.push_back(*m_colorAttachmentView);
2108                 if (m_renderType == RENDER_TYPE_RESOLVE)
2109                 {
2110                         attachments.push_back(*m_resolveAttachmentView);
2111                 }
2112                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2113                 {
2114                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2115                         {
2116                                 attachments.push_back(*m_perSampleImages[i]->m_attachmentView);
2117                         }
2118                 }
2119
2120                 if (m_useDepth || m_useStencil)
2121                 {
2122                         attachments.push_back(*m_depthStencilAttachmentView);
2123                 }
2124
2125                 const VkFramebufferCreateInfo framebufferParams =
2126                 {
2127                         VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,                      // VkStructureType                                      sType;
2128                         DE_NULL,                                                                                        // const void*                                          pNext;
2129                         0u,                                                                                                     // VkFramebufferCreateFlags                     flags;
2130                         *m_renderPass,                                                                          // VkRenderPass                                         renderPass;
2131                         (deUint32)attachments.size(),                                           // deUint32                                                     attachmentCount;
2132                         &attachments[0],                                                                        // const VkImageView*                           pAttachments;
2133                         (deUint32)m_renderSize.x(),                                                     // deUint32                                                     width;
2134                         (deUint32)m_renderSize.y(),                                                     // deUint32                                                     height;
2135                         1u                                                                                                      // deUint32                                                     layers;
2136                 };
2137
2138                 m_framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
2139         }
2140
2141         // Create pipeline layout
2142         {
2143                 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
2144                 {
2145                         VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,          // VkStructureType                                      sType;
2146                         DE_NULL,                                                                                        // const void*                                          pNext;
2147                         0u,                                                                                                     // VkPipelineLayoutCreateFlags          flags;
2148                         0u,                                                                                                     // deUint32                                                     setLayoutCount;
2149                         DE_NULL,                                                                                        // const VkDescriptorSetLayout*         pSetLayouts;
2150                         0u,                                                                                                     // deUint32                                                     pushConstantRangeCount;
2151                         DE_NULL                                                                                         // const VkPushConstantRange*           pPushConstantRanges;
2152                 };
2153
2154                 m_pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
2155
2156                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2157                 {
2158
2159                         // Create descriptor set layout
2160                         const VkDescriptorSetLayoutBinding              layoutBinding                                   =
2161                         {
2162                                 0u,                                                                                                                     // deUint32                                                             binding;
2163                                 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,                                            // VkDescriptorType                                             descriptorType;
2164                                 1u,                                                                                                                     // deUint32                                                             descriptorCount;
2165                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlags                                   stageFlags;
2166                                 DE_NULL,                                                                                                        // const VkSampler*                                             pImmutableSamplers;
2167                         };
2168
2169                         const VkDescriptorSetLayoutCreateInfo   descriptorSetLayoutParams               =
2170                         {
2171                                 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,            // VkStructureType                                              sType
2172                                 DE_NULL,                                                                                                        // const void*                                                  pNext
2173                                 0u,                                                                                                                     // VkDescriptorSetLayoutCreateFlags             flags
2174                                 1u,                                                                                                                     // deUint32                                                             bindingCount
2175                                 &layoutBinding                                                                                          // const VkDescriptorSetLayoutBinding*  pBindings
2176                         };
2177                         m_copySampleDesciptorLayout     = createDescriptorSetLayout(vk, vkDevice, &descriptorSetLayoutParams);
2178
2179                         // Create pipeline layout
2180
2181                         const VkPushConstantRange                               pushConstantRange                               =
2182                         {
2183                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlags                                   stageFlags;
2184                                 0u,                                                                                                                     // deUint32                                                             offset;
2185                                 sizeof(deInt32)                                                                                         // deUint32                                                             size;
2186                         };
2187                         const VkPipelineLayoutCreateInfo                copySamplePipelineLayoutParams  =
2188                         {
2189                                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,                          // VkStructureType                                              sType;
2190                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
2191                                 0u,                                                                                                                     // VkPipelineLayoutCreateFlags                  flags;
2192                                 1u,                                                                                                                     // deUint32                                                             setLayoutCount;
2193                                 &m_copySampleDesciptorLayout.get(),                                                     // const VkDescriptorSetLayout*                 pSetLayouts;
2194                                 1u,                                                                                                                     // deUint32                                                             pushConstantRangeCount;
2195                                 &pushConstantRange                                                                                      // const VkPushConstantRange*                   pPushConstantRanges;
2196                         };
2197                         m_copySamplePipelineLayout              = createPipelineLayout(vk, vkDevice, &copySamplePipelineLayoutParams);
2198                 }
2199         }
2200
2201         m_vertexShaderModule    = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_vert"), 0);
2202         m_fragmentShaderModule  = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("color_frag"), 0);
2203
2204         if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2205         {
2206                 m_copySampleVertexShaderModule          = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("quad_vert"), 0);
2207                 m_copySampleFragmentShaderModule        = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get("copy_sample_frag"), 0);
2208         }
2209
2210         // Create pipeline
2211         {
2212                 const VkPipelineShaderStageCreateInfo   shaderStageParams[2] =
2213                 {
2214                         {
2215                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
2216                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
2217                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
2218                                 VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                stage;
2219                                 *m_vertexShaderModule,                                                                          // VkShaderModule                                               module;
2220                                 "main",                                                                                                         // const char*                                                  pName;
2221                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
2222                         },
2223                         {
2224                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
2225                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
2226                                 0u,                                                                                                                     // VkPipelineShaderStageCreateFlags             flags;
2227                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                stage;
2228                                 *m_fragmentShaderModule,                                                                        // VkShaderModule                                               module;
2229                                 "main",                                                                                                         // const char*                                                  pName;
2230                                 DE_NULL                                                                                                         // const VkSpecializationInfo*                  pSpecializationInfo;
2231                         }
2232                 };
2233
2234                 const VkVertexInputBindingDescription   vertexInputBindingDescription =
2235                 {
2236                         0u,                                                                     // deUint32                             binding;
2237                         sizeof(Vertex4RGBA),                            // deUint32                             stride;
2238                         VK_VERTEX_INPUT_RATE_VERTEX                     // VkVertexInputRate    inputRate;
2239                 };
2240
2241                 const VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
2242                 {
2243                         {
2244                                 0u,                                                                     // deUint32     location;
2245                                 0u,                                                                     // deUint32     binding;
2246                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
2247                                 0u                                                                      // deUint32     offset;
2248                         },
2249                         {
2250                                 1u,                                                                     // deUint32     location;
2251                                 0u,                                                                     // deUint32     binding;
2252                                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat     format;
2253                                 DE_OFFSET_OF(Vertex4RGBA, color),       // deUint32     offset;
2254                         }
2255                 };
2256
2257                 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
2258                 {
2259                         VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
2260                         DE_NULL,                                                                                                                // const void*                                                          pNext;
2261                         0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
2262                         1u,                                                                                                                             // deUint32                                                                     vertexBindingDescriptionCount;
2263                         &vertexInputBindingDescription,                                                                 // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
2264                         2u,                                                                                                                             // deUint32                                                                     vertexAttributeDescriptionCount;
2265                         vertexInputAttributeDescriptions                                                                // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
2266                 };
2267
2268                 // Topology is set before the pipeline creation.
2269                 VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
2270                 {
2271                         VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
2272                         DE_NULL,                                                                                                                // const void*                                                          pNext;
2273                         0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
2274                         VK_PRIMITIVE_TOPOLOGY_LAST,                                                                             // VkPrimitiveTopology                                          topology;
2275                         false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
2276                 };
2277
2278                 const VkViewport viewport =
2279                 {
2280                         0.0f,                                           // float        x;
2281                         0.0f,                                           // float        y;
2282                         (float)m_renderSize.x(),        // float        width;
2283                         (float)m_renderSize.y(),        // float        height;
2284                         0.0f,                                           // float        minDepth;
2285                         1.0f                                            // float        maxDepth;
2286                 };
2287
2288                 const VkRect2D scissor =
2289                 {
2290                         { 0, 0 },                                                                                                       // VkOffset2D  offset;
2291                         { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y() }      // VkExtent2D  extent;
2292                 };
2293
2294                 const VkPipelineViewportStateCreateInfo viewportStateParams =
2295                 {
2296                         VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                              sType;
2297                         DE_NULL,                                                                                                                // const void*                                                  pNext;
2298                         0u,                                                                                                                             // VkPipelineViewportStateCreateFlags   flags;
2299                         1u,                                                                                                                             // deUint32                                                             viewportCount;
2300                         &viewport,                                                                                                              // const VkViewport*                                    pViewports;
2301                         1u,                                                                                                                             // deUint32                                                             scissorCount;
2302                         &scissor                                                                                                                // const VkRect2D*                                              pScissors;
2303                 };
2304
2305                 const VkPipelineRasterizationStateCreateInfo rasterStateParams =
2306                 {
2307                         VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
2308                         DE_NULL,                                                                                                                // const void*                                                          pNext;
2309                         0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
2310                         false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
2311                         false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
2312                         VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
2313                         VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
2314                         VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
2315                         VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
2316                         0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
2317                         0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
2318                         0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
2319                         1.0f                                                                                                                    // float                                                                        lineWidth;
2320                 };
2321
2322                 const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
2323                 {
2324                         VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
2325                         DE_NULL,                                                                                                        // const void*                                                                  pNext;
2326                         0u,                                                                                                                     // VkPipelineColorBlendStateCreateFlags                 flags;
2327                         false,                                                                                                          // VkBool32                                                                             logicOpEnable;
2328                         VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
2329                         1u,                                                                                                                     // deUint32                                                                             attachmentCount;
2330                         &m_colorBlendState,                                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
2331                         { 0.0f, 0.0f, 0.0f, 0.0f }                                                                      // float                                                                                blendConstants[4];
2332                 };
2333
2334                 const VkStencilOpState stencilOpState =
2335                 {
2336                         VK_STENCIL_OP_KEEP,                                             // VkStencilOp  failOp;
2337                         VK_STENCIL_OP_REPLACE,                                  // VkStencilOp  passOp;
2338                         VK_STENCIL_OP_KEEP,                                             // VkStencilOp  depthFailOp;
2339                         VK_COMPARE_OP_GREATER,                                  // VkCompareOp  compareOp;
2340                         1u,                                                                             // deUint32             compareMask;
2341                         1u,                                                                             // deUint32             writeMask;
2342                         1u,                                                                             // deUint32             reference;
2343                 };
2344
2345                 const VkPipelineDepthStencilStateCreateInfo depthStencilStateParams =
2346                 {
2347                         VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // VkStructureType                                                      sType;
2348                         DE_NULL,                                                                                                        // const void*                                                          pNext;
2349                         0u,                                                                                                                     // VkPipelineDepthStencilStateCreateFlags       flags;
2350                         m_useDepth,                                                                                                     // VkBool32                                                                     depthTestEnable;
2351                         m_useDepth,                                                                                                     // VkBool32                                                                     depthWriteEnable;
2352                         VK_COMPARE_OP_LESS,                                                                                     // VkCompareOp                                                          depthCompareOp;
2353                         false,                                                                                                          // VkBool32                                                                     depthBoundsTestEnable;
2354                         m_useStencil,                                                                                           // VkBool32                                                                     stencilTestEnable;
2355                         stencilOpState,                                                                                         // VkStencilOpState     front;
2356                         stencilOpState,                                                                                         // VkStencilOpState     back;
2357                         0.0f,                                                                                                           // float                        minDepthBounds;
2358                         1.0f,                                                                                                           // float                        maxDepthBounds;
2359                 };
2360
2361                 const VkGraphicsPipelineCreateInfo graphicsPipelineParams =
2362                 {
2363                         VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
2364                         DE_NULL,                                                                                        // const void*                                                                          pNext;
2365                         0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
2366                         2u,                                                                                                     // deUint32                                                                                     stageCount;
2367                         shaderStageParams,                                                                      // const VkPipelineShaderStageCreateInfo*                       pStages;
2368                         &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
2369                         &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
2370                         DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
2371                         &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
2372                         &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
2373                         &m_multisampleStateParams,                                                      // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
2374                         &depthStencilStateParams,                                                       // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
2375                         &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
2376                         (const VkPipelineDynamicStateCreateInfo*)DE_NULL,       // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
2377                         *m_pipelineLayout,                                                                      // VkPipelineLayout                                                                     layout;
2378                         *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
2379                         0u,                                                                                                     // deUint32                                                                                     subpass;
2380                         0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
2381                         0u                                                                                                      // deInt32                                                                                      basePipelineIndex;
2382                 };
2383
2384                 for (deUint32 i = 0u; i < numTopologies; ++i)
2385                 {
2386                         inputAssemblyStateParams.topology = pTopology[i];
2387                         m_graphicsPipelines.push_back(VkPipelineSp(new Unique<VkPipeline>(createGraphicsPipeline(vk, vkDevice, DE_NULL, &graphicsPipelineParams))));
2388                 }
2389         }
2390
2391         if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2392         {
2393                 // Create pipelines for copying samples to single sampled images
2394                 {
2395                         const VkPipelineShaderStageCreateInfo shaderStageParams[2] =
2396                         {
2397                                 {
2398                                         VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                                      sType;
2399                                         DE_NULL,                                                                                                        // const void*                                                          pNext;
2400                                         0u,                                                                                                                     // VkPipelineShaderStageCreateFlags                     flags;
2401                                         VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                        stage;
2402                                         *m_copySampleVertexShaderModule,                                                        // VkShaderModule                                                       module;
2403                                         "main",                                                                                                         // const char*                                                          pName;
2404                                         DE_NULL                                                                                                         // const VkSpecializationInfo*                          pSpecializationInfo;
2405                                 },
2406                                 {
2407                                         VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                                      sType;
2408                                         DE_NULL,                                                                                                        // const void*                                                          pNext;
2409                                         0u,                                                                                                                     // VkPipelineShaderStageCreateFlags                     flags;
2410                                         VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                        stage;
2411                                         *m_copySampleFragmentShaderModule,                                                      // VkShaderModule                                                       module;
2412                                         "main",                                                                                                         // const char*                                                          pName;
2413                                         DE_NULL                                                                                                         // const VkSpecializationInfo*                          pSpecializationInfo;
2414                                 }
2415                         };
2416
2417                         const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
2418                         {
2419                                 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                                                      sType;
2420                                 DE_NULL,                                                                                                                // const void*                                                          pNext;
2421                                 0u,                                                                                                                             // VkPipelineVertexInputStateCreateFlags        flags;
2422                                 0u,                                                                                                                             // deUint32                                                                     vertexBindingDescriptionCount;
2423                                 DE_NULL,                                                                                                                // const VkVertexInputBindingDescription*       pVertexBindingDescriptions;
2424                                 0u,                                                                                                                             // deUint32                                                                     vertexAttributeDescriptionCount;
2425                                 DE_NULL                                                                                                                 // const VkVertexInputAttributeDescription*     pVertexAttributeDescriptions;
2426                         };
2427
2428                         // Topology is set before the pipeline creation.
2429                         VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateParams =
2430                         {
2431                                 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                                                      sType;
2432                                 DE_NULL,                                                                                                                // const void*                                                          pNext;
2433                                 0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags      flags;
2434                                 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,                                                   // VkPrimitiveTopology                                          topology;
2435                                 false                                                                                                                   // VkBool32                                                                     primitiveRestartEnable;
2436                         };
2437
2438                         const VkViewport viewport =
2439                         {
2440                                 0.0f,                                           // float        x;
2441                                 0.0f,                                           // float        y;
2442                                 (float)m_renderSize.x(),        // float        width;
2443                                 (float)m_renderSize.y(),        // float        height;
2444                                 0.0f,                                           // float        minDepth;
2445                                 1.0f                                            // float        maxDepth;
2446                         };
2447
2448                         const VkRect2D scissor =
2449                         {
2450                                 { 0, 0 },                                                                                                       // VkOffset2D  offset;
2451                                 { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y() }      // VkExtent2D  extent;
2452                         };
2453
2454                         const VkPipelineViewportStateCreateInfo viewportStateParams =
2455                         {
2456                                 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                                                      sType;
2457                                 DE_NULL,                                                                                                                // const void*                                                          pNext;
2458                                 0u,                                                                                                                             // VkPipelineViewportStateCreateFlags           flags;
2459                                 1u,                                                                                                                             // deUint32                                                                     viewportCount;
2460                                 &viewport,                                                                                                              // const VkViewport*                                            pViewports;
2461                                 1u,                                                                                                                             // deUint32                                                                     scissorCount;
2462                                 &scissor                                                                                                                // const VkRect2D*                                                      pScissors;
2463                         };
2464
2465                         const VkPipelineRasterizationStateCreateInfo rasterStateParams =
2466                         {
2467                                 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
2468                                 DE_NULL,                                                                                                                // const void*                                                          pNext;
2469                                 0u,                                                                                                                             // VkPipelineRasterizationStateCreateFlags      flags;
2470                                 false,                                                                                                                  // VkBool32                                                                     depthClampEnable;
2471                                 false,                                                                                                                  // VkBool32                                                                     rasterizerDiscardEnable;
2472                                 VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
2473                                 VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
2474                                 VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
2475                                 VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
2476                                 0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
2477                                 0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
2478                                 0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
2479                                 1.0f                                                                                                                    // float                                                                        lineWidth;
2480                         };
2481
2482                         const VkPipelineColorBlendStateCreateInfo colorBlendStateParams =
2483                         {
2484                                 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,       // VkStructureType                                                              sType;
2485                                 DE_NULL,                                                                                                        // const void*                                                                  pNext;
2486                                 0u,                                                                                                                     // VkPipelineColorBlendStateCreateFlags                 flags;
2487                                 false,                                                                                                          // VkBool32                                                                             logicOpEnable;
2488                                 VK_LOGIC_OP_COPY,                                                                                       // VkLogicOp                                                                    logicOp;
2489                                 1u,                                                                                                                     // deUint32                                                                             attachmentCount;
2490                                 &m_colorBlendState,                                                                                     // const VkPipelineColorBlendAttachmentState*   pAttachments;
2491                                 { 0.0f, 0.0f, 0.0f, 0.0f }                                                                      // float                                                                                blendConstants[4];
2492                         };
2493
2494                         const  VkPipelineMultisampleStateCreateInfo multisampleStateParams =
2495                         {
2496                                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,       // VkStructureType                                                      sType
2497                                 DE_NULL,                                                                                                        // const void*                                                          pNext
2498                                 0u,                                                                                                                     // VkPipelineMultisampleStateCreateFlags        flags
2499                                 VK_SAMPLE_COUNT_1_BIT,                                                                          // VkSampleCountFlagBits                                        rasterizationSamples
2500                                 VK_FALSE,                                                                                                       // VkBool32                                                                     sampleShadingEnable
2501                                 0.0f,                                                                                                           // float                                                                        minSampleShading
2502                                 DE_NULL,                                                                                                        // const VkSampleMask*                                          pSampleMask
2503                                 VK_FALSE,                                                                                                       // VkBool32                                                                     alphaToCoverageEnable
2504                                 VK_FALSE,                                                                                                       // VkBool32                                                                     alphaToOneEnable
2505                         };
2506
2507                         const VkGraphicsPipelineCreateInfo graphicsPipelineTemplate =
2508                         {
2509                                 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
2510                                 DE_NULL,                                                                                        // const void*                                                                          pNext;
2511                                 0u,                                                                                                     // VkPipelineCreateFlags                                                        flags;
2512                                 2u,                                                                                                     // deUint32                                                                                     stageCount;
2513                                 shaderStageParams,                                                                      // const VkPipelineShaderStageCreateInfo*                       pStages;
2514                                 &vertexInputStateParams,                                                        // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
2515                                 &inputAssemblyStateParams,                                                      // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
2516                                 DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
2517                                 &viewportStateParams,                                                           // const VkPipelineViewportStateCreateInfo*                     pViewportState;
2518                                 &rasterStateParams,                                                                     // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
2519                                 &multisampleStateParams,                                                        // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
2520                                 DE_NULL,                                                                                        // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
2521                                 &colorBlendStateParams,                                                         // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
2522                                 (const VkPipelineDynamicStateCreateInfo*)DE_NULL,       // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
2523                                 *m_copySamplePipelineLayout,                                            // VkPipelineLayout                                                                     layout;
2524                                 *m_renderPass,                                                                          // VkRenderPass                                                                         renderPass;
2525                                 0u,                                                                                                     // deUint32                                                                                     subpass;
2526                                 0u,                                                                                                     // VkPipeline                                                                           basePipelineHandle;
2527                                 0u                                                                                                      // deInt32                                                                                      basePipelineIndex;
2528                         };
2529
2530                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2531                         {
2532                                 VkGraphicsPipelineCreateInfo graphicsPipelineParams = graphicsPipelineTemplate;
2533
2534                                 // Pipeline is to be used in subpasses subsequent to sample-shading subpass
2535                                 graphicsPipelineParams.subpass = 1u + (deUint32)i;
2536
2537                                 m_copySamplePipelines.push_back(VkPipelineSp(new Unique<VkPipeline>(createGraphicsPipeline(vk, vkDevice, DE_NULL, &graphicsPipelineParams))));
2538                         }
2539                 }
2540
2541
2542                 const VkDescriptorPoolSize                      descriptorPoolSize                      =
2543                 {
2544                         VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,                                    // VkDescriptorType                                     type;
2545                         1u                                                                                                              // deUint32                                                     descriptorCount;
2546                 };
2547
2548                 const VkDescriptorPoolCreateInfo        descriptorPoolCreateInfo        =
2549                 {
2550                         VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,                  // VkStructureType                                      sType
2551                         DE_NULL,                                                                                                // const void*                                          pNext
2552                         VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,              // VkDescriptorPoolCreateFlags          flags
2553                         1u,                                                                                                     // deUint32                                                     maxSets
2554                         1u,                                                                                                             // deUint32                                                     poolSizeCount
2555                         &descriptorPoolSize                                                                             // const VkDescriptorPoolSize*          pPoolSizes
2556                 };
2557
2558                 m_copySampleDesciptorPool = createDescriptorPool(vk, vkDevice, &descriptorPoolCreateInfo);
2559
2560                 const VkDescriptorSetAllocateInfo       descriptorSetAllocateInfo       =
2561                 {
2562                         VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,                 // VkStructureType                                      sType
2563                         DE_NULL,                                                                                                // const void*                                          pNext
2564                         *m_copySampleDesciptorPool,                                                             // VkDescriptorPool                                     descriptorPool
2565                         1u,                                                                                                             // deUint32                                                     descriptorSetCount
2566                         &m_copySampleDesciptorLayout.get(),                                             // const VkDescriptorSetLayout*         pSetLayouts
2567                 };
2568
2569                 m_copySampleDesciptorSet = allocateDescriptorSet(vk, vkDevice, &descriptorSetAllocateInfo);
2570
2571                 const VkDescriptorImageInfo                     imageInfo                                       =
2572                 {
2573                         DE_NULL,
2574                         *m_colorAttachmentView,
2575                         VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
2576                 };
2577                 const VkWriteDescriptorSet                      descriptorWrite                         =
2578                 {
2579                         VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,                 // VkStructureType                                      sType;
2580                         DE_NULL,                                                                                // const void*                                          pNext;
2581                         *m_copySampleDesciptorSet,                                              // VkDescriptorSet                                      dstSet;
2582                         0u,                                                                                             // deUint32                                                     dstBinding;
2583                         0u,                                                                                             // deUint32                                                     dstArrayElement;
2584                         1u,                                                                                             // deUint32                                                     descriptorCount;
2585                         VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,                    // VkDescriptorType                                     descriptorType;
2586                         &imageInfo,                                                                             // const VkDescriptorImageInfo*         pImageInfo;
2587                         DE_NULL,                                                                                // const VkDescriptorBufferInfo*        pBufferInfo;
2588                         DE_NULL,                                                                                // const VkBufferView*                          pTexelBufferView;
2589                 };
2590                 vk.updateDescriptorSets(vkDevice, 1u, &descriptorWrite, 0u, DE_NULL);
2591         }
2592
2593         // Create vertex buffer
2594         {
2595                 const VkBufferCreateInfo vertexBufferParams =
2596                 {
2597                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           // VkStructureType              sType;
2598                         DE_NULL,                                                                        // const void*                  pNext;
2599                         0u,                                                                                     // VkBufferCreateFlags  flags;
2600                         1024u,                                                                          // VkDeviceSize                 size;
2601                         VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,                      // VkBufferUsageFlags   usage;
2602                         VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                sharingMode;
2603                         1u,                                                                                     // deUint32                             queueFamilyIndexCount;
2604                         &queueFamilyIndices[0]                                          // const deUint32*              pQueueFamilyIndices;
2605                 };
2606
2607                 m_vertexBuffer          = createBuffer(vk, vkDevice, &vertexBufferParams);
2608                 m_vertexBufferAlloc     = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *m_vertexBuffer), MemoryRequirement::HostVisible);
2609
2610                 VK_CHECK(vk.bindBufferMemory(vkDevice, *m_vertexBuffer, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset()));
2611
2612                 // Load vertices into vertex buffer
2613                 {
2614                         Vertex4RGBA* pDst = static_cast<Vertex4RGBA*>(m_vertexBufferAlloc->getHostPtr());
2615                         for (deUint32 i = 0u; i < numTopologies; ++i)
2616                         {
2617                                 deMemcpy(pDst, &pVertices[i][0], pVertices[i].size() * sizeof(Vertex4RGBA));
2618                                 pDst += pVertices[i].size();
2619                         }
2620                 }
2621                 flushMappedMemoryRange(vk, vkDevice, m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferParams.size);
2622         }
2623
2624         // Create command pool
2625         m_cmdPool = createCommandPool(vk, vkDevice, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndices[0]);
2626
2627         // Create command buffer
2628         {
2629                 const VkCommandBufferBeginInfo cmdBufferBeginInfo =
2630                 {
2631                         VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,    // VkStructureType                                      sType;
2632                         DE_NULL,                                                                                // const void*                                          pNext;
2633                         0u,                                                                                             // VkCommandBufferUsageFlags            flags;
2634                         (const VkCommandBufferInheritanceInfo*)DE_NULL,
2635                 };
2636
2637                 VkClearValue colorClearValue;
2638                 colorClearValue.color.float32[0] = 0.0f;
2639                 colorClearValue.color.float32[1] = 0.0f;
2640                 colorClearValue.color.float32[2] = 0.0f;
2641                 colorClearValue.color.float32[3] = 0.0f;
2642
2643                 VkClearValue depthStencilClearValue;
2644                 depthStencilClearValue.depthStencil.depth = 1.0f;
2645                 depthStencilClearValue.depthStencil.stencil = 0u;
2646
2647                 std::vector<VkClearValue> clearValues;
2648                 clearValues.push_back(colorClearValue);
2649                 if (m_renderType == RENDER_TYPE_RESOLVE)
2650                 {
2651                         clearValues.push_back(colorClearValue);
2652                 }
2653                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2654                 {
2655                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2656                         {
2657                                 clearValues.push_back(colorClearValue);
2658                         }
2659                 }
2660                 if (m_useDepth || m_useStencil)
2661                 {
2662                         clearValues.push_back(depthStencilClearValue);
2663                 }
2664
2665                 const VkRenderPassBeginInfo renderPassBeginInfo =
2666                 {
2667                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
2668                         DE_NULL,                                                                                                // const void*                  pNext;
2669                         *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
2670                         *m_framebuffer,                                                                                 // VkFramebuffer                framebuffer;
2671                         {
2672                                 { 0, 0 },
2673                                 { (deUint32)m_renderSize.x(), (deUint32)m_renderSize.y() }
2674                         },                                                                                                              // VkRect2D                             renderArea;
2675                         (deUint32)clearValues.size(),                                                   // deUint32                             clearValueCount;
2676                         &clearValues[0]                                                                                 // const VkClearValue*  pClearValues;
2677                 };
2678
2679                 std::vector<VkImageMemoryBarrier> imageLayoutBarriers;
2680
2681                 {
2682                         const VkImageMemoryBarrier colorImageBarrier =
2683                                 // color attachment image
2684                         {
2685                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // VkStructureType                      sType;
2686                                 DE_NULL,                                                                                // const void*                          pNext;
2687                                 0u,                                                                                             // VkAccessFlags                        srcAccessMask;
2688                                 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                   // VkAccessFlags                        dstAccessMask;
2689                                 VK_IMAGE_LAYOUT_UNDEFINED,                                              // VkImageLayout                        oldLayout;
2690                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               // VkImageLayout                        newLayout;
2691                                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     srcQueueFamilyIndex;
2692                                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     dstQueueFamilyIndex;
2693                                 *m_colorImage,                                                                  // VkImage                                      image;
2694                                 { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u },  // VkImageSubresourceRange      subresourceRange;
2695                         };
2696                         imageLayoutBarriers.push_back(colorImageBarrier);
2697                 }
2698                 if (m_renderType == RENDER_TYPE_RESOLVE)
2699                 {
2700                         const VkImageMemoryBarrier resolveImageBarrier =
2701                         // resolve attachment image
2702                         {
2703                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // VkStructureType                      sType;
2704                                 DE_NULL,                                                                                // const void*                          pNext;
2705                                 0u,                                                                                             // VkAccessFlags                        srcAccessMask;
2706                                 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                   // VkAccessFlags                        dstAccessMask;
2707                                 VK_IMAGE_LAYOUT_UNDEFINED,                                              // VkImageLayout                        oldLayout;
2708                                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               // VkImageLayout                        newLayout;
2709                                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     srcQueueFamilyIndex;
2710                                 VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     dstQueueFamilyIndex;
2711                                 *m_resolveImage,                                                                // VkImage                                      image;
2712                                 { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u },  // VkImageSubresourceRange      subresourceRange;
2713                         };
2714                         imageLayoutBarriers.push_back(resolveImageBarrier);
2715                 }
2716                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2717                 {
2718                         for (size_t i = 0; i < m_perSampleImages.size(); ++i)
2719                         {
2720                                 const VkImageMemoryBarrier perSampleImageBarrier =
2721                                 // resolve attachment image
2722                                 {
2723                                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // VkStructureType                      sType;
2724                                         DE_NULL,                                                                                // const void*                          pNext;
2725                                         0u,                                                                                             // VkAccessFlags                        srcAccessMask;
2726                                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                   // VkAccessFlags                        dstAccessMask;
2727                                         VK_IMAGE_LAYOUT_UNDEFINED,                                              // VkImageLayout                        oldLayout;
2728                                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,               // VkImageLayout                        newLayout;
2729                                         VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     srcQueueFamilyIndex;
2730                                         VK_QUEUE_FAMILY_IGNORED,                                                // deUint32                                     dstQueueFamilyIndex;
2731                                         *m_perSampleImages[i]->m_image,                                 // VkImage                                      image;
2732                                         { VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u },  // VkImageSubresourceRange      subresourceRange;
2733                                 };
2734                                 imageLayoutBarriers.push_back(perSampleImageBarrier);
2735                         }
2736                 }
2737                 if (m_useDepth || m_useStencil)
2738                 {
2739                         const VkImageMemoryBarrier depthStencilImageBarrier =
2740                         // depth/stencil attachment image
2741                         {
2742                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                         // VkStructureType                      sType;
2743                                 DE_NULL,                                                                                        // const void*                          pNext;
2744                                 0u,                                                                                                     // VkAccessFlags                        srcAccessMask;
2745                                 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,           // VkAccessFlags                        dstAccessMask;
2746                                 VK_IMAGE_LAYOUT_UNDEFINED,                                                      // VkImageLayout                        oldLayout;
2747                                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,       // VkImageLayout                        newLayout;
2748                                 VK_QUEUE_FAMILY_IGNORED,                                                        // deUint32                                     srcQueueFamilyIndex;
2749                                 VK_QUEUE_FAMILY_IGNORED,                                                        // deUint32                                     dstQueueFamilyIndex;
2750                                 *m_depthStencilImage,                                                           // VkImage                                      image;
2751                                 { depthStencilAttachmentAspect, 0u, 1u, 0u, 1u },       // VkImageSubresourceRange      subresourceRange;
2752                         };
2753                         imageLayoutBarriers.push_back(depthStencilImageBarrier);
2754                 };
2755
2756                 m_cmdBuffer = allocateCommandBuffer(vk, vkDevice, *m_cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2757
2758                 VK_CHECK(vk.beginCommandBuffer(*m_cmdBuffer, &cmdBufferBeginInfo));
2759
2760                 vk.cmdPipelineBarrier(*m_cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, (VkDependencyFlags)0,
2761                         0u, DE_NULL, 0u, DE_NULL, (deUint32)imageLayoutBarriers.size(), &imageLayoutBarriers[0]);
2762
2763                 vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
2764
2765                 VkDeviceSize vertexBufferOffset = 0u;
2766
2767                 for (deUint32 i = 0u; i < numTopologies; ++i)
2768                 {
2769                         vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, **m_graphicsPipelines[i]);
2770                         vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &m_vertexBuffer.get(), &vertexBufferOffset);
2771                         vk.cmdDraw(*m_cmdBuffer, (deUint32)pVertices[i].size(), 1, 0, 0);
2772
2773                         vertexBufferOffset += static_cast<VkDeviceSize>(pVertices[i].size() * sizeof(Vertex4RGBA));
2774                 }
2775
2776                 if (m_renderType == RENDER_TYPE_COPY_SAMPLES)
2777                 {
2778                         // Copy each sample id to single sampled image
2779                         for (deInt32 sampleId = 0; sampleId < (deInt32)m_perSampleImages.size(); ++sampleId)
2780                         {
2781                                 vk.cmdNextSubpass(*m_cmdBuffer, VK_SUBPASS_CONTENTS_INLINE);
2782                                 vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, **m_copySamplePipelines[sampleId]);
2783                                 vk.cmdBindDescriptorSets(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_copySamplePipelineLayout, 0u, 1u, &m_copySampleDesciptorSet.get(), 0u, DE_NULL);
2784                                 vk.cmdPushConstants(*m_cmdBuffer, *m_copySamplePipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(deInt32), &sampleId);
2785                                 vk.cmdDraw(*m_cmdBuffer, 4, 1, 0, 0);
2786                         }
2787                 }
2788
2789                 vk.cmdEndRenderPass(*m_cmdBuffer);
2790
2791                 VK_CHECK(vk.endCommandBuffer(*m_cmdBuffer));
2792         }
2793
2794         // Create fence
2795         m_fence = createFence(vk, vkDevice);
2796 }
2797
2798 MultisampleRenderer::~MultisampleRenderer (void)
2799 {
2800 }
2801
2802 de::MovePtr<tcu::TextureLevel> MultisampleRenderer::render (void)
2803 {
2804         const DeviceInterface&          vk                                      = m_context.getDeviceInterface();
2805         const VkDevice                          vkDevice                        = m_context.getDevice();
2806         const VkQueue                           queue                           = m_context.getUniversalQueue();
2807         const deUint32                          queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
2808         const VkSubmitInfo                      submitInfo      =
2809         {
2810                 VK_STRUCTURE_TYPE_SUBMIT_INFO,  // VkStructureType                      sType;
2811                 DE_NULL,                                                // const void*                          pNext;
2812                 0u,                                                             // deUint32                                     waitSemaphoreCount;
2813                 DE_NULL,                                                // const VkSemaphore*           pWaitSemaphores;
2814                 (const VkPipelineStageFlags*)DE_NULL,
2815                 1u,                                                             // deUint32                                     commandBufferCount;
2816                 &m_cmdBuffer.get(),                             // const VkCommandBuffer*       pCommandBuffers;
2817                 0u,                                                             // deUint32                                     signalSemaphoreCount;
2818                 DE_NULL                                                 // const VkSemaphore*           pSignalSemaphores;
2819         };
2820
2821         VK_CHECK(vk.resetFences(vkDevice, 1, &m_fence.get()));
2822         VK_CHECK(vk.queueSubmit(queue, 1, &submitInfo, *m_fence));
2823         VK_CHECK(vk.waitForFences(vkDevice, 1, &m_fence.get(), true, ~(0ull) /* infinity*/));
2824
2825         if (m_renderType == RENDER_TYPE_RESOLVE)
2826         {
2827                 return readColorAttachment(vk, vkDevice, queue, queueFamilyIndex, m_context.getDefaultAllocator(), *m_resolveImage, m_colorFormat, m_renderSize.cast<deUint32>());
2828         }
2829         else
2830         {
2831                 return de::MovePtr<tcu::TextureLevel>();
2832         }
2833 }
2834
2835 de::MovePtr<tcu::TextureLevel> MultisampleRenderer::getSingleSampledImage (deUint32 sampleId)
2836 {
2837         return readColorAttachment(m_context.getDeviceInterface(), m_context.getDevice(), m_context.getUniversalQueue(), m_context.getUniversalQueueFamilyIndex(), m_context.getDefaultAllocator(), *m_perSampleImages[sampleId]->m_image, m_colorFormat, m_renderSize.cast<deUint32>());
2838 }
2839
2840 } // anonymous
2841
2842 tcu::TestCaseGroup* createMultisampleTests (tcu::TestContext& testCtx)
2843 {
2844         const VkSampleCountFlagBits samples[] =
2845         {
2846                 VK_SAMPLE_COUNT_2_BIT,
2847                 VK_SAMPLE_COUNT_4_BIT,
2848                 VK_SAMPLE_COUNT_8_BIT,
2849                 VK_SAMPLE_COUNT_16_BIT,
2850                 VK_SAMPLE_COUNT_32_BIT,
2851                 VK_SAMPLE_COUNT_64_BIT
2852         };
2853
2854         de::MovePtr<tcu::TestCaseGroup> multisampleTests (new tcu::TestCaseGroup(testCtx, "multisample", ""));
2855
2856         // Rasterization samples tests
2857         {
2858                 de::MovePtr<tcu::TestCaseGroup> rasterizationSamplesTests(new tcu::TestCaseGroup(testCtx, "raster_samples", ""));
2859
2860                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
2861                 {
2862                         std::ostringstream caseName;
2863                         caseName << "samples_" << samples[samplesNdx];
2864
2865                         de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
2866
2867                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_triangle", "",  samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR));
2868                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_line", "",              samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_REGULAR));
2869                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_point", "",             samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_REGULAR));
2870
2871                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "depth", "",                       samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR, TEST_MODE_DEPTH_BIT));
2872                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "stencil", "",                     samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR, TEST_MODE_STENCIL_BIT));
2873                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "depth_stencil", "",       samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR, TEST_MODE_DEPTH_BIT | TEST_MODE_STENCIL_BIT));
2874
2875                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_triangle_sparse", "",   samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE));
2876                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_line_sparse", "",               samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_SPARSE));
2877                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "primitive_point_sparse", "",              samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_SPARSE));
2878
2879                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "depth_sparse", "",                        samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE, TEST_MODE_DEPTH_BIT));
2880                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "stencil_sparse", "",                      samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE, TEST_MODE_STENCIL_BIT));
2881                         samplesTests->addChild(new RasterizationSamplesTest(testCtx, "depth_stencil_sparse", "",        samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE, TEST_MODE_DEPTH_BIT | TEST_MODE_STENCIL_BIT));
2882
2883                         rasterizationSamplesTests->addChild(samplesTests.release());
2884                 }
2885
2886                 multisampleTests->addChild(rasterizationSamplesTests.release());
2887         }
2888
2889         // Raster samples consistency check
2890         {
2891                 de::MovePtr<tcu::TestCaseGroup> rasterSamplesConsistencyTests   (new tcu::TestCaseGroup(testCtx, "raster_samples_consistency", ""));
2892                 MultisampleTestParams                   paramsRegular                                   = {GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR};
2893                 MultisampleTestParams                   paramsSparse                                    = {GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE};
2894
2895                 addFunctionCaseWithPrograms(rasterSamplesConsistencyTests.get(),
2896                                                                         "unique_colors_check",
2897                                                                         "",
2898                                                                         initMultisamplePrograms,
2899                                                                         testRasterSamplesConsistency,
2900                                                                         paramsRegular);
2901
2902                 addFunctionCaseWithPrograms(rasterSamplesConsistencyTests.get(),
2903                                                                         "unique_colors_check_sparse",
2904                                                                         "",
2905                                                                         initMultisamplePrograms,
2906                                                                         testRasterSamplesConsistency,
2907                                                                         paramsSparse);
2908
2909                 multisampleTests->addChild(rasterSamplesConsistencyTests.release());
2910         }
2911
2912         // minSampleShading tests
2913         {
2914                 struct TestConfig
2915                 {
2916                         const char*     name;
2917                         float           minSampleShading;
2918                 };
2919
2920                 const TestConfig testConfigs[] =
2921                 {
2922                         { "min_0_0",    0.0f },
2923                         { "min_0_25",   0.25f },
2924                         { "min_0_5",    0.5f },
2925                         { "min_0_75",   0.75f },
2926                         { "min_1_0",    1.0f }
2927                 };
2928
2929                 de::MovePtr<tcu::TestCaseGroup> minSampleShadingTests(new tcu::TestCaseGroup(testCtx, "min_sample_shading", ""));
2930
2931                 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(testConfigs); configNdx++)
2932                 {
2933                         const TestConfig&                               testConfig                              = testConfigs[configNdx];
2934                         de::MovePtr<tcu::TestCaseGroup> minShadingValueTests    (new tcu::TestCaseGroup(testCtx, testConfigs[configNdx].name, ""));
2935
2936                         for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
2937                         {
2938                                 std::ostringstream caseName;
2939                                 caseName << "samples_" << samples[samplesNdx];
2940
2941                                 de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
2942
2943                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_triangle", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR));
2944                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_line", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_REGULAR));
2945                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_point", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_REGULAR));
2946
2947                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_triangle_sparse", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE));
2948                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_line_sparse", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_SPARSE));
2949                                 samplesTests->addChild(new MinSampleShadingTest(testCtx, "primitive_point_sparse", "", samples[samplesNdx], testConfig.minSampleShading, GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_SPARSE));
2950
2951                                 minShadingValueTests->addChild(samplesTests.release());
2952                         }
2953
2954                         minSampleShadingTests->addChild(minShadingValueTests.release());
2955                 }
2956
2957                 multisampleTests->addChild(minSampleShadingTests.release());
2958         }
2959
2960         // pSampleMask tests
2961         {
2962                 struct TestConfig
2963                 {
2964                         const char*             name;
2965                         const char*             description;
2966                         VkSampleMask    sampleMask;
2967                 };
2968
2969                 const TestConfig testConfigs[] =
2970                 {
2971                         { "mask_all_on",        "All mask bits are off",                        0x0 },
2972                         { "mask_all_off",       "All mask bits are on",                         0xFFFFFFFF },
2973                         { "mask_one",           "All mask elements are 0x1",            0x1},
2974                         { "mask_random",        "All mask elements are 0xAAAAAAAA",     0xAAAAAAAA },
2975                 };
2976
2977                 de::MovePtr<tcu::TestCaseGroup> sampleMaskTests(new tcu::TestCaseGroup(testCtx, "sample_mask", ""));
2978
2979                 for (int configNdx = 0; configNdx < DE_LENGTH_OF_ARRAY(testConfigs); configNdx++)
2980                 {
2981                         const TestConfig&                               testConfig                              = testConfigs[configNdx];
2982                         de::MovePtr<tcu::TestCaseGroup> sampleMaskValueTests    (new tcu::TestCaseGroup(testCtx, testConfig.name, testConfig.description));
2983
2984                         for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
2985                         {
2986                                 std::ostringstream caseName;
2987                                 caseName << "samples_" << samples[samplesNdx];
2988
2989                                 const deUint32                                  sampleMaskCount = samples[samplesNdx] / 32;
2990                                 de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
2991
2992                                 std::vector<VkSampleMask> mask;
2993                                 for (deUint32 maskNdx = 0; maskNdx < sampleMaskCount; maskNdx++)
2994                                         mask.push_back(testConfig.sampleMask);
2995
2996                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_triangle", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_REGULAR));
2997                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_line", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_REGULAR));
2998                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_point", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_REGULAR));
2999
3000                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_triangle_sparse", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_TRIANGLE, IMAGE_BACKING_MODE_SPARSE));
3001                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_line_sparse", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_LINE, IMAGE_BACKING_MODE_SPARSE));
3002                                 samplesTests->addChild(new SampleMaskTest(testCtx, "primitive_point_sparse", "", samples[samplesNdx], mask, GEOMETRY_TYPE_OPAQUE_POINT, IMAGE_BACKING_MODE_SPARSE));
3003
3004                                 sampleMaskValueTests->addChild(samplesTests.release());
3005                         }
3006
3007                         sampleMaskTests->addChild(sampleMaskValueTests.release());
3008                 }
3009
3010                 multisampleTests->addChild(sampleMaskTests.release());
3011
3012         }
3013
3014         // AlphaToOne tests
3015         {
3016                 de::MovePtr<tcu::TestCaseGroup> alphaToOneTests(new tcu::TestCaseGroup(testCtx, "alpha_to_one", ""));
3017
3018                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
3019                 {
3020                         std::ostringstream caseName;
3021                         caseName << "samples_" << samples[samplesNdx];
3022
3023                         alphaToOneTests->addChild(new AlphaToOneTest(testCtx, caseName.str(), "", samples[samplesNdx], IMAGE_BACKING_MODE_REGULAR));
3024
3025                         caseName << "_sparse";
3026                         alphaToOneTests->addChild(new AlphaToOneTest(testCtx, caseName.str(), "", samples[samplesNdx], IMAGE_BACKING_MODE_SPARSE));
3027                 }
3028
3029                 multisampleTests->addChild(alphaToOneTests.release());
3030         }
3031
3032         // AlphaToCoverageEnable tests
3033         {
3034                 de::MovePtr<tcu::TestCaseGroup> alphaToCoverageTests (new tcu::TestCaseGroup(testCtx, "alpha_to_coverage", ""));
3035
3036                 for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); samplesNdx++)
3037                 {
3038                         std::ostringstream caseName;
3039                         caseName << "samples_" << samples[samplesNdx];
3040
3041                         de::MovePtr<tcu::TestCaseGroup> samplesTests    (new tcu::TestCaseGroup(testCtx, caseName.str().c_str(), ""));
3042
3043                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_opaque", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_QUAD, IMAGE_BACKING_MODE_REGULAR));
3044                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_translucent", "", samples[samplesNdx], GEOMETRY_TYPE_TRANSLUCENT_QUAD, IMAGE_BACKING_MODE_REGULAR));
3045                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_invisible", "", samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_QUAD, IMAGE_BACKING_MODE_REGULAR));
3046
3047                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_opaque_sparse", "", samples[samplesNdx], GEOMETRY_TYPE_OPAQUE_QUAD, IMAGE_BACKING_MODE_SPARSE));
3048                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_translucent_sparse", "", samples[samplesNdx], GEOMETRY_TYPE_TRANSLUCENT_QUAD, IMAGE_BACKING_MODE_SPARSE));
3049                         samplesTests->addChild(new AlphaToCoverageTest(testCtx, "alpha_invisible_sparse", "", samples[samplesNdx], GEOMETRY_TYPE_INVISIBLE_QUAD, IMAGE_BACKING_MODE_SPARSE));
3050
3051                         alphaToCoverageTests->addChild(samplesTests.release());
3052                 }
3053                 multisampleTests->addChild(alphaToCoverageTests.release());
3054         }
3055
3056         // Sampling from a multisampled image texture (texelFetch)
3057         {
3058                 multisampleTests->addChild(createMultisampleSampledImageTests(testCtx));
3059         }
3060
3061         // Load/store on a multisampled rendered image (different kinds of access: color attachment write, storage image, etc.)
3062         {
3063                 multisampleTests->addChild(createMultisampleStorageImageTests(testCtx));
3064         }
3065
3066         // VK_EXT_sample_locations
3067         {
3068                 multisampleTests->addChild(createMultisampleSampleLocationsExtTests(testCtx));
3069         }
3070
3071         return multisampleTests.release();
3072 }
3073
3074 } // pipeline
3075 } // vkt