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