Merge vk-gl-cts/vulkan-cts-1.0.0 into vk-gl-cts/vulkan-cts-1.0.1
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / sparse_resources / vktSparseResourcesShaderIntrinsicsBase.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*
20  * \file  vktSparseResourcesShaderIntrinsicsBase.cpp
21  * \brief Sparse Resources Shader Intrinsics Base Classes
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktSparseResourcesShaderIntrinsicsBase.hpp"
25
26 using namespace vk;
27
28 namespace vkt
29 {
30 namespace sparse
31 {
32
33 tcu::UVec3 alignedDivide (const VkExtent3D& extent, const VkExtent3D& divisor)
34 {
35         tcu::UVec3 result;
36
37         result.x() = extent.width  / divisor.width  + ((extent.width  % divisor.width)  ? 1u : 0u);
38         result.y() = extent.height / divisor.height + ((extent.height % divisor.height) ? 1u : 0u);
39         result.z() = extent.depth  / divisor.depth  + ((extent.depth  % divisor.depth)  ? 1u : 0u);
40
41         return result;
42 }
43
44 std::string getOpTypeImageComponent (const tcu::TextureFormat& format)
45 {
46         switch (tcu::getTextureChannelClass(format.type))
47         {
48                 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
49                         return "OpTypeInt 32 0";
50                 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
51                         return "OpTypeInt 32 1";
52                 default:
53                         DE_ASSERT(0);
54                         return "";
55         }
56 }
57
58 std::string getOpTypeImageSparse (const ImageType                       imageType,
59                                                                   const tcu::TextureFormat&     format,
60                                                                   const std::string&            componentType,
61                                                                   const bool                            requiresSampler)
62 {
63         std::ostringstream      src;
64
65         src << "OpTypeImage " << componentType << " ";
66
67         switch (imageType)
68         {
69                 case IMAGE_TYPE_1D :
70                         src << "1D 0 0 0 ";
71                 break;
72                 case IMAGE_TYPE_1D_ARRAY :
73                         src << "1D 0 1 0 ";
74                 break;
75                 case IMAGE_TYPE_2D :
76                         src << "2D 0 0 0 ";
77                 break;
78                 case IMAGE_TYPE_2D_ARRAY :
79                         src << "2D 0 1 0 ";
80                 break;
81                 case IMAGE_TYPE_3D :
82                         src << "3D 0 0 0 ";
83                 break;
84                 case IMAGE_TYPE_CUBE :
85                         src << "Cube 0 0 0 ";
86                 break;
87                 case IMAGE_TYPE_CUBE_ARRAY :
88                         src << "Cube 0 1 0 ";
89                 break;
90                 default :
91                         DE_ASSERT(0);
92                 break;
93         };
94
95         if (requiresSampler)
96                 src << "1 ";
97         else
98                 src << "2 ";
99
100         switch (format.order)
101         {
102                 case tcu::TextureFormat::R:
103                         src << "R";
104                 break;
105                 case tcu::TextureFormat::RG:
106                         src << "Rg";
107                         break;
108                 case tcu::TextureFormat::RGB:
109                         src << "Rgb";
110                         break;
111                 case tcu::TextureFormat::RGBA:
112                         src << "Rgba";
113                 break;
114                 default:
115                         DE_ASSERT(0);
116                 break;
117         }
118
119         switch (format.type)
120         {
121                 case tcu::TextureFormat::SIGNED_INT8:
122                         src << "8i";
123                 break;
124                 case tcu::TextureFormat::SIGNED_INT16:
125                         src << "16i";
126                 break;
127                 case tcu::TextureFormat::SIGNED_INT32:
128                         src << "32i";
129                 break;
130                 case tcu::TextureFormat::UNSIGNED_INT8:
131                         src << "8ui";
132                 break;
133                 case tcu::TextureFormat::UNSIGNED_INT16:
134                         src << "16ui";
135                 break;
136                 case tcu::TextureFormat::UNSIGNED_INT32:
137                         src << "32ui";
138                 break;
139                 default:
140                         DE_ASSERT(0);
141                 break;
142         };
143
144         return src.str();
145 }
146
147 std::string getOpTypeImageResidency (const ImageType imageType)
148 {
149         std::ostringstream      src;
150
151         src << "OpTypeImage %type_uint ";
152
153         switch (imageType)
154         {
155                 case IMAGE_TYPE_1D :
156                         src << "1D 0 0 0 2 R32ui";
157                 break;
158                 case IMAGE_TYPE_1D_ARRAY :
159                         src << "1D 0 1 0 2 R32ui";
160                 break;
161                 case IMAGE_TYPE_2D :
162                         src << "2D 0 0 0 2 R32ui";
163                 break;
164                 case IMAGE_TYPE_2D_ARRAY :
165                         src << "2D 0 1 0 2 R32ui";
166                 break;
167                 case IMAGE_TYPE_3D :
168                         src << "3D 0 0 0 2 R32ui";
169                 break;
170                 case IMAGE_TYPE_CUBE :
171                         src << "Cube 0 0 0 2 R32ui";
172                 break;
173                 case IMAGE_TYPE_CUBE_ARRAY :
174                         src << "Cube 0 1 0 2 R32ui";
175                 break;
176                 default :
177                         DE_ASSERT(0);
178                 break;
179         };
180
181         return src.str();
182 }
183
184 tcu::TestStatus SparseShaderIntrinsicsInstanceBase::iterate (void)
185 {
186         const InstanceInterface&                        instance                                = m_context.getInstanceInterface();
187         const DeviceInterface&                          deviceInterface                 = m_context.getDeviceInterface();
188         const VkPhysicalDevice                          physicalDevice                  = m_context.getPhysicalDevice();
189         VkImageCreateInfo                                       imageSparseInfo;
190         VkImageCreateInfo                                       imageTexelsInfo;
191         VkImageCreateInfo                                       imageResidencyInfo;
192         VkSparseImageMemoryRequirements         aspectRequirements;
193         std::vector <deUint32>                          residencyReferenceData;
194         std::vector<DeviceMemoryUniquePtr>      deviceMemUniquePtrVec;
195
196         // Check if image size does not exceed device limits
197         if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
198                 TCU_THROW(NotSupportedError, "Image size not supported for device");
199
200         // Check if device supports sparse operations for image type
201         if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
202                 TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
203
204         if (!getPhysicalDeviceFeatures(instance, physicalDevice).shaderResourceResidency)
205                 TCU_THROW(NotSupportedError, "Sparse resource residency information not supported in shader code.");
206
207         imageSparseInfo.sType                                   = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
208         imageSparseInfo.pNext                                   = DE_NULL;
209         imageSparseInfo.flags                                   = VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
210         imageSparseInfo.imageType                               = mapImageType(m_imageType);
211         imageSparseInfo.format                                  = mapTextureFormat(m_format);
212         imageSparseInfo.extent                                  = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
213         imageSparseInfo.arrayLayers                             = getNumLayers(m_imageType, m_imageSize);
214         imageSparseInfo.samples                                 = VK_SAMPLE_COUNT_1_BIT;
215         imageSparseInfo.tiling                                  = VK_IMAGE_TILING_OPTIMAL;
216         imageSparseInfo.initialLayout                   = VK_IMAGE_LAYOUT_UNDEFINED;
217         imageSparseInfo.usage                                   = VK_IMAGE_USAGE_TRANSFER_DST_BIT | imageSparseUsageFlags();
218         imageSparseInfo.sharingMode                             = VK_SHARING_MODE_EXCLUSIVE;
219         imageSparseInfo.queueFamilyIndexCount   = 0u;
220         imageSparseInfo.pQueueFamilyIndices             = DE_NULL;
221
222         if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
223         {
224                 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
225         }
226
227         {
228                 // Assign maximum allowed mipmap levels to image
229                 VkImageFormatProperties imageFormatProperties;
230                 instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
231                         imageSparseInfo.format,
232                         imageSparseInfo.imageType,
233                         imageSparseInfo.tiling,
234                         imageSparseInfo.usage,
235                         imageSparseInfo.flags,
236                         &imageFormatProperties);
237
238                 imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
239         }
240
241         // Check if device supports sparse operations for image format
242         if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
243                 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
244
245         {
246                 // Create logical device supporting both sparse and compute/graphics queues
247                 QueueRequirementsVec queueRequirements;
248                 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
249                 queueRequirements.push_back(QueueRequirements(getQueueFlags(), 1u));
250
251                 createDeviceSupportingQueues(queueRequirements);
252         }
253
254         // Create queues supporting sparse binding operations and compute/graphics operations
255         const Queue& sparseQueue        = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
256         const Queue& extractQueue       = getQueue(getQueueFlags(), 0);
257
258         // Create memory allocator for logical device
259         const de::UniquePtr<Allocator> allocator(new SimpleAllocator(deviceInterface, *m_logicalDevice, getPhysicalDeviceMemoryProperties(instance, physicalDevice)));
260
261         // Create sparse image
262         const Unique<VkImage> imageSparse(createImage(deviceInterface, *m_logicalDevice, &imageSparseInfo));
263
264         // Create sparse image memory bind semaphore
265         const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, *m_logicalDevice));
266
267         const deUint32                    imageSparseSizeInBytes                = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
268         const deUint32                    imageSizeInPixels                             = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels) / tcu::getPixelSize(m_format);
269
270         residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
271
272         {
273                 // Get sparse image general memory requirements
274                 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
275
276                 // Check if required image memory size does not exceed device limits
277                 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
278                         TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
279
280                 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
281
282                 // Get sparse image sparse memory requirements
283                 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
284
285                 DE_ASSERT(sparseMemoryRequirements.size() != 0);
286
287                 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
288
289                 if (colorAspectIndex == NO_MATCH_FOUND)
290                         TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
291
292                 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
293
294                 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
295
296                 const VkImageAspectFlags aspectMask                     = aspectRequirements.formatProperties.aspectMask;
297                 const VkExtent3D                 imageGranularity       = aspectRequirements.formatProperties.imageGranularity;
298                 const deUint32                   memoryType                     = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
299
300                 if (memoryType == NO_MATCH_FOUND)
301                         return tcu::TestStatus::fail("No matching memory type found");
302
303                 deUint32 pixelOffset = 0u;
304
305                 std::vector<VkSparseImageMemoryBind>  imageResidencyMemoryBinds;
306                 std::vector<VkSparseMemoryBind>           imageMipTailBinds;
307
308                 // Bind memory for each mipmap level
309                 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
310                 {
311                         const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
312
313                         if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
314                         {
315                                 pixelOffset += mipLevelSizeInPixels;
316                                 continue;
317                         }
318
319                         for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
320                         {
321                                 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
322                         }
323
324                         pixelOffset += mipLevelSizeInPixels;
325
326                         for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
327                         {
328                                 const VkExtent3D                 mipExtent                      = mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
329                                 const tcu::UVec3                 sparseBlocks           = alignedDivide(mipExtent, imageGranularity);
330                                 const deUint32                   numSparseBlocks        = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
331                                 const VkImageSubresource subresource            = { aspectMask, mipLevelNdx, layerNdx };
332
333                                 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, *m_logicalDevice,
334                                         imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
335
336                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
337
338                                 imageResidencyMemoryBinds.push_back(imageMemoryBind);
339                         }
340                 }
341
342                 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
343                 {
344                         if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
345                         {
346                                 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
347                                         aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
348
349                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
350
351                                 imageMipTailBinds.push_back(imageMipTailMemoryBind);
352                         }
353                         else
354                         {
355                                 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
356                                 {
357                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
358                                                 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
359
360                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
361
362                                         imageMipTailBinds.push_back(imageMipTailMemoryBind);
363                                 }
364                         }
365
366                         for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
367                         {
368                                 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
369                         }
370                 }
371
372                 VkBindSparseInfo bindSparseInfo =
373                 {
374                         VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,     //VkStructureType                                                       sType;
375                         DE_NULL,                                                        //const void*                                                           pNext;
376                         0u,                                                                     //deUint32                                                                      waitSemaphoreCount;
377                         DE_NULL,                                                        //const VkSemaphore*                                            pWaitSemaphores;
378                         0u,                                                                     //deUint32                                                                      bufferBindCount;
379                         DE_NULL,                                                        //const VkSparseBufferMemoryBindInfo*           pBufferBinds;
380                         0u,                                                                     //deUint32                                                                      imageOpaqueBindCount;
381                         DE_NULL,                                                        //const VkSparseImageOpaqueMemoryBindInfo*      pImageOpaqueBinds;
382                         0u,                                                                     //deUint32                                                                      imageBindCount;
383                         DE_NULL,                                                        //const VkSparseImageMemoryBindInfo*            pImageBinds;
384                         1u,                                                                     //deUint32                                                                      signalSemaphoreCount;
385                         &memoryBindSemaphore.get()                      //const VkSemaphore*                                            pSignalSemaphores;
386                 };
387
388                 VkSparseImageMemoryBindInfo               imageResidencyBindInfo;
389                 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
390
391                 if (imageResidencyMemoryBinds.size() > 0)
392                 {
393                         imageResidencyBindInfo.image            = *imageSparse;
394                         imageResidencyBindInfo.bindCount        = static_cast<deUint32>(imageResidencyMemoryBinds.size());
395                         imageResidencyBindInfo.pBinds           = &imageResidencyMemoryBinds[0];
396
397                         bindSparseInfo.imageBindCount           = 1u;
398                         bindSparseInfo.pImageBinds                      = &imageResidencyBindInfo;
399                 }
400
401                 if (imageMipTailBinds.size() > 0)
402                 {
403                         imageMipTailBindInfo.image                      = *imageSparse;
404                         imageMipTailBindInfo.bindCount          = static_cast<deUint32>(imageMipTailBinds.size());
405                         imageMipTailBindInfo.pBinds                     = &imageMipTailBinds[0];
406
407                         bindSparseInfo.imageOpaqueBindCount = 1u;
408                         bindSparseInfo.pImageOpaqueBinds        = &imageMipTailBindInfo;
409                 }
410
411                 // Submit sparse bind commands for execution
412                 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
413         }
414
415         // Create image to store texels copied from sparse image
416         imageTexelsInfo.sType                                   = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
417         imageTexelsInfo.pNext                                   = DE_NULL;
418         imageTexelsInfo.flags                                   = 0u;
419         imageTexelsInfo.imageType                               = imageSparseInfo.imageType;
420         imageTexelsInfo.format                                  = imageSparseInfo.format;
421         imageTexelsInfo.extent                                  = imageSparseInfo.extent;
422         imageTexelsInfo.arrayLayers                             = imageSparseInfo.arrayLayers;
423         imageTexelsInfo.mipLevels                               = imageSparseInfo.mipLevels;
424         imageTexelsInfo.samples                                 = imageSparseInfo.samples;
425         imageTexelsInfo.tiling                                  = VK_IMAGE_TILING_OPTIMAL;
426         imageTexelsInfo.initialLayout                   = VK_IMAGE_LAYOUT_UNDEFINED;
427         imageTexelsInfo.usage                                   = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | imageOutputUsageFlags();
428         imageTexelsInfo.sharingMode                             = VK_SHARING_MODE_EXCLUSIVE;
429         imageTexelsInfo.queueFamilyIndexCount   = 0u;
430         imageTexelsInfo.pQueueFamilyIndices             = DE_NULL;
431
432         if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
433         {
434                 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
435         }
436
437         const de::UniquePtr<Image> imageTexels(new Image(deviceInterface, *m_logicalDevice, *allocator, imageTexelsInfo, MemoryRequirement::Any));
438
439         // Create image to store residency info copied from sparse image
440         imageResidencyInfo                      = imageTexelsInfo;
441         imageResidencyInfo.format       = mapTextureFormat(m_residencyFormat);
442
443         const de::UniquePtr<Image> imageResidency(new Image(deviceInterface, *m_logicalDevice, *allocator, imageResidencyInfo, MemoryRequirement::Any));
444
445         // Create command buffer for compute and transfer oparations
446         const Unique<VkCommandPool>       commandPool(makeCommandPool(deviceInterface, *m_logicalDevice, extractQueue.queueFamilyIndex));
447         const Unique<VkCommandBuffer> commandBuffer(makeCommandBuffer(deviceInterface, *m_logicalDevice, *commandPool));
448
449         std::vector <VkBufferImageCopy> bufferImageSparseCopy(imageSparseInfo.mipLevels);
450
451         {
452                 deUint32 bufferOffset = 0u;
453                 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
454                 {
455                         bufferImageSparseCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
456                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
457                 }
458         }
459
460         // Start recording commands
461         beginCommandBuffer(deviceInterface, *commandBuffer);
462
463         // Create input buffer
464         const VkBufferCreateInfo        inputBufferCreateInfo = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
465         const de::UniquePtr<Buffer>     inputBuffer(new Buffer(deviceInterface, *m_logicalDevice, *allocator, inputBufferCreateInfo, MemoryRequirement::HostVisible));
466
467         // Fill input buffer with reference data
468         std::vector<deUint8> referenceData(imageSparseSizeInBytes);
469
470         for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
471         {
472                 const deUint32 mipLevelSizeinBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx);
473                 const deUint32 bufferOffset                     = static_cast<deUint32>(bufferImageSparseCopy[mipLevelNdx].bufferOffset);
474
475                 for (deUint32 byteNdx = 0u; byteNdx < mipLevelSizeinBytes; ++byteNdx)
476                 {
477                         referenceData[bufferOffset + byteNdx] = (deUint8)(mipLevelNdx + byteNdx);
478                 }
479         }
480
481         deMemcpy(inputBuffer->getAllocation().getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
482         flushMappedMemoryRange(deviceInterface, *m_logicalDevice, inputBuffer->getAllocation().getMemory(), inputBuffer->getAllocation().getOffset(), imageSparseSizeInBytes);
483
484         {
485                 // Prepare input buffer for data transfer operation
486                 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
487                 (
488                         VK_ACCESS_HOST_WRITE_BIT,
489                         VK_ACCESS_TRANSFER_READ_BIT,
490                         inputBuffer->get(),
491                         0u,
492                         imageSparseSizeInBytes
493                 );
494
495                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
496         }
497
498         const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers);
499
500         {
501                 // Prepare sparse image for data transfer operation
502                 const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
503                 (
504                         0u,
505                         VK_ACCESS_TRANSFER_WRITE_BIT,
506                         VK_IMAGE_LAYOUT_UNDEFINED,
507                         VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
508                         sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex  : VK_QUEUE_FAMILY_IGNORED,
509                         sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? extractQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
510                         *imageSparse,
511                         fullImageSubresourceRange
512                 );
513
514                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageSparseTransferDstBarrier);
515         }
516
517         // Copy reference data from input buffer to sparse image
518         deviceInterface.cmdCopyBufferToImage(*commandBuffer, inputBuffer->get(), *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
519
520         recordCommands(*allocator, *commandBuffer, imageSparseInfo, *imageSparse, imageTexels->get(), imageResidency->get());
521
522         const VkBufferCreateInfo        bufferTexelsInfo = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
523         const de::UniquePtr<Buffer>     bufferTexels(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferTexelsInfo, MemoryRequirement::HostVisible));
524
525         // Copy data from texels image to buffer
526         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageTexels->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferTexels->get(), static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
527
528         const deUint32                          imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
529
530         const VkBufferCreateInfo        bufferResidencyInfo = makeBufferCreateInfo(imageResidencySizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
531         const de::UniquePtr<Buffer>     bufferResidency(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferResidencyInfo, MemoryRequirement::HostVisible));
532
533         // Copy data from residency image to buffer
534         std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
535
536         {
537                 deUint32 bufferOffset = 0u;
538                 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
539                 {
540                         bufferImageResidencyCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
541                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipLevelNdx, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
542                 }
543         }
544
545         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageResidency->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferResidency->get(), static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
546
547         {
548                 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
549
550                 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
551                 (
552                         VK_ACCESS_TRANSFER_WRITE_BIT,
553                         VK_ACCESS_HOST_READ_BIT,
554                         bufferTexels->get(),
555                         0u,
556                         imageSparseSizeInBytes
557                 );
558
559                 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
560                 (
561                         VK_ACCESS_TRANSFER_WRITE_BIT,
562                         VK_ACCESS_HOST_READ_BIT,
563                         bufferResidency->get(),
564                         0u,
565                         imageResidencySizeInBytes
566                 );
567
568                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
569         }
570
571         // End recording commands
572         endCommandBuffer(deviceInterface, *commandBuffer);
573
574         const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
575
576         // Submit commands for execution and wait for completion
577         submitCommandsAndWait(deviceInterface, *m_logicalDevice, extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
578
579         // Wait for sparse queue to become idle
580         deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
581
582         // Retrieve data from residency buffer to host memory
583         const Allocation& bufferResidencyAllocation = bufferResidency->getAllocation();
584         invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferResidencyAllocation.getMemory(), bufferResidencyAllocation.getOffset(), imageResidencySizeInBytes);
585
586         const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAllocation.getHostPtr());
587
588         deUint32 pixelOffsetNotAligned = 0u;
589         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
590         {
591                 const deUint32 mipLevelSizeInBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipmapNdx);
592                 const deUint32 pixelOffsetAligned       = static_cast<deUint32>(bufferImageResidencyCopy[mipmapNdx].bufferOffset) / tcu::getPixelSize(m_residencyFormat);
593
594                 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
595                         return tcu::TestStatus::fail("Failed");
596
597                 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
598         }
599
600         // Retrieve data from texels buffer to host memory
601         const Allocation& bufferTexelsAllocation = bufferTexels->getAllocation();
602         invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferTexelsAllocation.getMemory(), bufferTexelsAllocation.getOffset(), imageSparseSizeInBytes);
603
604         const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAllocation.getHostPtr());
605
606         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
607         {
608                 const deUint32 mipLevelSizeInBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
609                 const deUint32 bufferOffset                     = static_cast<deUint32>(bufferImageSparseCopy[mipmapNdx].bufferOffset);
610
611                 if (mipmapNdx < aspectRequirements.imageMipTailFirstLod)
612                 {
613                         if (mipmapNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_BOUND)
614                         {
615                                 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
616                                         return tcu::TestStatus::fail("Failed");
617                         }
618                         else if (getPhysicalDeviceProperties(instance, physicalDevice).sparseProperties.residencyNonResidentStrict)
619                         {
620                                 std::vector<deUint8> zeroData;
621                                 zeroData.assign(mipLevelSizeInBytes, 0u);
622
623                                 if (deMemCmp(&bufferTexelsData[bufferOffset], &zeroData[0], mipLevelSizeInBytes) != 0)
624                                         return tcu::TestStatus::fail("Failed");
625                         }
626                 }
627                 else
628                 {
629                         if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
630                                 return tcu::TestStatus::fail("Failed");
631                 }
632         }
633
634         return tcu::TestStatus::pass("Passed");
635 }
636
637 } // sparse
638 } // vkt