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         const VkPipelineDepthStencilStateCreateInfo depthStencilState =
2010         {
2011                 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,     // sType
2012                 DE_NULL,                                                                                                        // pNext
2013                 (VkPipelineDepthStencilStateCreateFlags)0u,
2014                 writeDepth,                                                                                                     // depthTestEnable
2015                 writeDepth,                                                                                                     // depthWriteEnable
2016                 VK_COMPARE_OP_ALWAYS,                                                                           // depthCompareOp
2017                 VK_FALSE,                                                                                                       // depthBoundsEnable
2018                 writeStencil,                                                                                           // stencilTestEnable
2019                 {
2020                         VK_STENCIL_OP_REPLACE,                                                                  // stencilFailOp
2021                         VK_STENCIL_OP_REPLACE,                                                                  // stencilPassOp
2022                         VK_STENCIL_OP_REPLACE,                                                                  // stencilDepthFailOp
2023                         VK_COMPARE_OP_ALWAYS,                                                                   // stencilCompareOp
2024                         ~0u,                                                                                                    // stencilCompareMask
2025                         ~0u,                                                                                                    // stencilWriteMask
2026                         ((stencilIndex % 2) == 0) ? ~0x0u : 0x0u                                // stencilReference
2027                 },                                                                                                                      // front
2028                 {
2029                         VK_STENCIL_OP_REPLACE,                                                                  // stencilFailOp
2030                         VK_STENCIL_OP_REPLACE,                                                                  // stencilPassOp
2031                         VK_STENCIL_OP_REPLACE,                                                                  // stencilDepthFailOp
2032                         VK_COMPARE_OP_ALWAYS,                                                                   // stencilCompareOp
2033                         ~0u,                                                                                                    // stencilCompareMask
2034                         ~0u,                                                                                                    // stencilWriteMask
2035                         ((stencilIndex % 2) == 0) ? ~0x0u : 0x0u                                // stencilReference
2036                 },                                                                                                                      // back
2037
2038                 0.0f,                                                                                                           // minDepthBounds;
2039                 1.0f                                                                                                            // maxDepthBounds;
2040         };
2041
2042         const VkPipelineColorBlendStateCreateInfo blendState =
2043         {
2044                 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,                       // sType
2045                 DE_NULL,                                                                                                                        // pNext
2046                 (VkPipelineColorBlendStateCreateFlags)0u,
2047                 VK_FALSE,                                                                                                                       // logicOpEnable
2048                 VK_LOGIC_OP_COPY,                                                                                                       // logicOp
2049                 (deUint32)attachmentBlendStates.size(),                                                         // attachmentCount
2050                 attachmentBlendStates.empty() ? DE_NULL : &attachmentBlendStates[0],// pAttachments
2051                 { 0.0f, 0.0f, 0.0f, 0.0f }                                                                                      // blendConst
2052         };
2053
2054 #ifndef CTS_USES_VULKANSC
2055         std::vector<vk::VkFormat> colorAttachmentFormats;
2056         for (deUint32 i = 0; i < renderInfo.getColorAttachmentCount(); ++i)
2057                 colorAttachmentFormats.push_back(renderInfo.getColorAttachment(i).getFormat());
2058
2059         vk::VkFormat depthFormat = VK_FORMAT_UNDEFINED;
2060         vk::VkFormat stencilFormat = VK_FORMAT_UNDEFINED;
2061         if (renderInfo.getDepthStencilAttachment())
2062         {
2063                 const Attachment& attachment = *renderInfo.getDepthStencilAttachment();
2064                 vk::VkFormat depthStencilFormat = attachment.getFormat();
2065                 if (depthStencilFormat != VK_FORMAT_UNDEFINED)
2066                 {
2067                         if (tcu::hasDepthComponent(mapVkFormat(depthStencilFormat).order))
2068                         {
2069                                 depthFormat = depthStencilFormat;
2070                         }
2071                         if (tcu::hasStencilComponent(mapVkFormat(depthStencilFormat).order))
2072                         {
2073                                 stencilFormat = depthStencilFormat;
2074                         }
2075                 }
2076         }
2077
2078
2079         VkPipelineRenderingCreateInfoKHR renderingCreateInfo
2080         {
2081                 VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
2082                 DE_NULL,
2083                 0u,
2084                 static_cast<deUint32>(colorAttachmentFormats.size()),
2085                 colorAttachmentFormats.data(),
2086                 depthFormat,
2087                 stencilFormat
2088         };
2089 #endif // CTS_USES_VULKANSC
2090
2091         return makeGraphicsPipeline(vk,                                                                                         // const DeviceInterface&                        vk
2092                                                                 device,                                                                                 // const VkDevice                                device
2093                                                                 pipelineLayout,                                                                 // const VkPipelineLayout                        pipelineLayout
2094                                                                 vertexShaderModule,                                                             // const VkShaderModule                          vertexShaderModule
2095                                                                 DE_NULL,                                                                                // const VkShaderModule                          tessellationControlShaderModule
2096                                                                 DE_NULL,                                                                                // const VkShaderModule                          tessellationEvalShaderModule
2097                                                                 DE_NULL,                                                                                // const VkShaderModule                          geometryShaderModule
2098                                                                 fragmentShaderModule,                                                   // const VkShaderModule                          fragmentShaderModule
2099                                                                 renderPass,                                                                             // const VkRenderPass                            renderPass
2100                                                                 renderInfo.getSubpassIndex(),                                   // const deUint32                                subpass
2101                                                                 &vertexInputState,                                                              // const VkPipelineVertexInputStateCreateInfo*   vertexInputStateCreateInfo
2102                                                                 &inputAssemblyState,                                                    // const VkPipelineInputAssemblyStateCreateInfo* pInputAssemblyState;
2103                                                                 DE_NULL,                                                                                // const VkPipelineRasterizationStateCreateInfo* rasterizationStateCreateInfo
2104                                                                 &viewportState,                                                                 // const VkPipelineViewportStateCreateInfo*      pViewportStat;
2105                                                                 &rasterizationState,                                                    // const VkPipelineRasterizationStateCreateInfo* pRasterizationState
2106                                                                 &multisampleState,                                                              // const VkPipelineMultisampleStateCreateInfo*   multisampleStateCreateInfo
2107                                                                 &depthStencilState,                                                             // const VkPipelineDepthStencilStateCreateInfo*  depthStencilStateCreateInfo
2108                                                                 renderInfo.getOmitBlendState()
2109                                                                         ? DE_NULL : &blendState,                                        // const VkPipelineColorBlendStateCreateInfo*    colorBlendStateCreateInfo
2110                                                                 DE_NULL,                                                                                // const VkPipelineDynamicStateCreateInfo*       dynamicStateCreateInfo
2111 #ifndef CTS_USES_VULKANSC
2112                                                                 (renderPass == DE_NULL)
2113                                                                         ? &renderingCreateInfo : DE_NULL);                      // const void*                                   pNext)
2114 #else
2115                                                                 DE_NULL);                                                                               // const void*                                   pNext)
2116 #endif // CTS_USES_VULKANSC
2117 }
2118
2119 #ifndef CTS_USES_VULKANSC
2120 void beginDynamicRendering(const DeviceInterface&                                                               vk,
2121                                                    VkCommandBuffer                                                                              commandBuffer,
2122                                                    const RenderPass&                                                                    renderPassInfo,
2123                                                    const vector<de::SharedPtr<AttachmentResources> >&   attachmentResources,
2124                                                    const VkRect2D&                                                                              renderArea,
2125                                                    const vector<Maybe<VkClearValue> >&                                  renderPassClearValues,
2126                                                    const VkRenderingFlagsKHR                                                    renderingFlags = 0u)
2127 {
2128         const float                     clearNan                = tcu::Float32::nan().asFloat();
2129         const VkClearValue      clearValueNan   = makeClearValueColorF32(clearNan, clearNan, clearNan, clearNan);
2130
2131         // translate structures that were prepared to construct renderpass to structures needed for dynamic rendering
2132
2133         std::vector<vk::VkRenderingAttachmentInfoKHR>   colorAttachmentVect;
2134         const Subpass&                                                                  subpassInfo                             = renderPassInfo.getSubpasses()[0];
2135         const vector<AttachmentReference>&                              colorAttachmentsInfo    = subpassInfo.getColorAttachments();
2136         const vector<AttachmentReference>&                              resolveAttachmentsInfo  = subpassInfo.getResolveAttachments();
2137
2138         for (deUint32 i = 0; i < colorAttachmentsInfo.size(); ++i)
2139         {
2140                 const AttachmentReference&              colorAttachmentReference        = colorAttachmentsInfo[i];
2141                 const deUint32                                  colorAttachmentIndex            = colorAttachmentReference.getAttachment();
2142                 const Attachment&                               colorAttachmentInfo                     = renderPassInfo.getAttachments()[colorAttachmentIndex];
2143
2144                 VkResolveModeFlagBits                   resolveMode                                     = VK_RESOLVE_MODE_NONE;
2145                 VkImageView                                             resolveImageView                        = DE_NULL;
2146                 VkImageLayout                                   resolveImageLayout                      = VK_IMAGE_LAYOUT_UNDEFINED;
2147
2148                 // handle resolve attachments if they were specified
2149                 if (!resolveAttachmentsInfo.empty())
2150                 {
2151                         const AttachmentReference&      resolveAttachmentReference      = resolveAttachmentsInfo[i];
2152                         const deUint32                          resolveAttachmentIndex          = resolveAttachmentReference.getAttachment();
2153                         const Attachment&                       resolveAttachmentInfo           = renderPassInfo.getAttachments()[resolveAttachmentIndex];
2154
2155                         resolveMode                     = VK_RESOLVE_MODE_AVERAGE_BIT;
2156                         resolveImageView        = attachmentResources[resolveAttachmentIndex]->getAttachmentView();
2157                         resolveImageLayout      = resolveAttachmentInfo.getInitialLayout();
2158                 }
2159
2160                 colorAttachmentVect.push_back({
2161                         vk::VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,                    // VkStructureType                      sType
2162                         DE_NULL,                                                                                                                // const void*                          pNext
2163                         attachmentResources[colorAttachmentIndex]->getAttachmentView(), // VkImageView                          imageView
2164                         colorAttachmentReference.getImageLayout(),                                              // VkImageLayout                        imageLayout
2165                         resolveMode,                                                                                                    // VkResolveModeFlagBits        resolveMode
2166                         resolveImageView,                                                                                               // VkImageView                          resolveImageView
2167                         resolveImageLayout,                                                                                             // VkImageLayout                        resolveImageLayout
2168                         colorAttachmentInfo.getLoadOp(),                                                                // VkAttachmentLoadOp           loadOp
2169                         colorAttachmentInfo.getStoreOp(),                                                               // VkAttachmentStoreOp          storeOp
2170                         (renderPassClearValues[colorAttachmentIndex] ?
2171                                 *renderPassClearValues[colorAttachmentIndex] :
2172                                 clearValueNan)                                                                                          // VkClearValue                         clearValue
2173                         });
2174         }
2175
2176         VkRenderingAttachmentInfoKHR*   pDepthAttachment        = DE_NULL;
2177         VkRenderingAttachmentInfoKHR*   pStencilAttachment      = DE_NULL;
2178         VkRenderingAttachmentInfoKHR    depthAttachment
2179         {
2180                 vk::VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,                            // VkStructureType                      sType;
2181                 DE_NULL,                                                                                                                        // const void*                          pNext;
2182                 DE_NULL,                                                                                                                        // VkImageView                          imageView;
2183                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                                      // VkImageLayout                        imageLayout;
2184                 VK_RESOLVE_MODE_NONE,                                                                                           // VkResolveModeFlagBits        resolveMode;
2185                 DE_NULL,                                                                                                                        // VkImageView                          resolveImageView;
2186                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                                      // VkImageLayout                        resolveImageLayout;
2187                 VK_ATTACHMENT_LOAD_OP_LOAD,                                                                                     // VkAttachmentLoadOp           loadOp;
2188                 VK_ATTACHMENT_STORE_OP_STORE,                                                                           // VkAttachmentStoreOp          storeOp;
2189                 clearValueNan                                                                                                           // VkClearValue                         clearValue;
2190         };
2191         VkRenderingAttachmentInfoKHR    stencilAttachment                                       = depthAttachment;
2192         const AttachmentReference&              depthStencilAttachmentReference         = subpassInfo.getDepthStencilAttachment();
2193         const deUint32                                  dsAttachmentIndex                                       = depthStencilAttachmentReference.getAttachment();
2194
2195         if (dsAttachmentIndex != VK_ATTACHMENT_UNUSED)
2196         {
2197                 const Attachment&                       dsAttachmentInfo        = renderPassInfo.getAttachments()[dsAttachmentIndex];
2198                 const tcu::TextureFormat        format                          = mapVkFormat(dsAttachmentInfo.getFormat());
2199
2200                 if (tcu::hasDepthComponent(format.order))
2201                 {
2202                         depthAttachment.imageView               = attachmentResources[dsAttachmentIndex]->getAttachmentView();
2203                         depthAttachment.imageLayout             = depthStencilAttachmentReference.getImageLayout();
2204                         depthAttachment.loadOp                  = dsAttachmentInfo.getLoadOp();
2205                         depthAttachment.storeOp                 = dsAttachmentInfo.getStoreOp();
2206
2207                         if (renderPassClearValues[dsAttachmentIndex])
2208                                 depthAttachment.clearValue = *renderPassClearValues[dsAttachmentIndex];
2209
2210                         pDepthAttachment = &depthAttachment;
2211                 }
2212
2213                 if (tcu::hasStencilComponent(format.order))
2214                 {
2215                         stencilAttachment.imageView = attachmentResources[dsAttachmentIndex]->getAttachmentView();
2216                         stencilAttachment.imageLayout = depthStencilAttachmentReference.getImageLayout();
2217                         stencilAttachment.loadOp = dsAttachmentInfo.getStencilLoadOp();
2218                         stencilAttachment.storeOp = dsAttachmentInfo.getStencilStoreOp();
2219
2220                         if (renderPassClearValues[dsAttachmentIndex])
2221                                 stencilAttachment.clearValue = *renderPassClearValues[dsAttachmentIndex];
2222
2223                         pStencilAttachment = &stencilAttachment;
2224                 }
2225         }
2226
2227         vk::VkRenderingInfoKHR renderingInfo
2228         {
2229                 vk::VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
2230                 DE_NULL,
2231                 renderingFlags,                                                                                                         // VkRenderingFlagsKHR                                  flags;
2232                 renderArea,                                                                                                                     // VkRect2D                                                             renderArea;
2233                 1u,                                                                                                                                     // deUint32                                                             layerCount;
2234                 0u,                                                                                                                                     // deUint32                                                             viewMask;
2235                 static_cast<deUint32>(colorAttachmentVect.size()),                                      // deUint32                                                             colorAttachmentCount;
2236                 colorAttachmentVect.empty() ? DE_NULL : &colorAttachmentVect[0],        // const VkRenderingAttachmentInfoKHR*  pColorAttachments;
2237                 pDepthAttachment,                                                                                                       // const VkRenderingAttachmentInfoKHR*  pDepthAttachment;
2238                 pStencilAttachment                                                                                                      // const VkRenderingAttachmentInfoKHR*  pStencilAttachment;
2239         };
2240
2241         vk.cmdBeginRendering(commandBuffer, &renderingInfo);
2242 }
2243
2244 void endDynamicRendering(const DeviceInterface& vk, VkCommandBuffer commandBuffer)
2245 {
2246         vk.cmdEndRendering(commandBuffer);
2247 }
2248 #endif // CTS_USES_VULKANSC
2249
2250 class SubpassRenderer
2251 {
2252 public:
2253         SubpassRenderer (Context&                                                                                               context,
2254                                          const DeviceInterface&                                                                 vk,
2255                                          VkDevice                                                                                               device,
2256                                          Allocator&                                                                                             allocator,
2257                                          const RenderPass&                                                                              renderPassInfo,
2258                                          const vector<de::SharedPtr<AttachmentResources> >&             attachmentResources,
2259                                          const VkRect2D&                                                                                renderArea,
2260                                          const vector<Maybe<VkClearValue> >&                                    renderPassClearValues,
2261                                          VkRenderPass                                                                                   renderPass,
2262                                          VkFramebuffer                                                                                  framebuffer,
2263                                          VkCommandPool                                                                                  commandBufferPool,
2264                                          deUint32                                                                                               queueFamilyIndex,
2265                                          const vector<VkImage>&                                                                 attachmentImages,
2266                                          const vector<pair<VkImageView, VkImageView> >&                 attachmentViews,
2267                                          const SubpassRenderInfo&                                                               renderInfo,
2268                                          const AllocationKind                                                                   allocationKind,
2269                                          const bool                                                                                             dynamicRendering,
2270                                          const bool                                                                                             secondaryCmdBufferCompletelyContainsDynamicRenderpass)
2271                 : m_renderInfo  (renderInfo)
2272         {
2273                 // unreference values not used by Vulkan SC, no need to pu this under ifdef
2274                 DE_UNREF(attachmentResources);
2275                 DE_UNREF(renderArea);
2276                 DE_UNREF(renderPassClearValues);
2277
2278                 const InstanceInterface&                                vki                             = context.getInstanceInterface();
2279                 const VkPhysicalDevice&                                 physDevice              = context.getPhysicalDevice();
2280                 const vector<Attachment>&                               attachmentInfos = renderPassInfo.getAttachments();
2281                 const deUint32                                                  subpassIndex    = renderInfo.getSubpassIndex();
2282                 vector<VkDescriptorSetLayoutBinding>    bindings;
2283
2284                 for (deUint32 colorAttachmentNdx = 0; colorAttachmentNdx < renderInfo.getColorAttachmentCount();  colorAttachmentNdx++)
2285                 {
2286                         const deUint32 attachmentNdx    = (renderInfo.getColorAttachmentIndex(colorAttachmentNdx) == VK_ATTACHMENT_UNUSED) ? colorAttachmentNdx
2287                                                                                         : renderInfo.getColorAttachmentIndex(colorAttachmentNdx);
2288
2289                         m_colorAttachmentImages.push_back(attachmentImages[attachmentNdx]);
2290                 }
2291
2292                 if (renderInfo.getDepthStencilAttachmentIndex())
2293                         m_depthStencilAttachmentImage = attachmentImages[*renderInfo.getDepthStencilAttachmentIndex()];
2294
2295                 if (renderInfo.getRenderQuad())
2296                 {
2297                         const RenderQuad&       renderQuad      = *renderInfo.getRenderQuad();
2298
2299                         if (renderInfo.getInputAttachmentCount() > 0)
2300                         {
2301                                 deUint32                                                                bindingIndex    = 0;
2302
2303                                 for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2304                                 {
2305                                         const Attachment                        attachmentInfo  = attachmentInfos[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)];
2306                                         const VkImageLayout                     layout                  = renderInfo.getInputAttachmentLayout(inputAttachmentNdx);
2307                                         const tcu::TextureFormat        format                  = mapVkFormat(attachmentInfo.getFormat());
2308                                         const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
2309                                         const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
2310                                         const deUint32                          bindingCount    = (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2311                                                                                                                                         && (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
2312                                                                                                                                 ? 2u
2313                                                                                                                                 : 1u;
2314
2315                                         for (deUint32 bindingNdx = 0; bindingNdx < bindingCount; bindingNdx++)
2316                                         {
2317                                                 const VkDescriptorSetLayoutBinding binding =
2318                                                 {
2319                                                         bindingIndex,
2320                                                         vk::VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2321                                                         1u,
2322                                                         vk::VK_SHADER_STAGE_FRAGMENT_BIT,
2323                                                         DE_NULL
2324                                                 };
2325
2326                                                 bindings.push_back(binding);
2327                                                 bindingIndex++;
2328                                         }
2329                                 }
2330
2331                                 const VkDescriptorSetLayoutCreateInfo createInfo =
2332                                 {
2333                                         vk::VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
2334                                         DE_NULL,
2335
2336                                         0u,
2337                                         (deUint32)bindings.size(),
2338                                         &bindings[0]
2339                                 };
2340
2341                                 m_descriptorSetLayout = vk::createDescriptorSetLayout(vk, device, &createInfo);
2342                         }
2343
2344                         const VkDescriptorSetLayout                     descriptorSetLayout             = *m_descriptorSetLayout;
2345                         const VkPipelineLayoutCreateInfo        pipelineLayoutParams    =
2346                         {
2347                                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,                  // sType;
2348                                 DE_NULL,                                                                                                // pNext;
2349                                 (vk::VkPipelineLayoutCreateFlags)0,
2350                                 m_descriptorSetLayout ? 1u :0u ,                                                // setLayoutCount;
2351                                 m_descriptorSetLayout ? &descriptorSetLayout : DE_NULL, // pSetLayouts;
2352                                 0u,                                                                                                             // pushConstantRangeCount;
2353                                 DE_NULL,                                                                                                // pPushConstantRanges;
2354                         };
2355
2356                         m_vertexShaderModule    = createShaderModule(vk, device, context.getBinaryCollection().get(de::toString(subpassIndex) + "-vert"), 0u);
2357                         m_fragmentShaderModule  = createShaderModule(vk, device, context.getBinaryCollection().get(de::toString(subpassIndex) + "-frag"), 0u);
2358                         m_pipelineLayout                = createPipelineLayout(vk, device, &pipelineLayoutParams);
2359                         m_pipeline                              = createSubpassPipeline(vk, device, renderPass, *m_vertexShaderModule, *m_fragmentShaderModule, *m_pipelineLayout, m_renderInfo);
2360
2361                         // Round up the vertex buffer size to honor nonCoherentAtomSize.
2362                         const auto      properties                      = vk::getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice());
2363                         const auto      vertexBufferSize        = de::roundUp(static_cast<VkDeviceSize>(renderQuad.getVertexDataSize()), properties.limits.nonCoherentAtomSize);
2364
2365                         m_vertexBuffer                  = createBuffer(vk, device, 0u, vertexBufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE, 1u, &queueFamilyIndex);
2366                         m_vertexBufferMemory    = allocateBuffer(vki, vk, physDevice, device, *m_vertexBuffer, MemoryRequirement::HostVisible, allocator, allocationKind);
2367
2368                         bindBufferMemory(vk, device, *m_vertexBuffer, m_vertexBufferMemory->getMemory(), m_vertexBufferMemory->getOffset());
2369
2370                         uploadBufferData(vk, device, *m_vertexBufferMemory, renderQuad.getVertexDataSize(), renderQuad.getVertexPointer(), properties.limits.nonCoherentAtomSize);
2371
2372                         if (renderInfo.getInputAttachmentCount() > 0)
2373                         {
2374                                 {
2375                                         const VkDescriptorPoolSize poolSize =
2376                                         {
2377                                                 vk::VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2378                                                 // \note Reserve 2 per input attachment since depthStencil attachments require 2.
2379                                                 renderInfo.getInputAttachmentCount() * 2u
2380                                         };
2381                                         const VkDescriptorPoolCreateInfo createInfo =
2382                                         {
2383                                                 vk::VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
2384                                                 DE_NULL,
2385                                                 VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
2386
2387                                                 // \note Reserve 2 per input attachment since depthStencil attachments require 2.
2388                                                 renderInfo.getInputAttachmentCount() * 2u,
2389                                                 1u,
2390                                                 &poolSize
2391                                         };
2392
2393                                         m_descriptorPool = vk::createDescriptorPool(vk, device, &createInfo);
2394                                 }
2395                                 {
2396                                         const VkDescriptorSetAllocateInfo       allocateInfo =
2397                                         {
2398                                                 vk::VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2399                                                 DE_NULL,
2400
2401                                                 *m_descriptorPool,
2402                                                 1u,
2403                                                 &descriptorSetLayout
2404                                         };
2405
2406                                         m_descriptorSet = vk::allocateDescriptorSet(vk, device, &allocateInfo);
2407                                 }
2408                                 {
2409                                         vector<VkWriteDescriptorSet>    writes                  (bindings.size());
2410                                         vector<VkDescriptorImageInfo>   imageInfos              (bindings.size());
2411                                         deUint32                                                bindingIndex    = 0;
2412
2413                                         for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2414                                         {
2415                                                 const Attachment                        attachmentInfo                  = attachmentInfos[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)];
2416                                                 const tcu::TextureFormat        format                                  = mapVkFormat(attachmentInfo.getFormat());
2417                                                 const bool                                      isDepthFormat                   = tcu::hasDepthComponent(format.order);
2418                                                 const bool                                      isStencilFormat                 = tcu::hasStencilComponent(format.order);
2419                                                 const VkImageLayout                     inputAttachmentLayout   = renderInfo.getInputAttachmentLayout(inputAttachmentNdx);
2420
2421
2422                                                 if (isDepthFormat && isStencilFormat)
2423                                                 {
2424                                                         if (inputAttachmentLayout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
2425                                                         {
2426                                                                 const VkDescriptorImageInfo     imageInfo =
2427                                                                 {
2428                                                                         (VkSampler)0,
2429                                                                         attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].first,
2430                                                                         inputAttachmentLayout
2431                                                                 };
2432                                                                 imageInfos[bindingIndex] = imageInfo;
2433
2434                                                                 {
2435                                                                         const VkWriteDescriptorSet      write =
2436                                                                         {
2437                                                                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2438                                                                                 DE_NULL,
2439
2440                                                                                 *m_descriptorSet,
2441                                                                                 bindingIndex,
2442                                                                                 0u,
2443                                                                                 1u,
2444                                                                                 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2445                                                                                 &imageInfos[bindingIndex],
2446                                                                                 DE_NULL,
2447                                                                                 DE_NULL
2448                                                                         };
2449                                                                         writes[bindingIndex] = write;
2450
2451                                                                         bindingIndex++;
2452                                                                 }
2453                                                         }
2454
2455                                                         if (inputAttachmentLayout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2456                                                         {
2457                                                                 const VkDescriptorImageInfo     imageInfo =
2458                                                                 {
2459                                                                         (VkSampler)0,
2460                                                                         attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].second,
2461                                                                         inputAttachmentLayout
2462                                                                 };
2463                                                                 imageInfos[bindingIndex] = imageInfo;
2464
2465                                                                 {
2466                                                                         const VkWriteDescriptorSet      write =
2467                                                                         {
2468                                                                                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2469                                                                                 DE_NULL,
2470
2471                                                                                 *m_descriptorSet,
2472                                                                                 bindingIndex,
2473                                                                                 0u,
2474                                                                                 1u,
2475                                                                                 VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2476                                                                                 &imageInfos[bindingIndex],
2477                                                                                 DE_NULL,
2478                                                                                 DE_NULL
2479                                                                         };
2480                                                                         writes[bindingIndex] = write;
2481
2482                                                                         bindingIndex++;
2483                                                                 }
2484                                                         }
2485                                                 }
2486                                                 else
2487                                                 {
2488                                                         const VkDescriptorImageInfo     imageInfo =
2489                                                         {
2490                                                                 (VkSampler)0,
2491                                                                 attachmentViews[renderInfo.getInputAttachmentIndex(inputAttachmentNdx)].first,
2492                                                                 inputAttachmentLayout
2493                                                         };
2494                                                         imageInfos[bindingIndex] = imageInfo;
2495
2496                                                         {
2497                                                                 const VkWriteDescriptorSet      write =
2498                                                                 {
2499                                                                         VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
2500                                                                         DE_NULL,
2501
2502                                                                         *m_descriptorSet,
2503                                                                         bindingIndex,
2504                                                                         0u,
2505                                                                         1u,
2506                                                                         VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
2507                                                                         &imageInfos[bindingIndex],
2508                                                                         DE_NULL,
2509                                                                         DE_NULL
2510                                                                 };
2511                                                                 writes[bindingIndex] = write;
2512
2513                                                                 bindingIndex++;
2514                                                         }
2515                                                 }
2516                                         }
2517
2518                                         vk.updateDescriptorSets(device, (deUint32)writes.size(), &writes[0], 0u, DE_NULL);
2519                                 }
2520                         }
2521                 }
2522
2523                 if (renderInfo.isSecondary())
2524                 {
2525                         m_commandBuffer = allocateCommandBuffer(vk, device, commandBufferPool, VK_COMMAND_BUFFER_LEVEL_SECONDARY);
2526
2527                         beginCommandBuffer(vk, *m_commandBuffer, renderPass, subpassIndex, framebuffer, VK_FALSE, (VkQueryControlFlags)0,
2528                                                            (VkQueryPipelineStatisticFlags)0, &renderInfo, dynamicRendering, secondaryCmdBufferCompletelyContainsDynamicRenderpass);
2529
2530                         if (dynamicRendering && secondaryCmdBufferCompletelyContainsDynamicRenderpass)
2531                         {
2532 #ifndef CTS_USES_VULKANSC
2533                                 beginDynamicRendering(vk, *m_commandBuffer, renderPassInfo, attachmentResources, renderArea, renderPassClearValues);
2534                                 pushRenderCommands(vk, *m_commandBuffer);
2535                                 endDynamicRendering(vk, *m_commandBuffer);
2536 #endif // CTS_USES_VULKANSC
2537                         }
2538                         else
2539                                 pushRenderCommands(vk, *m_commandBuffer);
2540
2541                         endCommandBuffer(vk, *m_commandBuffer);
2542                 }
2543         }
2544
2545         bool isSecondary (void) const
2546         {
2547                 return !!m_commandBuffer;
2548         }
2549
2550         VkCommandBuffer getCommandBuffer (void) const
2551         {
2552                 DE_ASSERT(isSecondary());
2553                 return *m_commandBuffer;
2554         }
2555
2556         void pushRenderCommands (const DeviceInterface&         vk,
2557                                                          VkCommandBuffer                        commandBuffer)
2558         {
2559                 if (!m_renderInfo.getColorClears().empty())
2560                 {
2561                         const vector<ColorClear>&       colorClears     (m_renderInfo.getColorClears());
2562
2563                         for (deUint32 attachmentNdx = 0; attachmentNdx < m_renderInfo.getColorAttachmentCount(); attachmentNdx++)
2564                         {
2565                                 const ColorClear&               colorClear      = colorClears[attachmentNdx];
2566                                 const VkClearAttachment attachment      =
2567                                 {
2568                                         VK_IMAGE_ASPECT_COLOR_BIT,
2569                                         attachmentNdx,
2570                                         makeClearValue(colorClear.getColor()),
2571                                 };
2572                                 const VkClearRect               rect            =
2573                                 {
2574                                         {
2575                                                 { (deInt32)colorClear.getOffset().x(),  (deInt32)colorClear.getOffset().y()     },
2576                                                 { colorClear.getSize().x(),                             colorClear.getSize().y()                        }
2577                                         },                                      // rect
2578                                         0u,                                     // baseArrayLayer
2579                                         1u,                                     // layerCount
2580                                 };
2581
2582                                 vk.cmdClearAttachments(commandBuffer, 1u, &attachment, 1u, &rect);
2583                         }
2584                 }
2585
2586                 if (m_renderInfo.getDepthStencilClear())
2587                 {
2588                         const DepthStencilClear&        depthStencilClear       = *m_renderInfo.getDepthStencilClear();
2589                         const deUint32                          attachmentNdx           = m_renderInfo.getColorAttachmentCount();
2590                         tcu::TextureFormat                      format                          = mapVkFormat(m_renderInfo.getDepthStencilAttachment()->getFormat());
2591                         const VkImageLayout                     layout                          = *m_renderInfo.getDepthStencilAttachmentLayout();
2592                         const VkClearAttachment         attachment                      =
2593                         {
2594                                 (VkImageAspectFlags)((hasDepthComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ? VK_IMAGE_ASPECT_DEPTH_BIT : 0)
2595                                         | (hasStencilComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ? VK_IMAGE_ASPECT_STENCIL_BIT : 0)),
2596                                 attachmentNdx,
2597                                 makeClearValueDepthStencil(depthStencilClear.getDepth(), depthStencilClear.getStencil())
2598                         };
2599                         const VkClearRect                               rect                            =
2600                         {
2601                                 {
2602                                         { (deInt32)depthStencilClear.getOffset().x(),   (deInt32)depthStencilClear.getOffset().y()      },
2603                                         { depthStencilClear.getSize().x(),                              depthStencilClear.getSize().y()                         }
2604                                 },                                                      // rect
2605                                 0u,                                                     // baseArrayLayer
2606                                 1u,                                                     // layerCount
2607                         };
2608
2609                         if ((tcu::hasDepthComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
2610                                 || (tcu::hasStencilComponent(format.order) && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL))
2611                         {
2612                                 vk.cmdClearAttachments(commandBuffer, 1u, &attachment, 1u, &rect);
2613                         }
2614                 }
2615
2616                 vector<VkImageMemoryBarrier>    selfDeps;
2617                 VkPipelineStageFlags                    srcStages = 0;
2618                 VkPipelineStageFlags                    dstStages = 0;
2619
2620                 for (deUint32 inputAttachmentNdx = 0; inputAttachmentNdx < m_renderInfo.getInputAttachmentCount(); inputAttachmentNdx++)
2621                 {
2622                         for (deUint32 colorAttachmentNdx = 0; colorAttachmentNdx < m_renderInfo.getColorAttachmentCount(); colorAttachmentNdx++)
2623                         {
2624                                 if (m_renderInfo.getInputAttachmentIndex(inputAttachmentNdx) == m_renderInfo.getColorAttachmentIndex(colorAttachmentNdx))
2625                                 {
2626                                         const VkImageMemoryBarrier      barrier   =
2627                                         {
2628                                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                 // sType
2629                                                 DE_NULL,                                                                                // pNext
2630
2631                                                 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                   // srcAccessMask
2632                                                 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,                    // dstAccessMask
2633
2634                                                 VK_IMAGE_LAYOUT_GENERAL,                                                // oldLayout
2635                                                 VK_IMAGE_LAYOUT_GENERAL,                                                // newLayout
2636
2637                                                 VK_QUEUE_FAMILY_IGNORED,                                                // srcQueueFamilyIndex
2638                                                 VK_QUEUE_FAMILY_IGNORED,                                                // destQueueFamilyIndex
2639
2640                                                 m_colorAttachmentImages[colorAttachmentNdx],    // image
2641                                                 {                                                                                               // subresourceRange
2642                                                         VK_IMAGE_ASPECT_COLOR_BIT,                                              // aspect
2643                                                         0,                                                                                              // baseMipLevel
2644                                                         1,                                                                                              // mipLevels
2645                                                         0,                                                                                              // baseArraySlice
2646                                                         1                                                                                               // arraySize
2647                                                 }
2648                                         };
2649
2650                                         srcStages |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2651                                         dstStages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2652
2653                                         selfDeps.push_back(barrier);
2654                                 }
2655                         }
2656
2657                         if (m_renderInfo.getDepthStencilAttachmentIndex() && (m_renderInfo.getInputAttachmentIndex(inputAttachmentNdx) == *m_renderInfo.getDepthStencilAttachmentIndex()))
2658                         {
2659                                 const tcu::TextureFormat        format          = mapVkFormat(m_renderInfo.getDepthStencilAttachment()->getFormat());
2660                                 const bool                                      hasDepth        = hasDepthComponent(format.order);
2661                                 const bool                                      hasStencil      = hasStencilComponent(format.order);
2662                                 const VkImageMemoryBarrier      barrier         =
2663                                 {
2664                                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                 // sType;
2665                                         DE_NULL,                                                                                                // pNext;
2666
2667                                         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,                   // srcAccessMask
2668                                         VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,                                    // dstAccessMask
2669
2670                                         m_renderInfo.getInputAttachmentLayout(inputAttachmentNdx),      // oldLayout
2671                                         m_renderInfo.getInputAttachmentLayout(inputAttachmentNdx),      // newLayout;
2672
2673                                         VK_QUEUE_FAMILY_IGNORED,                                                                // srcQueueFamilyIndex;
2674                                         VK_QUEUE_FAMILY_IGNORED,                                                                // destQueueFamilyIndex;
2675
2676                                         m_depthStencilAttachmentImage,                                                  // image;
2677                                         {                                                                                                               // subresourceRange;
2678                                                 (hasDepth ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
2679                                                         | (hasStencil ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u),  // aspect;
2680                                                 0,                                                                                                                      // baseMipLevel;
2681                                                 1,                                                                                                                      // mipLevels;
2682                                                 0,                                                                                                                      // baseArraySlice;
2683                                                 1                                                                                                                       // arraySize;
2684                                         }
2685                                 };
2686
2687                                 srcStages |= VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
2688                                 dstStages |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
2689
2690                                 selfDeps.push_back(barrier);
2691                         }
2692                 }
2693
2694                 if (!selfDeps.empty())
2695                 {
2696                         DE_ASSERT(srcStages != 0);
2697                         DE_ASSERT(dstStages != 0);
2698                         vk.cmdPipelineBarrier(commandBuffer, srcStages, dstStages, VK_DEPENDENCY_BY_REGION_BIT, 0, DE_NULL, 0, DE_NULL, (deUint32)selfDeps.size(), &selfDeps[0]);
2699                 }
2700
2701                 if (m_renderInfo.getRenderQuad())
2702                 {
2703                         const VkDeviceSize      offset                  = 0;
2704                         const VkBuffer          vertexBuffer    = *m_vertexBuffer;
2705
2706                         vk.cmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
2707
2708                         if (m_descriptorSet)
2709                         {
2710                                 const VkDescriptorSet descriptorSet = *m_descriptorSet;
2711                                 vk.cmdBindDescriptorSets(commandBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipelineLayout, 0u, 1u, &descriptorSet, 0u, NULL);
2712                         }
2713
2714                         vk.cmdBindVertexBuffers(commandBuffer, 0u, 1u, &vertexBuffer, &offset);
2715                         vk.cmdDraw(commandBuffer, 6u, 1u, 0u, 0u);
2716                 }
2717         }
2718
2719 private:
2720         const SubpassRenderInfo         m_renderInfo;
2721         Move<VkCommandBuffer>           m_commandBuffer;
2722         Move<VkPipeline>                        m_pipeline;
2723         Move<VkDescriptorSetLayout>     m_descriptorSetLayout;
2724         Move<VkPipelineLayout>          m_pipelineLayout;
2725
2726         Move<VkShaderModule>            m_vertexShaderModule;
2727         Move<VkShaderModule>            m_fragmentShaderModule;
2728
2729         Move<VkDescriptorPool>          m_descriptorPool;
2730         Move<VkDescriptorSet>           m_descriptorSet;
2731         Move<VkBuffer>                          m_vertexBuffer;
2732         de::MovePtr<Allocation>         m_vertexBufferMemory;
2733         vector<VkImage>                         m_colorAttachmentImages;
2734         VkImage                                         m_depthStencilAttachmentImage;
2735 };
2736
2737 void pushImageInitializationCommands (const DeviceInterface&                                                            vk,
2738                                                                           VkCommandBuffer                                                                               commandBuffer,
2739                                                                           const vector<Attachment>&                                                             attachmentInfo,
2740                                                                           const vector<de::SharedPtr<AttachmentResources> >&    attachmentResources,
2741                                                                           deUint32                                                                                              queueIndex,
2742                                                                           const vector<Maybe<VkClearValue> >&                                   clearValues)
2743 {
2744         {
2745                 vector<VkImageMemoryBarrier>    initializeLayouts;
2746
2747                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2748                 {
2749                         if (!clearValues[attachmentNdx])
2750                                 continue;
2751
2752                         const VkImageMemoryBarrier barrier =
2753                         {
2754                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                 // sType;
2755                                 DE_NULL,                                                                                                                // pNext;
2756
2757                                 (VkAccessFlags)0,                                                                                               // srcAccessMask
2758                                 getAllMemoryReadFlags() | VK_ACCESS_TRANSFER_WRITE_BIT,                 // dstAccessMask
2759
2760                                 VK_IMAGE_LAYOUT_UNDEFINED,                                                                              // oldLayout
2761                                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,                                                   // newLayout;
2762
2763                                 queueIndex,                                                                                                             // srcQueueFamilyIndex;
2764                                 queueIndex,                                                                                                             // destQueueFamilyIndex;
2765
2766                                 attachmentResources[attachmentNdx]->getImage(),                                 // image;
2767                                 {                                                                                                                               // subresourceRange;
2768                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
2769                                         0,                                                                                                                                      // baseMipLevel;
2770                                         1,                                                                                                                                      // mipLevels;
2771                                         0,                                                                                                                                      // baseArraySlice;
2772                                         1                                                                                                                                       // arraySize;
2773                                 }
2774                         };
2775
2776                         initializeLayouts.push_back(barrier);
2777                 }
2778
2779                 if (!initializeLayouts.empty())
2780                         vk.cmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
2781                                                                   VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0,
2782                                                                   0, (const VkMemoryBarrier*)DE_NULL,
2783                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
2784                                                                   (deUint32)initializeLayouts.size(), &initializeLayouts[0]);
2785         }
2786
2787         for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2788         {
2789                 if (!clearValues[attachmentNdx])
2790                         continue;
2791
2792                 const tcu::TextureFormat format = mapVkFormat(attachmentInfo[attachmentNdx].getFormat());
2793
2794                 if (hasStencilComponent(format.order) || hasDepthComponent(format.order))
2795                 {
2796                         const float                                             clearNan                = tcu::Float32::nan().asFloat();
2797                         const float                                             clearDepth              = hasDepthComponent(format.order) ? clearValues[attachmentNdx]->depthStencil.depth : clearNan;
2798                         const deUint32                                  clearStencil    = hasStencilComponent(format.order) ? clearValues[attachmentNdx]->depthStencil.stencil : 0xDEu;
2799                         const VkClearDepthStencilValue  depthStencil    =
2800                         {
2801                                 clearDepth,
2802                                 clearStencil
2803                         };
2804                         const VkImageSubresourceRange range =
2805                         {
2806                                 (VkImageAspectFlags)((hasDepthComponent(format.order) ? VK_IMAGE_ASPECT_DEPTH_BIT : 0)
2807                                                                          | (hasStencilComponent(format.order) ? VK_IMAGE_ASPECT_STENCIL_BIT : 0)),
2808                                 0,
2809                                 1,
2810                                 0,
2811                                 1
2812                         };
2813
2814                         vk.cmdClearDepthStencilImage(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depthStencil, 1, &range);
2815                 }
2816                 else
2817                 {
2818                         const VkImageSubresourceRange   range           =
2819                         {
2820                                 VK_IMAGE_ASPECT_COLOR_BIT,      // aspectMask;
2821                                 0,                                                      // baseMipLevel;
2822                                 1,                                                      // mipLevels;
2823                                 0,                                                      // baseArrayLayer;
2824                                 1                                                       // layerCount;
2825                         };
2826                         const VkClearColorValue                 clearColor      = clearValues[attachmentNdx]->color;
2827
2828                         vk.cmdClearColorImage(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearColor, 1, &range);
2829                 }
2830         }
2831
2832         {
2833                 vector<VkImageMemoryBarrier>    renderPassLayouts;
2834
2835                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
2836                 {
2837                         const VkImageLayout                     oldLayout       = clearValues[attachmentNdx] ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED;
2838                         const VkImageMemoryBarrier      barrier         =
2839                         {
2840                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                 // sType;
2841                                 DE_NULL,                                                                                                // pNext;
2842
2843                                 getMemoryFlagsForLayout(oldLayout),                                                                                                                                             // srcAccessMask
2844                                 getAllMemoryReadFlags() | getMemoryFlagsForLayout(attachmentInfo[attachmentNdx].getInitialLayout()),    // dstAccessMask
2845
2846                                 oldLayout,                                                                                              // oldLayout
2847                                 attachmentInfo[attachmentNdx].getInitialLayout(),               // newLayout;
2848
2849                                 queueIndex,                                                                                             // srcQueueFamilyIndex;
2850                                 queueIndex,                                                                                             // destQueueFamilyIndex;
2851
2852                                 attachmentResources[attachmentNdx]->getImage(),                 // image;
2853                                 {                                                                                                               // subresourceRange;
2854                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
2855                                         0,                                                                                                                                      // baseMipLevel;
2856                                         1,                                                                                                                                      // mipLevels;
2857                                         0,                                                                                                                                      // baseArraySlice;
2858                                         1                                                                                                                                       // arraySize;
2859                                 }
2860                         };
2861
2862                         renderPassLayouts.push_back(barrier);
2863                 }
2864
2865                 if (!renderPassLayouts.empty())
2866                         vk.cmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
2867                                                                   VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0,
2868                                                                   0, (const VkMemoryBarrier*)DE_NULL,
2869                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
2870                                                                   (deUint32)renderPassLayouts.size(), &renderPassLayouts[0]);
2871         }
2872 }
2873
2874 template<typename RenderpassSubpass>
2875 void pushRenderPassCommands (const DeviceInterface&                                                     vk,
2876                                                          VkCommandBuffer                                                                commandBuffer,
2877                                                          VkRenderPass                                                                   renderPass,
2878                                                          VkFramebuffer                                                                  framebuffer,
2879                                                          const vector<de::SharedPtr<SubpassRenderer> >& subpassRenderers,
2880                                                          const VkRect2D&                                                                renderArea,
2881                                                          const vector<Maybe<VkClearValue> >&                    renderPassClearValues,
2882                                                          TestConfig::RenderTypes                                                render)
2883 {
2884         const float                                                                                     clearNan                                = tcu::Float32::nan().asFloat();
2885         vector<VkClearValue>                                                            attachmentClearValues;
2886         const typename RenderpassSubpass::SubpassEndInfo        subpassEndInfo                  (DE_NULL);
2887
2888         for (size_t attachmentNdx = 0; attachmentNdx < renderPassClearValues.size(); attachmentNdx++)
2889         {
2890                 if (renderPassClearValues[attachmentNdx])
2891                         attachmentClearValues.push_back(*renderPassClearValues[attachmentNdx]);
2892                 else
2893                         attachmentClearValues.push_back(makeClearValueColorF32(clearNan, clearNan, clearNan, clearNan));
2894         }
2895
2896         {
2897                 for (size_t subpassNdx = 0; subpassNdx < subpassRenderers.size(); subpassNdx++)
2898                 {
2899                         const VkSubpassContents                                                         contents                        = subpassRenderers[subpassNdx]->isSecondary() ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS : VK_SUBPASS_CONTENTS_INLINE;
2900                         const typename RenderpassSubpass::SubpassBeginInfo      subpassBeginInfo        (DE_NULL, contents);
2901                         const VkRenderPassBeginInfo                                                     renderPassBeginInfo     = createRenderPassBeginInfo(renderPass,
2902                                                                                                                                                                                                                                 framebuffer,
2903                                                                                                                                                                                                                                 renderArea,
2904                                                                                                                                                                                                                                 (deUint32)attachmentClearValues.size(),
2905                                                                                                                                                                                                                                 attachmentClearValues.empty() ? DE_NULL : &attachmentClearValues[0]);
2906
2907                         if (subpassNdx == 0)
2908                                 RenderpassSubpass::cmdBeginRenderPass(vk, commandBuffer, &renderPassBeginInfo, &subpassBeginInfo);
2909                         else
2910                                 RenderpassSubpass::cmdNextSubpass(vk, commandBuffer, &subpassBeginInfo, &subpassEndInfo);
2911
2912                         if (render)
2913                         {
2914                                 if (contents == VK_SUBPASS_CONTENTS_INLINE)
2915                                 {
2916                                         subpassRenderers[subpassNdx]->pushRenderCommands(vk, commandBuffer);
2917                                 }
2918                                 else if (contents == VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS)
2919                                 {
2920                                         const VkCommandBuffer cmd = subpassRenderers[subpassNdx]->getCommandBuffer();
2921                                         vk.cmdExecuteCommands(commandBuffer, 1, &cmd);
2922                                 }
2923                                 else
2924                                         DE_FATAL("Invalid contents");
2925                         }
2926                 }
2927
2928                 RenderpassSubpass::cmdEndRenderPass(vk, commandBuffer, &subpassEndInfo);
2929         }
2930 }
2931
2932 #ifndef CTS_USES_VULKANSC
2933 void pushDynamicRenderingCommands (const DeviceInterface&                                                               vk,
2934                                                                    VkCommandBuffer                                                                              commandBuffer,
2935                                                                    const RenderPass&                                                                    renderPassInfo,
2936                                                                    const vector<de::SharedPtr<AttachmentResources> >&   attachmentResources,
2937                                                                    const vector<de::SharedPtr<SubpassRenderer> >&               subpassRenderers,
2938                                                                    const VkRect2D&                                                                              renderArea,
2939                                                                    const vector<Maybe<VkClearValue> >&                                  renderPassClearValues,
2940                                                                    deUint32                                                                                             queueIndex,
2941                                                                    TestConfig::RenderTypes                                                              renderType,
2942                                                                    bool                                                                                                 secondaryCmdBufferCompletelyContainsDynamicRenderpass)
2943 {
2944         DE_ASSERT(subpassRenderers.size() == 1);
2945
2946         vector<VkImageMemoryBarrier>            imageBarriersBeforeRendering;
2947         vector<VkImageMemoryBarrier>            imageBarriersAfterRendering;
2948
2949         const Subpass&                                          subpassInfo                                     = renderPassInfo.getSubpasses()[0];
2950         const vector<AttachmentReference>&      colorAttachmentsInfo            = subpassInfo.getColorAttachments();
2951
2952         for (deUint32 i = 0 ; i < colorAttachmentsInfo.size() ; ++i)
2953         {
2954                 const AttachmentReference&              colorAttachmentReference        = colorAttachmentsInfo[i];
2955                 const deUint32                                  colorAttachmentIndex            = colorAttachmentReference.getAttachment();
2956                 const Attachment&                               colorAttachmentInfo                     = renderPassInfo.getAttachments()[colorAttachmentIndex];
2957
2958                 const VkImageLayout                             initialLayout                           = colorAttachmentInfo.getInitialLayout();
2959                 const VkImageLayout                             renderingLayout                         = colorAttachmentReference.getImageLayout();
2960                 const VkImageLayout                             finalLayout                                     = colorAttachmentInfo.getFinalLayout();
2961
2962                 const VkImageMemoryBarrier barrierBeforeRendering
2963                 {
2964                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2965                         DE_NULL,                                                                                                                        // pNext
2966
2967                         getAllMemoryWriteFlags() | getMemoryFlagsForLayout(initialLayout),      // srcAccessMask
2968                         getMemoryFlagsForLayout(renderingLayout),                                                       // dstAccessMask
2969
2970                         initialLayout,                                                                                                          // oldLayout
2971                         renderingLayout,                                                                                                        // newLayout
2972
2973                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2974                         queueIndex,                                                                                                                     // destQueueFamilyIndex
2975
2976                         attachmentResources[colorAttachmentIndex]->getImage(),                          // image
2977                         {                                                                                                                                       // subresourceRange
2978                                 getImageAspectFlags(colorAttachmentInfo.getFormat()),                   // aspect;
2979                                 0,                                                                                                                              // baseMipLevel
2980                                 1,                                                                                                                              // mipLevels
2981                                 0,                                                                                                                              // baseArraySlice
2982                                 1                                                                                                                               // arraySize
2983                         }
2984                 };
2985                 imageBarriersBeforeRendering.push_back(barrierBeforeRendering);
2986
2987                 const VkImageMemoryBarrier barrierAfterRendering
2988                 {
2989                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
2990                         DE_NULL,                                                                                                                        // pNext
2991
2992                         getMemoryFlagsForLayout(renderingLayout),                                                       // srcAccessMask
2993                         getAllMemoryReadFlags() | getMemoryFlagsForLayout(finalLayout),         // dstAccessMask
2994
2995                         renderingLayout,                                                                                                        // oldLayout
2996                         finalLayout,                                                                                                            // newLayout
2997
2998                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
2999                         queueIndex,                                                                                                                     // destQueueFamilyIndex
3000
3001                         attachmentResources[colorAttachmentIndex]->getImage(),                          // image
3002                         {                                                                                                                                       // subresourceRange
3003                                 getImageAspectFlags(colorAttachmentInfo.getFormat()),                   // aspect;
3004                                 0,                                                                                                                              // baseMipLevel
3005                                 1,                                                                                                                              // mipLevels
3006                                 0,                                                                                                                              // baseArraySlice
3007                                 1                                                                                                                               // arraySize
3008                         }
3009                 };
3010                 imageBarriersAfterRendering.push_back(barrierAfterRendering);
3011         }
3012
3013         const AttachmentReference&              depthStencilAttachmentReference = subpassInfo.getDepthStencilAttachment();
3014         const deUint32                                  dsAttachmentIndex                               = depthStencilAttachmentReference.getAttachment();
3015
3016         if (dsAttachmentIndex != VK_ATTACHMENT_UNUSED)
3017         {
3018                 const Attachment&                       dsAttachmentInfo        = renderPassInfo.getAttachments()[dsAttachmentIndex];
3019
3020                 const VkImageLayout initialLayout               = dsAttachmentInfo.getInitialLayout();
3021                 const VkImageLayout renderingLayout             = depthStencilAttachmentReference.getImageLayout();
3022                 const VkImageLayout finalLayout                 = dsAttachmentInfo.getFinalLayout();
3023
3024                 const VkImageMemoryBarrier barrierBeforeRendering
3025                 {
3026                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
3027                         DE_NULL,                                                                                                                        // pNext
3028
3029                         getAllMemoryWriteFlags() | getMemoryFlagsForLayout(initialLayout),      // srcAccessMask
3030                         getMemoryFlagsForLayout(renderingLayout),                                                       // dstAccessMask
3031
3032                         initialLayout,                                                                                                          // oldLayout
3033                         renderingLayout,                                                                                                        // newLayout
3034
3035                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
3036                         queueIndex,                                                                                                                     // destQueueFamilyIndex
3037
3038                         attachmentResources[dsAttachmentIndex]->getImage(),                                     // image
3039                         {                                                                                                                                       // subresourceRange
3040                                 getImageAspectFlags(dsAttachmentInfo.getFormat()),                              // aspect;
3041                                 0,                                                                                                                              // baseMipLevel
3042                                 1,                                                                                                                              // mipLevels
3043                                 0,                                                                                                                              // baseArraySlice
3044                                 1                                                                                                                               // arraySize
3045                         }
3046                 };
3047                 imageBarriersBeforeRendering.push_back(barrierBeforeRendering);
3048
3049                 const VkImageMemoryBarrier barrierAfterRendering
3050                 {
3051                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                         // sType
3052                         DE_NULL,                                                                                                                        // pNext
3053
3054                         getMemoryFlagsForLayout(renderingLayout),                                                       // srcAccessMask
3055                         getAllMemoryReadFlags() | getMemoryFlagsForLayout(finalLayout),         // dstAccessMask
3056
3057                         renderingLayout,                                                                                                        // oldLayout
3058                         finalLayout,                                                                                                            // newLayout
3059
3060                         queueIndex,                                                                                                                     // srcQueueFamilyIndex
3061                         queueIndex,                                                                                                                     // destQueueFamilyIndex
3062
3063                         attachmentResources[dsAttachmentIndex]->getImage(),                                     // image
3064                         {                                                                                                                                       // subresourceRange
3065                                 getImageAspectFlags(dsAttachmentInfo.getFormat()),                              // aspect;
3066                                 0,                                                                                                                              // baseMipLevel
3067                                 1,                                                                                                                              // mipLevels
3068                                 0,                                                                                                                              // baseArraySlice
3069                                 1                                                                                                                               // arraySize
3070                         }
3071                 };
3072                 imageBarriersAfterRendering.push_back(barrierAfterRendering);
3073         }
3074
3075         if (!imageBarriersBeforeRendering.empty())
3076                 vk.cmdPipelineBarrier(commandBuffer,
3077                                                           getAllPipelineStageFlags(),
3078                                                           getAllPipelineStageFlags(),
3079                                                           (VkDependencyFlags)0,
3080                                                           0, (const VkMemoryBarrier*)DE_NULL,
3081                                                           0, (const VkBufferMemoryBarrier*)DE_NULL,
3082                                                           (deUint32)imageBarriersBeforeRendering.size(),
3083                                                           &imageBarriersBeforeRendering[0]);
3084
3085         bool executeRenderCommands = (renderType != TestConfig::RENDERTYPES_NONE);
3086
3087         if (secondaryCmdBufferCompletelyContainsDynamicRenderpass)
3088         {
3089                 // when secondary command buffer completely contains dynamic renderpass
3090                 // then we need to execute it even when render type is none
3091                 executeRenderCommands = true;
3092         }
3093         else
3094         {
3095                 VkRenderingFlagsKHR renderingFlags = 0u;
3096                 if (subpassRenderers[0]->isSecondary())
3097                         renderingFlags = VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR;
3098
3099                 beginDynamicRendering(vk, commandBuffer, renderPassInfo, attachmentResources, renderArea, renderPassClearValues, renderingFlags);
3100         }
3101
3102         if (executeRenderCommands)
3103         {
3104                 if (subpassRenderers[0]->isSecondary())
3105                 {
3106                         const VkCommandBuffer cmd = subpassRenderers[0]->getCommandBuffer();
3107                         vk.cmdExecuteCommands(commandBuffer, 1, &cmd);
3108                 }
3109                 else
3110                         subpassRenderers[0]->pushRenderCommands(vk, commandBuffer);
3111         }
3112
3113         if (!secondaryCmdBufferCompletelyContainsDynamicRenderpass)
3114                 endDynamicRendering(vk, commandBuffer);
3115
3116         if (!imageBarriersAfterRendering.empty())
3117                 vk.cmdPipelineBarrier(commandBuffer,
3118                                                           getAllPipelineStageFlags(),
3119                                                           getAllPipelineStageFlags(),
3120                                                           (VkDependencyFlags)0,
3121                                                           0, (const VkMemoryBarrier*)DE_NULL,
3122                                                           0, (const VkBufferMemoryBarrier*)DE_NULL,
3123                                                           (deUint32)imageBarriersAfterRendering.size(),
3124                                                           &imageBarriersAfterRendering[0]);
3125 }
3126 #endif // CTS_USES_VULKANSC
3127
3128 void pushRenderPassCommands (const DeviceInterface&                                                             vk,
3129                                                          VkCommandBuffer                                                                        commandBuffer,
3130                                                          VkRenderPass                                                                           renderPass,
3131                                                          const RenderPass&                                                                      renderPassInfo,
3132                                                          const vector<de::SharedPtr<AttachmentResources> >&     attachmentResources,
3133                                                          VkFramebuffer                                                                          framebuffer,
3134                                                          const vector<de::SharedPtr<SubpassRenderer> >&         subpassRenderers,
3135                                                          const VkRect2D&                                                                        renderArea,
3136                                                          const vector<Maybe<VkClearValue> >&                            renderPassClearValues,
3137                                                          deUint32                                                                                       queueIndex,
3138                                                          TestConfig::RenderTypes                                                        render,
3139                                                          RenderingType                                                                          renderingType,
3140                                                          bool                                                                                           secondaryCmdBufferCompletelyContainsDynamicRenderpass)
3141 {
3142         // unreference arguments not used by Vulkan SC, no need to put them under ifdef
3143         DE_UNREF(renderPassInfo);
3144         DE_UNREF(attachmentResources);
3145         DE_UNREF(queueIndex);
3146         DE_UNREF(secondaryCmdBufferCompletelyContainsDynamicRenderpass);
3147
3148         switch (renderingType)
3149         {
3150                 case RENDERING_TYPE_RENDERPASS_LEGACY:
3151                         return pushRenderPassCommands<RenderpassSubpass1>(vk, commandBuffer, renderPass, framebuffer, subpassRenderers, renderArea, renderPassClearValues, render);
3152                 case RENDERING_TYPE_RENDERPASS2:
3153                         return pushRenderPassCommands<RenderpassSubpass2>(vk, commandBuffer, renderPass, framebuffer, subpassRenderers, renderArea, renderPassClearValues, render);
3154
3155 #ifndef CTS_USES_VULKANSC
3156                 case RENDERING_TYPE_DYNAMIC_RENDERING:
3157                         return pushDynamicRenderingCommands(vk, commandBuffer, renderPassInfo, attachmentResources, subpassRenderers, renderArea, renderPassClearValues, queueIndex, render, secondaryCmdBufferCompletelyContainsDynamicRenderpass);
3158 #endif // CTS_USES_VULKANSC
3159
3160                 default:
3161                         TCU_THROW(InternalError, "Impossible");
3162         }
3163 }
3164
3165 void pushReadImagesToBuffers (const DeviceInterface&                                                            vk,
3166                                                           VkCommandBuffer                                                                               commandBuffer,
3167                                                           deUint32                                                                                              queueIndex,
3168
3169                                                           const vector<de::SharedPtr<AttachmentResources> >&    attachmentResources,
3170                                                           const vector<Attachment>&                                                             attachmentInfo,
3171                                                           const vector<bool>&                                                                   isLazy,
3172
3173                                                           const UVec2&                                                                                  targetSize)
3174 {
3175         {
3176                 vector<VkImageMemoryBarrier>    imageBarriers;
3177
3178                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3179                 {
3180                         if (isLazy[attachmentNdx])
3181                                 continue;
3182
3183                         const VkImageLayout                     oldLayout       = attachmentInfo[attachmentNdx].getFinalLayout();
3184                         const VkImageMemoryBarrier      barrier         =
3185                         {
3186                                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                                 // sType
3187                                 DE_NULL,                                                                                                                // pNext
3188
3189                                 getAllMemoryWriteFlags() | getMemoryFlagsForLayout(oldLayout),  // srcAccessMask
3190                                 getAllMemoryReadFlags(),                                                                                // dstAccessMask
3191
3192                                 oldLayout,                                                                                                              // oldLayout
3193                                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,                                                   // newLayout
3194
3195                                 queueIndex,                                                                                                             // srcQueueFamilyIndex
3196                                 queueIndex,                                                                                                             // destQueueFamilyIndex
3197
3198                                 attachmentResources[attachmentNdx]->getImage(),                                 // image
3199                                 {                                                                                                                               // subresourceRange
3200                                         getImageAspectFlags(attachmentInfo[attachmentNdx].getFormat()),         // aspect;
3201                                         0,                                                                                                                                      // baseMipLevel
3202                                         1,                                                                                                                                      // mipLevels
3203                                         0,                                                                                                                                      // baseArraySlice
3204                                         1                                                                                                                                       // arraySize
3205                                 }
3206                         };
3207
3208                         imageBarriers.push_back(barrier);
3209                 }
3210
3211                 if (!imageBarriers.empty())
3212                         vk.cmdPipelineBarrier(commandBuffer,
3213                                                                   getAllPipelineStageFlags(),
3214                                                                   getAllPipelineStageFlags(),
3215                                                                   (VkDependencyFlags)0,
3216                                                                   0, (const VkMemoryBarrier*)DE_NULL,
3217                                                                   0, (const VkBufferMemoryBarrier*)DE_NULL,
3218                                                                   (deUint32)imageBarriers.size(), &imageBarriers[0]);
3219         }
3220
3221         for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3222         {
3223                 if (isLazy[attachmentNdx])
3224                         continue;
3225
3226                 const tcu::TextureFormat::ChannelOrder  order   = mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order;
3227                 const VkBufferImageCopy                                 rect    =
3228                 {
3229                         0, // bufferOffset
3230                         0, // bufferRowLength
3231                         0, // bufferImageHeight
3232                         {                                                       // imageSubresource
3233                                 (vk::VkImageAspectFlags)getPrimaryImageAspect(mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order),    // aspect
3234                                 0,                                              // mipLevel
3235                                 0,                                              // arraySlice
3236                                 1                                               // arraySize
3237                         },
3238                         { 0, 0, 0 },                            // imageOffset
3239                         { targetSize.x(), targetSize.y(), 1u }          // imageExtent
3240                 };
3241
3242                 vk.cmdCopyImageToBuffer(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, attachmentResources[attachmentNdx]->getBuffer(), 1, &rect);
3243
3244                 if (tcu::TextureFormat::DS == order)
3245                 {
3246                         const VkBufferImageCopy stencilRect =
3247                         {
3248                                 0,                                                                              // bufferOffset
3249                                 0,                                                                              // bufferRowLength
3250                                 0,                                                                              // bufferImageHeight
3251                                 {                                                                       // imageSubresource
3252                                         VK_IMAGE_ASPECT_STENCIL_BIT,    // aspect
3253                                         0,                                                              // mipLevel
3254                                         0,                                                              // arraySlice
3255                                         1                                                               // arraySize
3256                                 },
3257                                 { 0, 0, 0 },                                                    // imageOffset
3258                                 { targetSize.x(), targetSize.y(), 1u }  // imageExtent
3259                         };
3260
3261                         vk.cmdCopyImageToBuffer(commandBuffer, attachmentResources[attachmentNdx]->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, attachmentResources[attachmentNdx]->getSecondaryBuffer(), 1, &stencilRect);
3262                 }
3263         }
3264
3265         {
3266                 vector<VkBufferMemoryBarrier>   bufferBarriers;
3267
3268                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentInfo.size(); attachmentNdx++)
3269                 {
3270                         if (isLazy[attachmentNdx])
3271                                 continue;
3272
3273                         const tcu::TextureFormat::ChannelOrder  order                   = mapVkFormat(attachmentInfo[attachmentNdx].getFormat()).order;
3274                         const VkBufferMemoryBarrier                             bufferBarrier   =
3275                         {
3276                                 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3277                                 DE_NULL,
3278
3279                                 getAllMemoryWriteFlags(),
3280                                 getAllMemoryReadFlags(),
3281
3282                                 queueIndex,
3283                                 queueIndex,
3284
3285                                 attachmentResources[attachmentNdx]->getBuffer(),
3286                                 0,
3287                                 attachmentResources[attachmentNdx]->getBufferSize()
3288                         };
3289
3290                         bufferBarriers.push_back(bufferBarrier);
3291
3292                         if (tcu::TextureFormat::DS == order)
3293                         {
3294                                 const VkBufferMemoryBarrier secondaryBufferBarrier =
3295                                 {
3296                                         VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
3297                                         DE_NULL,
3298
3299                                         getAllMemoryWriteFlags(),
3300                                         getAllMemoryReadFlags(),
3301
3302                                         queueIndex,
3303                                         queueIndex,
3304
3305                                         attachmentResources[attachmentNdx]->getSecondaryBuffer(),
3306                                         0,
3307                                         attachmentResources[attachmentNdx]->getSecondaryBufferSize()
3308                                 };
3309
3310                                 bufferBarriers.push_back(secondaryBufferBarrier);
3311                         }
3312                 }
3313
3314                 if (!bufferBarriers.empty())
3315                         vk.cmdPipelineBarrier(commandBuffer,
3316                                                                   getAllPipelineStageFlags(),
3317                                                                   getAllPipelineStageFlags(),
3318                                                                   (VkDependencyFlags)0,
3319                                                                   0, (const VkMemoryBarrier*)DE_NULL,
3320                                                                   (deUint32)bufferBarriers.size(), &bufferBarriers[0],
3321                                                                   0, (const VkImageMemoryBarrier*)DE_NULL);
3322         }
3323 }
3324
3325 class PixelValue
3326 {
3327 public:
3328                                 PixelValue              (const Maybe<bool>&     x = tcu::Nothing,
3329                                                                  const Maybe<bool>&     y = tcu::Nothing,
3330                                                                  const Maybe<bool>&     z = tcu::Nothing,
3331                                                                  const Maybe<bool>&     w = tcu::Nothing);
3332
3333         void            setUndefined    (size_t ndx);
3334         void            setValue                (size_t ndx, bool value);
3335         Maybe<bool>     getValue                (size_t ndx) const;
3336
3337 private:
3338         deUint16        m_status;
3339 };
3340
3341 PixelValue::PixelValue (const Maybe<bool>&      x,
3342                                                 const Maybe<bool>&      y,
3343                                                 const Maybe<bool>&      z,
3344                                                 const Maybe<bool>&      w)
3345         : m_status (0)
3346 {
3347         const Maybe<bool> values[] =
3348         {
3349                 x, y, z, w
3350         };
3351
3352         for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(values); ndx++)
3353         {
3354                 if (values[ndx])
3355                         setValue(ndx, *values[ndx]);
3356                 else
3357                         setUndefined(ndx);
3358         }
3359
3360         DE_ASSERT(m_status <= 0xFFu);
3361 }
3362
3363 void PixelValue::setUndefined (size_t ndx)
3364 {
3365         DE_ASSERT(ndx < 4);
3366         DE_ASSERT(m_status <= 0xFFu);
3367
3368         m_status &= (deUint16)~(0x1u << (deUint16)(ndx * 2));
3369         DE_ASSERT(m_status <= 0xFFu);
3370 }
3371
3372 void PixelValue::setValue (size_t ndx, bool value)
3373 {
3374         DE_ASSERT(ndx < 4);
3375         DE_ASSERT(m_status <= 0xFFu);
3376
3377         m_status = (deUint16)(m_status | (deUint16)(0x1u << (ndx * 2)));
3378
3379         if (value)
3380                 m_status = (deUint16)(m_status | (deUint16)(0x1u << (ndx * 2 + 1)));
3381         else
3382                 m_status &= (deUint16)~(0x1u << (deUint16)(ndx * 2 + 1));
3383
3384         DE_ASSERT(m_status <= 0xFFu);
3385 }
3386
3387 Maybe<bool> PixelValue::getValue (size_t ndx) const
3388 {
3389         DE_ASSERT(ndx < 4);
3390         DE_ASSERT(m_status <= 0xFFu);
3391
3392         if ((m_status & (0x1u << (deUint16)(ndx * 2))) != 0)
3393         {
3394                 return just((m_status & (0x1u << (deUint32)(ndx * 2 + 1))) != 0);
3395         }
3396         else
3397                 return tcu::Nothing;
3398 }
3399
3400 void clearReferenceValues (vector<PixelValue>&  values,
3401                                                    const UVec2&                 targetSize,
3402                                                    const UVec2&                 offset,
3403                                                    const UVec2&                 size,
3404                                                    const BVec4&                 mask,
3405                                                    const PixelValue&    value)
3406 {
3407         DE_ASSERT(targetSize.x() * targetSize.y() == (deUint32)values.size());
3408         DE_ASSERT(offset.x() + size.x() <= targetSize.x());
3409         DE_ASSERT(offset.y() + size.y() <= targetSize.y());
3410
3411         for (deUint32 y = offset.y(); y < offset.y() + size.y(); y++)
3412         for (deUint32 x = offset.x(); x < offset.x() + size.x(); x++)
3413         {
3414                 for (int compNdx = 0; compNdx < 4; compNdx++)
3415                 {
3416                         if (mask[compNdx])
3417                         {
3418                                 if (value.getValue(compNdx))
3419                                         values[x + y * targetSize.x()].setValue(compNdx, *value.getValue(compNdx));
3420                                 else
3421                                         values[x + y * targetSize.x()].setUndefined(compNdx);
3422                         }
3423                 }
3424         }
3425 }
3426
3427 void markUndefined (vector<PixelValue>& values,
3428                                         const BVec4&            mask,
3429                                         const UVec2&            targetSize,
3430                                         const UVec2&            offset,
3431                                         const UVec2&            size)
3432 {
3433         DE_ASSERT(targetSize.x() * targetSize.y() == (deUint32)values.size());
3434
3435         for (deUint32 y = offset.y(); y < offset.y() + size.y(); y++)
3436         for (deUint32 x = offset.x(); x < offset.x() + size.x(); x++)
3437         {
3438                 for (int compNdx = 0; compNdx < 4; compNdx++)
3439                 {
3440                         if (mask[compNdx])
3441                                 values[x + y * targetSize.x()].setUndefined(compNdx);
3442                 }
3443         }
3444 }
3445
3446 PixelValue clearValueToPixelValue (const VkClearValue&                  value,
3447                                                                    const tcu::TextureFormat&    format,
3448                                                                    const DepthValuesArray&              depthValues)
3449 {
3450         const bool      isDepthAttachment                       = hasDepthComponent(format.order);
3451         const bool      isStencilAttachment                     = hasStencilComponent(format.order);
3452         const bool      isDepthOrStencilAttachment      = isDepthAttachment || isStencilAttachment;
3453         PixelValue      pixelValue;
3454
3455         if (isDepthOrStencilAttachment)
3456         {
3457                 if (isDepthAttachment)
3458                 {
3459                         if (value.depthStencil.depth == float(depthValues[1]) / 255.0f)
3460                                 pixelValue.setValue(0, true);
3461                         else if (value.depthStencil.depth == float(depthValues[0]) / 255.0f)
3462                                 pixelValue.setValue(0, false);
3463                         else
3464                                 DE_FATAL("Unknown depth value");
3465                 }
3466
3467                 if (isStencilAttachment)
3468                 {
3469                         if (value.depthStencil.stencil == 0xFFu)
3470                                 pixelValue.setValue(1, true);
3471                         else if (value.depthStencil.stencil == 0x0u)
3472                                 pixelValue.setValue(1, false);
3473                         else
3474                                 DE_FATAL("Unknown stencil value");
3475                 }
3476         }
3477         else
3478         {
3479                 const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
3480                 const tcu::BVec4                                channelMask             = tcu::getTextureFormatChannelMask(format);
3481
3482                 switch (channelClass)
3483                 {
3484                         case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
3485                                 for (int i = 0; i < 4; i++)
3486                                 {
3487                                         if (channelMask[i])
3488                                         {
3489                                                 if (value.color.int32[i] == 1)
3490                                                         pixelValue.setValue(i, true);
3491                                                 else if (value.color.int32[i] == 0)
3492                                                         pixelValue.setValue(i, false);
3493                                                 else
3494                                                         DE_FATAL("Unknown clear color value");
3495                                         }
3496                                 }
3497                                 break;
3498
3499                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
3500                                 for (int i = 0; i < 4; i++)
3501                                 {
3502                                         if (channelMask[i])
3503                                         {
3504                                                 if (value.color.uint32[i] == 1u)
3505                                                         pixelValue.setValue(i, true);
3506                                                 else if (value.color.uint32[i] == 0u)
3507                                                         pixelValue.setValue(i, false);
3508                                                 else
3509                                                         DE_FATAL("Unknown clear color value");
3510                                         }
3511                                 }
3512                                 break;
3513
3514                         case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
3515                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
3516                         case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
3517                                 for (int i = 0; i < 4; i++)
3518                                 {
3519                                         if (channelMask[i])
3520                                         {
3521                                                 if (value.color.float32[i] == 1.0f)
3522                                                         pixelValue.setValue(i, true);
3523                                                 else if (value.color.float32[i] == 0.0f)
3524                                                         pixelValue.setValue(i, false);
3525                                                 else
3526                                                         DE_FATAL("Unknown clear color value");
3527                                         }
3528                                 }
3529                                 break;
3530
3531                         default:
3532                                 DE_FATAL("Unknown channel class");
3533                 }
3534         }
3535
3536         return pixelValue;
3537 }
3538
3539 void renderReferenceValues (vector<vector<PixelValue> >&                referenceAttachments,
3540                                                         const RenderPass&                                       renderPassInfo,
3541                                                         const UVec2&                                            targetSize,
3542                                                         const vector<Maybe<VkClearValue> >&     imageClearValues,
3543                                                         const vector<Maybe<VkClearValue> >&     renderPassClearValues,
3544                                                         const vector<SubpassRenderInfo>&        subpassRenderInfo,
3545                                                         const UVec2&                                            renderPos,
3546                                                         const UVec2&                                            renderSize,
3547                                                         const deUint32                                          drawStartNdx,
3548                                                         const DepthValuesArray&                         depthValues)
3549 {
3550         const vector<Subpass>&  subpasses               = renderPassInfo.getSubpasses();
3551         vector<bool>                    attachmentUsed  (renderPassInfo.getAttachments().size(), false);
3552
3553         referenceAttachments.resize(renderPassInfo.getAttachments().size());
3554
3555         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
3556         {
3557                 const Attachment                        attachment      = renderPassInfo.getAttachments()[attachmentNdx];
3558                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3559                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentNdx];
3560
3561                 reference.resize(targetSize.x() * targetSize.y());
3562
3563                 if (imageClearValues[attachmentNdx])
3564                         clearReferenceValues(reference, targetSize, UVec2(0, 0), targetSize, BVec4(true), clearValueToPixelValue(*imageClearValues[attachmentNdx], format, depthValues));
3565         }
3566
3567         for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
3568         {
3569                 const Subpass&                                          subpass                         = subpasses[subpassNdx];
3570                 const SubpassRenderInfo&                        renderInfo                      = subpassRenderInfo[subpassNdx];
3571                 const vector<AttachmentReference>&      colorAttachments        = subpass.getColorAttachments();
3572
3573                 // Apply load op if attachment was used for the first time
3574                 for (size_t attachmentNdx = 0; attachmentNdx < colorAttachments.size(); attachmentNdx++)
3575                 {
3576                         const deUint32 attachmentIndex = getAttachmentNdx(colorAttachments, attachmentNdx);
3577
3578                         if (!attachmentUsed[attachmentIndex] && colorAttachments[attachmentNdx].getAttachment() != VK_ATTACHMENT_UNUSED)
3579                         {
3580                                 const Attachment&                       attachment      = renderPassInfo.getAttachments()[attachmentIndex];
3581                                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentIndex];
3582                                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3583
3584                                 DE_ASSERT(!tcu::hasDepthComponent(format.order));
3585                                 DE_ASSERT(!tcu::hasStencilComponent(format.order));
3586
3587                                 if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3588                                         clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(true), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3589                                 else if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3590                                         markUndefined(reference, BVec4(true), targetSize, renderPos, renderSize);
3591
3592                                 attachmentUsed[attachmentIndex] = true;
3593                         }
3594                 }
3595
3596                 // Apply load op to depth/stencil attachment if it was used for the first time
3597                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
3598                 {
3599                         const deUint32 attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3600
3601                         // Apply load op if attachment was used for the first time
3602                         if (!attachmentUsed[attachmentIndex])
3603                         {
3604                                 const Attachment&                       attachment      = renderPassInfo.getAttachments()[attachmentIndex];
3605                                 vector<PixelValue>&                     reference       = referenceAttachments[attachmentIndex];
3606                                 const tcu::TextureFormat        format          = mapVkFormat(attachment.getFormat());
3607
3608                                 if (tcu::hasDepthComponent(format.order))
3609                                 {
3610                                         if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3611                                                 clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(true, false, false, false), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3612                                         else if (attachment.getLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3613                                                 markUndefined(reference, BVec4(true, false, false, false), targetSize, renderPos, renderSize);
3614                                 }
3615
3616                                 if (tcu::hasStencilComponent(format.order))
3617                                 {
3618                                         if (attachment.getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
3619                                                 clearReferenceValues(reference, targetSize, renderPos, renderSize, BVec4(false, true, false, false), clearValueToPixelValue(*renderPassClearValues[attachmentIndex], format, depthValues));
3620                                         else if (attachment.getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_DONT_CARE)
3621                                                 markUndefined(reference, BVec4(false, true, false, false), targetSize, renderPos, renderSize);
3622                                 }
3623
3624                                 attachmentUsed[attachmentIndex] = true;
3625                         }
3626                 }
3627
3628                 for (size_t colorClearNdx = 0; colorClearNdx < renderInfo.getColorClears().size(); colorClearNdx++)
3629                 {
3630                         const ColorClear&                       colorClear              = renderInfo.getColorClears()[colorClearNdx];
3631                         const UVec2                                     offset                  = colorClear.getOffset();
3632                         const UVec2                                     size                    = colorClear.getSize();
3633                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[colorClearNdx].getAttachment();
3634                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3635                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3636                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3637                         VkClearValue                            value;
3638
3639                         value.color = colorClear.getColor();
3640
3641                         clearReferenceValues(reference, targetSize, offset, size, BVec4(true), clearValueToPixelValue(value, format, depthValues));
3642                 }
3643
3644                 if (renderInfo.getDepthStencilClear())
3645                 {
3646                         const DepthStencilClear&        dsClear                 = *renderInfo.getDepthStencilClear();
3647                         const UVec2                                     offset                  = dsClear.getOffset();
3648                         const UVec2                                     size                    = dsClear.getSize();
3649                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3650                         const VkImageLayout                     layout                  = subpass.getDepthStencilAttachment().getImageLayout();
3651                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3652                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3653                         const bool                                      hasStencil              = tcu::hasStencilComponent(format.order)
3654                                                                                                                 && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL;
3655                         const bool                                      hasDepth                = tcu::hasDepthComponent(format.order)
3656                                                                                                                 && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL;
3657                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3658                         VkClearValue                            value;
3659
3660                         value.depthStencil.depth = dsClear.getDepth();
3661                         value.depthStencil.stencil = dsClear.getStencil();
3662
3663                         clearReferenceValues(reference, targetSize, offset, size, BVec4(hasDepth, hasStencil, false, false), clearValueToPixelValue(value, format, depthValues));
3664                 }
3665
3666                 if (renderInfo.getRenderQuad())
3667                 {
3668                         const RenderQuad&       renderQuad      = *renderInfo.getRenderQuad();
3669                         const Vec2                      posA            = renderQuad.getCornerA();
3670                         const Vec2                      posB            = renderQuad.getCornerB();
3671                         const Vec2                      origin          = Vec2((float)renderInfo.getViewportOffset().x(), (float)renderInfo.getViewportOffset().y()) + Vec2((float)renderInfo.getViewportSize().x(), (float)renderInfo.getViewportSize().y()) / Vec2(2.0f);
3672                         const Vec2                      p                       = Vec2((float)renderInfo.getViewportSize().x(), (float)renderInfo.getViewportSize().y()) / Vec2(2.0f);
3673                         const IVec2                     posAI           (deRoundFloatToInt32(origin.x() + (p.x() * posA.x())),
3674                                                                                          deRoundFloatToInt32(origin.y() + (p.y() * posA.y())));
3675                         const IVec2                     posBI           (deRoundFloatToInt32(origin.x() + (p.x() * posB.x())),
3676                                                                                          deRoundFloatToInt32(origin.y() + (p.y() * posB.y())));
3677
3678                         DE_ASSERT(posAI.x() < posBI.x());
3679                         DE_ASSERT(posAI.y() < posBI.y());
3680
3681                         if (subpass.getInputAttachments().empty())
3682                         {
3683                                 for (size_t attachmentRefNdx = drawStartNdx; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3684                                 {
3685                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3686
3687                                         if (attachmentIndex == VK_ATTACHMENT_UNUSED)
3688                                                 continue;
3689
3690                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3691                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3692                                         const tcu::BVec4                        channelMask             = tcu::getTextureFormatChannelMask(format);
3693                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3694
3695                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3696                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3697                                         {
3698                                                 for (int compNdx = 0; compNdx < 4; compNdx++)
3699                                                 {
3700                                                         const size_t    index   = subpassNdx + attachmentIndex + compNdx;
3701                                                         const BoolOp    op              = boolOpFromIndex(index);
3702                                                         const bool              boolX   = x % 2 == (int)(index % 2);
3703                                                         const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3704
3705                                                         if (channelMask[compNdx])
3706                                                                 reference[x + y * targetSize.x()].setValue(compNdx, performBoolOp(op, boolX, boolY));
3707                                                 }
3708                                         }
3709                                 }
3710
3711                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
3712                                 {
3713                                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3714                                         const VkImageLayout                     layout                  = subpass.getDepthStencilAttachment().getImageLayout();
3715                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3716                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3717                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3718
3719                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3720                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3721                                         {
3722                                                 if (tcu::hasDepthComponent(format.order)
3723                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3724                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3725                                                 {
3726                                                         const size_t    index   = subpassNdx + 1;
3727                                                         const BoolOp    op              = boolOpFromIndex(index);
3728                                                         const bool              boolX   = x % 2 == (int)(index % 2);
3729                                                         const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3730
3731                                                         reference[x + y * targetSize.x()].setValue(0, performBoolOp(op, boolX, boolY));
3732                                                 }
3733
3734                                                 if (tcu::hasStencilComponent(format.order)
3735                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3736                                                         && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3737                                                 {
3738                                                         const size_t    index   = subpassNdx;
3739                                                         reference[x + y * targetSize.x()].setValue(1, (index % 2) == 0);
3740                                                 }
3741                                         }
3742                                 }
3743                         }
3744                         else
3745                         {
3746                                 size_t                                  outputComponentCount    = 0;
3747                                 vector<Maybe<bool> >    inputs;
3748
3749                                 DE_ASSERT(posAI.x() < posBI.x());
3750                                 DE_ASSERT(posAI.y() < posBI.y());
3751
3752                                 for (size_t attachmentRefNdx = 0; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3753                                 {
3754                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3755                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3756                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3757                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3758
3759                                         outputComponentCount += (size_t)componentCount;
3760                                 }
3761
3762                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3763                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3764                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3765                                 {
3766                                         const Attachment&                       attachment      (renderPassInfo.getAttachments()[subpass.getDepthStencilAttachment().getAttachment()]);
3767                                         const tcu::TextureFormat        format          (mapVkFormat(attachment.getFormat()));
3768
3769                                         if (tcu::hasDepthComponent(format.order))
3770                                                 outputComponentCount++;
3771                                 }
3772
3773                                 if (outputComponentCount > 0)
3774                                 {
3775                                         for (int y = posAI.y(); y < (int)posBI.y(); y++)
3776                                         for (int x = posAI.x(); x < (int)posBI.x(); x++)
3777                                         {
3778                                                 for (size_t inputAttachmentNdx = 0; inputAttachmentNdx < subpass.getInputAttachments().size(); inputAttachmentNdx++)
3779                                                 {
3780                                                         const deUint32                          attachmentIndex = subpass.getInputAttachments()[inputAttachmentNdx].getAttachment();
3781                                                         const VkImageLayout                     layout                  = subpass.getInputAttachments()[inputAttachmentNdx].getImageLayout();
3782                                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3783                                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3784                                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3785
3786                                                         for (int compNdx = 0; compNdx < componentCount; compNdx++)
3787                                                         {
3788                                                                 if ((compNdx != 0 || layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3789                                                                         && (compNdx != 1 || layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL))
3790                                                                 {
3791                                                                         inputs.push_back(referenceAttachments[attachmentIndex][x + y * targetSize.x()].getValue(compNdx));
3792                                                                 }
3793                                                         }
3794                                                 }
3795
3796                                                 const size_t inputsPerOutput = inputs.size() >= outputComponentCount
3797                                                                                                                 ? ((inputs.size() / outputComponentCount)
3798                                                                                                                         + ((inputs.size() % outputComponentCount) != 0 ? 1 : 0))
3799                                                                                                                 : 1;
3800
3801                                                 size_t outputValueNdx = 0;
3802
3803                                                 for (size_t attachmentRefNdx = 0; attachmentRefNdx < subpass.getColorAttachments().size(); attachmentRefNdx++)
3804                                                 {
3805                                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentRefNdx].getAttachment();
3806                                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3807                                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3808                                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3809                                                         const int                                       componentCount  = tcu::getNumUsedChannels(format.order);
3810
3811                                                         for (int compNdx = 0; compNdx < componentCount; compNdx++)
3812                                                         {
3813                                                                 const size_t    index   = subpassNdx + attachmentIndex + outputValueNdx;
3814                                                                 const BoolOp    op              = boolOpFromIndex(index);
3815                                                                 const bool              boolX   = x % 2 == (int)(index % 2);
3816                                                                 const bool              boolY   = y % 2 == (int)((index / 2) % 2);
3817                                                                 Maybe<bool>             output  = tcu::just(performBoolOp(op, boolX, boolY));
3818
3819                                                                 for (size_t i = 0; i < inputsPerOutput; i++)
3820                                                                 {
3821                                                                         if (!output)
3822                                                                                 break;
3823                                                                         else if (!inputs[((outputValueNdx + compNdx) * inputsPerOutput + i) % inputs.size()])
3824                                                                                 output = tcu::Nothing;
3825                                                                         else
3826                                                                                 output = (*output) == (*inputs[((outputValueNdx + compNdx) * inputsPerOutput + i) % inputs.size()]);
3827                                                                 }
3828
3829                                                                 if (output)
3830                                                                         reference[x + y * targetSize.x()].setValue(compNdx, *output);
3831                                                                 else
3832                                                                         reference[x + y * targetSize.x()].setUndefined(compNdx);
3833                                                         }
3834
3835                                                         outputValueNdx += componentCount;
3836                                                 }
3837
3838                                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3839                                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3840                                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
3841                                                 {
3842                                                         const deUint32          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3843                                                         vector<PixelValue>&     reference               = referenceAttachments[attachmentIndex];
3844                                                         const size_t            index                   = subpassNdx + attachmentIndex;
3845                                                         const BoolOp            op                              = boolOpFromIndex(index);
3846                                                         const bool                      boolX                   = x % 2 == (int)(index % 2);
3847                                                         const bool                      boolY                   = y % 2 == (int)((index / 2) % 2);
3848                                                         Maybe<bool>                     output                  = tcu::just(performBoolOp(op, boolX, boolY));
3849
3850                                                         for (size_t i = 0; i < inputsPerOutput; i++)
3851                                                         {
3852                                                                 if (!output)
3853                                                                         break;
3854                                                                 else if (inputs[(outputValueNdx * inputsPerOutput + i) % inputs.size()])
3855                                                                         output = (*output) == (*inputs[(outputValueNdx * inputsPerOutput + i) % inputs.size()]);
3856                                                                 else
3857                                                                         output = tcu::Nothing;
3858                                                         }
3859
3860                                                         if (output)
3861                                                                 reference[x + y * targetSize.x()].setValue(0, *output);
3862                                                         else
3863                                                                 reference[x + y * targetSize.x()].setUndefined(0);
3864                                                 }
3865
3866                                                 inputs.clear();
3867                                         }
3868                                 }
3869
3870                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
3871                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
3872                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
3873                                 {
3874                                         const deUint32                          attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
3875                                         const Attachment&                       attachment              = renderPassInfo.getAttachments()[attachmentIndex];
3876                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
3877                                         vector<PixelValue>&                     reference               = referenceAttachments[attachmentIndex];
3878
3879                                         if (tcu::hasStencilComponent(format.order))
3880                                         {
3881                                                 for (int y = posAI.y(); y < (int)posBI.y(); y++)
3882                                                 for (int x = posAI.x(); x < (int)posBI.x(); x++)
3883                                                 {
3884                                                         const size_t    index   = subpassNdx;
3885                                                         reference[x + y * targetSize.x()].setValue(1, (index % 2) == 0);
3886                                                 }
3887                                         }
3888                                 }
3889                         }
3890                 }
3891         }
3892
3893         // Mark all attachments that were used but not stored as undefined
3894         for (size_t attachmentIndex = 0; attachmentIndex < renderPassInfo.getAttachments().size(); attachmentIndex++)
3895         {
3896                 const Attachment                        attachment                                      = renderPassInfo.getAttachments()[attachmentIndex];
3897                 const tcu::TextureFormat        format                                          = mapVkFormat(attachment.getFormat());
3898                 vector<PixelValue>&                     reference                                       = referenceAttachments[attachmentIndex];
3899                 const bool                                      isStencilAttachment                     = hasStencilComponent(format.order);
3900                 const bool                                      isDepthOrStencilAttachment      = hasDepthComponent(format.order) || isStencilAttachment;
3901
3902                 if (attachmentUsed[attachmentIndex] && renderPassInfo.getAttachments()[attachmentIndex].getStoreOp() == VK_ATTACHMENT_STORE_OP_DONT_CARE)
3903                 {
3904                         if (isDepthOrStencilAttachment)
3905                                 markUndefined(reference, BVec4(true, false, false, false), targetSize, renderPos, renderSize);
3906                         else
3907                                 markUndefined(reference, BVec4(true), targetSize, renderPos, renderSize);
3908                 }
3909
3910                 if (attachmentUsed[attachmentIndex] && isStencilAttachment && renderPassInfo.getAttachments()[attachmentIndex].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_DONT_CARE)
3911                         markUndefined(reference, BVec4(false, true, false, false), targetSize, renderPos, renderSize);
3912         }
3913 }
3914
3915 void renderReferenceImagesFromValues (vector<tcu::TextureLevel>&                        referenceImages,
3916                                                                           const vector<vector<PixelValue> >&    referenceValues,
3917                                                                           const UVec2&                                                  targetSize,
3918                                                                           const RenderPass&                                             renderPassInfo,
3919                                                                           const DepthValuesArray&                               depthValues)
3920 {
3921         referenceImages.resize(referenceValues.size());
3922
3923         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
3924         {
3925                 const Attachment                        attachment                      = renderPassInfo.getAttachments()[attachmentNdx];
3926                 const tcu::TextureFormat        format                          = mapVkFormat(attachment.getFormat());
3927                 const vector<PixelValue>&       reference                       = referenceValues[attachmentNdx];
3928                 const bool                                      hasDepth                        = tcu::hasDepthComponent(format.order);
3929                 const bool                                      hasStencil                      = tcu::hasStencilComponent(format.order);
3930                 const bool                                      hasDepthOrStencil       = hasDepth || hasStencil;
3931                 tcu::TextureLevel&                      referenceImage          = referenceImages[attachmentNdx];
3932
3933                 referenceImage.setStorage(format, targetSize.x(), targetSize.y());
3934
3935                 if (hasDepthOrStencil)
3936                 {
3937                         if (hasDepth)
3938                         {
3939                                 const PixelBufferAccess depthAccess (tcu::getEffectiveDepthStencilAccess(referenceImage.getAccess(), tcu::Sampler::MODE_DEPTH));
3940
3941                                 for (deUint32 y = 0; y < targetSize.y(); y++)
3942                                 for (deUint32 x = 0; x < targetSize.x(); x++)
3943                                 {
3944                                         if (reference[x + y * targetSize.x()].getValue(0))
3945                                         {
3946                                                 if (*reference[x + y * targetSize.x()].getValue(0))
3947                                                         depthAccess.setPixDepth(float(depthValues[1]) / 255.0f, x, y);
3948                                                 else
3949                                                         depthAccess.setPixDepth(float(depthValues[0]) / 255.0f, x, y);
3950                                         }
3951                                         else // Fill with 3x3 grid
3952                                                 depthAccess.setPixDepth(((x / 3) % 2) == ((y / 3) % 2) ? 0.33f : 0.66f, x, y);
3953                                 }
3954                         }
3955
3956                         if (hasStencil)
3957                         {
3958                                 const PixelBufferAccess stencilAccess (tcu::getEffectiveDepthStencilAccess(referenceImage.getAccess(), tcu::Sampler::MODE_STENCIL));
3959
3960                                 for (deUint32 y = 0; y < targetSize.y(); y++)
3961                                 for (deUint32 x = 0; x < targetSize.x(); x++)
3962                                 {
3963                                         if (reference[x + y * targetSize.x()].getValue(1))
3964                                         {
3965                                                 if (*reference[x + y * targetSize.x()].getValue(1))
3966                                                         stencilAccess.setPixStencil(0xFFu, x, y);
3967                                                 else
3968                                                         stencilAccess.setPixStencil(0x0u, x, y);
3969                                         }
3970                                         else // Fill with 3x3 grid
3971                                                 stencilAccess.setPixStencil(((x / 3) % 2) == ((y / 3) % 2) ? 85 : 170, x, y);
3972                                 }
3973                         }
3974                 }
3975                 else
3976                 {
3977                         for (deUint32 y = 0; y < targetSize.y(); y++)
3978                         for (deUint32 x = 0; x < targetSize.x(); x++)
3979                         {
3980                                 tcu::Vec4 color;
3981
3982                                 for (int compNdx = 0; compNdx < 4; compNdx++)
3983                                 {
3984                                         if (reference[x + y * targetSize.x()].getValue(compNdx))
3985                                         {
3986                                                 if (*reference[x + y * targetSize.x()].getValue(compNdx))
3987                                                         color[compNdx] = 1.0f;
3988                                                 else
3989                                                         color[compNdx] = 0.0f;
3990                                         }
3991                                         else // Fill with 3x3 grid
3992                                                 color[compNdx] = ((compNdx + (x / 3)) % 2) == ((y / 3) % 2) ? 0.33f : 0.66f;
3993                                 }
3994
3995                                 referenceImage.getAccess().setPixel(color, x, y);
3996                         }
3997                 }
3998         }
3999 }
4000
4001 bool verifyColorAttachment (const vector<PixelValue>&           reference,
4002                                                         const ConstPixelBufferAccess&   result,
4003                                                         const PixelBufferAccess&                errorImage,
4004                                                         const deBool                                    useFormatCompCount)
4005 {
4006         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
4007         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
4008         bool            ok              = true;
4009
4010         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
4011         DE_ASSERT(result.getWidth() == errorImage.getWidth());
4012         DE_ASSERT(result.getHeight() == errorImage.getHeight());
4013
4014         for (int y = 0; y < result.getHeight(); y++)
4015         for (int x = 0; x < result.getWidth(); x++)
4016         {
4017                 const Vec4                      resultColor             = result.getPixel(x, y);
4018                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
4019                 bool                            pixelOk                 = true;
4020                 const deUint32          componentCount  = useFormatCompCount ? (deUint32)tcu::getNumUsedChannels(result.getFormat().order) : 4;
4021
4022                 for (deUint32 compNdx = 0; compNdx < componentCount; compNdx++)
4023                 {
4024                         const Maybe<bool> maybeValue = referenceValue.getValue(compNdx);
4025
4026                         if (maybeValue)
4027                         {
4028                                 const bool value = *maybeValue;
4029
4030                                 if ((value && (resultColor[compNdx] != 1.0f))
4031                                         || (!value && resultColor[compNdx] != 0.0f))
4032                                         pixelOk = false;
4033                         }
4034                 }
4035
4036                 if (!pixelOk)
4037                 {
4038                         errorImage.setPixel(red, x, y);
4039                         ok = false;
4040                 }
4041                 else
4042                         errorImage.setPixel(green, x, y);
4043         }
4044
4045         return ok;
4046 }
4047
4048 // Setting the alpha value to 1.0f by default helps visualization when the alpha channel is not used.
4049 const tcu::Vec4 kDefaultColorForLog     {0.0f, 0.0f, 0.0f, 1.0f};
4050 const float             kTrueComponent          = 1.0f;
4051 const float             kFalseComponent         = 0.5f;
4052 const float             kUnsetComponentLow      = 0.0f;
4053 const float             kUnsetComponentHigh     = 0.25f;
4054
4055 std::unique_ptr<tcu::TextureLevel> renderColorImageForLog (const ConstPixelBufferAccess& image, int numChannels)
4056 {
4057         // Same channel order, but using UNORM_INT8 for the color format.
4058         const auto                                                      order                   = image.getFormat().order;
4059         const tcu::TextureFormat                        loggableFormat  {order, tcu::TextureFormat::UNORM_INT8};
4060         const int                                                       width                   = image.getWidth();
4061         const int                                                       height                  = image.getHeight();
4062         std::unique_ptr<tcu::TextureLevel>      result                  {new tcu::TextureLevel{loggableFormat, width, height}};
4063         auto                                                            access                  = result->getAccess();
4064         tcu::Vec4                                                       outColor                = kDefaultColorForLog;
4065
4066         for (int x = 0; x < width; ++x)
4067         for (int y = 0; y < height; ++y)
4068         {
4069                 const auto value = image.getPixel(x, y);
4070                 for (int c = 0; c < numChannels; ++c)
4071                 {
4072                         if (value[c] == 0.0f)
4073                                 outColor[c] = kFalseComponent;
4074                         else if (value[c] == 1.0f)
4075                                 outColor[c] = kTrueComponent;
4076                         else
4077                                 DE_ASSERT(false);
4078                 }
4079                 access.setPixel(outColor, x, y);
4080         }
4081
4082         return result;
4083 }
4084
4085 std::unique_ptr<tcu::TextureLevel> renderColorImageForLog (const vector<PixelValue>& reference, const UVec2& targetSize, int numChannels)
4086 {
4087         const tcu::TextureFormat                        loggableFormat  {tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8};
4088         const int                                                       width                   = static_cast<int>(targetSize.x());
4089         const int                                                       height                  = static_cast<int>(targetSize.y());
4090         std::unique_ptr<tcu::TextureLevel>      result                  {new tcu::TextureLevel{loggableFormat, width, height}};
4091         auto                                                            access                  = result->getAccess();
4092         tcu::Vec4                                                       outColor                = kDefaultColorForLog;
4093
4094         for (int x = 0; x < width; ++x)
4095         for (int y = 0; y < height; ++y)
4096         {
4097                 const int index = x + y * width;
4098                 for (int c = 0; c < numChannels; ++c)
4099                 {
4100                         const auto maybeValue = reference[index].getValue(c);
4101                         if (maybeValue)
4102                                 outColor[c] = ((*maybeValue) ? kTrueComponent : kFalseComponent);
4103                         else
4104                                 outColor[c] = ((((x / 3) % 2) == ((y / 3) % 2)) ? kUnsetComponentLow : kUnsetComponentHigh);
4105                 }
4106                 access.setPixel(outColor, x, y);
4107         }
4108
4109         return result;
4110 }
4111
4112 bool verifyDepthAttachment (const vector<PixelValue>&           reference,
4113                                                         const ConstPixelBufferAccess&   result,
4114                                                         const PixelBufferAccess&                errorImage,
4115                                                         const DepthValuesArray&                 depthValues,
4116                                                         float                                                   epsilon)
4117 {
4118         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
4119         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
4120         bool            ok              = true;
4121
4122         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
4123         DE_ASSERT(result.getWidth() == errorImage.getWidth());
4124         DE_ASSERT(result.getHeight() == errorImage.getHeight());
4125
4126         for (int y = 0; y < result.getHeight(); y++)
4127         for (int x = 0; x < result.getWidth(); x++)
4128         {
4129                 bool pixelOk = true;
4130
4131                 const float                     resultDepth             = result.getPixDepth(x, y);
4132                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
4133                 const Maybe<bool>       maybeValue              = referenceValue.getValue(0);
4134
4135                 if (maybeValue)
4136                 {
4137                         const bool value = *maybeValue;
4138
4139                         if ((value && !depthsEqual(resultDepth, float(depthValues[1]) / 255.0f, epsilon))
4140                                 || (!value && !depthsEqual(resultDepth, float(depthValues[0]) / 255.0f, epsilon)))
4141                                 pixelOk = false;
4142                 }
4143
4144                 if (!pixelOk)
4145                 {
4146                         errorImage.setPixel(red, x, y);
4147                         ok = false;
4148                 }
4149                 else
4150                         errorImage.setPixel(green, x, y);
4151         }
4152
4153         return ok;
4154 }
4155
4156 bool verifyStencilAttachment (const vector<PixelValue>&         reference,
4157                                                           const ConstPixelBufferAccess& result,
4158                                                           const PixelBufferAccess&              errorImage)
4159 {
4160         const Vec4      red             (1.0f, 0.0f, 0.0f, 1.0f);
4161         const Vec4      green   (0.0f, 1.0f, 0.0f, 1.0f);
4162         bool            ok              = true;
4163
4164         DE_ASSERT(result.getWidth() * result.getHeight() == (int)reference.size());
4165         DE_ASSERT(result.getWidth() == errorImage.getWidth());
4166         DE_ASSERT(result.getHeight() == errorImage.getHeight());
4167
4168         for (int y = 0; y < result.getHeight(); y++)
4169         for (int x = 0; x < result.getWidth(); x++)
4170         {
4171                 bool pixelOk = true;
4172
4173                 const deUint32          resultStencil   = result.getPixStencil(x, y);
4174                 const PixelValue&       referenceValue  = reference[x + y * result.getWidth()];
4175                 const Maybe<bool>       maybeValue              = referenceValue.getValue(1);
4176
4177                 if (maybeValue)
4178                 {
4179                         const bool value = *maybeValue;
4180
4181                         if ((value && (resultStencil != 0xFFu))
4182                                 || (!value && resultStencil != 0x0u))
4183                                 pixelOk = false;
4184                 }
4185
4186                 if (!pixelOk)
4187                 {
4188                         errorImage.setPixel(red, x, y);
4189                         ok = false;
4190                 }
4191                 else
4192                         errorImage.setPixel(green, x, y);
4193         }
4194
4195         return ok;
4196 }
4197
4198 bool logAndVerifyImages (TestLog&                                                                                       log,
4199                                                  const DeviceInterface&                                                         vk,
4200                                                  VkDevice                                                                                       device,
4201                                                  const vector<de::SharedPtr<AttachmentResources> >&     attachmentResources,
4202                                                  const vector<bool>&                                                            attachmentIsLazy,
4203                                                  const RenderPass&                                                                      renderPassInfo,
4204                                                  const vector<Maybe<VkClearValue> >&                            renderPassClearValues,
4205                                                  const vector<Maybe<VkClearValue> >&                            imageClearValues,
4206                                                  const vector<SubpassRenderInfo>&                                       subpassRenderInfo,
4207                                                  const UVec2&                                                                           targetSize,
4208                                                  const TestConfig&                                                                      config)
4209 {
4210         vector<vector<PixelValue> >     referenceValues;
4211         vector<tcu::TextureLevel>       referenceAttachments;
4212         bool                                            isOk                                    = true;
4213
4214         log << TestLog::Message << "Reference images fill undefined pixels with 3x3 grid pattern." << TestLog::EndMessage;
4215
4216         renderReferenceValues(referenceValues, renderPassInfo, targetSize, imageClearValues, renderPassClearValues, subpassRenderInfo, config.renderPos, config.renderSize, config.drawStartNdx, config.depthValues);
4217         renderReferenceImagesFromValues(referenceAttachments, referenceValues, targetSize, renderPassInfo, config.depthValues);
4218
4219         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
4220         {
4221                 if (!attachmentIsLazy[attachmentNdx])
4222                 {
4223                         bool                                            attachmentOK    = true;
4224                         const Attachment                        attachment              = renderPassInfo.getAttachments()[attachmentNdx];
4225                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4226
4227                         if (tcu::hasDepthComponent(format.order) && tcu::hasStencilComponent(format.order))
4228                         {
4229                                 const tcu::TextureFormat        depthFormat                     = getDepthCopyFormat(attachment.getFormat());
4230                                 void* const                                     depthPtr                        = attachmentResources[attachmentNdx]->getResultMemory().getHostPtr();
4231
4232                                 const tcu::TextureFormat        stencilFormat           = getStencilCopyFormat(attachment.getFormat());
4233                                 void* const                                     stencilPtr                      = attachmentResources[attachmentNdx]->getSecondaryResultMemory().getHostPtr();
4234
4235                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getResultMemory());
4236                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getSecondaryResultMemory());
4237
4238                                 {
4239                                         bool                                                    depthOK                         = true;
4240                                         bool                                                    stencilOK                       = true;
4241                                         const ConstPixelBufferAccess    depthAccess                     (depthFormat, targetSize.x(), targetSize.y(), 1, depthPtr);
4242                                         const ConstPixelBufferAccess    stencilAccess           (stencilFormat, targetSize.x(), targetSize.y(), 1, stencilPtr);
4243                                         tcu::TextureLevel                               depthErrorImage         (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4244                                         tcu::TextureLevel                               stencilErrorImage       (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4245
4246                                         if (renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE
4247                                                 && !verifyDepthAttachment(referenceValues[attachmentNdx], depthAccess, depthErrorImage.getAccess(), config.depthValues, requiredDepthEpsilon(attachment.getFormat())))
4248                                         {
4249                                                 depthOK = false;
4250                                         }
4251
4252                                         if (renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE
4253                                                 && !verifyStencilAttachment(referenceValues[attachmentNdx], stencilAccess, stencilErrorImage.getAccess()))
4254                                         {
4255                                                 stencilOK = false;
4256                                         }
4257
4258                                         if (!depthOK || !stencilOK)
4259                                         {
4260                                                 const auto attachmentNdxStr = de::toString(attachmentNdx);
4261
4262                                                 // Output images.
4263                                                 log << TestLog::ImageSet("OutputAttachments" + attachmentNdxStr, "Output depth and stencil attachments " + attachmentNdxStr);
4264                                                 log << TestLog::Image("Attachment" + attachmentNdxStr + "Depth", "Attachment " + attachmentNdxStr + " Depth", depthAccess);
4265                                                 log << TestLog::Image("Attachment" + attachmentNdxStr + "Stencil", "Attachment " + attachmentNdxStr + " Stencil", stencilAccess);
4266                                                 log << TestLog::EndImageSet;
4267
4268                                                 // Reference images. These will be logged as image sets due to having depth and stencil aspects.
4269                                                 log << TestLog::Image("AttachmentReferences" + attachmentNdxStr, "Reference images " + attachmentNdxStr, referenceAttachments[attachmentNdx].getAccess());
4270
4271                                                 // Error masks.
4272                                                 log << TestLog::ImageSet("ErrorMasks" + attachmentNdxStr, "Error masks " + attachmentNdxStr);
4273                                                 if (!depthOK)
4274                                                         log << TestLog::Image("DepthAttachmentError" + attachmentNdxStr, "Depth Attachment Error " + attachmentNdxStr, depthErrorImage.getAccess());
4275                                                 if (!stencilOK)
4276                                                         log << TestLog::Image("StencilAttachmentError" + attachmentNdxStr, "Stencil Attachment Error " + attachmentNdxStr, stencilErrorImage.getAccess());
4277                                                 log << TestLog::EndImageSet;
4278
4279                                                 attachmentOK = false;
4280                                         }
4281                                 }
4282                         }
4283                         else
4284                         {
4285                                 void* const     ptr     = attachmentResources[attachmentNdx]->getResultMemory().getHostPtr();
4286
4287                                 invalidateAlloc(vk, device, attachmentResources[attachmentNdx]->getResultMemory());
4288
4289                                 bool                                                    depthOK         = true;
4290                                 bool                                                    stencilOK       = true;
4291                                 bool                                                    colorOK         = true;
4292                                 const ConstPixelBufferAccess    access          (format, targetSize.x(), targetSize.y(), 1, ptr);
4293                                 tcu::TextureLevel                               errorImage      (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), targetSize.x(), targetSize.y());
4294
4295                                 if (tcu::hasDepthComponent(format.order))
4296                                 {
4297                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4298                                                 && !verifyDepthAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess(), config.depthValues, requiredDepthEpsilon(attachment.getFormat())))
4299                                         {
4300                                                 depthOK = false;
4301                                         }
4302                                 }
4303                                 else if (tcu::hasStencilComponent(format.order))
4304                                 {
4305                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4306                                                 && !verifyStencilAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess()))
4307                                         {
4308                                                 stencilOK = false;
4309                                         }
4310                                 }
4311                                 else
4312                                 {
4313                                         if ((renderPassInfo.getAttachments()[attachmentNdx].getStoreOp() == VK_ATTACHMENT_STORE_OP_STORE || renderPassInfo.getAttachments()[attachmentNdx].getStencilStoreOp() == VK_ATTACHMENT_STORE_OP_STORE)
4314                                                 && !verifyColorAttachment(referenceValues[attachmentNdx], access, errorImage.getAccess(), config.useFormatCompCount))
4315                                         {
4316                                                 colorOK = false;
4317                                         }
4318                                 }
4319
4320                                 if (!depthOK || !stencilOK || !colorOK)
4321                                 {
4322                                         log << TestLog::ImageSet("TestImages", "Output attachment, reference image and error mask");
4323                                         if (!depthOK || !stencilOK)
4324                                         {
4325                                                 // Log without conversions.
4326                                                 log << TestLog::Image("Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx), access);
4327                                                 log << TestLog::Image("AttachmentReference" + de::toString(attachmentNdx), "Attachment reference " + de::toString(attachmentNdx), referenceAttachments[attachmentNdx].getAccess());
4328                                         }
4329                                         else
4330                                         {
4331                                                 // Convert color images to better reflect test status and output in any format.
4332                                                 const auto numChannels          = tcu::getNumUsedChannels(access.getFormat().order);
4333                                                 const auto attachmentForLog     = renderColorImageForLog(access, numChannels);
4334                                                 const auto referenceForLog      = renderColorImageForLog(referenceValues[attachmentNdx], targetSize, numChannels);
4335
4336                                                 log << TestLog::Message << "Check the attachment formats and test data to verify which components affect the test result." << TestLog::EndMessage;
4337                                                 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;
4338                                                 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;
4339
4340                                                 log << TestLog::Image("Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx), attachmentForLog->getAccess());
4341                                                 log << TestLog::Image("AttachmentReference" + de::toString(attachmentNdx), "Attachment reference " + de::toString(attachmentNdx), referenceForLog->getAccess());
4342                                         }
4343                                         log << TestLog::Image("AttachmentError" + de::toString(attachmentNdx), "Attachment Error " + de::toString(attachmentNdx), errorImage.getAccess());
4344                                         log << TestLog::EndImageSet;
4345
4346                                         attachmentOK = false;
4347                                 }
4348                         }
4349
4350                         if (!attachmentOK)
4351                                 isOk = false;
4352                 }
4353         }
4354
4355         return isOk;
4356 }
4357
4358 std::string getInputAttachmentType (VkFormat vkFormat)
4359 {
4360         const tcu::TextureFormat                format                  = mapVkFormat(vkFormat);
4361         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
4362
4363         switch (channelClass)
4364         {
4365                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
4366                         return "isubpassInput";
4367
4368                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
4369                         return "usubpassInput";
4370
4371                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
4372                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
4373                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
4374                         return "subpassInput";
4375
4376                 default:
4377                         DE_FATAL("Unknown channel class");
4378                         return "";
4379         }
4380 }
4381
4382 std::string getAttachmentType (VkFormat vkFormat, deBool useFormatCompCount)
4383 {
4384         const tcu::TextureFormat                format                  = mapVkFormat(vkFormat);
4385         const tcu::TextureChannelClass  channelClass    = tcu::getTextureChannelClass(format.type);
4386         const size_t                                    componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4387
4388         switch (channelClass)
4389         {
4390                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
4391                         if (useFormatCompCount)
4392                                 return (componentCount == 1 ? "int" : "ivec" + de::toString(componentCount));
4393                         else
4394                                 return "ivec4";
4395
4396                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
4397                         if (useFormatCompCount)
4398                                 return (componentCount == 1 ? "uint" : "uvec" + de::toString(componentCount));
4399                         else
4400                                 return "uvec4";
4401
4402                 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
4403                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
4404                 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
4405                         if (useFormatCompCount)
4406                                 return (componentCount == 1 ? "float" : "vec" + de::toString(componentCount));
4407                         else
4408                                 return "vec4";
4409
4410                 default:
4411                         DE_FATAL("Unknown channel class");
4412                         return "";
4413         }
4414 }
4415
4416 void createTestShaders (SourceCollections& dst, TestConfig config)
4417 {
4418         if (config.renderTypes & TestConfig::RENDERTYPES_DRAW)
4419         {
4420                 const vector<Subpass>&  subpasses       = config.renderPass.getSubpasses();
4421
4422                 for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
4423                 {
4424                         const Subpass&          subpass                                 = subpasses[subpassNdx];
4425                         deUint32                        inputAttachmentBinding  = 0;
4426                         std::ostringstream      vertexShader;
4427                         std::ostringstream      fragmentShader;
4428
4429                         vertexShader << "#version 310 es\n"
4430                                                  << "layout(location = 0) in highp vec2 a_position;\n"
4431                                                  << "void main (void) {\n"
4432                                                  << "\tgl_Position = vec4(a_position, 1.0, 1.0);\n"
4433                                                  << "}\n";
4434
4435                         fragmentShader << "#version 310 es\n"
4436                                                    << "precision highp float;\n";
4437
4438                         bool hasAnyDepthFormats = false;
4439
4440                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4441                         {
4442                                 const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4443                                 const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4444                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4445                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4446                                 const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
4447                                 const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
4448
4449                                 if (isDepthFormat || isStencilFormat)
4450                                 {
4451                                         if (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4452                                         {
4453                                                 hasAnyDepthFormats = true;
4454                                                 fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp subpassInput i_depth" << attachmentNdx << ";\n";
4455                                                 inputAttachmentBinding++;
4456                                         }
4457
4458                                         if (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4459                                         {
4460                                                 fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp usubpassInput i_stencil" << attachmentNdx << ";\n";
4461                                                 inputAttachmentBinding++;
4462                                         }
4463                                 }
4464                                 else
4465                                 {
4466                                         const std::string attachmentType = getInputAttachmentType(attachment.getFormat());
4467
4468                                         fragmentShader << "layout(input_attachment_index = " << attachmentNdx << ", set=0, binding=" << inputAttachmentBinding << ") uniform highp " << attachmentType << " i_color" << attachmentNdx << ";\n";
4469                                         inputAttachmentBinding++;
4470                                 }
4471                         }
4472
4473                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4474                         {
4475                                 const std::string attachmentType = getAttachmentType(config.renderPass.getAttachments()[getAttachmentNdx(subpass.getColorAttachments(), attachmentNdx)].getFormat(), config.useFormatCompCount);
4476                                 fragmentShader << "layout(location = " << attachmentNdx << ") out highp " << attachmentType << " o_color" << attachmentNdx << ";\n";
4477                         }
4478
4479                         if (hasAnyDepthFormats)
4480                                 fragmentShader << "\nbool depthsEqual(float a, float b, float epsilon) {\n"
4481                                                                 << "\treturn abs(a - b) <= epsilon;\n}\n\n";
4482
4483                         fragmentShader << "void main (void) {\n";
4484
4485                         if (subpass.getInputAttachments().empty())
4486                         {
4487                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4488                                 {
4489                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4490
4491                                         if (attachmentIndex == VK_ATTACHMENT_UNUSED)
4492                                                 continue;
4493
4494                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4495                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4496                                         const size_t                            componentCount  = config.useFormatCompCount ? (size_t)tcu::getNumUsedChannels(format.order) : 4;
4497                                         const std::string                       attachmentType  = getAttachmentType(attachment.getFormat(), config.useFormatCompCount);
4498
4499                                         fragmentShader << "\to_color" << attachmentNdx << " = " << attachmentType << "(" << attachmentType + "(";
4500
4501                                         for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4502                                         {
4503                                                 const size_t    index   = subpassNdx + attachmentIndex + compNdx;
4504                                                 const BoolOp    op              = boolOpFromIndex(index);
4505
4506                                                 if (compNdx > 0)
4507                                                         fragmentShader << ",\n\t\t";
4508
4509                                                 fragmentShader  << "((int(gl_FragCoord.x) % 2 == " << (index % 2)
4510                                                                                 << ") " << boolOpToString(op) << " ("
4511                                                                                 << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4512                                                                                 << ") ? 1.0 : 0.0)";
4513                                         }
4514
4515                                         fragmentShader << "));\n";
4516                                 }
4517
4518                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4519                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4520                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4521                                 {
4522                                         const size_t    index   = subpassNdx + 1;
4523                                         const BoolOp    op              = boolOpFromIndex(index);
4524
4525                                         fragmentShader  << "\tgl_FragDepth = ((int(gl_FragCoord.x) % 2 == " << (index % 2)
4526                                                                         << ") " << boolOpToString(op) << " ("
4527                                                                         << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4528                                                                         << ") ? " << deUint32(config.depthValues[1]) << ".0f/255.0f : " << deUint32(config.depthValues[0]) << ".0f/255.0f);\n";
4529                                 }
4530                         }
4531                         else
4532                         {
4533                                 size_t  inputComponentCount             = 0;
4534                                 size_t  outputComponentCount    = 0;
4535
4536                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4537                                 {
4538                                         const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4539                                         const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4540                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4541                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4542                                         const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4543
4544                                         if (layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4545                                                 inputComponentCount += 1;
4546                                         else if (layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4547                                                 inputComponentCount += 1;
4548                                         else
4549                                                 inputComponentCount += componentCount;
4550                                 }
4551
4552                                 for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4553                                 {
4554                                         const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4555                                         const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4556                                         const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4557                                         const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4558
4559                                         outputComponentCount += componentCount;
4560                                 }
4561
4562                                 if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4563                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4564                                         && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4565                                 {
4566                                         outputComponentCount++;
4567                                 }
4568
4569                                 if (outputComponentCount > 0)
4570                                 {
4571                                         const size_t inputsPerOutput = inputComponentCount >= outputComponentCount
4572                                                                                                         ? ((inputComponentCount / outputComponentCount)
4573                                                                                                                 + ((inputComponentCount % outputComponentCount) != 0 ? 1 : 0))
4574                                                                                                         : 1;
4575
4576                                         fragmentShader << "\tbool inputs[" << inputComponentCount << "];\n";
4577
4578                                         if (outputComponentCount > 0)
4579                                                 fragmentShader << "\tbool outputs[" << outputComponentCount << "];\n";
4580
4581                                         size_t inputValueNdx = 0;
4582
4583                                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
4584                                         {
4585                                                 const char* const       components[]    =
4586                                                 {
4587                                                         "x", "y", "z", "w"
4588                                                 };
4589                                                 const deUint32                          attachmentIndex = subpass.getInputAttachments()[attachmentNdx].getAttachment();
4590                                                 const VkImageLayout                     layout                  = subpass.getInputAttachments()[attachmentNdx].getImageLayout();
4591                                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4592                                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4593                                                 const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4594                                                 const bool                                      isDepthFormat   = tcu::hasDepthComponent(format.order);
4595                                                 const bool                                      isStencilFormat = tcu::hasStencilComponent(format.order);
4596
4597                                                 if (isDepthFormat || isStencilFormat)
4598                                                 {
4599                                                         if (isDepthFormat && layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)
4600                                                         {
4601                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = depthsEqual(" << deUint32(config.depthValues[1]) <<
4602                                                                         ".0f/255.0f, float(subpassLoad(i_depth" << attachmentNdx << ").x), " <<
4603                                                                         std::fixed << std::setprecision(12) << requiredDepthEpsilon(attachment.getFormat()) << ");\n";
4604                                                                 inputValueNdx++;
4605                                                         }
4606
4607                                                         if (isStencilFormat && layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4608                                                         {
4609                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = 255u == subpassLoad(i_stencil" << attachmentNdx << ").x;\n";
4610                                                                 inputValueNdx++;
4611                                                         }
4612                                                 }
4613                                                 else
4614                                                 {
4615                                                         for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4616                                                         {
4617                                                                 fragmentShader << "\tinputs[" << inputValueNdx << "] = 1.0 == float(subpassLoad(i_color" << attachmentNdx << ")." << components[compNdx] << ");\n";
4618                                                                 inputValueNdx++;
4619                                                         }
4620                                                 }
4621                                         }
4622
4623                                         size_t outputValueNdx = 0;
4624
4625                                         for (size_t attachmentNdx = config.drawStartNdx; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
4626                                         {
4627                                                 const deUint32                          attachmentIndex = subpass.getColorAttachments()[attachmentNdx].getAttachment();
4628                                                 const Attachment                        attachment              = config.renderPass.getAttachments()[attachmentIndex];
4629                                                 const std::string                       attachmentType  = getAttachmentType(config.renderPass.getAttachments()[attachmentIndex].getFormat(), config.useFormatCompCount);
4630                                                 const tcu::TextureFormat        format                  = mapVkFormat(attachment.getFormat());
4631                                                 const size_t                            componentCount  = (size_t)tcu::getNumUsedChannels(format.order);
4632
4633                                                 for (size_t compNdx = 0; compNdx < componentCount; compNdx++)
4634                                                 {
4635                                                         const size_t    index   = subpassNdx + attachmentIndex + outputValueNdx;
4636                                                         const BoolOp    op              = boolOpFromIndex(index);
4637
4638                                                         fragmentShader << "\toutputs[" << outputValueNdx + compNdx << "] = "
4639                                                                                         << "(int(gl_FragCoord.x) % 2 == " << (index % 2)
4640                                                                                         << ") " << boolOpToString(op) << " ("
4641                                                                                         << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4642                                                                                         << ");\n";
4643
4644                                                         for (size_t i = 0; i < inputsPerOutput; i++)
4645                                                                 fragmentShader << "\toutputs[" << outputValueNdx + compNdx << "] = outputs[" << outputValueNdx + compNdx << "] == inputs[" <<  ((outputValueNdx + compNdx) * inputsPerOutput + i) %  inputComponentCount << "];\n";
4646                                                 }
4647
4648                                                 fragmentShader << "\to_color" << attachmentNdx << " = " << attachmentType << "(";
4649
4650                                                 for (size_t compNdx = 0; compNdx < (config.useFormatCompCount ? componentCount : 4); compNdx++)
4651                                                 {
4652                                                         if (compNdx > 0)
4653                                                                 fragmentShader << ", ";
4654
4655                                                         if (compNdx < componentCount)
4656                                                                 fragmentShader << "outputs[" << outputValueNdx + compNdx << "]";
4657                                                         else
4658                                                                 fragmentShader << "0";
4659                                                 }
4660
4661                                                 outputValueNdx += componentCount;
4662
4663                                                 fragmentShader << ");\n";
4664                                         }
4665
4666                                         if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED
4667                                                 && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
4668                                                 && subpass.getDepthStencilAttachment().getImageLayout() != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
4669                                         {
4670                                                 const deUint32  attachmentIndex = subpass.getDepthStencilAttachment().getAttachment();
4671                                                 const size_t    index                   = subpassNdx + attachmentIndex;
4672                                                 const BoolOp    op                              = boolOpFromIndex(index);
4673
4674                                                 fragmentShader << "\toutputs[" << outputValueNdx << "] = "
4675                                                                                 << "(int(gl_FragCoord.x) % 2 == " << (index % 2)
4676                                                                                 << ") " << boolOpToString(op) << " ("
4677                                                                                 << "int(gl_FragCoord.y) % 2 == " << ((index / 2) % 2)
4678                                                                                 << ");\n";
4679
4680                                                 for (size_t i = 0; i < inputsPerOutput; i++)
4681                                                         fragmentShader << "\toutputs[" << outputValueNdx << "] = outputs[" << outputValueNdx << "] == inputs[" <<  (outputValueNdx * inputsPerOutput + i) %  inputComponentCount << "];\n";
4682
4683                                                 fragmentShader << "\tgl_FragDepth = outputs[" << outputValueNdx << "] ? " << deUint32(config.depthValues[1]) << ".0f/255.0f : " << deUint32(config.depthValues[0]) << ".0f/255.0f;\n";
4684                                         }
4685                                 }
4686                         }
4687
4688                         fragmentShader << "}\n";
4689
4690                         dst.glslSources.add(de::toString(subpassNdx) + "-vert") << glu::VertexSource(vertexShader.str());
4691                         dst.glslSources.add(de::toString(subpassNdx) + "-frag") << glu::FragmentSource(fragmentShader.str());
4692                 }
4693         }
4694 }
4695
4696 void initializeAttachmentIsLazy (vector<bool>& attachmentIsLazy, const vector<Attachment>& attachments, TestConfig::ImageMemory imageMemory)
4697 {
4698         bool lastAttachmentWasLazy      = false;
4699
4700         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4701         {
4702                 if (attachments[attachmentNdx].getLoadOp() != VK_ATTACHMENT_LOAD_OP_LOAD
4703                         && attachments[attachmentNdx].getStoreOp() != VK_ATTACHMENT_STORE_OP_STORE
4704                         && attachments[attachmentNdx].getStencilLoadOp() != VK_ATTACHMENT_LOAD_OP_LOAD
4705                         && attachments[attachmentNdx].getStencilStoreOp() != VK_ATTACHMENT_STORE_OP_STORE)
4706                 {
4707                         if (imageMemory == TestConfig::IMAGEMEMORY_LAZY || (imageMemory & TestConfig::IMAGEMEMORY_LAZY && !lastAttachmentWasLazy))
4708                         {
4709                                 attachmentIsLazy.push_back(true);
4710
4711                                 lastAttachmentWasLazy   = true;
4712                         }
4713                         else if (imageMemory & TestConfig::IMAGEMEMORY_STRICT)
4714                         {
4715                                 attachmentIsLazy.push_back(false);
4716                                 lastAttachmentWasLazy = false;
4717                         }
4718                         else
4719                                 DE_FATAL("Unknown imageMemory");
4720                 }
4721                 else
4722                         attachmentIsLazy.push_back(false);
4723         }
4724 }
4725
4726 enum AttachmentRefType
4727 {
4728         ATTACHMENTREFTYPE_COLOR,
4729         ATTACHMENTREFTYPE_DEPTH_STENCIL,
4730         ATTACHMENTREFTYPE_INPUT,
4731         ATTACHMENTREFTYPE_RESOLVE,
4732 };
4733
4734 VkImageUsageFlags getImageUsageFromLayout (VkImageLayout layout)
4735 {
4736         switch (layout)
4737         {
4738                 case VK_IMAGE_LAYOUT_GENERAL:
4739                 case VK_IMAGE_LAYOUT_PREINITIALIZED:
4740                         return 0;
4741
4742                 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
4743                         return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4744
4745                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
4746                 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
4747                         return VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4748
4749                 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
4750                         return VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
4751
4752                 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
4753                         return VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
4754
4755                 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
4756                         return VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4757
4758                 default:
4759                         DE_FATAL("Unexpected image layout");
4760                         return 0;
4761         }
4762 }
4763
4764 void getImageUsageFromAttachmentReferences(vector<VkImageUsageFlags>& attachmentImageUsage, AttachmentRefType refType, size_t count, const AttachmentReference* references)
4765 {
4766         for (size_t referenceNdx = 0; referenceNdx < count; ++referenceNdx)
4767         {
4768                 const deUint32 attachment = references[referenceNdx].getAttachment();
4769
4770                 if (attachment != VK_ATTACHMENT_UNUSED)
4771                 {
4772                         VkImageUsageFlags usage;
4773
4774                         switch (refType)
4775                         {
4776                                 case ATTACHMENTREFTYPE_COLOR:
4777                                 case ATTACHMENTREFTYPE_RESOLVE:
4778                                         usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4779                                         break;
4780
4781                                 case ATTACHMENTREFTYPE_DEPTH_STENCIL:
4782                                         usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4783                                         break;
4784
4785                                 case ATTACHMENTREFTYPE_INPUT:
4786                                         usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
4787                                         break;
4788
4789                                 default:
4790                                         DE_FATAL("Unexpected attachment reference type");
4791                                         usage = 0;
4792                                         break;
4793                         }
4794
4795                         attachmentImageUsage[attachment] |= usage;
4796                 }
4797         }
4798 }
4799
4800 void getImageUsageFromAttachmentReferences(vector<VkImageUsageFlags>& attachmentImageUsage, AttachmentRefType refType, const vector<AttachmentReference>& references)
4801 {
4802         if (!references.empty())
4803         {
4804                 getImageUsageFromAttachmentReferences(attachmentImageUsage, refType, references.size(), &references[0]);
4805         }
4806 }
4807
4808 void initializeAttachmentImageUsage (Context &context, vector<VkImageUsageFlags>& attachmentImageUsage, const RenderPass& renderPassInfo, const vector<bool>& attachmentIsLazy, const vector<Maybe<VkClearValue> >& clearValues)
4809 {
4810         attachmentImageUsage.resize(renderPassInfo.getAttachments().size(), VkImageUsageFlags(0));
4811
4812         for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); ++subpassNdx)
4813         {
4814                 const Subpass& subpass = renderPassInfo.getSubpasses()[subpassNdx];
4815
4816                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_COLOR, subpass.getColorAttachments());
4817                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_DEPTH_STENCIL, 1, &subpass.getDepthStencilAttachment());
4818                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_INPUT, subpass.getInputAttachments());
4819                 getImageUsageFromAttachmentReferences(attachmentImageUsage, ATTACHMENTREFTYPE_RESOLVE, subpass.getResolveAttachments());
4820         }
4821
4822         for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
4823         {
4824                 const Attachment& attachment = renderPassInfo.getAttachments()[attachmentNdx];
4825                 const VkFormatProperties        formatProperties        = getPhysicalDeviceFormatProperties(context.getInstanceInterface(), context.getPhysicalDevice(), attachment.getFormat());
4826                 const VkFormatFeatureFlags      supportedFeatures       = formatProperties.optimalTilingFeatures;
4827
4828                 if ((supportedFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0)
4829                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_SAMPLED_BIT;
4830
4831                 if ((supportedFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) != 0)
4832                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_STORAGE_BIT;
4833
4834                 attachmentImageUsage[attachmentNdx] |= getImageUsageFromLayout(attachment.getInitialLayout());
4835                 attachmentImageUsage[attachmentNdx] |= getImageUsageFromLayout(attachment.getFinalLayout());
4836
4837                 if (!attachmentIsLazy[attachmentNdx])
4838                 {
4839                         if (clearValues[attachmentNdx])
4840                                 attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4841
4842                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
4843                 }
4844                 else
4845                 {
4846                         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);
4847
4848                         attachmentImageUsage[attachmentNdx] &= allowedTransientBits;
4849                         attachmentImageUsage[attachmentNdx] |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
4850                 }
4851         }
4852 }
4853
4854 void initializeSubpassIsSecondary (vector<bool>& subpassIsSecondary, const vector<Subpass>& subpasses, TestConfig::CommandBufferTypes commandBuffer)
4855 {
4856         bool lastSubpassWasSecondary = false;
4857
4858         for (size_t subpassNdx = 0; subpassNdx < subpasses.size(); subpassNdx++)
4859         {
4860                 if (commandBuffer == TestConfig::COMMANDBUFFERTYPES_SECONDARY || (commandBuffer & TestConfig::COMMANDBUFFERTYPES_SECONDARY && !lastSubpassWasSecondary))
4861                 {
4862                         subpassIsSecondary.push_back(true);
4863                         lastSubpassWasSecondary = true;
4864                 }
4865                 else if (commandBuffer & TestConfig::COMMANDBUFFERTYPES_INLINE)
4866                 {
4867                         subpassIsSecondary.push_back(false);
4868                         lastSubpassWasSecondary = false;
4869                 }
4870                 else
4871                         DE_FATAL("Unknown commandBuffer");
4872         }
4873 }
4874
4875 void initializeImageClearValues (de::Random& rng, vector<Maybe<VkClearValue> >& clearValues, const vector<Attachment>& attachments, const vector<bool>& isLazy, deBool useFormatCompCount, const DepthValuesArray& depthValues)
4876 {
4877         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4878         {
4879                 if (!isLazy[attachmentNdx])
4880                         clearValues.push_back(just(randomClearValue(attachments[attachmentNdx], rng, useFormatCompCount, depthValues)));
4881                 else
4882                         clearValues.push_back(tcu::Nothing);
4883         }
4884 }
4885
4886 void initializeRenderPassClearValues (de::Random& rng, vector<Maybe<VkClearValue> >& clearValues, const vector<Attachment>& attachments, deBool useFormatCompCount, const DepthValuesArray& depthValues)
4887 {
4888         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
4889         {
4890                 if (attachments[attachmentNdx].getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR
4891                         || attachments[attachmentNdx].getStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR)
4892                 {
4893                         clearValues.push_back(just(randomClearValue(attachments[attachmentNdx], rng, useFormatCompCount, depthValues)));
4894                 }
4895                 else
4896                         clearValues.push_back(tcu::Nothing);
4897         }
4898 }
4899
4900 void logSubpassRenderInfo (TestLog& log, const SubpassRenderInfo& info, TestConfig config)
4901 {
4902         log << TestLog::Message << "Viewport, offset: " << info.getViewportOffset() << ", size: " << info.getViewportSize() << TestLog::EndMessage;
4903
4904         if (info.isSecondary())
4905                 log << TestLog::Message << "Subpass uses secondary command buffers" << TestLog::EndMessage;
4906         else
4907                 log << TestLog::Message << "Subpass uses inlined commands" << TestLog::EndMessage;
4908
4909         for (deUint32 attachmentNdx = 0; attachmentNdx < info.getColorClears().size(); attachmentNdx++)
4910         {
4911                 const ColorClear&       colorClear      = info.getColorClears()[attachmentNdx];
4912
4913                 log << TestLog::Message << "Clearing color attachment " << attachmentNdx
4914                         << ". Offset: " << colorClear.getOffset()
4915                         << ", Size: " << colorClear.getSize()
4916                         << ", Color: " << clearColorToString(info.getColorAttachment(attachmentNdx).getFormat(), colorClear.getColor(), config.useFormatCompCount) << TestLog::EndMessage;
4917         }
4918
4919         if (info.getDepthStencilClear())
4920         {
4921                 const DepthStencilClear&        depthStencilClear       = *info.getDepthStencilClear();
4922
4923                 log << TestLog::Message << "Clearing depth stencil attachment"
4924                         << ". Offset: " << depthStencilClear.getOffset()
4925                         << ", Size: " << depthStencilClear.getSize()
4926                         << ", Depth: " << depthStencilClear.getDepth()
4927                         << ", Stencil: " << depthStencilClear.getStencil() << TestLog::EndMessage;
4928         }
4929
4930         if (info.getRenderQuad())
4931         {
4932                 const RenderQuad&       renderQuad      = *info.getRenderQuad();
4933
4934                 log << TestLog::Message << "Rendering grid quad to " << renderQuad.getCornerA() << " -> " << renderQuad.getCornerB() << TestLog::EndMessage;
4935         }
4936 }
4937
4938 void logTestCaseInfo (TestLog&                                                          log,
4939                                           const TestConfig&                                             config,
4940                                           const vector<bool>&                                   attachmentIsLazy,
4941                                           const vector<Maybe<VkClearValue> >&   imageClearValues,
4942                                           const vector<Maybe<VkClearValue> >&   renderPassClearValues,
4943                                           const vector<SubpassRenderInfo>&              subpassRenderInfo)
4944 {
4945         const RenderPass&       renderPass      = config.renderPass;
4946
4947         logRenderPassInfo(log, renderPass);
4948
4949         DE_ASSERT(attachmentIsLazy.size() == renderPass.getAttachments().size());
4950         DE_ASSERT(imageClearValues.size() == renderPass.getAttachments().size());
4951         DE_ASSERT(renderPassClearValues.size() == renderPass.getAttachments().size());
4952
4953         log << TestLog::Message << "TargetSize: " << config.targetSize << TestLog::EndMessage;
4954         log << TestLog::Message << "Render area, Offset: " << config.renderPos << ", Size: " << config.renderSize << TestLog::EndMessage;
4955
4956         for (size_t attachmentNdx = 0; attachmentNdx < attachmentIsLazy.size(); attachmentNdx++)
4957         {
4958                 const tcu::ScopedLogSection     section (log, "Attachment" + de::toString(attachmentNdx), "Attachment " + de::toString(attachmentNdx));
4959
4960                 if (attachmentIsLazy[attachmentNdx])
4961                         log << TestLog::Message << "Is lazy." << TestLog::EndMessage;
4962
4963                 if (imageClearValues[attachmentNdx])
4964                         log << TestLog::Message << "Image is cleared to " << clearValueToString(renderPass.getAttachments()[attachmentNdx].getFormat(),
4965                                         *imageClearValues[attachmentNdx], config.useFormatCompCount) << " before rendering." << TestLog::EndMessage;
4966
4967                 if (renderPass.getAttachments()[attachmentNdx].getLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR && renderPassClearValues[attachmentNdx])
4968                         log << TestLog::Message << "Attachment is cleared to " << clearValueToString(renderPass.getAttachments()[attachmentNdx].getFormat(),
4969                                         *renderPassClearValues[attachmentNdx], config.useFormatCompCount) << " in the beginning of the render pass." << TestLog::EndMessage;
4970         }
4971
4972         for (size_t subpassNdx = 0; subpassNdx < renderPass.getSubpasses().size(); subpassNdx++)
4973         {
4974                 const tcu::ScopedLogSection section (log, "Subpass" + de::toString(subpassNdx), "Subpass " + de::toString(subpassNdx));
4975
4976                 logSubpassRenderInfo(log, subpassRenderInfo[subpassNdx], config);
4977         }
4978 }
4979
4980 float roundToViewport (float x, deUint32 offset, deUint32 size)
4981 {
4982         const float             origin  = (float)(offset) + ((float(size) / 2.0f));
4983         const float             p               = (float)(size) / 2.0f;
4984         const deInt32   xi              = deRoundFloatToInt32(origin + (p * x));
4985
4986         return (((float)xi) - origin) / p;
4987 }
4988
4989 void initializeSubpassRenderInfo (vector<SubpassRenderInfo>& renderInfos, de::Random& rng, const RenderPass& renderPass, const TestConfig& config)
4990 {
4991         const TestConfig::CommandBufferTypes    commandBuffer                   = config.commandBufferTypes;
4992         const vector<Subpass>&                                  subpasses                               = renderPass.getSubpasses();
4993         bool                                                                    lastSubpassWasSecondary = false;
4994
4995         for (deUint32 subpassNdx = 0; subpassNdx < (deUint32)subpasses.size(); subpassNdx++)
4996         {
4997                 const Subpass&                          subpass                         = subpasses[subpassNdx];
4998                 const bool                                      subpassIsSecondary      = commandBuffer == TestConfig::COMMANDBUFFERTYPES_SECONDARY
4999                                                                                                                 || (commandBuffer & TestConfig::COMMANDBUFFERTYPES_SECONDARY && !lastSubpassWasSecondary) ? true : false;
5000                 const bool                                      omitBlendState          = subpass.getOmitBlendState();
5001                 const UVec2                                     viewportSize            ((config.renderSize * UVec2(2)) / UVec2(3));
5002                 const UVec2                                     viewportOffset          (config.renderPos.x() + (subpassNdx % 2) * (config.renderSize.x() / 3),
5003                                                                                                                  config.renderPos.y() + ((subpassNdx / 2) % 2) * (config.renderSize.y() / 3));
5004
5005                 vector<ColorClear>                      colorClears;
5006                 Maybe<DepthStencilClear>        depthStencilClear;
5007                 Maybe<RenderQuad>                       renderQuad;
5008
5009                 lastSubpassWasSecondary         = subpassIsSecondary;
5010
5011                 if (config.renderTypes & TestConfig::RENDERTYPES_CLEAR)
5012                 {
5013                         const vector<AttachmentReference>&      colorAttachments        = subpass.getColorAttachments();
5014
5015                         for (size_t attachmentRefNdx = 0; attachmentRefNdx < colorAttachments.size(); attachmentRefNdx++)
5016                         {
5017                                 const AttachmentReference&      attachmentRef   = colorAttachments[attachmentRefNdx];
5018                                 const Attachment&                       attachment              = renderPass.getAttachments()[attachmentRef.getAttachment()];
5019                                 const UVec2                                     size                    ((viewportSize * UVec2(2)) / UVec2(3));
5020                                 const UVec2                                     offset                  (viewportOffset.x() + ((deUint32)attachmentRefNdx % 2u) * (viewportSize.x() / 3u),
5021                                                                                                                          viewportOffset.y() + (((deUint32)attachmentRefNdx / 2u) % 2u) * (viewportSize.y() / 3u));
5022                                 const VkClearColorValue         color                   = randomColorClearValue(attachment, rng, config.useFormatCompCount);
5023
5024                                 colorClears.push_back(ColorClear(offset, size, color));
5025                         }
5026
5027                         if (subpass.getDepthStencilAttachment().getAttachment() != VK_ATTACHMENT_UNUSED)
5028                         {
5029                                 const Attachment&       attachment      = renderPass.getAttachments()[subpass.getDepthStencilAttachment().getAttachment()];
5030                                 const UVec2                     size            ((viewportSize * UVec2(2)) / UVec2(3));
5031                                 const UVec2                     offset          (viewportOffset.x() + ((deUint32)colorAttachments.size() % 2u) * (viewportSize.x() / 3u),
5032                                                                                                  viewportOffset.y() + (((deUint32)colorAttachments.size() / 2u) % 2u) * (viewportSize.y() / 3u));
5033                                 const VkClearValue      value           = randomClearValue(attachment, rng, config.useFormatCompCount, config.depthValues);
5034
5035                                 depthStencilClear = tcu::just(DepthStencilClear(offset, size, value.depthStencil.depth, value.depthStencil.stencil));
5036                         }
5037                 }
5038
5039                 if (config.renderTypes & TestConfig::RENDERTYPES_DRAW)
5040                 {
5041                         const float     w       = (subpassNdx % 2) == 0 ? 1.0f : 1.25f;
5042                         const float     h       = (subpassNdx % 2) == 0 ? 1.25f : 1.0f;
5043
5044                         const float     x0      = roundToViewport((subpassNdx % 2) == 0 ? 1.0f - w : -1.0f, viewportOffset.x(), viewportSize.x());
5045                         const float     x1      = roundToViewport((subpassNdx % 2) == 0 ? 1.0f : -1.0f + w, viewportOffset.x(), viewportSize.x());
5046
5047                         const float     y0      = roundToViewport(((subpassNdx / 2) % 2) == 0 ? 1.0f - h : -1.0f, viewportOffset.y(), viewportSize.y());
5048                         const float     y1      = roundToViewport(((subpassNdx / 2) % 2) == 0 ? 1.0f : -1.0f + h, viewportOffset.y(), viewportSize.y());
5049
5050                         renderQuad = tcu::just(RenderQuad(tcu::Vec2(x0, y0), tcu::Vec2(x1, y1)));
5051                 }
5052
5053                 renderInfos.push_back(SubpassRenderInfo(renderPass, subpassNdx, config.drawStartNdx, subpassIsSecondary, omitBlendState, viewportOffset, viewportSize, renderQuad, colorClears, depthStencilClear));
5054         }
5055 }
5056
5057 void checkTextureFormatSupport (TestLog&                                        log,
5058                                                                 const InstanceInterface&        vk,
5059                                                                 VkPhysicalDevice                        device,
5060                                                                 const vector<Attachment>&       attachments)
5061 {
5062         bool supported = true;
5063
5064         for (size_t attachmentNdx = 0; attachmentNdx < attachments.size(); attachmentNdx++)
5065         {
5066                 const Attachment&                       attachment                                      = attachments[attachmentNdx];
5067                 const tcu::TextureFormat        format                                          = mapVkFormat(attachment.getFormat());
5068                 const bool                                      isDepthOrStencilAttachment      = hasDepthComponent(format.order) || hasStencilComponent(format.order);
5069                 const VkFormatFeatureFlags      flags                                           = isDepthOrStencilAttachment? VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
5070                 VkFormatProperties                      properties;
5071
5072                 vk.getPhysicalDeviceFormatProperties(device, attachment.getFormat(), &properties);
5073
5074                 if ((properties.optimalTilingFeatures & flags) != flags)
5075                 {
5076                         supported = false;
5077                         log << TestLog::Message << "Format: " << attachment.getFormat() << " not supported as " << (isDepthOrStencilAttachment ? "depth stencil attachment" : "color attachment") << TestLog::EndMessage;
5078                 }
5079         }
5080
5081         if (!supported)
5082                 TCU_THROW(NotSupportedError, "Format not supported");
5083 }
5084
5085 tcu::TestStatus renderPassTest (Context& context, TestConfig config)
5086 {
5087         const UVec2                                                     targetSize                      = config.targetSize;
5088         const UVec2                                                     renderPos                       = config.renderPos;
5089         const UVec2                                                     renderSize                      = config.renderSize;
5090         const RenderPass&                                       renderPassInfo          = config.renderPass;
5091
5092         TestLog&                                                        log                                     = context.getTestContext().getLog();
5093         de::Random                                                      rng                                     (config.seed);
5094
5095         vector<bool>                                            attachmentIsLazy;
5096         vector<VkImageUsageFlags>                       attachmentImageUsage;
5097         vector<Maybe<VkClearValue> >            imageClearValues;
5098         vector<Maybe<VkClearValue> >            renderPassClearValues;
5099
5100         vector<bool>                                            subpassIsSecondary;
5101         vector<SubpassRenderInfo>                       subpassRenderInfo;
5102
5103         if (config.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2)
5104                 context.requireDeviceFunctionality("VK_KHR_create_renderpass2");
5105
5106         if (config.groupParams->renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
5107                 context.requireDeviceFunctionality("VK_KHR_dynamic_rendering");
5108
5109         if (config.allocationKind == ALLOCATION_KIND_DEDICATED)
5110         {
5111                 if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
5112                         TCU_THROW(NotSupportedError, "VK_KHR_dedicated_allocation is not supported");
5113         }
5114
5115         if (!renderPassInfo.getInputAspects().empty())
5116         {
5117                 if (!context.isDeviceFunctionalitySupported("VK_KHR_maintenance2"))
5118                         TCU_THROW(NotSupportedError, "Extension VK_KHR_maintenance2 not supported.");
5119         }
5120
5121         {
5122                 bool requireDepthStencilLayout = false;
5123
5124                 for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
5125                 {
5126                         if (renderPassInfo.getAttachments()[attachmentNdx].getInitialLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5127                                 || renderPassInfo.getAttachments()[attachmentNdx].getInitialLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
5128                                 || renderPassInfo.getAttachments()[attachmentNdx].getFinalLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5129                                 || renderPassInfo.getAttachments()[attachmentNdx].getFinalLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5130                         {
5131                                 requireDepthStencilLayout = true;
5132                                 break;
5133                         }
5134                 }
5135
5136                 for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size() && !requireDepthStencilLayout; subpassNdx++)
5137                 {
5138                         const Subpass& subpass (renderPassInfo.getSubpasses()[subpassNdx]);
5139
5140                         for (size_t attachmentNdx = 0; attachmentNdx < subpass.getColorAttachments().size(); attachmentNdx++)
5141                         {
5142                                 if (subpass.getColorAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5143                                         || subpass.getColorAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5144                                 {
5145                                         requireDepthStencilLayout = true;
5146                                         break;
5147                                 }
5148                         }
5149
5150                         for (size_t attachmentNdx = 0; !requireDepthStencilLayout && attachmentNdx < subpass.getInputAttachments().size(); attachmentNdx++)
5151                         {
5152                                 if (subpass.getInputAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5153                                         || subpass.getInputAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5154                                 {
5155                                         requireDepthStencilLayout = true;
5156                                         break;
5157                                 }
5158                         }
5159
5160                         for (size_t attachmentNdx = 0; !requireDepthStencilLayout && attachmentNdx < subpass.getResolveAttachments().size(); attachmentNdx++)
5161                         {
5162                                 if (subpass.getResolveAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5163                                         || subpass.getResolveAttachments()[attachmentNdx].getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5164                                 {
5165                                         requireDepthStencilLayout = true;
5166                                         break;
5167                                 }
5168                         }
5169
5170                         if (subpass.getDepthStencilAttachment().getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
5171                                 || subpass.getDepthStencilAttachment().getImageLayout() == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)
5172                         {
5173                                 requireDepthStencilLayout = true;
5174                                 break;
5175                         }
5176                 }
5177
5178                 if (requireDepthStencilLayout && !context.isDeviceFunctionalitySupported("VK_KHR_maintenance2"))
5179                         TCU_THROW(NotSupportedError, "VK_KHR_maintenance2 is not supported");
5180         }
5181
5182         initializeAttachmentIsLazy(attachmentIsLazy, renderPassInfo.getAttachments(), config.imageMemory);
5183         initializeImageClearValues(rng, imageClearValues, renderPassInfo.getAttachments(), attachmentIsLazy, config.useFormatCompCount, config.depthValues);
5184         initializeAttachmentImageUsage(context, attachmentImageUsage, renderPassInfo, attachmentIsLazy, imageClearValues);
5185         initializeRenderPassClearValues(rng, renderPassClearValues, renderPassInfo.getAttachments(), config.useFormatCompCount, config.depthValues);
5186
5187         initializeSubpassIsSecondary(subpassIsSecondary, renderPassInfo.getSubpasses(), config.commandBufferTypes);
5188         initializeSubpassRenderInfo(subpassRenderInfo, rng, renderPassInfo, config);
5189
5190         logTestCaseInfo(log, config, attachmentIsLazy, imageClearValues, renderPassClearValues, subpassRenderInfo);
5191
5192         checkTextureFormatSupport(log, context.getInstanceInterface(), context.getPhysicalDevice(), config.renderPass.getAttachments());
5193
5194         {
5195                 const vk::VkPhysicalDeviceProperties properties = vk::getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice());
5196
5197                 log << TestLog::Message << "Max color attachments: " << properties.limits.maxColorAttachments << TestLog::EndMessage;
5198
5199                 for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); subpassNdx++)
5200                 {
5201                          if (renderPassInfo.getSubpasses()[subpassNdx].getColorAttachments().size() > (size_t)properties.limits.maxColorAttachments)
5202                                  TCU_THROW(NotSupportedError, "Subpass uses more than maxColorAttachments.");
5203                 }
5204         }
5205
5206         {
5207                 const InstanceInterface&                                        vki                                                                     = context.getInstanceInterface();
5208                 const VkPhysicalDevice&                                         physDevice                                                      = context.getPhysicalDevice();
5209                 const VkDevice                                                          device                                                          = context.getDevice();
5210                 const DeviceInterface&                                          vk                                                                      = context.getDeviceInterface();
5211                 const VkQueue                                                           queue                                                           = context.getUniversalQueue();
5212                 const deUint32                                                          queueIndex                                                      = context.getUniversalQueueFamilyIndex();
5213                 Allocator&                                                                      allocator                                                       = context.getDefaultAllocator();
5214
5215                 const Unique<VkCommandPool>                                     commandBufferPool                                       (createCommandPool(vk, device, 0, queueIndex));
5216                 const Unique<VkCommandBuffer>                           initializeImagesCommandBuffer           (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5217                 const Unique<VkCommandBuffer>                           renderCommandBuffer                                     (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5218                 const Unique<VkCommandBuffer>                           readImagesToBuffersCommandBuffer        (allocateCommandBuffer(vk, device, *commandBufferPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
5219
5220                 vector<de::SharedPtr<AttachmentResources> >     attachmentResources;
5221                 vector<de::SharedPtr<SubpassRenderer> >         subpassRenderers;
5222                 vector<VkImage>                                                         attachmentImages;
5223                 vector<VkImageView>                                                     attachmentViews;
5224                 vector<pair<VkImageView, VkImageView> >         inputAttachmentViews;
5225
5226                 Move<VkRenderPass> renderPass;
5227                 if (config.groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
5228                         renderPass = createRenderPass(vk, device, renderPassInfo, config.groupParams->renderingType);
5229
5230                 for (size_t attachmentNdx = 0; attachmentNdx < renderPassInfo.getAttachments().size(); attachmentNdx++)
5231                 {
5232                         const Attachment&       attachmentInfo  = renderPassInfo.getAttachments()[attachmentNdx];
5233
5234                         attachmentResources.push_back(de::SharedPtr<AttachmentResources>(new AttachmentResources(vki, physDevice, vk, device, allocator, queueIndex, targetSize, attachmentInfo, attachmentImageUsage[attachmentNdx], config.allocationKind)));
5235                         attachmentViews.push_back(attachmentResources[attachmentNdx]->getAttachmentView());
5236                         attachmentImages.push_back(attachmentResources[attachmentNdx]->getImage());
5237
5238                         inputAttachmentViews.push_back(attachmentResources[attachmentNdx]->getInputAttachmentViews());
5239                 }
5240
5241                 beginCommandBuffer(vk, *initializeImagesCommandBuffer, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5242                 pushImageInitializationCommands(vk, *initializeImagesCommandBuffer, renderPassInfo.getAttachments(), attachmentResources, queueIndex, imageClearValues);
5243                 endCommandBuffer(vk, *initializeImagesCommandBuffer);
5244
5245                 {
5246                         Move<VkFramebuffer> framebuffer;
5247                         if (config.groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
5248                                 framebuffer = createFramebuffer(vk, device, *renderPass, targetSize, attachmentViews);
5249
5250                         const VkRect2D renderArea
5251                         {
5252                                 { (deInt32)renderPos.x(),       (deInt32)renderPos.y()  },
5253                                 { renderSize.x(),                       renderSize.y()                  }
5254                         };
5255                         const bool dynamicRendering = (config.groupParams->renderingType == RENDERING_TYPE_DYNAMIC_RENDERING);
5256                         const bool secondaryCmdBufferCompletelyContainsDynamicRenderpass = (config.commandBufferTypes == TestConfig::COMMANDBUFFERTYPES_SECONDARY) &&
5257                                                                                                                                                                 config.groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass;
5258
5259                         for (size_t subpassNdx = 0; subpassNdx < renderPassInfo.getSubpasses().size(); subpassNdx++)
5260                         {
5261                                 subpassRenderers.push_back(de::SharedPtr<SubpassRenderer>(new SubpassRenderer(context, vk, device, allocator, renderPassInfo, attachmentResources,
5262                                                                                                                                                                                           renderArea, renderPassClearValues, *renderPass, *framebuffer,
5263                                                                                                                                                                                           *commandBufferPool, queueIndex, attachmentImages, inputAttachmentViews,
5264                                                                                                                                                                                           subpassRenderInfo[subpassNdx], config.allocationKind, dynamicRendering,
5265                                                                                                                                                                                           secondaryCmdBufferCompletelyContainsDynamicRenderpass)));
5266                         }
5267
5268                         beginCommandBuffer(vk, *renderCommandBuffer, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5269                         pushRenderPassCommands(vk, *renderCommandBuffer, *renderPass, renderPassInfo, attachmentResources, *framebuffer, subpassRenderers, renderArea,
5270                                                                    renderPassClearValues, queueIndex, config.renderTypes, config.groupParams->renderingType, secondaryCmdBufferCompletelyContainsDynamicRenderpass);
5271                         endCommandBuffer(vk, *renderCommandBuffer);
5272
5273                         beginCommandBuffer(vk, *readImagesToBuffersCommandBuffer, DE_NULL, 0, DE_NULL, VK_FALSE, (VkQueryControlFlags)0, (VkQueryPipelineStatisticFlags)0);
5274                         pushReadImagesToBuffers(vk, *readImagesToBuffersCommandBuffer, queueIndex, attachmentResources, renderPassInfo.getAttachments(), attachmentIsLazy, targetSize);
5275                         endCommandBuffer(vk, *readImagesToBuffersCommandBuffer);
5276                         {
5277                                 const VkCommandBuffer commandBuffers[] =
5278                                 {
5279                                         *initializeImagesCommandBuffer,
5280                                         *renderCommandBuffer,
5281                                         *readImagesToBuffersCommandBuffer
5282                                 };
5283                                 const Unique<VkFence>   fence           (createFence(vk, device, 0u));
5284
5285                                 queueSubmit(vk, queue, DE_LENGTH_OF_ARRAY(commandBuffers), commandBuffers, *fence);
5286                                 waitForFences(vk, device, 1, &fence.get(), VK_TRUE, ~0ull);
5287                         }
5288                 }
5289 #ifdef CTS_USES_VULKANSC
5290                 if (!context.getTestContext().getCommandLine().isSubProcess())
5291                         return tcu::TestStatus::pass("Pass");
5292 #endif
5293                 if (logAndVerifyImages(log, vk, device, attachmentResources, attachmentIsLazy, renderPassInfo, renderPassClearValues, imageClearValues, subpassRenderInfo, targetSize, config))
5294                         return tcu::TestStatus::pass("Pass");
5295                 else
5296                         return tcu::TestStatus::fail("Result verification failed");
5297         }
5298 }
5299
5300 static const VkFormat s_coreColorFormats[] =
5301 {
5302         VK_FORMAT_R5G6B5_UNORM_PACK16,
5303         VK_FORMAT_R8_UNORM,
5304         VK_FORMAT_R8_SNORM,
5305         VK_FORMAT_R8_UINT,
5306         VK_FORMAT_R8_SINT,
5307         VK_FORMAT_R8G8_UNORM,
5308         VK_FORMAT_R8G8_SNORM,
5309         VK_FORMAT_R8G8_UINT,
5310         VK_FORMAT_R8G8_SINT,
5311         VK_FORMAT_R8G8B8A8_UNORM,
5312         VK_FORMAT_R8G8B8A8_SNORM,
5313         VK_FORMAT_R8G8B8A8_UINT,
5314         VK_FORMAT_R8G8B8A8_SINT,
5315         VK_FORMAT_R8G8B8A8_SRGB,
5316         VK_FORMAT_A8B8G8R8_UNORM_PACK32,
5317         VK_FORMAT_A8B8G8R8_SNORM_PACK32,
5318         VK_FORMAT_A8B8G8R8_UINT_PACK32,
5319         VK_FORMAT_A8B8G8R8_SINT_PACK32,
5320         VK_FORMAT_A8B8G8R8_SRGB_PACK32,
5321         VK_FORMAT_B8G8R8A8_UNORM,
5322         VK_FORMAT_B8G8R8A8_SRGB,
5323         VK_FORMAT_A2R10G10B10_UNORM_PACK32,
5324         VK_FORMAT_A2B10G10R10_UNORM_PACK32,
5325         VK_FORMAT_A2B10G10R10_UINT_PACK32,
5326         VK_FORMAT_R16_UNORM,
5327         VK_FORMAT_R16_SNORM,
5328         VK_FORMAT_R16_UINT,
5329         VK_FORMAT_R16_SINT,
5330         VK_FORMAT_R16_SFLOAT,
5331         VK_FORMAT_R16G16_UNORM,
5332         VK_FORMAT_R16G16_SNORM,
5333         VK_FORMAT_R16G16_UINT,
5334         VK_FORMAT_R16G16_SINT,
5335         VK_FORMAT_R16G16_SFLOAT,
5336         VK_FORMAT_R16G16B16A16_UNORM,
5337         VK_FORMAT_R16G16B16A16_SNORM,
5338         VK_FORMAT_R16G16B16A16_UINT,
5339         VK_FORMAT_R16G16B16A16_SINT,
5340         VK_FORMAT_R16G16B16A16_SFLOAT,
5341         VK_FORMAT_R32_UINT,
5342         VK_FORMAT_R32_SINT,
5343         VK_FORMAT_R32_SFLOAT,
5344         VK_FORMAT_R32G32_UINT,
5345         VK_FORMAT_R32G32_SINT,
5346         VK_FORMAT_R32G32_SFLOAT,
5347         VK_FORMAT_R32G32B32A32_UINT,
5348         VK_FORMAT_R32G32B32A32_SINT,
5349         VK_FORMAT_R32G32B32A32_SFLOAT
5350 };
5351
5352 static const VkFormat s_coreDepthStencilFormats[] =
5353 {
5354         VK_FORMAT_D16_UNORM,
5355
5356         VK_FORMAT_X8_D24_UNORM_PACK32,
5357         VK_FORMAT_D32_SFLOAT,
5358
5359         VK_FORMAT_D24_UNORM_S8_UINT,
5360         VK_FORMAT_D32_SFLOAT_S8_UINT
5361 };
5362
5363 void addAttachmentTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5364 {
5365         const deUint32 attachmentCounts[] = { 1, 3, 4, 8 };
5366         const VkAttachmentLoadOp loadOps[] =
5367         {
5368                 VK_ATTACHMENT_LOAD_OP_LOAD,
5369                 VK_ATTACHMENT_LOAD_OP_CLEAR,
5370                 VK_ATTACHMENT_LOAD_OP_DONT_CARE
5371         };
5372
5373         const VkAttachmentStoreOp storeOps[] =
5374         {
5375                 VK_ATTACHMENT_STORE_OP_STORE,
5376                 VK_ATTACHMENT_STORE_OP_DONT_CARE
5377         };
5378
5379         const VkImageLayout initialAndFinalColorLayouts[] =
5380         {
5381                 VK_IMAGE_LAYOUT_GENERAL,
5382                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5383                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5384                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5385                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5386         };
5387
5388         const VkImageLayout initialAndFinalColorLayoutsLazy[] =
5389         {
5390                 VK_IMAGE_LAYOUT_GENERAL,
5391                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5392                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5393         };
5394
5395         const VkImageLayout initialAndFinalDepthStencilLayouts[] =
5396         {
5397                 VK_IMAGE_LAYOUT_GENERAL,
5398                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5399                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5400                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5401                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5402                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5403         };
5404
5405         const VkImageLayout initialAndFinalDepthStencilLayoutsLazy[] =
5406         {
5407                 VK_IMAGE_LAYOUT_GENERAL,
5408                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5409                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5410                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5411         };
5412
5413         const VkImageLayout subpassLayouts[] =
5414         {
5415                 VK_IMAGE_LAYOUT_GENERAL,
5416                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
5417         };
5418
5419         const VkImageLayout depthStencilLayouts[] =
5420         {
5421                 VK_IMAGE_LAYOUT_GENERAL,
5422                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5423         };
5424
5425         const TestConfig::RenderTypes renderCommands[] =
5426         {
5427                 TestConfig::RENDERTYPES_NONE,
5428                 TestConfig::RENDERTYPES_CLEAR,
5429                 TestConfig::RENDERTYPES_DRAW,
5430                 TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW,
5431         };
5432
5433         const TestConfig::CommandBufferTypes commandBuffers[] =
5434         {
5435                 TestConfig::COMMANDBUFFERTYPES_INLINE,
5436                 TestConfig::COMMANDBUFFERTYPES_SECONDARY,
5437                 TestConfig::COMMANDBUFFERTYPES_INLINE|TestConfig::COMMANDBUFFERTYPES_SECONDARY
5438         };
5439
5440         const TestConfig::ImageMemory imageMemories[] =
5441         {
5442                 TestConfig::IMAGEMEMORY_STRICT,
5443                 TestConfig::IMAGEMEMORY_LAZY,
5444                 TestConfig::IMAGEMEMORY_STRICT|TestConfig::IMAGEMEMORY_LAZY
5445         };
5446
5447         const UVec2 targetSizes[] =
5448         {
5449                 UVec2(64, 64),
5450                 UVec2(63, 65)
5451         };
5452
5453         const UVec2 renderPositions[] =
5454         {
5455                 UVec2(0, 0),
5456                 UVec2(3, 17)
5457         };
5458
5459         const UVec2 renderSizes[] =
5460         {
5461                 UVec2(32, 32),
5462                 UVec2(60, 47)
5463         };
5464
5465         tcu::TestContext&       testCtx                                 (group->getTestContext());
5466         bool                            useDynamicRendering             (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_DYNAMIC_RENDERING);
5467         de::Random                      rng                                             (1433774382u);
5468
5469         for (size_t attachmentCountNdx = 0; attachmentCountNdx < DE_LENGTH_OF_ARRAY(attachmentCounts); attachmentCountNdx++)
5470         {
5471                 const deUint32                                  attachmentCount                 = attachmentCounts[attachmentCountNdx];
5472                 const deUint32                                  testCaseCount                   = (attachmentCount == 1 ? 100 : 200);
5473                 de::MovePtr<tcu::TestCaseGroup> attachmentCountGroup    (new tcu::TestCaseGroup(testCtx, de::toString(attachmentCount).c_str(), de::toString(attachmentCount).c_str()));
5474
5475                 for (size_t testCaseNdx = 0; testCaseNdx < testCaseCount; testCaseNdx++)
5476                 {
5477                         const bool                                              useDepthStencil         = rng.getBool();
5478                         const TestConfig::ImageMemory   imageMemory                     = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
5479                         VkImageLayout                                   depthStencilLayout      = VK_IMAGE_LAYOUT_GENERAL;
5480                         vector<Attachment>                              attachments;
5481                         vector<AttachmentReference>             colorAttachmentReferences;
5482
5483                         // we want to make sure that dynamic rendering test cases have corresponding renderpass
5484                         // cases as this will allow drivers to easily compare GPU batches; since configurations
5485                         // for those tests are generated we need to generate configurations for all cases
5486                         // even when we know earlier that for dynamic rendering we will skip it
5487                         bool executeForDynamicRendering = true;
5488
5489                         for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
5490                         {
5491                                 const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
5492                                 const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
5493                                 const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5494                                 const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5495
5496                                 const VkImageLayout                     initialLayout   = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5497                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts))
5498                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayoutsLazy), DE_ARRAY_END(initialAndFinalColorLayoutsLazy));
5499                                 VkImageLayout                           finalizeLayout  = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5500                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts))
5501                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayoutsLazy), DE_ARRAY_END(initialAndFinalColorLayoutsLazy));
5502                                 const VkImageLayout                     subpassLayout   = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayouts), DE_ARRAY_END(subpassLayouts));
5503
5504                                 const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5505                                 const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5506
5507                                 if (useDynamicRendering)
5508                                 {
5509                                         // with renderpass we can have automatic layout transitions; to do the same with dynamic rendering cases
5510                                         // we would need to add addtional barries but since those tests won't add coverage we are skipping them
5511                                         if ((initialLayout == VK_IMAGE_LAYOUT_GENERAL) ||
5512                                                 (initialLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL))
5513                                                 finalizeLayout = initialLayout;
5514                                         else
5515                                                 executeForDynamicRendering = false;
5516                                 }
5517
5518                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5519                                 colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
5520                         }
5521
5522                         if (useDepthStencil)
5523                         {
5524                                 const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
5525                                 const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreDepthStencilFormats), DE_ARRAY_END(s_coreDepthStencilFormats));
5526                                 const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5527                                 const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5528
5529                                 const VkImageLayout                     initialLayout   = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5530                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5531                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayoutsLazy), DE_ARRAY_END(initialAndFinalDepthStencilLayoutsLazy));
5532                                 VkImageLayout                           finalizeLayout  = (imageMemory == TestConfig::IMAGEMEMORY_STRICT)
5533                                                                                                                                 ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5534                                                                                                                                 : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayoutsLazy), DE_ARRAY_END(initialAndFinalDepthStencilLayoutsLazy));
5535
5536                                 const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5537                                 const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5538
5539                                 if (useDynamicRendering)
5540                                 {
5541                                         if ((initialLayout == VK_IMAGE_LAYOUT_GENERAL) ||
5542                                                 (initialLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) ||
5543                                                 (initialLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL))
5544                                                 finalizeLayout = initialLayout;
5545                                         else
5546                                                 executeForDynamicRendering = false;
5547                                 }
5548
5549                                 depthStencilLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(depthStencilLayouts), DE_ARRAY_END(depthStencilLayouts));
5550                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5551                         }
5552
5553                         {
5554                                 const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
5555                                 const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
5556                                 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>()));
5557                                 const vector<SubpassDependency>                 deps;
5558                                 const string                                                    testCaseName    = de::toString(attachmentCountNdx * testCaseCount + testCaseNdx);
5559                                 const RenderPass                                                renderPass              (attachments, subpasses, deps);
5560                                 const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
5561                                 const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
5562                                 const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
5563
5564                                 if (useDynamicRendering)
5565                                 {
5566                                         // skip dynamic rendering cases (that don't add coverage) this can be done not earlier than after grabbing all
5567                                         // random numbers as we need to make sure that those tests that will be created for dynamic rendering have
5568                                         // corresponding renderpass tests with the same name
5569                                         if (!executeForDynamicRendering)
5570                                                 continue;
5571
5572                                         // dont repeat non secondary buffer cases when testing secondaryCmdBufferCompletelyContainsDynamicRenderpass flag
5573                                         if (testConfigExternal.groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass &&
5574                                                 (commandBuffer != TestConfig::COMMANDBUFFERTYPES_SECONDARY))
5575                                         {
5576                                                 continue;
5577                                         }
5578                                 }
5579
5580                                 const TestConfig                                                testConfig              (renderPass,
5581                                                                                                                                                  render,
5582                                                                                                                                                  commandBuffer,
5583                                                                                                                                                  imageMemory,
5584                                                                                                                                                  targetSize,
5585                                                                                                                                                  renderPos,
5586                                                                                                                                                  renderSize,
5587                                                                                                                                                  DE_FALSE,
5588                                                                                                                                                  1293809,
5589                                                                                                                                                  0,
5590                                                                                                                                                  testConfigExternal.allocationKind,
5591                                                                                                                                                  testConfigExternal.groupParams);
5592
5593                                 addFunctionCaseWithPrograms<TestConfig>(attachmentCountGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
5594                         }
5595                 }
5596
5597                 group->addChild(attachmentCountGroup.release());
5598         }
5599 }
5600
5601 void addAttachmentWriteMaskTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5602 {
5603         const deUint32 attachmentCounts[]       = { 1, 2, 3, 4, 8 };
5604
5605         const VkFormat attachmentFormats[]      =
5606         {
5607                 VK_FORMAT_R8G8B8A8_UINT,
5608                 VK_FORMAT_R8G8B8A8_UNORM,
5609                 VK_FORMAT_R5G6B5_UNORM_PACK16,
5610                 VK_FORMAT_R8G8_UNORM
5611         };
5612
5613         tcu::TestContext&       testCtx                 = group->getTestContext();
5614
5615         for (deUint32 attachmentCountNdx = 0; attachmentCountNdx < DE_LENGTH_OF_ARRAY(attachmentCounts); attachmentCountNdx++)
5616         {
5617                 const deUint32  attachmentCount = attachmentCounts[attachmentCountNdx];
5618                 const string    groupName               = "attachment_count_" + de::toString(attachmentCount);
5619
5620                 de::MovePtr<tcu::TestCaseGroup> attachmentCountGroup(new tcu::TestCaseGroup(testCtx, groupName.c_str(), de::toString(attachmentCount).c_str()));
5621
5622                 for (deUint32 drawStartNdx = 0; drawStartNdx < (attachmentCount); drawStartNdx++)
5623                 {
5624                         deUint32                                        formatNdx = 0;
5625                         vector<Attachment>                      attachments;
5626                         vector<AttachmentReference>     colorAttachmentReferences;
5627
5628                         for (deUint32 attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
5629                         {
5630                                 const VkFormat                          format                          = attachmentFormats[formatNdx];
5631                                 const VkSampleCountFlagBits     sampleCount                     = VK_SAMPLE_COUNT_1_BIT;
5632                                 const VkAttachmentLoadOp        loadOp                          = VK_ATTACHMENT_LOAD_OP_CLEAR;
5633                                 const VkAttachmentStoreOp       storeOp                         = VK_ATTACHMENT_STORE_OP_STORE;
5634                                 const VkAttachmentLoadOp        stencilLoadOp           = VK_ATTACHMENT_LOAD_OP_CLEAR;
5635                                 const VkAttachmentStoreOp       stencilStoreOp          = VK_ATTACHMENT_STORE_OP_STORE;
5636                                 const VkImageLayout                     initialLayout           = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5637                                 const VkImageLayout                     finalizeLayout          = (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
5638                                                                                                                                         ? initialLayout : VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
5639                                 const VkImageLayout                     subpassLayout           = VK_IMAGE_LAYOUT_GENERAL;
5640
5641                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5642                                 colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
5643
5644                                 if (++formatNdx == DE_LENGTH_OF_ARRAY(attachmentFormats))
5645                                         formatNdx = 0;
5646                         }
5647
5648                         {
5649                                 const VkImageLayout                                             depthStencilLayout      = VK_IMAGE_LAYOUT_GENERAL;
5650                                 const vector<Subpass>                                   subpass                         (1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u, vector<AttachmentReference>(), colorAttachmentReferences, vector<AttachmentReference>(), AttachmentReference(VK_ATTACHMENT_UNUSED, depthStencilLayout), vector<deUint32>()));
5651                                 const vector<SubpassDependency>                 deps;
5652
5653                                 const string                                                    testCaseName            = "start_index_" + de::toString(drawStartNdx);
5654                                 const RenderPass                                                renderPass                      (attachments, subpass, deps);
5655
5656                                 const TestConfig::RenderTypes                   render                          = TestConfig::RENDERTYPES_DRAW;
5657                                 const TestConfig::CommandBufferTypes    commandBuffer           = TestConfig::COMMANDBUFFERTYPES_INLINE;
5658                                 const TestConfig::ImageMemory                   imageMemory                     = TestConfig::IMAGEMEMORY_LAZY;
5659                                 const UVec2                                                             targetSize                      = UVec2(64, 64);
5660                                 const UVec2                                                             renderPos                       = UVec2(0, 0);
5661                                 const UVec2                                                             renderSize                      = UVec2(64, 64);
5662                                 const deBool                                                    useFormatCompCount      = DE_TRUE;
5663                                 const vector<DeviceCoreFeature>                 requiredFeatures        = {DEVICE_CORE_FEATURE_INDEPENDENT_BLEND};
5664                                 const TestConfig                                                testConfig                      (renderPass,
5665                                                                                                                                                          render,
5666                                                                                                                                                          commandBuffer,
5667                                                                                                                                                          imageMemory,
5668                                                                                                                                                          targetSize,
5669                                                                                                                                                          renderPos,
5670                                                                                                                                                          renderSize,
5671                                                                                                                                                          useFormatCompCount,
5672                                                                                                                                                          1293809,
5673                                                                                                                                                          drawStartNdx,
5674                                                                                                                                                          testConfigExternal.allocationKind,
5675                                                                                                                                                          testConfigExternal.groupParams,
5676                                                                                                                                                          requiredFeatures);
5677
5678                                 addFunctionCaseWithPrograms<TestConfig>(attachmentCountGroup.get(), testCaseName.c_str(), testCaseName.c_str(), checkSupport, createTestShaders, renderPassTest, testConfig);
5679                         }
5680                 }
5681
5682                 group->addChild(attachmentCountGroup.release());
5683         }
5684 }
5685
5686 template<typename T>
5687 T chooseRandom (de::Random& rng, const set<T>& values)
5688 {
5689         size_t                                                  ndx             = ((size_t)rng.getUint32()) % values.size();
5690         typename set<T>::const_iterator iter    = values.begin();
5691
5692         for (; ndx > 0; ndx--)
5693                 iter++;
5694
5695         return *iter;
5696 }
5697
5698 void addAttachmentAllocationTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
5699 {
5700         const deUint32 attachmentCounts[] = { 4, 8 };
5701         const VkAttachmentLoadOp loadOps[] =
5702         {
5703                 VK_ATTACHMENT_LOAD_OP_LOAD,
5704                 VK_ATTACHMENT_LOAD_OP_CLEAR,
5705                 VK_ATTACHMENT_LOAD_OP_DONT_CARE
5706         };
5707
5708         const VkAttachmentStoreOp storeOps[] =
5709         {
5710                 VK_ATTACHMENT_STORE_OP_STORE,
5711                 VK_ATTACHMENT_STORE_OP_DONT_CARE
5712         };
5713
5714         const VkImageLayout initialAndFinalColorLayouts[] =
5715         {
5716                 VK_IMAGE_LAYOUT_GENERAL,
5717                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
5718                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5719                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5720                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5721         };
5722
5723         const VkImageLayout initialAndFinalDepthStencilLayouts[] =
5724         {
5725                 VK_IMAGE_LAYOUT_GENERAL,
5726                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
5727                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
5728                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
5729                 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5730                 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
5731         };
5732
5733         const VkImageLayout subpassLayoutsColor[] =
5734         {
5735                 VK_IMAGE_LAYOUT_GENERAL,
5736                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
5737         };
5738
5739         const VkImageLayout subpassLayoutsDepthStencil[] =
5740         {
5741                 VK_IMAGE_LAYOUT_GENERAL,
5742                 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
5743         };
5744
5745         const VkImageLayout subpassLayoutsInput[] =
5746         {
5747                 VK_IMAGE_LAYOUT_GENERAL,
5748                 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
5749         };
5750
5751         enum AllocationType
5752         {
5753                 // Each pass uses one more attachmen than previous one
5754                 ALLOCATIONTYPE_GROW,
5755                 // Each pass uses one less attachment than previous one
5756                 ALLOCATIONTYPE_SHRINK,
5757                 // Each pass drops one attachment and picks up new one
5758                 ALLOCATIONTYPE_ROLL,
5759                 // Start by growing and end by shrinking
5760                 ALLOCATIONTYPE_GROW_SHRINK,
5761                 // Each subpass has single input and single output attachment
5762                 ALLOCATIONTYPE_IO_CHAIN,
5763                 // Each subpass has multiple inputs and multiple outputs attachment
5764                 ALLOCATIONTYPE_IO_GENERIC
5765         };
5766
5767         const AllocationType allocationTypes[] =
5768         {
5769                 ALLOCATIONTYPE_GROW,
5770                 ALLOCATIONTYPE_SHRINK,
5771                 ALLOCATIONTYPE_ROLL,
5772                 ALLOCATIONTYPE_GROW_SHRINK,
5773                 ALLOCATIONTYPE_IO_CHAIN,
5774                 ALLOCATIONTYPE_IO_GENERIC
5775         };
5776
5777         const char* const allocationTypeStr[] =
5778         {
5779                 "grow",
5780                 "shrink",
5781                 "roll",
5782                 "grow_shrink",
5783                 "input_output_chain",
5784                 "input_output",
5785         };
5786
5787         const TestConfig::RenderTypes renderCommands[] =
5788         {
5789                 TestConfig::RENDERTYPES_NONE,
5790                 TestConfig::RENDERTYPES_CLEAR,
5791                 TestConfig::RENDERTYPES_DRAW,
5792                 TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW,
5793         };
5794
5795         const TestConfig::CommandBufferTypes commandBuffers[] =
5796         {
5797                 TestConfig::COMMANDBUFFERTYPES_INLINE,
5798                 TestConfig::COMMANDBUFFERTYPES_SECONDARY,
5799                 TestConfig::COMMANDBUFFERTYPES_INLINE|TestConfig::COMMANDBUFFERTYPES_SECONDARY
5800         };
5801
5802         const TestConfig::ImageMemory imageMemories[] =
5803         {
5804                 TestConfig::IMAGEMEMORY_STRICT,
5805                 TestConfig::IMAGEMEMORY_LAZY,
5806                 TestConfig::IMAGEMEMORY_STRICT|TestConfig::IMAGEMEMORY_LAZY
5807         };
5808
5809         const UVec2 targetSizes[] =
5810         {
5811                 UVec2(64, 64),
5812                 UVec2(63, 65)
5813         };
5814
5815         const UVec2 renderPositions[] =
5816         {
5817                 UVec2(0, 0),
5818                 UVec2(3, 17)
5819         };
5820
5821         const UVec2 renderSizes[] =
5822         {
5823                 UVec2(32, 32),
5824                 UVec2(60, 47)
5825         };
5826
5827         tcu::TestContext&                               testCtx = group->getTestContext();
5828         de::Random                                              rng             (3700649827u);
5829
5830         for (size_t allocationTypeNdx = 0; allocationTypeNdx < DE_LENGTH_OF_ARRAY(allocationTypes); allocationTypeNdx++)
5831         {
5832                 const AllocationType                    allocationType          = allocationTypes[allocationTypeNdx];
5833                 const size_t                                    testCaseCount           = 100;
5834                 de::MovePtr<tcu::TestCaseGroup> allocationTypeGroup     (new tcu::TestCaseGroup(testCtx, allocationTypeStr[allocationTypeNdx], allocationTypeStr[allocationTypeNdx]));
5835
5836                 for (size_t testCaseNdx = 0; testCaseNdx < testCaseCount; testCaseNdx++)
5837                 {
5838                         if (allocationType == ALLOCATIONTYPE_IO_GENERIC)
5839                         {
5840                                 const deUint32          attachmentCount = 4u + rng.getUint32() % 31u;
5841                                 const deUint32          subpassCount    = 4u + rng.getUint32() % 31u;
5842                                 vector<Attachment>      attachments;
5843
5844                                 set<deUint32>           definedAttachments;
5845
5846                                 vector<Subpass>         subpasses;
5847                                 set<deUint32>           colorAttachments;
5848                                 set<deUint32>           depthStencilAttachments;
5849
5850                                 for (deUint32 attachmentIndex = 0; attachmentIndex < attachmentCount; attachmentIndex++)
5851                                 {
5852                                         const bool                                      isDepthStencilAttachment        = rng.getFloat() < 0.01f;
5853                                         const VkSampleCountFlagBits     sampleCount                                     = VK_SAMPLE_COUNT_1_BIT;
5854                                         const VkAttachmentLoadOp        loadOp                                          = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5855                                         const VkAttachmentStoreOp       storeOp                                         = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5856
5857                                         const VkImageLayout                     initialLayout                           = isDepthStencilAttachment
5858                                                                                                                                                         ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5859                                                                                                                                                         : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
5860                                         const VkImageLayout                     finalizeLayout                          = isDepthStencilAttachment
5861                                                                                                                                                         ? rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalDepthStencilLayouts), DE_ARRAY_END(initialAndFinalDepthStencilLayouts))
5862                                                                                                                                                         : rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
5863
5864                                         const VkAttachmentLoadOp        stencilLoadOp                           = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
5865                                         const VkAttachmentStoreOp       stencilStoreOp                          = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
5866
5867                                         if (isDepthStencilAttachment)
5868                                         {
5869                                                 const VkFormat  format  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreDepthStencilFormats), DE_ARRAY_END(s_coreDepthStencilFormats));
5870
5871                                                 if (loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR
5872                                                         || stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD || stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
5873                                                         definedAttachments.insert(attachmentIndex);
5874
5875                                                 depthStencilAttachments.insert(attachmentIndex);
5876
5877                                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5878                                         }
5879                                         else
5880                                         {
5881                                                 const VkFormat  format  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
5882
5883                                                 if (loadOp == VK_ATTACHMENT_LOAD_OP_LOAD || loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
5884                                                         definedAttachments.insert(attachmentIndex);
5885
5886                                                 colorAttachments.insert(attachmentIndex);
5887
5888                                                 attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
5889                                         }
5890                                 }
5891                                 vector<Maybe<deUint32> >        lastUseOfAttachment     (attachments.size(), tcu::Nothing);
5892                                 vector<SubpassDependency>       deps;
5893
5894                                 for (deUint32 subpassIndex = 0; subpassIndex < subpassCount; subpassIndex++)
5895                                 {
5896                                         const deUint32                          colorAttachmentCount            = depthStencilAttachments.empty()
5897                                                                                                                                                         ? 1 + rng.getUint32() % de::min(4u, (deUint32)colorAttachments.size())
5898                                                                                                                                                         : rng.getUint32() % (de::min(4u, (deUint32)colorAttachments.size()) + 1u);
5899                                         const deUint32                          inputAttachmentCount            = rng.getUint32() % (deUint32)(de::min<size_t>(4, definedAttachments.size()) + 1);
5900                                         const bool                                      useDepthStencilAttachment       = !depthStencilAttachments.empty() && (colorAttachmentCount == 0 || rng.getBool());
5901                                         std::vector<deUint32>           subpassColorAttachments         (colorAttachmentCount);
5902                                         std::vector<deUint32>           subpassInputAttachments         (inputAttachmentCount);
5903                                         Maybe<deUint32>                         depthStencilAttachment          (useDepthStencilAttachment
5904                                                                                                                                                         ? just(chooseRandom(rng, depthStencilAttachments))
5905                                                                                                                                                         : tcu::Nothing);
5906                                         std::vector<deUint32>           subpassPreserveAttachments;
5907
5908                                         rng.choose(colorAttachments.begin(), colorAttachments.end(), subpassColorAttachments.begin(), colorAttachmentCount);
5909                                         rng.choose(definedAttachments.begin(), definedAttachments.end(), subpassInputAttachments.begin(), inputAttachmentCount);
5910
5911                                         for (size_t colorAttachmentNdx = 0; colorAttachmentNdx < subpassColorAttachments.size(); colorAttachmentNdx++)
5912                                                 definedAttachments.insert(subpassColorAttachments[colorAttachmentNdx]);
5913
5914                                         if (depthStencilAttachment)
5915                                                 definedAttachments.insert(*depthStencilAttachment);
5916
5917                                         {
5918                                                 std::vector<AttachmentReference>        inputAttachmentReferences;
5919                                                 std::vector<AttachmentReference>        colorAttachmentReferences;
5920                                                 AttachmentReference                                     depthStencilAttachmentReference (VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL);
5921
5922                                                 for (size_t colorAttachmentNdx = 0; colorAttachmentNdx < subpassColorAttachments.size(); colorAttachmentNdx++)
5923                                                 {
5924                                                         const deUint32          colorAttachmentIndex    = subpassColorAttachments[colorAttachmentNdx];
5925
5926                                                         if (lastUseOfAttachment[colorAttachmentIndex])
5927                                                         {
5928                                                                 deBool foundDuplicate = false;
5929
5930                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[colorAttachmentIndex];
5931                                                                 const deUint32                  dstPass                 = subpassIndex;
5932                                                                 const VkDependencyFlags dependencyFlags = rng.getBool() ? (VkDependencyFlags) VK_DEPENDENCY_BY_REGION_BIT : 0u;
5933
5934                                                                 const SubpassDependency newDependency(srcPass, dstPass,
5935                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5936                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5937                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5938                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5939
5940                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5941                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5942                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5943                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5944
5945                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
5946                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
5947
5948                                                                                                                                           dependencyFlags);
5949
5950                                                                 for (SubpassDependency& dependency : deps)
5951                                                                 {
5952                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
5953                                                                         {
5954                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
5955                                                                                 dependency.setDstAccessMask(newDstFlags);
5956                                                                                 foundDuplicate = true;
5957                                                                                 break;
5958                                                                         }
5959                                                                 }
5960
5961                                                                 if (!foundDuplicate)
5962                                                                 {
5963                                                                         deps.push_back(newDependency);
5964                                                                 }
5965                                                         }
5966
5967                                                         lastUseOfAttachment[colorAttachmentIndex] = just(subpassIndex);
5968
5969                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)subpassColorAttachments[colorAttachmentNdx], VK_IMAGE_LAYOUT_GENERAL));
5970                                                 }
5971
5972                                                 for (size_t inputAttachmentNdx = 0; inputAttachmentNdx < subpassInputAttachments.size(); inputAttachmentNdx++)
5973                                                 {
5974                                                         const deUint32          inputAttachmentIndex    = subpassInputAttachments[inputAttachmentNdx];
5975
5976                                                         if(lastUseOfAttachment[inputAttachmentIndex])
5977                                                         {
5978                                                                 deBool foundDuplicate = false;
5979
5980                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[inputAttachmentIndex];
5981                                                                 const deUint32                  dstPass                 = subpassIndex;
5982                                                                 const VkDependencyFlags dependencyFlags = ((srcPass == subpassIndex) || rng.getBool()) ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u;
5983
5984                                                                 const SubpassDependency newDependency(srcPass, dstPass,
5985                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5986                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5987                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5988                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5989
5990                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
5991                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
5992                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
5993                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
5994
5995                                                                                                                                           VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
5996                                                                                                                                           VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
5997
5998                                                                                                                                           dependencyFlags);
5999                                                                 for (SubpassDependency& dependency : deps)
6000                                                                 {
6001                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
6002                                                                         {
6003                                                                                 const VkAccessFlags newSrcFlags = dependency.getSrcAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
6004                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
6005                                                                                 dependency.setDstAccessMask(newSrcFlags);
6006                                                                                 dependency.setDstAccessMask(newDstFlags);
6007                                                                                 foundDuplicate = true;
6008                                                                                 break;
6009                                                                         }
6010                                                                 }
6011
6012                                                                 if (!foundDuplicate)
6013                                                                 {
6014                                                                         deps.push_back(newDependency);
6015                                                                 }
6016
6017                                                                 lastUseOfAttachment[inputAttachmentIndex] = just(subpassIndex);
6018
6019                                                                 VkImageAspectFlags aspect = 0u;
6020                                                                 if (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2)
6021                                                                 {
6022                                                                         bool col = colorAttachments.find(inputAttachmentIndex) != colorAttachments.end();
6023                                                                         aspect = col ? VK_IMAGE_ASPECT_COLOR_BIT : VK_IMAGE_ASPECT_DEPTH_BIT;
6024                                                                 }
6025                                                                 inputAttachmentReferences.push_back(AttachmentReference((deUint32)subpassInputAttachments[inputAttachmentNdx], VK_IMAGE_LAYOUT_GENERAL, aspect));
6026                                                         }
6027                                                 }
6028
6029                                                 if (depthStencilAttachment)
6030                                                 {
6031                                                         if (lastUseOfAttachment[*depthStencilAttachment])
6032                                                         {
6033                                                                 deBool foundDuplicate = false;
6034
6035                                                                 const deUint32                  srcPass                 = *lastUseOfAttachment[*depthStencilAttachment];
6036                                                                 const deUint32                  dstPass                 = subpassIndex;
6037                                                                 const VkDependencyFlags dependencyFlags = ((srcPass == subpassIndex) || rng.getBool()) ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u;
6038
6039                                                                 const SubpassDependency newDependency(srcPass, dstPass,
6040                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6041                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6042                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6043                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6044
6045                                                                                                                                           VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6046                                                                                                                                           | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6047                                                                                                                                           | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6048                                                                                                                                           | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6049
6050                                                                                                                                           VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
6051                                                                                                                                           VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT
6052                                                                                                                                           | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6053
6054                                                                                                                                           dependencyFlags);
6055                                                                 for (SubpassDependency& dependency : deps)
6056                                                                 {
6057                                                                         if (dependency.getSrcPass() == srcPass && dependency.getDstPass() == dstPass)
6058                                                                         {
6059                                                                                 const VkAccessFlags newSrcFlags = dependency.getSrcAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
6060                                                                                 const VkAccessFlags newDstFlags = dependency.getDstAccessMask() | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
6061                                                                                 dependency.setDstAccessMask(newSrcFlags);
6062                                                                                 dependency.setDstAccessMask(newDstFlags);
6063                                                                                 foundDuplicate = true;
6064                                                                                 break;
6065                                                                         }
6066                                                                 }
6067
6068                                                                 if (!foundDuplicate)
6069                                                                 {
6070                                                                         deps.push_back(newDependency);
6071                                                                 }
6072                                                         }
6073
6074                                                         lastUseOfAttachment[*depthStencilAttachment] = just(subpassIndex);
6075
6076                                                         depthStencilAttachmentReference = AttachmentReference(*depthStencilAttachment, VK_IMAGE_LAYOUT_GENERAL);
6077                                                 }
6078                                                 else
6079                                                         depthStencilAttachmentReference = AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL);
6080
6081                                                 vector<deUint32>        preserveAttachments;
6082                                                 for (deUint32 attachmentIndex = 0; attachmentIndex < (deUint32)attachments.size(); attachmentIndex++)
6083                                                 {
6084                                                         if (lastUseOfAttachment[attachmentIndex] && (*lastUseOfAttachment[attachmentIndex]) != subpassIndex)
6085                                                                 preserveAttachments.push_back(attachmentIndex);
6086                                                 }
6087
6088                                                 // Use random image layout when possible
6089                                                 for (size_t colorRefIdx = 0; colorRefIdx < colorAttachmentReferences.size(); ++colorRefIdx)
6090                                                 {
6091                                                         bool usedAsInput = false;
6092                                                         for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
6093                                                                 if (colorAttachmentReferences[colorRefIdx].getAttachment() == inputAttachmentReferences[inputRefIdx].getAttachment())
6094                                                                         usedAsInput = true;
6095
6096                                                         if (!usedAsInput)
6097                                                                 colorAttachmentReferences[colorRefIdx].setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)));
6098                                                 }
6099                                                 for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
6100                                                 {
6101                                                         bool usedAsDepthStencil = inputAttachmentReferences[inputRefIdx].getAttachment() == depthStencilAttachmentReference.getAttachment();
6102                                                         bool usedAsColor                = false;
6103                                                         for (size_t colorRefIdx = 0; colorRefIdx < colorAttachmentReferences.size(); ++colorRefIdx)
6104                                                                 if (inputAttachmentReferences[inputRefIdx].getAttachment() == colorAttachmentReferences[colorRefIdx].getAttachment())
6105                                                                         usedAsColor = true;
6106
6107                                                         if (!usedAsColor && !usedAsDepthStencil)
6108                                                                 inputAttachmentReferences[inputRefIdx].setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsInput), DE_ARRAY_END(subpassLayoutsInput)));
6109                                                 }
6110                                                 {
6111                                                         bool usedAsInput = false;
6112                                                         for (size_t inputRefIdx = 0; inputRefIdx < inputAttachmentReferences.size(); ++inputRefIdx)
6113                                                                 if (depthStencilAttachmentReference.getAttachment() == inputAttachmentReferences[inputRefIdx].getAttachment())
6114                                                                         usedAsInput = true;
6115
6116                                                         if (!usedAsInput)
6117                                                                 depthStencilAttachmentReference.setImageLayout(rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsDepthStencil), DE_ARRAY_END(subpassLayoutsDepthStencil)));
6118                                                 }
6119
6120                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6121                                                                                                 inputAttachmentReferences,
6122                                                                                                 colorAttachmentReferences,
6123                                                                                                 vector<AttachmentReference>(),
6124                                                                                                 depthStencilAttachmentReference,
6125                                                                                                 preserveAttachments));
6126                                         }
6127                                 }
6128                                 {
6129                                         const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
6130                                         const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
6131                                         const TestConfig::ImageMemory                   imageMemory             = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
6132
6133                                         const string                                                    testCaseName    = de::toString(testCaseNdx);
6134                                         const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
6135                                         const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
6136                                         const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
6137
6138                                         const RenderPass                                                renderPass              (attachments, subpasses, deps);
6139                                         const TestConfig                                                testConfig              (renderPass,
6140                                                                                                                                                          render,
6141                                                                                                                                                          commandBuffer,
6142                                                                                                                                                          imageMemory,
6143                                                                                                                                                          targetSize,
6144                                                                                                                                                          renderPos,
6145                                                                                                                                                          renderSize,
6146                                                                                                                                                          DE_FALSE,
6147                                                                                                                                                          80329,
6148                                                                                                                                                          0,
6149                                                                                                                                                          testConfigExternal.allocationKind,
6150                                                                                                                                                          testConfigExternal.groupParams);
6151
6152                                         addFunctionCaseWithPrograms<TestConfig>(allocationTypeGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
6153                                 }
6154                         }
6155                         else
6156                         {
6157                                 const deUint32          attachmentCount = rng.choose<deUint32>(DE_ARRAY_BEGIN(attachmentCounts), DE_ARRAY_END(attachmentCounts));
6158                                 vector<Attachment>      attachments;
6159                                 vector<Subpass>         subpasses;
6160
6161                                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount; attachmentNdx++)
6162                                 {
6163                                         const VkSampleCountFlagBits     sampleCount             = VK_SAMPLE_COUNT_1_BIT;
6164                                         const VkFormat                          format                  = rng.choose<VkFormat>(DE_ARRAY_BEGIN(s_coreColorFormats), DE_ARRAY_END(s_coreColorFormats));
6165                                         const VkAttachmentLoadOp        loadOp                  = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
6166                                         const VkAttachmentStoreOp       storeOp                 = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
6167
6168                                         const VkImageLayout                     initialLayout   = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
6169                                         const VkImageLayout                     finalizeLayout  = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(initialAndFinalColorLayouts), DE_ARRAY_END(initialAndFinalColorLayouts));
6170
6171                                         const VkAttachmentLoadOp        stencilLoadOp   = rng.choose<VkAttachmentLoadOp>(DE_ARRAY_BEGIN(loadOps), DE_ARRAY_END(loadOps));
6172                                         const VkAttachmentStoreOp       stencilStoreOp  = rng.choose<VkAttachmentStoreOp>(DE_ARRAY_BEGIN(storeOps), DE_ARRAY_END(storeOps));
6173
6174                                         attachments.push_back(Attachment(format, sampleCount, loadOp, storeOp, stencilLoadOp, stencilStoreOp, initialLayout, finalizeLayout));
6175                                 }
6176
6177                                 if (allocationType == ALLOCATIONTYPE_GROW)
6178                                 {
6179                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6180                                         {
6181                                                 vector<AttachmentReference>     colorAttachmentReferences;
6182
6183                                                 for (size_t attachmentNdx = 0; attachmentNdx < subpassNdx + 1; attachmentNdx++)
6184                                                 {
6185                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6186
6187                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6188                                                 }
6189
6190                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6191                                                                                                 vector<AttachmentReference>(),
6192                                                                                                 colorAttachmentReferences,
6193                                                                                                 vector<AttachmentReference>(),
6194                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6195                                                                                                 vector<deUint32>()));
6196                                         }
6197                                 }
6198                                 else if (allocationType == ALLOCATIONTYPE_SHRINK)
6199                                 {
6200                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6201                                         {
6202                                                 vector<AttachmentReference>     colorAttachmentReferences;
6203
6204                                                 for (size_t attachmentNdx = 0; attachmentNdx < (attachmentCount - subpassNdx); attachmentNdx++)
6205                                                 {
6206                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6207
6208                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6209                                                 }
6210
6211                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6212                                                                                                         vector<AttachmentReference>(),
6213                                                                                                         colorAttachmentReferences,
6214                                                                                                         vector<AttachmentReference>(),
6215                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6216                                                                                                         vector<deUint32>()));
6217                                         }
6218                                 }
6219                                 else if (allocationType == ALLOCATIONTYPE_ROLL)
6220                                 {
6221                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount / 2; subpassNdx++)
6222                                         {
6223                                                 vector<AttachmentReference>     colorAttachmentReferences;
6224
6225                                                 for (size_t attachmentNdx = 0; attachmentNdx < attachmentCount / 2; attachmentNdx++)
6226                                                 {
6227                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6228
6229                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)(subpassNdx + attachmentNdx), subpassLayout));
6230                                                 }
6231
6232                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6233                                                                                                         vector<AttachmentReference>(),
6234                                                                                                         colorAttachmentReferences,
6235                                                                                                         vector<AttachmentReference>(),
6236                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6237                                                                                                         vector<deUint32>()));
6238                                         }
6239                                 }
6240                                 else if (allocationType == ALLOCATIONTYPE_GROW_SHRINK)
6241                                 {
6242                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6243                                         {
6244                                                 vector<AttachmentReference>     colorAttachmentReferences;
6245
6246                                                 for (size_t attachmentNdx = 0; attachmentNdx < subpassNdx + 1; attachmentNdx++)
6247                                                 {
6248                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6249
6250                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6251                                                 }
6252
6253                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6254                                                                                                         vector<AttachmentReference>(),
6255                                                                                                         colorAttachmentReferences,
6256                                                                                                         vector<AttachmentReference>(),
6257                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6258                                                                                                         vector<deUint32>()));
6259                                         }
6260                                         for (size_t subpassNdx = 0; subpassNdx < attachmentCount; subpassNdx++)
6261                                         {
6262                                                 vector<AttachmentReference>     colorAttachmentReferences;
6263
6264                                                 for (size_t attachmentNdx = 0; attachmentNdx < (attachmentCount - subpassNdx); attachmentNdx++)
6265                                                 {
6266                                                         const VkImageLayout subpassLayout = rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor));
6267
6268                                                         colorAttachmentReferences.push_back(AttachmentReference((deUint32)attachmentNdx, subpassLayout));
6269                                                 }
6270
6271                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6272                                                                                                         vector<AttachmentReference>(),
6273                                                                                                         colorAttachmentReferences,
6274                                                                                                         vector<AttachmentReference>(),
6275                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6276                                                                                                         vector<deUint32>()));
6277                                         }
6278                                 }
6279                                 else if (allocationType == ALLOCATIONTYPE_IO_CHAIN)
6280                                 {
6281                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6282                                                                                         vector<AttachmentReference>(),
6283                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)))),
6284                                                                                         vector<AttachmentReference>(),
6285                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6286                                                                                         vector<deUint32>()));
6287
6288                                         for (size_t subpassNdx = 1; subpassNdx < attachmentCount; subpassNdx++)
6289                                         {
6290                                                 const VkImageAspectFlags inputAttachmentAspectMask = (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2) ? VK_IMAGE_ASPECT_COLOR_BIT : static_cast<VkImageAspectFlagBits>(0);
6291                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS, 0u,
6292                                                                                                 vector<AttachmentReference>(1, AttachmentReference((deUint32)(subpassNdx - 1), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
6293                                                                                                 vector<AttachmentReference>(1, AttachmentReference((deUint32)(subpassNdx), rng.choose<VkImageLayout>(DE_ARRAY_BEGIN(subpassLayoutsColor), DE_ARRAY_END(subpassLayoutsColor)))),
6294                                                                                                 vector<AttachmentReference>(),
6295                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6296                                                                                                 vector<deUint32>()));
6297                                         }
6298                                 }
6299                                 else
6300                                         DE_FATAL("Unknown allocation type");
6301
6302                                 {
6303                                         const TestConfig::RenderTypes                   render                  = rng.choose<TestConfig::RenderTypes>(DE_ARRAY_BEGIN(renderCommands), DE_ARRAY_END(renderCommands));
6304                                         const TestConfig::CommandBufferTypes    commandBuffer   = rng.choose<TestConfig::CommandBufferTypes>(DE_ARRAY_BEGIN(commandBuffers), DE_ARRAY_END(commandBuffers));
6305                                         const TestConfig::ImageMemory                   imageMemory             = rng.choose<TestConfig::ImageMemory>(DE_ARRAY_BEGIN(imageMemories), DE_ARRAY_END(imageMemories));
6306
6307                                         const string                                                    testCaseName    = de::toString(testCaseNdx);
6308                                         const UVec2                                                             targetSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(targetSizes), DE_ARRAY_END(targetSizes));
6309                                         const UVec2                                                             renderPos               = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderPositions), DE_ARRAY_END(renderPositions));
6310                                         const UVec2                                                             renderSize              = rng.choose<UVec2>(DE_ARRAY_BEGIN(renderSizes), DE_ARRAY_END(renderSizes));
6311
6312                                         vector<SubpassDependency>                               deps;
6313
6314                                         for (size_t subpassNdx = 0; subpassNdx < subpasses.size() - 1; subpassNdx++)
6315                                         {
6316                                                 const bool byRegion                             = rng.getBool();
6317                                                 deps.push_back(SubpassDependency((deUint32)subpassNdx, (deUint32)subpassNdx + 1,
6318                                                                                                                  VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6319                                                                                                                         | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6320                                                                                                                         | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6321                                                                                                                         | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6322
6323                                                                                                                  VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
6324                                                                                                                         | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
6325                                                                                                                         | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
6326                                                                                                                         | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
6327
6328                                                                                                                  VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6329                                                                                                                  (VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT),
6330
6331                                                                                                                  byRegion ? (VkDependencyFlags)VK_DEPENDENCY_BY_REGION_BIT : 0u));
6332                                         }
6333
6334                                         const RenderPass                                        renderPass              (attachments, subpasses, deps);
6335                                         const TestConfig                                        testConfig              (renderPass,
6336                                                                                                                                                  render,
6337                                                                                                                                                  commandBuffer,
6338                                                                                                                                                  imageMemory,
6339                                                                                                                                                  targetSize,
6340                                                                                                                                                  renderPos,
6341                                                                                                                                                  renderSize,
6342                                                                                                                                                  DE_FALSE,
6343                                                                                                                                                  80329,
6344                                                                                                                                                  0,
6345                                                                                                                                                  testConfigExternal.allocationKind,
6346                                                                                                                                                  testConfigExternal.groupParams);
6347
6348                                         addFunctionCaseWithPrograms<TestConfig>(allocationTypeGroup.get(), testCaseName.c_str(), testCaseName.c_str(), createTestShaders, renderPassTest, testConfig);
6349                                 }
6350                         }
6351                 }
6352                 group->addChild(allocationTypeGroup.release());
6353         }
6354 }
6355
6356 void addSimpleTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
6357 {
6358         const UVec2     targetSize      (64, 64);
6359         const UVec2     renderPos       (0, 0);
6360         const UVec2     renderSize      (64, 64);
6361
6362         // color
6363         {
6364                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6365                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6366                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6367                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6368                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6369                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6370                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6371                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6372                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6373                                                                                                                                         0u,
6374                                                                                                                                         vector<AttachmentReference>(),
6375                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6376                                                                                                                                         vector<AttachmentReference>(),
6377                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6378                                                                                                                                         vector<deUint32>())),
6379                                                                                  vector<SubpassDependency>());
6380                 const TestConfig        testConfig      (renderPass,
6381                                                                                  TestConfig::RENDERTYPES_DRAW,
6382                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6383                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6384                                                                                  targetSize,
6385                                                                                  renderPos,
6386                                                                                  renderSize,
6387                                                                                  DE_FALSE,
6388                                                                                  90239,
6389                                                                                  0,
6390                                                                                  testConfigExternal.allocationKind,
6391                                                                                  testConfigExternal.groupParams);
6392
6393                 addFunctionCaseWithPrograms<TestConfig>(group, "color", "Single color attachment case.", createTestShaders, renderPassTest, testConfig);
6394         }
6395
6396         // depth
6397         {
6398                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_X8_D24_UNORM_PACK32,
6399                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6400                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6401                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6402                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6403                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6404                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6405                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6406                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6407                                                                                                                                         0u,
6408                                                                                                                                         vector<AttachmentReference>(),
6409                                                                                                                                         vector<AttachmentReference>(),
6410                                                                                                                                         vector<AttachmentReference>(),
6411                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6412                                                                                                                                         vector<deUint32>())),
6413                                                                                  vector<SubpassDependency>());
6414                 const TestConfig        testConfig      (renderPass,
6415                                                                                  TestConfig::RENDERTYPES_DRAW,
6416                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6417                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6418                                                                                  targetSize,
6419                                                                                  renderPos,
6420                                                                                  renderSize,
6421                                                                                  DE_FALSE,
6422                                                                                  90239,
6423                                                                                  0,
6424                                                                                  testConfigExternal.allocationKind,
6425                                                                                  testConfigExternal.groupParams);
6426
6427                 addFunctionCaseWithPrograms<TestConfig>(group, "depth", "Single depth attachment case.", createTestShaders, renderPassTest, testConfig);
6428         }
6429
6430         // stencil
6431         {
6432                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_S8_UINT,
6433                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6434                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6435                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6436                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6437                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6438                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6439                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6440                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6441                                                                                                                                         0u,
6442                                                                                                                                         vector<AttachmentReference>(),
6443                                                                                                                                         vector<AttachmentReference>(),
6444                                                                                                                                         vector<AttachmentReference>(),
6445                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6446                                                                                                                                         vector<deUint32>())),
6447                                                                                  vector<SubpassDependency>());
6448                 const TestConfig        testConfig      (renderPass,
6449                                                                                  TestConfig::RENDERTYPES_DRAW,
6450                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6451                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6452                                                                                  targetSize,
6453                                                                                  renderPos,
6454                                                                                  renderSize,
6455                                                                                  DE_FALSE,
6456                                                                                  90239,
6457                                                                                  0,
6458                                                                                  testConfigExternal.allocationKind,
6459                                                                                  testConfigExternal.groupParams);
6460
6461                 addFunctionCaseWithPrograms<TestConfig>(group, "stencil", "Single stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6462         }
6463
6464         // depth_stencil
6465         {
6466                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_D24_UNORM_S8_UINT,
6467                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6468                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6469                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6470                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6471                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6472                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6473                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
6474                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6475                                                                                                                                         0u,
6476                                                                                                                                         vector<AttachmentReference>(),
6477                                                                                                                                         vector<AttachmentReference>(),
6478                                                                                                                                         vector<AttachmentReference>(),
6479                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6480                                                                                                                                         vector<deUint32>())),
6481                                                                                  vector<SubpassDependency>());
6482                 const TestConfig        testConfig      (renderPass,
6483                                                                                  TestConfig::RENDERTYPES_DRAW,
6484                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6485                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6486                                                                                  targetSize,
6487                                                                                  renderPos,
6488                                                                                  renderSize,
6489                                                                                  DE_FALSE,
6490                                                                                  90239,
6491                                                                                  0,
6492                                                                                  testConfigExternal.allocationKind,
6493                                                                                  testConfigExternal.groupParams);
6494
6495                 addFunctionCaseWithPrograms<TestConfig>(group, "depth_stencil", "Single depth stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6496         }
6497
6498         // color_depth
6499         {
6500                 const Attachment        attachments[] =
6501                 {
6502                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6503                                            VK_SAMPLE_COUNT_1_BIT,
6504                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6505                                            VK_ATTACHMENT_STORE_OP_STORE,
6506                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6507                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6508                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6509                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6510                         Attachment(VK_FORMAT_X8_D24_UNORM_PACK32,
6511                                            VK_SAMPLE_COUNT_1_BIT,
6512                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6513                                            VK_ATTACHMENT_STORE_OP_STORE,
6514                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6515                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6516                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6517                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6518                 };
6519
6520                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6521                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6522                                                                                                                                         0u,
6523                                                                                                                                         vector<AttachmentReference>(),
6524                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6525                                                                                                                                         vector<AttachmentReference>(),
6526                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6527                                                                                                                                         vector<deUint32>())),
6528                                                                                  vector<SubpassDependency>());
6529                 const TestConfig        testConfig      (renderPass,
6530                                                                                  TestConfig::RENDERTYPES_DRAW,
6531                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6532                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6533                                                                                  targetSize,
6534                                                                                  renderPos,
6535                                                                                  renderSize,
6536                                                                                  DE_FALSE,
6537                                                                                  90239,
6538                                                                                  0,
6539                                                                                  testConfigExternal.allocationKind,
6540                                                                                  testConfigExternal.groupParams);
6541
6542                 addFunctionCaseWithPrograms<TestConfig>(group, "color_depth", "Color and depth attachment case.", createTestShaders, renderPassTest, testConfig);
6543         }
6544
6545         // color_stencil
6546         {
6547                 const Attachment        attachments[] =
6548                 {
6549                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6550                                            VK_SAMPLE_COUNT_1_BIT,
6551                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6552                                            VK_ATTACHMENT_STORE_OP_STORE,
6553                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6554                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6555                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6556                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6557                         Attachment(VK_FORMAT_S8_UINT,
6558                                            VK_SAMPLE_COUNT_1_BIT,
6559                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6560                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6561                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6562                                            VK_ATTACHMENT_STORE_OP_STORE,
6563                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6564                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6565                 };
6566
6567                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6568                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6569                                                                                                                                         0u,
6570                                                                                                                                         vector<AttachmentReference>(),
6571                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6572                                                                                                                                         vector<AttachmentReference>(),
6573                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6574                                                                                                                                         vector<deUint32>())),
6575                                                                                  vector<SubpassDependency>());
6576                 const TestConfig        testConfig      (renderPass,
6577                                                                                  TestConfig::RENDERTYPES_DRAW,
6578                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6579                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6580                                                                                  targetSize,
6581                                                                                  renderPos,
6582                                                                                  renderSize,
6583                                                                                  DE_FALSE,
6584                                                                                  90239,
6585                                                                                  0,
6586                                                                                  testConfigExternal.allocationKind,
6587                                                                                  testConfigExternal.groupParams);
6588
6589                 addFunctionCaseWithPrograms<TestConfig>(group, "color_stencil", "Color and stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6590         }
6591
6592         // color_depth_stencil
6593         {
6594                 const Attachment        attachments[] =
6595                 {
6596                         Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6597                                            VK_SAMPLE_COUNT_1_BIT,
6598                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6599                                            VK_ATTACHMENT_STORE_OP_STORE,
6600                                            VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6601                                            VK_ATTACHMENT_STORE_OP_DONT_CARE,
6602                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6603                                            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL),
6604                         Attachment(VK_FORMAT_D24_UNORM_S8_UINT,
6605                                            VK_SAMPLE_COUNT_1_BIT,
6606                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6607                                            VK_ATTACHMENT_STORE_OP_STORE,
6608                                            VK_ATTACHMENT_LOAD_OP_CLEAR,
6609                                            VK_ATTACHMENT_STORE_OP_STORE,
6610                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
6611                                            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6612                 };
6613
6614                 const RenderPass        renderPass      (vector<Attachment>(DE_ARRAY_BEGIN(attachments), DE_ARRAY_END(attachments)),
6615                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6616                                                                                                                                         0u,
6617                                                                                                                                         vector<AttachmentReference>(),
6618                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6619                                                                                                                                         vector<AttachmentReference>(),
6620                                                                                                                                         AttachmentReference(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
6621                                                                                                                                         vector<deUint32>())),
6622                                                                                  vector<SubpassDependency>());
6623                 const TestConfig        testConfig      (renderPass,
6624                                                                                  TestConfig::RENDERTYPES_DRAW,
6625                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6626                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6627                                                                                  targetSize,
6628                                                                                  renderPos,
6629                                                                                  renderSize,
6630                                                                                  DE_FALSE,
6631                                                                                  90239,
6632                                                                                  0,
6633                                                                                  testConfigExternal.allocationKind,
6634                                                                                  testConfigExternal.groupParams);
6635
6636                 addFunctionCaseWithPrograms<TestConfig>(group, "color_depth_stencil", "Color, depth and stencil attachment case.", createTestShaders, renderPassTest, testConfig);
6637         }
6638
6639         // no attachments
6640         {
6641                 const RenderPass        renderPass      (vector<Attachment>(),
6642                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6643                                                                                                                                         0u,
6644                                                                                                                                         vector<AttachmentReference>(),
6645                                                                                                                                         vector<AttachmentReference>(),
6646                                                                                                                                         vector<AttachmentReference>(),
6647                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6648                                                                                                                                         vector<deUint32>())),
6649                                                                                 vector<SubpassDependency>());
6650                 const TestConfig        testConfig      (renderPass,
6651                                                                                  TestConfig::RENDERTYPES_DRAW,
6652                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6653                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6654                                                                                  targetSize,
6655                                                                                  renderPos,
6656                                                                                  renderSize,
6657                                                                                  DE_FALSE,
6658                                                                                  90239,
6659                                                                                  0,
6660                                                                                  testConfigExternal.allocationKind,
6661                                                                                  testConfigExternal.groupParams);
6662
6663                 addFunctionCaseWithPrograms<TestConfig>(group, "no_attachments", "No attachments case.", createTestShaders, renderPassTest, testConfig);
6664         }
6665
6666         // color_unused_omit_blend_state
6667         if (testConfigExternal.groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
6668         {
6669                 vector<Subpass>         subpasses;
6670
6671                 // First subpass: use color attachment, create pipeline with color blend state
6672                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6673                                                                         0u,
6674                                                                         vector<AttachmentReference>(),
6675                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6676                                                                         vector<AttachmentReference>(),
6677                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6678                                                                         vector<deUint32>(),
6679                                                                         false));
6680
6681                 // Second subpass: don't use color attachment, create pipeline without color blend state
6682                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6683                                                                         0u,
6684                                                                         vector<AttachmentReference>(),
6685                                                                         vector<AttachmentReference>(1, AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6686                                                                         vector<AttachmentReference>(),
6687                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6688                                                                         vector<deUint32>(),
6689                                                                         true));
6690
6691                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(VK_FORMAT_R8G8B8A8_UNORM,
6692                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6693                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_CLEAR,
6694                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6695                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6696                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6697                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6698                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6699                                                                                  subpasses,
6700                                                                                  vector<SubpassDependency>());
6701
6702                 const TestConfig        testConfig      (renderPass,
6703                                                                                  TestConfig::RENDERTYPES_DRAW,
6704                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6705                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6706                                                                                  targetSize,
6707                                                                                  renderPos,
6708                                                                                  renderSize,
6709                                                                                  DE_FALSE,
6710                                                                                  90239,
6711                                                                                  0,
6712                                                                                  testConfigExternal.allocationKind,
6713                                                                                  testConfigExternal.groupParams);
6714                 addFunctionCaseWithPrograms<TestConfig>(group, "color_unused_omit_blend_state", "Two unused color attachment case without blend state", createTestShaders, renderPassTest, testConfig);
6715         }
6716 }
6717
6718 std::string formatToName (VkFormat format)
6719 {
6720         const std::string       formatStr       = de::toString(format);
6721         const std::string       prefix          = "VK_FORMAT_";
6722
6723         DE_ASSERT(formatStr.substr(0, prefix.length()) == prefix);
6724
6725         return de::toLower(formatStr.substr(prefix.length()));
6726 }
6727
6728 void addFormatTests (tcu::TestCaseGroup* group, const TestConfigExternal testConfigExternal)
6729 {
6730         tcu::TestContext&       testCtx         = group->getTestContext();
6731
6732         const UVec2                     targetSize      (64, 64);
6733         const UVec2                     renderPos       (0, 0);
6734         const UVec2                     renderSize      (64, 64);
6735
6736         const struct
6737         {
6738                 const char* const                       str;
6739                 const VkAttachmentStoreOp       op;
6740         } storeOps[] =
6741         {
6742                 { "store",              VK_ATTACHMENT_STORE_OP_STORE            },
6743                 { "dont_care",  VK_ATTACHMENT_STORE_OP_DONT_CARE        }
6744         };
6745
6746         const struct
6747         {
6748                 const char* const                       str;
6749                 const VkAttachmentLoadOp        op;
6750         } loadOps[] =
6751         {
6752                 { "clear",              VK_ATTACHMENT_LOAD_OP_CLEAR             },
6753                 { "load",               VK_ATTACHMENT_LOAD_OP_LOAD              },
6754                 { "dont_care",  VK_ATTACHMENT_LOAD_OP_DONT_CARE }
6755         };
6756
6757         const struct
6758         {
6759                  const char* const                              str;
6760                  const TestConfig::RenderTypes  types;
6761         } renderTypes[] =
6762         {
6763                 { "clear",              TestConfig::RENDERTYPES_CLEAR                                                           },
6764                 { "draw",               TestConfig::RENDERTYPES_DRAW                                                            },
6765                 { "clear_draw", TestConfig::RENDERTYPES_CLEAR|TestConfig::RENDERTYPES_DRAW      }
6766         };
6767
6768         // Color formats
6769         for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_coreColorFormats); formatNdx++)
6770         {
6771                 const VkFormat                                  format          = s_coreColorFormats[formatNdx];
6772                 de::MovePtr<tcu::TestCaseGroup> formatGroup     (new tcu::TestCaseGroup(testCtx, formatToName(format).c_str(), de::toString(format).c_str()));
6773
6774                 for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
6775                 {
6776                         const VkAttachmentLoadOp                loadOp  = loadOps[loadOpNdx].op;
6777                         de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
6778
6779                         for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
6780                         {
6781                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(format,
6782                                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
6783                                                                                                                                                                   loadOp,
6784                                                                                                                                                                   VK_ATTACHMENT_STORE_OP_STORE,
6785                                                                                                                                                                   VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6786                                                                                                                                                                   VK_ATTACHMENT_STORE_OP_DONT_CARE,
6787                                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6788                                                                                                                                                                   VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6789                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6790                                                                                                                                                         0u,
6791                                                                                                                                                         vector<AttachmentReference>(),
6792                                                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6793                                                                                                                                                         vector<AttachmentReference>(),
6794                                                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6795                                                                                                                                                         vector<deUint32>())),
6796                                                                                                  vector<SubpassDependency>());
6797                                 const TestConfig        testConfig      (renderPass,
6798                                                                                                  renderTypes[renderTypeNdx].types,
6799                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6800                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6801                                                                                                  targetSize,
6802                                                                                                  renderPos,
6803                                                                                                  renderSize,
6804                                                                                                  DE_FALSE,
6805                                                                                                  90239,
6806                                                                                                  0,
6807                                                                                                  testConfigExternal.allocationKind,
6808                                                                                                  testConfigExternal.groupParams);
6809
6810                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), renderTypes[renderTypeNdx].str, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6811                         }
6812
6813                         formatGroup->addChild(loadOpGroup.release());
6814                 }
6815
6816                 if (testConfigExternal.groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
6817                 {
6818                         de::MovePtr<tcu::TestCaseGroup> inputGroup (new tcu::TestCaseGroup(testCtx, "input", "Test attachment format as input"));
6819
6820                         for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
6821                         {
6822                                 const VkAttachmentLoadOp                loadOp          = loadOps[loadOpNdx].op;
6823                                 de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
6824
6825                                 for (size_t storeOpNdx = 0; storeOpNdx < DE_LENGTH_OF_ARRAY(storeOps); storeOpNdx++)
6826                                 {
6827                                         const VkImageAspectFlags                inputAttachmentAspectMask       = (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2)
6828                                                                                                                                                                 ? static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT)
6829                                                                                                                                                                 : static_cast<VkImageAspectFlags>(0);
6830                                         const VkAttachmentStoreOp               storeOp                                         = storeOps[storeOpNdx].op;
6831                                         de::MovePtr<tcu::TestCaseGroup> storeOpGroup                            (new tcu::TestCaseGroup(testCtx, storeOps[storeOpNdx].str, storeOps[storeOpNdx].str));
6832
6833                                         for (size_t useInputAspectNdx = 0; useInputAspectNdx < 2; useInputAspectNdx++)
6834                                         {
6835                                                 const bool useInputAspect = useInputAspectNdx != 0;
6836
6837                                                 if (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2 && useInputAspect)
6838                                                         continue;
6839
6840                                                 for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
6841                                                 {
6842                                                         {
6843                                                                 vector<Attachment>                                                      attachments;
6844                                                                 vector<Subpass>                                                         subpasses;
6845                                                                 vector<SubpassDependency>                                       deps;
6846                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
6847
6848                                                                 attachments.push_back(Attachment(format,
6849                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6850                                                                                                                                  loadOp,
6851                                                                                                                                  storeOp,
6852                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6853                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6854                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6855                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6856
6857                                                                 attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
6858                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6859                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6860                                                                                                                                  VK_ATTACHMENT_STORE_OP_STORE,
6861                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6862                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6863                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6864                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6865
6866                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6867                                                                                                                         0u,
6868                                                                                                                         vector<AttachmentReference>(),
6869                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6870                                                                                                                         vector<AttachmentReference>(),
6871                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6872                                                                                                                         vector<deUint32>()));
6873                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6874                                                                                                                         0u,
6875                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
6876                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6877                                                                                                                         vector<AttachmentReference>(),
6878                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6879                                                                                                                         vector<deUint32>()));
6880
6881                                                                 deps.push_back(SubpassDependency(0, 1,
6882
6883                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6884                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6885
6886                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6887                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6888                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6889
6890                                                                 if (useInputAspect)
6891                                                                 {
6892                                                                         const VkInputAttachmentAspectReference inputAspect =
6893                                                                         {
6894                                                                                 1u,
6895                                                                                 0u,
6896                                                                                 VK_IMAGE_ASPECT_COLOR_BIT
6897                                                                         };
6898
6899                                                                         inputAspects.push_back(inputAspect);
6900                                                                 }
6901
6902                                                                 {
6903                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
6904                                                                         const TestConfig        testConfig      (renderPass,
6905                                                                                                                                          renderTypes[renderTypeNdx].types,
6906                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
6907                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
6908                                                                                                                                          targetSize,
6909                                                                                                                                          renderPos,
6910                                                                                                                                          renderSize,
6911                                                                                                                                          DE_FALSE,
6912                                                                                                                                          89246,
6913                                                                                                                                          0,
6914                                                                                                                                          testConfigExternal.allocationKind,
6915                                                                                                                                          testConfigExternal.groupParams);
6916                                                                         const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : ""));
6917
6918                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6919                                                                 }
6920                                                         }
6921                                                         {
6922                                                                 vector<Attachment>                                                      attachments;
6923                                                                 vector<Subpass>                                                         subpasses;
6924                                                                 vector<SubpassDependency>                                       deps;
6925                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
6926
6927                                                                 attachments.push_back(Attachment(format,
6928                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
6929                                                                                                                                  loadOp,
6930                                                                                                                                  storeOp,
6931                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
6932                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
6933                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
6934                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
6935
6936                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6937                                                                                                                         0u,
6938                                                                                                                         vector<AttachmentReference>(),
6939                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
6940                                                                                                                         vector<AttachmentReference>(),
6941                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6942                                                                                                                         vector<deUint32>()));
6943                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
6944                                                                                                                         0u,
6945                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL, inputAttachmentAspectMask)),
6946                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL)),
6947                                                                                                                         vector<AttachmentReference>(),
6948                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
6949                                                                                                                         vector<deUint32>()));
6950
6951                                                                 deps.push_back(SubpassDependency(0, 1,
6952                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6953                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6954
6955                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6956                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6957                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6958
6959                                                                 deps.push_back(SubpassDependency(1, 1,
6960                                                                                                                                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
6961                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
6962
6963                                                                                                                                 vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6964                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
6965                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
6966
6967                                                                 if (useInputAspect)
6968                                                                 {
6969                                                                         const VkInputAttachmentAspectReference inputAspect =
6970                                                                         {
6971                                                                                 1u,
6972                                                                                 0u,
6973                                                                                 VK_IMAGE_ASPECT_COLOR_BIT
6974                                                                         };
6975
6976                                                                         inputAspects.push_back(inputAspect);
6977                                                                 }
6978
6979                                                                 {
6980                                                                         const RenderPass renderPass (attachments, subpasses, deps, inputAspects);
6981                                                                         const TestConfig testConfig (renderPass,
6982                                                                                                                                  renderTypes[renderTypeNdx].types,
6983                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
6984                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
6985                                                                                                                                  targetSize,
6986                                                                                                                                  renderPos,
6987                                                                                                                                  renderSize,
6988                                                                                                                                  DE_FALSE,
6989                                                                                                                                  89246,
6990                                                                                                                                  0,
6991                                                                                                                                  testConfigExternal.allocationKind,
6992                                                                                                                                  testConfigExternal.groupParams);
6993                                                                         const string    testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : ""));
6994
6995                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
6996                                                                 }
6997                                                         }
6998                                                 }
6999                                         }
7000
7001                                         loadOpGroup->addChild(storeOpGroup.release());
7002                                 }
7003
7004                                 inputGroup->addChild(loadOpGroup.release());
7005                         }
7006
7007                         formatGroup->addChild(inputGroup.release());
7008                 }
7009
7010                 group->addChild(formatGroup.release());
7011         }
7012
7013         // Depth stencil formats
7014         for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(s_coreDepthStencilFormats); formatNdx++)
7015         {
7016                 const VkFormat                                  vkFormat                        = s_coreDepthStencilFormats[formatNdx];
7017                 const tcu::TextureFormat                format                          = mapVkFormat(vkFormat);
7018                 const bool                                              isStencilAttachment     = hasStencilComponent(format.order);
7019                 const bool                                              isDepthAttachment       = hasDepthComponent(format.order);
7020                 const VkImageAspectFlags                formatAspectFlags       = (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7021                                                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u);
7022                 de::MovePtr<tcu::TestCaseGroup> formatGroup                     (new tcu::TestCaseGroup(testCtx, formatToName(vkFormat).c_str(), de::toString(vkFormat).c_str()));
7023
7024                 for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
7025                 {
7026                         const VkAttachmentLoadOp                loadOp  = loadOps[loadOpNdx].op;
7027                         de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
7028
7029                         for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
7030                         {
7031                                 {
7032                                         const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
7033                                                                                                                                                                           VK_SAMPLE_COUNT_1_BIT,
7034                                                                                                                                                                           isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7035                                                                                                                                                                           isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7036                                                                                                                                                                           isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7037                                                                                                                                                                           isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7038                                                                                                                                                                           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7039                                                                                                                                                                           VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
7040                                                                                                          vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7041                                                                                                                                                                 0u,
7042                                                                                                                                                                 vector<AttachmentReference>(),
7043                                                                                                                                                                 vector<AttachmentReference>(),
7044                                                                                                                                                                 vector<AttachmentReference>(),
7045                                                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7046                                                                                                                                                                 vector<deUint32>())),
7047                                                                                                          vector<SubpassDependency>());
7048                                         const TestConfig        testConfig      (renderPass,
7049                                                                                                          renderTypes[renderTypeNdx].types,
7050                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
7051                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
7052                                                                                                          targetSize,
7053                                                                                                          renderPos,
7054                                                                                                          renderSize,
7055                                                                                                          DE_FALSE,
7056                                                                                                          90239,
7057                                                                                                          0,
7058                                                                                                          testConfigExternal.allocationKind,
7059                                                                                                          testConfigExternal.groupParams);
7060
7061                                         addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), renderTypes[renderTypeNdx].str, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7062                                 }
7063
7064                                 if (isStencilAttachment && isDepthAttachment && loadOp != VK_ATTACHMENT_LOAD_OP_CLEAR)
7065                                 {
7066                                         {
7067                                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
7068                                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
7069                                                                                                                                                                   isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7070                                                                                                                                                                   isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7071                                                                                                                                                                   isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7072                                                                                                                                                                   isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7073                                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7074                                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
7075                                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7076                                                                                                                                                                         0u,
7077                                                                                                                                                                         vector<AttachmentReference>(),
7078                                                                                                                                                                         vector<AttachmentReference>(),
7079                                                                                                                                                                         vector<AttachmentReference>(),
7080                                                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL),
7081                                                                                                                                                                         vector<deUint32>())),
7082                                                                                                                  vector<SubpassDependency>());
7083                                                 const TestConfig        testConfig      (renderPass,
7084                                                                                                                  renderTypes[renderTypeNdx].types,
7085                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7086                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7087                                                                                                                  targetSize,
7088                                                                                                                  renderPos,
7089                                                                                                                  renderSize,
7090                                                                                                                  DE_FALSE,
7091                                                                                                                  90239,
7092                                                                                                                  0,
7093                                                                                                                  testConfigExternal.allocationKind,
7094                                                                                                                  testConfigExternal.groupParams);
7095                                                 const string            testName        (string(renderTypes[renderTypeNdx].str) + "_depth_read_only");
7096
7097                                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7098                                         }
7099
7100                                         {
7101                                                 const RenderPass        renderPass      (vector<Attachment>(1, Attachment(vkFormat,
7102                                                                                                                                                   VK_SAMPLE_COUNT_1_BIT,
7103                                                                                                                                                   isDepthAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7104                                                                                                                                                   isDepthAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7105                                                                                                                                                   isStencilAttachment ? loadOp : VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7106                                                                                                                                                   isStencilAttachment ? VK_ATTACHMENT_STORE_OP_STORE :VK_ATTACHMENT_STORE_OP_DONT_CARE,
7107                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7108                                                                                                                                                   VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)),
7109                                                                                                                  vector<Subpass>(1, Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7110                                                                                                                                                                         0u,
7111                                                                                                                                                                         vector<AttachmentReference>(),
7112                                                                                                                                                                         vector<AttachmentReference>(),
7113                                                                                                                                                                         vector<AttachmentReference>(),
7114                                                                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL),
7115                                                                                                                                                                         vector<deUint32>())),
7116                                                                                                                  vector<SubpassDependency>());
7117                                                 const TestConfig        testConfig      (renderPass,
7118                                                                                                                  renderTypes[renderTypeNdx].types,
7119                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7120                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7121                                                                                                                  targetSize,
7122                                                                                                                  renderPos,
7123                                                                                                                  renderSize,
7124                                                                                                                  DE_FALSE,
7125                                                                                                                  90239,
7126                                                                                                                  0,
7127                                                                                                                  testConfigExternal.allocationKind,
7128                                                                                                                  testConfigExternal.groupParams);
7129                                                 const string            testName        (string(renderTypes[renderTypeNdx].str) + "_stencil_read_only");
7130
7131                                                 addFunctionCaseWithPrograms<TestConfig>(loadOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7132                                         }
7133                                 }
7134                         }
7135
7136                         formatGroup->addChild(loadOpGroup.release());
7137                 }
7138
7139                 if (testConfigExternal.groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7140                 {
7141                         de::MovePtr<tcu::TestCaseGroup> inputGroup (new tcu::TestCaseGroup(testCtx, "input", "Test attachment format as input"));
7142
7143                         for (size_t loadOpNdx = 0; loadOpNdx < DE_LENGTH_OF_ARRAY(loadOps); loadOpNdx++)
7144                         {
7145                                 const VkAttachmentLoadOp                loadOp          = loadOps[loadOpNdx].op;
7146                                 de::MovePtr<tcu::TestCaseGroup> loadOpGroup     (new tcu::TestCaseGroup(testCtx, loadOps[loadOpNdx].str, loadOps[loadOpNdx].str));
7147
7148                                 for (size_t storeOpNdx = 0; storeOpNdx < DE_LENGTH_OF_ARRAY(storeOps); storeOpNdx++)
7149                                 {
7150                                         const VkImageAspectFlags                inputAttachmentAspectMask       = (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2)
7151                                                                                                                                                                 ? formatAspectFlags
7152                                                                                                                                                                 : static_cast<VkImageAspectFlags>(0);
7153                                         const VkAttachmentStoreOp               storeOp                                         = storeOps[storeOpNdx].op;
7154                                         de::MovePtr<tcu::TestCaseGroup> storeOpGroup                            (new tcu::TestCaseGroup(testCtx, storeOps[storeOpNdx].str, storeOps[storeOpNdx].str));
7155
7156                                         for (size_t useInputAspectNdx = 0; useInputAspectNdx < 2; useInputAspectNdx++)
7157                                         {
7158                                                 const bool useInputAspect = useInputAspectNdx != 0;
7159
7160                                                 if (testConfigExternal.groupParams->renderingType == RENDERING_TYPE_RENDERPASS2 && useInputAspect)
7161                                                         continue;
7162
7163                                                 for (size_t renderTypeNdx = 0; renderTypeNdx < DE_LENGTH_OF_ARRAY(renderTypes); renderTypeNdx++)
7164                                                 {
7165                                                         {
7166                                                                 vector<Attachment>                                                      attachments;
7167                                                                 vector<Subpass>                                                         subpasses;
7168                                                                 vector<SubpassDependency>                                       deps;
7169                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
7170
7171                                                                 attachments.push_back(Attachment(vkFormat,
7172                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7173                                                                                                                                  loadOp,
7174                                                                                                                                  storeOp,
7175                                                                                                                                  loadOp,
7176                                                                                                                                  storeOp,
7177                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7178                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7179
7180                                                                 attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7181                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7182                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7183                                                                                                                                  VK_ATTACHMENT_STORE_OP_STORE,
7184                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7185                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
7186                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7187                                                                                                                                  VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7188
7189                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7190                                                                                                                         0u,
7191                                                                                                                         vector<AttachmentReference>(),
7192                                                                                                                         vector<AttachmentReference>(),
7193                                                                                                                         vector<AttachmentReference>(),
7194                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7195                                                                                                                         vector<deUint32>()));
7196                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7197                                                                                                                         0u,
7198                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7199                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7200                                                                                                                         vector<AttachmentReference>(),
7201                                                                                                                         AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7202                                                                                                                         vector<deUint32>()));
7203
7204                                                                 deps.push_back(SubpassDependency(0, 1,
7205                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7206                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7207
7208                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7209                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7210                                                                                                                                 0u));
7211
7212                                                                 if (useInputAspect)
7213                                                                 {
7214                                                                         const VkInputAttachmentAspectReference inputAspect =
7215                                                                         {
7216                                                                                 1u,
7217                                                                                 0u,
7218                                                                                 (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7219                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7220                                                                         };
7221
7222                                                                         inputAspects.push_back(inputAspect);
7223                                                                 }
7224
7225                                                                 {
7226                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7227                                                                         const TestConfig        testConfig      (renderPass,
7228                                                                                                                                          renderTypes[renderTypeNdx].types,
7229                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
7230                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
7231                                                                                                                                          targetSize,
7232                                                                                                                                          renderPos,
7233                                                                                                                                          renderSize,
7234                                                                                                                                          DE_FALSE,
7235                                                                                                                                          89246,
7236                                                                                                                                          0,
7237                                                                                                                                          testConfigExternal.allocationKind,
7238                                                                                                                                          testConfigExternal.groupParams);
7239                                                                         const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : ""));
7240
7241                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7242                                                                 }
7243                                                         }
7244                                                         {
7245                                                                 vector<Attachment>                                                      attachments;
7246                                                                 vector<Subpass>                                                         subpasses;
7247                                                                 vector<SubpassDependency>                                       deps;
7248                                                                 vector<VkInputAttachmentAspectReference>        inputAspects;
7249
7250                                                                 attachments.push_back(Attachment(vkFormat,
7251                                                                                                                                  VK_SAMPLE_COUNT_1_BIT,
7252                                                                                                                                  loadOp,
7253                                                                                                                                  storeOp,
7254                                                                                                                                  VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7255                                                                                                                                  VK_ATTACHMENT_STORE_OP_DONT_CARE,
7256                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7257                                                                                                                                  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7258
7259                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7260                                                                                                                         0u,
7261                                                                                                                         vector<AttachmentReference>(),
7262                                                                                                                         vector<AttachmentReference>(),
7263                                                                                                                         vector<AttachmentReference>(),
7264                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7265                                                                                                                         vector<deUint32>()));
7266                                                                 subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7267                                                                                                                         0u,
7268                                                                                                                         vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL, inputAttachmentAspectMask)),
7269                                                                                                                         vector<AttachmentReference>(),
7270                                                                                                                         vector<AttachmentReference>(),
7271                                                                                                                         AttachmentReference(0, VK_IMAGE_LAYOUT_GENERAL),
7272                                                                                                                         vector<deUint32>()));
7273
7274                                                                 deps.push_back(SubpassDependency(0, 1,
7275                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7276                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7277
7278                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7279                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7280                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
7281
7282                                                                 deps.push_back(SubpassDependency(1, 1,
7283                                                                                                                                 vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7284                                                                                                                                 vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7285                                                                                                                                 vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7286                                                                                                                                 vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7287                                                                                                                                 vk::VK_DEPENDENCY_BY_REGION_BIT));
7288
7289
7290                                                                 if (useInputAspect)
7291                                                                 {
7292                                                                         const VkInputAttachmentAspectReference inputAspect =
7293                                                                         {
7294                                                                                 1u,
7295                                                                                 0u,
7296
7297                                                                                 (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7298                                                                                         | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7299                                                                         };
7300
7301                                                                         inputAspects.push_back(inputAspect);
7302                                                                 }
7303
7304                                                                 {
7305                                                                         const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7306                                                                         const TestConfig        testConfig      (renderPass,
7307                                                                                                                                          renderTypes[renderTypeNdx].types,
7308                                                                                                                                          TestConfig::COMMANDBUFFERTYPES_INLINE,
7309                                                                                                                                          TestConfig::IMAGEMEMORY_STRICT,
7310                                                                                                                                          targetSize,
7311                                                                                                                                          renderPos,
7312                                                                                                                                          renderSize,
7313                                                                                                                                          DE_FALSE,
7314                                                                                                                                          89246,
7315                                                                                                                                          0,
7316                                                                                                                                          testConfigExternal.allocationKind,
7317                                                                                                                                          testConfigExternal.groupParams);
7318                                                                         const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : ""));
7319
7320                                                                         addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7321                                                                 }
7322                                                         }
7323
7324                                                         if (isStencilAttachment && isDepthAttachment)
7325                                                         {
7326                                                                 // Depth read only
7327                                                                 {
7328                                                                         vector<Attachment>                                                      attachments;
7329                                                                         vector<Subpass>                                                         subpasses;
7330                                                                         vector<SubpassDependency>                                       deps;
7331                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7332
7333                                                                         attachments.push_back(Attachment(vkFormat,
7334                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7335                                                                                                                                          loadOp,
7336                                                                                                                                          storeOp,
7337                                                                                                                                          loadOp,
7338                                                                                                                                          storeOp,
7339                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7340                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7341
7342                                                                         attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7343                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7344                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7345                                                                                                                                          VK_ATTACHMENT_STORE_OP_STORE,
7346                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7347                                                                                                                                          VK_ATTACHMENT_STORE_OP_DONT_CARE,
7348                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7349                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7350
7351                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7352                                                                                                                                 0u,
7353                                                                                                                                 vector<AttachmentReference>(),
7354                                                                                                                                 vector<AttachmentReference>(),
7355                                                                                                                                 vector<AttachmentReference>(),
7356                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7357                                                                                                                                 vector<deUint32>()));
7358                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7359                                                                                                                                 0u,
7360                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, inputAttachmentAspectMask)),
7361                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7362                                                                                                                                 vector<AttachmentReference>(),
7363                                                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7364                                                                                                                                 vector<deUint32>()));
7365
7366                                                                         deps.push_back(SubpassDependency(0, 1,
7367                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7368                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7369
7370                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7371                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7372                                                                                                                                         0u));
7373
7374                                                                         if (useInputAspect)
7375                                                                         {
7376                                                                                 const VkInputAttachmentAspectReference inputAspect =
7377                                                                                 {
7378                                                                                         1u,
7379                                                                                         0u,
7380
7381                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7382                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7383                                                                                 };
7384
7385                                                                                 inputAspects.push_back(inputAspect);
7386                                                                         }
7387
7388                                                                         {
7389                                                                                 const RenderPass        renderPass       (attachments, subpasses, deps, inputAspects);
7390                                                                                 const TestConfig        testConfig       (renderPass,
7391                                                                                                                                                  renderTypes[renderTypeNdx].types,
7392                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7393                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7394                                                                                                                                                  targetSize,
7395                                                                                                                                                  renderPos,
7396                                                                                                                                                  renderSize,
7397                                                                                                                                                  DE_FALSE,
7398                                                                                                                                                  89246,
7399                                                                                                                                                  0,
7400                                                                                                                                                  testConfigExternal.allocationKind,
7401                                                                                                                                                  testConfigExternal.groupParams);
7402                                                                                 const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : "") + "_depth_read_only");
7403
7404                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7405                                                                         }
7406                                                                 }
7407                                                                 {
7408                                                                         vector<Attachment>                                                      attachments;
7409                                                                         vector<Subpass>                                                         subpasses;
7410                                                                         vector<SubpassDependency>                                       deps;
7411                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7412
7413                                                                         attachments.push_back(Attachment(vkFormat,
7414                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7415                                                                                                                                          loadOp,
7416                                                                                                                                          storeOp,
7417                                                                                                                                          loadOp,
7418                                                                                                                                          storeOp,
7419                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7420                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7421
7422                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7423                                                                                                                                 0u,
7424                                                                                                                                 vector<AttachmentReference>(),
7425                                                                                                                                 vector<AttachmentReference>(),
7426                                                                                                                                 vector<AttachmentReference>(),
7427                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7428                                                                                                                                 vector<deUint32>()));
7429                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7430                                                                                                                                 0u,
7431                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, inputAttachmentAspectMask)),
7432                                                                                                                                 vector<AttachmentReference>(),
7433                                                                                                                                 vector<AttachmentReference>(),
7434                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL),
7435                                                                                                                                 vector<deUint32>()));
7436
7437                                                                         deps.push_back(SubpassDependency(0, 1,
7438                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7439                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7440
7441                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7442                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7443                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7444
7445                                                                         deps.push_back(SubpassDependency(1, 1,
7446                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7447                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7448
7449                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7450                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7451                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7452
7453                                                                         if (useInputAspect)
7454                                                                         {
7455                                                                                 const VkInputAttachmentAspectReference inputAspect =
7456                                                                                 {
7457                                                                                         1u,
7458                                                                                         0u,
7459
7460                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7461                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7462                                                                                 };
7463
7464                                                                                 inputAspects.push_back(inputAspect);
7465                                                                         }
7466
7467                                                                         {
7468                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7469                                                                                 const TestConfig        testConfig      (renderPass,
7470                                                                                                                                                  renderTypes[renderTypeNdx].types,
7471                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7472                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7473                                                                                                                                                  targetSize,
7474                                                                                                                                                  renderPos,
7475                                                                                                                                                  renderSize,
7476                                                                                                                                                  DE_FALSE,
7477                                                                                                                                                  89246,
7478                                                                                                                                                  0,
7479                                                                                                                                                  testConfigExternal.allocationKind,
7480                                                                                                                                                  testConfigExternal.groupParams);
7481                                                                                 const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : "") + "_depth_read_only");
7482
7483                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7484                                                                         }
7485                                                                 }
7486                                                                 // Stencil read only
7487                                                                 {
7488                                                                         vector<Attachment>                                                      attachments;
7489                                                                         vector<Subpass>                                                         subpasses;
7490                                                                         vector<SubpassDependency>                                       deps;
7491                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7492
7493                                                                         attachments.push_back(Attachment(vkFormat,
7494                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7495                                                                                                                                          loadOp,
7496                                                                                                                                          storeOp,
7497                                                                                                                                          loadOp,
7498                                                                                                                                          storeOp,
7499                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7500                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7501
7502                                                                         attachments.push_back(Attachment(vk::VK_FORMAT_R8G8B8A8_UNORM,
7503                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7504                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7505                                                                                                                                          VK_ATTACHMENT_STORE_OP_STORE,
7506                                                                                                                                          VK_ATTACHMENT_LOAD_OP_DONT_CARE,
7507                                                                                                                                          VK_ATTACHMENT_STORE_OP_DONT_CARE,
7508                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
7509                                                                                                                                          VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
7510
7511                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7512                                                                                                                                 0u,
7513                                                                                                                                 vector<AttachmentReference>(),
7514                                                                                                                                 vector<AttachmentReference>(),
7515                                                                                                                                 vector<AttachmentReference>(),
7516                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7517                                                                                                                                 vector<deUint32>()));
7518                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7519                                                                                                                                 0u,
7520                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7521                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)),
7522                                                                                                                                 vector<AttachmentReference>(),
7523                                                                                                                                 AttachmentReference(VK_ATTACHMENT_UNUSED, VK_IMAGE_LAYOUT_GENERAL),
7524                                                                                                                                 vector<deUint32>()));
7525
7526                                                                         deps.push_back(SubpassDependency(0, 1,
7527                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
7528                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7529
7530                                                                                                                                         vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7531                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7532                                                                                                                                         0u));
7533
7534                                                                         if (useInputAspect)
7535                                                                         {
7536                                                                                 const VkInputAttachmentAspectReference inputAspect =
7537                                                                                 {
7538                                                                                         1u,
7539                                                                                         0u,
7540
7541                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7542                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7543                                                                                 };
7544
7545                                                                                 inputAspects.push_back(inputAspect);
7546                                                                         }
7547
7548                                                                         {
7549                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7550                                                                                 const TestConfig        testConfig      (renderPass,
7551                                                                                                                                                  renderTypes[renderTypeNdx].types,
7552                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7553                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7554                                                                                                                                                  targetSize,
7555                                                                                                                                                  renderPos,
7556                                                                                                                                                  renderSize,
7557                                                                                                                                                  DE_FALSE,
7558                                                                                                                                                  89246,
7559                                                                                                                                                  0,
7560                                                                                                                                                  testConfigExternal.allocationKind,
7561                                                                                                                                                  testConfigExternal.groupParams);
7562                                                                                 const string            testName        (renderTypes[renderTypeNdx].str + string(useInputAspect ? "_use_input_aspect" : "") + "_stencil_read_only");
7563
7564                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7565                                                                         }
7566                                                                 }
7567                                                                 {
7568                                                                         vector<Attachment>                                                      attachments;
7569                                                                         vector<Subpass>                                                         subpasses;
7570                                                                         vector<SubpassDependency>                                       deps;
7571                                                                         vector<VkInputAttachmentAspectReference>        inputAspects;
7572
7573                                                                         attachments.push_back(Attachment(vkFormat,
7574                                                                                                                                          VK_SAMPLE_COUNT_1_BIT,
7575                                                                                                                                          loadOp,
7576                                                                                                                                          storeOp,
7577                                                                                                                                          loadOp,
7578                                                                                                                                          storeOp,
7579                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
7580                                                                                                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
7581
7582                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7583                                                                                                                                 0u,
7584                                                                                                                                 vector<AttachmentReference>(),
7585                                                                                                                                 vector<AttachmentReference>(),
7586                                                                                                                                 vector<AttachmentReference>(),
7587                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL),
7588                                                                                                                                 vector<deUint32>()));
7589                                                                         subpasses.push_back(Subpass(VK_PIPELINE_BIND_POINT_GRAPHICS,
7590                                                                                                                                 0u,
7591                                                                                                                                 vector<AttachmentReference>(1, AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, inputAttachmentAspectMask)),
7592                                                                                                                                 vector<AttachmentReference>(),
7593                                                                                                                                 vector<AttachmentReference>(),
7594                                                                                                                                 AttachmentReference(0, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL),
7595                                                                                                                                 vector<deUint32>()));
7596
7597                                                                         deps.push_back(SubpassDependency(0, 1,
7598                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7599                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7600
7601                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7602                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7603                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7604
7605                                                                         deps.push_back(SubpassDependency(1, 1,
7606                                                                                                                                         vk::VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | vk::VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
7607                                                                                                                                         vk::VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
7608
7609                                                                                                                                         vk::VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
7610                                                                                                                                         vk::VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
7611                                                                                                                                         vk::VK_DEPENDENCY_BY_REGION_BIT));
7612
7613
7614                                                                         if (useInputAspect)
7615                                                                         {
7616                                                                                 const VkInputAttachmentAspectReference inputAspect =
7617                                                                                 {
7618                                                                                         1u,
7619                                                                                         0u,
7620
7621                                                                                         (isDepthAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_DEPTH_BIT : 0u)
7622                                                                                                 | (isStencilAttachment ? (VkImageAspectFlags)VK_IMAGE_ASPECT_STENCIL_BIT : 0u)
7623                                                                                 };
7624
7625                                                                                 inputAspects.push_back(inputAspect);
7626                                                                         }
7627
7628                                                                         {
7629                                                                                 const RenderPass        renderPass      (attachments, subpasses, deps, inputAspects);
7630                                                                                 const TestConfig        testConfig      (renderPass,
7631                                                                                                                                                  renderTypes[renderTypeNdx].types,
7632                                                                                                                                                  TestConfig::COMMANDBUFFERTYPES_INLINE,
7633                                                                                                                                                  TestConfig::IMAGEMEMORY_STRICT,
7634                                                                                                                                                  targetSize,
7635                                                                                                                                                  renderPos,
7636                                                                                                                                                  renderSize,
7637                                                                                                                                                  DE_FALSE,
7638                                                                                                                                                  89246,
7639                                                                                                                                                  0,
7640                                                                                                                                                  testConfigExternal.allocationKind,
7641                                                                                                                                                  testConfigExternal.groupParams);
7642                                                                                 const string            testName        (string("self_dep_") + renderTypes[renderTypeNdx].str + (useInputAspect ? "_use_input_aspect" : "") + "_stencil_read_only");
7643
7644                                                                                 addFunctionCaseWithPrograms<TestConfig>(storeOpGroup.get(), testName, string("self_dep_") + renderTypes[renderTypeNdx].str, createTestShaders, renderPassTest, testConfig);
7645                                                                         }
7646                                                                 }
7647                                                         }
7648                                                 }
7649                                         }
7650
7651                                         loadOpGroup->addChild(storeOpGroup.release());
7652                                 }
7653
7654                                 inputGroup->addChild(loadOpGroup.release());
7655                         }
7656
7657                         formatGroup->addChild(inputGroup.release());
7658                 }
7659
7660                 group->addChild(formatGroup.release());
7661         }
7662 }
7663
7664 void addRenderPassTests (tcu::TestCaseGroup* group, const AllocationKind allocationKind, const SharedGroupParams groupParams)
7665 {
7666         // tests added by this function have both primary and secondary cases and there is no need to repeat them for useSecondaryCmdBuffer flag;
7667         // but cases defined in other files that are later added to those groups in createRenderPassTestsInternal had to be adjusted and run
7668         // for useSecondaryCmdBuffer flag
7669         if (groupParams->useSecondaryCmdBuffer && !groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass)
7670                 return;
7671
7672         const TestConfigExternal        testConfigExternal      (allocationKind, groupParams);
7673
7674         // don't repeat cases that don't use CommandBufferTypes::COMMANDBUFFERTYPES_SECONDARY
7675         if (!groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass)
7676         {
7677                 addTestGroup(group, "simple", "Simple basic render pass tests", addSimpleTests, testConfigExternal);
7678                 addTestGroup(group, "formats", "Tests for different image formats.", addFormatTests, testConfigExternal);
7679         }
7680
7681         addTestGroup(group, "attachment", "Attachment format and count tests with load and store ops and image layouts", addAttachmentTests, testConfigExternal);
7682
7683         // don't repeat cases that don't use CommandBufferTypes::COMMANDBUFFERTYPES_SECONDARY
7684         if (!groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass)
7685                 addTestGroup(group, "attachment_write_mask", "Attachment write mask tests", addAttachmentWriteMaskTests, testConfigExternal);
7686
7687         if (groupParams->renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7688                 addTestGroup(group, "attachment_allocation", "Attachment allocation tests", addAttachmentAllocationTests, testConfigExternal);
7689 }
7690
7691 de::MovePtr<tcu::TestCaseGroup> createSuballocationTests(tcu::TestContext& testCtx, const SharedGroupParams groupParams)
7692 {
7693         de::MovePtr<tcu::TestCaseGroup> suballocationTestsGroup(new tcu::TestCaseGroup(testCtx, "suballocation", "Suballocation RenderPass Tests"));
7694
7695         addRenderPassTests(suballocationTestsGroup.get(), ALLOCATION_KIND_SUBALLOCATED, groupParams);
7696
7697         return suballocationTestsGroup;
7698 }
7699
7700 de::MovePtr<tcu::TestCaseGroup> createDedicatedAllocationTests(tcu::TestContext& testCtx, const SharedGroupParams groupParams)
7701 {
7702         de::MovePtr<tcu::TestCaseGroup> dedicatedAllocationTestsGroup(new tcu::TestCaseGroup(testCtx, "dedicated_allocation", "RenderPass Tests For Dedicated Allocation"));
7703
7704         addRenderPassTests(dedicatedAllocationTestsGroup.get(), ALLOCATION_KIND_DEDICATED, groupParams);
7705
7706         return dedicatedAllocationTestsGroup;
7707 }
7708
7709 tcu::TestCaseGroup* createRenderPassTestsInternal (tcu::TestContext& testCtx, const char* groupName, const SharedGroupParams groupParams)
7710 {
7711         de::MovePtr<tcu::TestCaseGroup> renderingTests                                  (new tcu::TestCaseGroup(testCtx, groupName, ""));
7712         de::MovePtr<tcu::TestCaseGroup> suballocationTestGroup                  = createSuballocationTests(testCtx, groupParams);
7713         de::MovePtr<tcu::TestCaseGroup> dedicatedAllocationTestGroup    = createDedicatedAllocationTests(testCtx, groupParams);
7714
7715         const RenderingType renderingType = groupParams->renderingType;
7716
7717         switch (renderingType)
7718         {
7719         case RENDERING_TYPE_RENDERPASS_LEGACY:
7720                 suballocationTestGroup->addChild(createRenderPassMultisampleTests(testCtx));
7721                 suballocationTestGroup->addChild(createRenderPassMultisampleResolveTests(testCtx, groupParams));
7722                 suballocationTestGroup->addChild(createRenderPassSubpassDependencyTests(testCtx));
7723                 suballocationTestGroup->addChild(createRenderPassSampleReadTests(testCtx));
7724
7725 #ifndef CTS_USES_VULKANSC
7726                 suballocationTestGroup->addChild(createRenderPassSparseRenderTargetTests(testCtx, groupParams));
7727                 renderingTests->addChild(createDepthStencilWriteConditionsTests(testCtx));
7728 #endif // CTS_USES_VULKANSC
7729
7730                 renderingTests->addChild(createRenderPassMultipleSubpassesMultipleCommandBuffersTests(testCtx));
7731
7732                 break;
7733
7734         case RENDERING_TYPE_RENDERPASS2:
7735                 suballocationTestGroup->addChild(createRenderPass2MultisampleTests(testCtx));
7736                 suballocationTestGroup->addChild(createRenderPass2MultisampleResolveTests(testCtx, groupParams));
7737                 suballocationTestGroup->addChild(createRenderPass2SubpassDependencyTests(testCtx));
7738                 suballocationTestGroup->addChild(createRenderPass2SampleReadTests(testCtx));
7739
7740 #ifndef CTS_USES_VULKANSC
7741                 suballocationTestGroup->addChild(createRenderPass2SparseRenderTargetTests(testCtx, groupParams));
7742 #endif // CTS_USES_VULKANSC
7743
7744                 renderingTests->addChild(createRenderPass2DepthStencilResolveTests(testCtx));
7745                 break;
7746
7747 #ifndef CTS_USES_VULKANSC
7748         case RENDERING_TYPE_DYNAMIC_RENDERING:
7749                 suballocationTestGroup->addChild(createDynamicRenderingMultisampleResolveTests(testCtx, groupParams));
7750                 suballocationTestGroup->addChild(createDynamicRenderingSparseRenderTargetTests(testCtx, groupParams));
7751
7752                 if (groupParams->useSecondaryCmdBuffer == false)
7753                 {
7754                         renderingTests->addChild(createDynamicRenderingRandomTests(testCtx));
7755                         renderingTests->addChild(createDynamicRenderingBasicTests(testCtx));
7756                 }
7757                 break;
7758 #endif // CTS_USES_VULKANSC
7759
7760         default:
7761                 break;
7762         }
7763
7764         if (renderingType != RENDERING_TYPE_DYNAMIC_RENDERING)
7765         {
7766                 suballocationTestGroup->addChild(createRenderPassUnusedAttachmentTests(testCtx, renderingType));
7767                 suballocationTestGroup->addChild(createRenderPassUnusedAttachmentSparseFillingTests(testCtx, renderingType));
7768         }
7769
7770         suballocationTestGroup->addChild(createRenderPassUnusedClearAttachmentTests(testCtx, groupParams));
7771
7772 #ifndef CTS_USES_VULKANSC
7773         suballocationTestGroup->addChild(createRenderPassLoadStoreOpNoneTests(testCtx, groupParams));
7774
7775         if (renderingType == RENDERING_TYPE_RENDERPASS2)
7776         {
7777                 suballocationTestGroup->addChild(createRenderPassSubpassMergeFeedbackTests(testCtx, renderingType));
7778         }
7779
7780         renderingTests->addChild(createFragmentDensityMapTests(testCtx, groupParams));
7781 #endif // CTS_USES_VULKANSC
7782
7783         renderingTests->addChild(suballocationTestGroup.release());
7784         renderingTests->addChild(dedicatedAllocationTestGroup.release());
7785
7786         return renderingTests.release();
7787 }
7788
7789 } // anonymous
7790
7791 tcu::TestCaseGroup* createRenderPassTests (tcu::TestContext& testCtx)
7792 {
7793         SharedGroupParams groupParams(
7794                 new GroupParams
7795                 {
7796                         RENDERING_TYPE_RENDERPASS_LEGACY,       // RenderingType renderingType;
7797                         false,                                                          // bool useSecondaryCmdBuffer;
7798                         false,                                                          // bool secondaryCmdBufferCompletelyContainsDynamicRenderpass;
7799                 });
7800         return createRenderPassTestsInternal(testCtx, "renderpass", groupParams);
7801 }
7802
7803 tcu::TestCaseGroup* createRenderPass2Tests (tcu::TestContext& testCtx)
7804 {
7805         SharedGroupParams groupParams(
7806                 new GroupParams
7807                 {
7808                         RENDERING_TYPE_RENDERPASS2,                     // RenderingType renderingType;
7809                         false,                                                          // bool useSecondaryCmdBuffer;
7810                         false,                                                          // bool secondaryCmdBufferCompletelyContainsDynamicRenderpass;
7811                 });
7812         return createRenderPassTestsInternal(testCtx, "renderpass2", groupParams);
7813 }
7814
7815 tcu::TestCaseGroup* createDynamicRenderingTests(tcu::TestContext& testCtx)
7816 {
7817         de::MovePtr<tcu::TestCaseGroup> dynamicRenderingGroup(new tcu::TestCaseGroup(testCtx, "dynamic_rendering", "Draw using VK_KHR_dynamic_rendering"));
7818
7819         dynamicRenderingGroup->addChild(createRenderPassTestsInternal(testCtx, "primary_cmd_buff", SharedGroupParams(
7820                 new GroupParams
7821                 {
7822                         RENDERING_TYPE_DYNAMIC_RENDERING,       // RenderingType renderingType;
7823                         false,                                                          // bool useSecondaryCmdBuffer;
7824                         false,                                                          // bool secondaryCmdBufferCompletelyContainsDynamicRenderpass;
7825                 })));
7826         dynamicRenderingGroup->addChild(createRenderPassTestsInternal(testCtx, "partial_secondary_cmd_buff", SharedGroupParams(
7827                 new GroupParams
7828                 {
7829                         RENDERING_TYPE_DYNAMIC_RENDERING,       // RenderingType renderingType;
7830                         true,                                                           // bool useSecondaryCmdBuffer;
7831                         false,                                                          // bool secondaryCmdBufferCompletelyContainsDynamicRenderpass;
7832                 })));
7833         dynamicRenderingGroup->addChild(createRenderPassTestsInternal(testCtx, "complete_secondary_cmd_buff", SharedGroupParams(
7834                 new GroupParams
7835                 {
7836                         RENDERING_TYPE_DYNAMIC_RENDERING,       // RenderingType renderingType;
7837                         true,                                                           // bool useSecondaryCmdBuffer;
7838                         true,                                                           // bool secondaryCmdBufferCompletelyContainsDynamicRenderpass;
7839                 })));
7840
7841         return dynamicRenderingGroup.release();
7842 }
7843
7844 } // vkt