Fix stencil op in render pass stencil read only tests
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / renderpass / vktRenderPassTests.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief RenderPass tests
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktRenderPassTests.hpp"
25 #include "vktRenderPassTestsUtil.hpp"
26
27 #include "vktRenderPassMultisampleTests.hpp"
28 #include "vktRenderPassMultisampleResolveTests.hpp"
29 #include "vktRenderPassSampleReadTests.hpp"
30 #include "vktRenderPassSparseRenderTargetTests.hpp"
31 #include "vktRenderPassSubpassDependencyTests.hpp"
32 #include "vktRenderPassUnusedAttachmentTests.hpp"
33 #include "vktRenderPassUnusedClearAttachmentTests.hpp"
34 #include "vktRenderPassDepthStencilResolveTests.hpp"
35 #include "vktRenderPassUnusedAttachmentSparseFillingTests.hpp"
36 #include "vktRenderPassFragmentDensityMapTests.hpp"
37 #include "vktRenderPassMultipleSubpassesMultipleCommandBuffersTests.hpp"
38 #include "vktRenderPassLoadStoreOpNoneTests.hpp"
39 #include "vktDynamicRenderingTests.hpp"
40
41 #include "vktTestCaseUtil.hpp"
42 #include "vktTestGroupUtil.hpp"
43
44 #include "vkDefs.hpp"
45 #include "vkDeviceUtil.hpp"
46 #include "vkImageUtil.hpp"
47 #include "vkMemUtil.hpp"
48 #include "vkPlatform.hpp"
49 #include "vkPrograms.hpp"
50 #include "vkQueryUtil.hpp"
51 #include "vkRef.hpp"
52 #include "vkRefUtil.hpp"
53 #include "vkStrUtil.hpp"
54 #include "vkTypeUtil.hpp"
55 #include "vkCmdUtil.hpp"
56 #include "vkObjUtil.hpp"
57
58 #include "tcuFloat.hpp"
59 #include "tcuFormatUtil.hpp"
60 #include "tcuMaybe.hpp"
61 #include "tcuResultCollector.hpp"
62 #include "tcuTestLog.hpp"
63 #include "tcuTextureUtil.hpp"
64 #include "tcuVectorUtil.hpp"
65
66 #include "deRandom.hpp"
67 #include "deSTLUtil.hpp"
68 #include "deSharedPtr.hpp"
69 #include "deStringUtil.hpp"
70 #include "deUniquePtr.hpp"
71
72 #include <limits>
73 #include <set>
74 #include <string>
75 #include <vector>
76 #include <memory>
77
78 using namespace vk;
79
80 using tcu::BVec4;
81 using tcu::IVec2;
82 using tcu::IVec4;
83 using tcu::UVec2;
84 using tcu::UVec4;
85 using tcu::Vec2;
86 using tcu::Vec4;
87
88 using tcu::Maybe;
89 using tcu::just;
90 using tcu::nothing;
91
92 using tcu::ConstPixelBufferAccess;
93 using tcu::PixelBufferAccess;
94
95 using tcu::TestLog;
96
97 using de::UniquePtr;
98
99 using std::pair;
100 using std::set;
101 using std::string;
102 using std::vector;
103
104 namespace vkt
105 {
106 namespace
107 {
108 using namespace renderpass;
109
110 typedef vector<deUint8> DepthValuesArray;
111
112 static const deUint8    DEPTH_VALUES[]  = { 0u, 255u, 1u };
113
114 enum AllocationKind
115 {
116         ALLOCATION_KIND_SUBALLOCATED,
117         ALLOCATION_KIND_DEDICATED,
118 };
119
120 struct TestConfigExternal
121 {
122         TestConfigExternal (AllocationKind      allocationKind_,
123                                                 RenderingType   renderingType_)
124         : allocationKind        (allocationKind_)
125         , renderingType         (renderingType_)
126         {
127         }
128
129         AllocationKind  allocationKind;
130         RenderingType   renderingType;
131 };
132
133 de::MovePtr<Allocation> allocateBuffer (const InstanceInterface&        vki,
134                                                                                 const DeviceInterface&          vkd,
135                                                                                 const VkPhysicalDevice&         physDevice,
136                                                                                 const VkDevice                          device,
137                                                                                 const VkBuffer&                         buffer,
138                                                                                 const MemoryRequirement         requirement,
139                                                                                 Allocator&                                      allocator,
140                                                                                 AllocationKind                          allocationKind)
141 {
142         switch (allocationKind)
143         {
144                 case ALLOCATION_KIND_SUBALLOCATED:
145                 {
146                         const VkMemoryRequirements      memoryRequirements      = getBufferMemoryRequirements(vkd, device, buffer);
147
148                         return allocator.allocate(memoryRequirements, requirement);
149                 }
150
151                 case ALLOCATION_KIND_DEDICATED:
152                 {
153                         return allocateDedicated(vki, vkd, physDevice, device, buffer, requirement);
154                 }
155
156                 default:
157                 {
158                         TCU_THROW(InternalError, "Invalid allocation kind");
159                 }
160         }
161 }
162
163 de::MovePtr<Allocation> allocateImage (const InstanceInterface&         vki,
164                                                                            const DeviceInterface&               vkd,
165                                                                            const VkPhysicalDevice&              physDevice,
166                                                                            const VkDevice                               device,
167                                                                            const VkImage&                               image,
168                                                                            const MemoryRequirement              requirement,
169                                                                            Allocator&                                   allocator,
170                                                                            AllocationKind                               allocationKind)
171 {
172         switch (allocationKind)
173         {
174                 case ALLOCATION_KIND_SUBALLOCATED:
175                 {
176                         const VkMemoryRequirements      memoryRequirements      = getImageMemoryRequirements(vkd, device, image);
177
178                         return allocator.allocate(memoryRequirements, requirement);
179                 }
180
181                 case ALLOCATION_KIND_DEDICATED:
182                 {
183                         return allocateDedicated(vki, vkd, physDevice, device, image, requirement);
184                 }
185
186                 default:
187                 {
188                         TCU_THROW(InternalError, "Invalid allocation kind");
189                 }
190         }
191 }
192
193 enum BoolOp
194 {
195         BOOLOP_AND,
196         BOOLOP_OR,
197         BOOLOP_EQ,
198         BOOLOP_NEQ
199 };
200
201 const char* boolOpToString (BoolOp op)
202 {
203         switch (op)
204         {
205                 case BOOLOP_OR:
206                         return "||";
207
208                 case BOOLOP_AND:
209                         return "&&";
210
211                 case BOOLOP_EQ:
212                         return "==";
213
214                 case BOOLOP_NEQ:
215                         return "!=";
216
217                 default:
218                         DE_FATAL("Unknown boolean operation.");
219                         return DE_NULL;
220         }
221 }
222
223 bool performBoolOp (BoolOp op, bool a, bool b)
224 {
225         switch (op)
226         {
227                 case BOOLOP_OR:
228                         return a || b;
229
230                 case BOOLOP_AND:
231                         return a && b;
232
233                 case BOOLOP_EQ:
234                         return a == b;
235
236                 case BOOLOP_NEQ:
237                         return a != b;
238
239                 default:
240                         DE_FATAL("Unknown boolean operation.");
241                         return false;
242         }
243 }
244
245 BoolOp boolOpFromIndex (size_t index)
246 {
247         const BoolOp ops[] =
248         {
249                 BOOLOP_OR,
250                 BOOLOP_AND,
251                 BOOLOP_EQ,
252                 BOOLOP_NEQ
253         };
254
255         return ops[index % DE_LENGTH_OF_ARRAY(ops)];
256 }
257
258 static float requiredDepthEpsilon(VkFormat format)
259 {
260         // Possible precision loss in the unorm depth pipeline means that we need to check depths
261         // that go in and back out of the depth buffer with an epsilon rather than an exact match
262         deUint32 unormBits = 0;
263
264         switch (format)
265         {
266         case VK_FORMAT_D16_UNORM:
267                 unormBits = 16;
268                 break;
269         case VK_FORMAT_X8_D24_UNORM_PACK32:
270         case VK_FORMAT_D24_UNORM_S8_UINT:
271                 unormBits = 24;
272                 break;
273         case VK_FORMAT_D32_SFLOAT:
274         case VK_FORMAT_D32_SFLOAT_S8_UINT:
275         default:
276                 unormBits = 0;
277                 break;
278         }
279
280         if (unormBits > 0)
281                 return 1.0f / (float)((1 << unormBits) - 1);
282
283         return 0.0f; // Require exact match
284 }
285
286 static bool depthsEqual(float a, float b, float epsilon)
287 {
288         return fabs(a - b) <= epsilon;
289 }
290
291 Move<VkFramebuffer> createFramebuffer (const DeviceInterface&   vk,
292                                                                            VkDevice                                     device,
293                                                                            VkFramebufferCreateFlags     pCreateInfo_flags,
294                                                                            VkRenderPass                         pCreateInfo_renderPass,
295                                                                            deUint32                                     pCreateInfo_attachmentCount,
296                                                                            const VkImageView*           pCreateInfo_pAttachments,
297                                                                            deUint32                                     pCreateInfo_width,
298                                                                            deUint32                                     pCreateInfo_height,
299                                                                            deUint32                                     pCreateInfo_layers)
300 {
301         const VkFramebufferCreateInfo pCreateInfo =
302         {
303                 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
304                 DE_NULL,
305                 pCreateInfo_flags,
306                 pCreateInfo_renderPass,
307                 pCreateInfo_attachmentCount,
308                 pCreateInfo_pAttachments,
309                 pCreateInfo_width,
310                 pCreateInfo_height,
311                 pCreateInfo_layers,
312         };
313         return createFramebuffer(vk, device, &pCreateInfo);
314 }
315
316 Move<VkImage> createImage (const DeviceInterface&       vk,
317                                                    VkDevice                                     device,
318                                                    VkImageCreateFlags           pCreateInfo_flags,
319                                                    VkImageType                          pCreateInfo_imageType,
320                                                    VkFormat                                     pCreateInfo_format,
321                                                    VkExtent3D                           pCreateInfo_extent,
322                                                    deUint32                                     pCreateInfo_mipLevels,
323                                                    deUint32                                     pCreateInfo_arrayLayers,
324                                                    VkSampleCountFlagBits        pCreateInfo_samples,
325                                                    VkImageTiling                        pCreateInfo_tiling,
326                                                    VkImageUsageFlags            pCreateInfo_usage,
327                                                    VkSharingMode                        pCreateInfo_sharingMode,
328                                                    deUint32                                     pCreateInfo_queueFamilyCount,
329                                                    const deUint32*                      pCreateInfo_pQueueFamilyIndices,
330                                                    VkImageLayout                        pCreateInfo_initialLayout)
331 {
332         const VkImageCreateInfo pCreateInfo =
333         {
334                 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
335                 DE_NULL,
336                 pCreateInfo_flags,
337                 pCreateInfo_imageType,
338                 pCreateInfo_format,
339                 pCreateInfo_extent,
340                 pCreateInfo_mipLevels,
341                 pCreateInfo_arrayLayers,
342                 pCreateInfo_samples,
343                 pCreateInfo_tiling,
344                 pCreateInfo_usage,
345                 pCreateInfo_sharingMode,
346                 pCreateInfo_queueFamilyCount,
347                 pCreateInfo_pQueueFamilyIndices,
348                 pCreateInfo_initialLayout
349         };
350         return createImage(vk, device, &pCreateInfo);
351 }
352
353 void bindBufferMemory (const DeviceInterface& vk, VkDevice device, VkBuffer buffer, VkDeviceMemory mem, VkDeviceSize memOffset)
354 {
355         VK_CHECK(vk.bindBufferMemory(device, buffer, mem, memOffset));
356 }
357
358 void bindImageMemory (const DeviceInterface& vk, VkDevice device, VkImage image, VkDeviceMemory mem, VkDeviceSize memOffset)
359 {
360         VK_CHECK(vk.bindImageMemory(device, image, mem, memOffset));
361 }
362
363 Move<VkImageView> createImageView (const DeviceInterface&       vk,
364                                                                         VkDevice                                device,
365                                                                         VkImageViewCreateFlags  pCreateInfo_flags,
366                                                                         VkImage                                 pCreateInfo_image,
367                                                                         VkImageViewType                 pCreateInfo_viewType,
368                                                                         VkFormat                                pCreateInfo_format,
369                                                                         VkComponentMapping              pCreateInfo_components,
370                                                                         VkImageSubresourceRange pCreateInfo_subresourceRange)
371 {
372         const VkImageViewCreateInfo pCreateInfo =
373         {
374                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
375                 DE_NULL,
376                 pCreateInfo_flags,
377                 pCreateInfo_image,
378                 pCreateInfo_viewType,
379                 pCreateInfo_format,
380                 pCreateInfo_components,
381                 pCreateInfo_subresourceRange,
382         };
383         return createImageView(vk, device, &pCreateInfo);
384 }
385
386 Move<VkBuffer> createBuffer (const DeviceInterface&     vk,
387                                                          VkDevice                               device,
388                                                          VkBufferCreateFlags    pCreateInfo_flags,
389                                                          VkDeviceSize                   pCreateInfo_size,
390                                                          VkBufferUsageFlags             pCreateInfo_usage,
391                                                          VkSharingMode                  pCreateInfo_sharingMode,
392                                                          deUint32                               pCreateInfo_queueFamilyCount,
393                                                          const deUint32*                pCreateInfo_pQueueFamilyIndices)
394 {
395         const VkBufferCreateInfo pCreateInfo =
396         {
397                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
398                 DE_NULL,
399                 pCreateInfo_flags,
400                 pCreateInfo_size,
401                 pCreateInfo_usage,
402                 pCreateInfo_sharingMode,
403                 pCreateInfo_queueFamilyCount,
404                 pCreateInfo_pQueueFamilyIndices,
405         };
406         return createBuffer(vk, device, &pCreateInfo);
407 }
408
409 VkRenderPassBeginInfo createRenderPassBeginInfo (VkRenderPass                   pRenderPassBegin_renderPass,
410                                                                                                  VkFramebuffer                  pRenderPassBegin_framebuffer,
411                                                                                                  VkRect2D                               pRenderPassBegin_renderArea,
412                                                                                                  deUint32                               pRenderPassBegin_clearValueCount,
413                                                                                                  const VkClearValue*    pRenderPassBegin_pAttachmentClearValues)
414 {
415         const VkRenderPassBeginInfo renderPassBeginInfo =
416         {
417                 VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
418                 DE_NULL,
419                 pRenderPassBegin_renderPass,
420                 pRenderPassBegin_framebuffer,
421                 pRenderPassBegin_renderArea,
422                 pRenderPassBegin_clearValueCount,
423                 pRenderPassBegin_pAttachmentClearValues,
424         };
425
426         return renderPassBeginInfo;
427 }
428
429 void queueSubmit (const DeviceInterface& vk, VkQueue queue, deUint32 cmdBufferCount, const VkCommandBuffer* pCmdBuffers, VkFence fence)
430 {
431         const VkSubmitInfo submitInfo =
432         {
433                 VK_STRUCTURE_TYPE_SUBMIT_INFO,
434                 DE_NULL,
435                 0u,                                                             // waitSemaphoreCount
436                 (const VkSemaphore*)DE_NULL,    // pWaitSemaphores
437                 (const VkPipelineStageFlags*)DE_NULL,
438                 cmdBufferCount,                                 // commandBufferCount
439                 pCmdBuffers,
440                 0u,                                                             // signalSemaphoreCount
441                 (const VkSemaphore*)DE_NULL,    // pSignalSemaphores
442         };
443         VK_CHECK(vk.queueSubmit(queue, 1u, &submitInfo, fence));
444 }
445
446 void waitForFences (const DeviceInterface& vk, VkDevice device, deUint32 fenceCount, const VkFence* pFences, VkBool32 waitAll, deUint64 timeout)
447 {
448         VK_CHECK(vk.waitForFences(device, fenceCount, pFences, waitAll, timeout));
449 }
450
451 VkImageAspectFlags getImageAspectFlags (VkFormat vkFormat)
452 {
453         const tcu::TextureFormat format = mapVkFormat(vkFormat);
454
455         DE_STATIC_ASSERT(tcu::TextureFormat::CHANNELORDER_LAST == 22);
456
457         switch (format.order)
458         {
459                 case tcu::TextureFormat::DS:
460                         return VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT;
461
462                 case tcu::TextureFormat::D:
463                         return VK_IMAGE_ASPECT_DEPTH_BIT;
464
465                 case tcu::TextureFormat::S:
466                         return VK_IMAGE_ASPECT_STENCIL_BIT;
467
468                 default:
469                         return VK_IMAGE_ASPECT_COLOR_BIT;
470         }
471 }
472
473 VkAccessFlags getAllMemoryReadFlags (void)
474 {
475         return VK_ACCESS_TRANSFER_READ_BIT
476                    | VK_ACCESS_UNIFORM_READ_BIT
477                    | VK_ACCESS_HOST_READ_BIT
478                    | VK_ACCESS_INDEX_READ_BIT
479                    | VK_ACCESS_SHADER_READ_BIT
480                    | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
481                    | VK_ACCESS_INDIRECT_COMMAND_READ_BIT
482                    | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
483                    | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT
484                    | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
485 }
486
487 VkAccessFlags getAllMemoryWriteFlags (void)
488 {
489         return VK_ACCESS_TRANSFER_WRITE_BIT
490                    | VK_ACCESS_HOST_WRITE_BIT
491                    | VK_ACCESS_SHADER_WRITE_BIT
492                    | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
493                    | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
494 }
495
496 VkAccessFlags getMemoryFlagsForLayout (const VkImageLayout layout)
497 {
498         switch (layout)
499         {
500                 case VK_IMAGE_LAYOUT_GENERAL:                                                                           return getAllMemoryReadFlags() | getAllMemoryWriteFlags();
501                 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:                                          return VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
502                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:                          return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
503                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:                           return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
504                 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:                                          return VK_ACCESS_SHADER_READ_BIT;
505                 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:                                                      return VK_ACCESS_TRANSFER_READ_BIT;
506                 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:                                                      return VK_ACCESS_TRANSFER_WRITE_BIT;
507                 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:        return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT;
508                 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:        return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_SHADER_READ_BIT;
509                 default:
510                         return (VkAccessFlags)0;
511         }
512 }
513
514 VkPipelineStageFlags getAllPipelineStageFlags (void)
515 {
516         /* All relevant flags for a pipeline containing VS+PS. */
517         return VK_PIPELINE_STAGE_TRANSFER_BIT
518                    | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
519                    | VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT
520                    | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT
521                    | VK_PIPELINE_STAGE_VERTEX_SHADER_BIT
522                    | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
523                    | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
524                    | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT
525                    | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
526                    | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
527                    | VK_PIPELINE_STAGE_HOST_BIT;
528 }
529
530 class AttachmentReference
531 {
532 public:
533                                                 AttachmentReference             (deUint32                       attachment,
534                                                                                                  VkImageLayout          layout,
535                                                                                                  VkImageAspectFlags     aspectMask = static_cast<VkImageAspectFlags>(0u))
536                 : m_attachment  (attachment)
537                 , m_layout              (layout)
538                 , m_aspectMask  (aspectMask)
539         {
540         }
541
542         deUint32                        getAttachment                   (void) const { return m_attachment;     }
543         VkImageLayout           getImageLayout                  (void) const { return m_layout;         }
544         VkImageAspectFlags      getAspectMask                   (void) const { return m_aspectMask;     }
545         void                            setImageLayout                  (VkImageLayout layout) { m_layout = layout;     }
546
547 private:
548         deUint32                        m_attachment;
549         VkImageLayout           m_layout;
550         VkImageAspectFlags      m_aspectMask;
551 };
552
553 class Subpass
554 {
555 public:
556                                                                                 Subpass                                         (VkPipelineBindPoint                            pipelineBindPoint,
557                                                                                                                                          VkSubpassDescriptionFlags                      flags,
558                                                                                                                                          const vector<AttachmentReference>&     inputAttachments,
559                                                                                                                                          const vector<AttachmentReference>&     colorAttachments,
560                                                                                                                                          const vector<AttachmentReference>&     resolveAttachments,
561                                                                                                                                          AttachmentReference                            depthStencilAttachment,
562                                                                                                                                          const vector<deUint32>&                        preserveAttachments,
563                                                                                                                                          bool                                                           omitBlendState = false)
564                 : m_pipelineBindPoint           (pipelineBindPoint)
565                 , m_flags                                       (flags)
566                 , m_inputAttachments            (inputAttachments)
567                 , m_colorAttachments            (colorAttachments)
568                 , m_resolveAttachments          (resolveAttachments)
569                 , m_depthStencilAttachment      (depthStencilAttachment)
570                 , m_preserveAttachments         (preserveAttachments)
571                 , m_omitBlendState                      (omitBlendState)
572         {
573         }
574
575         VkPipelineBindPoint                                     getPipelineBindPoint            (void) const { return m_pipelineBindPoint;              }
576         VkSubpassDescriptionFlags                       getFlags                                        (void) const { return m_flags;                                  }
577         const vector<AttachmentReference>&      getInputAttachments                     (void) const { return m_inputAttachments;               }
578         const vector<AttachmentReference>&      getColorAttachments                     (void) const { return m_colorAttachments;               }
579         const vector<AttachmentReference>&      getResolveAttachments           (void) const { return m_resolveAttachments;             }
580         const AttachmentReference&                      getDepthStencilAttachment       (void) const { return m_depthStencilAttachment; }
581         const vector<deUint32>&                         getPreserveAttachments          (void) const { return m_preserveAttachments;    }
582         bool                                                            getOmitBlendState                       (void) const { return m_omitBlendState;                 }
583
584 private:
585         VkPipelineBindPoint                                     m_pipelineBindPoint;
586         VkSubpassDescriptionFlags                       m_flags;
587
588         vector<AttachmentReference>                     m_inputAttachments;
589         vector<AttachmentReference>                     m_colorAttachments;
590         vector<AttachmentReference>                     m_resolveAttachments;
591         AttachmentReference                                     m_depthStencilAttachment;
592
593         vector<deUint32>                                        m_preserveAttachments;
594         bool                                                            m_omitBlendState;
595 };
596
597 class SubpassDependency
598 {
599 public:
600                                                         SubpassDependency       (deUint32                               srcPass,
601                                                                                                  deUint32                               dstPass,
602
603                                                                                                  VkPipelineStageFlags   srcStageMask,
604                                                                                                  VkPipelineStageFlags   dstStageMask,
605
606                                                                                                  VkAccessFlags                  srcAccessMask,
607                                                                                                  VkAccessFlags                  dstAccessMask,
608
609                                                                                                  VkDependencyFlags              flags)
610                 : m_srcPass                     (srcPass)
611                 , m_dstPass                     (dstPass)
612
613                 , m_srcStageMask        (srcStageMask)
614                 , m_dstStageMask        (dstStageMask)
615
616                 , m_srcAccessMask       (srcAccessMask)
617                 , m_dstAccessMask       (dstAccessMask)
618                 , m_flags                       (flags)
619         {
620         }
621
622         deUint32                                getSrcPass                      (void) const { return m_srcPass;                }
623         deUint32                                getDstPass                      (void) const { return m_dstPass;                }
624
625         VkPipelineStageFlags    getSrcStageMask         (void) const { return m_srcStageMask;   }
626         VkPipelineStageFlags    getDstStageMask         (void) const { return m_dstStageMask;   }
627
628         VkAccessFlags                   getSrcAccessMask        (void) const { return m_srcAccessMask;  }
629         VkAccessFlags                   getDstAccessMask        (void) const { return m_dstAccessMask;  }
630
631         VkDependencyFlags               getFlags                        (void) const { return m_flags;          }
632
633         void                                    setSrcAccessMask        (const VkAccessFlags& flags) { m_srcAccessMask = flags; }
634         void                                    setDstAccessMask        (const VkAccessFlags& flags) { m_dstAccessMask = flags; }
635
636 private:
637         deUint32                                m_srcPass;
638         deUint32                                m_dstPass;
639
640         VkPipelineStageFlags    m_srcStageMask;
641         VkPipelineStageFlags    m_dstStageMask;
642
643         VkAccessFlags                   m_srcAccessMask;
644         VkAccessFlags                   m_dstAccessMask;
645         VkDependencyFlags               m_flags;
646 };
647
648 class Attachment
649 {
650 public:
651                                                         Attachment                      (VkFormat                               format,
652                                                                                                  VkSampleCountFlagBits  samples,
653
654                                                                                                  VkAttachmentLoadOp             loadOp,
655                                                                                                  VkAttachmentStoreOp    storeOp,
656
657                                                                                                  VkAttachmentLoadOp             stencilLoadOp,
658                                                                                                  VkAttachmentStoreOp    stencilStoreOp,
659
660                                                                                                  VkImageLayout                  initialLayout,
661                                                                                                  VkImageLayout                  finalLayout)
662                 : m_format                      (format)
663                 , m_samples                     (samples)
664
665                 , m_loadOp                      (loadOp)
666                 , m_storeOp                     (storeOp)
667
668                 , m_stencilLoadOp       (stencilLoadOp)
669                 , m_stencilStoreOp      (stencilStoreOp)
670
671                 , m_initialLayout       (initialLayout)
672                 , m_finalLayout         (finalLayout)
673         {
674         }
675
676         VkFormat                                getFormat                       (void) const { return m_format;                 }
677         VkSampleCountFlagBits   getSamples                      (void) const { return m_samples;                }
678
679         VkAttachmentLoadOp              getLoadOp                       (void) const { return m_loadOp;                 }
680         VkAttachmentStoreOp             getStoreOp                      (void) const { return m_storeOp;                }
681
682
683         VkAttachmentLoadOp              getStencilLoadOp        (void) const { return m_stencilLoadOp;  }
684         VkAttachmentStoreOp             getStencilStoreOp       (void) const { return m_stencilStoreOp; }
685
686         VkImageLayout                   getInitialLayout        (void) const { return m_initialLayout;  }
687         VkImageLayout                   getFinalLayout          (void) const { return m_finalLayout;    }
688
689 private:
690         VkFormat                                m_format;
691         VkSampleCountFlagBits   m_samples;
692
693         VkAttachmentLoadOp              m_loadOp;
694         VkAttachmentStoreOp             m_storeOp;
695
696         VkAttachmentLoadOp              m_stencilLoadOp;
697         VkAttachmentStoreOp             m_stencilStoreOp;
698
699         VkImageLayout                   m_initialLayout;
700         VkImageLayout                   m_finalLayout;
701 };
702
703 class RenderPass
704 {
705 public:
706                                                                                                                 RenderPass              (const vector<Attachment>&                                              attachments,
707                                                                                                                                                  const vector<Subpass>&                                                 subpasses,
708                                                                                                                                                  const vector<SubpassDependency>&                               dependencies,
709                                                                                                                                                  const vector<VkInputAttachmentAspectReference> inputAspects = vector<VkInputAttachmentAspectReference>())
710                 : m_attachments         (attachments)
711                 , m_subpasses           (subpasses)
712                 , m_dependencies        (dependencies)
713                 , m_inputAspects        (inputAspects)
714         {
715         }
716
717         const vector<Attachment>&                                                       getAttachments  (void) const { return m_attachments;    }
718         const vector<Subpass>&                                                          getSubpasses    (void) const { return m_subpasses;              }
719         const vector<SubpassDependency>&                                        getDependencies (void) const { return m_dependencies;   }
720         const vector<VkInputAttachmentAspectReference>&         getInputAspects (void) const { return m_inputAspects;   }
721
722 private:
723         const vector<Attachment>                                                        m_attachments;
724         const vector<Subpass>                                                           m_subpasses;
725         const vector<SubpassDependency>                                         m_dependencies;
726         const vector<VkInputAttachmentAspectReference>          m_inputAspects;
727 };
728
729 struct TestConfig
730 {
731         enum RenderTypes
732         {
733                 RENDERTYPES_NONE        = 0,
734                 RENDERTYPES_CLEAR       = (1<<1),
735                 RENDERTYPES_DRAW        = (1<<2)
736         };
737
738         enum CommandBufferTypes
739         {
740                 COMMANDBUFFERTYPES_INLINE               = (1<<0),
741                 COMMANDBUFFERTYPES_SECONDARY    = (1<<1)
742         };
743
744         enum ImageMemory
745         {
746                 IMAGEMEMORY_STRICT              = (1<<0),
747                 IMAGEMEMORY_LAZY                = (1<<1)
748         };
749
750                                                 TestConfig (const RenderPass&                   renderPass_,
751                                                                         RenderTypes                                     renderTypes_,
752                                                                         CommandBufferTypes                      commandBufferTypes_,
753                                                                         ImageMemory                                     imageMemory_,
754                                                                         const UVec2&                            targetSize_,
755                                                                         const UVec2&                            renderPos_,
756                                                                         const UVec2&                            renderSize_,
757                                                                         deBool                                          useFormatCompCount_,
758                                                                         deUint32                                        seed_,
759                                                                         deUint32                                        drawStartNdx_,
760                                                                         AllocationKind                          allocationKind_,
761                                                                         RenderingType                           renderingType_,
762                                                                         vector<DeviceCoreFeature>       requiredFeatures_ = vector<DeviceCoreFeature>())
763                 : renderPass                    (renderPass_)
764                 , renderTypes                   (renderTypes_)
765                 , commandBufferTypes    (commandBufferTypes_)
766                 , imageMemory                   (imageMemory_)
767                 , targetSize                    (targetSize_)
768                 , renderPos                             (renderPos_)
769                 , renderSize                    (renderSize_)
770                 , useFormatCompCount    (useFormatCompCount_)
771                 , seed                                  (seed_)
772                 , drawStartNdx                  (drawStartNdx_)
773                 , allocationKind                (allocationKind_)
774                 , renderingType                 (renderingType_)
775                 , requiredFeatures              (requiredFeatures_)
776         {
777                 DepthValuesArray        shuffledDepthValues     (&DEPTH_VALUES[0], &DEPTH_VALUES[DE_LENGTH_OF_ARRAY(DEPTH_VALUES)]);
778                 de::Random                      rng                                     (seed + 1);
779
780                 rng.shuffle(shuffledDepthValues.begin(), shuffledDepthValues.end());
781
782                 depthValues.push_back(shuffledDepthValues[0]);
783                 depthValues.push_back(shuffledDepthValues[1]);
784         }
785
786         RenderPass                                      renderPass;
787         RenderTypes                                     renderTypes;
788         CommandBufferTypes                      commandBufferTypes;
789         ImageMemory                                     imageMemory;
790         UVec2                                           targetSize;
791         UVec2                                           renderPos;
792         UVec2                                           renderSize;
793         deBool                                          useFormatCompCount;
794         deUint32                                        seed;
795         deUint32                                        drawStartNdx;
796         AllocationKind                          allocationKind;
797         RenderingType                           renderingType;
798         vector<DeviceCoreFeature>       requiredFeatures;
799         DepthValuesArray                        depthValues;
800 };
801
802 TestConfig::RenderTypes operator| (TestConfig::RenderTypes a, TestConfig::RenderTypes b)
803 {
804         return (TestConfig::RenderTypes)(((deUint32)a) | ((deUint32)b));
805 }
806
807 TestConfig::CommandBufferTypes operator| (TestConfig::CommandBufferTypes a, TestConfig::CommandBufferTypes b)
808 {
809         return (TestConfig::CommandBufferTypes)(((deUint32)a) | ((deUint32)b));
810 }
811
812 TestConfig::ImageMemory operator| (TestConfig::ImageMemory a, TestConfig::ImageMemory b)
813 {
814         return (TestConfig::ImageMemory)(((deUint32)a) | ((deUint32)b));
815 }
816
817 void checkSupport (Context& context, TestConfig config)
818 {
819         for (size_t featureNdx = 0; featureNdx < config.requiredFeatures.size(); featureNdx++)
820                 context.requireDeviceCoreFeature(config.requiredFeatures[featureNdx]);
821 }
822
823 void logRenderPassInfo (TestLog&                        log,
824                                                 const RenderPass&       renderPass)
825 {
826         const bool                                      useExternalInputAspect  = !renderPass.getInputAspects().empty();
827         const tcu::ScopedLogSection     section                                 (log, "RenderPass", "RenderPass");
828
829         {
830                 const tcu::ScopedLogSection     attachmentsSection      (log, "Attachments", "Attachments");
831                 const vector<Attachment>&       attachments                     = renderPass.getAttachments();
832
833                 for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
834                 {
835                         const tcu::ScopedLogSection     attachmentSection       (log, "Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx));
836                         const Attachment&                       attachment                      = attachments[attachmentNdx];
837
838                         log << TestLog::Message << "Format: " << attachment.getFormat() << TestLog::EndMessage;
839                         log << TestLog::Message << "Samples: " << attachment.getSamples() << TestLog::EndMessage;
840
841                         log << TestLog::Message << "LoadOp: " << attachment.getLoadOp() << TestLog::EndMessage;
842                         log << TestLog::Message << "StoreOp: " << attachment.getStoreOp() << TestLog::EndMessage;
843
844                         log << TestLog::Message << "StencilLoadOp: " << attachment.getStencilLoadOp() << TestLog::EndMessage;
845                         log << TestLog::Message << "StencilStoreOp: " << attachment.getStencilStoreOp() << TestLog::EndMessage;
846
847                         log << TestLog::Message << "InitialLayout: " << attachment.getInitialLayout() << TestLog::EndMessage;
848                         log << TestLog::Message << "FinalLayout: " << attachment.getFinalLayout() << TestLog::EndMessage;
849                 }
850         }
851
852         if (useExternalInputAspect)
853         {
854                 const tcu::ScopedLogSection     inputAspectSection      (log, "InputAspects", "InputAspects");
855
856                 for (size_t aspectNdx = 0; aspectNdx < renderPass.getInputAspects().size(); aspectNdx++)
857                 {
858                         const VkInputAttachmentAspectReference& inputAspect     (renderPass.getInputAspects()[aspectNdx]);
859
860                         log << TestLog::Message << "Subpass: " << inputAspect.subpass << TestLog::EndMessage;
861                         log << TestLog::Message << "InputAttachmentIndex: " << inputAspect.inputAttachmentIndex << TestLog::EndMessage;
862                         log << TestLog::Message << "AspectFlags: " << getImageAspectFlagsStr(inputAspect.aspectMask) << TestLog::EndMessage;
863                 }
864         }
865
866         {
867                 const tcu::ScopedLogSection     subpassesSection        (log, "Subpasses", "Subpasses");
868                 const vector<Subpass>&          subpasses                       = renderPass.getSubpasses();
869
870                 for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
871                 {
872                         const tcu::ScopedLogSection                     subpassSection          (log, "Subpass" + de::toString(subpassNdx), "Subpass " + de::toString(subpassNdx));
873                         const Subpass&                                          subpass                         = subpasses[subpassNdx];
874
875                         const vector<AttachmentReference>&      inputAttachments        = subpass.getInputAttachments();
876                         const vector<AttachmentReference>&      colorAttachments        = subpass.getColorAttachments();
877                         const vector<AttachmentReference>&      resolveAttachments      = subpass.getResolveAttachments();
878                         const vector<deUint32>&                         preserveAttachments     = subpass.getPreserveAttachments();
879
880                         if (!inputAttachments.empty())
881                         {
882                                 const tcu::ScopedLogSection     inputAttachmentsSection (log, "Inputs", "Inputs");
883
884                                 for (size_t inputNdx = 0; inputNdx < inputAttachments.size(); inputNdx++)
885                                 {
886                                         const tcu::ScopedLogSection     inputAttachmentSection  (log, "Input" + de::toString(inputNdx), "Input " + de::toString(inputNdx));
887                                         const AttachmentReference&      inputAttachment                 = inputAttachments[inputNdx];
888
889                                         log << TestLog::Message << "Attachment: " << inputAttachment.getAttachment() << TestLog::EndMessage;
890                                         log << TestLog::Message << "Layout: " << inputAttachment.getImageLayout() << TestLog::EndMessage;
891                                         if (!useExternalInputAspect)
892                                                 log << TestLog::Message << "AspectMask: " << inputAttachment.getAspectMask() << TestLog::EndMessage;
893                                 }
894                         }
895
896                         if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
897                         {
898                                 const tcu::ScopedLogSection     depthStencilAttachmentSection   (log, "DepthStencil", "DepthStencil");
899                                 const AttachmentReference&      depthStencilAttachment                  = subpass.getDepthStencilAttachment();
900
901                                 log << TestLog::Message << "Attachment: " << depthStencilAttachment.getAttachment() << TestLog::EndMessage;
902                                 log << TestLog::Message << "Layout: " << depthStencilAttachment.getImageLayout() << TestLog::EndMessage;
903                         }
904
905                         if (!colorAttachments.empty())
906                         {
907                                 const tcu::ScopedLogSection     colorAttachmentsSection (log, "Colors", "Colors");
908
909                                 for (size_t colorNdx = 0; colorNdx < colorAttachments.size(); colorNdx++)
910                                 {
911                                         const tcu::ScopedLogSection     colorAttachmentSection  (log, "Color" + de::toString(colorNdx), "Color " + de::toString(colorNdx));
912                                         const AttachmentReference&      colorAttachment                 = colorAttachments[colorNdx];
913
914                                         log << TestLog::Message << "Attachment: " << colorAttachment.getAttachment() << TestLog::EndMessage;
915                                         log << TestLog::Message << "Layout: " << colorAttachment.getImageLayout() << TestLog::EndMessage;
916                                 }
917                         }
918
919                         if (!resolveAttachments.empty())
920                         {
921                                 const tcu::ScopedLogSection     resolveAttachmentsSection       (log, "Resolves", "Resolves");
922
923                                 for (size_t resolveNdx = 0; resolveNdx < resolveAttachments.size(); resolveNdx++)
924                                 {
925                                         const tcu::ScopedLogSection     resolveAttachmentSection        (log, "Resolve" + de::toString(resolveNdx), "Resolve " + de::toString(resolveNdx));
926                                         const AttachmentReference&      resolveAttachment                       = resolveAttachments[resolveNdx];
927
928                                         log << TestLog::Message << "Attachment: " << resolveAttachment.getAttachment() << TestLog::EndMessage;
929                                         log << TestLog::Message << "Layout: " << resolveAttachment.getImageLayout() << TestLog::EndMessage;
930                                 }
931                         }
932
933                         if (!preserveAttachments.empty())
934                         {
935                                 const tcu::ScopedLogSection     preserveAttachmentsSection      (log, "Preserves", "Preserves");
936
937                                 for (size_t preserveNdx = 0; preserveNdx < preserveAttachments.size(); preserveNdx++)
938                                 {
939                                         const tcu::ScopedLogSection     preserveAttachmentSection       (log, "Preserve" + de::toString(preserveNdx), "Preserve " + de::toString(preserveNdx));
940                                         const deUint32                          preserveAttachment                      = preserveAttachments[preserveNdx];
941
942                                         log << TestLog::Message << "Attachment: " << preserveAttachment << TestLog::EndMessage;
943                                 }
944                         }
945                 }
946
947         }
948
949         if (!renderPass.getDependencies().empty())
950         {
951                 const tcu::ScopedLogSection     dependenciesSection     (log, "Dependencies", "Dependencies");
952
953                 for (size_t depNdx = 0; depNdx < renderPass.getDependencies().size(); depNdx++)
954                 {
955                         const tcu::ScopedLogSection     dependencySection       (log, "Dependency" + de::toString(depNdx), "Dependency " + de::toString(depNdx));
956                         const SubpassDependency&        dep                                     = renderPass.getDependencies()[depNdx];
957
958                         log << TestLog::Message << "Source: " << dep.getSrcPass() << TestLog::EndMessage;
959                         log << TestLog::Message << "Destination: " << dep.getDstPass() << TestLog::EndMessage;
960
961                         log << TestLog::Message << "Source Stage Mask: " << dep.getSrcStageMask() << TestLog::EndMessage;
962                         log << TestLog::Message << "Destination Stage Mask: " << dep.getDstStageMask() << TestLog::EndMessage;
963
964                         log << TestLog::Message << "Input Mask: " << dep.getDstAccessMask() << TestLog::EndMessage;
965                         log << TestLog::Message << "Output Mask: " << dep.getSrcAccessMask() << TestLog::EndMessage;
966                         log << TestLog::Message << "Dependency Flags: " << getDependencyFlagsStr(dep.getFlags()) << TestLog::EndMessage;
967                 }
968         }
969 }
970
971 std::string clearColorToString (VkFormat vkFormat, VkClearColorValue value, deBool useFormatCompCount)
972 {
973         const tcu::TextureFormat                format                  = mapVkFormat(vkFormat);
974         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
975         const tcu::BVec4                                channelMask             = tcu::getTextureFormatChannelMask(format);
976         const deUint32                                  componentCount  = (useFormatCompCount ? (deUint32)tcu::getNumUsedChannels(format.order) : 4);
977
978         std::ostringstream                              stream;
979
980         stream << "(";
981
982         switch (channelClass)
983         {
984                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
985                         for (deUint32 i = 0; i < componentCount; i++)
986                         {
987                                 if (i > 0)
988                                         stream << ", ";
989
990                                 if (channelMask[i])
991                                         stream << value.int32[i];
992                                 else
993                                         stream << "Undef";
994                         }
995                         break;
996
997                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
998                         for (deUint32 i = 0; i < componentCount; i++)
999                         {
1000                                 if (i > 0)
1001                                         stream << ", ";
1002
1003                                 if (channelMask[i])
1004                                         stream << value.uint32[i];
1005                                 else
1006                                         stream << "Undef";
1007                         }
1008                         break;
1009
1010                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1011                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1012                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
1013                         for (deUint32 i = 0; i < componentCount; i++)
1014                         {
1015                                 if (i > 0)
1016                                         stream << ", ";
1017
1018                                 if (channelMask[i])
1019                                         stream << value.float32[i];
1020                                 else
1021                                         stream << "Undef";
1022                         }
1023                         break;
1024
1025                 default:
1026                         DE_FATAL("Unknown channel class");
1027         }
1028
1029         stream << ")";
1030
1031         return stream.str();
1032 }
1033
1034 std::string clearValueToString (VkFormat vkFormat, VkClearValue value, deBool useFormatCompCount)
1035 {
1036         const tcu::TextureFormat        format  = mapVkFormat(vkFormat);
1037
1038         if (tcu::hasStencilComponent(format.order) || tcu::hasDepthComponent(format.order))
1039         {
1040                 std::ostringstream stream;
1041
1042                 stream << "(";
1043
1044                 if (tcu::hasStencilComponent(format.order))
1045                         stream << "stencil: " << value.depthStencil.stencil;
1046
1047                 if (tcu::hasStencilComponent(format.order) && tcu::hasDepthComponent(format.order))
1048                         stream << ", ";
1049
1050                 if (tcu::hasDepthComponent(format.order))
1051                         stream << "depth: " << value.depthStencil.depth;
1052
1053                 stream << ")";
1054
1055                 return stream.str();
1056         }
1057         else
1058                 return clearColorToString(vkFormat, value.color, useFormatCompCount);
1059 }
1060
1061 VkClearColorValue randomColorClearValue (const Attachment& attachment, de::Random& rng, deBool useFormatCompCount)
1062 {
1063         const float                                             clearNan                = tcu::Float32::nan().asFloat();
1064         const tcu::TextureFormat                format                  = mapVkFormat(attachment.getFormat());
1065         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
1066         const tcu::BVec4                                channelMask             = tcu::getTextureFormatChannelMask(format);
1067         const deUint32                                  componentCount  = (useFormatCompCount ? (deUint32)tcu::getNumUsedChannels(format.order) : 4);
1068         VkClearColorValue                               clearColor;
1069
1070         switch (channelClass)
1071         {
1072                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
1073                 {
1074                         for (deUint32 ndx = 0; ndx < componentCount; ndx++)
1075                         {
1076                                 if (!channelMask[ndx])
1077                                         clearColor.int32[ndx] = std::numeric_limits<deInt32>::min();
1078                                 else
1079                                         clearColor.uint32[ndx] = rng.getBool() ? 1u : 0u;
1080                         }
1081                         break;
1082                 }
1083
1084                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
1085                 {
1086                         for (deUint32 ndx = 0; ndx < componentCount; ndx++)
1087                         {
1088                                 if (!channelMask[ndx])
1089                                         clearColor.uint32[ndx] = std::numeric_limits<deUint32>::max();
1090                                 else
1091                                         clearColor.uint32[ndx] = rng.getBool() ? 1u : 0u;
1092                         }
1093                         break;
1094                 }
1095
1096                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1097                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1098                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
1099                 {
1100                         for (deUint32 ndx = 0; ndx < componentCount; ndx++)
1101                         {
1102                                 if (!channelMask[ndx])
1103                                         clearColor.float32[ndx] = clearNan;
1104                                 else
1105                                         clearColor.float32[ndx] = rng.getBool() ? 1.0f : 0.0f;
1106                         }
1107                         break;
1108                 }
1109
1110                 default:
1111                         DE_FATAL("Unknown channel class");
1112         }
1113
1114         return clearColor;
1115 }
1116
1117 template <typename AttachmentDesc>
1118 AttachmentDesc createAttachmentDescription (const Attachment& attachment)
1119 {
1120         const AttachmentDesc    attachmentDescription   //  VkAttachmentDescription                                                                             ||  VkAttachmentDescription2KHR
1121         (
1122                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
1123                 DE_NULL,                                                                        //                                                                                                                              ||  const void*                                                 pNext;
1124                 0u,                                                                                     //  VkAttachmentDescriptionFlags        flags;                                          ||  VkAttachmentDescriptionFlags                flags;
1125                 attachment.getFormat(),                                         //  VkFormat                                            format;                                         ||  VkFormat                                                    format;
1126                 attachment.getSamples(),                                        //  VkSampleCountFlagBits                       samples;                                        ||  VkSampleCountFlagBits                               samples;
1127                 attachment.getLoadOp(),                                         //  VkAttachmentLoadOp                          loadOp;                                         ||  VkAttachmentLoadOp                                  loadOp;
1128                 attachment.getStoreOp(),                                        //  VkAttachmentStoreOp                         storeOp;                                        ||  VkAttachmentStoreOp                                 storeOp;
1129                 attachment.getStencilLoadOp(),                          //  VkAttachmentLoadOp                          stencilLoadOp;                          ||  VkAttachmentLoadOp                                  stencilLoadOp;
1130                 attachment.getStencilStoreOp(),                         //  VkAttachmentStoreOp                         stencilStoreOp;                         ||  VkAttachmentStoreOp                                 stencilStoreOp;
1131                 attachment.getInitialLayout(),                          //  VkImageLayout                                       initialLayout;                          ||  VkImageLayout                                               initialLayout;
1132                 attachment.getFinalLayout()                                     //  VkImageLayout                                       finalLayout;                            ||  VkImageLayout                                               finalLayout;
1133         );
1134
1135         return attachmentDescription;
1136 }
1137
1138 template <typename AttachmentRef>
1139 AttachmentRef createAttachmentReference (const AttachmentReference& referenceInfo)
1140 {
1141         const AttachmentRef     reference                                       //  VkAttachmentReference                                                                               ||  VkAttachmentReference2KHR
1142         (
1143                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
1144                 DE_NULL,                                                                        //                                                                                                                              ||  const void*                                                 pNext;
1145                 referenceInfo.getAttachment(),                          //  deUint32                                            attachment;                                     ||  deUint32                                                    attachment;
1146                 referenceInfo.getImageLayout(),                         //  VkImageLayout                                       layout;                                         ||  VkImageLayout                                               layout;
1147                 referenceInfo.getAspectMask()                           //                                                                                                                              ||  VkImageAspectFlags                                  aspectMask;
1148         );
1149
1150         return reference;
1151 }
1152
1153 template <typename SubpassDesc, typename AttachmentRef>
1154 SubpassDesc createSubpassDescription (const Subpass&                    subpass,
1155                                                                           vector<AttachmentRef>*        attachmentReferenceLists,
1156                                                                           vector<deUint32>*                     preserveAttachmentReferences)
1157 {
1158         vector<AttachmentRef>&  inputAttachmentReferences                       = attachmentReferenceLists[0];
1159         vector<AttachmentRef>&  colorAttachmentReferences                       = attachmentReferenceLists[1];
1160         vector<AttachmentRef>&  resolveAttachmentReferences                     = attachmentReferenceLists[2];
1161         vector<AttachmentRef>&  depthStencilAttachmentReferences        = attachmentReferenceLists[3];
1162
1163         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
1164                 colorAttachmentReferences.push_back(createAttachmentReference<AttachmentRef>(subpass.getColorAttachments()[attachmentNdx]));
1165
1166         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
1167                 inputAttachmentReferences.push_back(createAttachmentReference<AttachmentRef>(subpass.getInputAttachments()[attachmentNdx]));
1168
1169         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getResolveAttachments().size(); attachmentNdx++)
1170                 resolveAttachmentReferences.push_back(createAttachmentReference<AttachmentRef>(subpass.getResolveAttachments()[attachmentNdx]));
1171
1172         depthStencilAttachmentReferences.push_back(createAttachmentReference<AttachmentRef>(subpass.getDepthStencilAttachment()));
1173
1174         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getPreserveAttachments().size(); attachmentNdx++)
1175                 preserveAttachmentReferences->push_back(subpass.getPreserveAttachments()[attachmentNdx]);
1176
1177         DE_ASSERT(resolveAttachmentReferences.empty() || colorAttachmentReferences.size() == resolveAttachmentReferences.size());
1178
1179         {
1180                 const SubpassDesc subpassDescription                                                                                                            //  VkSubpassDescription                                                                                ||  VkSubpassDescription2KHR
1181                 (
1182                                                                                                                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
1183                         DE_NULL,                                                                                                                                                                //                                                                                                                              ||  const void*                                                 pNext;
1184                         subpass.getFlags(),                                                                                                                                             //  VkSubpassDescriptionFlags           flags;                                          ||  VkSubpassDescriptionFlags                   flags;
1185                         subpass.getPipelineBindPoint(),                                                                                                                 //  VkPipelineBindPoint                         pipelineBindPoint;                      ||  VkPipelineBindPoint                                 pipelineBindPoint;
1186                         0u,                                                                                                                                                                             //                                                                                                                              ||  deUint32                                                    viewMask;
1187                         (deUint32)inputAttachmentReferences.size(),                                                                                             //  deUint32                                            inputAttachmentCount;           ||  deUint32                                                    inputAttachmentCount;
1188                         inputAttachmentReferences.empty() ? DE_NULL : &inputAttachmentReferences[0],                    //  const VkAttachmentReference*        pInputAttachments;                      ||  const VkAttachmentReference2KHR*    pInputAttachments;
1189                         (deUint32)colorAttachmentReferences.size(),                                                                                             //  deUint32                                            colorAttachmentCount;           ||  deUint32                                                    colorAttachmentCount;
1190                         colorAttachmentReferences.empty() ? DE_NULL :  &colorAttachmentReferences[0],                   //  const VkAttachmentReference*        pColorAttachments;                      ||  const VkAttachmentReference2KHR*    pColorAttachments;
1191                         resolveAttachmentReferences.empty() ? DE_NULL : &resolveAttachmentReferences[0],                //  const VkAttachmentReference*        pResolveAttachments;            ||  const VkAttachmentReference2KHR*    pResolveAttachments;
1192                         &depthStencilAttachmentReferences[0],                                                                                                   //  const VkAttachmentReference*        pDepthStencilAttachment;        ||  const VkAttachmentReference2KHR*    pDepthStencilAttachment;
1193                         (deUint32)preserveAttachmentReferences->size(),                                                                                 //  deUint32                                            preserveAttachmentCount;        ||  deUint32                                                    preserveAttachmentCount;
1194                         preserveAttachmentReferences->empty() ? DE_NULL : &(*preserveAttachmentReferences)[0]   //  const deUint32*                                     pPreserveAttachments;           ||  const deUint32*                                             pPreserveAttachments;
1195                 );
1196
1197                 return subpassDescription;
1198         }
1199 }
1200
1201 template <typename SubpassDep>
1202 SubpassDep createSubpassDependency (const SubpassDependency& dependencyInfo)
1203 {
1204         const SubpassDep        dependency                      //  VkSubpassDependency                                                                                 ||  VkSubpassDependency2KHR
1205         (
1206                                                                                         //                                                                                                                              ||      VkStructureType                                         sType;
1207                 DE_NULL,                                                        //                                                                                                                              ||      const void*                                                     pNext;
1208                 dependencyInfo.getSrcPass(),            //  deUint32                                            srcSubpass;                                     ||      deUint32                                                        srcSubpass;
1209                 dependencyInfo.getDstPass(),            //  deUint32                                            dstSubpass;                                     ||      deUint32                                                        dstSubpass;
1210                 dependencyInfo.getSrcStageMask(),       //  VkPipelineStageFlags                        srcStageMask;                           ||      VkPipelineStageFlags                            srcStageMask;
1211                 dependencyInfo.getDstStageMask(),       //  VkPipelineStageFlags                        dstStageMask;                           ||      VkPipelineStageFlags                            dstStageMask;
1212                 dependencyInfo.getSrcAccessMask(),      //  VkAccessFlags                                       srcAccessMask;                          ||      VkAccessFlags                                           srcAccessMask;
1213                 dependencyInfo.getDstAccessMask(),      //  VkAccessFlags                                       dstAccessMask;                          ||      VkAccessFlags                                           dstAccessMask;
1214                 dependencyInfo.getFlags(),                      //  VkDependencyFlags                           dependencyFlags;                        ||      VkDependencyFlags                                       dependencyFlags;
1215                 0u                                                                      //                                                                                                                              ||      deInt32                                                         viewOffset;
1216         );
1217
1218         return dependency;
1219 }
1220
1221 de::MovePtr<VkRenderPassInputAttachmentAspectCreateInfo> createRenderPassInputAttachmentAspectCreateInfo(const RenderPass& renderPassInfo)
1222 {
1223         de::MovePtr<VkRenderPassInputAttachmentAspectCreateInfo>        result  (DE_NULL);
1224
1225         if (!renderPassInfo.getInputAspects().empty())
1226         {
1227                 const VkRenderPassInputAttachmentAspectCreateInfo       inputAspectCreateInfo   =
1228                 {
1229                         VK_STRUCTURE_TYPE_RENDER_PASS_INPUT_ATTACHMENT_ASPECT_CREATE_INFO,
1230                         DE_NULL,
1231
1232                         (deUint32)renderPassInfo.getInputAspects().size(),
1233                         renderPassInfo.getInputAspects().data(),
1234                 };
1235
1236                 result = de::MovePtr<VkRenderPassInputAttachmentAspectCreateInfo>(new VkRenderPassInputAttachmentAspectCreateInfo(inputAspectCreateInfo));
1237         }
1238
1239         return result;
1240 }
1241
1242 template<typename AttachmentDesc, typename AttachmentRef, typename SubpassDesc, typename SubpassDep, typename RenderPassCreateInfo>
1243 Move<VkRenderPass> createRenderPass (const DeviceInterface&     vk,
1244                                                                          VkDevice                               device,
1245                                                                          const RenderPass&              renderPassInfo)
1246 {
1247         const size_t                                                                                            perSubpassAttachmentReferenceLists = 4;
1248         vector<AttachmentDesc>                                                                          attachments;
1249         vector<SubpassDesc>                                                                                     subpasses;
1250         vector<SubpassDep>                                                                                      dependencies;
1251         vector<vector<AttachmentRef> >                                                          attachmentReferenceLists(renderPassInfo.getSubpasses().size() * perSubpassAttachmentReferenceLists);
1252         vector<vector<deUint32> >                                                                       preserveAttachments(renderPassInfo.getSubpasses().size());
1253         de::MovePtr<VkRenderPassInputAttachmentAspectCreateInfo>        inputAspectCreateInfo(createRenderPassInputAttachmentAspectCreateInfo(renderPassInfo));
1254
1255         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
1256                 attachments.push_back(createAttachmentDescription<AttachmentDesc>(renderPassInfo.getAttachments()[attachmentNdx]));
1257
1258         for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); subpassNdx++)
1259                 subpasses.push_back(createSubpassDescription<SubpassDesc>(renderPassInfo.getSubpasses()[subpassNdx], &(attachmentReferenceLists[subpassNdx * perSubpassAttachmentReferenceLists]), &preserveAttachments[subpassNdx]));
1260
1261         for (size_t depNdx = 0; depNdx < renderPassInfo.getDependencies().size(); depNdx++)
1262                 dependencies.push_back(createSubpassDependency<SubpassDep>(renderPassInfo.getDependencies()[depNdx]));
1263
1264         const RenderPassCreateInfo      renderPassCreator                               //  VkRenderPassCreateInfo                                                                              ||  VkRenderPassCreateInfo2KHR
1265         (
1266                                                                                                                                 //  VkStructureType                                     sType;                                          ||  VkStructureType                                             sType;
1267                 inputAspectCreateInfo.get(),                                                    //  const void*                                         pNext;                                          ||  const void*                                                 pNext;
1268                 (VkRenderPassCreateFlags)0u,                                                    //  VkRenderPassCreateFlags                     flags;                                          ||  VkRenderPassCreateFlags                             flags;
1269                 (deUint32)attachments.size(),                                                   //  deUint32                                            attachmentCount;                        ||  deUint32                                                    attachmentCount;
1270                 (attachments.empty() ? DE_NULL : &attachments[0]),              //  const VkAttachmentDescription*      pAttachments;                           ||  const VkAttachmentDescription2KHR*  pAttachments;
1271                 (deUint32)subpasses.size(),                                                             //  deUint32                                            subpassCount;                           ||  deUint32                                                    subpassCount;
1272                 (subpasses.empty() ? DE_NULL : &subpasses[0]),                  //  const VkSubpassDescription*         pSubpasses;                                     ||  const VkSubpassDescription2KHR*             pSubpasses;
1273                 (deUint32)dependencies.size(),                                                  //  deUint32                                            dependencyCount;                        ||  deUint32                                                    dependencyCount;
1274                 (dependencies.empty() ? DE_NULL : &dependencies[0]),    //  const VkSubpassDependency*          pDependencies;                          ||  const VkSubpassDependency2KHR*              pDependencies;
1275                 0u,                                                                                                             //                                                                                                                              ||  deUint32                                                    correlatedViewMaskCount;
1276                 DE_NULL                                                                                                 //                                                                                                                              ||  const deUint32*                                             pCorrelatedViewMasks;
1277         );
1278
1279         return renderPassCreator.createRenderPass(vk, device);
1280 }
1281
1282 Move<VkRenderPass> createRenderPass (const DeviceInterface&     vk,
1283                                                                          VkDevice                               device,
1284                                                                          const RenderPass&              renderPassInfo,
1285                                                                          const RenderingType    renderPassType)
1286 {
1287         switch (renderPassType)
1288         {
1289                 case RENDERING_TYPE_RENDERPASS_LEGACY:
1290                         return createRenderPass<AttachmentDescription1, AttachmentReference1, SubpassDescription1, SubpassDependency1, RenderPassCreateInfo1>(vk, device, renderPassInfo);
1291                 case RENDERING_TYPE_RENDERPASS2:
1292                         return createRenderPass<AttachmentDescription2, AttachmentReference2, SubpassDescription2, SubpassDependency2, RenderPassCreateInfo2>(vk, device, renderPassInfo);
1293                 default:
1294                         TCU_THROW(InternalError, "Impossible");
1295         }
1296 }
1297
1298 Move<VkFramebuffer> createFramebuffer (const DeviceInterface&           vk,
1299                                                                            VkDevice                                             device,
1300                                                                            VkRenderPass                                 renderPass,
1301                                                                            const UVec2&                                 size,
1302                                                                            const vector<VkImageView>&   attachments)
1303 {
1304         return createFramebuffer(vk, device, 0u, renderPass, (deUint32)attachments.size(), attachments.empty() ? DE_NULL : &attachments[0], size.x(), size.y(), 1u);
1305 }
1306
1307 Move<VkImage> createAttachmentImage (const DeviceInterface&     vk,
1308                                                                          VkDevice                               device,
1309                                                                          deUint32                               queueIndex,
1310                                                                          const UVec2&                   size,
1311                                                                          VkFormat                               format,
1312                                                                          VkSampleCountFlagBits  samples,
1313                                                                          VkImageUsageFlags              usageFlags,
1314                                                                          VkImageLayout                  layout)
1315 {
1316         VkImageUsageFlags                       targetUsageFlags        = 0;
1317         const tcu::TextureFormat        textureFormat           = mapVkFormat(format);
1318
1319         DE_ASSERT(!(tcu::hasDepthComponent(vk::mapVkFormat(format).order) || tcu::hasStencilComponent(vk::mapVkFormat(format).order))
1320                                         || ((usageFlags & vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0));
1321
1322         DE_ASSERT((tcu::hasDepthComponent(vk::mapVkFormat(format).order) || tcu::hasStencilComponent(vk::mapVkFormat(format).order))
1323                                         || ((usageFlags & vk::VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0));
1324
1325         if (tcu::hasDepthComponent(textureFormat.order) || tcu::hasStencilComponent(textureFormat.order))
1326                 targetUsageFlags |= vk::VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1327         else
1328                 targetUsageFlags |= vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1329
1330         return createImage(vk, device,
1331                                            (VkImageCreateFlags)0,
1332                                            VK_IMAGE_TYPE_2D,
1333                                            format,
1334                                            vk::makeExtent3D(size.x(), size.y(), 1u),
1335                                            1u /* mipLevels */,
1336                                            1u /* arraySize */,
1337                                            samples,
1338                                            VK_IMAGE_TILING_OPTIMAL,
1339                                            usageFlags | targetUsageFlags,
1340                                            VK_SHARING_MODE_EXCLUSIVE,
1341                                            1,
1342                                            &queueIndex,
1343                                            layout);
1344 }
1345
1346 de::MovePtr<Allocation> createImageMemory (const InstanceInterface&     vki,
1347                                                                                    const VkPhysicalDevice&      vkd,
1348                                                                                    const DeviceInterface&       vk,
1349                                                                                    VkDevice                                     device,
1350                                                                                    Allocator&                           allocator,
1351                                                                                    VkImage                                      image,
1352                                                                                    bool                                         lazy,
1353                                                                                    AllocationKind                       allocationKind)
1354 {
1355         const MemoryRequirement memoryRequirement       = lazy ? MemoryRequirement::LazilyAllocated : MemoryRequirement::Any;
1356         de::MovePtr<Allocation> allocation                      = allocateImage(vki, vk, vkd, device, image, memoryRequirement, allocator, allocationKind);
1357
1358         bindImageMemory(vk, device, image, allocation->getMemory(), allocation->getOffset());
1359
1360         return allocation;
1361 }
1362
1363 Move<VkImageView> createImageAttachmentView (const DeviceInterface&     vk,
1364                                                                                          VkDevice                               device,
1365                                                                                          VkImage                                image,
1366                                                                                          VkFormat                               format,
1367                                                                                          VkImageAspectFlags             aspect)
1368 {
1369         const VkImageSubresourceRange range =
1370         {
1371                 aspect,
1372                 0,
1373                 1,
1374                 0,
1375                 1
1376         };
1377
1378         return createImageView(vk, device, 0u, image, VK_IMAGE_VIEW_TYPE_2D, format, makeComponentMappingRGBA(), range);
1379 }
1380
1381 VkClearValue randomClearValue (const Attachment& attachment, de::Random& rng, deBool useFormatCompCount, const DepthValuesArray& depthValues)
1382 {
1383         const float                                     clearNan        = tcu::Float32::nan().asFloat();
1384         const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
1385
1386         if (tcu::hasStencilComponent(format.order) || tcu::hasDepthComponent(format.order))
1387         {
1388                 VkClearValue clearValue;
1389
1390                 clearValue.depthStencil.depth   = clearNan;
1391                 clearValue.depthStencil.stencil = 0xCDu;
1392
1393                 if (tcu::hasStencilComponent(format.order))
1394                         clearValue.depthStencil.stencil = rng.getBool()
1395                                                                                         ? 0xFFu
1396                                                                                         : 0x0u;
1397
1398                 if (tcu::hasDepthComponent(format.order))
1399                         clearValue.depthStencil.depth   = float(depthValues[rng.getBool() ? 1 : 0]) / 255.0f;
1400
1401                 return clearValue;
1402         }
1403         else
1404         {
1405                 VkClearValue clearValue;
1406
1407                 clearValue.color = randomColorClearValue(attachment, rng, useFormatCompCount);
1408
1409                 return clearValue;
1410         }
1411 }
1412
1413 class AttachmentResources
1414 {
1415 public:
1416         AttachmentResources (const InstanceInterface&   vki,
1417                                                  const VkPhysicalDevice&        physDevice,
1418                                                  const DeviceInterface&         vk,
1419                                                  VkDevice                                       device,
1420                                                  Allocator&                                     allocator,
1421                                                  deUint32                                       queueIndex,
1422                                                  const UVec2&                           size,
1423                                                  const Attachment&                      attachmentInfo,
1424                                                  VkImageUsageFlags                      usageFlags,
1425                                                  const AllocationKind           allocationKind)
1426                 : m_image                       (createAttachmentImage(vk, device, queueIndex, size, attachmentInfo.getFormat(), attachmentInfo.getSamples(), usageFlags, VK_IMAGE_LAYOUT_UNDEFINED))
1427                 , m_imageMemory         (createImageMemory(vki, physDevice, vk, device, allocator, *m_image, ((usageFlags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) != 0), allocationKind))
1428                 , m_attachmentView      (createImageAttachmentView(vk, device, *m_image, attachmentInfo.getFormat(), getImageAspectFlags(attachmentInfo.getFormat())))
1429         {
1430                 const tcu::TextureFormat        format                  = mapVkFormat(attachmentInfo.getFormat());
1431                 const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
1432                 const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
1433
1434                 if (isDepthFormat && isStencilFormat)
1435                 {
1436                         m_depthInputAttachmentView              = createImageAttachmentView(vk, device, *m_image, attachmentInfo.getFormat(), VK_IMAGE_ASPECT_DEPTH_BIT);
1437                         m_stencilInputAttachmentView    = createImageAttachmentView(vk, device, *m_image, attachmentInfo.getFormat(), VK_IMAGE_ASPECT_STENCIL_BIT);
1438
1439                         m_inputAttachmentViews = std::make_pair(*m_depthInputAttachmentView, *m_stencilInputAttachmentView);
1440                 }
1441                 else
1442                         m_inputAttachmentViews = std::make_pair(*m_attachmentView, (vk::VkImageView)0u);
1443
1444                 if ((usageFlags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT) == 0)
1445                 {
1446                         if (tcu::hasDepthComponent(format.order) && tcu::hasStencilComponent(format.order))
1447                         {
1448                                 const tcu::TextureFormat        depthFormat             = getDepthCopyFormat(attachmentInfo.getFormat());
1449                                 const tcu::TextureFormat        stencilFormat   = getStencilCopyFormat(attachmentInfo.getFormat());
1450
1451                                 m_bufferSize                    = size.x() * size.y() * depthFormat.getPixelSize();
1452                                 m_secondaryBufferSize   = size.x() * size.y() * stencilFormat.getPixelSize();
1453
1454                                 m_buffer                                = createBuffer(vk, device, 0, m_bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE, 1, &queueIndex);
1455                                 m_bufferMemory                  = allocateBuffer(vki, vk, physDevice, device, *m_buffer, MemoryRequirement::HostVisible, allocator, allocationKind);
1456
1457                                 bindBufferMemory(vk, device, *m_buffer, m_bufferMemory->getMemory(), m_bufferMemory->getOffset());
1458
1459                                 m_secondaryBuffer               = createBuffer(vk, device, 0, m_secondaryBufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE, 1, &queueIndex);
1460                                 m_secondaryBufferMemory = allocateBuffer(vki, vk, physDevice, device, *m_secondaryBuffer, MemoryRequirement::HostVisible, allocator, allocationKind);
1461
1462                                 bindBufferMemory(vk, device, *m_secondaryBuffer, m_secondaryBufferMemory->getMemory(), m_secondaryBufferMemory->getOffset());
1463                         }
1464                         else
1465                         {
1466                                 m_bufferSize    = size.x() * size.y() * format.getPixelSize();
1467
1468                                 m_buffer                = createBuffer(vk, device, 0, m_bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE, 1, &queueIndex);
1469                                 m_bufferMemory  = allocateBuffer(vki, vk, physDevice, device, *m_buffer, MemoryRequirement::HostVisible, allocator, allocationKind);
1470
1471                                 bindBufferMemory(vk, device, *m_buffer, m_bufferMemory->getMemory(), m_bufferMemory->getOffset());
1472                         }
1473                 }
1474         }
1475
1476         const pair<VkImageView, VkImageView>& getInputAttachmentViews (void) const
1477         {
1478                 return m_inputAttachmentViews;
1479         }
1480
1481         ~AttachmentResources (void)
1482         {
1483         }
1484
1485         VkImageView getAttachmentView (void) const
1486         {
1487                 return *m_attachmentView;
1488         }
1489
1490         VkImage getImage (void) const
1491         {
1492                 return *m_image;
1493         }
1494
1495         VkBuffer getBuffer (void) const
1496         {
1497                 DE_ASSERT(*m_buffer != DE_NULL);
1498                 return *m_buffer;
1499         }
1500
1501         VkDeviceSize getBufferSize (void) const
1502         {
1503                 DE_ASSERT(*m_buffer != DE_NULL);
1504                 return m_bufferSize;
1505         }
1506
1507         const Allocation& getResultMemory (void) const
1508         {
1509                 DE_ASSERT(m_bufferMemory);
1510                 return *m_bufferMemory;
1511         }
1512
1513         VkBuffer getSecondaryBuffer (void) const
1514         {
1515                 DE_ASSERT(*m_secondaryBuffer != DE_NULL);
1516                 return *m_secondaryBuffer;
1517         }
1518
1519         VkDeviceSize getSecondaryBufferSize (void) const
1520         {
1521                 DE_ASSERT(*m_secondaryBuffer != DE_NULL);
1522                 return m_secondaryBufferSize;
1523         }
1524
1525         const Allocation& getSecondaryResultMemory (void) const
1526         {
1527                 DE_ASSERT(m_secondaryBufferMemory);
1528                 return *m_secondaryBufferMemory;
1529         }
1530
1531 private:
1532         const Unique<VkImage>                   m_image;
1533         const UniquePtr<Allocation>             m_imageMemory;
1534         const Unique<VkImageView>               m_attachmentView;
1535
1536         Move<VkImageView>                               m_depthInputAttachmentView;
1537         Move<VkImageView>                               m_stencilInputAttachmentView;
1538         pair<VkImageView, VkImageView>  m_inputAttachmentViews;
1539
1540         Move<VkBuffer>                                  m_buffer;
1541         VkDeviceSize                                    m_bufferSize;
1542         de::MovePtr<Allocation>                 m_bufferMemory;
1543
1544         Move<VkBuffer>                                  m_secondaryBuffer;
1545         VkDeviceSize                                    m_secondaryBufferSize;
1546         de::MovePtr<Allocation>                 m_secondaryBufferMemory;
1547 };
1548
1549 void uploadBufferData (const DeviceInterface&   vk,
1550                                            VkDevice                                     device,
1551                                            const Allocation&            memory,
1552                                            size_t                                       size,
1553                                            const void*                          data,
1554                                            VkDeviceSize                         nonCoherentAtomSize)
1555 {
1556         // Expand the range to flush to account for the nonCoherentAtomSize
1557         const VkDeviceSize roundedOffset        = de::roundDown(memory.getOffset(), nonCoherentAtomSize);
1558         const VkDeviceSize roundedSize          = de::roundUp(memory.getOffset() - roundedOffset + static_cast<VkDeviceSize>(size), nonCoherentAtomSize);
1559
1560         const VkMappedMemoryRange range =
1561         {
1562                 VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  // sType;
1563                 DE_NULL,                                                                // pNext;
1564                 memory.getMemory(),                                             // mem;
1565                 roundedOffset,                                                  // offset;
1566                 roundedSize,                                                    // size;
1567         };
1568         void* const ptr = memory.getHostPtr();
1569
1570         deMemcpy(ptr, data, size);
1571         VK_CHECK(vk.flushMappedMemoryRanges(device, 1, &range));
1572 }
1573
1574 VkImageAspectFlagBits getPrimaryImageAspect (tcu::TextureFormat::ChannelOrder order)
1575 {
1576         DE_STATIC_ASSERT(tcu::TextureFormat::CHANNELORDER_LAST == 22);
1577
1578         switch (order)
1579         {
1580                 case tcu::TextureFormat::D:
1581                 case tcu::TextureFormat::DS:
1582                         return VK_IMAGE_ASPECT_DEPTH_BIT;
1583
1584                 case tcu::TextureFormat::S:
1585                         return VK_IMAGE_ASPECT_STENCIL_BIT;
1586
1587                 default:
1588                         return VK_IMAGE_ASPECT_COLOR_BIT;
1589         }
1590 }
1591
1592 deUint32 getAttachmentNdx (const vector<AttachmentReference>& colorAttachments, size_t ndx)
1593 {
1594         return (colorAttachments[ndx].getAttachment() == VK_ATTACHMENT_UNUSED) ? (deUint32)ndx : colorAttachments[ndx].getAttachment();
1595 }
1596
1597 class RenderQuad
1598 {
1599 public:
1600                                         RenderQuad                      (const Vec2& posA, const Vec2& posB)
1601                 : m_vertices(6)
1602         {
1603                 m_vertices[0] = posA;
1604                 m_vertices[1] = Vec2(posA[0], posB[1]);
1605                 m_vertices[2] = posB;
1606
1607                 m_vertices[3] = posB;
1608                 m_vertices[4] = Vec2(posB[0], posA[1]);
1609                 m_vertices[5] = posA;
1610         }
1611
1612         const Vec2&             getCornerA                      (void) const
1613         {
1614                 return m_vertices[0];
1615         }
1616
1617         const Vec2&             getCornerB                      (void) const
1618         {
1619                 return m_vertices[2];
1620         }
1621
1622         const void*             getVertexPointer        (void) const
1623         {
1624                 return &m_vertices[0];
1625         }
1626
1627         size_t                  getVertexDataSize       (void) const
1628         {
1629                 return sizeof(Vec2) * m_vertices.size();
1630         }
1631
1632 private:
1633         vector<Vec2>    m_vertices;
1634 };
1635
1636 class ColorClear
1637 {
1638 public:
1639                                                                 ColorClear      (const UVec2&                           offset,
1640                                                                                          const UVec2&                           size,
1641                                                                                          const VkClearColorValue&       color)
1642                 : m_offset      (offset)
1643                 , m_size        (size)
1644                 , m_color       (color)
1645         {
1646         }
1647
1648         const UVec2&                            getOffset       (void) const { return m_offset; }
1649         const UVec2&                            getSize         (void) const { return m_size;   }
1650         const VkClearColorValue&        getColor        (void) const { return m_color;  }
1651
1652 private:
1653         UVec2                                           m_offset;
1654         UVec2                                           m_size;
1655         VkClearColorValue                       m_color;
1656 };
1657
1658 class DepthStencilClear
1659 {
1660 public:
1661                                         DepthStencilClear       (const UVec2&   offset,
1662                                                                                  const UVec2&   size,
1663                                                                                  float                  depth,
1664                                                                                  deUint32               stencil)
1665                 : m_offset      (offset)
1666                 , m_size        (size)
1667                 , m_depth       (depth)
1668                 , m_stencil     (stencil)
1669         {
1670         }
1671
1672         const UVec2&    getOffset                       (void) const { return m_offset;         }
1673         const UVec2&    getSize                         (void) const { return m_size;           }
1674         float                   getDepth                        (void) const { return m_depth;          }
1675         deUint32                getStencil                      (void) const { return m_stencil;        }
1676
1677 private:
1678         const UVec2             m_offset;
1679         const UVec2             m_size;
1680
1681         const float             m_depth;
1682         const deUint32  m_stencil;
1683 };
1684
1685 class SubpassRenderInfo
1686 {
1687 public:
1688                                                                         SubpassRenderInfo                               (const RenderPass&                                      renderPass,
1689                                                                                                                                          deUint32                                                       subpassIndex,
1690                                                                                                                                          deUint32                                                       drawStartNdx,
1691
1692                                                                                                                                          bool                                                           isSecondary_,
1693                                                                                                                                          bool                                                           omitBlendState_,
1694
1695                                                                                                                                          const UVec2&                                           viewportOffset,
1696                                                                                                                                          const UVec2&                                           viewportSize,
1697
1698                                                                                                                                          const Maybe<RenderQuad>&                       renderQuad,
1699                                                                                                                                          const vector<ColorClear>&                      colorClears,
1700                                                                                                                                          const Maybe<DepthStencilClear>&        depthStencilClear)
1701                 : m_viewportOffset              (viewportOffset)
1702                 , m_viewportSize                (viewportSize)
1703                 , m_subpassIndex                (subpassIndex)
1704                 , m_drawStartNdx                (drawStartNdx)
1705                 , m_isSecondary                 (isSecondary_)
1706                 , m_omitBlendState              (omitBlendState_)
1707                 , m_flags                               (renderPass.getSubpasses()[subpassIndex].getFlags())
1708                 , m_renderQuad                  (renderQuad)
1709                 , m_colorClears                 (colorClears)
1710                 , m_depthStencilClear   (depthStencilClear)
1711                 , m_colorAttachments    (renderPass.getSubpasses()[subpassIndex].getColorAttachments())
1712                 , m_inputAttachments    (renderPass.getSubpasses()[subpassIndex].getInputAttachments())
1713         {
1714                 for (deUint32 attachmentNdx = 0; attachmentNdx < (deUint32)m_colorAttachments.size(); attachmentNdx++)
1715                         m_colorAttachmentInfo.push_back(renderPass.getAttachments()[getAttachmentNdx(m_colorAttachments, attachmentNdx)]);
1716
1717                 if (renderPass.getSubpasses()[subpassIndex].getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
1718                 {
1719                         m_depthStencilAttachment                = tcu::just(renderPass.getSubpasses()[subpassIndex].getDepthStencilAttachment());
1720                         m_depthStencilAttachmentInfo    = tcu::just(renderPass.getAttachments()[renderPass.getSubpasses()[subpassIndex].getDepthStencilAttachment().getAttachment()]);
1721                 }
1722         }
1723
1724         const UVec2&                                    getViewportOffset                               (void) const { return m_viewportOffset;         }
1725         const UVec2&                                    getViewportSize                                 (void) const { return m_viewportSize;           }
1726
1727         deUint32                                                getSubpassIndex                                 (void) const { return m_subpassIndex;           }
1728         deUint32                                                getDrawStartNdx                                 (void) const { return m_drawStartNdx;           }
1729         bool                                                    isSecondary                                             (void) const { return m_isSecondary;            }
1730         bool                                                    getOmitBlendState                               (void) const { return m_omitBlendState;         }
1731
1732         const Maybe<RenderQuad>&                getRenderQuad                                   (void) const { return m_renderQuad;                     }
1733         const vector<ColorClear>&               getColorClears                                  (void) const { return m_colorClears;            }
1734         const Maybe<DepthStencilClear>& getDepthStencilClear                    (void) const { return m_depthStencilClear;      }
1735
1736         deUint32                                                getInputAttachmentCount                 (void) const { return (deUint32)m_inputAttachments.size(); }
1737         deUint32                                                getInputAttachmentIndex                 (deUint32 attachmentNdx) const { return m_inputAttachments[attachmentNdx].getAttachment(); }
1738         VkImageLayout                                   getInputAttachmentLayout                (deUint32 attachmentNdx) const { return m_inputAttachments[attachmentNdx].getImageLayout(); }
1739
1740         deUint32                                                getColorAttachmentCount                 (void) const { return (deUint32)m_colorAttachments.size(); }
1741         VkImageLayout                                   getColorAttachmentLayout                (deUint32 attachmentNdx) const { return m_colorAttachments[attachmentNdx].getImageLayout(); }
1742         deUint32                                                getColorAttachmentIndex                 (deUint32 attachmentNdx) const { return m_colorAttachments[attachmentNdx].getAttachment(); }
1743         const Attachment&                               getColorAttachment                              (deUint32 attachmentNdx) const { return m_colorAttachmentInfo[attachmentNdx]; }
1744         Maybe<VkImageLayout>                    getDepthStencilAttachmentLayout (void) const { return m_depthStencilAttachment ? tcu::just(m_depthStencilAttachment->getImageLayout()) : tcu::nothing<VkImageLayout>(); }
1745         Maybe<deUint32>                                 getDepthStencilAttachmentIndex  (void) const { return m_depthStencilAttachment ? tcu::just(m_depthStencilAttachment->getAttachment()) : tcu::nothing<deUint32>(); }
1746         const Maybe<Attachment>&                getDepthStencilAttachment               (void) const { return m_depthStencilAttachmentInfo; }
1747         VkSubpassDescriptionFlags               getSubpassFlags                                 (void) const { return m_flags; }
1748
1749 private:
1750         UVec2                                                   m_viewportOffset;
1751         UVec2                                                   m_viewportSize;
1752
1753         deUint32                                                m_subpassIndex;
1754         deUint32                                                m_drawStartNdx;
1755         bool                                                    m_isSecondary;
1756         bool                                                    m_omitBlendState;
1757         VkSubpassDescriptionFlags               m_flags;
1758
1759         Maybe<RenderQuad>                               m_renderQuad;
1760         vector<ColorClear>                              m_colorClears;
1761         Maybe<DepthStencilClear>                m_depthStencilClear;
1762
1763         vector<AttachmentReference>             m_colorAttachments;
1764         vector<Attachment>                              m_colorAttachmentInfo;
1765
1766         Maybe<AttachmentReference>              m_depthStencilAttachment;
1767         Maybe<Attachment>                               m_depthStencilAttachmentInfo;
1768
1769         vector<AttachmentReference>             m_inputAttachments;
1770 };
1771
1772 void beginCommandBuffer (const DeviceInterface&                 vk,
1773                                                  VkCommandBuffer                                cmdBuffer,
1774                                                  VkCommandBufferUsageFlags              pBeginInfo_flags,
1775                                                  VkRenderPass                                   pInheritanceInfo_renderPass,
1776                                                  deUint32                                               pInheritanceInfo_subpass,
1777                                                  VkFramebuffer                                  pInheritanceInfo_framebuffer,
1778                                                  VkBool32                                               pInheritanceInfo_occlusionQueryEnable,
1779                                                  VkQueryControlFlags                    pInheritanceInfo_queryFlags,
1780                                                  VkQueryPipelineStatisticFlags  pInheritanceInfo_pipelineStatistics,
1781                                                  const SubpassRenderInfo*               pRenderInfo = 0,
1782                                                  bool                                                   dynamicRenderPass = false )
1783 {
1784         VkCommandBufferInheritanceInfo pInheritanceInfo =
1785         {
1786                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
1787                 DE_NULL,
1788                 pInheritanceInfo_renderPass,
1789                 pInheritanceInfo_subpass,
1790                 pInheritanceInfo_framebuffer,
1791                 pInheritanceInfo_occlusionQueryEnable,
1792                 pInheritanceInfo_queryFlags,
1793                 pInheritanceInfo_pipelineStatistics,
1794         };
1795         std::vector<vk::VkFormat> colorAttachmentFormats;
1796         VkCommandBufferInheritanceRenderingInfoKHR inheritanceRenderingInfo
1797         {
1798                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_RENDERING_INFO_KHR,
1799                 DE_NULL,
1800                 0u,
1801                 0u,
1802                 0u,
1803                 DE_NULL,
1804                 VK_FORMAT_UNDEFINED,
1805                 VK_FORMAT_UNDEFINED,
1806                 VK_SAMPLE_COUNT_1_BIT,
1807         };
1808         if (pRenderInfo)
1809         {
1810                 for (deUint32 i = 0; i < pRenderInfo->getColorAttachmentCount(); ++i)
1811                         colorAttachmentFormats.push_back(pRenderInfo->getColorAttachment(i).getFormat());
1812
1813                 inheritanceRenderingInfo.colorAttachmentCount = static_cast<deUint32>(colorAttachmentFormats.size());
1814                 inheritanceRenderingInfo.pColorAttachmentFormats = colorAttachmentFormats.data();
1815                 if (pRenderInfo->getDepthStencilAttachment())
1816                 {
1817                         const VkFormat dsFormat = pRenderInfo->getDepthStencilAttachment()->getFormat();
1818                         inheritanceRenderingInfo.depthAttachmentFormat          = tcu::hasDepthComponent(mapVkFormat(dsFormat).order) ? dsFormat : VK_FORMAT_UNDEFINED;
1819                         inheritanceRenderingInfo.stencilAttachmentFormat        = tcu::hasStencilComponent(mapVkFormat(dsFormat).order) ? dsFormat : VK_FORMAT_UNDEFINED;
1820                 }
1821                 if (pRenderInfo->getColorAttachmentCount())
1822                         inheritanceRenderingInfo.rasterizationSamples = pRenderInfo->getColorAttachment(0).getSamples();
1823                 else if (pRenderInfo->getDepthStencilAttachment())
1824                         inheritanceRenderingInfo.rasterizationSamples = pRenderInfo->getDepthStencilAttachment()->getSamples();
1825
1826                 if (dynamicRenderPass)
1827                         pInheritanceInfo.pNext = &inheritanceRenderingInfo;
1828         }
1829         const VkCommandBufferBeginInfo pBeginInfo =
1830         {
1831                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1832                 DE_NULL,
1833                 pBeginInfo_flags,
1834                 &pInheritanceInfo,
1835         };
1836         VK_CHECK(vk.beginCommandBuffer(cmdBuffer, &pBeginInfo));
1837 }
1838
1839 Move<VkPipeline> createSubpassPipeline (const DeviceInterface&          vk,
1840                                                                                 VkDevice                                        device,
1841                                                                                 VkRenderPass                            renderPass,
1842                                                                                 VkShaderModule                          vertexShaderModule,
1843                                                                                 VkShaderModule                          fragmentShaderModule,
1844                                                                                 VkPipelineLayout                        pipelineLayout,
1845                                                                                 const SubpassRenderInfo&        renderInfo)
1846 {
1847         Maybe<VkSampleCountFlagBits>                                    rasterSamples;
1848         vector<VkPipelineColorBlendAttachmentState>             attachmentBlendStates;
1849
1850         for (deUint32 attachmentNdx = 0; attachmentNdx < renderInfo.getColorAttachmentCount(); attachmentNdx++)
1851         {
1852                 const Attachment& attachment = renderInfo.getColorAttachment(attachmentNdx);
1853
1854                 DE_ASSERT(!rasterSamples || *rasterSamples == attachment.getSamples());
1855
1856                 rasterSamples = attachment.getSamples();
1857
1858                 {
1859                         const VkPipelineColorBlendAttachmentState attachmentBlendState =
1860                         {
1861                                 VK_FALSE,                                                                                                                                                                                                       // blendEnable
1862                                 VK_BLEND_FACTOR_SRC_ALPHA,                                                                                                                                                                      // srcBlendColor
1863                                 VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,                                                                                                                                            // destBlendColor
1864                                 VK_BLEND_OP_ADD,                                                                                                                                                                                        // blendOpColor
1865                                 VK_BLEND_FACTOR_ONE,                                                                                                                                                                            // srcBlendAlpha
1866                                 VK_BLEND_FACTOR_ONE,                                                                                                                                                                            // destBlendAlpha
1867                                 VK_BLEND_OP_ADD,                                                                                                                                                                                        // blendOpAlpha
1868                                 (attachmentNdx < renderInfo.getDrawStartNdx() ? (deUint32)0 :
1869                                         VK_COLOR_COMPONENT_R_BIT|VK_COLOR_COMPONENT_G_BIT|VK_COLOR_COMPONENT_B_BIT|VK_COLOR_COMPONENT_A_BIT)    // channelWriteMask
1870                         };
1871
1872                         attachmentBlendStates.push_back(attachmentBlendState);
1873                 }
1874         }
1875
1876         if (renderInfo.getDepthStencilAttachment())
1877         {
1878                 const Attachment& attachment = *renderInfo.getDepthStencilAttachment();
1879
1880                 DE_ASSERT(!rasterSamples || *rasterSamples == attachment.getSamples());
1881                 rasterSamples = attachment.getSamples();
1882         }
1883
1884         // If there are no attachment use single sample
1885         if (!rasterSamples)
1886                 rasterSamples = VK_SAMPLE_COUNT_1_BIT;
1887
1888         const VkVertexInputBindingDescription                   vertexBinding           =
1889         {
1890                 0u,                                                                                                                     // binding
1891                 (deUint32)sizeof(tcu::Vec2),                                                            // strideInBytes
1892                 VK_VERTEX_INPUT_RATE_VERTEX,                                                            // stepRate
1893         };
1894
1895         const VkVertexInputAttributeDescription                 vertexAttrib            =
1896         {
1897                 0u,                                                                                                                     // location
1898                 0u,                                                                                                                     // binding
1899                 VK_FORMAT_R32G32_SFLOAT,                                                                        // format
1900                 0u,                                                                                                                     // offsetInBytes
1901         };
1902
1903         const VkPipelineVertexInputStateCreateInfo              vertexInputState        =
1904         {
1905                 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,      //      sType
1906                 DE_NULL,                                                                                                        //      pNext
1907                 (VkPipelineVertexInputStateCreateFlags)0u,
1908                 1u,                                                                                                                     //      bindingCount
1909                 &vertexBinding,                                                                                         //      pVertexBindingDescriptions
1910                 1u,                                                                                                                     //      attributeCount
1911                 &vertexAttrib,                                                                                          //      pVertexAttributeDescriptions
1912         };
1913
1914         const VkPipelineInputAssemblyStateCreateInfo    inputAssemblyState      =
1915         {
1916                 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                            sType
1917                 DE_NULL,                                                                                                                // const void*                                pNext
1918                 0u,                                                                                                                             // VkPipelineInputAssemblyStateCreateFlags    flags
1919                 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,                                                    // VkPrimitiveTopology                        topology
1920                 VK_FALSE                                                                                                                // VkBool32                                   primitiveRestartEnable
1921         };
1922
1923         const VkViewport                                                                viewport                        =
1924         {
1925                 (float)renderInfo.getViewportOffset().x(),      (float)renderInfo.getViewportOffset().y(),
1926                 (float)renderInfo.getViewportSize().x(),        (float)renderInfo.getViewportSize().y(),
1927                 0.0f, 1.0f
1928         };
1929
1930         const VkRect2D                                                                  scissor                         =
1931         {
1932                 { (deInt32)renderInfo.getViewportOffset().x(),  (deInt32)renderInfo.getViewportOffset().y() },
1933                 { renderInfo.getViewportSize().x(),                             renderInfo.getViewportSize().y() }
1934         };
1935
1936         const VkPipelineViewportStateCreateInfo                 viewportState           =
1937         {
1938                 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,  // VkStructureType                             sType
1939                 DE_NULL,                                                                                                // const void*                                 pNext
1940                 (VkPipelineViewportStateCreateFlags)0,                                  // VkPipelineViewportStateCreateFlags          flags
1941                 1u,                                                                                                             // deUint32                                    viewportCount
1942                 &viewport,                                                                                              // const VkViewport*                           pViewports
1943                 1u,                                                                                                             // deUint32                                    scissorCount
1944                 &scissor                                                                                                // const VkRect2D*                             pScissors
1945         };
1946
1947         const VkPipelineRasterizationStateCreateInfo    rasterizationState      =
1948         {
1949                 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,     // VkStructureType                            sType
1950                 DE_NULL,                                                                                                        // const void*                                pNext
1951                 0u,                                                                                                                     // VkPipelineRasterizationStateCreateFlags    flags
1952                 VK_FALSE,                                                                                                       // VkBool32                                   depthClampEnable
1953                 VK_FALSE,                                                                                                       // VkBool32                                   rasterizerDiscardEnable
1954                 VK_POLYGON_MODE_FILL,                                                                           // VkPolygonMode                              polygonMode
1955                 VK_CULL_MODE_NONE,                                                                                      // VkCullModeFlags                            cullMode
1956                 VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                        // VkFrontFace                                frontFace
1957                 VK_FALSE,                                                                                                       // VkBool32                                   depthBiasEnable
1958                 0.0f,                                                                                                           // float                                      depthBiasConstantFactor
1959                 0.0f,                                                                                                           // float                                      depthBiasClamp
1960                 0.0f,                                                                                                           // float                                      depthBiasSlopeFactor
1961                 1.0f                                                                                                            // float                                      lineWidth
1962         };
1963
1964         const VkPipelineMultisampleStateCreateInfo              multisampleState        =
1965         {
1966                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,               // sType
1967                 DE_NULL,                                                                                                                // pNext
1968                 (VkPipelineMultisampleStateCreateFlags)0u,
1969                 *rasterSamples,                                                                                                 // rasterSamples
1970                 VK_FALSE,                                                                                                               // sampleShadingEnable
1971                 0.0f,                                                                                                                   // minSampleShading
1972                 DE_NULL,                                                                                                                // pSampleMask
1973                 VK_FALSE,                                                                                                               // alphaToCoverageEnable
1974                 VK_FALSE,                                                                                                               // alphaToOneEnable
1975         };
1976         const size_t    stencilIndex    = renderInfo.getSubpassIndex();
1977
1978         const VkBool32  writeDepth              = renderInfo.getDepthStencilAttachmentLayout()
1979                                                                                 && *renderInfo.getDepthStencilAttachmentLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
1980                                                                                 && *renderInfo.getDepthStencilAttachmentLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
1981                                                                         ? VK_TRUE
1982                                                                         : VK_FALSE;
1983
1984         const VkBool32  writeStencil    = renderInfo.getDepthStencilAttachmentLayout()
1985                                                                                 && *renderInfo.getDepthStencilAttachmentLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
1986                                                                                 && *renderInfo.getDepthStencilAttachmentLayout() != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
1987                                                                         ? VK_TRUE
1988                                                                         : VK_FALSE;
1989
1990         VkStencilOp             stencilOp               = writeStencil ? VK_STENCIL_OP_REPLACE : VK_STENCIL_OP_KEEP;
1991
1992         const VkPipelineDepthStencilStateCreateInfo depthStencilState =
1993         {
1994                 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // sType
1995                 DE_NULL,                                                                                                        // pNext
1996                 (VkPipelineDepthStencilStateCreateFlags)0u,
1997                 writeDepth,                                                                                                     // depthTestEnable
1998                 writeDepth,                                                                                                     // depthWriteEnable
1999                 VK_COMPARE_OP_ALWAYS,                                                                           // depthCompareOp
2000                 VK_FALSE,                                                                                                       // depthBoundsEnable
2001                 writeStencil,                                                                                           // stencilTestEnable
2002                 {
2003                         stencilOp,                                                                                              // stencilFailOp
2004                         stencilOp,                                                                                              // stencilPassOp
2005                         stencilOp,                                                                                              // stencilDepthFailOp
2006                         VK_COMPARE_OP_ALWAYS,                                                                   // stencilCompareOp
2007                         ~0u,                                                                                                    // stencilCompareMask
2008                         ~0u,                                                                                                    // stencilWriteMask
2009                         ((stencilIndex % 2) == 0) ? ~0x0u : 0x0u                                // stencilReference
2010                 },                                                                                                                      // front
2011                 {
2012                         stencilOp,                                                                                              // stencilFailOp
2013                         stencilOp,                                                                                              // stencilPassOp
2014                         stencilOp,                                                                                              // stencilDepthFailOp
2015                         VK_COMPARE_OP_ALWAYS,                                                                   // stencilCompareOp
2016                         ~0u,                                                                                                    // stencilCompareMask
2017                         ~0u,                                                                                                    // stencilWriteMask
2018                         ((stencilIndex % 2) == 0) ? ~0x0u : 0x0u                                // stencilReference
2019                 },                                                                                                                      // back
2020
2021                 0.0f,                                                                                                           // minDepthBounds;
2022                 1.0f                                                                                                            // maxDepthBounds;
2023         };
2024
2025         const VkPipelineColorBlendStateCreateInfo blendState =
2026         {
2027                 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,                       // sType
2028                 DE_NULL,                                                                                                                        // pNext
2029                 (VkPipelineColorBlendStateCreateFlags)0u,
2030                 VK_FALSE,                                                                                                                       // logicOpEnable
2031                 VK_LOGIC_OP_COPY,                                                                                                       // logicOp
2032                 (deUint32)attachmentBlendStates.size(),                                                         // attachmentCount
2033                 attachmentBlendStates.empty() ? DE_NULL : &attachmentBlendStates[0],// pAttachments
2034                 { 0.0f, 0.0f, 0.0f, 0.0f }                                                                                      // blendConst
2035         };
2036
2037         std::vector<vk::VkFormat> colorAttachmentFormats;
2038         for (deUint32 i = 0; i < renderInfo.getColorAttachmentCount(); ++i)
2039                 colorAttachmentFormats.push_back(renderInfo.getColorAttachment(i).getFormat());
2040
2041         vk::VkFormat depthFormat = VK_FORMAT_UNDEFINED;
2042         vk::VkFormat stencilFormat = VK_FORMAT_UNDEFINED;
2043         if (renderInfo.getDepthStencilAttachment())
2044         {
2045                 const Attachment& attachment = *renderInfo.getDepthStencilAttachment();
2046                 vk::VkFormat depthStencilFormat = attachment.getFormat();
2047                 if (depthStencilFormat != VK_FORMAT_UNDEFINED)
2048                 {
2049                         if (tcu::hasDepthComponent(mapVkFormat(depthStencilFormat).order))
2050                         {
2051                                 depthFormat = depthStencilFormat;
2052                         }
2053                         if (tcu::hasStencilComponent(mapVkFormat(depthStencilFormat).order))
2054                         {
2055                                 stencilFormat = depthStencilFormat;
2056                         }
2057                 }
2058         }
2059
2060
2061         VkPipelineRenderingCreateInfoKHR renderingCreateInfo
2062         {
2063                 VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
2064                 DE_NULL,
2065                 0u,
2066                 static_cast<deUint32>(colorAttachmentFormats.size()),
2067                 colorAttachmentFormats.data(),
2068                 depthFormat,
2069                 stencilFormat
2070         };
2071
2072         return makeGraphicsPipeline(vk,                                                                                         // const DeviceInterface&                        vk
2073                                                                 device,                                                                                 // const VkDevice                                device
2074                                                                 pipelineLayout,                                                                 // const VkPipelineLayout                        pipelineLayout
2075                                                                 vertexShaderModule,                                                             // const VkShaderModule                          vertexShaderModule
2076                                                                 DE_NULL,                                                                                // const VkShaderModule                          tessellationControlShaderModule
2077                                                                 DE_NULL,                                                                                // const VkShaderModule                          tessellationEvalShaderModule
2078                                                                 DE_NULL,                                                                                // const VkShaderModule                          geometryShaderModule
2079                                                                 fragmentShaderModule,                                                   // const VkShaderModule                          fragmentShaderModule
2080                                                                 renderPass,                                                                             // const VkRenderPass                            renderPass
2081                                                                 renderInfo.getSubpassIndex(),                                   // const deUint32                                subpass
2082                                                                 &vertexInputState,                                                              // const VkPipelineVertexInputStateCreateInfo*   vertexInputStateCreateInfo
2083                                                                 &inputAssemblyState,                                                    // const VkPipelineInputAssemblyStateCreateInfo* pInputAssemblyState;
2084                                                                 DE_NULL,                                                                                // const VkPipelineRasterizationStateCreateInfo* rasterizationStateCreateInfo
2085                                                                 &viewportState,                                                                 // const VkPipelineViewportStateCreateInfo*      pViewportStat;
2086                                                                 &rasterizationState,                                                    // const VkPipelineRasterizationStateCreateInfo* pRasterizationState
2087                                                                 &multisampleState,                                                              // const VkPipelineMultisampleStateCreateInfo*   multisampleStateCreateInfo
2088                                                                 &depthStencilState,                                                             // const VkPipelineDepthStencilStateCreateInfo*  depthStencilStateCreateInfo
2089                                                                 renderInfo.getOmitBlendState()
2090                                                                         ? DE_NULL : &blendState,                                        // const VkPipelineColorBlendStateCreateInfo*    colorBlendStateCreateInfo
2091                                                                 DE_NULL,                                                                                // const VkPipelineDynamicStateCreateInfo*       dynamicStateCreateInfo
2092                                                                 (renderPass == DE_NULL)
2093                                                                         ? &renderingCreateInfo : DE_NULL);                      // const void*                                   pNext)
2094 }
2095
2096 class SubpassRenderer
2097 {
2098 public:
2099         SubpassRenderer (Context&                                                                               context,
2100                                          const DeviceInterface&                                                 vk,
2101                                          VkDevice                                                                               device,
2102                                          Allocator&                                                                             allocator,
2103                                          VkRenderPass                                                                   renderPass,
2104                                          VkFramebuffer                                                                  framebuffer,
2105                                          VkCommandPool                                                                  commandBufferPool,
2106                                          deUint32                                                                               queueFamilyIndex,
2107                                          const vector<VkImage>&                                                 attachmentImages,
2108                                          const vector<pair<VkImageView, VkImageView> >& attachmentViews,
2109                                          const SubpassRenderInfo&                                               renderInfo,
2110                                          const vector<Attachment>&                                              attachmentInfos,
2111                                          const AllocationKind                                                   allocationKind,
2112                                          const bool                                                                             dynamicRendering)
2113                 : m_renderInfo  (renderInfo)
2114         {
2115                 const InstanceInterface&                                vki                             = context.getInstanceInterface();
2116                 const VkPhysicalDevice&                                 physDevice              = context.getPhysicalDevice();
2117                 const deUint32                                                  subpassIndex    = renderInfo.getSubpassIndex();
2118                 vector<VkDescriptorSetLayoutBinding>    bindings;
2119
2120                 for (deUint32 colorAttachmentNdx = 0; colorAttachmentNdx < renderInfo.getColorAttachmentCount();  colorAttachmentNdx++)
2121                 {
2122                         const deUint32 attachmentNdx    = (renderInfo.getColorAttachmentIndex(colorAttachmentNdx) == VK_ATTACHMENT_UNUSED) ? colorAttachmentNdx
2123                                                                                         : renderInfo.getColorAttachmentIndex(colorAttachmentNdx);
2124
2125                         m_colorAttachmentImages.push_back(attachmentImages[attachmentNdx]);
2126                 }
2127
2128                 if (renderInfo.getDepthStencilAttachmentIndex())
2129                         m_depthStencilAttachmentImage = attachmentImages[*renderInfo.getDepthStencilAttachmentIndex()];
2130
2131                 if (renderInfo.getRenderQuad())
2132                 {
2133                         const RenderQuad&       renderQuad      = *renderInfo.getRenderQuad();
2134
2135                         if (renderInfo.getInputAttachmentCount() > 0)
2136                         {
2137                                 deUint32                                                                bindingIndex    = 0;
2138
2139                                 for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2140                                 {
2141                                         const Attachment                        attachmentInfo  = attachmentInfos[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)];
2142                                         const VkImageLayout                     layout                  = renderInfo.getInputAttachmentLayout(inputAttachmentNdx);
2143                                         const tcu::TextureFormat        format                  = mapVkFormat(attachmentInfo.getFormat());
2144                                         const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
2145                                         const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
2146                                         const deUint32                          bindingCount    = (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2147                                                                                                                                         && (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
2148                                                                                                                                 ? 2u
2149                                                                                                                                 : 1u;
2150
2151                                         for (deUint32 bindingNdx = 0; bindingNdx < bindingCount; bindingNdx++)
2152                                         {
2153                                                 const VkDescriptorSetLayoutBinding binding =
2154                                                 {
2155                                                         bindingIndex,
2156                                                         vk::VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2157                                                         1u,
2158                                                         vk::VK_SHADER_STAGE_FRAGMENT_BIT,
2159                                                         DE_NULL
2160                                                 };
2161
2162                                                 bindings.push_back(binding);
2163                                                 bindingIndex++;
2164                                         }
2165                                 }
2166
2167                                 const VkDescriptorSetLayoutCreateInfo createInfo =
2168                                 {
2169                                         vk::VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
2170                                         DE_NULL,
2171
2172                                         0u,
2173                                         (deUint32)bindings.size(),
2174                                         &bindings[0]
2175                                 };
2176
2177                                 m_descriptorSetLayout = vk::createDescriptorSetLayout(vk, device, &createInfo);
2178                         }
2179
2180                         const VkDescriptorSetLayout                     descriptorSetLayout             = *m_descriptorSetLayout;
2181                         const VkPipelineLayoutCreateInfo        pipelineLayoutParams    =
2182                         {
2183                                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,                  // sType;
2184                                 DE_NULL,                                                                                                // pNext;
2185                                 (vk::VkPipelineLayoutCreateFlags)0,
2186                                 m_descriptorSetLayout ? 1u :0u ,                                                // setLayoutCount;
2187                                 m_descriptorSetLayout ? &descriptorSetLayout : DE_NULL, // pSetLayouts;
2188                                 0u,                                                                                                             // pushConstantRangeCount;
2189                                 DE_NULL,                                                                                                // pPushConstantRanges;
2190                         };
2191
2192                         m_vertexShaderModule    = createShaderModule(vk, device, context.getBinaryCollection().get(de::toString(subpassIndex) + "-vert"), 0u);
2193                         m_fragmentShaderModule  = createShaderModule(vk, device, context.getBinaryCollection().get(de::toString(subpassIndex) + "-frag"), 0u);
2194                         m_pipelineLayout                = createPipelineLayout(vk, device, &pipelineLayoutParams);
2195                         m_pipeline                              = createSubpassPipeline(vk, device, renderPass, *m_vertexShaderModule, *m_fragmentShaderModule, *m_pipelineLayout, m_renderInfo);
2196
2197                         // Round up the vertex buffer size to honor nonCoherentAtomSize.
2198                         const auto      properties                      = vk::getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice());
2199                         const auto      vertexBufferSize        = de::roundUp(static_cast<VkDeviceSize>(renderQuad.getVertexDataSize()), properties.limits.nonCoherentAtomSize);
2200
2201                         m_vertexBuffer                  = createBuffer(vk, device, 0u, vertexBufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE, 1u, &queueFamilyIndex);
2202                         m_vertexBufferMemory    = allocateBuffer(vki, vk, physDevice, device, *m_vertexBuffer, MemoryRequirement::HostVisible, allocator, allocationKind);
2203
2204                         bindBufferMemory(vk, device, *m_vertexBuffer, m_vertexBufferMemory->getMemory(), m_vertexBufferMemory->getOffset());
2205
2206                         uploadBufferData(vk, device, *m_vertexBufferMemory, renderQuad.getVertexDataSize(), renderQuad.getVertexPointer(), properties.limits.nonCoherentAtomSize);
2207
2208                         if (renderInfo.getInputAttachmentCount() > 0)
2209                         {
2210                                 {
2211                                         const VkDescriptorPoolSize poolSize =
2212                                         {
2213                                                 vk::VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2214                                                 // \note Reserve 2 per input attachment since depthStencil attachments require 2.
2215                                                 renderInfo.getInputAttachmentCount() * 2u
2216                                         };
2217                                         const VkDescriptorPoolCreateInfo createInfo =
2218                                         {
2219                                                 vk::VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
2220                                                 DE_NULL,
2221                                                 VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
2222
2223                                                 // \note Reserve 2 per input attachment since depthStencil attachments require 2.
2224                                                 renderInfo.getInputAttachmentCount() * 2u,
2225                                                 1u,
2226                                                 &poolSize
2227                                         };
2228
2229                                         m_descriptorPool = vk::createDescriptorPool(vk, device, &createInfo);
2230                                 }
2231                                 {
2232                                         const VkDescriptorSetAllocateInfo       allocateInfo =
2233                                         {
2234                                                 vk::VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2235                                                 DE_NULL,
2236
2237                                                 *m_descriptorPool,
2238                                                 1u,
2239                                                 &descriptorSetLayout
2240                                         };
2241
2242                                         m_descriptorSet = vk::allocateDescriptorSet(vk, device, &allocateInfo);
2243                                 }
2244                                 {
2245                                         vector<VkWriteDescriptorSet>    writes                  (bindings.size());
2246                                         vector<VkDescriptorImageInfo>   imageInfos              (bindings.size());
2247                                         deUint32                                                bindingIndex    = 0;
2248
2249                                         for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2250                                         {
2251                                                 const Attachment                        attachmentInfo                  = attachmentInfos[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)];
2252                                                 const tcu::TextureFormat        format                                  = mapVkFormat(attachmentInfo.getFormat());
2253                                                 const bool                                      isDepthFormat                   = tcu::hasDepthComponent(format.order);
2254                                                 const bool                                      isStencilFormat                 = tcu::hasStencilComponent(format.order);
2255                                                 const VkImageLayout                     inputAttachmentLayout   = renderInfo.getInputAttachmentLayout(inputAttachmentNdx);
2256
2257
2258                                                 if (isDepthFormat && isStencilFormat)
2259                                                 {
2260                                                         if (inputAttachmentLayout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
2261                                                         {
2262                                                                 const VkDescriptorImageInfo     imageInfo =
2263                                                                 {
2264                                                                         (VkSampler)0,
2265                                                                         attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].first,
2266                                                                         inputAttachmentLayout
2267                                                                 };
2268                                                                 imageInfos[bindingIndex] = imageInfo;
2269
2270                                                                 {
2271                                                                         const VkWriteDescriptorSet      write =
2272                                                                         {
2273                                                                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2274                                                                                 DE_NULL,
2275
2276                                                                                 *m_descriptorSet,
2277                                                                                 bindingIndex,
2278                                                                                 0u,
2279                                                                                 1u,
2280                                                                                 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2281                                                                                 &imageInfos[bindingIndex],
2282                                                                                 DE_NULL,
2283                                                                                 DE_NULL
2284                                                                         };
2285                                                                         writes[bindingIndex] = write;
2286
2287                                                                         bindingIndex++;
2288                                                                 }
2289                                                         }
2290
2291                                                         if (inputAttachmentLayout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2292                                                         {
2293                                                                 const VkDescriptorImageInfo     imageInfo =
2294                                                                 {
2295                                                                         (VkSampler)0,
2296                                                                         attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].second,
2297                                                                         inputAttachmentLayout
2298                                                                 };
2299                                                                 imageInfos[bindingIndex] = imageInfo;
2300
2301                                                                 {
2302                                                                         const VkWriteDescriptorSet      write =
2303                                                                         {
2304                                                                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2305                                                                                 DE_NULL,
2306
2307                                                                                 *m_descriptorSet,
2308                                                                                 bindingIndex,
2309                                                                                 0u,
2310                                                                                 1u,
2311                                                                                 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2312                                                                                 &imageInfos[bindingIndex],
2313                                                                                 DE_NULL,
2314                                                                                 DE_NULL
2315                                                                         };
2316                                                                         writes[bindingIndex] = write;
2317
2318                                                                         bindingIndex++;
2319                                                                 }
2320                                                         }
2321                                                 }
2322                                                 else
2323                                                 {
2324                                                         const VkDescriptorImageInfo     imageInfo =
2325                                                         {
2326                                                                 (VkSampler)0,
2327                                                                 attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].first,
2328                                                                 inputAttachmentLayout
2329                                                         };
2330                                                         imageInfos[bindingIndex] = imageInfo;
2331
2332                                                         {
2333                                                                 const VkWriteDescriptorSet      write =
2334                                                                 {
2335                                                                         VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2336                                                                         DE_NULL,
2337
2338                                                                         *m_descriptorSet,
2339                                                                         bindingIndex,
2340                                                                         0u,
2341                                                                         1u,
2342                                                                         VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2343                                                                         &imageInfos[bindingIndex],
2344                                                                         DE_NULL,
2345                                                                         DE_NULL
2346                                                                 };
2347                                                                 writes[bindingIndex] = write;
2348
2349                                                                 bindingIndex++;
2350                                                         }
2351                                                 }
2352                                         }
2353
2354                                         vk.updateDescriptorSets(device, (deUint32)writes.size(), &writes[0], 0u, DE_NULL);
2355                                 }
2356                         }
2357                 }
2358
2359                 if (renderInfo.isSecondary())
2360                 {
2361                         m_commandBuffer = allocateCommandBuffer(vk, device, commandBufferPool, VK_COMMAND_BUFFER_LEVEL_SECONDARY);
2362
2363                         beginCommandBuffer(vk, *m_commandBuffer, vk::VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, renderPass, subpassIndex, framebuffer, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0, &renderInfo, dynamicRendering);
2364                         pushRenderCommands(vk, *m_commandBuffer);
2365                         endCommandBuffer(vk, *m_commandBuffer);
2366                 }
2367         }
2368
2369         bool isSecondary (void) const
2370         {
2371                 return m_commandBuffer;
2372         }
2373
2374         VkCommandBuffer getCommandBuffer (void) const
2375         {
2376                 DE_ASSERT(isSecondary());
2377                 return *m_commandBuffer;
2378         }
2379
2380         void pushRenderCommands (const DeviceInterface&         vk,
2381                                                          VkCommandBuffer                        commandBuffer)
2382         {
2383                 if (!m_renderInfo.getColorClears().empty())
2384                 {
2385                         const vector<ColorClear>&       colorClears     (m_renderInfo.getColorClears());
2386
2387                         for (deUint32 attachmentNdx = 0; attachmentNdx < m_renderInfo.getColorAttachmentCount(); attachmentNdx++)
2388                         {
2389                                 const ColorClear&               colorClear      = colorClears[attachmentNdx];
2390                                 const VkClearAttachment attachment      =
2391                                 {
2392                                         VK_IMAGE_ASPECT_COLOR_BIT,
2393                                         attachmentNdx,
2394                                         makeClearValue(colorClear.getColor()),
2395                                 };
2396                                 const VkClearRect               rect            =
2397                                 {
2398                                         {
2399                                                 { (deInt32)colorClear.getOffset().x(),  (deInt32)colorClear.getOffset().y()     },
2400                                                 { colorClear.getSize().x(),                             colorClear.getSize().y()                        }
2401                                         },                                      // rect
2402                                         0u,                                     // baseArrayLayer
2403                                         1u,                                     // layerCount
2404                                 };
2405
2406                                 vk.cmdClearAttachments(commandBuffer, 1u, &attachment, 1u, &rect);
2407                         }
2408                 }
2409
2410                 if (m_renderInfo.getDepthStencilClear())
2411                 {
2412                         const DepthStencilClear&        depthStencilClear       = *m_renderInfo.getDepthStencilClear();
2413                         const deUint32                          attachmentNdx           = m_renderInfo.getColorAttachmentCount();
2414                         tcu::TextureFormat                      format                          = mapVkFormat(m_renderInfo.getDepthStencilAttachment()->getFormat());
2415                         const VkImageLayout                     layout                          = *m_renderInfo.getDepthStencilAttachmentLayout();
2416                         const VkClearAttachment         attachment                      =
2417                         {
2418                                 (VkImageAspectFlags)((hasDepthComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ? VK_IMAGE_ASPECT_DEPTH_BIT : 0)
2419                                         | (hasStencilComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ? VK_IMAGE_ASPECT_STENCIL_BIT : 0)),
2420                                 attachmentNdx,
2421                                 makeClearValueDepthStencil(depthStencilClear.getDepth(), depthStencilClear.getStencil())
2422                         };
2423                         const VkClearRect                               rect                            =
2424                         {
2425                                 {
2426                                         { (deInt32)depthStencilClear.getOffset().x(),   (deInt32)depthStencilClear.getOffset().y()      },
2427                                         { depthStencilClear.getSize().x(),                              depthStencilClear.getSize().y()                         }
2428                                 },                                                      // rect
2429                                 0u,                                                     // baseArrayLayer
2430                                 1u,                                                     // layerCount
2431                         };
2432
2433                         if ((tcu::hasDepthComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2434                                 || (tcu::hasStencilComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL))
2435                         {
2436                                 vk.cmdClearAttachments(commandBuffer, 1u, &attachment, 1u, &rect);
2437                         }
2438                 }
2439
2440                 vector<VkImageMemoryBarrier>    selfDeps;
2441                 VkPipelineStageFlags                    srcStages = 0;
2442                 VkPipelineStageFlags                    dstStages = 0;
2443
2444                 for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < m_renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2445                 {
2446                         for (deUint32 colorAttachmentNdx = 0; colorAttachmentNdx < m_renderInfo.getColorAttachmentCount(); colorAttachmentNdx++)
2447                         {
2448                                 if (m_renderInfo.getInputAttachmentIndex(inputAttachmentNdx) == m_renderInfo.getColorAttachmentIndex(colorAttachmentNdx))
2449                                 {
2450                                         const VkImageMemoryBarrier      barrier   =
2451                                         {
2452                                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // sType
2453                                                 DE_NULL,                                                                                // pNext
2454
2455                                                 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                   // srcAccessMask
2456                                                 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,                    // dstAccessMask
2457
2458                                                 VK_IMAGE_LAYOUT_GENERAL,                                                // oldLayout
2459                                                 VK_IMAGE_LAYOUT_GENERAL,                                                // newLayout
2460
2461                                                 VK_QUEUE_FAMILY_IGNORED,                                                // srcQueueFamilyIndex
2462                                                 VK_QUEUE_FAMILY_IGNORED,                                                // destQueueFamilyIndex
2463
2464                                                 m_colorAttachmentImages[colorAttachmentNdx],    // image
2465                                                 {                                                                                               // subresourceRange
2466                                                         VK_IMAGE_ASPECT_COLOR_BIT,                                              // aspect
2467                                                         0,                                                                                              // baseMipLevel
2468                                                         1,                                                                                              // mipLevels
2469                                                         0,                                                                                              // baseArraySlice
2470                                                         1                                                                                               // arraySize
2471                                                 }
2472                                         };
2473
2474                                         srcStages |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2475                                         dstStages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2476
2477                                         selfDeps.push_back(barrier);
2478                                 }
2479                         }
2480
2481                         if (m_renderInfo.getDepthStencilAttachmentIndex() && (m_renderInfo.getInputAttachmentIndex(inputAttachmentNdx) == *m_renderInfo.getDepthStencilAttachmentIndex()))
2482                         {
2483                                 const tcu::TextureFormat        format          = mapVkFormat(m_renderInfo.getDepthStencilAttachment()->getFormat());
2484                                 const bool                                      hasDepth        = hasDepthComponent(format.order);
2485                                 const bool                                      hasStencil      = hasStencilComponent(format.order);
2486                                 const VkImageMemoryBarrier      barrier         =
2487                                 {
2488                                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                 // sType;
2489                                         DE_NULL,                                                                                                // pNext;
2490
2491                                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,                   // srcAccessMask
2492                                         VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,                                    // dstAccessMask
2493
2494                                         m_renderInfo.getInputAttachmentLayout(inputAttachmentNdx),      // oldLayout
2495                                         m_renderInfo.getInputAttachmentLayout(inputAttachmentNdx),      // newLayout;
2496
2497                                         VK_QUEUE_FAMILY_IGNORED,                                                                // srcQueueFamilyIndex;
2498                                         VK_QUEUE_FAMILY_IGNORED,                                                                // destQueueFamilyIndex;
2499
2500                                         m_depthStencilAttachmentImage,                                                  // image;
2501                                         {                                                                                                               // subresourceRange;
2502                                                 (hasDepth ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
2503                                                         | (hasStencil ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u),  // aspect;
2504                                                 0,                                                                                                                      // baseMipLevel;
2505                                                 1,                                                                                                                      // mipLevels;
2506                                                 0,                                                                                                                      // baseArraySlice;
2507                                                 1                                                                                                                       // arraySize;
2508                                         }
2509                                 };
2510
2511                                 srcStages |= VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
2512                                 dstStages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2513
2514                                 selfDeps.push_back(barrier);
2515                         }
2516                 }
2517
2518                 if (!selfDeps.empty())
2519                 {
2520                         DE_ASSERT(srcStages != 0);
2521                         DE_ASSERT(dstStages != 0);
2522                         vk.cmdPipelineBarrier(commandBuffer, srcStages, dstStages, VK_DEPENDENCY_BY_REGION_BIT, 0, DE_NULL, 0, DE_NULL, (deUint32)selfDeps.size(), &selfDeps[0]);
2523                 }
2524
2525                 if (m_renderInfo.getRenderQuad())
2526                 {
2527                         const VkDeviceSize      offset                  = 0;
2528                         const VkBuffer          vertexBuffer    = *m_vertexBuffer;
2529
2530                         vk.cmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
2531
2532                         if (m_descriptorSet)
2533                         {
2534                                 const VkDescriptorSet descriptorSet = *m_descriptorSet;
2535                                 vk.cmdBindDescriptorSets(commandBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipelineLayout, 0u, 1u, &descriptorSet, 0u, NULL);
2536                         }
2537
2538                         vk.cmdBindVertexBuffers(commandBuffer, 0u, 1u, &vertexBuffer, &offset);
2539                         vk.cmdDraw(commandBuffer, 6u, 1u, 0u, 0u);
2540                 }
2541         }
2542
2543 private:
2544         const SubpassRenderInfo         m_renderInfo;
2545         Move<VkCommandBuffer>           m_commandBuffer;
2546         Move<VkPipeline>                        m_pipeline;
2547         Move<VkDescriptorSetLayout>     m_descriptorSetLayout;
2548         Move<VkPipelineLayout>          m_pipelineLayout;
2549
2550         Move<VkShaderModule>            m_vertexShaderModule;
2551         Move<VkShaderModule>            m_fragmentShaderModule;
2552
2553         Move<VkDescriptorPool>          m_descriptorPool;
2554         Move<VkDescriptorSet>           m_descriptorSet;
2555         Move<VkBuffer>                          m_vertexBuffer;
2556         de::MovePtr<Allocation>         m_vertexBufferMemory;
2557         vector<VkImage>                         m_colorAttachmentImages;
2558         VkImage                                         m_depthStencilAttachmentImage;
2559 };
2560
2561 void pushImageInitializationCommands (const DeviceInterface&                                                            vk,
2562                                                                           VkCommandBuffer                                                                               commandBuffer,
2563                                                                           const vector<Attachment>&                                                             attachmentInfo,
2564                                                                           const vector<de::SharedPtr<AttachmentResources> >&    attachmentResources,
2565                                                                           deUint32                                                                                              queueIndex,
2566                                                                           const vector<Maybe<VkClearValue> >&                                   clearValues)
2567 {
2568         {
2569                 vector<VkImageMemoryBarrier>    initializeLayouts;
2570
2571                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2572                 {
2573                         if (!clearValues[attachmentNdx])
2574                                 continue;
2575
2576                         const VkImageMemoryBarrier barrier =
2577                         {
2578                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                 // sType;
2579                                 DE_NULL,                                                                                                                // pNext;
2580
2581                                 (VkAccessFlags)0,                                                                                               // srcAccessMask
2582                                 getAllMemoryReadFlags() | VK_ACCESS_TRANSFER_WRITE_BIT,                 // dstAccessMask
2583
2584                                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                              // oldLayout
2585                                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,                                                   // newLayout;
2586
2587                                 queueIndex,                                                                                                             // srcQueueFamilyIndex;
2588                                 queueIndex,                                                                                                             // destQueueFamilyIndex;
2589
2590                                 attachmentResources[attachmentNdx]->getImage(),                                 // image;
2591                                 {                                                                                                                               // subresourceRange;
2592                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
2593                                         0,                                                                                                                                      // baseMipLevel;
2594                                         1,                                                                                                                                      // mipLevels;
2595                                         0,                                                                                                                                      // baseArraySlice;
2596                                         1                                                                                                                                       // arraySize;
2597                                 }
2598                         };
2599
2600                         initializeLayouts.push_back(barrier);
2601                 }
2602
2603                 if (!initializeLayouts.empty())
2604                         vk.cmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
2605                                                                   VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0,
2606                                                                   0, (const VkMemoryBarrier*)DE_NULL,
2607                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
2608                                                                   (deUint32)initializeLayouts.size(), &initializeLayouts[0]);
2609         }
2610
2611         for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2612         {
2613                 if (!clearValues[attachmentNdx])
2614                         continue;
2615
2616                 const tcu::TextureFormat format = mapVkFormat(attachmentInfo[attachmentNdx].getFormat());
2617
2618                 if (hasStencilComponent(format.order) || hasDepthComponent(format.order))
2619                 {
2620                         const float                                             clearNan                = tcu::Float32::nan().asFloat();
2621                         const float                                             clearDepth              = hasDepthComponent(format.order) ? clearValues[attachmentNdx]->depthStencil.depth : clearNan;
2622                         const deUint32                                  clearStencil    = hasStencilComponent(format.order) ? clearValues[attachmentNdx]->depthStencil.stencil : 0xDEu;
2623                         const VkClearDepthStencilValue  depthStencil    =
2624                         {
2625                                 clearDepth,
2626                                 clearStencil
2627                         };
2628                         const VkImageSubresourceRange range =
2629                         {
2630                                 (VkImageAspectFlags)((hasDepthComponent(format.order) ? VK_IMAGE_ASPECT_DEPTH_BIT : 0)
2631                                                                          | (hasStencilComponent(format.order) ? VK_IMAGE_ASPECT_STENCIL_BIT : 0)),
2632                                 0,
2633                                 1,
2634                                 0,
2635                                 1
2636                         };
2637
2638                         vk.cmdClearDepthStencilImage(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depthStencil, 1, &range);
2639                 }
2640                 else
2641                 {
2642                         const VkImageSubresourceRange   range           =
2643                         {
2644                                 VK_IMAGE_ASPECT_COLOR_BIT,      // aspectMask;
2645                                 0,                                                      // baseMipLevel;
2646                                 1,                                                      // mipLevels;
2647                                 0,                                                      // baseArrayLayer;
2648                                 1                                                       // layerCount;
2649                         };
2650                         const VkClearColorValue                 clearColor      = clearValues[attachmentNdx]->color;
2651
2652                         vk.cmdClearColorImage(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearColor, 1, &range);
2653                 }
2654         }
2655
2656         {
2657                 vector<VkImageMemoryBarrier>    renderPassLayouts;
2658
2659                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2660                 {
2661                         const VkImageLayout                     oldLayout       = clearValues[attachmentNdx] ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED;
2662                         const VkImageMemoryBarrier      barrier         =
2663                         {
2664                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                 // sType;
2665                                 DE_NULL,                                                                                                // pNext;
2666
2667                                 getMemoryFlagsForLayout(oldLayout),                                                                                                                                             // srcAccessMask
2668                                 getAllMemoryReadFlags() | getMemoryFlagsForLayout(attachmentInfo[attachmentNdx].getInitialLayout()),    // dstAccessMask
2669
2670                                 oldLayout,                                                                                              // oldLayout
2671                                 attachmentInfo[attachmentNdx].getInitialLayout(),               // newLayout;
2672
2673                                 queueIndex,                                                                                             // srcQueueFamilyIndex;
2674                                 queueIndex,                                                                                             // destQueueFamilyIndex;
2675
2676                                 attachmentResources[attachmentNdx]->getImage(),                 // image;
2677                                 {                                                                                                               // subresourceRange;
2678                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
2679                                         0,                                                                                                                                      // baseMipLevel;
2680                                         1,                                                                                                                                      // mipLevels;
2681                                         0,                                                                                                                                      // baseArraySlice;
2682                                         1                                                                                                                                       // arraySize;
2683                                 }
2684                         };
2685
2686                         renderPassLayouts.push_back(barrier);
2687                 }
2688
2689                 if (!renderPassLayouts.empty())
2690                         vk.cmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
2691                                                                   VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0,
2692                                                                   0, (const VkMemoryBarrier*)DE_NULL,
2693                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
2694                                                                   (deUint32)renderPassLayouts.size(), &renderPassLayouts[0]);
2695         }
2696 }
2697
2698 template<typename RenderpassSubpass>
2699 void pushRenderPassCommands (const DeviceInterface&                                                     vk,
2700                                                          VkCommandBuffer                                                                commandBuffer,
2701                                                          VkRenderPass                                                                   renderPass,
2702                                                          VkFramebuffer                                                                  framebuffer,
2703                                                          const vector<de::SharedPtr<SubpassRenderer> >& subpassRenderers,
2704                                                          const UVec2&                                                                   renderPos,
2705                                                          const UVec2&                                                                   renderSize,
2706                                                          const vector<Maybe<VkClearValue> >&                    renderPassClearValues,
2707                                                          TestConfig::RenderTypes                                                render)
2708 {
2709         const float                                                                                     clearNan                                = tcu::Float32::nan().asFloat();
2710         vector<VkClearValue>                                                            attachmentClearValues;
2711         const typename RenderpassSubpass::SubpassEndInfo        subpassEndInfo                  (DE_NULL);
2712
2713         for (size_t attachmentNdx = 0; attachmentNdx < renderPassClearValues.size(); attachmentNdx++)
2714         {
2715                 if (renderPassClearValues[attachmentNdx])
2716                         attachmentClearValues.push_back(*renderPassClearValues[attachmentNdx]);
2717                 else
2718                         attachmentClearValues.push_back(makeClearValueColorF32(clearNan, clearNan, clearNan, clearNan));
2719         }
2720
2721         {
2722                 const VkRect2D renderArea =
2723                 {
2724                         { (deInt32)renderPos.x(),       (deInt32)renderPos.y()  },
2725                         { renderSize.x(),                       renderSize.y()                  }
2726                 };
2727
2728                 for (size_t subpassNdx = 0; subpassNdx < subpassRenderers.size(); subpassNdx++)
2729                 {
2730                         const VkSubpassContents                                                         contents                        = subpassRenderers[subpassNdx]->isSecondary() ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS : VK_SUBPASS_CONTENTS_INLINE;
2731                         const typename RenderpassSubpass::SubpassBeginInfo      subpassBeginInfo        (DE_NULL, contents);
2732                         const VkRenderPassBeginInfo                                                     renderPassBeginInfo     = createRenderPassBeginInfo(renderPass,
2733                                                                                                                                                                                                                                 framebuffer,
2734                                                                                                                                                                                                                                 renderArea,
2735                                                                                                                                                                                                                                 (deUint32)attachmentClearValues.size(),
2736                                                                                                                                                                                                                                 attachmentClearValues.empty() ? DE_NULL : &attachmentClearValues[0]);
2737
2738                         if (subpassNdx == 0)
2739                                 RenderpassSubpass::cmdBeginRenderPass(vk, commandBuffer, &renderPassBeginInfo, &subpassBeginInfo);
2740                         else
2741                                 RenderpassSubpass::cmdNextSubpass(vk, commandBuffer, &subpassBeginInfo, &subpassEndInfo);
2742
2743                         if (render)
2744                         {
2745                                 if (contents == VK_SUBPASS_CONTENTS_INLINE)
2746                                 {
2747                                         subpassRenderers[subpassNdx]->pushRenderCommands(vk, commandBuffer);
2748                                 }
2749                                 else if (contents == VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS)
2750                                 {
2751                                         const VkCommandBuffer cmd = subpassRenderers[subpassNdx]->getCommandBuffer();
2752                                         vk.cmdExecuteCommands(commandBuffer, 1, &cmd);
2753                                 }
2754                                 else
2755                                         DE_FATAL("Invalid contents");
2756                         }
2757                 }
2758
2759                 RenderpassSubpass::cmdEndRenderPass(vk, commandBuffer, &subpassEndInfo);
2760         }
2761 }
2762
2763 void pushDynamicRenderingCommands (const DeviceInterface&                                                               vk,
2764                                                                    VkCommandBuffer                                                                              commandBuffer,
2765                                                                    const RenderPass&                                                                    renderPassInfo,
2766                                                                    const vector<de::SharedPtr<AttachmentResources> >&   attachmentResources,
2767                                                                    const vector<de::SharedPtr<SubpassRenderer> >&               subpassRenderers,
2768                                                                    const UVec2&                                                                                 renderPos,
2769                                                                    const UVec2&                                                                                 renderSize,
2770                                                                    const vector<Maybe<VkClearValue> >&                                  renderPassClearValues,
2771                                                                    deUint32                                                                                             queueIndex,
2772                                                                    TestConfig::RenderTypes                                                              render)
2773 {
2774         const float                     clearNan                = tcu::Float32::nan().asFloat();
2775         const VkClearValue      clearValueNan   = makeClearValueColorF32(clearNan, clearNan, clearNan, clearNan);
2776
2777         DE_ASSERT(subpassRenderers.size() == 1);
2778
2779         VkRenderingFlagsKHR renderingFlags = 0u;
2780         if (subpassRenderers[0]->isSecondary())
2781                 renderingFlags = VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR;
2782
2783         const VkRect2D renderArea
2784         {
2785                 { (deInt32)renderPos.x(),       (deInt32)renderPos.y()  },
2786                 { renderSize.x(),                       renderSize.y()                  }
2787         };
2788
2789         // translate structures that were prepared to construct renderpass to structures needed for dynamic rendering
2790
2791         vector<VkImageMemoryBarrier>                                    imageBarriersBeforeRendering;
2792         vector<VkImageMemoryBarrier>                                    imageBarriersAfterRendering;
2793         std::vector<vk::VkRenderingAttachmentInfoKHR>   colorAttachmentVect;
2794         const Subpass&                                                                  subpassInfo                             = renderPassInfo.getSubpasses()[0];
2795         const vector<AttachmentReference>&                              colorAttachmentsInfo    = subpassInfo.getColorAttachments();
2796         const vector<AttachmentReference>&                              resolveAttachmentsInfo  = subpassInfo.getResolveAttachments();
2797
2798         for (deUint32 i = 0 ; i < colorAttachmentsInfo.size() ; ++i)
2799         {
2800                 const AttachmentReference&      colorAttachmentReference        = colorAttachmentsInfo[i];
2801                 const deUint32                          colorAttachmentIndex            = colorAttachmentReference.getAttachment();
2802                 const Attachment&                       colorAttachmentInfo                     = renderPassInfo.getAttachments()[colorAttachmentIndex];
2803
2804                 VkResolveModeFlagBits   resolveMode                     = VK_RESOLVE_MODE_NONE;
2805                 VkImageView                             resolveImageView        = DE_NULL;
2806                 VkImageLayout                   resolveImageLayout      = VK_IMAGE_LAYOUT_UNDEFINED;
2807
2808                 // handle resolve attachments if they were specified
2809                 if (!resolveAttachmentsInfo.empty())
2810                 {
2811                         const AttachmentReference&      resolveAttachmentReference      = resolveAttachmentsInfo[i];
2812                         const deUint32                          resolveAttachmentIndex          = resolveAttachmentReference.getAttachment();
2813                         const Attachment&                       resolveAttachmentInfo           = renderPassInfo.getAttachments()[resolveAttachmentIndex];
2814
2815                         resolveMode                     = VK_RESOLVE_MODE_AVERAGE_BIT;
2816                         resolveImageView        = attachmentResources[resolveAttachmentIndex]->getAttachmentView();
2817                         resolveImageLayout      = resolveAttachmentInfo.getInitialLayout();
2818                 }
2819
2820                 colorAttachmentVect.push_back({
2821                         vk::VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,                    // VkStructureType                      sType
2822                         DE_NULL,                                                                                                                // const void*                          pNext
2823                         attachmentResources[colorAttachmentIndex]->getAttachmentView(), // VkImageView                          imageView
2824                         colorAttachmentReference.getImageLayout(),                                              // VkImageLayout                        imageLayout
2825                         resolveMode,                                                                                                    // VkResolveModeFlagBits        resolveMode
2826                         resolveImageView,                                                                                               // VkImageView                          resolveImageView
2827                         resolveImageLayout,                                                                                             // VkImageLayout                        resolveImageLayout
2828                         colorAttachmentInfo.getLoadOp(),                                                                // VkAttachmentLoadOp           loadOp
2829                         colorAttachmentInfo.getStoreOp(),                                                               // VkAttachmentStoreOp          storeOp
2830                         (renderPassClearValues[colorAttachmentIndex] ?
2831                                 *renderPassClearValues[colorAttachmentIndex] :
2832                                 clearValueNan)                                                                                          // VkClearValue                         clearValue
2833                         });
2834
2835                 const VkImageLayout initialLayout = colorAttachmentInfo.getInitialLayout();
2836                 const VkImageLayout renderingLayout = colorAttachmentReference.getImageLayout();
2837                 const VkImageLayout finalLayout = colorAttachmentInfo.getFinalLayout();
2838
2839                 const VkImageMemoryBarrier barrierBeforeRendering
2840                 {
2841                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2842                         DE_NULL,                                                                                                                        // pNext
2843
2844                         getAllMemoryWriteFlags() | getMemoryFlagsForLayout(initialLayout),      // srcAccessMask
2845                         getMemoryFlagsForLayout(renderingLayout),                                                       // dstAccessMask
2846
2847                         initialLayout,                                                                                                          // oldLayout
2848                         renderingLayout,                                                                                                        // newLayout
2849
2850                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2851                         queueIndex,                                                                                                                     // destQueueFamilyIndex
2852
2853                         attachmentResources[colorAttachmentIndex]->getImage(),                          // image
2854                         {                                                                                                                                       // subresourceRange
2855                                 getImageAspectFlags(colorAttachmentInfo.getFormat()),                   // aspect;
2856                                 0,                                                                                                                              // baseMipLevel
2857                                 1,                                                                                                                              // mipLevels
2858                                 0,                                                                                                                              // baseArraySlice
2859                                 1                                                                                                                               // arraySize
2860                         }
2861                 };
2862                 imageBarriersBeforeRendering.push_back(barrierBeforeRendering);
2863
2864                 const VkImageMemoryBarrier barrierAfterRendering
2865                 {
2866                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2867                         DE_NULL,                                                                                                                        // pNext
2868
2869                         getMemoryFlagsForLayout(renderingLayout),                                                       // srcAccessMask
2870                         getAllMemoryReadFlags() | getMemoryFlagsForLayout(finalLayout),         // dstAccessMask
2871
2872                         renderingLayout,                                                                                                        // oldLayout
2873                         finalLayout,                                                                                                            // newLayout
2874
2875                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2876                         queueIndex,                                                                                                                     // destQueueFamilyIndex
2877
2878                         attachmentResources[colorAttachmentIndex]->getImage(),                          // image
2879                         {                                                                                                                                       // subresourceRange
2880                                 getImageAspectFlags(colorAttachmentInfo.getFormat()),                   // aspect;
2881                                 0,                                                                                                                              // baseMipLevel
2882                                 1,                                                                                                                              // mipLevels
2883                                 0,                                                                                                                              // baseArraySlice
2884                                 1                                                                                                                               // arraySize
2885                         }
2886                 };
2887                 imageBarriersAfterRendering.push_back(barrierAfterRendering);
2888         }
2889
2890         VkRenderingAttachmentInfoKHR*   pDepthAttachment        = DE_NULL;
2891         VkRenderingAttachmentInfoKHR*   pStencilAttachment      = DE_NULL;
2892         VkRenderingAttachmentInfoKHR    depthAttachment
2893         {
2894                 vk::VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,            // VkStructureType                      sType;
2895                 DE_NULL,                                                                                                        // const void*                          pNext;
2896                 DE_NULL,                                                                                                        // VkImageView                          imageView;
2897                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                      // VkImageLayout                        imageLayout;
2898                 VK_RESOLVE_MODE_NONE,                                                                           // VkResolveModeFlagBits        resolveMode;
2899                 DE_NULL,                                                                                                        // VkImageView                          resolveImageView;
2900                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                      // VkImageLayout                        resolveImageLayout;
2901                 VK_ATTACHMENT_LOAD_OP_LOAD,                                                                     // VkAttachmentLoadOp           loadOp;
2902                 VK_ATTACHMENT_STORE_OP_STORE,                                                           // VkAttachmentStoreOp          storeOp;
2903                 clearValueNan                                                                                           // VkClearValue                         clearValue;
2904         };
2905         VkRenderingAttachmentInfoKHR    stencilAttachment                               = depthAttachment;
2906         const AttachmentReference&              depthStencilAttachmentReference = subpassInfo.getDepthStencilAttachment();
2907         const deUint32                                  dsAttachmentIndex                               = depthStencilAttachmentReference.getAttachment();
2908
2909         if (dsAttachmentIndex != VK_ATTACHMENT_UNUSED)
2910         {
2911                 const Attachment&                       dsAttachmentInfo        = renderPassInfo.getAttachments()[dsAttachmentIndex];
2912                 const tcu::TextureFormat        format                          = mapVkFormat(dsAttachmentInfo.getFormat());
2913
2914                 if (tcu::hasDepthComponent(format.order))
2915                 {
2916                         depthAttachment.imageView               = attachmentResources[dsAttachmentIndex]->getAttachmentView();
2917                         depthAttachment.imageLayout             = depthStencilAttachmentReference.getImageLayout();
2918                         depthAttachment.loadOp                  = dsAttachmentInfo.getLoadOp();
2919                         depthAttachment.storeOp                 = dsAttachmentInfo.getStoreOp();
2920
2921                         if (renderPassClearValues[dsAttachmentIndex])
2922                                 depthAttachment.clearValue = *renderPassClearValues[dsAttachmentIndex];
2923
2924                         pDepthAttachment = &depthAttachment;
2925                 }
2926
2927                 if (tcu::hasStencilComponent(format.order))
2928                 {
2929                         stencilAttachment.imageView             = attachmentResources[dsAttachmentIndex]->getAttachmentView();
2930                         stencilAttachment.imageLayout           = depthStencilAttachmentReference.getImageLayout();
2931                         stencilAttachment.loadOp                = dsAttachmentInfo.getStencilLoadOp();
2932                         stencilAttachment.storeOp               = dsAttachmentInfo.getStencilStoreOp();
2933
2934                         if (renderPassClearValues[dsAttachmentIndex])
2935                                 stencilAttachment.clearValue = *renderPassClearValues[dsAttachmentIndex];
2936
2937                         pStencilAttachment = &stencilAttachment;
2938                 }
2939
2940                 const VkImageLayout initialLayout = dsAttachmentInfo.getInitialLayout();
2941                 const VkImageLayout renderingLayout = depthStencilAttachmentReference.getImageLayout();
2942                 const VkImageLayout finalLayout = dsAttachmentInfo.getFinalLayout();
2943
2944                 const VkImageMemoryBarrier barrierBeforeRendering
2945                 {
2946                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2947                         DE_NULL,                                                                                                                        // pNext
2948
2949                         getAllMemoryWriteFlags() | getMemoryFlagsForLayout(initialLayout),      // srcAccessMask
2950                         getMemoryFlagsForLayout(renderingLayout),                                                       // dstAccessMask
2951
2952                         initialLayout,                                                                                                          // oldLayout
2953                         renderingLayout,                                                                                                        // newLayout
2954
2955                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2956                         queueIndex,                                                                                                                     // destQueueFamilyIndex
2957
2958                         attachmentResources[dsAttachmentIndex]->getImage(),                                     // image
2959                         {                                                                                                                                       // subresourceRange
2960                                 getImageAspectFlags(dsAttachmentInfo.getFormat()),                              // aspect;
2961                                 0,                                                                                                                              // baseMipLevel
2962                                 1,                                                                                                                              // mipLevels
2963                                 0,                                                                                                                              // baseArraySlice
2964                                 1                                                                                                                               // arraySize
2965                         }
2966                 };
2967                 imageBarriersBeforeRendering.push_back(barrierBeforeRendering);
2968
2969                 const VkImageMemoryBarrier barrierAfterRendering
2970                 {
2971                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2972                         DE_NULL,                                                                                                                        // pNext
2973
2974                         getMemoryFlagsForLayout(renderingLayout),                                                       // srcAccessMask
2975                         getAllMemoryReadFlags() | getMemoryFlagsForLayout(finalLayout),         // dstAccessMask
2976
2977                         renderingLayout,                                                                                                        // oldLayout
2978                         finalLayout,                                                                                                            // newLayout
2979
2980                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2981                         queueIndex,                                                                                                                     // destQueueFamilyIndex
2982
2983                         attachmentResources[dsAttachmentIndex]->getImage(),                                     // image
2984                         {                                                                                                                                       // subresourceRange
2985                                 getImageAspectFlags(dsAttachmentInfo.getFormat()),                              // aspect;
2986                                 0,                                                                                                                              // baseMipLevel
2987                                 1,                                                                                                                              // mipLevels
2988                                 0,                                                                                                                              // baseArraySlice
2989                                 1                                                                                                                               // arraySize
2990                         }
2991                 };
2992                 imageBarriersAfterRendering.push_back(barrierAfterRendering);
2993         }
2994
2995         if (!imageBarriersBeforeRendering.empty())
2996                 vk.cmdPipelineBarrier(commandBuffer,
2997                                                           getAllPipelineStageFlags(),
2998                                                           getAllPipelineStageFlags(),
2999                                                           (VkDependencyFlags)0,
3000                                                           0, (const VkMemoryBarrier*)DE_NULL,
3001                                                           0, (const VkBufferMemoryBarrier*)DE_NULL,
3002                                                           (deUint32)imageBarriersBeforeRendering.size(),
3003                                                           &imageBarriersBeforeRendering[0]);
3004
3005         vk::VkRenderingInfoKHR renderingInfo
3006         {
3007                 vk::VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
3008                 DE_NULL,
3009                 renderingFlags,                                                                                                         // VkRenderingFlagsKHR                                  flags;
3010                 renderArea,                                                                                                                     // VkRect2D                                                             renderArea;
3011                 1u,                                                                                                                                     // deUint32                                                             layerCount;
3012                 0u,                                                                                                                                     // deUint32                                                             viewMask;
3013                 static_cast<deUint32>(colorAttachmentVect.size()),                                      // deUint32                                                             colorAttachmentCount;
3014                 colorAttachmentVect.empty() ? DE_NULL : &colorAttachmentVect[0],        // const VkRenderingAttachmentInfoKHR*  pColorAttachments;
3015                 pDepthAttachment,                                                                                                       // const VkRenderingAttachmentInfoKHR*  pDepthAttachment;
3016                 pStencilAttachment                                                                                                      // const VkRenderingAttachmentInfoKHR*  pStencilAttachment;
3017         };
3018
3019         vk.cmdBeginRenderingKHR(commandBuffer, &renderingInfo);
3020
3021         if (render)
3022         {
3023                 if (subpassRenderers[0]->isSecondary())
3024                 {
3025                         const VkCommandBuffer cmd = subpassRenderers[0]->getCommandBuffer();
3026                         vk.cmdExecuteCommands(commandBuffer, 1, &cmd);
3027                 }
3028                 else
3029                         subpassRenderers[0]->pushRenderCommands(vk, commandBuffer);
3030         }
3031
3032         vk.cmdEndRenderingKHR(commandBuffer);
3033
3034         if (!imageBarriersAfterRendering.empty())
3035                 vk.cmdPipelineBarrier(commandBuffer,
3036                                                           getAllPipelineStageFlags(),
3037                                                           getAllPipelineStageFlags(),
3038                                                           (VkDependencyFlags)0,
3039                                                           0, (const VkMemoryBarrier*)DE_NULL,
3040                                                           0, (const VkBufferMemoryBarrier*)DE_NULL,
3041                                                           (deUint32)imageBarriersAfterRendering.size(),
3042                                                           &imageBarriersAfterRendering[0]);
3043 }
3044
3045 void pushRenderPassCommands (const DeviceInterface&                                                             vk,
3046                                                          VkCommandBuffer                                                                        commandBuffer,
3047                                                          VkRenderPass                                                                           renderPass,
3048                                                          const RenderPass&                                                                      renderPassInfo,
3049                                                          const vector<de::SharedPtr<AttachmentResources> >&     attachmentResources,
3050                                                          VkFramebuffer                                                                          framebuffer,
3051                                                          const vector<de::SharedPtr<SubpassRenderer> >&         subpassRenderers,
3052                                                          const UVec2&                                                                           renderPos,
3053                                                          const UVec2&                                                                           renderSize,
3054                                                          const vector<Maybe<VkClearValue> >&                            renderPassClearValues,
3055                                                          deUint32                                                                                       queueIndex,
3056                                                          TestConfig::RenderTypes                                                        render,
3057                                                          RenderingType                                                                          renderingType)
3058 {
3059         switch (renderingType)
3060         {
3061                 case RENDERING_TYPE_RENDERPASS_LEGACY:
3062                         return pushRenderPassCommands<RenderpassSubpass1>(vk, commandBuffer, renderPass, framebuffer, subpassRenderers, renderPos, renderSize, renderPassClearValues, render);
3063                 case RENDERING_TYPE_RENDERPASS2:
3064                         return pushRenderPassCommands<RenderpassSubpass2>(vk, commandBuffer, renderPass, framebuffer, subpassRenderers, renderPos, renderSize, renderPassClearValues, render);
3065                 case RENDERING_TYPE_DYNAMIC_RENDERING:
3066                         return pushDynamicRenderingCommands(vk, commandBuffer, renderPassInfo, attachmentResources, subpassRenderers, renderPos, renderSize, renderPassClearValues, queueIndex, render);
3067                 default:
3068                         TCU_THROW(InternalError, "Impossible");
3069         }
3070 }
3071
3072 void pushReadImagesToBuffers (const DeviceInterface&                                                            vk,
3073                                                           VkCommandBuffer                                                                               commandBuffer,
3074                                                           deUint32                                                                                              queueIndex,
3075
3076                                                           const vector<de::SharedPtr<AttachmentResources> >&    attachmentResources,
3077                                                           const vector<Attachment>&                                                             attachmentInfo,
3078                                                           const vector<bool>&                                                                   isLazy,
3079
3080                                                           const UVec2&                                                                                  targetSize)
3081 {
3082         {
3083                 vector<VkImageMemoryBarrier>    imageBarriers;
3084
3085                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3086                 {
3087                         if (isLazy[attachmentNdx])
3088                                 continue;
3089
3090                         const VkImageLayout                     oldLayout       = attachmentInfo[attachmentNdx].getFinalLayout();
3091                         const VkImageMemoryBarrier      barrier         =
3092                         {
3093                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                 // sType
3094                                 DE_NULL,                                                                                                                // pNext
3095
3096                                 getAllMemoryWriteFlags() | getMemoryFlagsForLayout(oldLayout),  // srcAccessMask
3097                                 getAllMemoryReadFlags(),                                                                                // dstAccessMask
3098
3099                                 oldLayout,                                                                                                              // oldLayout
3100                                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,                                                   // newLayout
3101
3102                                 queueIndex,                                                                                                             // srcQueueFamilyIndex
3103                                 queueIndex,                                                                                                             // destQueueFamilyIndex
3104
3105                                 attachmentResources[attachmentNdx]->getImage(),                                 // image
3106                                 {                                                                                                                               // subresourceRange
3107                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
3108                                         0,                                                                                                                                      // baseMipLevel
3109                                         1,                                                                                                                                      // mipLevels
3110                                         0,                                                                                                                                      // baseArraySlice
3111                                         1                                                                                                                                       // arraySize
3112                                 }
3113                         };
3114
3115                         imageBarriers.push_back(barrier);
3116                 }
3117
3118                 if (!imageBarriers.empty())
3119                         vk.cmdPipelineBarrier(commandBuffer,
3120                                                                   getAllPipelineStageFlags(),
3121                                                                   getAllPipelineStageFlags(),
3122                                                                   (VkDependencyFlags)0,
3123                                                                   0, (const VkMemoryBarrier*)DE_NULL,
3124                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
3125                                                                   (deUint32)imageBarriers.size(), &imageBarriers[0]);
3126         }
3127
3128         for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3129         {
3130                 if (isLazy[attachmentNdx])
3131                         continue;
3132
3133                 const tcu::TextureFormat::ChannelOrder  order   = mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order;
3134                 const VkBufferImageCopy                                 rect    =
3135                 {
3136                         0, // bufferOffset
3137                         0, // bufferRowLength
3138                         0, // bufferImageHeight
3139                         {                                                       // imageSubresource
3140                                 (vk::VkImageAspectFlags)getPrimaryImageAspect(mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order),    // aspect
3141                                 0,                                              // mipLevel
3142                                 0,                                              // arraySlice
3143                                 1                                               // arraySize
3144                         },
3145                         { 0, 0, 0 },                            // imageOffset
3146                         { targetSize.x(), targetSize.y(), 1u }          // imageExtent
3147                 };
3148
3149                 vk.cmdCopyImageToBuffer(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, attachmentResources[attachmentNdx]->getBuffer(), 1, &rect);
3150
3151                 if (tcu::TextureFormat::DS == order)
3152                 {
3153                         const VkBufferImageCopy stencilRect =
3154                         {
3155                                 0,                                                                              // bufferOffset
3156                                 0,                                                                              // bufferRowLength
3157                                 0,                                                                              // bufferImageHeight
3158                                 {                                                                       // imageSubresource
3159                                         VK_IMAGE_ASPECT_STENCIL_BIT,    // aspect
3160                                         0,                                                              // mipLevel
3161                                         0,                                                              // arraySlice
3162                                         1                                                               // arraySize
3163                                 },
3164                                 { 0, 0, 0 },                                                    // imageOffset
3165                                 { targetSize.x(), targetSize.y(), 1u }  // imageExtent
3166                         };
3167
3168                         vk.cmdCopyImageToBuffer(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, attachmentResources[attachmentNdx]->getSecondaryBuffer(), 1, &stencilRect);
3169                 }
3170         }
3171
3172         {
3173                 vector<VkBufferMemoryBarrier>   bufferBarriers;
3174
3175                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3176                 {
3177                         if (isLazy[attachmentNdx])
3178                                 continue;
3179
3180                         const tcu::TextureFormat::ChannelOrder  order                   = mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order;
3181                         const VkBufferMemoryBarrier                             bufferBarrier   =
3182                         {
3183                                 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3184                                 DE_NULL,
3185
3186                                 getAllMemoryWriteFlags(),
3187                                 getAllMemoryReadFlags(),
3188
3189                                 queueIndex,
3190                                 queueIndex,
3191
3192                                 attachmentResources[attachmentNdx]->getBuffer(),
3193                                 0,
3194                                 attachmentResources[attachmentNdx]->getBufferSize()
3195                         };
3196
3197                         bufferBarriers.push_back(bufferBarrier);
3198
3199                         if (tcu::TextureFormat::DS == order)
3200                         {
3201                                 const VkBufferMemoryBarrier secondaryBufferBarrier =
3202                                 {
3203                                         VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3204                                         DE_NULL,
3205
3206                                         getAllMemoryWriteFlags(),
3207                                         getAllMemoryReadFlags(),
3208
3209                                         queueIndex,
3210                                         queueIndex,
3211
3212                                         attachmentResources[attachmentNdx]->getSecondaryBuffer(),
3213                                         0,
3214                                         attachmentResources[attachmentNdx]->getSecondaryBufferSize()
3215                                 };
3216
3217                                 bufferBarriers.push_back(secondaryBufferBarrier);
3218                         }
3219                 }
3220
3221                 if (!bufferBarriers.empty())
3222                         vk.cmdPipelineBarrier(commandBuffer,
3223                                                                   getAllPipelineStageFlags(),
3224                                                                   getAllPipelineStageFlags(),
3225                                                                   (VkDependencyFlags)0,
3226                                                                   0, (const VkMemoryBarrier*)DE_NULL,
3227                                                                   (deUint32)bufferBarriers.size(), &bufferBarriers[0],
3228                                                                   0, (const VkImageMemoryBarrier*)DE_NULL);
3229         }
3230 }
3231
3232 class PixelValue
3233 {
3234 public:
3235                                 PixelValue              (const Maybe<bool>&     x = nothing<bool>(),
3236                                                                  const Maybe<bool>&     y = nothing<bool>(),
3237                                                                  const Maybe<bool>&     z = nothing<bool>(),
3238                                                                  const Maybe<bool>&     w = nothing<bool>());
3239
3240         void            setUndefined    (size_t ndx);
3241         void            setValue                (size_t ndx, bool value);
3242         Maybe<bool>     getValue                (size_t ndx) const;
3243
3244 private:
3245         deUint16        m_status;
3246 };
3247
3248 PixelValue::PixelValue (const Maybe<bool>&      x,
3249                                                 const Maybe<bool>&      y,
3250                                                 const Maybe<bool>&      z,
3251                                                 const Maybe<bool>&      w)
3252         : m_status (0)
3253 {
3254         const Maybe<bool> values[] =
3255         {
3256                 x, y, z, w
3257         };
3258
3259         for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(values); ndx++)
3260         {
3261                 if (values[ndx])
3262                         setValue(ndx, *values[ndx]);
3263                 else
3264                         setUndefined(ndx);
3265         }
3266
3267         DE_ASSERT(m_status <= 0xFFu);
3268 }
3269
3270 void PixelValue::setUndefined (size_t ndx)
3271 {
3272         DE_ASSERT(ndx < 4);
3273         DE_ASSERT(m_status <= 0xFFu);
3274
3275         m_status &= (deUint16)~(0x1u << (deUint16)(ndx * 2));
3276         DE_ASSERT(m_status <= 0xFFu);
3277 }
3278
3279 void PixelValue::setValue (size_t ndx, bool value)
3280 {
3281         DE_ASSERT(ndx < 4);
3282         DE_ASSERT(m_status <= 0xFFu);
3283
3284         m_status = (deUint16)(m_status | (deUint16)(0x1u << (ndx * 2)));
3285
3286         if (value)
3287                 m_status = (deUint16)(m_status | (deUint16)(0x1u << (ndx * 2 + 1)));
3288         else
3289                 m_status &= (deUint16)~(0x1u << (deUint16)(ndx * 2 + 1));
3290
3291         DE_ASSERT(m_status <= 0xFFu);
3292 }
3293
3294 Maybe<bool> PixelValue::getValue (size_t ndx) const
3295 {
3296         DE_ASSERT(ndx < 4);
3297         DE_ASSERT(m_status <= 0xFFu);
3298
3299         if ((m_status & (0x1u << (deUint16)(ndx * 2))) != 0)
3300         {
3301                 return just((m_status & (0x1u << (deUint32)(ndx * 2 + 1))) != 0);
3302         }
3303         else
3304                 return nothing<bool>();
3305 }
3306
3307 void clearReferenceValues (vector<PixelValue>&  values,
3308                                                    const UVec2&                 targetSize,
3309                                                    const UVec2&                 offset,
3310                                                    const UVec2&                 size,
3311                                                    const BVec4&                 mask,
3312                                                    const PixelValue&    value)
3313 {
3314         DE_ASSERT(targetSize.x() * targetSize.y() == (deUint32)values.size());
3315         DE_ASSERT(offset.x() + size.x() <= targetSize.x());
3316         DE_ASSERT(offset.y() + size.y() <= targetSize.y());
3317
3318         for (deUint32 y = offset.y(); y < offset.y() + size.y(); y++)
3319         for (deUint32 x = offset.x(); x < offset.x() + size.x(); x++)
3320         {
3321                 for (int compNdx = 0; compNdx < 4; compNdx++)
3322                 {
3323                         if (mask[compNdx])
3324                         {
3325                                 if (value.getValue(compNdx))
3326                                         values[x + y * targetSize.x()].setValue(compNdx, *value.getValue(compNdx));
3327                                 else
3328                                         values[x + y * targetSize.x()].setUndefined(compNdx);
3329                         }
3330                 }
3331         }
3332 }
3333
3334 void markUndefined (vector<PixelValue>& values,
3335                                         const BVec4&            mask,
3336                                         const UVec2&            targetSize,
3337                                         const UVec2&            offset,
3338                                         const UVec2&            size)
3339 {
3340         DE_ASSERT(targetSize.x() * targetSize.y() == (deUint32)values.size());
3341
3342         for (deUint32 y = offset.y(); y < offset.y() + size.y(); y++)
3343         for (deUint32 x = offset.x(); x < offset.x() + size.x(); x++)
3344         {
3345                 for (int compNdx = 0; compNdx < 4; compNdx++)
3346                 {
3347                         if (mask[compNdx])
3348                                 values[x + y * targetSize.x()].setUndefined(compNdx);
3349                 }
3350         }
3351 }
3352
3353 PixelValue clearValueToPixelValue (const VkClearValue&                  value,
3354                                                                    const tcu::TextureFormat&    format,
3355                                                                    const DepthValuesArray&              depthValues)
3356 {
3357         const bool      isDepthAttachment                       = hasDepthComponent(format.order);
3358         const bool      isStencilAttachment                     = hasStencilComponent(format.order);
3359         const bool      isDepthOrStencilAttachment      = isDepthAttachment || isStencilAttachment;
3360         PixelValue      pixelValue;
3361
3362         if (isDepthOrStencilAttachment)
3363         {
3364                 if (isDepthAttachment)
3365                 {
3366                         if (value.depthStencil.depth == float(depthValues[1]) / 255.0f)
3367                                 pixelValue.setValue(0, true);
3368                         else if (value.depthStencil.depth == float(depthValues[0]) / 255.0f)
3369                                 pixelValue.setValue(0, false);
3370                         else
3371                                 DE_FATAL("Unknown depth value");
3372                 }
3373
3374                 if (isStencilAttachment)
3375                 {
3376                         if (value.depthStencil.stencil == 0xFFu)
3377                                 pixelValue.setValue(1, true);
3378                         else if (value.depthStencil.stencil == 0x0u)
3379                                 pixelValue.setValue(1, false);
3380                         else
3381                                 DE_FATAL("Unknown stencil value");
3382                 }
3383         }
3384         else
3385         {
3386                 const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
3387                 const tcu::BVec4                                channelMask             = tcu::getTextureFormatChannelMask(format);
3388
3389                 switch (channelClass)
3390                 {
3391                         case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
3392                                 for (int i = 0; i < 4; i++)
3393                                 {
3394                                         if (channelMask[i])
3395                                         {
3396                                                 if (value.color.int32[i] == 1)
3397                                                         pixelValue.setValue(i, true);
3398                                                 else if (value.color.int32[i] == 0)
3399                                                         pixelValue.setValue(i, false);
3400                                                 else
3401                                                         DE_FATAL("Unknown clear color value");
3402                                         }
3403                                 }
3404                                 break;
3405
3406                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
3407                                 for (int i = 0; i < 4; i++)
3408                                 {
3409                                         if (channelMask[i])
3410                                         {
3411                                                 if (value.color.uint32[i] == 1u)
3412                                                         pixelValue.setValue(i, true);
3413                                                 else if (value.color.uint32[i] == 0u)
3414                                                         pixelValue.setValue(i, false);
3415                                                 else
3416                                                         DE_FATAL("Unknown clear color value");
3417                                         }
3418                                 }
3419                                 break;
3420
3421                         case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
3422                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
3423                         case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
3424                                 for (int i = 0; i < 4; i++)
3425                                 {
3426                                         if (channelMask[i])
3427                                         {
3428                                                 if (value.color.float32[i] == 1.0f)
3429                                                         pixelValue.setValue(i, true);
3430                                                 else if (value.color.float32[i] == 0.0f)
3431                                                         pixelValue.setValue(i, false);
3432                                                 else
3433                                                         DE_FATAL("Unknown clear color value");
3434                                         }
3435                                 }
3436                                 break;
3437
3438                         default:
3439                                 DE_FATAL("Unknown channel class");
3440                 }
3441         }
3442
3443         return pixelValue;
3444 }
3445
3446 void renderReferenceValues (vector<vector<PixelValue> >&                referenceAttachments,
3447                                                         const RenderPass&                                       renderPassInfo,
3448                                                         const UVec2&                                            targetSize,
3449                                                         const vector<Maybe<VkClearValue> >&     imageClearValues,
3450                                                         const vector<Maybe<VkClearValue> >&     renderPassClearValues,
3451                                                         const vector<SubpassRenderInfo>&        subpassRenderInfo,
3452                                                         const UVec2&                                            renderPos,
3453                                                         const UVec2&                                            renderSize,
3454                                                         const deUint32                                          drawStartNdx,
3455                                                         const DepthValuesArray&                         depthValues)
3456 {
3457         const vector<Subpass>&  subpasses               = renderPassInfo.getSubpasses();
3458         vector<bool>                    attachmentUsed  (renderPassInfo.getAttachments().size(), false);
3459
3460         referenceAttachments.resize(renderPassInfo.getAttachments().size());
3461
3462         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
3463         {
3464                 const Attachment                        attachment      = renderPassInfo.getAttachments()[attachmentNdx];
3465                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3466                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentNdx];
3467
3468                 reference.resize(targetSize.x() * targetSize.y());
3469
3470                 if (imageClearValues[attachmentNdx])
3471                         clearReferenceValues(reference, targetSize, UVec2(0, 0), targetSize, BVec4(true), clearValueToPixelValue(*imageClearValues[attachmentNdx], format, depthValues));
3472         }
3473
3474         for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
3475         {
3476                 const Subpass&                                          subpass                         = subpasses[subpassNdx];
3477                 const SubpassRenderInfo&                        renderInfo                      = subpassRenderInfo[subpassNdx];
3478                 const vector<AttachmentReference>&      colorAttachments        = subpass.getColorAttachments();
3479
3480                 // Apply load op if attachment was used for the first time
3481                 for (size_t attachmentNdx = 0; attachmentNdx < colorAttachments.size(); attachmentNdx++)
3482                 {
3483                         const deUint32 attachmentIndex = getAttachmentNdx(colorAttachments, attachmentNdx);
3484
3485                         if (!attachmentUsed[attachmentIndex] && colorAttachments[attachmentNdx].getAttachment() != VK_ATTACHMENT_UNUSED)
3486                         {
3487                                 const Attachment&                       attachment      = renderPassInfo.getAttachments()[attachmentIndex];
3488                                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentIndex];
3489                                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3490
3491                                 DE_ASSERT(!tcu::hasDepthComponent(format.order));
3492                                 DE_ASSERT(!tcu::hasStencilComponent(format.order));
3493
3494                                 if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3495                                         clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(true), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3496                                 else if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3497                                         markUndefined(reference, BVec4(true), targetSize, renderPos, renderSize);
3498
3499                                 attachmentUsed[attachmentIndex] = true;
3500                         }
3501                 }
3502
3503                 // Apply load op to depth/stencil attachment if it was used for the first time
3504                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
3505                 {
3506                         const deUint32 attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3507
3508                         // Apply load op if attachment was used for the first time
3509                         if (!attachmentUsed[attachmentIndex])
3510                         {
3511                                 const Attachment&                       attachment      = renderPassInfo.getAttachments()[attachmentIndex];
3512                                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentIndex];
3513                                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3514
3515                                 if (tcu::hasDepthComponent(format.order))
3516                                 {
3517                                         if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3518                                                 clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(true, false, false, false), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3519                                         else if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3520                                                 markUndefined(reference, BVec4(true, false, false, false), targetSize, renderPos, renderSize);
3521                                 }
3522
3523                                 if (tcu::hasStencilComponent(format.order))
3524                                 {
3525                                         if (attachment.getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3526                                                 clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(false, true, false, false), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3527                                         else if (attachment.getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3528                                                 markUndefined(reference, BVec4(false, true, false, false), targetSize, renderPos, renderSize);
3529                                 }
3530
3531                                 attachmentUsed[attachmentIndex] = true;
3532                         }
3533                 }
3534
3535                 for (size_t colorClearNdx = 0; colorClearNdx < renderInfo.getColorClears().size(); colorClearNdx++)
3536                 {
3537                         const ColorClear&                       colorClear              = renderInfo.getColorClears()[colorClearNdx];
3538                         const UVec2                                     offset                  = colorClear.getOffset();
3539                         const UVec2                                     size                    = colorClear.getSize();
3540                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[colorClearNdx].getAttachment();
3541                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3542                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3543                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3544                         VkClearValue                            value;
3545
3546                         value.color = colorClear.getColor();
3547
3548                         clearReferenceValues(reference, targetSize, offset, size, BVec4(true), clearValueToPixelValue(value, format, depthValues));
3549                 }
3550
3551                 if (renderInfo.getDepthStencilClear())
3552                 {
3553                         const DepthStencilClear&        dsClear                 = *renderInfo.getDepthStencilClear();
3554                         const UVec2                                     offset                  = dsClear.getOffset();
3555                         const UVec2                                     size                    = dsClear.getSize();
3556                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3557                         const VkImageLayout                     layout                  = subpass.getDepthStencilAttachment().getImageLayout();
3558                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3559                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3560                         const bool                                      hasStencil              = tcu::hasStencilComponent(format.order)
3561                                                                                                                 && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL;
3562                         const bool                                      hasDepth                = tcu::hasDepthComponent(format.order)
3563                                                                                                                 && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL;
3564                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3565                         VkClearValue                            value;
3566
3567                         value.depthStencil.depth = dsClear.getDepth();
3568                         value.depthStencil.stencil = dsClear.getStencil();
3569
3570                         clearReferenceValues(reference, targetSize, offset, size, BVec4(hasDepth, hasStencil, false, false), clearValueToPixelValue(value, format, depthValues));
3571                 }
3572
3573                 if (renderInfo.getRenderQuad())
3574                 {
3575                         const RenderQuad&       renderQuad      = *renderInfo.getRenderQuad();
3576                         const Vec2                      posA            = renderQuad.getCornerA();
3577                         const Vec2                      posB            = renderQuad.getCornerB();
3578                         const Vec2                      origin          = Vec2((float)renderInfo.getViewportOffset().x(), (float)renderInfo.getViewportOffset().y()) + Vec2((float)renderInfo.getViewportSize().x(), (float)renderInfo.getViewportSize().y()) / Vec2(2.0f);
3579                         const Vec2                      p                       = Vec2((float)renderInfo.getViewportSize().x(), (float)renderInfo.getViewportSize().y()) / Vec2(2.0f);
3580                         const IVec2                     posAI           (deRoundFloatToInt32(origin.x() + (p.x() * posA.x())),
3581                                                                                          deRoundFloatToInt32(origin.y() + (p.y() * posA.y())));
3582                         const IVec2                     posBI           (deRoundFloatToInt32(origin.x() + (p.x() * posB.x())),
3583                                                                                          deRoundFloatToInt32(origin.y() + (p.y() * posB.y())));
3584
3585                         DE_ASSERT(posAI.x() < posBI.x());
3586                         DE_ASSERT(posAI.y() < posBI.y());
3587
3588                         if (subpass.getInputAttachments().empty())
3589                         {
3590                                 for (size_t attachmentRefNdx = drawStartNdx; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3591                                 {
3592                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3593
3594                                         if (attachmentIndex == VK_ATTACHMENT_UNUSED)
3595                                                 continue;
3596
3597                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3598                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3599                                         const tcu::BVec4                        channelMask             = tcu::getTextureFormatChannelMask(format);
3600                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3601
3602                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3603                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3604                                         {
3605                                                 for (int compNdx = 0; compNdx < 4; compNdx++)
3606                                                 {
3607                                                         const size_t    index   = subpassNdx + attachmentIndex + compNdx;
3608                                                         const BoolOp    op              = boolOpFromIndex(index);
3609                                                         const bool              boolX   = x % 2 == (int)(index % 2);
3610                                                         const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3611
3612                                                         if (channelMask[compNdx])
3613                                                                 reference[x + y * targetSize.x()].setValue(compNdx, performBoolOp(op, boolX, boolY));
3614                                                 }
3615                                         }
3616                                 }
3617
3618                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
3619                                 {
3620                                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3621                                         const VkImageLayout                     layout                  = subpass.getDepthStencilAttachment().getImageLayout();
3622                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3623                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3624                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3625
3626                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3627                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3628                                         {
3629                                                 if (tcu::hasDepthComponent(format.order)
3630                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3631                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3632                                                 {
3633                                                         const size_t    index   = subpassNdx + 1;
3634                                                         const BoolOp    op              = boolOpFromIndex(index);
3635                                                         const bool              boolX   = x % 2 == (int)(index % 2);
3636                                                         const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3637
3638                                                         reference[x + y * targetSize.x()].setValue(0, performBoolOp(op, boolX, boolY));
3639                                                 }
3640
3641                                                 if (tcu::hasStencilComponent(format.order)
3642                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3643                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3644                                                 {
3645                                                         const size_t    index   = subpassNdx;
3646                                                         reference[x + y * targetSize.x()].setValue(1, (index % 2) == 0);
3647                                                 }
3648                                         }
3649                                 }
3650                         }
3651                         else
3652                         {
3653                                 size_t                                  outputComponentCount    = 0;
3654                                 vector<Maybe<bool> >    inputs;
3655
3656                                 DE_ASSERT(posAI.x() < posBI.x());
3657                                 DE_ASSERT(posAI.y() < posBI.y());
3658
3659                                 for (size_t attachmentRefNdx = 0; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3660                                 {
3661                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3662                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3663                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3664                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3665
3666                                         outputComponentCount += (size_t)componentCount;
3667                                 }
3668
3669                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3670                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3671                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3672                                 {
3673                                         const Attachment&                       attachment      (renderPassInfo.getAttachments()[subpass.getDepthStencilAttachment().getAttachment()]);
3674                                         const tcu::TextureFormat        format          (mapVkFormat(attachment.getFormat()));
3675
3676                                         if (tcu::hasDepthComponent(format.order))
3677                                                 outputComponentCount++;
3678                                 }
3679
3680                                 if (outputComponentCount > 0)
3681                                 {
3682                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3683                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3684                                         {
3685                                                 for (size_t inputAttachmentNdx = 0; inputAttachmentNdx < subpass.getInputAttachments().size(); inputAttachmentNdx++)
3686                                                 {
3687                                                         const deUint32                          attachmentIndex = subpass.getInputAttachments()[inputAttachmentNdx].getAttachment();
3688                                                         const VkImageLayout                     layout                  = subpass.getInputAttachments()[inputAttachmentNdx].getImageLayout();
3689                                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3690                                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3691                                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3692
3693                                                         for (int compNdx = 0; compNdx < componentCount; compNdx++)
3694                                                         {
3695                                                                 if ((compNdx != 0 || layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3696                                                                         && (compNdx != 1 || layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL))
3697                                                                 {
3698                                                                         inputs.push_back(referenceAttachments[attachmentIndex][x + y * targetSize.x()].getValue(compNdx));
3699                                                                 }
3700                                                         }
3701                                                 }
3702
3703                                                 const size_t inputsPerOutput = inputs.size() >= outputComponentCount
3704                                                                                                                 ? ((inputs.size() / outputComponentCount)
3705                                                                                                                         + ((inputs.size() % outputComponentCount) != 0 ? 1 : 0))
3706                                                                                                                 : 1;
3707
3708                                                 size_t outputValueNdx = 0;
3709
3710                                                 for (size_t attachmentRefNdx = 0; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3711                                                 {
3712                                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3713                                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3714                                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3715                                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3716                                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3717
3718                                                         for (int compNdx = 0; compNdx < componentCount; compNdx++)
3719                                                         {
3720                                                                 const size_t    index   = subpassNdx + attachmentIndex + outputValueNdx;
3721                                                                 const BoolOp    op              = boolOpFromIndex(index);
3722                                                                 const bool              boolX   = x % 2 == (int)(index % 2);
3723                                                                 const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3724                                                                 Maybe<bool>             output  = tcu::just(performBoolOp(op, boolX, boolY));
3725
3726                                                                 for (size_t i = 0; i < inputsPerOutput; i++)
3727                                                                 {
3728                                                                         if (!output)
3729                                                                                 break;
3730                                                                         else if (!inputs[((outputValueNdx + compNdx) * inputsPerOutput + i) % inputs.size()])
3731                                                                                 output = tcu::nothing<bool>();
3732                                                                         else
3733                                                                                 output = (*output) == (*inputs[((outputValueNdx + compNdx) * inputsPerOutput + i) % inputs.size()]);
3734                                                                 }
3735
3736                                                                 if (output)
3737                                                                         reference[x + y * targetSize.x()].setValue(compNdx, *output);
3738                                                                 else
3739                                                                         reference[x + y * targetSize.x()].setUndefined(compNdx);
3740                                                         }
3741
3742                                                         outputValueNdx += componentCount;
3743                                                 }
3744
3745                                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3746                                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3747                                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3748                                                 {
3749                                                         const deUint32          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3750                                                         vector<PixelValue>&     reference               = referenceAttachments[attachmentIndex];
3751                                                         const size_t            index                   = subpassNdx + attachmentIndex;
3752                                                         const BoolOp            op                              = boolOpFromIndex(index);
3753                                                         const bool                      boolX                   = x % 2 == (int)(index % 2);
3754                                                         const bool                      boolY                   = y % 2 == (int)((index / 2) % 2);
3755                                                         Maybe<bool>                     output                  = tcu::just(performBoolOp(op, boolX, boolY));
3756
3757                                                         for (size_t i = 0; i < inputsPerOutput; i++)
3758                                                         {
3759                                                                 if (!output)
3760                                                                         break;
3761                                                                 else if (inputs[(outputValueNdx * inputsPerOutput + i) % inputs.size()])
3762                                                                         output = (*output) == (*inputs[(outputValueNdx * inputsPerOutput + i) % inputs.size()]);
3763                                                                 else
3764                                                                         output = tcu::nothing<bool>();
3765                                                         }
3766
3767                                                         if (output)
3768                                                                 reference[x + y * targetSize.x()].setValue(0, *output);
3769                                                         else
3770                                                                 reference[x + y * targetSize.x()].setUndefined(0);
3771                                                 }
3772
3773                                                 inputs.clear();
3774                                         }
3775                                 }
3776
3777                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3778                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3779                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3780                                 {
3781                                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3782                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3783                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3784                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3785
3786                                         if (tcu::hasStencilComponent(format.order))
3787                                         {
3788                                                 for (int y = posAI.y(); y < (int)posBI.y(); y++)
3789                                                 for (int x = posAI.x(); x < (int)posBI.x(); x++)
3790                                                 {
3791                                                         const size_t    index   = subpassNdx;
3792                                                         reference[x + y * targetSize.x()].setValue(1, (index % 2) == 0);
3793                                                 }
3794                                         }
3795                                 }
3796                         }
3797                 }
3798         }
3799
3800         // Mark all attachments that were used but not stored as undefined
3801         for (size_t attachmentIndex = 0; attachmentIndex < renderPassInfo.getAttachments().size(); attachmentIndex++)
3802         {
3803                 const Attachment                        attachment                                      = renderPassInfo.getAttachments()[attachmentIndex];
3804                 const tcu::TextureFormat        format                                          = mapVkFormat(attachment.getFormat());
3805                 vector<PixelValue>&                     reference                                       = referenceAttachments[attachmentIndex];
3806                 const bool                                      isStencilAttachment                     = hasStencilComponent(format.order);
3807                 const bool                                      isDepthOrStencilAttachment      = hasDepthComponent(format.order) || isStencilAttachment;
3808
3809                 if (attachmentUsed[attachmentIndex] && renderPassInfo.getAttachments()[attachmentIndex].getStoreOp() == VK_ATTACHMENT_STORE_OP_DONT_CARE)
3810                 {
3811                         if (isDepthOrStencilAttachment)
3812                                 markUndefined(reference, BVec4(true, false, false, false), targetSize, renderPos, renderSize);
3813                         else
3814                                 markUndefined(reference, BVec4(true), targetSize, renderPos, renderSize);
3815                 }
3816
3817                 if (attachmentUsed[attachmentIndex] && isStencilAttachment && renderPassInfo.getAttachments()[attachmentIndex].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_DONT_CARE)
3818                         markUndefined(reference, BVec4(false, true, false, false), targetSize, renderPos, renderSize);
3819         }
3820 }
3821
3822 void renderReferenceImagesFromValues (vector<tcu::TextureLevel>&                        referenceImages,
3823                                                                           const vector<vector<PixelValue> >&    referenceValues,
3824                                                                           const UVec2&                                                  targetSize,
3825                                                                           const RenderPass&                                             renderPassInfo,
3826                                                                           const DepthValuesArray&                               depthValues)
3827 {
3828         referenceImages.resize(referenceValues.size());
3829
3830         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
3831         {
3832                 const Attachment                        attachment                      = renderPassInfo.getAttachments()[attachmentNdx];
3833                 const tcu::TextureFormat        format                          = mapVkFormat(attachment.getFormat());
3834                 const vector<PixelValue>&       reference                       = referenceValues[attachmentNdx];
3835                 const bool                                      hasDepth                        = tcu::hasDepthComponent(format.order);
3836                 const bool                                      hasStencil                      = tcu::hasStencilComponent(format.order);
3837                 const bool                                      hasDepthOrStencil       = hasDepth || hasStencil;
3838                 tcu::TextureLevel&                      referenceImage          = referenceImages[attachmentNdx];
3839
3840                 referenceImage.setStorage(format, targetSize.x(), targetSize.y());
3841
3842                 if (hasDepthOrStencil)
3843                 {
3844                         if (hasDepth)
3845                         {
3846                                 const PixelBufferAccess depthAccess (tcu::getEffectiveDepthStencilAccess(referenceImage.getAccess(), tcu::Sampler::MODE_DEPTH));
3847
3848                                 for (deUint32 y = 0; y < targetSize.y(); y++)
3849                                 for (deUint32 x = 0; x < targetSize.x(); x++)
3850                                 {
3851                                         if (reference[x + y * targetSize.x()].getValue(0))
3852                                         {
3853                                                 if (*reference[x + y * targetSize.x()].getValue(0))
3854                                                         depthAccess.setPixDepth(float(depthValues[1]) / 255.0f, x, y);
3855                                                 else
3856                                                         depthAccess.setPixDepth(float(depthValues[0]) / 255.0f, x, y);
3857                                         }
3858                                         else // Fill with 3x3 grid
3859                                                 depthAccess.setPixDepth(((x / 3) % 2) == ((y / 3) % 2) ? 0.33f : 0.66f, x, y);
3860                                 }
3861                         }
3862
3863                         if (hasStencil)
3864                         {
3865                                 const PixelBufferAccess stencilAccess (tcu::getEffectiveDepthStencilAccess(referenceImage.getAccess(), tcu::Sampler::MODE_STENCIL));
3866
3867                                 for (deUint32 y = 0; y < targetSize.y(); y++)
3868                                 for (deUint32 x = 0; x < targetSize.x(); x++)
3869                                 {
3870                                         if (reference[x + y * targetSize.x()].getValue(1))
3871                                         {
3872                                                 if (*reference[x + y * targetSize.x()].getValue(1))
3873                                                         stencilAccess.setPixStencil(0xFFu, x, y);
3874                                                 else
3875                                                         stencilAccess.setPixStencil(0x0u, x, y);
3876                                         }
3877                                         else // Fill with 3x3 grid
3878                                                 stencilAccess.setPixStencil(((x / 3) % 2) == ((y / 3) % 2) ? 85 : 170, x, y);
3879                                 }
3880                         }
3881                 }
3882                 else
3883                 {
3884                         for (deUint32 y = 0; y < targetSize.y(); y++)
3885                         for (deUint32 x = 0; x < targetSize.x(); x++)
3886                         {
3887                                 tcu::Vec4 color;
3888
3889                                 for (int compNdx = 0; compNdx < 4; compNdx++)
3890                                 {
3891                                         if (reference[x + y * targetSize.x()].getValue(compNdx))
3892                                         {
3893                                                 if (*reference[x + y * targetSize.x()].getValue(compNdx))
3894                                                         color[compNdx] = 1.0f;
3895                                                 else
3896                                                         color[compNdx] = 0.0f;
3897                                         }
3898                                         else // Fill with 3x3 grid
3899                                                 color[compNdx] = ((compNdx + (x / 3)) % 2) == ((y / 3) % 2) ? 0.33f : 0.66f;
3900                                 }
3901
3902                                 referenceImage.getAccess().setPixel(color, x, y);
3903                         }
3904                 }
3905         }
3906 }
3907
3908 bool verifyColorAttachment (const vector<PixelValue>&           reference,
3909                                                         const ConstPixelBufferAccess&   result,
3910                                                         const PixelBufferAccess&                errorImage,
3911                                                         const deBool                                    useFormatCompCount)
3912 {
3913         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
3914         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
3915         bool            ok              = true;
3916
3917         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
3918         DE_ASSERT(result.getWidth() == errorImage.getWidth());
3919         DE_ASSERT(result.getHeight() == errorImage.getHeight());
3920
3921         for (int y = 0; y < result.getHeight(); y++)
3922         for (int x = 0; x < result.getWidth(); x++)
3923         {
3924                 const Vec4                      resultColor             = result.getPixel(x, y);
3925                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
3926                 bool                            pixelOk                 = true;
3927                 const deUint32          componentCount  = useFormatCompCount ? (deUint32)tcu::getNumUsedChannels(result.getFormat().order) : 4;
3928
3929                 for (deUint32 compNdx = 0; compNdx < componentCount; compNdx++)
3930                 {
3931                         const Maybe<bool> maybeValue = referenceValue.getValue(compNdx);
3932
3933                         if (maybeValue)
3934                         {
3935                                 const bool value = *maybeValue;
3936
3937                                 if ((value && (resultColor[compNdx] != 1.0f))
3938                                         || (!value && resultColor[compNdx] != 0.0f))
3939                                         pixelOk = false;
3940                         }
3941                 }
3942
3943                 if (!pixelOk)
3944                 {
3945                         errorImage.setPixel(red, x, y);
3946                         ok = false;
3947                 }
3948                 else
3949                         errorImage.setPixel(green, x, y);
3950         }
3951
3952         return ok;
3953 }
3954
3955 // Setting the alpha value to 1.0f by default helps visualization when the alpha channel is not used.
3956 const tcu::Vec4 kDefaultColorForLog     {0.0f, 0.0f, 0.0f, 1.0f};
3957 const float             kTrueComponent          = 1.0f;
3958 const float             kFalseComponent         = 0.5f;
3959 const float             kUnsetComponentLow      = 0.0f;
3960 const float             kUnsetComponentHigh     = 0.25f;
3961
3962 std::unique_ptr<tcu::TextureLevel> renderColorImageForLog (const ConstPixelBufferAccess& image, int numChannels)
3963 {
3964         // Same channel order, but using UNORM_INT8 for the color format.
3965         const auto                                                      order                   = image.getFormat().order;
3966         const tcu::TextureFormat                        loggableFormat  {order, tcu::TextureFormat::UNORM_INT8};
3967         const int                                                       width                   = image.getWidth();
3968         const int                                                       height                  = image.getHeight();
3969         std::unique_ptr<tcu::TextureLevel>      result                  {new tcu::TextureLevel{loggableFormat, width, height}};
3970         auto                                                            access                  = result->getAccess();
3971         tcu::Vec4                                                       outColor                = kDefaultColorForLog;
3972
3973         for (int x = 0; x < width; ++x)
3974         for (int y = 0; y < height; ++y)
3975         {
3976                 const auto value = image.getPixel(x, y);
3977                 for (int c = 0; c < numChannels; ++c)
3978                 {
3979                         if (value[c] == 0.0f)
3980                                 outColor[c] = kFalseComponent;
3981                         else if (value[c] == 1.0f)
3982                                 outColor[c] = kTrueComponent;
3983                         else
3984                                 DE_ASSERT(false);
3985                 }
3986                 access.setPixel(outColor, x, y);
3987         }
3988
3989         return result;
3990 }
3991
3992 std::unique_ptr<tcu::TextureLevel> renderColorImageForLog (const vector<PixelValue>& reference, const UVec2& targetSize, int numChannels)
3993 {
3994         const tcu::TextureFormat                        loggableFormat  {tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8};
3995         const int                                                       width                   = static_cast<int>(targetSize.x());
3996         const int                                                       height                  = static_cast<int>(targetSize.y());
3997         std::unique_ptr<tcu::TextureLevel>      result                  {new tcu::TextureLevel{loggableFormat, width, height}};
3998         auto                                                            access                  = result->getAccess();
3999         tcu::Vec4                                                       outColor                = kDefaultColorForLog;
4000
4001         for (int x = 0; x < width; ++x)
4002         for (int y = 0; y < height; ++y)
4003         {
4004                 const int index = x + y * width;
4005                 for (int c = 0; c < numChannels; ++c)
4006                 {
4007                         const auto maybeValue = reference[index].getValue(c);
4008                         if (maybeValue)
4009                                 outColor[c] = ((*maybeValue) ? kTrueComponent : kFalseComponent);
4010                         else
4011                                 outColor[c] = ((((x / 3) % 2) == ((y / 3) % 2)) ? kUnsetComponentLow : kUnsetComponentHigh);
4012                 }
4013                 access.setPixel(outColor, x, y);
4014         }
4015
4016         return result;
4017 }
4018
4019 bool verifyDepthAttachment (const vector<PixelValue>&           reference,
4020                                                         const ConstPixelBufferAccess&   result,
4021                                                         const PixelBufferAccess&                errorImage,
4022                                                         const DepthValuesArray&                 depthValues,
4023                                                         float                                                   epsilon)
4024 {
4025         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
4026         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
4027         bool            ok              = true;
4028
4029         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
4030         DE_ASSERT(result.getWidth() == errorImage.getWidth());
4031         DE_ASSERT(result.getHeight() == errorImage.getHeight());
4032
4033         for (int y = 0; y < result.getHeight(); y++)
4034         for (int x = 0; x < result.getWidth(); x++)
4035         {
4036                 bool pixelOk = true;
4037
4038                 const float                     resultDepth             = result.getPixDepth(x, y);
4039                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
4040                 const Maybe<bool>       maybeValue              = referenceValue.getValue(0);
4041
4042                 if (maybeValue)
4043                 {
4044                         const bool value = *maybeValue;
4045
4046                         if ((value && !depthsEqual(resultDepth, float(depthValues[1]) / 255.0f, epsilon))
4047                                 || (!value && !depthsEqual(resultDepth, float(depthValues[0]) / 255.0f, epsilon)))
4048                                 pixelOk = false;
4049                 }
4050
4051                 if (!pixelOk)
4052                 {
4053                         errorImage.setPixel(red, x, y);
4054                         ok = false;
4055                 }
4056                 else
4057                         errorImage.setPixel(green, x, y);
4058         }
4059
4060         return ok;
4061 }
4062
4063 bool verifyStencilAttachment (const vector<PixelValue>&         reference,
4064                                                           const ConstPixelBufferAccess& result,
4065                                                           const PixelBufferAccess&              errorImage)
4066 {
4067         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
4068         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
4069         bool            ok              = true;
4070
4071         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
4072         DE_ASSERT(result.getWidth() == errorImage.getWidth());
4073         DE_ASSERT(result.getHeight() == errorImage.getHeight());
4074
4075         for (int y = 0; y < result.getHeight(); y++)
4076         for (int x = 0; x < result.getWidth(); x++)
4077         {
4078                 bool pixelOk = true;
4079
4080                 const deUint32          resultStencil   = result.getPixStencil(x, y);
4081                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
4082                 const Maybe<bool>       maybeValue              = referenceValue.getValue(1);
4083
4084                 if (maybeValue)
4085                 {
4086                         const bool value = *maybeValue;
4087
4088                         if ((value && (resultStencil != 0xFFu))
4089                                 || (!value && resultStencil != 0x0u))
4090                                 pixelOk = false;
4091                 }
4092
4093                 if (!pixelOk)
4094                 {
4095                         errorImage.setPixel(red, x, y);
4096                         ok = false;
4097                 }
4098                 else
4099                         errorImage.setPixel(green, x, y);
4100         }
4101
4102         return ok;
4103 }
4104
4105 bool logAndVerifyImages (TestLog&                                                                                       log,
4106                                                  const DeviceInterface&                                                         vk,
4107                                                  VkDevice                                                                                       device,
4108                                                  const vector<de::SharedPtr<AttachmentResources> >&     attachmentResources,
4109                                                  const vector<bool>&                                                            attachmentIsLazy,
4110                                                  const RenderPass&                                                                      renderPassInfo,
4111                                                  const vector<Maybe<VkClearValue> >&                            renderPassClearValues,
4112                                                  const vector<Maybe<VkClearValue> >&                            imageClearValues,
4113                                                  const vector<SubpassRenderInfo>&                                       subpassRenderInfo,
4114                                                  const UVec2&                                                                           targetSize,
4115                                                  const TestConfig&                                                                      config)
4116 {
4117         vector<vector<PixelValue> >     referenceValues;
4118         vector<tcu::TextureLevel>       referenceAttachments;
4119         bool                                            isOk                                    = true;
4120
4121         log << TestLog::Message << "Reference images fill undefined pixels with 3x3 grid pattern." << TestLog::EndMessage;
4122
4123         renderReferenceValues(referenceValues, renderPassInfo, targetSize, imageClearValues, renderPassClearValues, subpassRenderInfo, config.renderPos, config.renderSize, config.drawStartNdx, config.depthValues);
4124         renderReferenceImagesFromValues(referenceAttachments, referenceValues, targetSize, renderPassInfo, config.depthValues);
4125
4126         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
4127         {
4128                 if (!attachmentIsLazy[attachmentNdx])
4129                 {
4130                         bool                                            attachmentOK    = true;
4131                         const Attachment                        attachment              = renderPassInfo.getAttachments()[attachmentNdx];
4132                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4133
4134                         if (tcu::hasDepthComponent(format.order) && tcu::hasStencilComponent(format.order))
4135                         {
4136                                 const tcu::TextureFormat        depthFormat                     = getDepthCopyFormat(attachment.getFormat());
4137                                 void* const                                     depthPtr                        = attachmentResources[attachmentNdx]->getResultMemory().getHostPtr();
4138
4139                                 const tcu::TextureFormat        stencilFormat           = getStencilCopyFormat(attachment.getFormat());
4140                                 void* const                                     stencilPtr                      = attachmentResources[attachmentNdx]->getSecondaryResultMemory().getHostPtr();
4141
4142                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getResultMemory());
4143                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getSecondaryResultMemory());
4144
4145                                 {
4146                                         bool                                                    depthOK                         = true;
4147                                         bool                                                    stencilOK                       = true;
4148                                         const ConstPixelBufferAccess    depthAccess                     (depthFormat, targetSize.x(), targetSize.y(), 1, depthPtr);
4149                                         const ConstPixelBufferAccess    stencilAccess           (stencilFormat, targetSize.x(), targetSize.y(), 1, stencilPtr);
4150                                         tcu::TextureLevel                               depthErrorImage         (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4151                                         tcu::TextureLevel                               stencilErrorImage       (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4152
4153                                         if (renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE
4154                                                 && !verifyDepthAttachment(referenceValues[attachmentNdx], depthAccess, depthErrorImage.getAccess(), config.depthValues, requiredDepthEpsilon(attachment.getFormat())))
4155                                         {
4156                                                 depthOK = false;
4157                                         }
4158
4159                                         if (renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE
4160                                                 && !verifyStencilAttachment(referenceValues[attachmentNdx], stencilAccess, stencilErrorImage.getAccess()))
4161                                         {
4162                                                 stencilOK = false;
4163                                         }
4164
4165                                         if (!depthOK || !stencilOK)
4166                                         {
4167                                                 const auto attachmentNdxStr = de::toString(attachmentNdx);
4168
4169                                                 // Output images.
4170                                                 log << TestLog::ImageSet("OutputAttachments" + attachmentNdxStr, "Output depth and stencil attachments " + attachmentNdxStr);
4171                                                 log << TestLog::Image("Attachment" + attachmentNdxStr + "Depth", "Attachment " + attachmentNdxStr + " Depth", depthAccess);
4172                                                 log << TestLog::Image("Attachment" + attachmentNdxStr + "Stencil", "Attachment " + attachmentNdxStr + " Stencil", stencilAccess);
4173                                                 log << TestLog::EndImageSet;
4174
4175                                                 // Reference images. These will be logged as image sets due to having depth and stencil aspects.
4176                                                 log << TestLog::Image("AttachmentReferences" + attachmentNdxStr, "Reference images " + attachmentNdxStr, referenceAttachments[attachmentNdx].getAccess());
4177
4178                                                 // Error masks.
4179                                                 log << TestLog::ImageSet("ErrorMasks" + attachmentNdxStr, "Error masks " + attachmentNdxStr);
4180                                                 if (!depthOK)
4181                                                         log << TestLog::Image("DepthAttachmentError" + attachmentNdxStr, "Depth Attachment Error " + attachmentNdxStr, depthErrorImage.getAccess());
4182                                                 if (!stencilOK)
4183                                                         log << TestLog::Image("StencilAttachmentError" + attachmentNdxStr, "Stencil Attachment Error " + attachmentNdxStr, stencilErrorImage.getAccess());
4184                                                 log << TestLog::EndImageSet;
4185
4186                                                 attachmentOK = false;
4187                                         }
4188                                 }
4189                         }
4190                         else
4191                         {
4192                                 void* const     ptr     = attachmentResources[attachmentNdx]->getResultMemory().getHostPtr();
4193
4194                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getResultMemory());
4195
4196                                 bool                                                    depthOK         = true;
4197                                 bool                                                    stencilOK       = true;
4198                                 bool                                                    colorOK         = true;
4199                                 const ConstPixelBufferAccess    access          (format, targetSize.x(), targetSize.y(), 1, ptr);
4200                                 tcu::TextureLevel                               errorImage      (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4201
4202                                 if (tcu::hasDepthComponent(format.order))
4203                                 {
4204                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4205                                                 && !verifyDepthAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess(), config.depthValues, requiredDepthEpsilon(attachment.getFormat())))
4206                                         {
4207                                                 depthOK = false;
4208                                         }
4209                                 }
4210                                 else if (tcu::hasStencilComponent(format.order))
4211                                 {
4212                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4213                                                 && !verifyStencilAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess()))
4214                                         {
4215                                                 stencilOK = false;
4216                                         }
4217                                 }
4218                                 else
4219                                 {
4220                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4221                                                 && !verifyColorAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess(), config.useFormatCompCount))
4222                                         {
4223                                                 colorOK = false;
4224                                         }
4225                                 }
4226
4227                                 if (!depthOK || !stencilOK || !colorOK)
4228                                 {
4229                                         log << TestLog::ImageSet("TestImages", "Output attachment, reference image and error mask");
4230                                         if (!depthOK || !stencilOK)
4231                                         {
4232                                                 // Log without conversions.
4233                                                 log << TestLog::Image("Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx), access);
4234                                                 log << TestLog::Image("AttachmentReference" + de::toString(attachmentNdx), "Attachment reference " + de::toString(attachmentNdx), referenceAttachments[attachmentNdx].getAccess());
4235                                         }
4236                                         else
4237                                         {
4238                                                 // Convert color images to better reflect test status and output in any format.
4239                                                 const auto numChannels          = tcu::getNumUsedChannels(access.getFormat().order);
4240                                                 const auto attachmentForLog     = renderColorImageForLog(access, numChannels);
4241                                                 const auto referenceForLog      = renderColorImageForLog(referenceValues[attachmentNdx], targetSize, numChannels);
4242
4243                                                 log << TestLog::Message << "Check the attachment formats and test data to verify which components affect the test result." << TestLog::EndMessage;
4244                                                 log << TestLog::Message << "In the reference image, unset pixel components are marked with a 3x3 grid storing values 0.0 and 0.25, pixel components set to false are stored as 0.5 and pixel components set to true are stored as 1.0." << TestLog::EndMessage;
4245                                                 log << TestLog::Message << "Output attachment pixel components are always set to 0.5 or 1.0 but may not be taken into account if not set in the reference image." << TestLog::EndMessage;
4246
4247                                                 log << TestLog::Image("Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx), attachmentForLog->getAccess());
4248                                                 log << TestLog::Image("AttachmentReference" + de::toString(attachmentNdx), "Attachment reference " + de::toString(attachmentNdx), referenceForLog->getAccess());
4249                                         }
4250                                         log << TestLog::Image("AttachmentError" + de::toString(attachmentNdx), "Attachment Error " + de::toString(attachmentNdx), errorImage.getAccess());
4251                                         log << TestLog::EndImageSet;
4252
4253                                         attachmentOK = false;
4254                                 }
4255                         }
4256
4257                         if (!attachmentOK)
4258                                 isOk = false;
4259                 }
4260         }
4261
4262         return isOk;
4263 }
4264
4265 std::string getInputAttachmentType (VkFormat vkFormat)
4266 {
4267         const tcu::TextureFormat                format                  = mapVkFormat(vkFormat);
4268         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
4269
4270         switch (channelClass)
4271         {
4272                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
4273                         return "isubpassInput";
4274
4275                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
4276                         return "usubpassInput";
4277
4278                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
4279                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
4280                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
4281                         return "subpassInput";
4282
4283                 default:
4284                         DE_FATAL("Unknown channel class");
4285                         return "";
4286         }
4287 }
4288
4289 std::string getAttachmentType (VkFormat vkFormat, deBool useFormatCompCount)
4290 {
4291         const tcu::TextureFormat                format                  = mapVkFormat(vkFormat);
4292         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
4293         const size_t                                    componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4294
4295         switch (channelClass)
4296         {
4297                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
4298                         if (useFormatCompCount)
4299                                 return (componentCount == 1 ? "int" : "ivec" + de::toString(componentCount));
4300                         else
4301                                 return "ivec4";
4302
4303                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
4304                         if (useFormatCompCount)
4305                                 return (componentCount == 1 ? "uint" : "uvec" + de::toString(componentCount));
4306                         else
4307                                 return "uvec4";
4308
4309                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
4310                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
4311                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
4312                         if (useFormatCompCount)
4313                                 return (componentCount == 1 ? "float" : "vec" + de::toString(componentCount));
4314                         else
4315                                 return "vec4";
4316
4317                 default:
4318                         DE_FATAL("Unknown channel class");
4319                         return "";
4320         }
4321 }
4322
4323 void createTestShaders (SourceCollections& dst, TestConfig config)
4324 {
4325         if (config.renderTypes & TestConfig::RENDERTYPES_DRAW)
4326         {
4327                 const vector<Subpass>&  subpasses       = config.renderPass.getSubpasses();
4328
4329                 for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
4330                 {
4331                         const Subpass&          subpass                                 = subpasses[subpassNdx];
4332                         deUint32                        inputAttachmentBinding  = 0;
4333                         std::ostringstream      vertexShader;
4334                         std::ostringstream      fragmentShader;
4335
4336                         vertexShader << "#version 310 es\n"
4337                                                  << "layout(location = 0) in highp vec2 a_position;\n"
4338                                                  << "void main (void) {\n"
4339                                                  << "\tgl_Position = vec4(a_position, 1.0, 1.0);\n"
4340                                                  << "}\n";
4341
4342                         fragmentShader << "#version 310 es\n"
4343                                                    << "precision highp float;\n";
4344
4345                         bool hasAnyDepthFormats = false;
4346
4347                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4348                         {
4349                                 const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4350                                 const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4351                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4352                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4353                                 const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
4354                                 const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
4355
4356                                 if (isDepthFormat || isStencilFormat)
4357                                 {
4358                                         if (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4359                                         {
4360                                                 hasAnyDepthFormats = true;
4361                                                 fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp subpassInput i_depth" << attachmentNdx << ";\n";
4362                                                 inputAttachmentBinding++;
4363                                         }
4364
4365                                         if (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4366                                         {
4367                                                 fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp usubpassInput i_stencil" << attachmentNdx << ";\n";
4368                                                 inputAttachmentBinding++;
4369                                         }
4370                                 }
4371                                 else
4372                                 {
4373                                         const std::string attachmentType = getInputAttachmentType(attachment.getFormat());
4374
4375                                         fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp " << attachmentType << " i_color" << attachmentNdx << ";\n";
4376                                         inputAttachmentBinding++;
4377                                 }
4378                         }
4379
4380                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4381                         {
4382                                 const std::string attachmentType = getAttachmentType(config.renderPass.getAttachments()[getAttachmentNdx(subpass.getColorAttachments(), attachmentNdx)].getFormat(), config.useFormatCompCount);
4383                                 fragmentShader << "layout(location = " << attachmentNdx << ") out highp " << attachmentType << " o_color" << attachmentNdx << ";\n";
4384                         }
4385
4386                         if (hasAnyDepthFormats)
4387                                 fragmentShader << "\nbool depthsEqual(float a, float b, float epsilon) {\n"
4388                                                                 << "\treturn abs(a - b) <= epsilon;\n}\n\n";
4389
4390                         fragmentShader << "void main (void) {\n";
4391
4392                         if (subpass.getInputAttachments().empty())
4393                         {
4394                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4395                                 {
4396                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4397
4398                                         if (attachmentIndex == VK_ATTACHMENT_UNUSED)
4399                                                 continue;
4400
4401                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4402                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4403                                         const size_t                            componentCount  = config.useFormatCompCount ? (size_t)tcu::getNumUsedChannels(format.order) : 4;
4404                                         const std::string                       attachmentType  = getAttachmentType(attachment.getFormat(), config.useFormatCompCount);
4405
4406                                         fragmentShader << "\to_color" << attachmentNdx << " = " << attachmentType << "(" << attachmentType + "(";
4407
4408                                         for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4409                                         {
4410                                                 const size_t    index   = subpassNdx + attachmentIndex + compNdx;
4411                                                 const BoolOp    op              = boolOpFromIndex(index);
4412
4413                                                 if (compNdx > 0)
4414                                                         fragmentShader << ",\n\t\t";
4415
4416                                                 fragmentShader  << "((int(gl_FragCoord.x) % 2 == " << (index % 2)
4417                                                                                 << ") " << boolOpToString(op) << " ("
4418                                                                                 << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4419                                                                                 << ") ? 1.0 : 0.0)";
4420                                         }
4421
4422                                         fragmentShader << "));\n";
4423                                 }
4424
4425                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4426                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4427                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4428                                 {
4429                                         const size_t    index   = subpassNdx + 1;
4430                                         const BoolOp    op              = boolOpFromIndex(index);
4431
4432                                         fragmentShader  << "\tgl_FragDepth = ((int(gl_FragCoord.x) % 2 == " << (index % 2)
4433                                                                         << ") " << boolOpToString(op) << " ("
4434                                                                         << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4435                                                                         << ") ? " << deUint32(config.depthValues[1]) << ".0f/255.0f : " << deUint32(config.depthValues[0]) << ".0f/255.0f);\n";
4436                                 }
4437                         }
4438                         else
4439                         {
4440                                 size_t  inputComponentCount             = 0;
4441                                 size_t  outputComponentCount    = 0;
4442
4443                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4444                                 {
4445                                         const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4446                                         const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4447                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4448                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4449                                         const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4450
4451                                         if (layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4452                                                 inputComponentCount += 1;
4453                                         else if (layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4454                                                 inputComponentCount += 1;
4455                                         else
4456                                                 inputComponentCount += componentCount;
4457                                 }
4458
4459                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4460                                 {
4461                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4462                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4463                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4464                                         const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4465
4466                                         outputComponentCount += componentCount;
4467                                 }
4468
4469                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4470                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4471                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4472                                 {
4473                                         outputComponentCount++;
4474                                 }
4475
4476                                 if (outputComponentCount > 0)
4477                                 {
4478                                         const size_t inputsPerOutput = inputComponentCount >= outputComponentCount
4479                                                                                                         ? ((inputComponentCount / outputComponentCount)
4480                                                                                                                 + ((inputComponentCount % outputComponentCount) != 0 ? 1 : 0))
4481                                                                                                         : 1;
4482
4483                                         fragmentShader << "\tbool inputs[" << inputComponentCount << "];\n";
4484
4485                                         if (outputComponentCount > 0)
4486                                                 fragmentShader << "\tbool outputs[" << outputComponentCount << "];\n";
4487
4488                                         size_t inputValueNdx = 0;
4489
4490                                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4491                                         {
4492                                                 const char* const       components[]    =
4493                                                 {
4494                                                         "x", "y", "z", "w"
4495                                                 };
4496                                                 const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4497                                                 const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4498                                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4499                                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4500                                                 const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4501                                                 const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
4502                                                 const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
4503
4504                                                 if (isDepthFormat || isStencilFormat)
4505                                                 {
4506                                                         if (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4507                                                         {
4508                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = depthsEqual(" << deUint32(config.depthValues[1]) <<
4509                                                                         ".0f/255.0f, float(subpassLoad(i_depth" << attachmentNdx << ").x), " <<
4510                                                                         std::fixed << std::setprecision(12) << requiredDepthEpsilon(attachment.getFormat()) << ");\n";
4511                                                                 inputValueNdx++;
4512                                                         }
4513
4514                                                         if (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4515                                                         {
4516                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = 255u == subpassLoad(i_stencil" << attachmentNdx << ").x;\n";
4517                                                                 inputValueNdx++;
4518                                                         }
4519                                                 }
4520                                                 else
4521                                                 {
4522                                                         for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4523                                                         {
4524                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = 1.0 == float(subpassLoad(i_color" << attachmentNdx << ")." << components[compNdx] << ");\n";
4525                                                                 inputValueNdx++;
4526                                                         }
4527                                                 }
4528                                         }
4529
4530                                         size_t outputValueNdx = 0;
4531
4532                                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4533                                         {
4534                                                 const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4535                                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4536                                                 const std::string                       attachmentType  = getAttachmentType(config.renderPass.getAttachments()[attachmentIndex].getFormat(), config.useFormatCompCount);
4537                                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4538                                                 const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4539
4540                                                 for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4541                                                 {
4542                                                         const size_t    index   = subpassNdx + attachmentIndex + outputValueNdx;
4543                                                         const BoolOp    op              = boolOpFromIndex(index);
4544
4545                                                         fragmentShader << "\toutputs[" << outputValueNdx + compNdx << "] = "
4546                                                                                         << "(int(gl_FragCoord.x) % 2 == " << (index % 2)
4547                                                                                         << ") " << boolOpToString(op) << " ("
4548                                                                                         << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4549                                                                                         << ");\n";
4550
4551                                                         for (size_t i = 0; i < inputsPerOutput; i++)
4552                                                                 fragmentShader << "\toutputs[" << outputValueNdx + compNdx << "] = outputs[" << outputValueNdx + compNdx << "] == inputs[" <<  ((outputValueNdx + compNdx) * inputsPerOutput + i) %  inputComponentCount << "];\n";
4553                                                 }
4554
4555                                                 fragmentShader << "\to_color" << attachmentNdx << " = " << attachmentType << "(";
4556
4557                                                 for (size_t compNdx = 0; compNdx < (config.useFormatCompCount ? componentCount : 4); compNdx++)
4558                                                 {
4559                                                         if (compNdx > 0)
4560                                                                 fragmentShader << ", ";
4561
4562                                                         if (compNdx < componentCount)
4563                                                                 fragmentShader << "outputs[" << outputValueNdx + compNdx << "]";
4564                                                         else
4565                                                                 fragmentShader << "0";
4566                                                 }
4567
4568                                                 outputValueNdx += componentCount;
4569
4570                                                 fragmentShader << ");\n";
4571                                         }
4572
4573                                         if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4574                                                 && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4575                                                 && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4576                                         {
4577                                                 const deUint32  attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
4578                                                 const size_t    index                   = subpassNdx + attachmentIndex;
4579                                                 const BoolOp    op                              = boolOpFromIndex(index);
4580
4581                                                 fragmentShader << "\toutputs[" << outputValueNdx << "] = "
4582                                                                                 << "(int(gl_FragCoord.x) % 2 == " << (index % 2)
4583                                                                                 << ") " << boolOpToString(op) << " ("
4584                                                                                 << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4585                                                                                 << ");\n";
4586
4587                                                 for (size_t i = 0; i < inputsPerOutput; i++)
4588                                                         fragmentShader << "\toutputs[" << outputValueNdx << "] = outputs[" << outputValueNdx << "] == inputs[" <<  (outputValueNdx * inputsPerOutput + i) %  inputComponentCount << "];\n";
4589
4590                                                 fragmentShader << "\tgl_FragDepth = outputs[" << outputValueNdx << "] ? " << deUint32(config.depthValues[1]) << ".0f/255.0f : " << deUint32(config.depthValues[0]) << ".0f/255.0f;\n";
4591                                         }
4592                                 }
4593                         }
4594
4595                         fragmentShader << "}\n";
4596
4597                         dst.glslSources.add(de::toString(subpassNdx) + "-vert") << glu::VertexSource(vertexShader.str());
4598                         dst.glslSources.add(de::toString(subpassNdx) + "-frag") << glu::FragmentSource(fragmentShader.str());
4599                 }
4600         }
4601 }
4602
4603 void initializeAttachmentIsLazy (vector<bool>& attachmentIsLazy, const vector<Attachment>& attachments, TestConfig::ImageMemory imageMemory)
4604 {
4605         bool lastAttachmentWasLazy      = false;
4606
4607         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4608         {
4609                 if (attachments[attachmentNdx].getLoadOp() != VK_ATTACHMENT_LOAD_OP_LOAD
4610                         && attachments[attachmentNdx].getStoreOp() != VK_ATTACHMENT_STORE_OP_STORE
4611                         && attachments[attachmentNdx].getStencilLoadOp() != VK_ATTACHMENT_LOAD_OP_LOAD
4612                         && attachments[attachmentNdx].getStencilStoreOp() != VK_ATTACHMENT_STORE_OP_STORE)
4613                 {
4614                         if (imageMemory == TestConfig::IMAGEMEMORY_LAZY || (imageMemory & TestConfig::IMAGEMEMORY_LAZY && !lastAttachmentWasLazy))
4615                         {
4616                                 attachmentIsLazy.push_back(true);
4617
4618                                 lastAttachmentWasLazy   = true;
4619                         }
4620                         else if (imageMemory & TestConfig::IMAGEMEMORY_STRICT)
4621                         {
4622                                 attachmentIsLazy.push_back(false);
4623                                 lastAttachmentWasLazy = false;
4624                         }
4625                         else
4626                                 DE_FATAL("Unknown imageMemory");
4627                 }
4628                 else
4629                         attachmentIsLazy.push_back(false);
4630         }
4631 }
4632
4633 enum AttachmentRefType
4634 {
4635         ATTACHMENTREFTYPE_COLOR,
4636         ATTACHMENTREFTYPE_DEPTH_STENCIL,
4637         ATTACHMENTREFTYPE_INPUT,
4638         ATTACHMENTREFTYPE_RESOLVE,
4639 };
4640
4641 VkImageUsageFlags getImageUsageFromLayout (VkImageLayout layout)
4642 {
4643         switch (layout)
4644         {
4645                 case VK_IMAGE_LAYOUT_GENERAL:
4646                 case VK_IMAGE_LAYOUT_PREINITIALIZED:
4647                         return 0;
4648
4649                 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
4650                         return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4651
4652                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
4653                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
4654                         return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4655
4656                 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
4657                         return VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
4658
4659                 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
4660                         return VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
4661
4662                 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
4663                         return VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4664
4665                 default:
4666                         DE_FATAL("Unexpected image layout");
4667                         return 0;
4668         }
4669 }
4670
4671 void getImageUsageFromAttachmentReferences(vector<VkImageUsageFlags>& attachmentImageUsage, AttachmentRefType refType, size_t count, const AttachmentReference* references)
4672 {
4673         for (size_t referenceNdx = 0; referenceNdx < count; ++referenceNdx)
4674         {
4675                 const deUint32 attachment = references[referenceNdx].getAttachment();
4676
4677                 if (attachment != VK_ATTACHMENT_UNUSED)
4678                 {
4679                         VkImageUsageFlags usage;
4680
4681                         switch (refType)
4682                         {
4683                                 case ATTACHMENTREFTYPE_COLOR:
4684                                 case ATTACHMENTREFTYPE_RESOLVE:
4685                                         usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4686                                         break;
4687
4688                                 case ATTACHMENTREFTYPE_DEPTH_STENCIL:
4689                                         usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4690                                         break;
4691
4692                                 case ATTACHMENTREFTYPE_INPUT:
4693                                         usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
4694                                         break;
4695
4696                                 default:
4697                                         DE_FATAL("Unexpected attachment reference type");
4698                                         usage = 0;
4699                                         break;
4700                         }
4701
4702                         attachmentImageUsage[attachment] |= usage;
4703                 }
4704         }
4705 }
4706
4707 void getImageUsageFromAttachmentReferences(vector<VkImageUsageFlags>& attachmentImageUsage, AttachmentRefType refType, const vector<AttachmentReference>& references)
4708 {
4709         if (!references.empty())
4710         {
4711                 getImageUsageFromAttachmentReferences(attachmentImageUsage, refType, references.size(), &references[0]);
4712         }
4713 }
4714
4715 void initializeAttachmentImageUsage (Context &context, vector<VkImageUsageFlags>& attachmentImageUsage, const RenderPass& renderPassInfo, const vector<bool>& attachmentIsLazy, const vector<Maybe<VkClearValue> >& clearValues)
4716 {
4717         attachmentImageUsage.resize(renderPassInfo.getAttachments().size(), VkImageUsageFlags(0));
4718
4719         for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); ++subpassNdx)
4720         {
4721                 const Subpass& subpass = renderPassInfo.getSubpasses()[subpassNdx];
4722
4723                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_COLOR, subpass.getColorAttachments());
4724                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_DEPTH_STENCIL, 1, &subpass.getDepthStencilAttachment());
4725                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_INPUT, subpass.getInputAttachments());
4726                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_RESOLVE, subpass.getResolveAttachments());
4727         }
4728
4729         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
4730         {
4731                 const Attachment& attachment = renderPassInfo.getAttachments()[attachmentNdx];
4732                 const VkFormatProperties        formatProperties        = getPhysicalDeviceFormatProperties(context.getInstanceInterface(), context.getPhysicalDevice(), attachment.getFormat());
4733                 const VkFormatFeatureFlags      supportedFeatures       = formatProperties.optimalTilingFeatures;
4734
4735                 if ((supportedFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0)
4736                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_SAMPLED_BIT;
4737
4738                 if ((supportedFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) != 0)
4739                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_STORAGE_BIT;
4740
4741                 attachmentImageUsage[attachmentNdx] |= getImageUsageFromLayout(attachment.getInitialLayout());
4742                 attachmentImageUsage[attachmentNdx] |= getImageUsageFromLayout(attachment.getFinalLayout());
4743
4744                 if (!attachmentIsLazy[attachmentNdx])
4745                 {
4746                         if (clearValues[attachmentNdx])
4747                                 attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4748
4749                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
4750                 }
4751                 else
4752                 {
4753                         const VkImageUsageFlags allowedTransientBits = static_cast<VkImageUsageFlags>(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT);
4754
4755                         attachmentImageUsage[attachmentNdx] &= allowedTransientBits;
4756                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
4757                 }
4758         }
4759 }
4760
4761 void initializeSubpassIsSecondary (vector<bool>& subpassIsSecondary, const vector<Subpass>& subpasses, TestConfig::CommandBufferTypes commandBuffer)
4762 {
4763         bool lastSubpassWasSecondary = false;
4764
4765         for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
4766         {
4767                 if (commandBuffer == TestConfig::COMMANDBUFFERTYPES_SECONDARY || (commandBuffer & TestConfig::COMMANDBUFFERTYPES_SECONDARY && !lastSubpassWasSecondary))
4768                 {
4769                         subpassIsSecondary.push_back(true);
4770                         lastSubpassWasSecondary = true;
4771                 }
4772                 else if (commandBuffer & TestConfig::COMMANDBUFFERTYPES_INLINE)
4773                 {
4774                         subpassIsSecondary.push_back(false);
4775                         lastSubpassWasSecondary = false;
4776                 }
4777                 else
4778                         DE_FATAL("Unknown commandBuffer");
4779         }
4780 }
4781
4782 void initializeImageClearValues (de::Random& rng, vector<Maybe<VkClearValue> >& clearValues, const vector<Attachment>& attachments, const vector<bool>& isLazy, deBool useFormatCompCount, const DepthValuesArray& depthValues)
4783 {
4784         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4785         {
4786                 if (!isLazy[attachmentNdx])
4787                         clearValues.push_back(just(randomClearValue(attachments[attachmentNdx], rng, useFormatCompCount, depthValues)));
4788                 else
4789                         clearValues.push_back(nothing<VkClearValue>());
4790         }
4791 }
4792
4793 void initializeRenderPassClearValues (de::Random& rng, vector<Maybe<VkClearValue> >& clearValues, const vector<Attachment>& attachments, deBool useFormatCompCount, const DepthValuesArray& depthValues)
4794 {
4795         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4796         {
4797                 if (attachments[attachmentNdx].getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR
4798                         || attachments[attachmentNdx].getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
4799                 {
4800                         clearValues.push_back(just(randomClearValue(attachments[attachmentNdx], rng, useFormatCompCount, depthValues)));
4801                 }
4802                 else
4803                         clearValues.push_back(nothing<VkClearValue>());
4804         }
4805 }
4806
4807 void logSubpassRenderInfo (TestLog& log, const SubpassRenderInfo& info, TestConfig config)
4808 {
4809         log << TestLog::Message << "Viewport, offset: " << info.getViewportOffset() << ", size: " << info.getViewportSize() << TestLog::EndMessage;
4810
4811         if (info.isSecondary())
4812                 log << TestLog::Message << "Subpass uses secondary command buffers" << TestLog::EndMessage;
4813         else
4814                 log << TestLog::Message << "Subpass uses inlined commands" << TestLog::EndMessage;
4815
4816         for (deUint32 attachmentNdx = 0; attachmentNdx < info.getColorClears().size(); attachmentNdx++)
4817         {
4818                 const ColorClear&       colorClear      = info.getColorClears()[attachmentNdx];
4819
4820                 log << TestLog::Message << "Clearing color attachment " << attachmentNdx
4821                         << ". Offset: " << colorClear.getOffset()
4822                         << ", Size: " << colorClear.getSize()
4823                         << ", Color: " << clearColorToString(info.getColorAttachment(attachmentNdx).getFormat(), colorClear.getColor(), config.useFormatCompCount) << TestLog::EndMessage;
4824         }
4825
4826         if (info.getDepthStencilClear())
4827         {
4828                 const DepthStencilClear&        depthStencilClear       = *info.getDepthStencilClear();
4829
4830                 log << TestLog::Message << "Clearing depth stencil attachment"
4831                         << ". Offset: " << depthStencilClear.getOffset()
4832                         << ", Size: " << depthStencilClear.getSize()
4833                         << ", Depth: " << depthStencilClear.getDepth()
4834                         << ", Stencil: " << depthStencilClear.getStencil() << TestLog::EndMessage;
4835         }
4836
4837         if (info.getRenderQuad())
4838         {
4839                 const RenderQuad&       renderQuad      = *info.getRenderQuad();
4840
4841                 log << TestLog::Message << "Rendering grid quad to " << renderQuad.getCornerA() << " -> " << renderQuad.getCornerB() << TestLog::EndMessage;
4842         }
4843 }
4844
4845 void logTestCaseInfo (TestLog&                                                          log,
4846                                           const TestConfig&                                             config,
4847                                           const vector<bool>&                                   attachmentIsLazy,
4848                                           const vector<Maybe<VkClearValue> >&   imageClearValues,
4849                                           const vector<Maybe<VkClearValue> >&   renderPassClearValues,
4850                                           const vector<SubpassRenderInfo>&              subpassRenderInfo)
4851 {
4852         const RenderPass&       renderPass      = config.renderPass;
4853
4854         logRenderPassInfo(log, renderPass);
4855
4856         DE_ASSERT(attachmentIsLazy.size() == renderPass.getAttachments().size());
4857         DE_ASSERT(imageClearValues.size() == renderPass.getAttachments().size());
4858         DE_ASSERT(renderPassClearValues.size() == renderPass.getAttachments().size());
4859
4860         log << TestLog::Message << "TargetSize: " << config.targetSize << TestLog::EndMessage;
4861         log << TestLog::Message << "Render area, Offset: " << config.renderPos << ", Size: " << config.renderSize << TestLog::EndMessage;
4862
4863         for (size_t attachmentNdx = 0; attachmentNdx < attachmentIsLazy.size(); attachmentNdx++)
4864         {
4865                 const tcu::ScopedLogSection     section (log, "Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx));
4866
4867                 if (attachmentIsLazy[attachmentNdx])
4868                         log << TestLog::Message << "Is lazy." << TestLog::EndMessage;
4869
4870                 if (imageClearValues[attachmentNdx])
4871                         log << TestLog::Message << "Image is cleared to " << clearValueToString(renderPass.getAttachments()[attachmentNdx].getFormat(),
4872                                         *imageClearValues[attachmentNdx], config.useFormatCompCount) << " before rendering." << TestLog::EndMessage;
4873
4874                 if (renderPass.getAttachments()[attachmentNdx].getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR && renderPassClearValues[attachmentNdx])
4875                         log << TestLog::Message << "Attachment is cleared to " << clearValueToString(renderPass.getAttachments()[attachmentNdx].getFormat(),
4876                                         *renderPassClearValues[attachmentNdx], config.useFormatCompCount) << " in the beginning of the render pass." << TestLog::EndMessage;
4877         }
4878
4879         for (size_t subpassNdx = 0; subpassNdx < renderPass.getSubpasses().size(); subpassNdx++)
4880         {
4881                 const tcu::ScopedLogSection section (log, "Subpass" + de::toString(subpassNdx), "Subpass " + de::toString(subpassNdx));
4882
4883                 logSubpassRenderInfo(log, subpassRenderInfo[subpassNdx], config);
4884         }
4885 }
4886
4887 float roundToViewport (float x, deUint32 offset, deUint32 size)
4888 {
4889         const float             origin  = (float)(offset) + ((float(size) / 2.0f));
4890         const float             p               = (float)(size) / 2.0f;
4891         const deInt32   xi              = deRoundFloatToInt32(origin + (p * x));
4892
4893         return (((float)xi) - origin) / p;
4894 }
4895
4896 void initializeSubpassRenderInfo (vector<SubpassRenderInfo>& renderInfos, de::Random& rng, const RenderPass& renderPass, const TestConfig& config)
4897 {
4898         const TestConfig::CommandBufferTypes    commandBuffer                   = config.commandBufferTypes;
4899         const vector<Subpass>&                                  subpasses                               = renderPass.getSubpasses();
4900         bool                                                                    lastSubpassWasSecondary = false;
4901
4902         for (deUint32 subpassNdx = 0; subpassNdx < (deUint32)subpasses.size(); subpassNdx++)
4903         {
4904                 const Subpass&                          subpass                         = subpasses[subpassNdx];
4905                 const bool                                      subpassIsSecondary      = commandBuffer == TestConfig::COMMANDBUFFERTYPES_SECONDARY
4906                                                                                                                 || (commandBuffer & TestConfig::COMMANDBUFFERTYPES_SECONDARY && !lastSubpassWasSecondary) ? true : false;
4907                 const bool                                      omitBlendState          = subpass.getOmitBlendState();
4908                 const UVec2                                     viewportSize            ((config.renderSize * UVec2(2)) / UVec2(3));
4909                 const UVec2                                     viewportOffset          (config.renderPos.x() + (subpassNdx % 2) * (config.renderSize.x() / 3),
4910                                                                                                                  config.renderPos.y() + ((subpassNdx / 2) % 2) * (config.renderSize.y() / 3));
4911
4912                 vector<ColorClear>                      colorClears;
4913                 Maybe<DepthStencilClear>        depthStencilClear;
4914                 Maybe<RenderQuad>                       renderQuad;
4915
4916                 lastSubpassWasSecondary         = subpassIsSecondary;
4917
4918                 if (config.renderTypes & TestConfig::RENDERTYPES_CLEAR)
4919                 {
4920                         const vector<AttachmentReference>&      colorAttachments        = subpass.getColorAttachments();
4921
4922                         for (size_t attachmentRefNdx = 0; attachmentRefNdx < colorAttachments.size(); attachmentRefNdx++)
4923                         {
4924                                 const AttachmentReference&      attachmentRef   = colorAttachments[attachmentRefNdx];
4925                                 const Attachment&                       attachment              = renderPass.getAttachments()[attachmentRef.getAttachment()];
4926                                 const UVec2                                     size                    ((viewportSize * UVec2(2)) / UVec2(3));
4927                                 const UVec2                                     offset                  (viewportOffset.x() + ((deUint32)attachmentRefNdx % 2u) * (viewportSize.x() / 3u),
4928                                                                                                                          viewportOffset.y() + (((deUint32)attachmentRefNdx / 2u) % 2u) * (viewportSize.y() / 3u));
4929                                 const VkClearColorValue         color                   = randomColorClearValue(attachment, rng, config.useFormatCompCount);
4930
4931                                 colorClears.push_back(ColorClear(offset, size, color));
4932                         }
4933
4934                         if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
4935                         {
4936                                 const Attachment&       attachment      = renderPass.getAttachments()[subpass.getDepthStencilAttachment().getAttachment()];
4937                                 const UVec2                     size            ((viewportSize * UVec2(2)) / UVec2(3));
4938                                 const UVec2                     offset          (viewportOffset.x() + ((deUint32)colorAttachments.size() % 2u) * (viewportSize.x() / 3u),
4939                                                                                                  viewportOffset.y() + (((deUint32)colorAttachments.size() / 2u) % 2u) * (viewportSize.y() / 3u));
4940                                 const VkClearValue      value           = randomClearValue(attachment, rng, config.useFormatCompCount, config.depthValues);
4941
4942                                 depthStencilClear = tcu::just(DepthStencilClear(offset, size, value.depthStencil.depth, value.depthStencil.stencil));
4943                         }
4944                 }
4945
4946                 if (config.renderTypes & TestConfig::RENDERTYPES_DRAW)
4947                 {
4948                         const float     w       = (subpassNdx % 2) == 0 ? 1.0f : 1.25f;
4949                         const float     h       = (subpassNdx % 2) == 0 ? 1.25f : 1.0f;
4950
4951                         const float     x0      = roundToViewport((subpassNdx % 2) == 0 ? 1.0f - w : -1.0f, viewportOffset.x(), viewportSize.x());
4952                         const float     x1      = roundToViewport((subpassNdx % 2) == 0 ? 1.0f : -1.0f + w, viewportOffset.x(), viewportSize.x());
4953
4954                         const float     y0      = roundToViewport(((subpassNdx / 2) % 2) == 0 ? 1.0f - h : -1.0f, viewportOffset.y(), viewportSize.y());
4955                         const float     y1      = roundToViewport(((subpassNdx / 2) % 2) == 0 ? 1.0f : -1.0f + h, viewportOffset.y(), viewportSize.y());
4956
4957                         renderQuad = tcu::just(RenderQuad(tcu::Vec2(x0, y0), tcu::Vec2(x1, y1)));
4958                 }
4959
4960                 renderInfos.push_back(SubpassRenderInfo(renderPass, subpassNdx, config.drawStartNdx, subpassIsSecondary, omitBlendState, viewportOffset, viewportSize, renderQuad, colorClears, depthStencilClear));
4961         }
4962 }
4963
4964 void checkTextureFormatSupport (TestLog&                                        log,
4965                                                                 const InstanceInterface&        vk,
4966                                                                 VkPhysicalDevice                        device,
4967                                                                 const vector<Attachment>&       attachments)
4968 {
4969         bool supported = true;
4970
4971         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4972         {
4973                 const Attachment&                       attachment                                      = attachments[attachmentNdx];
4974                 const tcu::TextureFormat        format                                          = mapVkFormat(attachment.getFormat());
4975                 const bool                                      isDepthOrStencilAttachment      = hasDepthComponent(format.order) || hasStencilComponent(format.order);
4976                 const VkFormatFeatureFlags      flags                                           = isDepthOrStencilAttachment? VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
4977                 VkFormatProperties                      properties;
4978
4979                 vk.getPhysicalDeviceFormatProperties(device, attachment.getFormat(), &properties);
4980
4981                 if ((properties.optimalTilingFeatures & flags) != flags)
4982                 {
4983                         supported = false;
4984                         log << TestLog::Message << "Format: " << attachment.getFormat() << " not supported as " << (isDepthOrStencilAttachment ? "depth stencil attachment" : "color attachment") << TestLog::EndMessage;
4985                 }
4986         }
4987
4988         if (!supported)
4989                 TCU_THROW(NotSupportedError, "Format not supported");
4990 }
4991
4992 tcu::TestStatus renderPassTest (Context& context, TestConfig config)
4993 {
4994         const UVec2                                                     targetSize                      = config.targetSize;
4995         const UVec2                                                     renderPos                       = config.renderPos;
4996         const UVec2                                                     renderSize                      = config.renderSize;
4997         const RenderPass&                                       renderPassInfo          = config.renderPass;
4998
4999         TestLog&                                                        log                                     = context.getTestContext().getLog();
5000         de::Random                                                      rng                                     (config.seed);
5001
5002         vector<bool>                                            attachmentIsLazy;
5003         vector<VkImageUsageFlags>                       attachmentImageUsage;
5004         vector<Maybe<VkClearValue> >            imageClearValues;
5005         vector<Maybe<VkClearValue> >            renderPassClearValues;
5006
5007         vector<bool>                                            subpassIsSecondary;
5008         vector<SubpassRenderInfo>                       subpassRenderInfo;
5009
5010         if (config.renderingType == RENDERING_TYPE_RENDERPASS2)
5011                 context.requireDeviceFunctionality("VK_KHR_create_renderpass2");
5012
5013         if (config.renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
5014                 context.requireDeviceFunctionality("VK_KHR_dynamic_rendering");
5015
5016         if (config.allocationKind == ALLOCATION_KIND_DEDICATED)
5017         {
5018                 if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
5019                         TCU_THROW(NotSupportedError, "VK_KHR_dedicated_allocation is not supported");
5020         }
5021
5022         if (!renderPassInfo.getInputAspects().empty())
5023         {
5024                 if (!context.isDeviceFunctionalitySupported("VK_KHR_maintenance2"))
5025                         TCU_THROW(NotSupportedError, "Extension VK_KHR_maintenance2 not supported.");
5026         }
5027
5028         {
5029                 bool requireDepthStencilLayout = false;
5030
5031                 for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
5032                 {
5033                         if (renderPassInfo.getAttachments()[attachmentNdx].getInitialLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5034                                 || renderPassInfo.getAttachments()[attachmentNdx].getInitialLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
5035                                 || renderPassInfo.getAttachments()[attachmentNdx].getFinalLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5036                                 || renderPassInfo.getAttachments()[attachmentNdx].getFinalLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5037                         {
5038                                 requireDepthStencilLayout = true;
5039                                 break;
5040                         }
5041                 }
5042
5043                 for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size() && !requireDepthStencilLayout; subpassNdx++)
5044                 {
5045                         const Subpass& subpass (renderPassInfo.getSubpasses()[subpassNdx]);
5046
5047                         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
5048                         {
5049                                 if (subpass.getColorAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5050                                         || subpass.getColorAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5051                                 {
5052                                         requireDepthStencilLayout = true;
5053                                         break;
5054                                 }
5055                         }
5056
5057                         for (size_t attachmentNdx = 0; !requireDepthStencilLayout && attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
5058                         {
5059                                 if (subpass.getInputAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5060                                         || subpass.getInputAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5061                                 {
5062                                         requireDepthStencilLayout = true;
5063                                         break;
5064                                 }
5065                         }
5066
5067                         for (size_t attachmentNdx = 0; !requireDepthStencilLayout && attachmentNdx < subpass.getResolveAttachments().size(); attachmentNdx++)
5068                         {
5069                                 if (subpass.getResolveAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5070                                         || subpass.getResolveAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5071                                 {
5072                                         requireDepthStencilLayout = true;
5073                                         break;
5074                                 }
5075                         }
5076
5077                         if (subpass.getDepthStencilAttachment().getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5078                                 || subpass.getDepthStencilAttachment().getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5079                         {
5080                                 requireDepthStencilLayout = true;
5081                                 break;
5082                         }
5083                 }
5084
5085                 if (requireDepthStencilLayout && !context.isDeviceFunctionalitySupported("VK_KHR_maintenance2"))
5086                         TCU_THROW(NotSupportedError, "VK_KHR_maintenance2 is not supported");
5087         }
5088
5089         initializeAttachmentIsLazy(attachmentIsLazy, renderPassInfo.getAttachments(), config.imageMemory);
5090         initializeImageClearValues(rng, imageClearValues, renderPassInfo.getAttachments(), attachmentIsLazy, config.useFormatCompCount, config.depthValues);
5091         initializeAttachmentImageUsage(context, attachmentImageUsage, renderPassInfo, attachmentIsLazy, imageClearValues);
5092         initializeRenderPassClearValues(rng, renderPassClearValues, renderPassInfo.getAttachments(), config.useFormatCompCount, config.depthValues);
5093
5094         initializeSubpassIsSecondary(subpassIsSecondary, renderPassInfo.getSubpasses(), config.commandBufferTypes);
5095         initializeSubpassRenderInfo(subpassRenderInfo, rng, renderPassInfo, config);
5096
5097         logTestCaseInfo(log, config, attachmentIsLazy, imageClearValues, renderPassClearValues, subpassRenderInfo);
5098
5099         checkTextureFormatSupport(log, context.getInstanceInterface(), context.getPhysicalDevice(), config.renderPass.getAttachments());
5100
5101         {
5102                 const vk::VkPhysicalDeviceProperties properties = vk::getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice());
5103
5104                 log << TestLog::Message << "Max color attachments: " << properties.limits.maxColorAttachments << TestLog::EndMessage;
5105
5106                 for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); subpassNdx++)
5107                 {
5108                          if (renderPassInfo.getSubpasses()[subpassNdx].getColorAttachments().size() > (size_t)properties.limits.maxColorAttachments)
5109                                  TCU_THROW(NotSupportedError, "Subpass uses more than maxColorAttachments.");
5110                 }
5111         }
5112
5113         {
5114                 const InstanceInterface&                                        vki                                                                     = context.getInstanceInterface();
5115                 const VkPhysicalDevice&                                         physDevice                                                      = context.getPhysicalDevice();
5116                 const VkDevice                                                          device                                                          = context.getDevice();
5117                 const DeviceInterface&                                          vk                                                                      = context.getDeviceInterface();
5118                 const VkQueue                                                           queue                                                           = context.getUniversalQueue();
5119                 const deUint32                                                          queueIndex                                                      = context.getUniversalQueueFamilyIndex();
5120                 Allocator&                                                                      allocator                                                       = context.getDefaultAllocator();
5121
5122                 const Unique<VkCommandPool>                                     commandBufferPool                                       (createCommandPool(vk, device, 0, queueIndex));
5123                 const Unique<VkCommandBuffer>                           initializeImagesCommandBuffer           (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5124                 const Unique<VkCommandBuffer>                           renderCommandBuffer                                     (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5125                 const Unique<VkCommandBuffer>                           readImagesToBuffersCommandBuffer        (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5126
5127                 vector<de::SharedPtr<AttachmentResources> >     attachmentResources;
5128                 vector<de::SharedPtr<SubpassRenderer> >         subpassRenderers;
5129                 vector<VkImage>                                                         attachmentImages;
5130                 vector<VkImageView>                                                     attachmentViews;
5131                 vector<pair<VkImageView, VkImageView> >         inputAttachmentViews;
5132
5133                 Move<VkRenderPass> renderPass;
5134                 if (config.renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
5135                         renderPass = createRenderPass(vk, device, renderPassInfo, config.renderingType);
5136
5137                 for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
5138                 {
5139                         const Attachment&       attachmentInfo  = renderPassInfo.getAttachments()[attachmentNdx];
5140
5141                         attachmentResources.push_back(de::SharedPtr<AttachmentResources>(new AttachmentResources(vki, physDevice, vk, device, allocator, queueIndex, targetSize, attachmentInfo, attachmentImageUsage[attachmentNdx], config.allocationKind)));
5142                         attachmentViews.push_back(attachmentResources[attachmentNdx]->getAttachmentView());
5143                         attachmentImages.push_back(attachmentResources[attachmentNdx]->getImage());
5144
5145                         inputAttachmentViews.push_back(attachmentResources[attachmentNdx]->getInputAttachmentViews());
5146                 }
5147
5148                 beginCommandBuffer(vk, *initializeImagesCommandBuffer, (VkCommandBufferUsageFlags)0, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5149                 pushImageInitializationCommands(vk, *initializeImagesCommandBuffer, renderPassInfo.getAttachments(), attachmentResources, queueIndex, imageClearValues);
5150                 endCommandBuffer(vk, *initializeImagesCommandBuffer);
5151
5152                 {
5153                         Move<VkFramebuffer> framebuffer;
5154                         if (config.renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
5155                                 framebuffer = createFramebuffer(vk, device, *renderPass, targetSize, attachmentViews);
5156
5157                         for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); subpassNdx++)
5158                                 subpassRenderers.push_back(de::SharedPtr<SubpassRenderer>(new SubpassRenderer(context, vk, device, allocator, *renderPass, *framebuffer, *commandBufferPool, queueIndex, attachmentImages, inputAttachmentViews, subpassRenderInfo[subpassNdx], config.renderPass.getAttachments(), config.allocationKind, config.renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)));
5159
5160                         beginCommandBuffer(vk, *renderCommandBuffer, (VkCommandBufferUsageFlags)0, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5161                         pushRenderPassCommands(vk, *renderCommandBuffer, *renderPass, renderPassInfo, attachmentResources, *framebuffer, subpassRenderers, renderPos, renderSize, renderPassClearValues, queueIndex, config.renderTypes, config.renderingType);
5162                         endCommandBuffer(vk, *renderCommandBuffer);
5163
5164                         beginCommandBuffer(vk, *readImagesToBuffersCommandBuffer, (VkCommandBufferUsageFlags)0, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5165                         pushReadImagesToBuffers(vk, *readImagesToBuffersCommandBuffer, queueIndex, attachmentResources, renderPassInfo.getAttachments(), attachmentIsLazy, targetSize);
5166                         endCommandBuffer(vk, *readImagesToBuffersCommandBuffer);
5167                         {
5168                                 const VkCommandBuffer commandBuffers[] =
5169                                 {
5170                                         *initializeImagesCommandBuffer,
5171                                         *renderCommandBuffer,
5172                                         *readImagesToBuffersCommandBuffer
5173                                 };
5174                                 const Unique<VkFence>   fence           (createFence(vk, device, 0u));
5175
5176                                 queueSubmit(vk, queue, DE_LENGTH_OF_ARRAY(commandBuffers), commandBuffers, *fence);
5177                                 waitForFences(vk, device, 1, &fence.get(), VK_TRUE, ~0ull);
5178                         }
5179                 }
5180
5181                 if (logAndVerifyImages(log, vk, device, attachmentResources, attachmentIsLazy, renderPassInfo, renderPassClearValues, imageClearValues, subpassRenderInfo, targetSize, config))
5182                         return tcu::TestStatus::pass("Pass");
5183                 else
5184                         return tcu::TestStatus::fail("Result verification failed");
5185         }
5186 }
5187
5188 static const VkFormat s_coreColorFormats[] =
5189 {
5190         VK_FORMAT_R5G6B5_UNORM_PACK16,
5191         VK_FORMAT_R8_UNORM,
5192         VK_FORMAT_R8_SNORM,
5193         VK_FORMAT_R8_UINT,
5194         VK_FORMAT_R8_SINT,
5195         VK_FORMAT_R8G8_UNORM,
5196         VK_FORMAT_R8G8_SNORM,
5197         VK_FORMAT_R8G8_UINT,
5198         VK_FORMAT_R8G8_SINT,
5199         VK_FORMAT_R8G8B8A8_UNORM,
5200         VK_FORMAT_R8G8B8A8_SNORM,
5201         VK_FORMAT_R8G8B8A8_UINT,
5202         VK_FORMAT_R8G8B8A8_SINT,
5203         VK_FORMAT_R8G8B8A8_SRGB,
5204         VK_FORMAT_A8B8G8R8_UNORM_PACK32,
5205         VK_FORMAT_A8B8G8R8_SNORM_PACK32,
5206         VK_FORMAT_A8B8G8R8_UINT_PACK32,
5207         VK_FORMAT_A8B8G8R8_SINT_PACK32,
5208         VK_FORMAT_A8B8G8R8_SRGB_PACK32,
5209         VK_FORMAT_B8G8R8A8_UNORM,
5210         VK_FORMAT_B8G8R8A8_SRGB,
5211         VK_FORMAT_A2R10G10B10_UNORM_PACK32,
5212         VK_FORMAT_A2B10G10R10_UNORM_PACK32,
5213         VK_FORMAT_A2B10G10R10_UINT_PACK32,
5214         VK_FORMAT_R16_UNORM,
5215         VK_FORMAT_R16_SNORM,
5216         VK_FORMAT_R16_UINT,
5217         VK_FORMAT_R16_SINT,
5218         VK_FORMAT_R16_SFLOAT,
5219         VK_FORMAT_R16G16_UNORM,
5220         VK_FORMAT_R16G16_SNORM,
5221         VK_FORMAT_R16G16_UINT,
5222         VK_FORMAT_R16G16_SINT,
5223         VK_FORMAT_R16G16_SFLOAT,
5224         VK_FORMAT_R16G16B16A16_UNORM,
5225         VK_FORMAT_R16G16B16A16_SNORM,
5226         VK_FORMAT_R16G16B16A16_UINT,
5227         VK_FORMAT_R16G16B16A16_SINT,
5228         VK_FORMAT_R16G16B16A16_SFLOAT,
5229         VK_FORMAT_R32_UINT,
5230         VK_FORMAT_R32_SINT,
5231         VK_FORMAT_R32_SFLOAT,
5232         VK_FORMAT_R32G32_UINT,
5233         VK_FORMAT_R32G32_SINT,
5234         VK_FORMAT_R32G32_SFLOAT,
5235         VK_FORMAT_R32G32B32A32_UINT,
5236         VK_FORMAT_R32G32B32A32_SINT,
5237         VK_FORMAT_R32G32B32A32_SFLOAT
5238 };
5239
5240 static const VkFormat s_coreDepthStencilFormats[] =
5241 {
5242         VK_FORMAT_D16_UNORM,
5243
5244         VK_FORMAT_X8_D24_UNORM_PACK32,
5245         VK_FORMAT_D32_SFLOAT,
5246
5247         VK_FORMAT_D24_UNORM_S8_UINT,
5248         VK_FORMAT_D32_SFLOAT_S8_UINT
5249 };
5250
5251 void addAttachmentTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5252 {
5253         const deUint32 attachmentCounts[] = { 1, 3, 4, 8 };
5254         const VkAttachmentLoadOp loadOps[] =
5255         {
5256                 VK_ATTACHMENT_LOAD_OP_LOAD,
5257                 VK_ATTACHMENT_LOAD_OP_CLEAR,
5258                 VK_ATTACHMENT_LOAD_OP_DONT_CARE
5259         };
5260
5261         const VkAttachmentStoreOp storeOps[] =
5262         {
5263                 VK_ATTACHMENT_STORE_OP_STORE,
5264                 VK_ATTACHMENT_STORE_OP_DONT_CARE
5265         };
5266
5267         const VkImageLayout initialAndFinalColorLayouts[] =
5268         {
5269                 VK_IMAGE_LAYOUT_GENERAL,
5270                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5271                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5272                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5273                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5274         };
5275
5276         const VkImageLayout initialAndFinalColorLayoutsLazy[] =
5277         {
5278                 VK_IMAGE_LAYOUT_GENERAL,
5279                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5280                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5281         };
5282
5283         const VkImageLayout initialAndFinalDepthStencilLayouts[] =
5284         {
5285                 VK_IMAGE_LAYOUT_GENERAL,
5286                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5287                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5288                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5289                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5290                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5291         };
5292
5293         const VkImageLayout initialAndFinalDepthStencilLayoutsLazy[] =
5294         {
5295                 VK_IMAGE_LAYOUT_GENERAL,
5296                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5297                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5298                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5299         };
5300
5301         const VkImageLayout subpassLayouts[] =
5302         {
5303                 VK_IMAGE_LAYOUT_GENERAL,
5304                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
5305         };
5306
5307         const VkImageLayout depthStencilLayouts[] =
5308         {
5309                 VK_IMAGE_LAYOUT_GENERAL,
5310                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5311         };
5312
5313         const TestConfig::RenderTypes renderCommands[] =
5314         {
5315                 TestConfig::RENDERTYPES_NONE,
5316                 TestConfig::RENDERTYPES_CLEAR,
5317                 TestConfig::RENDERTYPES_DRAW,
5318                 TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW,
5319         };
5320
5321         const TestConfig::CommandBufferTypes commandBuffers[] =
5322         {
5323                 TestConfig::COMMANDBUFFERTYPES_INLINE,
5324                 TestConfig::COMMANDBUFFERTYPES_SECONDARY,
5325                 TestConfig::COMMANDBUFFERTYPES_INLINE|TestConfig::COMMANDBUFFERTYPES_SECONDARY
5326         };
5327
5328         const TestConfig::ImageMemory imageMemories[] =
5329         {
5330                 TestConfig::IMAGEMEMORY_STRICT,
5331                 TestConfig::IMAGEMEMORY_LAZY,
5332                 TestConfig::IMAGEMEMORY_STRICT|TestConfig::IMAGEMEMORY_LAZY
5333         };
5334
5335         const UVec2 targetSizes[] =
5336         {
5337                 UVec2(64, 64),
5338                 UVec2(63, 65)
5339         };
5340
5341         const UVec2 renderPositions[] =
5342         {
5343                 UVec2(0, 0),
5344                 UVec2(3, 17)
5345         };
5346
5347         const UVec2 renderSizes[] =
5348         {
5349                 UVec2(32, 32),
5350                 UVec2(60, 47)
5351         };
5352
5353         tcu::TestContext&       testCtx                                 (group->getTestContext());
5354         bool                            useDynamicRendering             (testConfigExternal.renderingType == RENDERING_TYPE_DYNAMIC_RENDERING);
5355         de::Random                      rng                                             (1433774382u);
5356
5357         for (size_t attachmentCountNdx = 0; attachmentCountNdx < DE_LENGTH_OF_ARRAY(attachmentCounts); attachmentCountNdx++)
5358         {
5359                 const deUint32                                  attachmentCount                 = attachmentCounts[attachmentCountNdx];
5360                 const deUint32                                  testCaseCount                   = (attachmentCount == 1 ? 100 : 200);
5361                 de::MovePtr<tcu::TestCaseGroup> attachmentCountGroup    (new tcu::TestCaseGroup(testCtx, de::toString(attachmentCount).c_str(), de::toString(attachmentCount).c_str()));
5362
5363                 for (size_t testCaseNdx = 0; testCaseNdx < testCaseCount; testCaseNdx++)
5364                 {
5365                         const bool                                              useDepthStencil         = rng.getBool();
5366                         const TestConfig::ImageMemory   imageMemory                     = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
5367                         VkImageLayout                                   depthStencilLayout      = VK_IMAGE_LAYOUT_GENERAL;
5368                         vector<Attachment>                              attachments;
5369                         vector<AttachmentReference>             colorAttachmentReferences;
5370
5371                         // we want to make sure that dynamic rendering test cases have corresponding renderpass
5372                         // cases as this will allow drivers to easily compare GPU batches; since configurations
5373                         // for those tests are generated we need to generate configurations for all cases
5374                         // even when we know earlier that for dynamic rendering we will skip it
5375                         bool executeForDynamicRendering = true;
5376
5377                         for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
5378                         {
5379                                 const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
5380                                 const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
5381                                 const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5382                                 const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5383
5384                                 const VkImageLayout                     initialLayout   = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5385                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts))
5386                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayoutsLazy), DE_ARRAY_END(initialAndFinalColorLayoutsLazy));
5387                                 VkImageLayout                           finalizeLayout  = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5388                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts))
5389                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayoutsLazy), DE_ARRAY_END(initialAndFinalColorLayoutsLazy));
5390                                 const VkImageLayout                     subpassLayout   = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayouts), DE_ARRAY_END(subpassLayouts));
5391
5392                                 const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5393                                 const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5394
5395                                 if (useDynamicRendering)
5396                                 {
5397                                         // with renderpass we can have automatic layout transitions; to do the same with dynamic rendering cases
5398                                         // we would need to add addtional barries but since those tests won't add coverage we are skipping them
5399                                         if ((initialLayout == VK_IMAGE_LAYOUT_GENERAL) ||
5400                                                 (initialLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL))
5401                                                 finalizeLayout = initialLayout;
5402                                         else
5403                                                 executeForDynamicRendering = false;
5404                                 }
5405
5406                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5407                                 colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
5408                         }
5409
5410                         if (useDepthStencil)
5411                         {
5412                                 const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
5413                                 const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreDepthStencilFormats), DE_ARRAY_END(s_coreDepthStencilFormats));
5414                                 const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5415                                 const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5416
5417                                 const VkImageLayout                     initialLayout   = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5418                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5419                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayoutsLazy), DE_ARRAY_END(initialAndFinalDepthStencilLayoutsLazy));
5420                                 VkImageLayout                           finalizeLayout  = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5421                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5422                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayoutsLazy), DE_ARRAY_END(initialAndFinalDepthStencilLayoutsLazy));
5423
5424                                 const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5425                                 const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5426
5427                                 if (useDynamicRendering)
5428                                 {
5429                                         if ((initialLayout == VK_IMAGE_LAYOUT_GENERAL) ||
5430                                                 (initialLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) ||
5431                                                 (initialLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL))
5432                                                 finalizeLayout = initialLayout;
5433                                         else
5434                                                 executeForDynamicRendering = false;
5435                                 }
5436
5437                                 depthStencilLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(depthStencilLayouts), DE_ARRAY_END(depthStencilLayouts));
5438                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5439                         }
5440
5441                         {
5442                                 const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
5443                                 const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
5444                                 const vector<Subpass>                                   subpasses               (1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u, vector<AttachmentReference>(), colorAttachmentReferences, vector<AttachmentReference>(), AttachmentReference((useDepthStencil ? (deUint32)(attachments.size() - 1) : VK_ATTACHMENT_UNUSED), depthStencilLayout), vector<deUint32>()));
5445                                 const vector<SubpassDependency>                 deps;
5446                                 const string                                                    testCaseName    = de::toString(attachmentCountNdx * testCaseCount + testCaseNdx);
5447                                 const RenderPass                                                renderPass              (attachments, subpasses, deps);
5448                                 const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
5449                                 const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
5450                                 const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
5451
5452                                 // skip dynamic rendering cases (that don't add coverage)
5453                                 // this can be done not earlier than after grabbing all random numbers as
5454                                 // we need to make sure that those tests that will be created for dynamic
5455                                 // rendering have corresponding renderpass tests with the same name
5456                                 if (useDynamicRendering && !executeForDynamicRendering)
5457                                         continue;
5458
5459                                 const TestConfig                                                testConfig              (renderPass,
5460                                                                                                                                                  render,
5461                                                                                                                                                  commandBuffer,
5462                                                                                                                                                  imageMemory,
5463                                                                                                                                                  targetSize,
5464                                                                                                                                                  renderPos,
5465                                                                                                                                                  renderSize,
5466                                                                                                                                                  DE_FALSE,
5467                                                                                                                                                  1293809,
5468                                                                                                                                                  0,
5469                                                                                                                                                  testConfigExternal.allocationKind,
5470                                                                                                                                                  testConfigExternal.renderingType);
5471
5472                                 addFunctionCaseWithPrograms<TestConfig>(attachmentCountGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
5473                         }
5474                 }
5475
5476                 group->addChild(attachmentCountGroup.release());
5477         }
5478 }
5479
5480 void addAttachmentWriteMaskTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5481 {
5482         const deUint32 attachmentCounts[]       = { 1, 2, 3, 4, 8 };
5483
5484         const VkFormat attachmentFormats[]      =
5485         {
5486                 VK_FORMAT_R8G8B8A8_UINT,
5487                 VK_FORMAT_R8G8B8A8_UNORM,
5488                 VK_FORMAT_R5G6B5_UNORM_PACK16,
5489                 VK_FORMAT_R8G8_UNORM
5490         };
5491
5492         tcu::TestContext&       testCtx                 = group->getTestContext();
5493
5494         for (deUint32 attachmentCountNdx = 0; attachmentCountNdx < DE_LENGTH_OF_ARRAY(attachmentCounts); attachmentCountNdx++)
5495         {
5496                 const deUint32  attachmentCount = attachmentCounts[attachmentCountNdx];
5497                 const string    groupName               = "attachment_count_" + de::toString(attachmentCount);
5498
5499                 de::MovePtr<tcu::TestCaseGroup> attachmentCountGroup(new tcu::TestCaseGroup(testCtx, groupName.c_str(), de::toString(attachmentCount).c_str()));
5500
5501                 for (deUint32 drawStartNdx = 0; drawStartNdx < (attachmentCount); drawStartNdx++)
5502                 {
5503                         deUint32                                        formatNdx = 0;
5504                         vector<Attachment>                      attachments;
5505                         vector<AttachmentReference>     colorAttachmentReferences;
5506
5507                         for (deUint32 attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
5508                         {
5509                                 const VkFormat                          format                          = attachmentFormats[formatNdx];
5510                                 const VkSampleCountFlagBits     sampleCount                     = VK_SAMPLE_COUNT_1_BIT;
5511                                 const VkAttachmentLoadOp        loadOp                          = VK_ATTACHMENT_LOAD_OP_CLEAR;
5512                                 const VkAttachmentStoreOp       storeOp                         = VK_ATTACHMENT_STORE_OP_STORE;
5513                                 const VkAttachmentLoadOp        stencilLoadOp           = VK_ATTACHMENT_LOAD_OP_CLEAR;
5514                                 const VkAttachmentStoreOp       stencilStoreOp          = VK_ATTACHMENT_STORE_OP_STORE;
5515                                 const VkImageLayout                     initialLayout           = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5516                                 const VkImageLayout                     finalizeLayout          = (testConfigExternal.renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
5517                                                                                                                                         ? initialLayout : VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
5518                                 const VkImageLayout                     subpassLayout           = VK_IMAGE_LAYOUT_GENERAL;
5519
5520                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5521                                 colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
5522
5523                                 if (++formatNdx == DE_LENGTH_OF_ARRAY(attachmentFormats))
5524                                         formatNdx = 0;
5525                         }
5526
5527                         {
5528                                 const VkImageLayout                                             depthStencilLayout      = VK_IMAGE_LAYOUT_GENERAL;
5529                                 const vector<Subpass>                                   subpass                         (1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u, vector<AttachmentReference>(), colorAttachmentReferences, vector<AttachmentReference>(), AttachmentReference(VK_ATTACHMENT_UNUSED, depthStencilLayout), vector<deUint32>()));
5530                                 const vector<SubpassDependency>                 deps;
5531
5532                                 const string                                                    testCaseName            = "start_index_" + de::toString(drawStartNdx);
5533                                 const RenderPass                                                renderPass                      (attachments, subpass, deps);
5534
5535                                 const TestConfig::RenderTypes                   render                          = TestConfig::RENDERTYPES_DRAW;
5536                                 const TestConfig::CommandBufferTypes    commandBuffer           = TestConfig::COMMANDBUFFERTYPES_INLINE;
5537                                 const TestConfig::ImageMemory                   imageMemory                     = TestConfig::IMAGEMEMORY_LAZY;
5538                                 const UVec2                                                             targetSize                      = UVec2(64, 64);
5539                                 const UVec2                                                             renderPos                       = UVec2(0, 0);
5540                                 const UVec2                                                             renderSize                      = UVec2(64, 64);
5541                                 const deBool                                                    useFormatCompCount      = DE_TRUE;
5542                                 const vector<DeviceCoreFeature>                 requiredFeatures        = {DEVICE_CORE_FEATURE_INDEPENDENT_BLEND};
5543                                 const TestConfig                                                testConfig                      (renderPass,
5544                                                                                                                                                          render,
5545                                                                                                                                                          commandBuffer,
5546                                                                                                                                                          imageMemory,
5547                                                                                                                                                          targetSize,
5548                                                                                                                                                          renderPos,
5549                                                                                                                                                          renderSize,
5550                                                                                                                                                          useFormatCompCount,
5551                                                                                                                                                          1293809,
5552                                                                                                                                                          drawStartNdx,
5553                                                                                                                                                          testConfigExternal.allocationKind,
5554                                                                                                                                                          testConfigExternal.renderingType,
5555                                                                                                                                                          requiredFeatures);
5556
5557                                 addFunctionCaseWithPrograms<TestConfig>(attachmentCountGroup.get(), testCaseName.c_str(), testCaseName.c_str(), checkSupport, createTestShaders, renderPassTest, testConfig);
5558                         }
5559                 }
5560
5561                 group->addChild(attachmentCountGroup.release());
5562         }
5563 }
5564
5565 template<typename T>
5566 T chooseRandom (de::Random& rng, const set<T>& values)
5567 {
5568         size_t                                                  ndx             = ((size_t)rng.getUint32()) % values.size();
5569         typename set<T>::const_iterator iter    = values.begin();
5570
5571         for (; ndx > 0; ndx--)
5572                 iter++;
5573
5574         return *iter;
5575 }
5576
5577 void addAttachmentAllocationTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5578 {
5579         const deUint32 attachmentCounts[] = { 4, 8 };
5580         const VkAttachmentLoadOp loadOps[] =
5581         {
5582                 VK_ATTACHMENT_LOAD_OP_LOAD,
5583                 VK_ATTACHMENT_LOAD_OP_CLEAR,
5584                 VK_ATTACHMENT_LOAD_OP_DONT_CARE
5585         };
5586
5587         const VkAttachmentStoreOp storeOps[] =
5588         {
5589                 VK_ATTACHMENT_STORE_OP_STORE,
5590                 VK_ATTACHMENT_STORE_OP_DONT_CARE
5591         };
5592
5593         const VkImageLayout initialAndFinalColorLayouts[] =
5594         {
5595                 VK_IMAGE_LAYOUT_GENERAL,
5596                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5597                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5598                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5599                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5600         };
5601
5602         const VkImageLayout initialAndFinalDepthStencilLayouts[] =
5603         {
5604                 VK_IMAGE_LAYOUT_GENERAL,
5605                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5606                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5607                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5608                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5609                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5610         };
5611
5612         const VkImageLayout subpassLayoutsColor[] =
5613         {
5614                 VK_IMAGE_LAYOUT_GENERAL,
5615                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
5616         };
5617
5618         const VkImageLayout subpassLayoutsDepthStencil[] =
5619         {
5620                 VK_IMAGE_LAYOUT_GENERAL,
5621                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5622         };
5623
5624         const VkImageLayout subpassLayoutsInput[] =
5625         {
5626                 VK_IMAGE_LAYOUT_GENERAL,
5627                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5628         };
5629
5630         enum AllocationType
5631         {
5632                 // Each pass uses one more attachmen than previous one
5633                 ALLOCATIONTYPE_GROW,
5634                 // Each pass uses one less attachment than previous one
5635                 ALLOCATIONTYPE_SHRINK,
5636                 // Each pass drops one attachment and picks up new one
5637                 ALLOCATIONTYPE_ROLL,
5638                 // Start by growing and end by shrinking
5639                 ALLOCATIONTYPE_GROW_SHRINK,
5640                 // Each subpass has single input and single output attachment
5641                 ALLOCATIONTYPE_IO_CHAIN,
5642                 // Each subpass has multiple inputs and multiple outputs attachment
5643                 ALLOCATIONTYPE_IO_GENERIC
5644         };
5645
5646         const AllocationType allocationTypes[] =
5647         {
5648                 ALLOCATIONTYPE_GROW,
5649                 ALLOCATIONTYPE_SHRINK,
5650                 ALLOCATIONTYPE_ROLL,
5651                 ALLOCATIONTYPE_GROW_SHRINK,
5652                 ALLOCATIONTYPE_IO_CHAIN,
5653                 ALLOCATIONTYPE_IO_GENERIC
5654         };
5655
5656         const char* const allocationTypeStr[] =
5657         {
5658                 "grow",
5659                 "shrink",
5660                 "roll",
5661                 "grow_shrink",
5662                 "input_output_chain",
5663                 "input_output",
5664         };
5665
5666         const TestConfig::RenderTypes renderCommands[] =
5667         {
5668                 TestConfig::RENDERTYPES_NONE,
5669                 TestConfig::RENDERTYPES_CLEAR,
5670                 TestConfig::RENDERTYPES_DRAW,
5671                 TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW,
5672         };
5673
5674         const TestConfig::CommandBufferTypes commandBuffers[] =
5675         {
5676                 TestConfig::COMMANDBUFFERTYPES_INLINE,
5677                 TestConfig::COMMANDBUFFERTYPES_SECONDARY,
5678                 TestConfig::COMMANDBUFFERTYPES_INLINE|TestConfig::COMMANDBUFFERTYPES_SECONDARY
5679         };
5680
5681         const TestConfig::ImageMemory imageMemories[] =
5682         {
5683                 TestConfig::IMAGEMEMORY_STRICT,
5684                 TestConfig::IMAGEMEMORY_LAZY,
5685                 TestConfig::IMAGEMEMORY_STRICT|TestConfig::IMAGEMEMORY_LAZY
5686         };
5687
5688         const UVec2 targetSizes[] =
5689         {
5690                 UVec2(64, 64),
5691                 UVec2(63, 65)
5692         };
5693
5694         const UVec2 renderPositions[] =
5695         {
5696                 UVec2(0, 0),
5697                 UVec2(3, 17)
5698         };
5699
5700         const UVec2 renderSizes[] =
5701         {
5702                 UVec2(32, 32),
5703                 UVec2(60, 47)
5704         };
5705
5706         tcu::TestContext&                               testCtx = group->getTestContext();
5707         de::Random                                              rng             (3700649827u);
5708
5709         for (size_t allocationTypeNdx = 0; allocationTypeNdx < DE_LENGTH_OF_ARRAY(allocationTypes); allocationTypeNdx++)
5710         {
5711                 const AllocationType                    allocationType          = allocationTypes[allocationTypeNdx];
5712                 const size_t                                    testCaseCount           = 100;
5713                 de::MovePtr<tcu::TestCaseGroup> allocationTypeGroup     (new tcu::TestCaseGroup(testCtx, allocationTypeStr[allocationTypeNdx], allocationTypeStr[allocationTypeNdx]));
5714
5715                 for (size_t testCaseNdx = 0; testCaseNdx < testCaseCount; testCaseNdx++)
5716                 {
5717                         if (allocationType == ALLOCATIONTYPE_IO_GENERIC)
5718                         {
5719                                 const deUint32          attachmentCount = 4u + rng.getUint32() % 31u;
5720                                 const deUint32          subpassCount    = 4u + rng.getUint32() % 31u;
5721                                 vector<Attachment>      attachments;
5722
5723                                 set<deUint32>           definedAttachments;
5724
5725                                 vector<Subpass>         subpasses;
5726                                 set<deUint32>           colorAttachments;
5727                                 set<deUint32>           depthStencilAttachments;
5728
5729                                 for (deUint32 attachmentIndex = 0; attachmentIndex < attachmentCount; attachmentIndex++)
5730                                 {
5731                                         const bool                                      isDepthStencilAttachment        = rng.getFloat() < 0.01f;
5732                                         const VkSampleCountFlagBits     sampleCount                                     = VK_SAMPLE_COUNT_1_BIT;
5733                                         const VkAttachmentLoadOp        loadOp                                          = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5734                                         const VkAttachmentStoreOp       storeOp                                         = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5735
5736                                         const VkImageLayout                     initialLayout                           = isDepthStencilAttachment
5737                                                                                                                                                         ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5738                                                                                                                                                         : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
5739                                         const VkImageLayout                     finalizeLayout                          = isDepthStencilAttachment
5740                                                                                                                                                         ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5741                                                                                                                                                         : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
5742
5743                                         const VkAttachmentLoadOp        stencilLoadOp                           = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5744                                         const VkAttachmentStoreOp       stencilStoreOp                          = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5745
5746                                         if (isDepthStencilAttachment)
5747                                         {
5748                                                 const VkFormat  format  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreDepthStencilFormats), DE_ARRAY_END(s_coreDepthStencilFormats));
5749
5750                                                 if (loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR
5751                                                         || stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD || stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
5752                                                         definedAttachments.insert(attachmentIndex);
5753
5754                                                 depthStencilAttachments.insert(attachmentIndex);
5755
5756                                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5757                                         }
5758                                         else
5759                                         {
5760                                                 const VkFormat  format  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
5761
5762                                                 if (loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
5763                                                         definedAttachments.insert(attachmentIndex);
5764
5765                                                 colorAttachments.insert(attachmentIndex);
5766
5767                                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5768                                         }
5769                                 }
5770                                 vector<Maybe<deUint32> >        lastUseOfAttachment     (attachments.size(), nothing<deUint32>());
5771                                 vector<SubpassDependency>       deps;
5772
5773                                 for (deUint32 subpassIndex = 0; subpassIndex < subpassCount; subpassIndex++)
5774                                 {
5775                                         const deUint32                          colorAttachmentCount            = depthStencilAttachments.empty()
5776                                                                                                                                                         ? 1 + rng.getUint32() % de::min(4u, (deUint32)colorAttachments.size())
5777                                                                                                                                                         : rng.getUint32() % (de::min(4u, (deUint32)colorAttachments.size()) + 1u);
5778                                         const deUint32                          inputAttachmentCount            = rng.getUint32() % (deUint32)(de::min<size_t>(4, definedAttachments.size()) + 1);
5779                                         const bool                                      useDepthStencilAttachment       = !depthStencilAttachments.empty() && (colorAttachmentCount == 0 || rng.getBool());
5780                                         std::vector<deUint32>           subpassColorAttachments         (colorAttachmentCount);
5781                                         std::vector<deUint32>           subpassInputAttachments         (inputAttachmentCount);
5782                                         Maybe<deUint32>                         depthStencilAttachment          (useDepthStencilAttachment
5783                                                                                                                                                         ? just(chooseRandom(rng, depthStencilAttachments))
5784                                                                                                                                                         : nothing<deUint32>());
5785                                         std::vector<deUint32>           subpassPreserveAttachments;
5786
5787                                         rng.choose(colorAttachments.begin(), colorAttachments.end(), subpassColorAttachments.begin(), colorAttachmentCount);
5788                                         rng.choose(definedAttachments.begin(), definedAttachments.end(), subpassInputAttachments.begin(), inputAttachmentCount);
5789
5790                                         for (size_t colorAttachmentNdx = 0; colorAttachmentNdx < subpassColorAttachments.size(); colorAttachmentNdx++)
5791                                                 definedAttachments.insert(subpassColorAttachments[colorAttachmentNdx]);
5792
5793                                         if (depthStencilAttachment)
5794                                                 definedAttachments.insert(*depthStencilAttachment);
5795
5796                                         {
5797                                                 std::vector<AttachmentReference>        inputAttachmentReferences;
5798                                                 std::vector<AttachmentReference>        colorAttachmentReferences;
5799                                                 AttachmentReference                                     depthStencilAttachmentReference (VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL);
5800
5801                                                 for (size_t colorAttachmentNdx = 0; colorAttachmentNdx < subpassColorAttachments.size(); colorAttachmentNdx++)
5802                                                 {
5803                                                         const deUint32          colorAttachmentIndex    = subpassColorAttachments[colorAttachmentNdx];
5804
5805                                                         if (lastUseOfAttachment[colorAttachmentIndex])
5806                                                         {
5807                                                                 deBool foundDuplicate = false;
5808
5809                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[colorAttachmentIndex];
5810                                                                 const deUint32                  dstPass                 = subpassIndex;
5811                                                                 const VkDependencyFlags dependencyFlags = rng.getBool() ? (VkDependencyFlags) VK_DEPENDENCY_BY_REGION_BIT : 0u;
5812
5813                                                                 const SubpassDependency newDependency(srcPass, dstPass,
5814                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5815                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5816                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5817                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5818
5819                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5820                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5821                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5822                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5823
5824                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
5825                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
5826
5827                                                                                                                                           dependencyFlags);
5828
5829                                                                 for (SubpassDependency& dependency : deps)
5830                                                                 {
5831                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
5832                                                                         {
5833                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
5834                                                                                 dependency.setDstAccessMask(newDstFlags);
5835                                                                                 foundDuplicate = true;
5836                                                                                 break;
5837                                                                         }
5838                                                                 }
5839
5840                                                                 if (!foundDuplicate)
5841                                                                 {
5842                                                                         deps.push_back(newDependency);
5843                                                                 }
5844                                                         }
5845
5846                                                         lastUseOfAttachment[colorAttachmentIndex] = just(subpassIndex);
5847
5848                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)subpassColorAttachments[colorAttachmentNdx], VK_IMAGE_LAYOUT_GENERAL));
5849                                                 }
5850
5851                                                 for (size_t inputAttachmentNdx = 0; inputAttachmentNdx < subpassInputAttachments.size(); inputAttachmentNdx++)
5852                                                 {
5853                                                         const deUint32          inputAttachmentIndex    = subpassInputAttachments[inputAttachmentNdx];
5854
5855                                                         if(lastUseOfAttachment[inputAttachmentIndex])
5856                                                         {
5857                                                                 deBool foundDuplicate = false;
5858
5859                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[inputAttachmentIndex];
5860                                                                 const deUint32                  dstPass                 = subpassIndex;
5861                                                                 const VkDependencyFlags dependencyFlags = ((srcPass == subpassIndex) || rng.getBool()) ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u;
5862
5863                                                                 const SubpassDependency newDependency(srcPass, dstPass,
5864                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5865                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5866                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5867                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5868
5869                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5870                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5871                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5872                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5873
5874                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
5875                                                                                                                                           VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
5876
5877                                                                                                                                           dependencyFlags);
5878                                                                 for (SubpassDependency& dependency : deps)
5879                                                                 {
5880                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
5881                                                                         {
5882                                                                                 const VkAccessFlags newSrcFlags = dependency.getSrcAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
5883                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
5884                                                                                 dependency.setDstAccessMask(newSrcFlags);
5885                                                                                 dependency.setDstAccessMask(newDstFlags);
5886                                                                                 foundDuplicate = true;
5887                                                                                 break;
5888                                                                         }
5889                                                                 }
5890
5891                                                                 if (!foundDuplicate)
5892                                                                 {
5893                                                                         deps.push_back(newDependency);
5894                                                                 }
5895
5896                                                                 lastUseOfAttachment[inputAttachmentIndex] = just(subpassIndex);
5897
5898                                                                 VkImageAspectFlags aspect = 0u;
5899                                                                 if (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2)
5900                                                                 {
5901                                                                         bool col = colorAttachments.find(inputAttachmentIndex) != colorAttachments.end();
5902                                                                         aspect = col ? VK_IMAGE_ASPECT_COLOR_BIT : VK_IMAGE_ASPECT_DEPTH_BIT;
5903                                                                 }
5904                                                                 inputAttachmentReferences.push_back(AttachmentReference((deUint32)subpassInputAttachments[inputAttachmentNdx], VK_IMAGE_LAYOUT_GENERAL, aspect));
5905                                                         }
5906                                                 }
5907
5908                                                 if (depthStencilAttachment)
5909                                                 {
5910                                                         if (lastUseOfAttachment[*depthStencilAttachment])
5911                                                         {
5912                                                                 deBool foundDuplicate = false;
5913
5914                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[*depthStencilAttachment];
5915                                                                 const deUint32                  dstPass                 = subpassIndex;
5916                                                                 const VkDependencyFlags dependencyFlags = ((srcPass == subpassIndex) || rng.getBool()) ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u;
5917
5918                                                                 const SubpassDependency newDependency(srcPass, dstPass,
5919                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5920                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5921                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5922                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5923
5924                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5925                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5926                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5927                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5928
5929                                                                                                                                           VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
5930                                                                                                                                           VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT
5931                                                                                                                                           | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
5932
5933                                                                                                                                           dependencyFlags);
5934                                                                 for (SubpassDependency& dependency : deps)
5935                                                                 {
5936                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
5937                                                                         {
5938                                                                                 const VkAccessFlags newSrcFlags = dependency.getSrcAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
5939                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
5940                                                                                 dependency.setDstAccessMask(newSrcFlags);
5941                                                                                 dependency.setDstAccessMask(newDstFlags);
5942                                                                                 foundDuplicate = true;
5943                                                                                 break;
5944                                                                         }
5945                                                                 }
5946
5947                                                                 if (!foundDuplicate)
5948                                                                 {
5949                                                                         deps.push_back(newDependency);
5950                                                                 }
5951                                                         }
5952
5953                                                         lastUseOfAttachment[*depthStencilAttachment] = just(subpassIndex);
5954
5955                                                         depthStencilAttachmentReference = AttachmentReference(*depthStencilAttachment, VK_IMAGE_LAYOUT_GENERAL);
5956                                                 }
5957                                                 else
5958                                                         depthStencilAttachmentReference = AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL);
5959
5960                                                 vector<deUint32>        preserveAttachments;
5961                                                 for (deUint32 attachmentIndex = 0; attachmentIndex < (deUint32)attachments.size(); attachmentIndex++)
5962                                                 {
5963                                                         if (lastUseOfAttachment[attachmentIndex] && (*lastUseOfAttachment[attachmentIndex]) != subpassIndex)
5964                                                                 preserveAttachments.push_back(attachmentIndex);
5965                                                 }
5966
5967                                                 // Use random image layout when possible
5968                                                 for (size_t colorRefIdx = 0; colorRefIdx < colorAttachmentReferences.size(); ++colorRefIdx)
5969                                                 {
5970                                                         bool usedAsInput = false;
5971                                                         for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
5972                                                                 if (colorAttachmentReferences[colorRefIdx].getAttachment() == inputAttachmentReferences[inputRefIdx].getAttachment())
5973                                                                         usedAsInput = true;
5974
5975                                                         if (!usedAsInput)
5976                                                                 colorAttachmentReferences[colorRefIdx].setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)));
5977                                                 }
5978                                                 for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
5979                                                 {
5980                                                         bool usedAsDepthStencil = inputAttachmentReferences[inputRefIdx].getAttachment() == depthStencilAttachmentReference.getAttachment();
5981                                                         bool usedAsColor                = false;
5982                                                         for (size_t colorRefIdx = 0; colorRefIdx < colorAttachmentReferences.size(); ++colorRefIdx)
5983                                                                 if (inputAttachmentReferences[inputRefIdx].getAttachment() == colorAttachmentReferences[colorRefIdx].getAttachment())
5984                                                                         usedAsColor = true;
5985
5986                                                         if (!usedAsColor && !usedAsDepthStencil)
5987                                                                 inputAttachmentReferences[inputRefIdx].setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsInput), DE_ARRAY_END(subpassLayoutsInput)));
5988                                                 }
5989                                                 {
5990                                                         bool usedAsInput = false;
5991                                                         for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
5992                                                                 if (depthStencilAttachmentReference.getAttachment() == inputAttachmentReferences[inputRefIdx].getAttachment())
5993                                                                         usedAsInput = true;
5994
5995                                                         if (!usedAsInput)
5996                                                                 depthStencilAttachmentReference.setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsDepthStencil), DE_ARRAY_END(subpassLayoutsDepthStencil)));
5997                                                 }
5998
5999                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6000                                                                                                 inputAttachmentReferences,
6001                                                                                                 colorAttachmentReferences,
6002                                                                                                 vector<AttachmentReference>(),
6003                                                                                                 depthStencilAttachmentReference,
6004                                                                                                 preserveAttachments));
6005                                         }
6006                                 }
6007                                 {
6008                                         const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
6009                                         const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
6010                                         const TestConfig::ImageMemory                   imageMemory             = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
6011
6012                                         const string                                                    testCaseName    = de::toString(testCaseNdx);
6013                                         const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
6014                                         const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
6015                                         const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
6016
6017                                         const RenderPass                                                renderPass              (attachments, subpasses, deps);
6018                                         const TestConfig                                                testConfig              (renderPass,
6019                                                                                                                                                          render,
6020                                                                                                                                                          commandBuffer,
6021                                                                                                                                                          imageMemory,
6022                                                                                                                                                          targetSize,
6023                                                                                                                                                          renderPos,
6024                                                                                                                                                          renderSize,
6025                                                                                                                                                          DE_FALSE,
6026                                                                                                                                                          80329,
6027                                                                                                                                                          0,
6028                                                                                                                                                          testConfigExternal.allocationKind,
6029                                                                                                                                                          testConfigExternal.renderingType);
6030
6031                                         addFunctionCaseWithPrograms<TestConfig>(allocationTypeGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
6032                                 }
6033                         }
6034                         else
6035                         {
6036                                 const deUint32          attachmentCount = rng.choose<deUint32>(DE_ARRAY_BEGIN(attachmentCounts), DE_ARRAY_END(attachmentCounts));
6037                                 vector<Attachment>      attachments;
6038                                 vector<Subpass>         subpasses;
6039
6040                                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
6041                                 {
6042                                         const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
6043                                         const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
6044                                         const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
6045                                         const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
6046
6047                                         const VkImageLayout                     initialLayout   = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
6048                                         const VkImageLayout                     finalizeLayout  = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
6049
6050                                         const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
6051                                         const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
6052
6053                                         attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
6054                                 }
6055
6056                                 if (allocationType == ALLOCATIONTYPE_GROW)
6057                                 {
6058                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6059                                         {
6060                                                 vector<AttachmentReference>     colorAttachmentReferences;
6061
6062                                                 for (size_t attachmentNdx = 0; attachmentNdx < subpassNdx + 1; attachmentNdx++)
6063                                                 {
6064                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6065
6066                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6067                                                 }
6068
6069                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6070                                                                                                 vector<AttachmentReference>(),
6071                                                                                                 colorAttachmentReferences,
6072                                                                                                 vector<AttachmentReference>(),
6073                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6074                                                                                                 vector<deUint32>()));
6075                                         }
6076                                 }
6077                                 else if (allocationType == ALLOCATIONTYPE_SHRINK)
6078                                 {
6079                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6080                                         {
6081                                                 vector<AttachmentReference>     colorAttachmentReferences;
6082
6083                                                 for (size_t attachmentNdx = 0; attachmentNdx < (attachmentCount - subpassNdx); attachmentNdx++)
6084                                                 {
6085                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6086
6087                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6088                                                 }
6089
6090                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6091                                                                                                         vector<AttachmentReference>(),
6092                                                                                                         colorAttachmentReferences,
6093                                                                                                         vector<AttachmentReference>(),
6094                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6095                                                                                                         vector<deUint32>()));
6096                                         }
6097                                 }
6098                                 else if (allocationType == ALLOCATIONTYPE_ROLL)
6099                                 {
6100                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount / 2; subpassNdx++)
6101                                         {
6102                                                 vector<AttachmentReference>     colorAttachmentReferences;
6103
6104                                                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount / 2; attachmentNdx++)
6105                                                 {
6106                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6107
6108                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)(subpassNdx + attachmentNdx), subpassLayout));
6109                                                 }
6110
6111                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6112                                                                                                         vector<AttachmentReference>(),
6113                                                                                                         colorAttachmentReferences,
6114                                                                                                         vector<AttachmentReference>(),
6115                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6116                                                                                                         vector<deUint32>()));
6117                                         }
6118                                 }
6119                                 else if (allocationType == ALLOCATIONTYPE_GROW_SHRINK)
6120                                 {
6121                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6122                                         {
6123                                                 vector<AttachmentReference>     colorAttachmentReferences;
6124
6125                                                 for (size_t attachmentNdx = 0; attachmentNdx < subpassNdx + 1; attachmentNdx++)
6126                                                 {
6127                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6128
6129                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6130                                                 }
6131
6132                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6133                                                                                                         vector<AttachmentReference>(),
6134                                                                                                         colorAttachmentReferences,
6135                                                                                                         vector<AttachmentReference>(),
6136                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6137                                                                                                         vector<deUint32>()));
6138                                         }
6139                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6140                                         {
6141                                                 vector<AttachmentReference>     colorAttachmentReferences;
6142
6143                                                 for (size_t attachmentNdx = 0; attachmentNdx < (attachmentCount - subpassNdx); attachmentNdx++)
6144                                                 {
6145                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6146
6147                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6148                                                 }
6149
6150                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6151                                                                                                         vector<AttachmentReference>(),
6152                                                                                                         colorAttachmentReferences,
6153                                                                                                         vector<AttachmentReference>(),
6154                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6155                                                                                                         vector<deUint32>()));
6156                                         }
6157                                 }
6158                                 else if (allocationType == ALLOCATIONTYPE_IO_CHAIN)
6159                                 {
6160                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6161                                                                                         vector<AttachmentReference>(),
6162                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)))),
6163                                                                                         vector<AttachmentReference>(),
6164                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6165                                                                                         vector<deUint32>()));
6166
6167                                         for (size_t subpassNdx = 1; subpassNdx < attachmentCount; subpassNdx++)
6168                                         {
6169                                                 const VkImageAspectFlags inputAttachmentAspectMask = (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2) ? VK_IMAGE_ASPECT_COLOR_BIT : static_cast<VkImageAspectFlagBits>(0);
6170                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6171                                                                                                 vector<AttachmentReference>(1, AttachmentReference((deUint32)(subpassNdx - 1), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
6172                                                                                                 vector<AttachmentReference>(1, AttachmentReference((deUint32)(subpassNdx), rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)))),
6173                                                                                                 vector<AttachmentReference>(),
6174                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6175                                                                                                 vector<deUint32>()));
6176                                         }
6177                                 }
6178                                 else
6179                                         DE_FATAL("Unknown allocation type");
6180
6181                                 {
6182                                         const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
6183                                         const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
6184                                         const TestConfig::ImageMemory                   imageMemory             = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
6185
6186                                         const string                                                    testCaseName    = de::toString(testCaseNdx);
6187                                         const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
6188                                         const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
6189                                         const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
6190
6191                                         vector<SubpassDependency>                               deps;
6192
6193                                         for (size_t subpassNdx = 0; subpassNdx < subpasses.size() - 1; subpassNdx++)
6194                                         {
6195                                                 const bool byRegion                             = rng.getBool();
6196                                                 deps.push_back(SubpassDependency((deUint32)subpassNdx, (deUint32)subpassNdx + 1,
6197                                                                                                                  VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6198                                                                                                                         | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6199                                                                                                                         | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6200                                                                                                                         | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6201
6202                                                                                                                  VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6203                                                                                                                         | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6204                                                                                                                         | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6205                                                                                                                         | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6206
6207                                                                                                                  VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6208                                                                                                                  (VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT),
6209
6210                                                                                                                  byRegion ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u));
6211                                         }
6212
6213                                         const RenderPass                                        renderPass              (attachments, subpasses, deps);
6214                                         const TestConfig                                        testConfig              (renderPass,
6215                                                                                                                                                  render,
6216                                                                                                                                                  commandBuffer,
6217                                                                                                                                                  imageMemory,
6218                                                                                                                                                  targetSize,
6219                                                                                                                                                  renderPos,
6220                                                                                                                                                  renderSize,
6221                                                                                                                                                  DE_FALSE,
6222                                                                                                                                                  80329,
6223                                                                                                                                                  0,
6224                                                                                                                                                  testConfigExternal.allocationKind,
6225                                                                                                                                                  testConfigExternal.renderingType);
6226
6227                                         addFunctionCaseWithPrograms<TestConfig>(allocationTypeGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
6228                                 }
6229                         }
6230                 }
6231                 group->addChild(allocationTypeGroup.release());
6232         }
6233 }
6234
6235 void addSimpleTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
6236 {
6237         const UVec2     targetSize      (64, 64);
6238         const UVec2     renderPos       (0, 0);
6239         const UVec2     renderSize      (64, 64);
6240
6241         // color
6242         {
6243                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6244                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6245                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6246                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6247                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6248                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6249                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6250                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6251                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6252                                                                                                                                         0u,
6253                                                                                                                                         vector<AttachmentReference>(),
6254                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6255                                                                                                                                         vector<AttachmentReference>(),
6256                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6257                                                                                                                                         vector<deUint32>())),
6258                                                                                  vector<SubpassDependency>());
6259                 const TestConfig        testConfig      (renderPass,
6260                                                                                  TestConfig::RENDERTYPES_DRAW,
6261                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6262                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6263                                                                                  targetSize,
6264                                                                                  renderPos,
6265                                                                                  renderSize,
6266                                                                                  DE_FALSE,
6267                                                                                  90239,
6268                                                                                  0,
6269                                                                                  testConfigExternal.allocationKind,
6270                                                                                  testConfigExternal.renderingType);
6271
6272                 addFunctionCaseWithPrograms<TestConfig>(group, "color", "Single color attachment case.", createTestShaders, renderPassTest, testConfig);
6273         }
6274
6275         // depth
6276         {
6277                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_X8_D24_UNORM_PACK32,
6278                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6279                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6280                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6281                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6282                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6283                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6284                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6285                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6286                                                                                                                                         0u,
6287                                                                                                                                         vector<AttachmentReference>(),
6288                                                                                                                                         vector<AttachmentReference>(),
6289                                                                                                                                         vector<AttachmentReference>(),
6290                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6291                                                                                                                                         vector<deUint32>())),
6292                                                                                  vector<SubpassDependency>());
6293                 const TestConfig        testConfig      (renderPass,
6294                                                                                  TestConfig::RENDERTYPES_DRAW,
6295                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6296                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6297                                                                                  targetSize,
6298                                                                                  renderPos,
6299                                                                                  renderSize,
6300                                                                                  DE_FALSE,
6301                                                                                  90239,
6302                                                                                  0,
6303                                                                                  testConfigExternal.allocationKind,
6304                                                                                  testConfigExternal.renderingType);
6305
6306                 addFunctionCaseWithPrograms<TestConfig>(group, "depth", "Single depth attachment case.", createTestShaders, renderPassTest, testConfig);
6307         }
6308
6309         // stencil
6310         {
6311                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_S8_UINT,
6312                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6313                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6314                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6315                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6316                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6317                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6318                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6319                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6320                                                                                                                                         0u,
6321                                                                                                                                         vector<AttachmentReference>(),
6322                                                                                                                                         vector<AttachmentReference>(),
6323                                                                                                                                         vector<AttachmentReference>(),
6324                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6325                                                                                                                                         vector<deUint32>())),
6326                                                                                  vector<SubpassDependency>());
6327                 const TestConfig        testConfig      (renderPass,
6328                                                                                  TestConfig::RENDERTYPES_DRAW,
6329                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6330                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6331                                                                                  targetSize,
6332                                                                                  renderPos,
6333                                                                                  renderSize,
6334                                                                                  DE_FALSE,
6335                                                                                  90239,
6336                                                                                  0,
6337                                                                                  testConfigExternal.allocationKind,
6338                                                                                  testConfigExternal.renderingType);
6339
6340                 addFunctionCaseWithPrograms<TestConfig>(group, "stencil", "Single stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6341         }
6342
6343         // depth_stencil
6344         {
6345                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_D24_UNORM_S8_UINT,
6346                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6347                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6348                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6349                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6350                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6351                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6352                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6353                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6354                                                                                                                                         0u,
6355                                                                                                                                         vector<AttachmentReference>(),
6356                                                                                                                                         vector<AttachmentReference>(),
6357                                                                                                                                         vector<AttachmentReference>(),
6358                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6359                                                                                                                                         vector<deUint32>())),
6360                                                                                  vector<SubpassDependency>());
6361                 const TestConfig        testConfig      (renderPass,
6362                                                                                  TestConfig::RENDERTYPES_DRAW,
6363                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6364                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6365                                                                                  targetSize,
6366                                                                                  renderPos,
6367                                                                                  renderSize,
6368                                                                                  DE_FALSE,
6369                                                                                  90239,
6370                                                                                  0,
6371                                                                                  testConfigExternal.allocationKind,
6372                                                                                  testConfigExternal.renderingType);
6373
6374                 addFunctionCaseWithPrograms<TestConfig>(group, "depth_stencil", "Single depth stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6375         }
6376
6377         // color_depth
6378         {
6379                 const Attachment        attachments[] =
6380                 {
6381                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6382                                            VK_SAMPLE_COUNT_1_BIT,
6383                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6384                                            VK_ATTACHMENT_STORE_OP_STORE,
6385                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6386                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6387                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6388                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6389                         Attachment(VK_FORMAT_X8_D24_UNORM_PACK32,
6390                                            VK_SAMPLE_COUNT_1_BIT,
6391                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6392                                            VK_ATTACHMENT_STORE_OP_STORE,
6393                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6394                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6395                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6396                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6397                 };
6398
6399                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6400                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6401                                                                                                                                         0u,
6402                                                                                                                                         vector<AttachmentReference>(),
6403                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6404                                                                                                                                         vector<AttachmentReference>(),
6405                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6406                                                                                                                                         vector<deUint32>())),
6407                                                                                  vector<SubpassDependency>());
6408                 const TestConfig        testConfig      (renderPass,
6409                                                                                  TestConfig::RENDERTYPES_DRAW,
6410                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6411                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6412                                                                                  targetSize,
6413                                                                                  renderPos,
6414                                                                                  renderSize,
6415                                                                                  DE_FALSE,
6416                                                                                  90239,
6417                                                                                  0,
6418                                                                                  testConfigExternal.allocationKind,
6419                                                                                  testConfigExternal.renderingType);
6420
6421                 addFunctionCaseWithPrograms<TestConfig>(group, "color_depth", "Color and depth attachment case.", createTestShaders, renderPassTest, testConfig);
6422         }
6423
6424         // color_stencil
6425         {
6426                 const Attachment        attachments[] =
6427                 {
6428                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6429                                            VK_SAMPLE_COUNT_1_BIT,
6430                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6431                                            VK_ATTACHMENT_STORE_OP_STORE,
6432                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6433                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6434                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6435                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6436                         Attachment(VK_FORMAT_S8_UINT,
6437                                            VK_SAMPLE_COUNT_1_BIT,
6438                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6439                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6440                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6441                                            VK_ATTACHMENT_STORE_OP_STORE,
6442                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6443                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6444                 };
6445
6446                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6447                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6448                                                                                                                                         0u,
6449                                                                                                                                         vector<AttachmentReference>(),
6450                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6451                                                                                                                                         vector<AttachmentReference>(),
6452                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6453                                                                                                                                         vector<deUint32>())),
6454                                                                                  vector<SubpassDependency>());
6455                 const TestConfig        testConfig      (renderPass,
6456                                                                                  TestConfig::RENDERTYPES_DRAW,
6457                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6458                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6459                                                                                  targetSize,
6460                                                                                  renderPos,
6461                                                                                  renderSize,
6462                                                                                  DE_FALSE,
6463                                                                                  90239,
6464                                                                                  0,
6465                                                                                  testConfigExternal.allocationKind,
6466                                                                                  testConfigExternal.renderingType);
6467
6468                 addFunctionCaseWithPrograms<TestConfig>(group, "color_stencil", "Color and stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6469         }
6470
6471         // color_depth_stencil
6472         {
6473                 const Attachment        attachments[] =
6474                 {
6475                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6476                                            VK_SAMPLE_COUNT_1_BIT,
6477                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6478                                            VK_ATTACHMENT_STORE_OP_STORE,
6479                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6480                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6481                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6482                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6483                         Attachment(VK_FORMAT_D24_UNORM_S8_UINT,
6484                                            VK_SAMPLE_COUNT_1_BIT,
6485                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6486                                            VK_ATTACHMENT_STORE_OP_STORE,
6487                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6488                                            VK_ATTACHMENT_STORE_OP_STORE,
6489                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6490                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6491                 };
6492
6493                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6494                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6495                                                                                                                                         0u,
6496                                                                                                                                         vector<AttachmentReference>(),
6497                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6498                                                                                                                                         vector<AttachmentReference>(),
6499                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6500                                                                                                                                         vector<deUint32>())),
6501                                                                                  vector<SubpassDependency>());
6502                 const TestConfig        testConfig      (renderPass,
6503                                                                                  TestConfig::RENDERTYPES_DRAW,
6504                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6505                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6506                                                                                  targetSize,
6507                                                                                  renderPos,
6508                                                                                  renderSize,
6509                                                                                  DE_FALSE,
6510                                                                                  90239,
6511                                                                                  0,
6512                                                                                  testConfigExternal.allocationKind,
6513                                                                                  testConfigExternal.renderingType);
6514
6515                 addFunctionCaseWithPrograms<TestConfig>(group, "color_depth_stencil", "Color, depth and stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6516         }
6517
6518         // no attachments
6519         {
6520                 const RenderPass        renderPass      (vector<Attachment>(),
6521                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6522                                                                                                                                         0u,
6523                                                                                                                                         vector<AttachmentReference>(),
6524                                                                                                                                         vector<AttachmentReference>(),
6525                                                                                                                                         vector<AttachmentReference>(),
6526                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6527                                                                                                                                         vector<deUint32>())),
6528                                                                                 vector<SubpassDependency>());
6529                 const TestConfig        testConfig      (renderPass,
6530                                                                                  TestConfig::RENDERTYPES_DRAW,
6531                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6532                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6533                                                                                  targetSize,
6534                                                                                  renderPos,
6535                                                                                  renderSize,
6536                                                                                  DE_FALSE,
6537                                                                                  90239,
6538                                                                                  0,
6539                                                                                  testConfigExternal.allocationKind,
6540                                                                                  testConfigExternal.renderingType);
6541
6542                 addFunctionCaseWithPrograms<TestConfig>(group, "no_attachments", "No attachments case.", createTestShaders, renderPassTest, testConfig);
6543         }
6544
6545         // color_unused_omit_blend_state
6546         if (testConfigExternal.renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
6547         {
6548                 vector<Subpass>         subpasses;
6549
6550                 // First subpass: use color attachment, create pipeline with color blend state
6551                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6552                                                                         0u,
6553                                                                         vector<AttachmentReference>(),
6554                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6555                                                                         vector<AttachmentReference>(),
6556                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6557                                                                         vector<deUint32>(),
6558                                                                         false));
6559
6560                 // Second subpass: don't use color attachment, create pipeline without color blend state
6561                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6562                                                                         0u,
6563                                                                         vector<AttachmentReference>(),
6564                                                                         vector<AttachmentReference>(1, AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6565                                                                         vector<AttachmentReference>(),
6566                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6567                                                                         vector<deUint32>(),
6568                                                                         true));
6569
6570                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6571                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6572                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6573                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6574                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6575                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6576                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6577                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6578                                                                                  subpasses,
6579                                                                                  vector<SubpassDependency>());
6580
6581                 const TestConfig        testConfig      (renderPass,
6582                                                                                  TestConfig::RENDERTYPES_DRAW,
6583                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6584                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6585                                                                                  targetSize,
6586                                                                                  renderPos,
6587                                                                                  renderSize,
6588                                                                                  DE_FALSE,
6589                                                                                  90239,
6590                                                                                  0,
6591                                                                                  testConfigExternal.allocationKind,
6592                                                                                  testConfigExternal.renderingType);
6593                 addFunctionCaseWithPrograms<TestConfig>(group, "color_unused_omit_blend_state", "Two unused color attachment case without blend state", createTestShaders, renderPassTest, testConfig);
6594         }
6595 }
6596
6597 std::string formatToName (VkFormat format)
6598 {
6599         const std::string       formatStr       = de::toString(format);
6600         const std::string       prefix          = "VK_FORMAT_";
6601
6602         DE_ASSERT(formatStr.substr(0, prefix.length()) == prefix);
6603
6604         return de::toLower(formatStr.substr(prefix.length()));
6605 }
6606
6607 void addFormatTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
6608 {
6609         tcu::TestContext&       testCtx         = group->getTestContext();
6610
6611         const UVec2                     targetSize      (64, 64);
6612         const UVec2                     renderPos       (0, 0);
6613         const UVec2                     renderSize      (64, 64);
6614
6615         const struct
6616         {
6617                 const char* const                       str;
6618                 const VkAttachmentStoreOp       op;
6619         } storeOps[] =
6620         {
6621                 { "store",              VK_ATTACHMENT_STORE_OP_STORE            },
6622                 { "dont_care",  VK_ATTACHMENT_STORE_OP_DONT_CARE        }
6623         };
6624
6625         const struct
6626         {
6627                 const char* const                       str;
6628                 const VkAttachmentLoadOp        op;
6629         } loadOps[] =
6630         {
6631                 { "clear",              VK_ATTACHMENT_LOAD_OP_CLEAR             },
6632                 { "load",               VK_ATTACHMENT_LOAD_OP_LOAD              },
6633                 { "dont_care",  VK_ATTACHMENT_LOAD_OP_DONT_CARE }
6634         };
6635
6636         const struct
6637         {
6638                  const char* const                              str;
6639                  const TestConfig::RenderTypes  types;
6640         } renderTypes[] =
6641         {
6642                 { "clear",              TestConfig::RENDERTYPES_CLEAR                                                           },
6643                 { "draw",               TestConfig::RENDERTYPES_DRAW                                                            },
6644                 { "clear_draw", TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW      }
6645         };
6646
6647         // Color formats
6648         for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_coreColorFormats); formatNdx++)
6649         {
6650                 const VkFormat                                  format          = s_coreColorFormats[formatNdx];
6651                 de::MovePtr<tcu::TestCaseGroup> formatGroup     (new tcu::TestCaseGroup(testCtx, formatToName(format).c_str(), de::toString(format).c_str()));
6652
6653                 for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
6654                 {
6655                         const VkAttachmentLoadOp                loadOp  = loadOps[loadOpNdx].op;
6656                         de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
6657
6658                         for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
6659                         {
6660                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(format,
6661                                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6662                                                                                                                                                                   loadOp,
6663                                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6664                                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6665                                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6666                                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6667                                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6668                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6669                                                                                                                                                         0u,
6670                                                                                                                                                         vector<AttachmentReference>(),
6671                                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6672                                                                                                                                                         vector<AttachmentReference>(),
6673                                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6674                                                                                                                                                         vector<deUint32>())),
6675                                                                                                  vector<SubpassDependency>());
6676                                 const TestConfig        testConfig      (renderPass,
6677                                                                                                  renderTypes[renderTypeNdx].types,
6678                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6679                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6680                                                                                                  targetSize,
6681                                                                                                  renderPos,
6682                                                                                                  renderSize,
6683                                                                                                  DE_FALSE,
6684                                                                                                  90239,
6685                                                                                                  0,
6686                                                                                                  testConfigExternal.allocationKind,
6687                                                                                                  testConfigExternal.renderingType);
6688
6689                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), renderTypes[renderTypeNdx].str, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6690                         }
6691
6692                         formatGroup->addChild(loadOpGroup.release());
6693                 }
6694
6695                 if (testConfigExternal.renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
6696                 {
6697                         de::MovePtr<tcu::TestCaseGroup> inputGroup (new tcu::TestCaseGroup(testCtx, "input", "Test attachment format as input"));
6698
6699                         for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
6700                         {
6701                                 const VkAttachmentLoadOp                loadOp          = loadOps[loadOpNdx].op;
6702                                 de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
6703
6704                                 for (size_t storeOpNdx = 0; storeOpNdx < DE_LENGTH_OF_ARRAY(storeOps); storeOpNdx++)
6705                                 {
6706                                         const VkImageAspectFlags                inputAttachmentAspectMask       = (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2)
6707                                                                                                                                                                 ? static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT)
6708                                                                                                                                                                 : static_cast<VkImageAspectFlags>(0);
6709                                         const VkAttachmentStoreOp               storeOp                                         = storeOps[storeOpNdx].op;
6710                                         de::MovePtr<tcu::TestCaseGroup> storeOpGroup                            (new tcu::TestCaseGroup(testCtx, storeOps[storeOpNdx].str, storeOps[storeOpNdx].str));
6711
6712                                         for (size_t useInputAspectNdx = 0; useInputAspectNdx < 2; useInputAspectNdx++)
6713                                         {
6714                                                 const bool useInputAspect = useInputAspectNdx != 0;
6715
6716                                                 if (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2 && useInputAspect)
6717                                                         continue;
6718
6719                                                 for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
6720                                                 {
6721                                                         {
6722                                                                 vector<Attachment>                                                      attachments;
6723                                                                 vector<Subpass>                                                         subpasses;
6724                                                                 vector<SubpassDependency>                                       deps;
6725                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
6726
6727                                                                 attachments.push_back(Attachment(format,
6728                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6729                                                                                                                                  loadOp,
6730                                                                                                                                  storeOp,
6731                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6732                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6733                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6734                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6735
6736                                                                 attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
6737                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6738                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6739                                                                                                                                  VK_ATTACHMENT_STORE_OP_STORE,
6740                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6741                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6742                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6743                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6744
6745                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6746                                                                                                                         0u,
6747                                                                                                                         vector<AttachmentReference>(),
6748                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6749                                                                                                                         vector<AttachmentReference>(),
6750                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6751                                                                                                                         vector<deUint32>()));
6752                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6753                                                                                                                         0u,
6754                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
6755                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6756                                                                                                                         vector<AttachmentReference>(),
6757                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6758                                                                                                                         vector<deUint32>()));
6759
6760                                                                 deps.push_back(SubpassDependency(0, 1,
6761
6762                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6763                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6764
6765                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6766                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6767                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6768
6769                                                                 if (useInputAspect)
6770                                                                 {
6771                                                                         const VkInputAttachmentAspectReference inputAspect =
6772                                                                         {
6773                                                                                 1u,
6774                                                                                 0u,
6775                                                                                 VK_IMAGE_ASPECT_COLOR_BIT
6776                                                                         };
6777
6778                                                                         inputAspects.push_back(inputAspect);
6779                                                                 }
6780
6781                                                                 {
6782                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
6783                                                                         const TestConfig        testConfig      (renderPass,
6784                                                                                                                                          renderTypes[renderTypeNdx].types,
6785                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
6786                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
6787                                                                                                                                          targetSize,
6788                                                                                                                                          renderPos,
6789                                                                                                                                          renderSize,
6790                                                                                                                                          DE_FALSE,
6791                                                                                                                                          89246,
6792                                                                                                                                          0,
6793                                                                                                                                          testConfigExternal.allocationKind,
6794                                                                                                                                          testConfigExternal.renderingType);
6795                                                                         const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : ""));
6796
6797                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6798                                                                 }
6799                                                         }
6800                                                         {
6801                                                                 vector<Attachment>                                                      attachments;
6802                                                                 vector<Subpass>                                                         subpasses;
6803                                                                 vector<SubpassDependency>                                       deps;
6804                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
6805
6806                                                                 attachments.push_back(Attachment(format,
6807                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6808                                                                                                                                  loadOp,
6809                                                                                                                                  storeOp,
6810                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6811                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6812                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6813                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6814
6815                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6816                                                                                                                         0u,
6817                                                                                                                         vector<AttachmentReference>(),
6818                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6819                                                                                                                         vector<AttachmentReference>(),
6820                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6821                                                                                                                         vector<deUint32>()));
6822                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6823                                                                                                                         0u,
6824                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL, inputAttachmentAspectMask)),
6825                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL)),
6826                                                                                                                         vector<AttachmentReference>(),
6827                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6828                                                                                                                         vector<deUint32>()));
6829
6830                                                                 deps.push_back(SubpassDependency(0, 1,
6831                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6832                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6833
6834                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6835                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6836                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6837
6838                                                                 deps.push_back(SubpassDependency(1, 1,
6839                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6840                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6841
6842                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6843                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6844                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6845
6846                                                                 if (useInputAspect)
6847                                                                 {
6848                                                                         const VkInputAttachmentAspectReference inputAspect =
6849                                                                         {
6850                                                                                 1u,
6851                                                                                 0u,
6852                                                                                 VK_IMAGE_ASPECT_COLOR_BIT
6853                                                                         };
6854
6855                                                                         inputAspects.push_back(inputAspect);
6856                                                                 }
6857
6858                                                                 {
6859                                                                         const RenderPass renderPass (attachments, subpasses, deps, inputAspects);
6860                                                                         const TestConfig testConfig (renderPass,
6861                                                                                                                                  renderTypes[renderTypeNdx].types,
6862                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6863                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6864                                                                                                                                  targetSize,
6865                                                                                                                                  renderPos,
6866                                                                                                                                  renderSize,
6867                                                                                                                                  DE_FALSE,
6868                                                                                                                                  89246,
6869                                                                                                                                  0,
6870                                                                                                                                  testConfigExternal.allocationKind,
6871                                                                                                                                  testConfigExternal.renderingType);
6872                                                                         const string    testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : ""));
6873
6874                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6875                                                                 }
6876                                                         }
6877                                                 }
6878                                         }
6879
6880                                         loadOpGroup->addChild(storeOpGroup.release());
6881                                 }
6882
6883                                 inputGroup->addChild(loadOpGroup.release());
6884                         }
6885
6886                         formatGroup->addChild(inputGroup.release());
6887                 }
6888
6889                 group->addChild(formatGroup.release());
6890         }
6891
6892         // Depth stencil formats
6893         for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_coreDepthStencilFormats); formatNdx++)
6894         {
6895                 const VkFormat                                  vkFormat                        = s_coreDepthStencilFormats[formatNdx];
6896                 const tcu::TextureFormat                format                          = mapVkFormat(vkFormat);
6897                 const bool                                              isStencilAttachment     = hasStencilComponent(format.order);
6898                 const bool                                              isDepthAttachment       = hasDepthComponent(format.order);
6899                 const VkImageAspectFlags                formatAspectFlags       = (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
6900                                                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u);
6901                 de::MovePtr<tcu::TestCaseGroup> formatGroup                     (new tcu::TestCaseGroup(testCtx, formatToName(vkFormat).c_str(), de::toString(vkFormat).c_str()));
6902
6903                 for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
6904                 {
6905                         const VkAttachmentLoadOp                loadOp  = loadOps[loadOpNdx].op;
6906                         de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
6907
6908                         for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
6909                         {
6910                                 {
6911                                         const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
6912                                                                                                                                                                           VK_SAMPLE_COUNT_1_BIT,
6913                                                                                                                                                                           isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6914                                                                                                                                                                           isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6915                                                                                                                                                                           isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6916                                                                                                                                                                           isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6917                                                                                                                                                                           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6918                                                                                                                                                                           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6919                                                                                                          vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6920                                                                                                                                                                 0u,
6921                                                                                                                                                                 vector<AttachmentReference>(),
6922                                                                                                                                                                 vector<AttachmentReference>(),
6923                                                                                                                                                                 vector<AttachmentReference>(),
6924                                                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6925                                                                                                                                                                 vector<deUint32>())),
6926                                                                                                          vector<SubpassDependency>());
6927                                         const TestConfig        testConfig      (renderPass,
6928                                                                                                          renderTypes[renderTypeNdx].types,
6929                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
6930                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
6931                                                                                                          targetSize,
6932                                                                                                          renderPos,
6933                                                                                                          renderSize,
6934                                                                                                          DE_FALSE,
6935                                                                                                          90239,
6936                                                                                                          0,
6937                                                                                                          testConfigExternal.allocationKind,
6938                                                                                                          testConfigExternal.renderingType);
6939
6940                                         addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), renderTypes[renderTypeNdx].str, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6941                                 }
6942
6943                                 if (isStencilAttachment && isDepthAttachment && loadOp != VK_ATTACHMENT_LOAD_OP_CLEAR)
6944                                 {
6945                                         {
6946                                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
6947                                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6948                                                                                                                                                                   isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6949                                                                                                                                                                   isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6950                                                                                                                                                                   isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6951                                                                                                                                                                   isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6952                                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6953                                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6954                                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6955                                                                                                                                                                         0u,
6956                                                                                                                                                                         vector<AttachmentReference>(),
6957                                                                                                                                                                         vector<AttachmentReference>(),
6958                                                                                                                                                                         vector<AttachmentReference>(),
6959                                                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL),
6960                                                                                                                                                                         vector<deUint32>())),
6961                                                                                                                  vector<SubpassDependency>());
6962                                                 const TestConfig        testConfig      (renderPass,
6963                                                                                                                  renderTypes[renderTypeNdx].types,
6964                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6965                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6966                                                                                                                  targetSize,
6967                                                                                                                  renderPos,
6968                                                                                                                  renderSize,
6969                                                                                                                  DE_FALSE,
6970                                                                                                                  90239,
6971                                                                                                                  0,
6972                                                                                                                  testConfigExternal.allocationKind,
6973                                                                                                                  testConfigExternal.renderingType);
6974                                                 const string            testName        (string(renderTypes[renderTypeNdx].str) + "_depth_read_only");
6975
6976                                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6977                                         }
6978
6979                                         {
6980                                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
6981                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6982                                                                                                                                                   isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6983                                                                                                                                                   isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6984                                                                                                                                                   isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6985                                                                                                                                                   isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
6986                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6987                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6988                                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6989                                                                                                                                                                         0u,
6990                                                                                                                                                                         vector<AttachmentReference>(),
6991                                                                                                                                                                         vector<AttachmentReference>(),
6992                                                                                                                                                                         vector<AttachmentReference>(),
6993                                                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL),
6994                                                                                                                                                                         vector<deUint32>())),
6995                                                                                                                  vector<SubpassDependency>());
6996                                                 const TestConfig        testConfig      (renderPass,
6997                                                                                                                  renderTypes[renderTypeNdx].types,
6998                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6999                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7000                                                                                                                  targetSize,
7001                                                                                                                  renderPos,
7002                                                                                                                  renderSize,
7003                                                                                                                  DE_FALSE,
7004                                                                                                                  90239,
7005                                                                                                                  0,
7006                                                                                                                  testConfigExternal.allocationKind,
7007                                                                                                                  testConfigExternal.renderingType);
7008                                                 const string            testName        (string(renderTypes[renderTypeNdx].str) + "_stencil_read_only");
7009
7010                                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7011                                         }
7012                                 }
7013                         }
7014
7015                         formatGroup->addChild(loadOpGroup.release());
7016                 }
7017
7018                 if (testConfigExternal.renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7019                 {
7020                         de::MovePtr<tcu::TestCaseGroup> inputGroup (new tcu::TestCaseGroup(testCtx, "input", "Test attachment format as input"));
7021
7022                         for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
7023                         {
7024                                 const VkAttachmentLoadOp                loadOp          = loadOps[loadOpNdx].op;
7025                                 de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
7026
7027                                 for (size_t storeOpNdx = 0; storeOpNdx < DE_LENGTH_OF_ARRAY(storeOps); storeOpNdx++)
7028                                 {
7029                                         const VkImageAspectFlags                inputAttachmentAspectMask       = (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2)
7030                                                                                                                                                                 ? formatAspectFlags
7031                                                                                                                                                                 : static_cast<VkImageAspectFlags>(0);
7032                                         const VkAttachmentStoreOp               storeOp                                         = storeOps[storeOpNdx].op;
7033                                         de::MovePtr<tcu::TestCaseGroup> storeOpGroup                            (new tcu::TestCaseGroup(testCtx, storeOps[storeOpNdx].str, storeOps[storeOpNdx].str));
7034
7035                                         for (size_t useInputAspectNdx = 0; useInputAspectNdx < 2; useInputAspectNdx++)
7036                                         {
7037                                                 const bool useInputAspect = useInputAspectNdx != 0;
7038
7039                                                 if (testConfigExternal.renderingType == RENDERING_TYPE_RENDERPASS2 && useInputAspect)
7040                                                         continue;
7041
7042                                                 for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
7043                                                 {
7044                                                         {
7045                                                                 vector<Attachment>                                                      attachments;
7046                                                                 vector<Subpass>                                                         subpasses;
7047                                                                 vector<SubpassDependency>                                       deps;
7048                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
7049
7050                                                                 attachments.push_back(Attachment(vkFormat,
7051                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7052                                                                                                                                  loadOp,
7053                                                                                                                                  storeOp,
7054                                                                                                                                  loadOp,
7055                                                                                                                                  storeOp,
7056                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7057                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7058
7059                                                                 attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7060                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7061                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7062                                                                                                                                  VK_ATTACHMENT_STORE_OP_STORE,
7063                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7064                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
7065                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7066                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7067
7068                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7069                                                                                                                         0u,
7070                                                                                                                         vector<AttachmentReference>(),
7071                                                                                                                         vector<AttachmentReference>(),
7072                                                                                                                         vector<AttachmentReference>(),
7073                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7074                                                                                                                         vector<deUint32>()));
7075                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7076                                                                                                                         0u,
7077                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7078                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7079                                                                                                                         vector<AttachmentReference>(),
7080                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7081                                                                                                                         vector<deUint32>()));
7082
7083                                                                 deps.push_back(SubpassDependency(0, 1,
7084                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7085                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7086
7087                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7088                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7089                                                                                                                                 0u));
7090
7091                                                                 if (useInputAspect)
7092                                                                 {
7093                                                                         const VkInputAttachmentAspectReference inputAspect =
7094                                                                         {
7095                                                                                 1u,
7096                                                                                 0u,
7097                                                                                 (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7098                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7099                                                                         };
7100
7101                                                                         inputAspects.push_back(inputAspect);
7102                                                                 }
7103
7104                                                                 {
7105                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7106                                                                         const TestConfig        testConfig      (renderPass,
7107                                                                                                                                          renderTypes[renderTypeNdx].types,
7108                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
7109                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
7110                                                                                                                                          targetSize,
7111                                                                                                                                          renderPos,
7112                                                                                                                                          renderSize,
7113                                                                                                                                          DE_FALSE,
7114                                                                                                                                          89246,
7115                                                                                                                                          0,
7116                                                                                                                                          testConfigExternal.allocationKind,
7117                                                                                                                                          testConfigExternal.renderingType);
7118                                                                         const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : ""));
7119
7120                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7121                                                                 }
7122                                                         }
7123                                                         {
7124                                                                 vector<Attachment>                                                      attachments;
7125                                                                 vector<Subpass>                                                         subpasses;
7126                                                                 vector<SubpassDependency>                                       deps;
7127                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
7128
7129                                                                 attachments.push_back(Attachment(vkFormat,
7130                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7131                                                                                                                                  loadOp,
7132                                                                                                                                  storeOp,
7133                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7134                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
7135                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7136                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7137
7138                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7139                                                                                                                         0u,
7140                                                                                                                         vector<AttachmentReference>(),
7141                                                                                                                         vector<AttachmentReference>(),
7142                                                                                                                         vector<AttachmentReference>(),
7143                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7144                                                                                                                         vector<deUint32>()));
7145                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7146                                                                                                                         0u,
7147                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL, inputAttachmentAspectMask)),
7148                                                                                                                         vector<AttachmentReference>(),
7149                                                                                                                         vector<AttachmentReference>(),
7150                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL),
7151                                                                                                                         vector<deUint32>()));
7152
7153                                                                 deps.push_back(SubpassDependency(0, 1,
7154                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7155                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7156
7157                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7158                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7159                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
7160
7161                                                                 deps.push_back(SubpassDependency(1, 1,
7162                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7163                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7164                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7165                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7166                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
7167
7168
7169                                                                 if (useInputAspect)
7170                                                                 {
7171                                                                         const VkInputAttachmentAspectReference inputAspect =
7172                                                                         {
7173                                                                                 1u,
7174                                                                                 0u,
7175
7176                                                                                 (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7177                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7178                                                                         };
7179
7180                                                                         inputAspects.push_back(inputAspect);
7181                                                                 }
7182
7183                                                                 {
7184                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7185                                                                         const TestConfig        testConfig      (renderPass,
7186                                                                                                                                          renderTypes[renderTypeNdx].types,
7187                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
7188                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
7189                                                                                                                                          targetSize,
7190                                                                                                                                          renderPos,
7191                                                                                                                                          renderSize,
7192                                                                                                                                          DE_FALSE,
7193                                                                                                                                          89246,
7194                                                                                                                                          0,
7195                                                                                                                                          testConfigExternal.allocationKind,
7196                                                                                                                                          testConfigExternal.renderingType);
7197                                                                         const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : ""));
7198
7199                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7200                                                                 }
7201                                                         }
7202
7203                                                         if (isStencilAttachment && isDepthAttachment)
7204                                                         {
7205                                                                 // Depth read only
7206                                                                 {
7207                                                                         vector<Attachment>                                                      attachments;
7208                                                                         vector<Subpass>                                                         subpasses;
7209                                                                         vector<SubpassDependency>                                       deps;
7210                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7211
7212                                                                         attachments.push_back(Attachment(vkFormat,
7213                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7214                                                                                                                                          loadOp,
7215                                                                                                                                          storeOp,
7216                                                                                                                                          loadOp,
7217                                                                                                                                          storeOp,
7218                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7219                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7220
7221                                                                         attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7222                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7223                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7224                                                                                                                                          VK_ATTACHMENT_STORE_OP_STORE,
7225                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7226                                                                                                                                          VK_ATTACHMENT_STORE_OP_DONT_CARE,
7227                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7228                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7229
7230                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7231                                                                                                                                 0u,
7232                                                                                                                                 vector<AttachmentReference>(),
7233                                                                                                                                 vector<AttachmentReference>(),
7234                                                                                                                                 vector<AttachmentReference>(),
7235                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7236                                                                                                                                 vector<deUint32>()));
7237                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7238                                                                                                                                 0u,
7239                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, inputAttachmentAspectMask)),
7240                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7241                                                                                                                                 vector<AttachmentReference>(),
7242                                                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7243                                                                                                                                 vector<deUint32>()));
7244
7245                                                                         deps.push_back(SubpassDependency(0, 1,
7246                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7247                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7248
7249                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7250                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7251                                                                                                                                         0u));
7252
7253                                                                         if (useInputAspect)
7254                                                                         {
7255                                                                                 const VkInputAttachmentAspectReference inputAspect =
7256                                                                                 {
7257                                                                                         1u,
7258                                                                                         0u,
7259
7260                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7261                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7262                                                                                 };
7263
7264                                                                                 inputAspects.push_back(inputAspect);
7265                                                                         }
7266
7267                                                                         {
7268                                                                                 const RenderPass        renderPass       (attachments, subpasses, deps, inputAspects);
7269                                                                                 const TestConfig        testConfig       (renderPass,
7270                                                                                                                                                  renderTypes[renderTypeNdx].types,
7271                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7272                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7273                                                                                                                                                  targetSize,
7274                                                                                                                                                  renderPos,
7275                                                                                                                                                  renderSize,
7276                                                                                                                                                  DE_FALSE,
7277                                                                                                                                                  89246,
7278                                                                                                                                                  0,
7279                                                                                                                                                  testConfigExternal.allocationKind,
7280                                                                                                                                                  testConfigExternal.renderingType);
7281                                                                                 const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : "") + "_depth_read_only");
7282
7283                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7284                                                                         }
7285                                                                 }
7286                                                                 {
7287                                                                         vector<Attachment>                                                      attachments;
7288                                                                         vector<Subpass>                                                         subpasses;
7289                                                                         vector<SubpassDependency>                                       deps;
7290                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7291
7292                                                                         attachments.push_back(Attachment(vkFormat,
7293                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7294                                                                                                                                          loadOp,
7295                                                                                                                                          storeOp,
7296                                                                                                                                          loadOp,
7297                                                                                                                                          storeOp,
7298                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7299                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7300
7301                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7302                                                                                                                                 0u,
7303                                                                                                                                 vector<AttachmentReference>(),
7304                                                                                                                                 vector<AttachmentReference>(),
7305                                                                                                                                 vector<AttachmentReference>(),
7306                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7307                                                                                                                                 vector<deUint32>()));
7308                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7309                                                                                                                                 0u,
7310                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, inputAttachmentAspectMask)),
7311                                                                                                                                 vector<AttachmentReference>(),
7312                                                                                                                                 vector<AttachmentReference>(),
7313                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL),
7314                                                                                                                                 vector<deUint32>()));
7315
7316                                                                         deps.push_back(SubpassDependency(0, 1,
7317                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7318                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7319
7320                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7321                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7322                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7323
7324                                                                         deps.push_back(SubpassDependency(1, 1,
7325                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7326                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7327
7328                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7329                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7330                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7331
7332                                                                         if (useInputAspect)
7333                                                                         {
7334                                                                                 const VkInputAttachmentAspectReference inputAspect =
7335                                                                                 {
7336                                                                                         1u,
7337                                                                                         0u,
7338
7339                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7340                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7341                                                                                 };
7342
7343                                                                                 inputAspects.push_back(inputAspect);
7344                                                                         }
7345
7346                                                                         {
7347                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7348                                                                                 const TestConfig        testConfig      (renderPass,
7349                                                                                                                                                  renderTypes[renderTypeNdx].types,
7350                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7351                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7352                                                                                                                                                  targetSize,
7353                                                                                                                                                  renderPos,
7354                                                                                                                                                  renderSize,
7355                                                                                                                                                  DE_FALSE,
7356                                                                                                                                                  89246,
7357                                                                                                                                                  0,
7358                                                                                                                                                  testConfigExternal.allocationKind,
7359                                                                                                                                                  testConfigExternal.renderingType);
7360                                                                                 const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : "") + "_depth_read_only");
7361
7362                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7363                                                                         }
7364                                                                 }
7365                                                                 // Stencil read only
7366                                                                 {
7367                                                                         vector<Attachment>                                                      attachments;
7368                                                                         vector<Subpass>                                                         subpasses;
7369                                                                         vector<SubpassDependency>                                       deps;
7370                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7371
7372                                                                         attachments.push_back(Attachment(vkFormat,
7373                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7374                                                                                                                                          loadOp,
7375                                                                                                                                          storeOp,
7376                                                                                                                                          loadOp,
7377                                                                                                                                          storeOp,
7378                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7379                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7380
7381                                                                         attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7382                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7383                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7384                                                                                                                                          VK_ATTACHMENT_STORE_OP_STORE,
7385                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7386                                                                                                                                          VK_ATTACHMENT_STORE_OP_DONT_CARE,
7387                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7388                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7389
7390                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7391                                                                                                                                 0u,
7392                                                                                                                                 vector<AttachmentReference>(),
7393                                                                                                                                 vector<AttachmentReference>(),
7394                                                                                                                                 vector<AttachmentReference>(),
7395                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7396                                                                                                                                 vector<deUint32>()));
7397                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7398                                                                                                                                 0u,
7399                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7400                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7401                                                                                                                                 vector<AttachmentReference>(),
7402                                                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7403                                                                                                                                 vector<deUint32>()));
7404
7405                                                                         deps.push_back(SubpassDependency(0, 1,
7406                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
7407                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7408
7409                                                                                                                                         vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7410                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7411                                                                                                                                         0u));
7412
7413                                                                         if (useInputAspect)
7414                                                                         {
7415                                                                                 const VkInputAttachmentAspectReference inputAspect =
7416                                                                                 {
7417                                                                                         1u,
7418                                                                                         0u,
7419
7420                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7421                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7422                                                                                 };
7423
7424                                                                                 inputAspects.push_back(inputAspect);
7425                                                                         }
7426
7427                                                                         {
7428                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7429                                                                                 const TestConfig        testConfig      (renderPass,
7430                                                                                                                                                  renderTypes[renderTypeNdx].types,
7431                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7432                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7433                                                                                                                                                  targetSize,
7434                                                                                                                                                  renderPos,
7435                                                                                                                                                  renderSize,
7436                                                                                                                                                  DE_FALSE,
7437                                                                                                                                                  89246,
7438                                                                                                                                                  0,
7439                                                                                                                                                  testConfigExternal.allocationKind,
7440                                                                                                                                                  testConfigExternal.renderingType);
7441                                                                                 const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : "") + "_stencil_read_only");
7442
7443                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7444                                                                         }
7445                                                                 }
7446                                                                 {
7447                                                                         vector<Attachment>                                                      attachments;
7448                                                                         vector<Subpass>                                                         subpasses;
7449                                                                         vector<SubpassDependency>                                       deps;
7450                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7451
7452                                                                         attachments.push_back(Attachment(vkFormat,
7453                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7454                                                                                                                                          loadOp,
7455                                                                                                                                          storeOp,
7456                                                                                                                                          loadOp,
7457                                                                                                                                          storeOp,
7458                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7459                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7460
7461                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7462                                                                                                                                 0u,
7463                                                                                                                                 vector<AttachmentReference>(),
7464                                                                                                                                 vector<AttachmentReference>(),
7465                                                                                                                                 vector<AttachmentReference>(),
7466                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7467                                                                                                                                 vector<deUint32>()));
7468                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7469                                                                                                                                 0u,
7470                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7471                                                                                                                                 vector<AttachmentReference>(),
7472                                                                                                                                 vector<AttachmentReference>(),
7473                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL),
7474                                                                                                                                 vector<deUint32>()));
7475
7476                                                                         deps.push_back(SubpassDependency(0, 1,
7477                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7478                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7479
7480                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7481                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7482                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7483
7484                                                                         deps.push_back(SubpassDependency(1, 1,
7485                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7486                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7487
7488                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7489                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7490                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7491
7492
7493                                                                         if (useInputAspect)
7494                                                                         {
7495                                                                                 const VkInputAttachmentAspectReference inputAspect =
7496                                                                                 {
7497                                                                                         1u,
7498                                                                                         0u,
7499
7500                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7501                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7502                                                                                 };
7503
7504                                                                                 inputAspects.push_back(inputAspect);
7505                                                                         }
7506
7507                                                                         {
7508                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7509                                                                                 const TestConfig        testConfig      (renderPass,
7510                                                                                                                                                  renderTypes[renderTypeNdx].types,
7511                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7512                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7513                                                                                                                                                  targetSize,
7514                                                                                                                                                  renderPos,
7515                                                                                                                                                  renderSize,
7516                                                                                                                                                  DE_FALSE,
7517                                                                                                                                                  89246,
7518                                                                                                                                                  0,
7519                                                                                                                                                  testConfigExternal.allocationKind,
7520                                                                                                                                                  testConfigExternal.renderingType);
7521                                                                                 const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : "") + "_stencil_read_only");
7522
7523                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7524                                                                         }
7525                                                                 }
7526                                                         }
7527                                                 }
7528                                         }
7529
7530                                         loadOpGroup->addChild(storeOpGroup.release());
7531                                 }
7532
7533                                 inputGroup->addChild(loadOpGroup.release());
7534                         }
7535
7536                         formatGroup->addChild(inputGroup.release());
7537                 }
7538
7539                 group->addChild(formatGroup.release());
7540         }
7541 }
7542
7543 void addRenderPassTests (tcu::TestCaseGroup* group, const AllocationKind allocationKind, const RenderingType renderingType)
7544 {
7545         const TestConfigExternal        testConfigExternal      (allocationKind, renderingType);
7546
7547         addTestGroup(group, "simple", "Simple basic render pass tests", addSimpleTests, testConfigExternal);
7548         addTestGroup(group, "formats", "Tests for different image formats.", addFormatTests, testConfigExternal);
7549         addTestGroup(group, "attachment", "Attachment format and count tests with load and store ops and image layouts", addAttachmentTests, testConfigExternal);
7550         addTestGroup(group, "attachment_write_mask", "Attachment write mask tests", addAttachmentWriteMaskTests, testConfigExternal);
7551
7552         if (renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7553                 addTestGroup(group, "attachment_allocation", "Attachment allocation tests", addAttachmentAllocationTests, testConfigExternal);
7554 }
7555
7556 de::MovePtr<tcu::TestCaseGroup> createSuballocationTests(tcu::TestContext& testCtx, RenderingType renderingType)
7557 {
7558         de::MovePtr<tcu::TestCaseGroup> suballocationTestsGroup(new tcu::TestCaseGroup(testCtx, "suballocation", "Suballocation RenderPass Tests"));
7559
7560         addRenderPassTests(suballocationTestsGroup.get(), ALLOCATION_KIND_SUBALLOCATED, renderingType);
7561
7562         return suballocationTestsGroup;
7563 }
7564
7565 de::MovePtr<tcu::TestCaseGroup> createDedicatedAllocationTests(tcu::TestContext& testCtx, RenderingType renderingType)
7566 {
7567         de::MovePtr<tcu::TestCaseGroup> dedicatedAllocationTestsGroup(new tcu::TestCaseGroup(testCtx, "dedicated_allocation", "RenderPass Tests For Dedicated Allocation"));
7568
7569         addRenderPassTests(dedicatedAllocationTestsGroup.get(), ALLOCATION_KIND_DEDICATED, renderingType);
7570
7571         return dedicatedAllocationTestsGroup;
7572 }
7573
7574 tcu::TestCaseGroup* createRenderPassTestsInternal (tcu::TestContext& testCtx, RenderingType renderingType)
7575 {
7576         const char*             renderingTestsGroupName         = (renderingType == RENDERING_TYPE_RENDERPASS_LEGACY) ? "renderpass" :
7577                                                                                                   (renderingType == RENDERING_TYPE_RENDERPASS2) ? "renderpass2" :
7578                                                                                                   (renderingType == RENDERING_TYPE_DYNAMIC_RENDERING) ? "dynamic_rendering" :
7579                                                                                                   "";
7580         const char*             renderingTestsGroupDescription  = (renderingType == RENDERING_TYPE_RENDERPASS_LEGACY) ? "RenderPass Tests" :
7581                                                                                                           (renderingType == RENDERING_TYPE_RENDERPASS2) ? "RenderPass2 Tests" :
7582                                                                                                           (renderingType == RENDERING_TYPE_DYNAMIC_RENDERING) ? "Dynamic Rendering Tests" :
7583                                                                                                           "";
7584
7585         de::MovePtr<tcu::TestCaseGroup> renderingTests                                  (new tcu::TestCaseGroup(testCtx, renderingTestsGroupName, renderingTestsGroupDescription));
7586         de::MovePtr<tcu::TestCaseGroup> suballocationTestGroup                  = createSuballocationTests(testCtx, renderingType);
7587         de::MovePtr<tcu::TestCaseGroup> dedicatedAllocationTestGroup    = createDedicatedAllocationTests(testCtx, renderingType);
7588
7589         switch (renderingType)
7590         {
7591         case RENDERING_TYPE_RENDERPASS_LEGACY:
7592                 suballocationTestGroup->addChild(createRenderPassMultisampleTests(testCtx));
7593                 suballocationTestGroup->addChild(createRenderPassMultisampleResolveTests(testCtx));
7594                 suballocationTestGroup->addChild(createRenderPassSubpassDependencyTests(testCtx));
7595                 suballocationTestGroup->addChild(createRenderPassSampleReadTests(testCtx));
7596                 suballocationTestGroup->addChild(createRenderPassSparseRenderTargetTests(testCtx));
7597
7598                 renderingTests->addChild(createRenderPassMultipleSubpassesMultipleCommandBuffersTests(testCtx));
7599                 break;
7600
7601         case RENDERING_TYPE_RENDERPASS2:
7602                 suballocationTestGroup->addChild(createRenderPass2MultisampleTests(testCtx));
7603                 suballocationTestGroup->addChild(createRenderPass2MultisampleResolveTests(testCtx));
7604                 suballocationTestGroup->addChild(createRenderPass2SubpassDependencyTests(testCtx));
7605                 suballocationTestGroup->addChild(createRenderPass2SampleReadTests(testCtx));
7606                 suballocationTestGroup->addChild(createRenderPass2SparseRenderTargetTests(testCtx));
7607
7608                 renderingTests->addChild(createRenderPass2DepthStencilResolveTests(testCtx));
7609                 renderingTests->addChild(createFragmentDensityMapTests(testCtx));
7610                 break;
7611
7612         case RENDERING_TYPE_DYNAMIC_RENDERING:
7613                 suballocationTestGroup->addChild(createDynamicRenderingMultisampleResolveTests(testCtx));
7614                 suballocationTestGroup->addChild(createDynamicRenderingSparseRenderTargetTests(testCtx));
7615
7616                 renderingTests->addChild(createDynamicRenderingBasicTests(testCtx));
7617                 break;
7618         }
7619
7620         if (renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7621         {
7622                 suballocationTestGroup->addChild(createRenderPassUnusedAttachmentTests(testCtx, renderingType));
7623                 suballocationTestGroup->addChild(createRenderPassUnusedAttachmentSparseFillingTests(testCtx, renderingType));
7624         }
7625
7626         suballocationTestGroup->addChild(createRenderPassUnusedClearAttachmentTests(testCtx, renderingType));
7627         suballocationTestGroup->addChild(createRenderPassLoadStoreOpNoneTests(testCtx, renderingType));
7628
7629         renderingTests->addChild(suballocationTestGroup.release());
7630         renderingTests->addChild(dedicatedAllocationTestGroup.release());
7631
7632         return renderingTests.release();
7633 }
7634
7635 } // anonymous
7636
7637 tcu::TestCaseGroup* createRenderPassTests (tcu::TestContext& testCtx)
7638 {
7639         return createRenderPassTestsInternal(testCtx, RENDERING_TYPE_RENDERPASS_LEGACY);
7640 }
7641
7642 tcu::TestCaseGroup* createRenderPass2Tests (tcu::TestContext& testCtx)
7643 {
7644         return createRenderPassTestsInternal(testCtx, RENDERING_TYPE_RENDERPASS2);
7645 }
7646
7647 tcu::TestCaseGroup* createDynamicRenderingTests(tcu::TestContext& testCtx)
7648 {
7649         return createRenderPassTestsInternal(testCtx, RENDERING_TYPE_DYNAMIC_RENDERING);
7650 }
7651
7652 } // vkt