Added two-step variants of surface clear tests
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiImageClearingTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Vulkan Image Clearing Tests
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktApiImageClearingTests.hpp"
26
27 #include "deRandom.hpp"
28 #include "deMath.h"
29 #include "deSTLUtil.hpp"
30 #include "deStringUtil.hpp"
31 #include "deUniquePtr.hpp"
32 #include "deArrayUtil.hpp"
33 #include "deInt32.h"
34 #include "vkImageUtil.hpp"
35 #include "vkMemUtil.hpp"
36 #include "vktTestCase.hpp"
37 #include "vktTestCaseUtil.hpp"
38 #include "vktTestGroupUtil.hpp"
39 #include "vkQueryUtil.hpp"
40 #include "vkRefUtil.hpp"
41 #include "vkTypeUtil.hpp"
42 #include "tcuImageCompare.hpp"
43 #include "tcuTexture.hpp"
44 #include "tcuTextureUtil.hpp"
45 #include "tcuVectorType.hpp"
46 #include "tcuTexture.hpp"
47 #include "tcuFloat.hpp"
48 #include "tcuTestLog.hpp"
49 #include "tcuVectorUtil.hpp"
50 #include <sstream>
51 #include <numeric>
52
53 namespace vkt
54 {
55
56 namespace api
57 {
58
59 using namespace vk;
60 using namespace tcu;
61
62 namespace
63 {
64
65 enum AllocationKind
66 {
67         ALLOCATION_KIND_SUBALLOCATED = 0,
68         ALLOCATION_KIND_DEDICATED,
69
70         ALLOCATION_KIND_LAST,
71 };
72
73 de::MovePtr<Allocation> allocateBuffer (const InstanceInterface&        vki,
74                                                                                 const DeviceInterface&          vkd,
75                                                                                 const VkPhysicalDevice&         physDevice,
76                                                                                 const VkDevice                          device,
77                                                                                 const VkBuffer&                         buffer,
78                                                                                 const MemoryRequirement         requirement,
79                                                                                 Allocator&                                      allocator,
80                                                                                 AllocationKind                          allocationKind)
81 {
82         switch (allocationKind)
83         {
84                 case ALLOCATION_KIND_SUBALLOCATED:
85                 {
86                         const VkMemoryRequirements memoryRequirements = getBufferMemoryRequirements(vkd, device, buffer);
87
88                         return allocator.allocate(memoryRequirements, requirement);
89                 }
90
91                 case ALLOCATION_KIND_DEDICATED:
92                 {
93                         return allocateDedicated(vki, vkd, physDevice, device, buffer, requirement);
94                 }
95
96                 default:
97                 {
98                         TCU_THROW(InternalError, "Invalid allocation kind");
99                 }
100         }
101 }
102
103 de::MovePtr<Allocation> allocateImage (const InstanceInterface&         vki,
104                                                                            const DeviceInterface&               vkd,
105                                                                            const VkPhysicalDevice&              physDevice,
106                                                                            const VkDevice                               device,
107                                                                            const VkImage&                               image,
108                                                                            const MemoryRequirement              requirement,
109                                                                            Allocator&                                   allocator,
110                                                                            AllocationKind                               allocationKind)
111 {
112         switch (allocationKind)
113         {
114                 case ALLOCATION_KIND_SUBALLOCATED:
115                 {
116                         const VkMemoryRequirements memoryRequirements = getImageMemoryRequirements(vkd, device, image);
117
118                         return allocator.allocate(memoryRequirements, requirement);
119                 }
120
121                 case ALLOCATION_KIND_DEDICATED:
122                 {
123                         return allocateDedicated(vki, vkd, physDevice, device, image, requirement);
124                 }
125
126                 default:
127                 {
128                         TCU_THROW(InternalError, "Invalid allocation kind");
129                 }
130         }
131 }
132
133 VkExtent3D getMipLevelExtent (VkExtent3D baseExtent, const deUint32 mipLevel)
134 {
135         baseExtent.width        = std::max(baseExtent.width  >> mipLevel, 1u);
136         baseExtent.height       = std::max(baseExtent.height >> mipLevel, 1u);
137         baseExtent.depth        = std::max(baseExtent.depth  >> mipLevel, 1u);
138         return baseExtent;
139 }
140
141 deUint32 getNumMipLevels (const VkExtent3D& baseExtent, const deUint32 maxMipLevels)
142 {
143         const deUint32 widestEdge = std::max(std::max(baseExtent.width, baseExtent.height), baseExtent.depth);
144         return std::min(static_cast<deUint32>(deFloatLog2(static_cast<float>(widestEdge))) + 1u, maxMipLevels);
145 }
146
147 deUint32 greatestCommonDivisor (const deUint32 a, const deUint32 b)
148 {
149         /* Find GCD */
150         deUint32 temp;
151         deUint32 x=a;
152         deUint32 y=b;
153
154         while (x%y != 0)
155         {
156                 temp = y;
157                 y = x%y;
158                 x = temp;
159         }
160         return y;
161 }
162
163 deUint32 lowestCommonMultiple (const deUint32 a, const deUint32 b)
164 {
165         return (a*b)/greatestCommonDivisor(a,b);
166 }
167
168 std::vector<deUint32> getImageMipLevelSizes (const deUint32 pixelSize, const VkExtent3D& baseExtent, const deUint32 numMipLevels, const deUint32 perLevelAlignment = 1u)
169 {
170         std::vector<deUint32> results(numMipLevels);
171
172         for (deUint32 mipLevel = 0; mipLevel < numMipLevels; ++mipLevel)
173         {
174                 const VkExtent3D extent = getMipLevelExtent(baseExtent, mipLevel);
175                 results[mipLevel] = static_cast<deUint32>(extent.width * extent.height * extent.depth * pixelSize);
176                 results[mipLevel] = ((results[mipLevel] + perLevelAlignment-1) / perLevelAlignment) * perLevelAlignment;
177         }
178
179         return results;
180 }
181
182 struct LayerRange
183 {
184         deUint32 baseArrayLayer;
185         deUint32 layerCount;
186 };
187
188 inline bool isInClearRange (const UVec4& clearCoords, const deUint32 x, const deUint32 y, deUint32 arrayLayer = 0, tcu::Maybe<LayerRange> imageViewLayerRange = tcu::Maybe<LayerRange>(), tcu::Maybe<LayerRange> attachmentClearLayerRange = tcu::Maybe<LayerRange>())
189 {
190         if (attachmentClearLayerRange)
191         {
192                 // Only layers in range passed to clear command are cleared
193
194                 const deUint32  clearBaseLayer  = (imageViewLayerRange ? imageViewLayerRange->baseArrayLayer : 0) + attachmentClearLayerRange->baseArrayLayer;
195                 const deUint32  clearLayerCount = (attachmentClearLayerRange->layerCount == VK_REMAINING_ARRAY_LAYERS) ? imageViewLayerRange->layerCount : clearBaseLayer + attachmentClearLayerRange->layerCount;
196
197                 if ((arrayLayer < clearBaseLayer) || (arrayLayer >= (clearLayerCount)))
198                 {
199                         return false;
200                 }
201         }
202
203         if (clearCoords == UVec4())
204         {
205                 return true;
206         }
207
208         //! Check if a point lies in a cross-like area.
209         return !((x <  clearCoords[0] && y <  clearCoords[1]) ||
210                          (x <  clearCoords[0] && y >= clearCoords[3]) ||
211                          (x >= clearCoords[2] && y <  clearCoords[1]) ||
212                          (x >= clearCoords[2] && y >= clearCoords[3]));
213 }
214
215 inline bool isInInitialClearRange (bool isAttachmentformat, deUint32 mipLevel, deUint32 arrayLayer, LayerRange imageViewLayerRange)
216 {
217         if (!isAttachmentformat)
218         {
219                 // initial clear is done using renderpass load op - does not apply for non-renderable formats
220                 return false;
221         }
222
223         if (mipLevel > 0)
224         {
225                 // intial clear is done using FB bound to level 0 only
226                 return false;
227         }
228
229         // Only layers in range bound to framebuffer are cleared to initial color
230         if ((arrayLayer < imageViewLayerRange.baseArrayLayer) || (arrayLayer >= (imageViewLayerRange.baseArrayLayer + imageViewLayerRange.layerCount)))
231         {
232                 return false;
233         }
234
235         return true;
236 }
237
238 // This method is copied from the vktRenderPassTests.cpp. It should be moved to a common place.
239 int calcFloatDiff (float a, float b)
240 {
241         const int                       asign   = Float32(a).sign();
242         const int                       bsign   = Float32(a).sign();
243
244         const deUint32          avalue  = (Float32(a).bits() & ((0x1u << 31u) - 1u));
245         const deUint32          bvalue  = (Float32(b).bits() & ((0x1u << 31u) - 1u));
246
247         if (asign != bsign)
248                 return avalue + bvalue + 1u;
249         else if (avalue < bvalue)
250                 return bvalue - avalue;
251         else
252                 return avalue - bvalue;
253 }
254
255 // This method is copied from the vktRenderPassTests.cpp and extended with the stringResult parameter.
256 bool comparePixelToDepthClearValue (const ConstPixelBufferAccess&       access,
257                                                                         int                                                             x,
258                                                                         int                                                             y,
259                                                                         float                                                   ref,
260                                                                         std::string&                                    stringResult)
261 {
262         const TextureFormat                     format                  = getEffectiveDepthStencilTextureFormat(access.getFormat(), Sampler::MODE_DEPTH);
263         const TextureChannelClass       channelClass    = getTextureChannelClass(format.type);
264
265         switch (channelClass)
266         {
267                 case TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
268                 case TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
269                 {
270                         const int       bitDepth        = getTextureFormatBitDepth(format).x();
271                         const float     depth           = access.getPixDepth(x, y);
272                         const float     threshold       = 2.0f / (float)((1 << bitDepth) - 1);
273                         const bool      result          = deFloatAbs(depth - ref) <= threshold;
274
275                         if (!result)
276                         {
277                                 std::stringstream s;
278                                 s << "Ref:" << ref << " Threshold:" << threshold << " Depth:" << depth;
279                                 stringResult    = s.str();
280                         }
281
282                         return result;
283                 }
284
285                 case TEXTURECHANNELCLASS_FLOATING_POINT:
286                 {
287                         const float     depth                   = access.getPixDepth(x, y);
288                         const int       mantissaBits    = getTextureFormatMantissaBitDepth(format).x();
289                         const int       threshold               = 10 * 1 << (23 - mantissaBits);
290
291                         DE_ASSERT(mantissaBits <= 23);
292
293                         const bool      result                  = calcFloatDiff(depth, ref) <= threshold;
294
295                         if (!result)
296                         {
297                                 float                           floatThreshold  = Float32((deUint32)threshold).asFloat();
298                                 std::stringstream       s;
299
300                                 s << "Ref:" << ref << " Threshold:" << floatThreshold << " Depth:" << depth;
301                                 stringResult    = s.str();
302                         }
303
304                         return result;
305                 }
306
307                 default:
308                         DE_FATAL("Invalid channel class");
309                         return false;
310         }
311 }
312
313 // This method is copied from the vktRenderPassTests.cpp and extended with the stringResult parameter.
314 bool comparePixelToStencilClearValue (const ConstPixelBufferAccess&     access,
315                                                                           int                                                   x,
316                                                                           int                                                   y,
317                                                                           deUint32                                              ref,
318                                                                           std::string&                                  stringResult)
319 {
320         const deUint32  stencil = access.getPixStencil(x, y);
321         const bool              result  = stencil == ref;
322
323         if (!result)
324         {
325                 std::stringstream s;
326                 s << "Ref:" << ref << " Threshold:0" << " Stencil:" << stencil;
327                 stringResult    = s.str();
328         }
329
330         return result;
331 }
332
333 // This method is copied from the vktRenderPassTests.cpp and extended with the stringResult parameter.
334 bool comparePixelToColorClearValue (const ConstPixelBufferAccess&       access,
335                                                                         int                                                             x,
336                                                                         int                                                             y,
337                                                                         int                                                             z,
338                                                                         const VkClearColorValue&                ref,
339                                                                         std::string&                                    stringResult)
340 {
341         const TextureFormat                             format                  = access.getFormat();
342         const TextureChannelClass               channelClass    = getTextureChannelClass(format.type);
343         const BVec4                                             channelMask             = getTextureFormatChannelMask(format);
344
345         switch (channelClass)
346         {
347                 case TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
348                 case TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
349                 {
350                         const IVec4     bitDepth        (getTextureFormatBitDepth(format));
351                         const Vec4      resColor        (access.getPixel(x, y, z));
352                         Vec4            refColor        (ref.float32[0],
353                                                                          ref.float32[1],
354                                                                          ref.float32[2],
355                                                                          ref.float32[3]);
356                         const int       modifier        = (channelClass == TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT) ? 0 : 1;
357                         const Vec4      threshold       (bitDepth[0] > 0 ? 1.0f / ((float)(1 << (bitDepth[0] - modifier)) - 1.0f) : 1.0f,
358                                                                          bitDepth[1] > 0 ? 1.0f / ((float)(1 << (bitDepth[1] - modifier)) - 1.0f) : 1.0f,
359                                                                          bitDepth[2] > 0 ? 1.0f / ((float)(1 << (bitDepth[2] - modifier)) - 1.0f) : 1.0f,
360                                                                          bitDepth[3] > 0 ? 1.0f / ((float)(1 << (bitDepth[3] - modifier)) - 1.0f) : 1.0f);
361
362                         if (isSRGB(access.getFormat()))
363                                 refColor        = linearToSRGB(refColor);
364
365                         const bool      result          = !(anyNotEqual(logicalAnd(lessThanEqual(absDiff(resColor, refColor), threshold), channelMask), channelMask));
366
367                         if (!result)
368                         {
369                                 std::stringstream s;
370                                 s << "Ref:" << refColor << " Mask:" << channelMask << " Threshold:" << threshold << " Color:" << resColor;
371                                 stringResult    = s.str();
372                         }
373
374                         return result;
375                 }
376
377                 case TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
378                 {
379                         const UVec4     resColor        (access.getPixelUint(x, y, z));
380                         const UVec4     refColor        (ref.uint32[0],
381                                                                          ref.uint32[1],
382                                                                          ref.uint32[2],
383                                                                          ref.uint32[3]);
384                         const UVec4     threshold       (1);
385
386                         const bool      result          = !(anyNotEqual(logicalAnd(lessThanEqual(absDiff(resColor, refColor), threshold), channelMask), channelMask));
387
388                         if (!result)
389                         {
390                                 std::stringstream s;
391                                 s << "Ref:" << refColor << " Mask:" << channelMask << " Threshold:" << threshold << " Color:" << resColor;
392                                 stringResult    = s.str();
393                         }
394
395                         return result;
396                 }
397
398                 case TEXTURECHANNELCLASS_SIGNED_INTEGER:
399                 {
400                         const IVec4     resColor        (access.getPixelInt(x, y, z));
401                         const IVec4     refColor        (ref.int32[0],
402                                                                          ref.int32[1],
403                                                                          ref.int32[2],
404                                                                          ref.int32[3]);
405                         const IVec4     threshold       (1);
406
407                         const bool      result          = !(anyNotEqual(logicalAnd(lessThanEqual(absDiff(resColor, refColor), threshold), channelMask), channelMask));
408
409                         if (!result)
410                         {
411                                 std::stringstream s;
412                                 s << "Ref:" << refColor << " Mask:" << channelMask << " Threshold:" << threshold << " Color:" << resColor;
413                                 stringResult    = s.str();
414                         }
415
416                         return result;
417                 }
418
419                 case TEXTURECHANNELCLASS_FLOATING_POINT:
420                 {
421                         const Vec4      resColor                (access.getPixel(x, y, z));
422                         const Vec4      refColor                (ref.float32[0],
423                                                                                  ref.float32[1],
424                                                                                  ref.float32[2],
425                                                                                  ref.float32[3]);
426                         const IVec4     mantissaBits    (getTextureFormatMantissaBitDepth(format));
427                         const IVec4     threshold               (10 * IVec4(1) << (23 - mantissaBits));
428
429                         DE_ASSERT(allEqual(greaterThanEqual(threshold, IVec4(0)), BVec4(true)));
430
431                         for (int ndx = 0; ndx < 4; ndx++)
432                         {
433                                 const bool result       = !(calcFloatDiff(resColor[ndx], refColor[ndx]) > threshold[ndx] && channelMask[ndx]);
434
435                                 if (!result)
436                                 {
437                                         float                           floatThreshold  = Float32((deUint32)(threshold)[0]).asFloat();
438                                         Vec4                            thresholdVec4   (floatThreshold,
439                                                                                                                  floatThreshold,
440                                                                                                                  floatThreshold,
441                                                                                                                  floatThreshold);
442                                         std::stringstream       s;
443                                         s << "Ref:" << refColor << " Mask:" << channelMask << " Threshold:" << thresholdVec4 << " Color:" << resColor;
444                                         stringResult    = s.str();
445
446                                         return false;
447                                 }
448                         }
449
450                         return true;
451                 }
452
453                 default:
454                         DE_FATAL("Invalid channel class");
455                         return false;
456         }
457 }
458
459 struct TestParams
460 {
461         bool                    useSingleMipLevel;      //!< only mip level 0, otherwise up to maxMipLevels
462         VkImageType             imageType;
463         VkFormat                imageFormat;
464         VkExtent3D              imageExtent;
465         deUint32        imageLayerCount;
466         LayerRange      imageViewLayerRange;
467         VkClearValue    initValue;
468         VkClearValue    clearValue[2];          //!< the second value is used with more than one mip map
469         LayerRange              clearLayerRange;
470         AllocationKind  allocationKind;
471 };
472
473 class ImageClearingTestInstance : public vkt::TestInstance
474 {
475 public:
476                                                                                 ImageClearingTestInstance               (Context&                       context,
477                                                                                                                                                  const TestParams&      testParams);
478
479         Move<VkCommandPool>                                     createCommandPool                               (VkCommandPoolCreateFlags commandPoolCreateFlags) const;
480         Move<VkCommandBuffer>                           allocatePrimaryCommandBuffer    (VkCommandPool commandPool) const;
481         Move<VkImage>                                           createImage                                             (VkImageType imageType, VkFormat format, VkExtent3D extent, deUint32 arrayLayerCount, VkImageUsageFlags usage) const;
482         Move<VkImageView>                                       createImageView                                 (VkImage image, VkImageViewType viewType, VkFormat format, VkImageAspectFlags aspectMask, LayerRange layerRange) const;
483         Move<VkRenderPass>                                      createRenderPass                                (VkFormat format) const;
484         Move<VkFramebuffer>                                     createFrameBuffer                               (VkImageView imageView, VkRenderPass renderPass, deUint32 imageWidth, deUint32 imageHeight, deUint32 imageLayersCount) const;
485
486         void                                                            beginCommandBuffer                              (VkCommandBufferUsageFlags usageFlags) const;
487         void                                                            endCommandBuffer                                (void) const;
488         void                                                            submitCommandBuffer                             (void) const;
489         void                                                            beginRenderPass                                 (VkSubpassContents content, VkClearValue clearValue) const;
490
491         void                                                            pipelineImageBarrier                    (VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask, VkImageLayout oldLayout, VkImageLayout newLayout) const;
492         de::MovePtr<TextureLevelPyramid>        readImage                                               (VkImageAspectFlags aspectMask, deUint32 baseLayer) const;
493         tcu::TestStatus                                         verifyResultImage                               (const std::string& successMessage, const UVec4& clearCoords = UVec4()) const;
494
495 protected:
496         enum ViewType
497         {
498                 VIEW_TYPE_SINGLE,
499                 VIEW_TYPE_ARRAY
500         };
501         VkImageViewType                                         getCorrespondingImageViewType   (VkImageType imageType, ViewType viewType) const;
502         VkImageUsageFlags                                       getImageUsageFlags                              (VkFormat format) const;
503         VkImageAspectFlags                                      getImageAspectFlags                             (VkFormat format) const;
504         bool                                                            getIsAttachmentFormat                   (VkFormat format) const;
505         bool                                                            getIsStencilFormat                              (VkFormat format) const;
506         bool                                                            getIsDepthFormat                                (VkFormat format) const;
507         VkImageFormatProperties                         getImageFormatProperties                (void) const;
508         de::MovePtr<Allocation>                         allocateAndBindImageMemory              (VkImage image) const;
509
510         const TestParams&                                       m_params;
511         const VkDevice                                          m_device;
512         const InstanceInterface&                        m_vki;
513         const DeviceInterface&                          m_vkd;
514         const VkQueue                                           m_queue;
515         const deUint32                                          m_queueFamilyIndex;
516         Allocator&                                                      m_allocator;
517
518         const bool                                                      m_isAttachmentFormat;
519         const VkImageUsageFlags                         m_imageUsageFlags;
520         const VkImageAspectFlags                        m_imageAspectFlags;
521         const VkImageFormatProperties           m_imageFormatProperties;
522         const deUint32                                          m_imageMipLevels;
523         const deUint32                                          m_thresholdMipLevel;
524
525         Unique<VkCommandPool>                           m_commandPool;
526         Unique<VkCommandBuffer>                         m_commandBuffer;
527
528         Unique<VkImage>                                         m_image;
529         de::MovePtr<Allocation>                         m_imageMemory;
530         Unique<VkImageView>                                     m_imageView;
531         Unique<VkRenderPass>                            m_renderPass;
532         Unique<VkFramebuffer>                           m_frameBuffer;
533 };
534
535 ImageClearingTestInstance::ImageClearingTestInstance (Context& context, const TestParams& params)
536         : TestInstance                          (context)
537         , m_params                                      (params)
538         , m_device                                      (context.getDevice())
539         , m_vki                                         (context.getInstanceInterface())
540         , m_vkd                                         (context.getDeviceInterface())
541         , m_queue                                       (context.getUniversalQueue())
542         , m_queueFamilyIndex            (context.getUniversalQueueFamilyIndex())
543         , m_allocator                           (context.getDefaultAllocator())
544         , m_isAttachmentFormat          (getIsAttachmentFormat(params.imageFormat))
545         , m_imageUsageFlags                     (getImageUsageFlags(params.imageFormat))
546         , m_imageAspectFlags            (getImageAspectFlags(params.imageFormat))
547         , m_imageFormatProperties       (getImageFormatProperties())
548         , m_imageMipLevels                      (params.useSingleMipLevel ? 1u : getNumMipLevels(params.imageExtent, m_imageFormatProperties.maxMipLevels))
549         , m_thresholdMipLevel           (std::max(m_imageMipLevels / 2u, 1u))
550         , m_commandPool                         (createCommandPool(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT))
551         , m_commandBuffer                       (allocatePrimaryCommandBuffer(*m_commandPool))
552
553         , m_image                                       (createImage(params.imageType,
554                                                                                          params.imageFormat,
555                                                                                          params.imageExtent,
556                                                                                          params.imageLayerCount,
557                                                                                          m_imageUsageFlags))
558
559         , m_imageMemory                         (allocateAndBindImageMemory(*m_image))
560         , m_imageView                           (m_isAttachmentFormat ? createImageView(*m_image,
561                                                                                                  getCorrespondingImageViewType(params.imageType, params.imageLayerCount > 1u ? VIEW_TYPE_ARRAY : VIEW_TYPE_SINGLE),
562                                                                                                  params.imageFormat,
563                                                                                                  m_imageAspectFlags,
564                                                                                                  params.imageViewLayerRange) : vk::Move<VkImageView>())
565
566         , m_renderPass                          (m_isAttachmentFormat ? createRenderPass(params.imageFormat) : vk::Move<vk::VkRenderPass>())
567         , m_frameBuffer                         (m_isAttachmentFormat ? createFrameBuffer(*m_imageView, *m_renderPass, params.imageExtent.width, params.imageExtent.height, params.imageViewLayerRange.layerCount) : vk::Move<vk::VkFramebuffer>())
568 {
569         if (m_params.allocationKind == ALLOCATION_KIND_DEDICATED)
570         {
571                 const std::string extensionName("VK_KHR_dedicated_allocation");
572
573                 if (!de::contains(context.getDeviceExtensions().begin(), context.getDeviceExtensions().end(), extensionName))
574                         TCU_THROW(NotSupportedError, std::string(extensionName + " is not supported").c_str());
575         }
576 }
577
578 VkImageViewType ImageClearingTestInstance::getCorrespondingImageViewType (VkImageType imageType, ViewType viewType) const
579 {
580         switch (imageType)
581         {
582         case VK_IMAGE_TYPE_1D:
583                 return (viewType == VIEW_TYPE_ARRAY) ?  VK_IMAGE_VIEW_TYPE_1D_ARRAY : VK_IMAGE_VIEW_TYPE_1D;
584         case VK_IMAGE_TYPE_2D:
585                 return (viewType == VIEW_TYPE_ARRAY) ?  VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D;
586         case VK_IMAGE_TYPE_3D:
587                 if (viewType != VIEW_TYPE_SINGLE)
588                 {
589                         DE_FATAL("Cannot have 3D image array");
590                 }
591                 return VK_IMAGE_VIEW_TYPE_3D;
592         default:
593                 DE_FATAL("Unknown image type!");
594         }
595
596         return VK_IMAGE_VIEW_TYPE_2D;
597 }
598
599 VkImageUsageFlags ImageClearingTestInstance::getImageUsageFlags (VkFormat format) const
600 {
601         VkImageUsageFlags       commonFlags     = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
602
603         if (m_isAttachmentFormat)
604         {
605                 if (isDepthStencilFormat(format))
606                         return commonFlags | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
607
608                 return commonFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
609         }
610         return commonFlags;
611 }
612
613 VkImageAspectFlags ImageClearingTestInstance::getImageAspectFlags (VkFormat format) const
614 {
615         VkImageAspectFlags      imageAspectFlags        = 0;
616
617         if (getIsDepthFormat(format))
618                 imageAspectFlags |= VK_IMAGE_ASPECT_DEPTH_BIT;
619
620         if (getIsStencilFormat(format))
621                 imageAspectFlags |= VK_IMAGE_ASPECT_STENCIL_BIT;
622
623         if (imageAspectFlags == 0)
624                 imageAspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
625
626         return imageAspectFlags;
627 }
628
629 bool ImageClearingTestInstance::getIsAttachmentFormat (VkFormat format) const
630 {
631         const VkFormatProperties props  = vk::getPhysicalDeviceFormatProperties(m_vki, m_context.getPhysicalDevice(), format);
632
633         return (props.optimalTilingFeatures & (vk::VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | vk::VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) != 0;
634 }
635
636 bool ImageClearingTestInstance::getIsStencilFormat (VkFormat format) const
637 {
638         const TextureFormat tcuFormat   = mapVkFormat(format);
639
640         if (tcuFormat.order == TextureFormat::S || tcuFormat.order == TextureFormat::DS)
641                 return true;
642
643         return false;
644 }
645
646 bool ImageClearingTestInstance::getIsDepthFormat (VkFormat format) const
647 {
648         const TextureFormat     tcuFormat       = mapVkFormat(format);
649
650         if (tcuFormat.order == TextureFormat::D || tcuFormat.order == TextureFormat::DS)
651                 return true;
652
653         return false;
654 }
655
656 VkImageFormatProperties ImageClearingTestInstance::getImageFormatProperties (void) const
657 {
658         VkImageFormatProperties properties;
659         const VkResult result = m_vki.getPhysicalDeviceImageFormatProperties(m_context.getPhysicalDevice(), m_params.imageFormat, m_params.imageType,
660                                                                                                                                                  VK_IMAGE_TILING_OPTIMAL, m_imageUsageFlags, (VkImageCreateFlags)0, &properties);
661
662         if (result == VK_ERROR_FORMAT_NOT_SUPPORTED)
663                 TCU_THROW(NotSupportedError, "Format not supported");
664         else
665                 return properties;
666 }
667
668 de::MovePtr<Allocation> ImageClearingTestInstance::allocateAndBindImageMemory (VkImage image) const
669 {
670         de::MovePtr<Allocation> imageMemory     (allocateImage(m_vki, m_vkd, m_context.getPhysicalDevice(), m_device, image, MemoryRequirement::Any, m_allocator, m_params.allocationKind));
671         VK_CHECK(m_vkd.bindImageMemory(m_device, image, imageMemory->getMemory(), imageMemory->getOffset()));
672         return imageMemory;
673 }
674
675 Move<VkCommandPool> ImageClearingTestInstance::createCommandPool (VkCommandPoolCreateFlags commandPoolCreateFlags) const
676 {
677         return vk::createCommandPool(m_vkd, m_device, commandPoolCreateFlags, m_queueFamilyIndex);
678 }
679
680 Move<VkCommandBuffer> ImageClearingTestInstance::allocatePrimaryCommandBuffer (VkCommandPool commandPool) const
681 {
682         return vk::allocateCommandBuffer(m_vkd, m_device, commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
683 }
684
685 Move<VkImage> ImageClearingTestInstance::createImage (VkImageType imageType, VkFormat format, VkExtent3D extent, deUint32 arrayLayerCount, VkImageUsageFlags usage) const
686 {
687         const VkImageCreateInfo                                 imageCreateInfo                 =
688         {
689                 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,            // VkStructureType                      sType;
690                 DE_NULL,                                                                        // const void*                          pNext;
691                 0u,                                                                                     // VkImageCreateFlags           flags;
692                 imageType,                                                                      // VkImageType                          imageType;
693                 format,                                                                         // VkFormat                                     format;
694                 extent,                                                                         // VkExtent3D                           extent;
695                 m_imageMipLevels,                                                       // deUint32                                     mipLevels;
696                 arrayLayerCount,                                                        // deUint32                                     arrayLayers;
697                 VK_SAMPLE_COUNT_1_BIT,                                          // VkSampleCountFlagBits        samples;
698                 VK_IMAGE_TILING_OPTIMAL,                                        // VkImageTiling                        tiling;
699                 usage,                                                                          // VkImageUsageFlags            usage;
700                 VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                        sharingMode;
701                 1u,                                                                                     // deUint32                                     queueFamilyIndexCount;
702                 &m_queueFamilyIndex,                                            // const deUint32*                      pQueueFamilyIndices;
703                 VK_IMAGE_LAYOUT_UNDEFINED                                       // VkImageLayout                        initialLayout;
704         };
705
706         return vk::createImage(m_vkd, m_device, &imageCreateInfo, DE_NULL);
707 }
708
709 Move<VkImageView> ImageClearingTestInstance::createImageView (VkImage image, VkImageViewType viewType, VkFormat format, VkImageAspectFlags aspectMask, LayerRange layerRange) const
710 {
711         const VkImageViewCreateInfo                             imageViewCreateInfo             =
712         {
713                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,       // VkStructureType                              sType;
714                 DE_NULL,                                                                        // const void*                                  pNext;
715                 0u,                                                                                     // VkImageViewCreateFlags               flags;
716                 image,                                                                          // VkImage                                              image;
717                 viewType,                                                                       // VkImageViewType                              viewType;
718                 format,                                                                         // VkFormat                                             format;
719                 {
720                         VK_COMPONENT_SWIZZLE_IDENTITY,                          // VkComponentSwizzle                   r;
721                         VK_COMPONENT_SWIZZLE_IDENTITY,                          // VkComponentSwizzle                   g;
722                         VK_COMPONENT_SWIZZLE_IDENTITY,                          // VkComponentSwizzle                   b;
723                         VK_COMPONENT_SWIZZLE_IDENTITY,                          // VkComponentSwizzle                   a;
724                 },                                                                                      // VkComponentMapping                   components;
725                 {
726                         aspectMask,                                                                     // VkImageAspectFlags                   aspectMask;
727                         0u,                                                                                     // deUint32                                             baseMipLevel;
728                         1u,                                                                                     // deUint32                                             mipLevels;
729                         layerRange.baseArrayLayer,                                      // deUint32                                             baseArrayLayer;
730                         layerRange.layerCount,                                          // deUint32                                             arraySize;
731                 },                                                                                              // VkImageSubresourceRange              subresourceRange;
732         };
733
734         return vk::createImageView(m_vkd, m_device, &imageViewCreateInfo, DE_NULL);
735 }
736
737 Move<VkRenderPass> ImageClearingTestInstance::createRenderPass (VkFormat format) const
738 {
739         VkImageLayout                                                   imageLayout;
740
741         if (isDepthStencilFormat(format))
742                 imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
743         else
744                 imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
745
746         const VkAttachmentDescription                   attachmentDesc                  =
747         {
748                 0u,                                                                                                     // VkAttachmentDescriptionFlags         flags;
749                 format,                                                                                         // VkFormat                                                     format;
750                 VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
751                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
752                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
753                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           stencilLoadOp;
754                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          stencilStoreOp;
755                 imageLayout,                                                                            // VkImageLayout                                        initialLayout;
756                 imageLayout,                                                                            // VkImageLayout                                        finalLayout;
757         };
758
759         const VkAttachmentDescription                   attachments[1]                  =
760         {
761                 attachmentDesc
762         };
763
764         const VkAttachmentReference                             attachmentRef                   =
765         {
766                 0u,                                                                                                     // deUint32                                                     attachment;
767                 imageLayout,                                                                            // VkImageLayout                                        layout;
768         };
769
770         const VkAttachmentReference*                    pColorAttachments               = DE_NULL;
771         const VkAttachmentReference*                    pDepthStencilAttachment = DE_NULL;
772         deUint32                                                                colorAttachmentCount    = 1;
773
774         if (isDepthStencilFormat(format))
775         {
776                 colorAttachmentCount    = 0;
777                 pDepthStencilAttachment = &attachmentRef;
778         }
779         else
780         {
781                 colorAttachmentCount    = 1;
782                 pColorAttachments               = &attachmentRef;
783         }
784
785         const VkSubpassDescription                              subpassDesc[1]                  =
786         {
787                 {
788                         0u,                                                                                             // VkSubpassDescriptionFlags            flags;
789                         VK_PIPELINE_BIND_POINT_GRAPHICS,                                // VkPipelineBindPoint                          pipelineBindPoint;
790                         0u,                                                                                             // deUint32                                                     inputAttachmentCount;
791                         DE_NULL,                                                                                // const VkAttachmentReference*         pInputAttachments;
792                         colorAttachmentCount,                                                   // deUint32                                                     colorAttachmentCount;
793                         pColorAttachments,                                                              // const VkAttachmentReference*         pColorAttachments;
794                         DE_NULL,                                                                                // const VkAttachmentReference*         pResolveAttachments;
795                         pDepthStencilAttachment,                                                // const VkAttachmentReference*         pDepthStencilAttachment;
796                         0u,                                                                                             // deUint32                                                     preserveAttachmentCount;
797                         DE_NULL,                                                                                // const VkAttachmentReference*         pPreserveAttachments;
798                 }
799         };
800
801         const VkRenderPassCreateInfo                    renderPassCreateInfo    =
802         {
803                 VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
804                 DE_NULL,                                                                                        // const void*                                          pNext;
805                 0u,                                                                                                     // VkRenderPassCreateFlags                      flags;
806                 1u,                                                                                                     // deUint32                                                     attachmentCount;
807                 attachments,                                                                            // const VkAttachmentDescription*       pAttachments;
808                 1u,                                                                                                     // deUint32                                                     subpassCount;
809                 subpassDesc,                                                                            // const VkSubpassDescription*          pSubpasses;
810                 0u,                                                                                                     // deUint32                                                     dependencyCount;
811                 DE_NULL,                                                                                        // const VkSubpassDependency*           pDependencies;
812         };
813
814         return vk::createRenderPass(m_vkd, m_device, &renderPassCreateInfo, DE_NULL);
815 }
816
817 Move<VkFramebuffer> ImageClearingTestInstance::createFrameBuffer (VkImageView imageView, VkRenderPass renderPass, deUint32 imageWidth, deUint32 imageHeight, deUint32 imageLayersCount) const
818 {
819         const VkImageView                                               attachmentViews[1]              =
820         {
821                 imageView
822         };
823
824         const VkFramebufferCreateInfo                   framebufferCreateInfo   =
825         {
826                 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,      // VkStructureType                      sType;
827                 DE_NULL,                                                                        // const void*                          pNext;
828                 0u,                                                                                     // VkFramebufferCreateFlags     flags;
829                 renderPass,                                                                     // VkRenderPass                         renderPass;
830                 1,                                                                                      // deUint32                                     attachmentCount;
831                 attachmentViews,                                                        // const VkImageView*           pAttachments;
832                 imageWidth,                                                                     // deUint32                                     width;
833                 imageHeight,                                                            // deUint32                                     height;
834                 imageLayersCount,                                                       // deUint32                                     layers;
835         };
836
837         return createFramebuffer(m_vkd, m_device, &framebufferCreateInfo, DE_NULL);
838 }
839
840 void ImageClearingTestInstance::beginCommandBuffer (VkCommandBufferUsageFlags usageFlags) const
841 {
842         const VkCommandBufferBeginInfo                  commandBufferBeginInfo  =
843         {
844                 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,                    // VkStructureType                          sType;
845                 DE_NULL,                                                                                                // const void*                              pNext;
846                 usageFlags,                                                                                             // VkCommandBufferUsageFlags                flags;
847                 DE_NULL                                                                                                 // const VkCommandBufferInheritanceInfo*    pInheritanceInfo;
848         };
849
850         VK_CHECK(m_vkd.beginCommandBuffer(*m_commandBuffer, &commandBufferBeginInfo));
851 }
852
853 void ImageClearingTestInstance::endCommandBuffer (void) const
854 {
855         VK_CHECK(m_vkd.endCommandBuffer(*m_commandBuffer));
856 }
857
858 void ImageClearingTestInstance::submitCommandBuffer (void) const
859 {
860         const Unique<VkFence>                                   fence                                   (createFence(m_vkd, m_device));
861
862         const VkSubmitInfo                                              submitInfo                              =
863         {
864                 VK_STRUCTURE_TYPE_SUBMIT_INFO,                                                  // VkStructureType                sType;
865                 DE_NULL,                                                                                                // const void*                    pNext;
866                 0u,                                                                                                             // deUint32                       waitSemaphoreCount;
867                 DE_NULL,                                                                                                // const VkSemaphore*             pWaitSemaphores;
868                 DE_NULL,                                                                                                // const VkPipelineStageFlags*    pWaitDstStageMask;
869                 1u,                                                                                                             // deUint32                       commandBufferCount;
870                 &(*m_commandBuffer),                                                                    // const VkCommandBuffer*         pCommandBuffers;
871                 0u,                                                                                                             // deUint32                       signalSemaphoreCount;
872                 DE_NULL                                                                                                 // const VkSemaphore*             pSignalSemaphores;
873         };
874
875         VK_CHECK(m_vkd.queueSubmit(m_queue, 1, &submitInfo, *fence));
876
877         VK_CHECK(m_vkd.waitForFences(m_device, 1, &fence.get(), VK_TRUE, ~0ull));
878 }
879
880 void ImageClearingTestInstance::pipelineImageBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask, VkImageLayout oldLayout, VkImageLayout newLayout) const
881 {
882         const VkImageMemoryBarrier              imageBarrier    =
883         {
884                 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,         // VkStructureType                      sType;
885                 DE_NULL,                                                                        // const void*                          pNext;
886                 srcAccessMask,                                                          // VkAccessFlags                        srcAccessMask;
887                 dstAccessMask,                                                          // VkAccessFlags                        dstAccessMask;
888                 oldLayout,                                                                      // VkImageLayout                        oldLayout;
889                 newLayout,                                                                      // VkImageLayout                        newLayout;
890                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                                     srcQueueFamilyIndex;
891                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                                     destQueueFamilyIndex;
892                 *m_image,                                                                       // VkImage                                      image;
893                 {
894                         m_imageAspectFlags,                                                     // VkImageAspectFlags   aspectMask;
895                         0u,                                                                                     // deUint32                             baseMipLevel;
896                         VK_REMAINING_MIP_LEVELS,                                        // deUint32                             levelCount;
897                         0u,                                                                                     // deUint32                             baseArrayLayer;
898                         VK_REMAINING_ARRAY_LAYERS,                                      // deUint32                             layerCount;
899                 },                                                                                      // VkImageSubresourceRange      subresourceRange;
900         };
901
902         m_vkd.cmdPipelineBarrier(*m_commandBuffer, srcStageMask, dstStageMask, 0, 0, DE_NULL, 0, DE_NULL, 1, &imageBarrier);
903 }
904
905 de::MovePtr<TextureLevelPyramid> ImageClearingTestInstance::readImage (VkImageAspectFlags aspectMask, deUint32 arrayLayer) const
906 {
907         const TextureFormat                                     tcuFormat               = aspectMask == VK_IMAGE_ASPECT_COLOR_BIT ? mapVkFormat(m_params.imageFormat) :
908                                                                                                                   aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT ? getDepthCopyFormat(m_params.imageFormat) :
909                                                                                                                   aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT ? getStencilCopyFormat(m_params.imageFormat) :
910                                                                                                                   TextureFormat();
911         const deUint32                                          pixelSize               = getPixelSize(tcuFormat);
912         deUint32                                                        alignment               = 4;    // subsequent mip levels aligned to 4 bytes
913
914         if (!getIsDepthFormat(m_params.imageFormat) && !getIsStencilFormat(m_params.imageFormat))
915                 alignment = lowestCommonMultiple(pixelSize, alignment); // alignment must be multiple of pixel size, if not D/S.
916
917         const std::vector<deUint32>                     mipLevelSizes   = getImageMipLevelSizes(pixelSize, m_params.imageExtent, m_imageMipLevels, alignment);
918         const VkDeviceSize                                      imageTotalSize  = std::accumulate(mipLevelSizes.begin(), mipLevelSizes.end(), 0u);
919
920         de::MovePtr<TextureLevelPyramid>        result                  (new TextureLevelPyramid(tcuFormat, m_imageMipLevels));
921         Move<VkBuffer>                                          buffer;
922         de::MovePtr<Allocation>                         bufferAlloc;
923
924         // Create destination buffer
925         {
926                 const VkBufferCreateInfo        bufferParams    =
927                 {
928                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,           // VkStructureType              sType;
929                         DE_NULL,                                                                        // const void*                  pNext;
930                         0u,                                                                                     // VkBufferCreateFlags  flags;
931                         imageTotalSize,                                                         // VkDeviceSize                 size;
932                         VK_BUFFER_USAGE_TRANSFER_DST_BIT,                       // VkBufferUsageFlags   usage;
933                         VK_SHARING_MODE_EXCLUSIVE,                                      // VkSharingMode                sharingMode;
934                         0u,                                                                                     // deUint32                             queueFamilyIndexCount;
935                         DE_NULL                                                                         // const deUint32*              pQueueFamilyIndices;
936                 };
937
938                 buffer          = createBuffer(m_vkd, m_device, &bufferParams);
939                 bufferAlloc     = allocateBuffer(m_vki, m_vkd, m_context.getPhysicalDevice(), m_device, *buffer, MemoryRequirement::HostVisible, m_allocator, m_params.allocationKind);
940                 VK_CHECK(m_vkd.bindBufferMemory(m_device, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
941         }
942
943         // Barriers for copying image to buffer
944
945         const VkBufferMemoryBarrier             bufferBarrier   =
946         {
947                 VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,        // VkStructureType      sType;
948                 DE_NULL,                                                                        // const void*          pNext;
949                 VK_ACCESS_TRANSFER_WRITE_BIT,                           // VkAccessFlags        srcAccessMask;
950                 VK_ACCESS_HOST_READ_BIT,                                        // VkAccessFlags        dstAccessMask;
951                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     srcQueueFamilyIndex;
952                 VK_QUEUE_FAMILY_IGNORED,                                        // deUint32                     dstQueueFamilyIndex;
953                 *buffer,                                                                        // VkBuffer                     buffer;
954                 0u,                                                                                     // VkDeviceSize         offset;
955                 imageTotalSize,                                                         // VkDeviceSize         size;
956         };
957
958         // Copy image to buffer
959         std::vector<VkBufferImageCopy> copyRegions;
960         {
961                 deUint32 offset = 0u;
962                 for (deUint32 mipLevel = 0; mipLevel < m_imageMipLevels; ++mipLevel)
963                 {
964                         const VkExtent3D                extent  = getMipLevelExtent(m_params.imageExtent, mipLevel);
965                         const VkBufferImageCopy region  =
966                         {
967                                 offset,                                                                         // VkDeviceSize                         bufferOffset;
968                                 0u,                                                                                     // deUint32                                     bufferRowLength;
969                                 0u,                                                                                     // deUint32                                     bufferImageHeight;
970                                 { aspectMask, mipLevel, arrayLayer, 1u },       // VkImageSubresourceLayers     imageSubresource;
971                                 { 0, 0, 0 },                                                            // VkOffset3D                           imageOffset;
972                                 extent                                                                          // VkExtent3D                           imageExtent;
973                         };
974                         copyRegions.push_back(region);
975                         offset += mipLevelSizes[mipLevel];
976                 }
977         }
978
979         beginCommandBuffer(0);
980
981         pipelineImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
982                                                  VK_PIPELINE_STAGE_TRANSFER_BIT,
983                                                  VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
984                                                  VK_ACCESS_TRANSFER_READ_BIT,
985                                                  VK_IMAGE_LAYOUT_GENERAL,
986                                                  VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
987
988         m_vkd.cmdCopyImageToBuffer(*m_commandBuffer, *m_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *buffer, static_cast<deUint32>(copyRegions.size()), &copyRegions[0]);
989         m_vkd.cmdPipelineBarrier(*m_commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 1, &bufferBarrier, 0, (const VkImageMemoryBarrier*)DE_NULL);
990
991         pipelineImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
992                                                  VK_PIPELINE_STAGE_TRANSFER_BIT,
993                                                  VK_ACCESS_TRANSFER_READ_BIT,
994                                                  VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
995                                                  VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
996                                                  VK_IMAGE_LAYOUT_GENERAL);
997
998         endCommandBuffer();
999         submitCommandBuffer();
1000
1001         invalidateMappedMemoryRange(m_vkd, m_device, bufferAlloc->getMemory(), bufferAlloc->getOffset(), imageTotalSize);
1002
1003         {
1004                 deUint32 offset = 0u;
1005                 for (deUint32 mipLevel = 0; mipLevel < m_imageMipLevels; ++mipLevel)
1006                 {
1007                         const VkExtent3D        extent          = getMipLevelExtent(m_params.imageExtent, mipLevel);
1008                         const void*                     pLevelData      = static_cast<const void*>(reinterpret_cast<deUint8*>(bufferAlloc->getHostPtr()) + offset);
1009
1010                         result->allocLevel(mipLevel, extent.width, extent.height, extent.depth);
1011                         copy(result->getLevel(mipLevel), ConstPixelBufferAccess(result->getFormat(), result->getLevel(mipLevel).getSize(), pLevelData));
1012
1013                         offset += mipLevelSizes[mipLevel];
1014                 }
1015         }
1016
1017         return result;
1018 }
1019
1020 tcu::TestStatus ImageClearingTestInstance::verifyResultImage (const std::string& successMessage, const UVec4& clearCoords) const
1021 {
1022         DE_ASSERT((clearCoords == UVec4()) || m_params.imageExtent.depth == 1u);
1023
1024         if (getIsDepthFormat(m_params.imageFormat))
1025         {
1026                 DE_ASSERT(m_imageMipLevels == 1u);
1027
1028                 for (deUint32 arrayLayer = 0; arrayLayer < m_params.imageLayerCount; ++arrayLayer)
1029                 {
1030                         de::MovePtr<TextureLevelPyramid>        image                   = readImage(VK_IMAGE_ASPECT_DEPTH_BIT, arrayLayer);
1031                         std::string                                                     message;
1032                         float                                                           depthValue;
1033
1034                         for (deUint32 y = 0; y < m_params.imageExtent.height; ++y)
1035                         for (deUint32 x = 0; x < m_params.imageExtent.width; ++x)
1036                         {
1037                                 if (isInClearRange(clearCoords, x, y, arrayLayer, m_params.imageViewLayerRange, m_params.clearLayerRange))
1038                                         depthValue = m_params.clearValue[0].depthStencil.depth;
1039                                 else
1040                                 if (isInInitialClearRange(m_isAttachmentFormat, 0u /* mipLevel */, arrayLayer, m_params.imageViewLayerRange))
1041                                 {
1042                                         depthValue = m_params.initValue.depthStencil.depth;
1043                                 }
1044                                 else
1045                                         continue;
1046
1047                                 if (!comparePixelToDepthClearValue(image->getLevel(0), x, y, depthValue, message))
1048                                         return TestStatus::fail("Depth value mismatch! " + message);
1049                         }
1050                 }
1051         }
1052
1053         if (getIsStencilFormat(m_params.imageFormat))
1054         {
1055                 DE_ASSERT(m_imageMipLevels == 1u);
1056
1057                 for (deUint32 arrayLayer = 0; arrayLayer < m_params.imageLayerCount; ++arrayLayer)
1058                 {
1059                         de::MovePtr<TextureLevelPyramid>        image                   = readImage(VK_IMAGE_ASPECT_STENCIL_BIT, arrayLayer);
1060                         std::string                                                     message;
1061                         deUint32                                                        stencilValue;
1062
1063                         for (deUint32 y = 0; y < m_params.imageExtent.height; ++y)
1064                         for (deUint32 x = 0; x < m_params.imageExtent.width; ++x)
1065                         {
1066                                 if (isInClearRange(clearCoords, x, y, arrayLayer, m_params.imageViewLayerRange, m_params.clearLayerRange))
1067                                         stencilValue = m_params.clearValue[0].depthStencil.stencil;
1068                                 else
1069                                 if (isInInitialClearRange(m_isAttachmentFormat, 0u /* mipLevel */, arrayLayer, m_params.imageViewLayerRange))
1070                                 {
1071                                         stencilValue = m_params.initValue.depthStencil.stencil;
1072                                 }
1073                                 else
1074                                         continue;
1075
1076                                 if (!comparePixelToStencilClearValue(image->getLevel(0), x, y, stencilValue, message))
1077                                         return TestStatus::fail("Stencil value mismatch! " + message);
1078                         }
1079                 }
1080         }
1081
1082         if (!isDepthStencilFormat(m_params.imageFormat))
1083         {
1084                 for (deUint32 arrayLayer = 0; arrayLayer < m_params.imageLayerCount; ++arrayLayer)
1085                 {
1086                         de::MovePtr<TextureLevelPyramid>        image                   = readImage(VK_IMAGE_ASPECT_COLOR_BIT, arrayLayer);
1087                         std::string                                                     message;
1088                         const VkClearColorValue*                        pColorValue;
1089
1090                         for (deUint32 mipLevel = 0; mipLevel < m_imageMipLevels; ++mipLevel)
1091                         {
1092                                 const int                       clearColorNdx   = (mipLevel < m_thresholdMipLevel ? 0 : 1);
1093                                 const VkExtent3D        extent                  = getMipLevelExtent(m_params.imageExtent, mipLevel);
1094
1095                                 for (deUint32 z = 0; z < extent.depth;  ++z)
1096                                 for (deUint32 y = 0; y < extent.height; ++y)
1097                                 for (deUint32 x = 0; x < extent.width;  ++x)
1098                                 {
1099                                         if (isInClearRange(clearCoords, x, y, arrayLayer, m_params.imageViewLayerRange, m_params.clearLayerRange))
1100                                                 pColorValue = &m_params.clearValue[clearColorNdx].color;
1101                                         else
1102                                         {
1103                                                 if (isInInitialClearRange(m_isAttachmentFormat, mipLevel, arrayLayer, m_params.imageViewLayerRange))
1104                                                 {
1105                                                         pColorValue = &m_params.initValue.color;
1106                                                 }
1107                                                 else
1108                                                         continue;
1109                                         }
1110                                         if (!comparePixelToColorClearValue(image->getLevel(mipLevel), x, y, z, *pColorValue, message))
1111                                                 return TestStatus::fail("Color value mismatch! " + message);
1112                                 }
1113                         }
1114                 }
1115         }
1116
1117         return TestStatus::pass(successMessage);
1118 }
1119
1120 void ImageClearingTestInstance::beginRenderPass (VkSubpassContents content, VkClearValue clearValue) const
1121 {
1122         const VkRenderPassBeginInfo renderPassBeginInfo =
1123         {
1124                 VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                               // VkStructureType              sType;
1125                 DE_NULL,                                                                                                // const void*                  pNext;
1126                 *m_renderPass,                                                                                  // VkRenderPass                 renderPass;
1127                 *m_frameBuffer,                                                                                 // VkFramebuffer                framebuffer;
1128                 {
1129                         { 0, 0 },                                                                                               // VkOffset2D                   offset;
1130                         {
1131                                 m_params.imageExtent.width,                                                             // deUint32                             width;
1132                                 m_params.imageExtent.height                                                             // deUint32                             height;
1133                         }                                                                                                               // VkExtent2D                   extent;
1134                 },                                                                                                              // VkRect2D                             renderArea;
1135                 1u,                                                                                                             // deUint32                             clearValueCount;
1136                 &clearValue                                                                                             // const VkClearValue*  pClearValues;
1137         };
1138
1139         m_vkd.cmdBeginRenderPass(*m_commandBuffer, &renderPassBeginInfo, content);
1140 }
1141
1142 class ClearColorImageTestInstance : public ImageClearingTestInstance
1143 {
1144 public:
1145                                 ClearColorImageTestInstance     (Context& context, const TestParams& testParams, bool twoStep = false) : ImageClearingTestInstance (context, testParams), m_twoStep(twoStep) {}
1146         TestStatus      iterate                                         (void);
1147 protected:
1148         bool            m_twoStep;
1149 };
1150
1151 class TwoStepClearColorImageTestInstance : public ClearColorImageTestInstance
1152 {
1153 public:
1154         TwoStepClearColorImageTestInstance (Context& context, const TestParams& testParams) : ClearColorImageTestInstance(context, testParams, true) {}
1155 };
1156
1157 TestStatus ClearColorImageTestInstance::iterate (void)
1158 {
1159         std::vector<VkImageSubresourceRange> subresourceRanges;
1160         std::vector<VkImageSubresourceRange> steptwoRanges;
1161
1162         if (m_imageMipLevels == 1)
1163         {
1164                 subresourceRanges.push_back(makeImageSubresourceRange(m_imageAspectFlags, 0u,                                   1u,                                                     m_params.clearLayerRange.baseArrayLayer, m_twoStep ? 1 : m_params.clearLayerRange.layerCount));
1165                 steptwoRanges.push_back(        makeImageSubresourceRange(m_imageAspectFlags, 0u,                                       VK_REMAINING_MIP_LEVELS,        m_params.clearLayerRange.baseArrayLayer, VK_REMAINING_ARRAY_LAYERS));
1166         }
1167         else
1168         {
1169                 subresourceRanges.push_back(makeImageSubresourceRange(m_imageAspectFlags, 0u,                                   m_thresholdMipLevel,            m_params.clearLayerRange.baseArrayLayer, m_params.clearLayerRange.layerCount));
1170                 subresourceRanges.push_back(makeImageSubresourceRange(m_imageAspectFlags, m_thresholdMipLevel,  VK_REMAINING_MIP_LEVELS,        m_params.clearLayerRange.baseArrayLayer, m_params.clearLayerRange.layerCount));
1171                 steptwoRanges.push_back(        makeImageSubresourceRange(m_imageAspectFlags, 0u,                                       m_thresholdMipLevel,            m_params.clearLayerRange.baseArrayLayer, VK_REMAINING_ARRAY_LAYERS));
1172                 steptwoRanges.push_back(        makeImageSubresourceRange(m_imageAspectFlags, m_thresholdMipLevel,      VK_REMAINING_MIP_LEVELS,        m_params.clearLayerRange.baseArrayLayer, VK_REMAINING_ARRAY_LAYERS));
1173         }
1174
1175         beginCommandBuffer(0);
1176
1177         pipelineImageBarrier(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,                         // VkPipelineStageFlags         srcStageMask
1178                                                  VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,                    // VkPipelineStageFlags         dstStageMask
1179                                                  0,                                                                                             // VkAccessFlags                        srcAccessMask
1180                                                  (m_isAttachmentFormat
1181                                                         ? VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
1182                                                         : VK_ACCESS_TRANSFER_WRITE_BIT),                        // VkAccessFlags                        dstAccessMask
1183                                                  VK_IMAGE_LAYOUT_UNDEFINED,                                             // VkImageLayout                        oldLayout;
1184                                                  (m_isAttachmentFormat
1185                                                         ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
1186                                                         : VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL));       // VkImageLayout                        newLayout;
1187
1188         if (m_isAttachmentFormat)
1189         {
1190                 beginRenderPass(VK_SUBPASS_CONTENTS_INLINE, m_params.initValue);
1191                 m_vkd.cmdEndRenderPass(*m_commandBuffer);
1192
1193                 pipelineImageBarrier(VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,                // VkPipelineStageFlags         srcStageMask
1194                         VK_PIPELINE_STAGE_TRANSFER_BIT,                                                         // VkPipelineStageFlags         dstStageMask
1195                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                                           // VkAccessFlags                        srcAccessMask
1196                         VK_ACCESS_TRANSFER_WRITE_BIT,                                                           // VkAccessFlags                        dstAccessMask
1197                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                                       // VkImageLayout                        oldLayout;
1198                         VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);                                          // VkImageLayout                        newLayout;
1199         }
1200
1201         // Different clear color per range
1202         for (std::size_t i = 0u; i < subresourceRanges.size(); ++i)
1203         {
1204                 m_vkd.cmdClearColorImage(*m_commandBuffer, *m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &m_params.clearValue[i].color, 1, &subresourceRanges[i]);
1205                 if (m_twoStep)
1206                         m_vkd.cmdClearColorImage(*m_commandBuffer, *m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &m_params.clearValue[i].color, 1, &steptwoRanges[i]);
1207         }
1208
1209         pipelineImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,                            // VkPipelineStageFlags         srcStageMask
1210                                                  VK_PIPELINE_STAGE_TRANSFER_BIT,                                // VkPipelineStageFlags         dstStageMask
1211                                                  VK_ACCESS_TRANSFER_WRITE_BIT,                                  // VkAccessFlags                        srcAccessMask
1212                                                  VK_ACCESS_TRANSFER_READ_BIT,                                   // VkAccessFlags                        dstAccessMask
1213                                                  VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,                  // VkImageLayout                        oldLayout;
1214                                                  VK_IMAGE_LAYOUT_GENERAL);                                              // VkImageLayout                        newLayout;
1215
1216         endCommandBuffer();
1217         submitCommandBuffer();
1218
1219         return verifyResultImage("cmdClearColorImage passed");
1220 }
1221
1222 class ClearDepthStencilImageTestInstance : public ImageClearingTestInstance
1223 {
1224 public:
1225                                 ClearDepthStencilImageTestInstance      (Context& context, const TestParams& testParams, bool twoStep = false) : ImageClearingTestInstance (context, testParams), m_twoStep(twoStep) {}
1226         TestStatus      iterate                                                         (void);
1227 protected:
1228         bool            m_twoStep;
1229 };
1230
1231 class TwoStepClearDepthStencilImageTestInstance : public ClearDepthStencilImageTestInstance
1232 {
1233 public:
1234         TwoStepClearDepthStencilImageTestInstance (Context& context, const TestParams& testParams) : ClearDepthStencilImageTestInstance (context, testParams, true) { }
1235 };
1236
1237 TestStatus ClearDepthStencilImageTestInstance::iterate (void)
1238 {
1239         const VkImageSubresourceRange subresourceRange  = makeImageSubresourceRange(m_imageAspectFlags, 0u, 1u,                                                 m_params.clearLayerRange.baseArrayLayer, m_twoStep ? 1 : m_params.clearLayerRange.layerCount);
1240         const VkImageSubresourceRange steptwoRange              = makeImageSubresourceRange(m_imageAspectFlags, 0u, VK_REMAINING_MIP_LEVELS,    m_params.clearLayerRange.baseArrayLayer, VK_REMAINING_ARRAY_LAYERS);
1241
1242         beginCommandBuffer(0);
1243
1244         pipelineImageBarrier(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,                                 // VkPipelineStageFlags         srcStageMask
1245                                                  VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,                            // VkPipelineStageFlags         dstStageMask
1246                                                  0,                                                                                                     // VkAccessFlags                        srcAccessMask
1247                                                  (m_isAttachmentFormat
1248                                                         ?       VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
1249                                                         :       VK_ACCESS_TRANSFER_WRITE_BIT),                          // VkAccessFlags                        dstAccessMask
1250                                                  VK_IMAGE_LAYOUT_UNDEFINED,                                                     // VkImageLayout                        oldLayout;
1251                                                  (m_isAttachmentFormat
1252                                                         ?       VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
1253                                                         :       VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL));         // VkImageLayout                        newLayout;
1254
1255         if (m_isAttachmentFormat)
1256         {
1257                 beginRenderPass(VK_SUBPASS_CONTENTS_INLINE, m_params.initValue);
1258                 m_vkd.cmdEndRenderPass(*m_commandBuffer);
1259
1260                 pipelineImageBarrier(VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,                                        // VkPipelineStageFlags         srcStageMask
1261                                                          VK_PIPELINE_STAGE_TRANSFER_BIT,                                                // VkPipelineStageFlags         dstStageMask
1262                                                          VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,                  // VkAccessFlags                        srcAccessMask
1263                                                          VK_ACCESS_TRANSFER_WRITE_BIT,                                                  // VkAccessFlags                        dstAccessMask
1264                                                          VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,              // VkImageLayout                        oldLayout;
1265                                                          VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);                                 // VkImageLayout                        newLayout;
1266         }
1267
1268         m_vkd.cmdClearDepthStencilImage(*m_commandBuffer, *m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &m_params.clearValue[0].depthStencil, 1, &subresourceRange);
1269
1270         if (m_twoStep)
1271                 m_vkd.cmdClearDepthStencilImage(*m_commandBuffer, *m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &m_params.clearValue[0].depthStencil, 1, &steptwoRange);
1272
1273         pipelineImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,                                    // VkPipelineStageFlags         srcStageMask
1274                                                  VK_PIPELINE_STAGE_TRANSFER_BIT,                                        // VkPipelineStageFlags         dstStageMask
1275                                                  VK_ACCESS_TRANSFER_WRITE_BIT,                                          // VkAccessFlags                        srcAccessMask
1276                                                  VK_ACCESS_TRANSFER_READ_BIT,                                           // VkAccessFlags                        dstAccessMask
1277                                                  VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,                          // VkImageLayout                        oldLayout;
1278                                                  VK_IMAGE_LAYOUT_GENERAL);                                                      // VkImageLayout                        newLayout;
1279
1280         endCommandBuffer();
1281         submitCommandBuffer();
1282
1283         return verifyResultImage("cmdClearDepthStencilImage passed");
1284 }
1285
1286 class ClearAttachmentTestInstance : public ImageClearingTestInstance
1287 {
1288 public:
1289         enum ClearType
1290         {
1291                 FULL_CLEAR,
1292                 PARTIAL_CLEAR,
1293         };
1294
1295         ClearAttachmentTestInstance (Context& context, const TestParams& testParams, const ClearType clearType = FULL_CLEAR)
1296                 : ImageClearingTestInstance     (context, testParams)
1297                 , m_clearType                           (clearType)
1298         {
1299                 if (!m_isAttachmentFormat)
1300                         TCU_THROW(NotSupportedError, "Format not renderable");
1301         }
1302
1303         TestStatus iterate (void)
1304         {
1305                 const VkClearAttachment clearAttachment =
1306                 {
1307                         m_imageAspectFlags,                                     // VkImageAspectFlags   aspectMask;
1308                         0u,                                                                     // deUint32                             colorAttachment;
1309                         m_params.clearValue[0]                          // VkClearValue                 clearValue;
1310                 };
1311
1312                 UVec4                                           clearCoords;
1313                 std::vector<VkClearRect>        clearRects;
1314
1315                 if (m_clearType == FULL_CLEAR)
1316                 {
1317                         const VkClearRect rect =
1318                         {
1319                                 {
1320                                         { 0, 0 },                                                                                                                                       // VkOffset2D    offset;
1321                                         { m_params.imageExtent.width, m_params.imageExtent.height }                                     // VkExtent2D    extent;
1322                                 },                                                                                                                                                      // VkRect2D     rect;
1323                                 m_params.clearLayerRange.baseArrayLayer,                                                                // deUint32     baseArrayLayer;
1324                                 m_params.clearLayerRange.layerCount,                                                                    // deUint32     layerCount;
1325                         };
1326
1327                         clearRects.push_back(rect);
1328                 }
1329                 else
1330                 {
1331                         const deUint32  clearX          = m_params.imageExtent.width  / 4u;
1332                         const deUint32  clearY          = m_params.imageExtent.height / 4u;
1333                         const deUint32  clearWidth      = m_params.imageExtent.width  / 2u;
1334                         const deUint32  clearHeight     = m_params.imageExtent.height / 2u;
1335
1336                         clearCoords     = UVec4(clearX,                                 clearY,
1337                                                                 clearX + clearWidth,    clearY + clearHeight);
1338
1339                         const VkClearRect rects[2] =
1340                         {
1341                                 {
1342                                         {
1343                                                 { 0,                                                    static_cast<deInt32>(clearY)    },              // VkOffset2D    offset;
1344                                                 { m_params.imageExtent.width,   clearHeight                                             }               // VkExtent2D    extent;
1345                                         },                                                                                                                                              // VkRect2D     rect;
1346                                         m_params.clearLayerRange.baseArrayLayer,                                                                // deUint32     baseArrayLayer;
1347                                         m_params.clearLayerRange.layerCount                                                                             // deUint32     layerCount;
1348                                 },
1349                                 {
1350                                         {
1351                                                 { static_cast<deInt32>(clearX), 0                                                       },                      // VkOffset2D    offset;
1352                                                 { clearWidth,                                   m_params.imageExtent.height     }                       // VkExtent2D    extent;
1353                                         },                                                                                                                                              // VkRect2D     rect;
1354                                         m_params.clearLayerRange.baseArrayLayer,                                                                // deUint32     baseArrayLayer;
1355                                         m_params.clearLayerRange.layerCount                                                                             // deUint32     layerCount;
1356                                 }
1357                         };
1358
1359                         clearRects.push_back(rects[0]);
1360                         clearRects.push_back(rects[1]);
1361                 }
1362
1363                 const bool                      isDepthStencil          = isDepthStencilFormat(m_params.imageFormat);
1364                 const VkAccessFlags     accessMask                      = (isDepthStencil ? VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT     : VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
1365                 const VkImageLayout     attachmentLayout        = (isDepthStencil ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1366
1367                 beginCommandBuffer(0);
1368
1369                 pipelineImageBarrier(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,                         // VkPipelineStageFlags         srcStageMask
1370                                                          VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,                    // VkPipelineStageFlags         dstStageMask
1371                                                          0,                                                                                             // VkAccessFlags                        srcAccessMask
1372                                                          accessMask,                                                                    // VkAccessFlags                        dstAccessMask
1373                                                          VK_IMAGE_LAYOUT_UNDEFINED,                                             // VkImageLayout                        oldLayout;
1374                                                          attachmentLayout);                                                             // VkImageLayout                        newLayout;
1375
1376                 beginRenderPass(VK_SUBPASS_CONTENTS_INLINE, m_params.initValue);
1377                 m_vkd.cmdClearAttachments(*m_commandBuffer, 1, &clearAttachment, static_cast<deUint32>(clearRects.size()), &clearRects[0]);
1378                 m_vkd.cmdEndRenderPass(*m_commandBuffer);
1379
1380                 pipelineImageBarrier(VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,                        // VkPipelineStageFlags         srcStageMask
1381                                                          VK_PIPELINE_STAGE_TRANSFER_BIT,                                // VkPipelineStageFlags         dstStageMask
1382                                                          accessMask,                                                                    // VkAccessFlags                        srcAccessMask
1383                                                          VK_ACCESS_TRANSFER_READ_BIT,                                   // VkAccessFlags                        dstAccessMask
1384                                                          attachmentLayout,                                                              // VkImageLayout                        oldLayout;
1385                                                          VK_IMAGE_LAYOUT_GENERAL);                                              // VkImageLayout                        newLayout;
1386
1387                 endCommandBuffer();
1388                 submitCommandBuffer();
1389
1390                 return verifyResultImage("cmdClearAttachments passed", clearCoords);
1391         }
1392
1393 private:
1394         const ClearType m_clearType;
1395 };
1396
1397 class PartialClearAttachmentTestInstance : public ClearAttachmentTestInstance
1398 {
1399 public:
1400         PartialClearAttachmentTestInstance (Context& context, const TestParams& testParams) : ClearAttachmentTestInstance (context, testParams, PARTIAL_CLEAR) {}
1401 };
1402
1403 VkClearValue makeClearColorValue (VkFormat format, float r, float g, float b, float a)
1404 {
1405         const   TextureFormat tcuFormat = mapVkFormat(format);
1406         VkClearValue clearValue;
1407
1408         if (getTextureChannelClass(tcuFormat.type) == TEXTURECHANNELCLASS_FLOATING_POINT
1409                 || getTextureChannelClass(tcuFormat.type) == TEXTURECHANNELCLASS_SIGNED_FIXED_POINT
1410                 || getTextureChannelClass(tcuFormat.type) == TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT)
1411         {
1412                 clearValue.color.float32[0] = r;
1413                 clearValue.color.float32[1] = g;
1414                 clearValue.color.float32[2] = b;
1415                 clearValue.color.float32[3] = a;
1416         }
1417         else if (getTextureChannelClass(tcuFormat.type) == TEXTURECHANNELCLASS_UNSIGNED_INTEGER)
1418         {
1419                 UVec4 maxValues = getFormatMaxUintValue(tcuFormat);
1420
1421                 clearValue.color.uint32[0] = (deUint32)((float)maxValues[0] * r);
1422                 clearValue.color.uint32[1] = (deUint32)((float)maxValues[1] * g);
1423                 clearValue.color.uint32[2] = (deUint32)((float)maxValues[2] * b);
1424                 clearValue.color.uint32[3] = (deUint32)((float)maxValues[3] * a);
1425         }
1426         else if (getTextureChannelClass(tcuFormat.type) == TEXTURECHANNELCLASS_SIGNED_INTEGER)
1427         {
1428                 IVec4 maxValues = getFormatMaxIntValue(tcuFormat);
1429
1430                 clearValue.color.int32[0] = (deUint32)((float)maxValues[0] * r);
1431                 clearValue.color.int32[1] = (deUint32)((float)maxValues[1] * g);
1432                 clearValue.color.int32[2] = (deUint32)((float)maxValues[2] * b);
1433                 clearValue.color.int32[3] = (deUint32)((float)maxValues[3] * a);
1434         }
1435         else
1436                 DE_FATAL("Unknown channel class");
1437
1438         return clearValue;
1439 }
1440
1441 std::string getFormatCaseName (VkFormat format)
1442 {
1443         return de::toLower(de::toString(getFormatStr(format)).substr(10));
1444 }
1445
1446 const char* getImageTypeCaseName (VkImageType type)
1447 {
1448         const char* s_names[] =
1449         {
1450                 "1d",
1451                 "2d",
1452                 "3d"
1453         };
1454         return de::getSizedArrayElement<VK_IMAGE_TYPE_LAST>(s_names, type);
1455 }
1456
1457 TestCaseGroup* createImageClearingTestsCommon (TestContext& testCtx, tcu::TestCaseGroup* imageClearingTests, AllocationKind allocationKind)
1458 {
1459         de::MovePtr<TestCaseGroup>      colorImageClearTests                                    (new TestCaseGroup(testCtx, "clear_color_image", "Color Image Clear Tests"));
1460         de::MovePtr<TestCaseGroup>      depthStencilImageClearTests                             (new TestCaseGroup(testCtx, "clear_depth_stencil_image", "Color Depth/Stencil Image Tests"));
1461         de::MovePtr<TestCaseGroup>      colorAttachmentClearTests                               (new TestCaseGroup(testCtx, "clear_color_attachment", "Color Color Attachment Tests"));
1462         de::MovePtr<TestCaseGroup>      depthStencilAttachmentClearTests                (new TestCaseGroup(testCtx, "clear_depth_stencil_attachment", "Color Depth/Stencil Attachment Tests"));
1463         de::MovePtr<TestCaseGroup>      partialColorAttachmentClearTests                (new TestCaseGroup(testCtx, "partial_clear_color_attachment", "Clear Partial Color Attachment Tests"));
1464         de::MovePtr<TestCaseGroup>      partialDepthStencilAttachmentClearTests (new TestCaseGroup(testCtx, "partial_clear_depth_stencil_attachment", "Clear Partial Depth/Stencil Attachment Tests"));
1465
1466         // Some formats are commented out due to the tcu::TextureFormat does not support them yet.
1467         const VkFormat          colorImageFormatsToTest[]       =
1468         {
1469                 VK_FORMAT_R4G4_UNORM_PACK8,
1470                 VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1471                 VK_FORMAT_B4G4R4A4_UNORM_PACK16,
1472                 VK_FORMAT_R5G6B5_UNORM_PACK16,
1473                 VK_FORMAT_B5G6R5_UNORM_PACK16,
1474                 VK_FORMAT_R5G5B5A1_UNORM_PACK16,
1475                 VK_FORMAT_B5G5R5A1_UNORM_PACK16,
1476                 VK_FORMAT_A1R5G5B5_UNORM_PACK16,
1477                 VK_FORMAT_R8_UNORM,
1478                 VK_FORMAT_R8_SNORM,
1479                 VK_FORMAT_R8_USCALED,
1480                 VK_FORMAT_R8_SSCALED,
1481                 VK_FORMAT_R8_UINT,
1482                 VK_FORMAT_R8_SINT,
1483                 VK_FORMAT_R8_SRGB,
1484                 VK_FORMAT_R8G8_UNORM,
1485                 VK_FORMAT_R8G8_SNORM,
1486                 VK_FORMAT_R8G8_USCALED,
1487                 VK_FORMAT_R8G8_SSCALED,
1488                 VK_FORMAT_R8G8_UINT,
1489                 VK_FORMAT_R8G8_SINT,
1490                 VK_FORMAT_R8G8_SRGB,
1491                 VK_FORMAT_R8G8B8_UNORM,
1492                 VK_FORMAT_R8G8B8_SNORM,
1493                 VK_FORMAT_R8G8B8_USCALED,
1494                 VK_FORMAT_R8G8B8_SSCALED,
1495                 VK_FORMAT_R8G8B8_UINT,
1496                 VK_FORMAT_R8G8B8_SINT,
1497                 VK_FORMAT_R8G8B8_SRGB,
1498                 VK_FORMAT_B8G8R8_UNORM,
1499                 VK_FORMAT_B8G8R8_SNORM,
1500                 VK_FORMAT_B8G8R8_USCALED,
1501                 VK_FORMAT_B8G8R8_SSCALED,
1502                 VK_FORMAT_B8G8R8_UINT,
1503                 VK_FORMAT_B8G8R8_SINT,
1504                 VK_FORMAT_B8G8R8_SRGB,
1505                 VK_FORMAT_R8G8B8A8_UNORM,
1506                 VK_FORMAT_R8G8B8A8_SNORM,
1507                 VK_FORMAT_R8G8B8A8_USCALED,
1508                 VK_FORMAT_R8G8B8A8_SSCALED,
1509                 VK_FORMAT_R8G8B8A8_UINT,
1510                 VK_FORMAT_R8G8B8A8_SINT,
1511                 VK_FORMAT_R8G8B8A8_SRGB,
1512                 VK_FORMAT_B8G8R8A8_UNORM,
1513                 VK_FORMAT_B8G8R8A8_SNORM,
1514                 VK_FORMAT_B8G8R8A8_USCALED,
1515                 VK_FORMAT_B8G8R8A8_SSCALED,
1516                 VK_FORMAT_B8G8R8A8_UINT,
1517                 VK_FORMAT_B8G8R8A8_SINT,
1518                 VK_FORMAT_B8G8R8A8_SRGB,
1519                 VK_FORMAT_A8B8G8R8_UNORM_PACK32,
1520                 VK_FORMAT_A8B8G8R8_SNORM_PACK32,
1521                 VK_FORMAT_A8B8G8R8_USCALED_PACK32,
1522                 VK_FORMAT_A8B8G8R8_SSCALED_PACK32,
1523                 VK_FORMAT_A8B8G8R8_UINT_PACK32,
1524                 VK_FORMAT_A8B8G8R8_SINT_PACK32,
1525                 VK_FORMAT_A8B8G8R8_SRGB_PACK32,
1526                 VK_FORMAT_A2R10G10B10_UNORM_PACK32,
1527                 VK_FORMAT_A2R10G10B10_SNORM_PACK32,
1528                 VK_FORMAT_A2R10G10B10_USCALED_PACK32,
1529                 VK_FORMAT_A2R10G10B10_SSCALED_PACK32,
1530                 VK_FORMAT_A2R10G10B10_UINT_PACK32,
1531                 VK_FORMAT_A2R10G10B10_SINT_PACK32,
1532                 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
1533                 VK_FORMAT_A2B10G10R10_SNORM_PACK32,
1534                 VK_FORMAT_A2B10G10R10_USCALED_PACK32,
1535                 VK_FORMAT_A2B10G10R10_SSCALED_PACK32,
1536                 VK_FORMAT_A2B10G10R10_UINT_PACK32,
1537                 VK_FORMAT_A2B10G10R10_SINT_PACK32,
1538                 VK_FORMAT_R16_UNORM,
1539                 VK_FORMAT_R16_SNORM,
1540                 VK_FORMAT_R16_USCALED,
1541                 VK_FORMAT_R16_SSCALED,
1542                 VK_FORMAT_R16_UINT,
1543                 VK_FORMAT_R16_SINT,
1544                 VK_FORMAT_R16_SFLOAT,
1545                 VK_FORMAT_R16G16_UNORM,
1546                 VK_FORMAT_R16G16_SNORM,
1547                 VK_FORMAT_R16G16_USCALED,
1548                 VK_FORMAT_R16G16_SSCALED,
1549                 VK_FORMAT_R16G16_UINT,
1550                 VK_FORMAT_R16G16_SINT,
1551                 VK_FORMAT_R16G16_SFLOAT,
1552                 VK_FORMAT_R16G16B16_UNORM,
1553                 VK_FORMAT_R16G16B16_SNORM,
1554                 VK_FORMAT_R16G16B16_USCALED,
1555                 VK_FORMAT_R16G16B16_SSCALED,
1556                 VK_FORMAT_R16G16B16_UINT,
1557                 VK_FORMAT_R16G16B16_SINT,
1558                 VK_FORMAT_R16G16B16_SFLOAT,
1559                 VK_FORMAT_R16G16B16A16_UNORM,
1560                 VK_FORMAT_R16G16B16A16_SNORM,
1561                 VK_FORMAT_R16G16B16A16_USCALED,
1562                 VK_FORMAT_R16G16B16A16_SSCALED,
1563                 VK_FORMAT_R16G16B16A16_UINT,
1564                 VK_FORMAT_R16G16B16A16_SINT,
1565                 VK_FORMAT_R16G16B16A16_SFLOAT,
1566                 VK_FORMAT_R32_UINT,
1567                 VK_FORMAT_R32_SINT,
1568                 VK_FORMAT_R32_SFLOAT,
1569                 VK_FORMAT_R32G32_UINT,
1570                 VK_FORMAT_R32G32_SINT,
1571                 VK_FORMAT_R32G32_SFLOAT,
1572                 VK_FORMAT_R32G32B32_UINT,
1573                 VK_FORMAT_R32G32B32_SINT,
1574                 VK_FORMAT_R32G32B32_SFLOAT,
1575                 VK_FORMAT_R32G32B32A32_UINT,
1576                 VK_FORMAT_R32G32B32A32_SINT,
1577                 VK_FORMAT_R32G32B32A32_SFLOAT,
1578 //              VK_FORMAT_R64_UINT,
1579 //              VK_FORMAT_R64_SINT,
1580 //              VK_FORMAT_R64_SFLOAT,
1581 //              VK_FORMAT_R64G64_UINT,
1582 //              VK_FORMAT_R64G64_SINT,
1583 //              VK_FORMAT_R64G64_SFLOAT,
1584 //              VK_FORMAT_R64G64B64_UINT,
1585 //              VK_FORMAT_R64G64B64_SINT,
1586 //              VK_FORMAT_R64G64B64_SFLOAT,
1587 //              VK_FORMAT_R64G64B64A64_UINT,
1588 //              VK_FORMAT_R64G64B64A64_SINT,
1589 //              VK_FORMAT_R64G64B64A64_SFLOAT,
1590                 VK_FORMAT_B10G11R11_UFLOAT_PACK32,
1591                 VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
1592 //              VK_FORMAT_BC1_RGB_UNORM_BLOCK,
1593 //              VK_FORMAT_BC1_RGB_SRGB_BLOCK,
1594 //              VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
1595 //              VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
1596 //              VK_FORMAT_BC2_UNORM_BLOCK,
1597 //              VK_FORMAT_BC2_SRGB_BLOCK,
1598 //              VK_FORMAT_BC3_UNORM_BLOCK,
1599 //              VK_FORMAT_BC3_SRGB_BLOCK,
1600 //              VK_FORMAT_BC4_UNORM_BLOCK,
1601 //              VK_FORMAT_BC4_SNORM_BLOCK,
1602 //              VK_FORMAT_BC5_UNORM_BLOCK,
1603 //              VK_FORMAT_BC5_SNORM_BLOCK,
1604 //              VK_FORMAT_BC6H_UFLOAT_BLOCK,
1605 //              VK_FORMAT_BC6H_SFLOAT_BLOCK,
1606 //              VK_FORMAT_BC7_UNORM_BLOCK,
1607 //              VK_FORMAT_BC7_SRGB_BLOCK,
1608 //              VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
1609 //              VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,
1610 //              VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK,
1611 //              VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK,
1612 //              VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK,
1613 //              VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK,
1614 //              VK_FORMAT_EAC_R11_UNORM_BLOCK,
1615 //              VK_FORMAT_EAC_R11_SNORM_BLOCK,
1616 //              VK_FORMAT_EAC_R11G11_UNORM_BLOCK,
1617 //              VK_FORMAT_EAC_R11G11_SNORM_BLOCK,
1618 //              VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
1619 //              VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
1620 //              VK_FORMAT_ASTC_5x4_UNORM_BLOCK,
1621 //              VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
1622 //              VK_FORMAT_ASTC_5x5_UNORM_BLOCK,
1623 //              VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
1624 //              VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
1625 //              VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
1626 //              VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
1627 //              VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
1628 //              VK_FORMAT_ASTC_8x5_UNORM_BLOCK,
1629 //              VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
1630 //              VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
1631 //              VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
1632 //              VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
1633 //              VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
1634 //              VK_FORMAT_ASTC_10x5_UNORM_BLOCK,
1635 //              VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
1636 //              VK_FORMAT_ASTC_10x6_UNORM_BLOCK,
1637 //              VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
1638 //              VK_FORMAT_ASTC_10x8_UNORM_BLOCK,
1639 //              VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
1640 //              VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
1641 //              VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
1642 //              VK_FORMAT_ASTC_12x10_UNORM_BLOCK,
1643 //              VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
1644 //              VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
1645 //              VK_FORMAT_ASTC_12x12_SRGB_BLOCK
1646         };
1647         const size_t    numOfColorImageFormatsToTest                    = DE_LENGTH_OF_ARRAY(colorImageFormatsToTest);
1648
1649         const VkFormat  depthStencilImageFormatsToTest[]                =
1650         {
1651                 VK_FORMAT_D16_UNORM,
1652                 VK_FORMAT_X8_D24_UNORM_PACK32,
1653                 VK_FORMAT_D32_SFLOAT,
1654                 VK_FORMAT_S8_UINT,
1655                 VK_FORMAT_D16_UNORM_S8_UINT,
1656                 VK_FORMAT_D24_UNORM_S8_UINT,
1657                 VK_FORMAT_D32_SFLOAT_S8_UINT
1658         };
1659         const size_t    numOfDepthStencilImageFormatsToTest             = DE_LENGTH_OF_ARRAY(depthStencilImageFormatsToTest);
1660
1661         struct ImageLayerParams
1662         {
1663                 deUint32                imageLayerCount;
1664                 LayerRange              imageViewRange;
1665                 LayerRange              clearLayerRange;
1666                 bool                    twoStep;
1667                 const char*             testName;
1668         };
1669         const ImageLayerParams imageLayerParamsToTest[] =
1670         {
1671                 {
1672                         1u,                                                                     // imageLayerCount
1673                         {0u, 1u},                                                       // imageViewRange
1674                         {0u, 1u},                                                       // clearLayerRange
1675                         false,                                                          // twoStep
1676                         "single_layer"                                          // testName
1677                 },
1678                 {
1679                         16u,                                                            // imageLayerCount
1680                         {3u, 12u},                                                      // imageViewRange
1681                         {2u, 5u},                                                       // clearLayerRange
1682                         false,                                                          // twoStep
1683                         "multiple_layers"                                       // testName
1684                 },
1685                 {
1686                         16u,                                                            // imageLayerCount
1687                         { 3u, 12u },                                            // imageViewRange
1688                         { 8u, VK_REMAINING_ARRAY_LAYERS },      // clearLayerRange
1689                         false,                                                          // twoStep
1690                         "remaining_array_layers"                        // testName
1691                 },
1692                 {
1693                         16u,                                                            // imageLayerCount
1694                         { 3u, 12u },                                            // imageViewRange
1695                         { 8u, VK_REMAINING_ARRAY_LAYERS },      // clearLayerRange
1696                         true,                                                           // twoStep
1697                         "remaining_array_layers_twostep"        // testName
1698                 }
1699         };
1700
1701         // Include test cases with VK_REMAINING_ARRAY_LAYERS when using vkCmdClearColorImage
1702         const size_t    numOfImageLayerParamsToTest                             = DE_LENGTH_OF_ARRAY(imageLayerParamsToTest);
1703
1704         // Exclude test cases with VK_REMAINING_ARRAY_LAYERS when using vkCmdClearAttachments
1705         const size_t    numOfAttachmentLayerParamsToTest                = numOfImageLayerParamsToTest - 2;
1706
1707         // Clear color image
1708         {
1709                 const VkImageType                       imageTypesToTest[]              =
1710                 {
1711                         VK_IMAGE_TYPE_1D,
1712                         VK_IMAGE_TYPE_2D,
1713                         VK_IMAGE_TYPE_3D
1714                 };
1715                 const size_t                            numOfImageTypesToTest   = DE_LENGTH_OF_ARRAY(imageTypesToTest);
1716
1717                 const VkExtent3D                        imageDimensionsByType[] =
1718                 {
1719                         { 256, 1, 1},
1720                         { 256, 256, 1},
1721                         { 256, 256, 16}
1722                 };
1723
1724                 for (size_t     imageTypeIndex = 0; imageTypeIndex < numOfImageTypesToTest; ++imageTypeIndex)
1725                 {
1726                         de::MovePtr<TestCaseGroup> imageTypeGroup(new TestCaseGroup(testCtx, getImageTypeCaseName(imageTypesToTest[imageTypeIndex]), ""));
1727
1728                         for (size_t imageLayerParamsIndex = 0; imageLayerParamsIndex < numOfImageLayerParamsToTest; ++imageLayerParamsIndex)
1729                         {
1730                                 // 3D ARRAY images are not supported
1731                                 if (imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount > 1u && imageTypesToTest[imageTypeIndex] == VK_IMAGE_TYPE_3D)
1732                                         continue;
1733
1734                                 de::MovePtr<TestCaseGroup> imageLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1735
1736                                 for (size_t imageFormatIndex = 0; imageFormatIndex < numOfColorImageFormatsToTest; ++imageFormatIndex)
1737                                 {
1738                                         const VkFormat          format                  = colorImageFormatsToTest[imageFormatIndex];
1739                                         const std::string       testCaseName    = getFormatCaseName(format);
1740                                         const TestParams        testParams              =
1741                                         {
1742                                                 false,                                                                                                                          // bool                         useSingleMipLevel;
1743                                                 imageTypesToTest[imageTypeIndex],                                                                       // VkImageType          imageType;
1744                                                 format,                                                                                                                         // VkFormat                     imageFormat;
1745                                                 imageDimensionsByType[imageTypeIndex],                                                          // VkExtent3D           imageExtent;
1746                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount,          // deUint32         imageLayerCount;
1747                                                 {
1748                                                         0u,
1749                                                         imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount
1750                                                 },                                                                                                                                      // LayerRange           imageViewLayerRange;
1751                                                 makeClearColorValue(format, 0.2f, 0.1f, 0.7f, 0.8f),                            // VkClearValue         initValue;
1752                                                 {
1753                                                         makeClearColorValue(format, 0.1f, 0.5f, 0.3f, 0.9f),                            // VkClearValue         clearValue[0];
1754                                                         makeClearColorValue(format, 0.3f, 0.6f, 0.2f, 0.7f),                            // VkClearValue         clearValue[1];
1755                                                 },
1756                                                 imageLayerParamsToTest[imageLayerParamsIndex].clearLayerRange,          // LayerRange       clearLayerRange;
1757                                                 allocationKind                                                                                                          // AllocationKind       allocationKind;
1758                                         };
1759                                         if (!imageLayerParamsToTest[imageLayerParamsIndex].twoStep)
1760                                                 imageLayersGroup->addChild(new InstanceFactory1<ClearColorImageTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Color Image", testParams));
1761                                         else
1762                                                 imageLayersGroup->addChild(new InstanceFactory1<TwoStepClearColorImageTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Color Image", testParams));
1763                                 }
1764                                 imageTypeGroup->addChild(imageLayersGroup.release());
1765                         }
1766                         colorImageClearTests->addChild(imageTypeGroup.release());
1767                 }
1768                 imageClearingTests->addChild(colorImageClearTests.release());
1769         }
1770
1771         // Clear depth/stencil image
1772         {
1773                 for (size_t imageLayerParamsIndex = 0; imageLayerParamsIndex < numOfImageLayerParamsToTest; ++imageLayerParamsIndex)
1774                 {
1775                         de::MovePtr<TestCaseGroup> imageLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1776
1777                         for (size_t imageFormatIndex = 0; imageFormatIndex < numOfDepthStencilImageFormatsToTest; ++imageFormatIndex)
1778                         {
1779                                 const VkFormat          format                  = depthStencilImageFormatsToTest[imageFormatIndex];
1780                                 const std::string       testCaseName    = getFormatCaseName(format);
1781                                 const TestParams        testParams              =
1782                                 {
1783                                         true,                                                                                                                           // bool                         useSingleMipLevel;
1784                                         VK_IMAGE_TYPE_2D,                                                                                                       // VkImageType          imageType;
1785                                         format,                                                                                                                         // VkFormat                     format;
1786                                         { 256, 256, 1 },                                                                                                        // VkExtent3D           extent;
1787                                         imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount,          // deUint32         imageLayerCount;
1788                                         {
1789                                                 0u,
1790                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount
1791                                         },                                                                                                                                      // LayerRange           imageViewLayerRange;
1792                                         makeClearValueDepthStencil(0.5f, 0x03),                                                         // VkClearValue         initValue
1793                                         {
1794                                                 makeClearValueDepthStencil(0.1f, 0x06),                                                         // VkClearValue         clearValue[0];
1795                                                 makeClearValueDepthStencil(0.3f, 0x04),                                                         // VkClearValue         clearValue[1];
1796                                         },
1797                                         imageLayerParamsToTest[imageLayerParamsIndex].clearLayerRange,          // LayerRange       clearLayerRange;
1798                                         allocationKind                                                                                                          // AllocationKind       allocationKind;
1799                                 };
1800
1801                                 if (!imageLayerParamsToTest[imageLayerParamsIndex].twoStep)
1802                                         imageLayersGroup->addChild(new InstanceFactory1<ClearDepthStencilImageTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Depth/Stencil Image", testParams));
1803                                 else
1804                                         imageLayersGroup->addChild(new InstanceFactory1<TwoStepClearDepthStencilImageTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Depth/Stencil Image", testParams));
1805                         }
1806                         depthStencilImageClearTests->addChild(imageLayersGroup.release());
1807                 }
1808                 imageClearingTests->addChild(depthStencilImageClearTests.release());
1809         }
1810
1811         // Clear color attachment
1812         {
1813                 for (size_t imageLayerParamsIndex = 0; imageLayerParamsIndex < numOfAttachmentLayerParamsToTest; ++imageLayerParamsIndex)
1814                 {
1815                         if (!imageLayerParamsToTest[imageLayerParamsIndex].twoStep)
1816                         {
1817                                 de::MovePtr<TestCaseGroup> colorAttachmentClearLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1818                                 de::MovePtr<TestCaseGroup> partialColorAttachmentClearLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1819
1820                                 for (size_t imageFormatIndex = 0; imageFormatIndex < numOfColorImageFormatsToTest; ++imageFormatIndex)
1821                                 {
1822                                         const VkFormat          format                  = colorImageFormatsToTest[imageFormatIndex];
1823                                         const std::string       testCaseName    = getFormatCaseName(format);
1824                                         const TestParams        testParams              =
1825                                         {
1826                                                 true,                                                                                                                   // bool                         useSingleMipLevel;
1827                                                 VK_IMAGE_TYPE_2D,                                                                                               // VkImageType          imageType;
1828                                                 format,                                                                                                                 // VkFormat                     format;
1829                                                 { 256, 256, 1 },                                                                                                // VkExtent3D           extent;
1830                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount,  // deUint32         imageLayerCount;
1831                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageViewRange,   // LayerRange           imageViewLayerRange;
1832                                                 makeClearColorValue(format, 0.2f, 0.1f, 0.7f, 0.8f),                    // VkClearValue         initValue
1833                                                 {
1834                                                         makeClearColorValue(format, 0.1f, 0.5f, 0.3f, 0.9f),                    // VkClearValue         clearValue[0];
1835                                                         makeClearColorValue(format, 0.3f, 0.6f, 0.2f, 0.7f),                    // VkClearValue         clearValue[1];
1836                                                 },
1837                                                 imageLayerParamsToTest[imageLayerParamsIndex].clearLayerRange,  // LayerRange       clearLayerRange;
1838                                                 allocationKind                                                                                                  // AllocationKind       allocationKind;
1839                                         };
1840                                         colorAttachmentClearLayersGroup->addChild(new InstanceFactory1<ClearAttachmentTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Color Attachment", testParams));
1841                                         partialColorAttachmentClearLayersGroup->addChild(new InstanceFactory1<PartialClearAttachmentTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Partial Clear Color Attachment", testParams));
1842                                 }
1843                                 colorAttachmentClearTests->addChild(colorAttachmentClearLayersGroup.release());
1844                                 partialColorAttachmentClearTests->addChild(partialColorAttachmentClearLayersGroup.release());
1845                         }
1846                 }
1847                 imageClearingTests->addChild(colorAttachmentClearTests.release());
1848                 imageClearingTests->addChild(partialColorAttachmentClearTests.release());
1849         }
1850
1851         // Clear depth/stencil attachment
1852         {
1853                 for (size_t imageLayerParamsIndex = 0; imageLayerParamsIndex < numOfAttachmentLayerParamsToTest; ++imageLayerParamsIndex)
1854                 {
1855                         if (!imageLayerParamsToTest[imageLayerParamsIndex].twoStep)
1856                         {
1857                                 de::MovePtr<TestCaseGroup> depthStencilLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1858                                 de::MovePtr<TestCaseGroup> partialDepthStencilLayersGroup(new TestCaseGroup(testCtx, imageLayerParamsToTest[imageLayerParamsIndex].testName, ""));
1859
1860                                 for (size_t imageFormatIndex = 0; imageFormatIndex < numOfDepthStencilImageFormatsToTest; ++imageFormatIndex)
1861                                 {
1862                                         const VkFormat          format                  = depthStencilImageFormatsToTest[imageFormatIndex];
1863                                         const std::string       testCaseName    = getFormatCaseName(format);
1864                                         const TestParams        testParams              =
1865                                         {
1866                                                 true,                                                                                                                   // bool                         useSingleMipLevel;
1867                                                 VK_IMAGE_TYPE_2D,                                                                                               // VkImageType          imageType;
1868                                                 format,                                                                                                                 // VkFormat                     format;
1869                                                 { 256, 256, 1 },                                                                                                // VkExtent3D           extent;
1870                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageLayerCount,  // deUint32         imageLayerCount;
1871                                                 imageLayerParamsToTest[imageLayerParamsIndex].imageViewRange,   // LayerRange           imageViewLayerRange;
1872                                                 makeClearValueDepthStencil(0.5f, 0x03),                                                 // VkClearValue         initValue
1873                                                 {
1874                                                         makeClearValueDepthStencil(0.1f, 0x06),                                                 // VkClearValue         clearValue[0];
1875                                                         makeClearValueDepthStencil(0.3f, 0x04),                                                 // VkClearValue         clearValue[1];
1876                                                 },
1877                                                 imageLayerParamsToTest[imageLayerParamsIndex].clearLayerRange,  // LayerRange       clearLayerRange;
1878                                                 allocationKind                                                                                                  // AllocationKind       allocationKind;
1879                                         };
1880                                         depthStencilLayersGroup->addChild(new InstanceFactory1<ClearAttachmentTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Clear Depth/Stencil Attachment", testParams));
1881                                         partialDepthStencilLayersGroup->addChild(new InstanceFactory1<PartialClearAttachmentTestInstance, TestParams>(testCtx, NODETYPE_SELF_VALIDATE, testCaseName, "Parital Clear Depth/Stencil Attachment", testParams));
1882                                 }
1883                                 depthStencilAttachmentClearTests->addChild(depthStencilLayersGroup.release());
1884                                 partialDepthStencilAttachmentClearTests->addChild(partialDepthStencilLayersGroup.release());
1885                         }
1886                 }
1887                 imageClearingTests->addChild(depthStencilAttachmentClearTests.release());
1888                 imageClearingTests->addChild(partialDepthStencilAttachmentClearTests.release());
1889         }
1890
1891         return imageClearingTests;
1892 }
1893
1894 void createCoreImageClearingTests (tcu::TestCaseGroup* group)
1895 {
1896         createImageClearingTestsCommon(group->getTestContext(), group, ALLOCATION_KIND_SUBALLOCATED);
1897 }
1898
1899 void createDedicatedAllocationImageClearingTests (tcu::TestCaseGroup* group)
1900 {
1901         createImageClearingTestsCommon(group->getTestContext(), group, ALLOCATION_KIND_DEDICATED);
1902 }
1903
1904 } // anonymous
1905
1906 TestCaseGroup* createImageClearingTests (TestContext& testCtx)
1907 {
1908         de::MovePtr<TestCaseGroup>      imageClearingTests      (new TestCaseGroup(testCtx, "image_clearing", "Image Clearing Tests"));
1909
1910         imageClearingTests->addChild(createTestGroup(testCtx, "core",                                   "Core Image Clearing Tests",                                                    createCoreImageClearingTests));
1911         imageClearingTests->addChild(createTestGroup(testCtx, "dedicated_allocation",   "Image Clearing Tests For Dedicated Memory Allocation", createDedicatedAllocationImageClearingTests));
1912
1913         return imageClearingTests.release();
1914 }
1915
1916 } // api
1917 } // vkt