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