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