Refactor sparse resources module
[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<DeviceMemorySp>                     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 sparse image
259         const Unique<VkImage> imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
260
261         // Create sparse image memory bind semaphore
262         const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, getDevice()));
263
264         const deUint32                    imageSparseSizeInBytes                = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
265         const deUint32                    imageSizeInPixels                             = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels) / tcu::getPixelSize(m_format);
266
267         residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
268
269         {
270                 // Get sparse image general memory requirements
271                 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
272
273                 // Check if required image memory size does not exceed device limits
274                 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
275                         TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
276
277                 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
278
279                 // Get sparse image sparse memory requirements
280                 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
281
282                 DE_ASSERT(sparseMemoryRequirements.size() != 0);
283
284                 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
285
286                 if (colorAspectIndex == NO_MATCH_FOUND)
287                         TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
288
289                 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
290
291                 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
292
293                 const VkImageAspectFlags aspectMask                     = aspectRequirements.formatProperties.aspectMask;
294                 const VkExtent3D                 imageGranularity       = aspectRequirements.formatProperties.imageGranularity;
295                 const deUint32                   memoryType                     = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
296
297                 if (memoryType == NO_MATCH_FOUND)
298                         return tcu::TestStatus::fail("No matching memory type found");
299
300                 deUint32 pixelOffset = 0u;
301
302                 std::vector<VkSparseImageMemoryBind>  imageResidencyMemoryBinds;
303                 std::vector<VkSparseMemoryBind>           imageMipTailBinds;
304
305                 // Bind memory for each mipmap level
306                 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
307                 {
308                         const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
309
310                         if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
311                         {
312                                 pixelOffset += mipLevelSizeInPixels;
313                                 continue;
314                         }
315
316                         for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
317                         {
318                                 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
319                         }
320
321                         pixelOffset += mipLevelSizeInPixels;
322
323                         for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
324                         {
325                                 const VkExtent3D                 mipExtent                      = mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
326                                 const tcu::UVec3                 sparseBlocks           = alignedDivide(mipExtent, imageGranularity);
327                                 const deUint32                   numSparseBlocks        = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
328                                 const VkImageSubresource subresource            = { aspectMask, mipLevelNdx, layerNdx };
329
330                                 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
331                                         imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
332
333                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
334
335                                 imageResidencyMemoryBinds.push_back(imageMemoryBind);
336                         }
337                 }
338
339                 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
340                 {
341                         if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
342                         {
343                                 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
344                                         aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
345
346                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
347
348                                 imageMipTailBinds.push_back(imageMipTailMemoryBind);
349                         }
350                         else
351                         {
352                                 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
353                                 {
354                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
355                                                 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
356
357                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
358
359                                         imageMipTailBinds.push_back(imageMipTailMemoryBind);
360                                 }
361                         }
362
363                         for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
364                         {
365                                 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
366                         }
367                 }
368
369                 VkBindSparseInfo bindSparseInfo =
370                 {
371                         VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,     //VkStructureType                                                       sType;
372                         DE_NULL,                                                        //const void*                                                           pNext;
373                         0u,                                                                     //deUint32                                                                      waitSemaphoreCount;
374                         DE_NULL,                                                        //const VkSemaphore*                                            pWaitSemaphores;
375                         0u,                                                                     //deUint32                                                                      bufferBindCount;
376                         DE_NULL,                                                        //const VkSparseBufferMemoryBindInfo*           pBufferBinds;
377                         0u,                                                                     //deUint32                                                                      imageOpaqueBindCount;
378                         DE_NULL,                                                        //const VkSparseImageOpaqueMemoryBindInfo*      pImageOpaqueBinds;
379                         0u,                                                                     //deUint32                                                                      imageBindCount;
380                         DE_NULL,                                                        //const VkSparseImageMemoryBindInfo*            pImageBinds;
381                         1u,                                                                     //deUint32                                                                      signalSemaphoreCount;
382                         &memoryBindSemaphore.get()                      //const VkSemaphore*                                            pSignalSemaphores;
383                 };
384
385                 VkSparseImageMemoryBindInfo               imageResidencyBindInfo;
386                 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
387
388                 if (imageResidencyMemoryBinds.size() > 0)
389                 {
390                         imageResidencyBindInfo.image            = *imageSparse;
391                         imageResidencyBindInfo.bindCount        = static_cast<deUint32>(imageResidencyMemoryBinds.size());
392                         imageResidencyBindInfo.pBinds           = &imageResidencyMemoryBinds[0];
393
394                         bindSparseInfo.imageBindCount           = 1u;
395                         bindSparseInfo.pImageBinds                      = &imageResidencyBindInfo;
396                 }
397
398                 if (imageMipTailBinds.size() > 0)
399                 {
400                         imageMipTailBindInfo.image                      = *imageSparse;
401                         imageMipTailBindInfo.bindCount          = static_cast<deUint32>(imageMipTailBinds.size());
402                         imageMipTailBindInfo.pBinds                     = &imageMipTailBinds[0];
403
404                         bindSparseInfo.imageOpaqueBindCount = 1u;
405                         bindSparseInfo.pImageOpaqueBinds        = &imageMipTailBindInfo;
406                 }
407
408                 // Submit sparse bind commands for execution
409                 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
410         }
411
412         // Create image to store texels copied from sparse image
413         imageTexelsInfo.sType                                   = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
414         imageTexelsInfo.pNext                                   = DE_NULL;
415         imageTexelsInfo.flags                                   = 0u;
416         imageTexelsInfo.imageType                               = imageSparseInfo.imageType;
417         imageTexelsInfo.format                                  = imageSparseInfo.format;
418         imageTexelsInfo.extent                                  = imageSparseInfo.extent;
419         imageTexelsInfo.arrayLayers                             = imageSparseInfo.arrayLayers;
420         imageTexelsInfo.mipLevels                               = imageSparseInfo.mipLevels;
421         imageTexelsInfo.samples                                 = imageSparseInfo.samples;
422         imageTexelsInfo.tiling                                  = VK_IMAGE_TILING_OPTIMAL;
423         imageTexelsInfo.initialLayout                   = VK_IMAGE_LAYOUT_UNDEFINED;
424         imageTexelsInfo.usage                                   = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | imageOutputUsageFlags();
425         imageTexelsInfo.sharingMode                             = VK_SHARING_MODE_EXCLUSIVE;
426         imageTexelsInfo.queueFamilyIndexCount   = 0u;
427         imageTexelsInfo.pQueueFamilyIndices             = DE_NULL;
428
429         if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
430         {
431                 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
432         }
433
434         const Unique<VkImage>                   imageTexels                     (createImage(deviceInterface, getDevice(), &imageTexelsInfo));
435         const de::UniquePtr<Allocation> imageTexelsAlloc        (bindImage(deviceInterface, getDevice(), getAllocator(), *imageTexels, MemoryRequirement::Any));
436
437         // Create image to store residency info copied from sparse image
438         imageResidencyInfo                      = imageTexelsInfo;
439         imageResidencyInfo.format       = mapTextureFormat(m_residencyFormat);
440
441         const Unique<VkImage>                   imageResidency          (createImage(deviceInterface, getDevice(), &imageResidencyInfo));
442         const de::UniquePtr<Allocation> imageResidencyAlloc     (bindImage(deviceInterface, getDevice(), getAllocator(), *imageResidency, MemoryRequirement::Any));
443
444         // Create command buffer for compute and transfer oparations
445         const Unique<VkCommandPool>       commandPool(makeCommandPool(deviceInterface, getDevice(), extractQueue.queueFamilyIndex));
446         const Unique<VkCommandBuffer> commandBuffer(makeCommandBuffer(deviceInterface, getDevice(), *commandPool));
447
448         std::vector <VkBufferImageCopy> bufferImageSparseCopy(imageSparseInfo.mipLevels);
449
450         {
451                 deUint32 bufferOffset = 0u;
452                 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
453                 {
454                         bufferImageSparseCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
455                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
456                 }
457         }
458
459         // Start recording commands
460         beginCommandBuffer(deviceInterface, *commandBuffer);
461
462         // Create input buffer
463         const VkBufferCreateInfo                inputBufferCreateInfo   = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
464         const Unique<VkBuffer>                  inputBuffer                             (createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
465         const de::UniquePtr<Allocation> inputBufferAlloc                (bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, 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(inputBufferAlloc->getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
482         flushMappedMemoryRange(deviceInterface, getDevice(), inputBufferAlloc->getMemory(), inputBufferAlloc->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,
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, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
519
520         recordCommands(*commandBuffer, imageSparseInfo, *imageSparse, *imageTexels, *imageResidency);
521
522         const VkBufferCreateInfo                bufferTexelsCreateInfo  = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
523         const Unique<VkBuffer>                  bufferTexels                    (createBuffer(deviceInterface, getDevice(), &bufferTexelsCreateInfo));
524         const de::UniquePtr<Allocation> bufferTexelsAlloc               (bindBuffer(deviceInterface, getDevice(), getAllocator(), *bufferTexels, MemoryRequirement::HostVisible));
525
526         // Copy data from texels image to buffer
527         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageTexels, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *bufferTexels, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
528
529         const deUint32                          imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
530
531         const VkBufferCreateInfo                bufferResidencyCreateInfo       = makeBufferCreateInfo(imageResidencySizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
532         const Unique<VkBuffer>                  bufferResidency                         (createBuffer(deviceInterface, getDevice(), &bufferResidencyCreateInfo));
533         const de::UniquePtr<Allocation> bufferResidencyAlloc            (bindBuffer(deviceInterface, getDevice(), getAllocator(), *bufferResidency, MemoryRequirement::HostVisible));
534
535         // Copy data from residency image to buffer
536         std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
537
538         {
539                 deUint32 bufferOffset = 0u;
540                 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
541                 {
542                         bufferImageResidencyCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
543                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipLevelNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
544                 }
545         }
546
547         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageResidency, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *bufferResidency, static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
548
549         {
550                 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
551
552                 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
553                 (
554                         VK_ACCESS_TRANSFER_WRITE_BIT,
555                         VK_ACCESS_HOST_READ_BIT,
556                         *bufferTexels,
557                         0u,
558                         imageSparseSizeInBytes
559                 );
560
561                 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
562                 (
563                         VK_ACCESS_TRANSFER_WRITE_BIT,
564                         VK_ACCESS_HOST_READ_BIT,
565                         *bufferResidency,
566                         0u,
567                         imageResidencySizeInBytes
568                 );
569
570                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
571         }
572
573         // End recording commands
574         endCommandBuffer(deviceInterface, *commandBuffer);
575
576         const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
577
578         // Submit commands for execution and wait for completion
579         submitCommandsAndWait(deviceInterface, getDevice(), extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
580
581         // Wait for sparse queue to become idle
582         deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
583
584         // Retrieve data from residency buffer to host memory
585         invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferResidencyAlloc->getMemory(), bufferResidencyAlloc->getOffset(), imageResidencySizeInBytes);
586
587         const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAlloc->getHostPtr());
588
589         deUint32 pixelOffsetNotAligned = 0u;
590         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
591         {
592                 const deUint32 mipLevelSizeInBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipmapNdx);
593                 const deUint32 pixelOffsetAligned       = static_cast<deUint32>(bufferImageResidencyCopy[mipmapNdx].bufferOffset) / tcu::getPixelSize(m_residencyFormat);
594
595                 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
596                         return tcu::TestStatus::fail("Failed");
597
598                 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
599         }
600
601         // Retrieve data from texels buffer to host memory
602         invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferTexelsAlloc->getMemory(), bufferTexelsAlloc->getOffset(), imageSparseSizeInBytes);
603
604         const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAlloc->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