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