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