Sparse resources: create correct device interface
[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 VkPhysicalDevice                          physicalDevice                  = m_context.getPhysicalDevice();
188         VkImageCreateInfo                                       imageSparseInfo;
189         VkImageCreateInfo                                       imageTexelsInfo;
190         VkImageCreateInfo                                       imageResidencyInfo;
191         VkSparseImageMemoryRequirements         aspectRequirements;
192         std::vector <deUint32>                          residencyReferenceData;
193         std::vector<DeviceMemorySp>                     deviceMemUniquePtrVec;
194
195         // Check if image size does not exceed device limits
196         if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
197                 TCU_THROW(NotSupportedError, "Image size not supported for device");
198
199         // Check if device supports sparse operations for image type
200         if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
201                 TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
202
203         if (!getPhysicalDeviceFeatures(instance, physicalDevice).shaderResourceResidency)
204                 TCU_THROW(NotSupportedError, "Sparse resource residency information not supported in shader code.");
205
206         imageSparseInfo.sType                                   = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
207         imageSparseInfo.pNext                                   = DE_NULL;
208         imageSparseInfo.flags                                   = VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
209         imageSparseInfo.imageType                               = mapImageType(m_imageType);
210         imageSparseInfo.format                                  = mapTextureFormat(m_format);
211         imageSparseInfo.extent                                  = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
212         imageSparseInfo.arrayLayers                             = getNumLayers(m_imageType, m_imageSize);
213         imageSparseInfo.samples                                 = VK_SAMPLE_COUNT_1_BIT;
214         imageSparseInfo.tiling                                  = VK_IMAGE_TILING_OPTIMAL;
215         imageSparseInfo.initialLayout                   = VK_IMAGE_LAYOUT_UNDEFINED;
216         imageSparseInfo.usage                                   = VK_IMAGE_USAGE_TRANSFER_DST_BIT | imageSparseUsageFlags();
217         imageSparseInfo.sharingMode                             = VK_SHARING_MODE_EXCLUSIVE;
218         imageSparseInfo.queueFamilyIndexCount   = 0u;
219         imageSparseInfo.pQueueFamilyIndices             = DE_NULL;
220
221         if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
222         {
223                 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
224         }
225
226         {
227                 // Assign maximum allowed mipmap levels to image
228                 VkImageFormatProperties imageFormatProperties;
229                 instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
230                         imageSparseInfo.format,
231                         imageSparseInfo.imageType,
232                         imageSparseInfo.tiling,
233                         imageSparseInfo.usage,
234                         imageSparseInfo.flags,
235                         &imageFormatProperties);
236
237                 imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
238         }
239
240         // Check if device supports sparse operations for image format
241         if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
242                 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
243
244         {
245                 // Create logical device supporting both sparse and compute/graphics queues
246                 QueueRequirementsVec queueRequirements;
247                 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
248                 queueRequirements.push_back(QueueRequirements(getQueueFlags(), 1u));
249
250                 createDeviceSupportingQueues(queueRequirements);
251         }
252
253         const DeviceInterface&  deviceInterface = getDeviceInterface();
254
255         // Create queues supporting sparse binding operations and compute/graphics operations
256         const Queue&                    sparseQueue             = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
257         const Queue&                    extractQueue    = getQueue(getQueueFlags(), 0);
258
259         // Create sparse image
260         const Unique<VkImage> imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
261
262         // Create sparse image memory bind semaphore
263         const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, getDevice()));
264
265         const deUint32                    imageSparseSizeInBytes                = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
266         const deUint32                    imageSizeInPixels                             = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels) / tcu::getPixelSize(m_format);
267
268         residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
269
270         {
271                 // Get sparse image general memory requirements
272                 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
273
274                 // Check if required image memory size does not exceed device limits
275                 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
276                         TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
277
278                 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
279
280                 // Get sparse image sparse memory requirements
281                 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
282
283                 DE_ASSERT(sparseMemoryRequirements.size() != 0);
284
285                 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
286
287                 if (colorAspectIndex == NO_MATCH_FOUND)
288                         TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
289
290                 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
291
292                 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
293
294                 const VkImageAspectFlags aspectMask                     = aspectRequirements.formatProperties.aspectMask;
295                 const VkExtent3D                 imageGranularity       = aspectRequirements.formatProperties.imageGranularity;
296                 const deUint32                   memoryType                     = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
297
298                 if (memoryType == NO_MATCH_FOUND)
299                         return tcu::TestStatus::fail("No matching memory type found");
300
301                 deUint32 pixelOffset = 0u;
302
303                 std::vector<VkSparseImageMemoryBind>  imageResidencyMemoryBinds;
304                 std::vector<VkSparseMemoryBind>           imageMipTailBinds;
305
306                 // Bind memory for each mipmap level
307                 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
308                 {
309                         const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
310
311                         if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
312                         {
313                                 pixelOffset += mipLevelSizeInPixels;
314                                 continue;
315                         }
316
317                         for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
318                         {
319                                 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
320                         }
321
322                         pixelOffset += mipLevelSizeInPixels;
323
324                         for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
325                         {
326                                 const VkExtent3D                 mipExtent                      = mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
327                                 const tcu::UVec3                 sparseBlocks           = alignedDivide(mipExtent, imageGranularity);
328                                 const deUint32                   numSparseBlocks        = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
329                                 const VkImageSubresource subresource            = { aspectMask, mipLevelNdx, layerNdx };
330
331                                 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
332                                         imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
333
334                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
335
336                                 imageResidencyMemoryBinds.push_back(imageMemoryBind);
337                         }
338                 }
339
340                 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
341                 {
342                         if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
343                         {
344                                 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
345                                         aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
346
347                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
348
349                                 imageMipTailBinds.push_back(imageMipTailMemoryBind);
350                         }
351                         else
352                         {
353                                 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
354                                 {
355                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
356                                                 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
357
358                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
359
360                                         imageMipTailBinds.push_back(imageMipTailMemoryBind);
361                                 }
362                         }
363
364                         for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
365                         {
366                                 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
367                         }
368                 }
369
370                 VkBindSparseInfo bindSparseInfo =
371                 {
372                         VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,     //VkStructureType                                                       sType;
373                         DE_NULL,                                                        //const void*                                                           pNext;
374                         0u,                                                                     //deUint32                                                                      waitSemaphoreCount;
375                         DE_NULL,                                                        //const VkSemaphore*                                            pWaitSemaphores;
376                         0u,                                                                     //deUint32                                                                      bufferBindCount;
377                         DE_NULL,                                                        //const VkSparseBufferMemoryBindInfo*           pBufferBinds;
378                         0u,                                                                     //deUint32                                                                      imageOpaqueBindCount;
379                         DE_NULL,                                                        //const VkSparseImageOpaqueMemoryBindInfo*      pImageOpaqueBinds;
380                         0u,                                                                     //deUint32                                                                      imageBindCount;
381                         DE_NULL,                                                        //const VkSparseImageMemoryBindInfo*            pImageBinds;
382                         1u,                                                                     //deUint32                                                                      signalSemaphoreCount;
383                         &memoryBindSemaphore.get()                      //const VkSemaphore*                                            pSignalSemaphores;
384                 };
385
386                 VkSparseImageMemoryBindInfo               imageResidencyBindInfo;
387                 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
388
389                 if (imageResidencyMemoryBinds.size() > 0)
390                 {
391                         imageResidencyBindInfo.image            = *imageSparse;
392                         imageResidencyBindInfo.bindCount        = static_cast<deUint32>(imageResidencyMemoryBinds.size());
393                         imageResidencyBindInfo.pBinds           = &imageResidencyMemoryBinds[0];
394
395                         bindSparseInfo.imageBindCount           = 1u;
396                         bindSparseInfo.pImageBinds                      = &imageResidencyBindInfo;
397                 }
398
399                 if (imageMipTailBinds.size() > 0)
400                 {
401                         imageMipTailBindInfo.image                      = *imageSparse;
402                         imageMipTailBindInfo.bindCount          = static_cast<deUint32>(imageMipTailBinds.size());
403                         imageMipTailBindInfo.pBinds                     = &imageMipTailBinds[0];
404
405                         bindSparseInfo.imageOpaqueBindCount = 1u;
406                         bindSparseInfo.pImageOpaqueBinds        = &imageMipTailBindInfo;
407                 }
408
409                 // Submit sparse bind commands for execution
410                 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
411         }
412
413         // Create image to store texels copied from sparse image
414         imageTexelsInfo.sType                                   = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
415         imageTexelsInfo.pNext                                   = DE_NULL;
416         imageTexelsInfo.flags                                   = 0u;
417         imageTexelsInfo.imageType                               = imageSparseInfo.imageType;
418         imageTexelsInfo.format                                  = imageSparseInfo.format;
419         imageTexelsInfo.extent                                  = imageSparseInfo.extent;
420         imageTexelsInfo.arrayLayers                             = imageSparseInfo.arrayLayers;
421         imageTexelsInfo.mipLevels                               = imageSparseInfo.mipLevels;
422         imageTexelsInfo.samples                                 = imageSparseInfo.samples;
423         imageTexelsInfo.tiling                                  = VK_IMAGE_TILING_OPTIMAL;
424         imageTexelsInfo.initialLayout                   = VK_IMAGE_LAYOUT_UNDEFINED;
425         imageTexelsInfo.usage                                   = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | imageOutputUsageFlags();
426         imageTexelsInfo.sharingMode                             = VK_SHARING_MODE_EXCLUSIVE;
427         imageTexelsInfo.queueFamilyIndexCount   = 0u;
428         imageTexelsInfo.pQueueFamilyIndices             = DE_NULL;
429
430         if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
431         {
432                 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
433         }
434
435         const Unique<VkImage>                   imageTexels                     (createImage(deviceInterface, getDevice(), &imageTexelsInfo));
436         const de::UniquePtr<Allocation> imageTexelsAlloc        (bindImage(deviceInterface, getDevice(), getAllocator(), *imageTexels, MemoryRequirement::Any));
437
438         // Create image to store residency info copied from sparse image
439         imageResidencyInfo                      = imageTexelsInfo;
440         imageResidencyInfo.format       = mapTextureFormat(m_residencyFormat);
441
442         const Unique<VkImage>                   imageResidency          (createImage(deviceInterface, getDevice(), &imageResidencyInfo));
443         const de::UniquePtr<Allocation> imageResidencyAlloc     (bindImage(deviceInterface, getDevice(), getAllocator(), *imageResidency, MemoryRequirement::Any));
444
445         // Create command buffer for compute and transfer oparations
446         const Unique<VkCommandPool>       commandPool(makeCommandPool(deviceInterface, getDevice(), extractQueue.queueFamilyIndex));
447         const Unique<VkCommandBuffer> commandBuffer(makeCommandBuffer(deviceInterface, getDevice(), *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, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
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 Unique<VkBuffer>                  inputBuffer                             (createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
466         const de::UniquePtr<Allocation> inputBufferAlloc                (bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
467
468         // Fill input buffer with reference data
469         std::vector<deUint8> referenceData(imageSparseSizeInBytes);
470
471         for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
472         {
473                 const deUint32 mipLevelSizeinBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx);
474                 const deUint32 bufferOffset                     = static_cast<deUint32>(bufferImageSparseCopy[mipLevelNdx].bufferOffset);
475
476                 for (deUint32 byteNdx = 0u; byteNdx < mipLevelSizeinBytes; ++byteNdx)
477                 {
478                         referenceData[bufferOffset + byteNdx] = (deUint8)(mipLevelNdx + byteNdx);
479                 }
480         }
481
482         deMemcpy(inputBufferAlloc->getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
483         flushMappedMemoryRange(deviceInterface, getDevice(), inputBufferAlloc->getMemory(), inputBufferAlloc->getOffset(), imageSparseSizeInBytes);
484
485         {
486                 // Prepare input buffer for data transfer operation
487                 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
488                 (
489                         VK_ACCESS_HOST_WRITE_BIT,
490                         VK_ACCESS_TRANSFER_READ_BIT,
491                         *inputBuffer,
492                         0u,
493                         imageSparseSizeInBytes
494                 );
495
496                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
497         }
498
499         const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers);
500
501         {
502                 // Prepare sparse image for data transfer operation
503                 const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
504                 (
505                         0u,
506                         VK_ACCESS_TRANSFER_WRITE_BIT,
507                         VK_IMAGE_LAYOUT_UNDEFINED,
508                         VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
509                         sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex  : VK_QUEUE_FAMILY_IGNORED,
510                         sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? extractQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
511                         *imageSparse,
512                         fullImageSubresourceRange
513                 );
514
515                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageSparseTransferDstBarrier);
516         }
517
518         // Copy reference data from input buffer to sparse image
519         deviceInterface.cmdCopyBufferToImage(*commandBuffer, *inputBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
520
521         recordCommands(*commandBuffer, imageSparseInfo, *imageSparse, *imageTexels, *imageResidency);
522
523         const VkBufferCreateInfo                bufferTexelsCreateInfo  = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
524         const Unique<VkBuffer>                  bufferTexels                    (createBuffer(deviceInterface, getDevice(), &bufferTexelsCreateInfo));
525         const de::UniquePtr<Allocation> bufferTexelsAlloc               (bindBuffer(deviceInterface, getDevice(), getAllocator(), *bufferTexels, MemoryRequirement::HostVisible));
526
527         // Copy data from texels image to buffer
528         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageTexels, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *bufferTexels, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
529
530         const deUint32                          imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
531
532         const VkBufferCreateInfo                bufferResidencyCreateInfo       = makeBufferCreateInfo(imageResidencySizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
533         const Unique<VkBuffer>                  bufferResidency                         (createBuffer(deviceInterface, getDevice(), &bufferResidencyCreateInfo));
534         const de::UniquePtr<Allocation> bufferResidencyAlloc            (bindBuffer(deviceInterface, getDevice(), getAllocator(), *bufferResidency, MemoryRequirement::HostVisible));
535
536         // Copy data from residency image to buffer
537         std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
538
539         {
540                 deUint32 bufferOffset = 0u;
541                 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
542                 {
543                         bufferImageResidencyCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
544                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipLevelNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
545                 }
546         }
547
548         deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageResidency, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *bufferResidency, static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
549
550         {
551                 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
552
553                 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
554                 (
555                         VK_ACCESS_TRANSFER_WRITE_BIT,
556                         VK_ACCESS_HOST_READ_BIT,
557                         *bufferTexels,
558                         0u,
559                         imageSparseSizeInBytes
560                 );
561
562                 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
563                 (
564                         VK_ACCESS_TRANSFER_WRITE_BIT,
565                         VK_ACCESS_HOST_READ_BIT,
566                         *bufferResidency,
567                         0u,
568                         imageResidencySizeInBytes
569                 );
570
571                 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
572         }
573
574         // End recording commands
575         endCommandBuffer(deviceInterface, *commandBuffer);
576
577         const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
578
579         // Submit commands for execution and wait for completion
580         submitCommandsAndWait(deviceInterface, getDevice(), extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
581
582         // Wait for sparse queue to become idle
583         deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
584
585         // Retrieve data from residency buffer to host memory
586         invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferResidencyAlloc->getMemory(), bufferResidencyAlloc->getOffset(), imageResidencySizeInBytes);
587
588         const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAlloc->getHostPtr());
589
590         deUint32 pixelOffsetNotAligned = 0u;
591         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
592         {
593                 const deUint32 mipLevelSizeInBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipmapNdx);
594                 const deUint32 pixelOffsetAligned       = static_cast<deUint32>(bufferImageResidencyCopy[mipmapNdx].bufferOffset) / tcu::getPixelSize(m_residencyFormat);
595
596                 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
597                         return tcu::TestStatus::fail("Failed");
598
599                 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
600         }
601
602         // Retrieve data from texels buffer to host memory
603         invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferTexelsAlloc->getMemory(), bufferTexelsAlloc->getOffset(), imageSparseSizeInBytes);
604
605         const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAlloc->getHostPtr());
606
607         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
608         {
609                 const deUint32 mipLevelSizeInBytes      = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
610                 const deUint32 bufferOffset                     = static_cast<deUint32>(bufferImageSparseCopy[mipmapNdx].bufferOffset);
611
612                 if (mipmapNdx < aspectRequirements.imageMipTailFirstLod)
613                 {
614                         if (mipmapNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_BOUND)
615                         {
616                                 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
617                                         return tcu::TestStatus::fail("Failed");
618                         }
619                         else if (getPhysicalDeviceProperties(instance, physicalDevice).sparseProperties.residencyNonResidentStrict)
620                         {
621                                 std::vector<deUint8> zeroData;
622                                 zeroData.assign(mipLevelSizeInBytes, 0u);
623
624                                 if (deMemCmp(&bufferTexelsData[bufferOffset], &zeroData[0], mipLevelSizeInBytes) != 0)
625                                         return tcu::TestStatus::fail("Failed");
626                         }
627                 }
628                 else
629                 {
630                         if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
631                                 return tcu::TestStatus::fail("Failed");
632                 }
633         }
634
635         return tcu::TestStatus::pass("Passed");
636 }
637
638 } // sparse
639 } // vkt