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