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