1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * \file vktSparseResourcesShaderIntrinsicsBase.cpp
21 * \brief Sparse Resources Shader Intrinsics Base Classes
22 *//*--------------------------------------------------------------------*/
24 #include "vktSparseResourcesShaderIntrinsicsBase.hpp"
33 tcu::UVec3 alignedDivide (const VkExtent3D& extent, const VkExtent3D& divisor)
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);
44 std::string getOpTypeImageComponent (const tcu::TextureFormat& format)
46 switch (tcu::getTextureChannelClass(format.type))
48 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
49 return "OpTypeInt 32 0";
50 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
51 return "OpTypeInt 32 1";
58 std::string getImageComponentTypeName (const tcu::TextureFormat& format)
60 switch (tcu::getTextureChannelClass(format.type))
62 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
64 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
72 std::string getImageComponentVec4TypeName (const tcu::TextureFormat& format)
74 switch (tcu::getTextureChannelClass(format.type))
76 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
78 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
86 std::string getOpTypeImageSparse (const ImageType imageType,
87 const tcu::TextureFormat& format,
88 const std::string& componentType,
89 const bool requiresSampler)
91 std::ostringstream src;
93 src << "OpTypeImage " << componentType << " ";
100 case IMAGE_TYPE_1D_ARRAY :
106 case IMAGE_TYPE_2D_ARRAY :
112 case IMAGE_TYPE_CUBE :
113 src << "Cube 0 0 0 ";
115 case IMAGE_TYPE_CUBE_ARRAY :
116 src << "Cube 0 1 0 ";
128 switch (format.order)
130 case tcu::TextureFormat::R:
133 case tcu::TextureFormat::RG:
136 case tcu::TextureFormat::RGB:
139 case tcu::TextureFormat::RGBA:
149 case tcu::TextureFormat::SIGNED_INT8:
152 case tcu::TextureFormat::SIGNED_INT16:
155 case tcu::TextureFormat::SIGNED_INT32:
158 case tcu::TextureFormat::UNSIGNED_INT8:
161 case tcu::TextureFormat::UNSIGNED_INT16:
164 case tcu::TextureFormat::UNSIGNED_INT32:
175 std::string getOpTypeImageResidency (const ImageType imageType)
177 std::ostringstream src;
179 src << "OpTypeImage %type_uint ";
184 src << "1D 0 0 0 2 R32ui";
186 case IMAGE_TYPE_1D_ARRAY :
187 src << "1D 0 1 0 2 R32ui";
190 src << "2D 0 0 0 2 R32ui";
192 case IMAGE_TYPE_2D_ARRAY :
193 src << "2D 0 1 0 2 R32ui";
196 src << "3D 0 0 0 2 R32ui";
198 case IMAGE_TYPE_CUBE :
199 src << "Cube 0 0 0 2 R32ui";
201 case IMAGE_TYPE_CUBE_ARRAY :
202 src << "Cube 0 1 0 2 R32ui";
212 tcu::TestStatus SparseShaderIntrinsicsInstanceBase::iterate (void)
214 const InstanceInterface& instance = m_context.getInstanceInterface();
215 const DeviceInterface& deviceInterface = m_context.getDeviceInterface();
216 const VkPhysicalDevice physicalDevice = m_context.getPhysicalDevice();
217 VkImageCreateInfo imageSparseInfo;
218 VkImageCreateInfo imageTexelsInfo;
219 VkImageCreateInfo imageResidencyInfo;
220 VkSparseImageMemoryRequirements aspectRequirements;
221 std::vector <deUint32> residencyReferenceData;
222 std::vector<DeviceMemoryUniquePtr> deviceMemUniquePtrVec;
224 // Check if image size does not exceed device limits
225 if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
226 TCU_THROW(NotSupportedError, "Image size not supported for device");
228 // Check if device supports sparse operations for image type
229 if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
230 TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
232 if (!getPhysicalDeviceFeatures(instance, physicalDevice).shaderResourceResidency)
233 TCU_THROW(NotSupportedError, "Sparse resource residency information not supported in shader code.");
235 imageSparseInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
236 imageSparseInfo.pNext = DE_NULL;
237 imageSparseInfo.flags = VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
238 imageSparseInfo.imageType = mapImageType(m_imageType);
239 imageSparseInfo.format = mapTextureFormat(m_format);
240 imageSparseInfo.extent = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
241 imageSparseInfo.arrayLayers = getNumLayers(m_imageType, m_imageSize);
242 imageSparseInfo.samples = VK_SAMPLE_COUNT_1_BIT;
243 imageSparseInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
244 imageSparseInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
245 imageSparseInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | imageSparseUsageFlags();
246 imageSparseInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
247 imageSparseInfo.queueFamilyIndexCount = 0u;
248 imageSparseInfo.pQueueFamilyIndices = DE_NULL;
250 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
252 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
256 // Assign maximum allowed mipmap levels to image
257 VkImageFormatProperties imageFormatProperties;
258 instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
259 imageSparseInfo.format,
260 imageSparseInfo.imageType,
261 imageSparseInfo.tiling,
262 imageSparseInfo.usage,
263 imageSparseInfo.flags,
264 &imageFormatProperties);
266 imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
269 // Check if device supports sparse operations for image format
270 if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
271 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
274 // Create logical device supporting both sparse and compute/graphics queues
275 QueueRequirementsVec queueRequirements;
276 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
277 queueRequirements.push_back(QueueRequirements(getQueueFlags(), 1u));
279 createDeviceSupportingQueues(queueRequirements);
282 // Create queues supporting sparse binding operations and compute/graphics operations
283 const Queue& sparseQueue = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
284 const Queue& extractQueue = getQueue(getQueueFlags(), 0);
286 // Create memory allocator for logical device
287 const de::UniquePtr<Allocator> allocator(new SimpleAllocator(deviceInterface, *m_logicalDevice, getPhysicalDeviceMemoryProperties(instance, physicalDevice)));
289 // Create sparse image
290 const Unique<VkImage> imageSparse(createImage(deviceInterface, *m_logicalDevice, &imageSparseInfo));
292 // Create sparse image memory bind semaphore
293 const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, *m_logicalDevice));
295 const deUint32 imageSparseSizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
296 const deUint32 imageSizeInPixels = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels) / tcu::getPixelSize(m_format);
298 residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
301 // Get sparse image general memory requirements
302 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
304 // Check if required image memory size does not exceed device limits
305 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
306 TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
308 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
310 // Get sparse image sparse memory requirements
311 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
313 DE_ASSERT(sparseMemoryRequirements.size() != 0);
315 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
317 if (colorAspectIndex == NO_MATCH_FOUND)
318 TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
320 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
322 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
324 const VkImageAspectFlags aspectMask = aspectRequirements.formatProperties.aspectMask;
325 const VkExtent3D imageGranularity = aspectRequirements.formatProperties.imageGranularity;
326 const deUint32 memoryType = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
328 if (memoryType == NO_MATCH_FOUND)
329 return tcu::TestStatus::fail("No matching memory type found");
331 deUint32 pixelOffset = 0u;
333 std::vector<VkSparseImageMemoryBind> imageResidencyMemoryBinds;
334 std::vector<VkSparseMemoryBind> imageMipTailBinds;
336 // Bind memory for each mipmap level
337 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
339 const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
341 if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
343 pixelOffset += mipLevelSizeInPixels;
347 for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
349 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
352 pixelOffset += mipLevelSizeInPixels;
354 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
356 const VkExtent3D mipExtent = mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
357 const tcu::UVec3 sparseBlocks = alignedDivide(mipExtent, imageGranularity);
358 const deUint32 numSparseBlocks = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
359 const VkImageSubresource subresource = { aspectMask, mipLevelNdx, layerNdx };
361 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, *m_logicalDevice,
362 imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
364 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
366 imageResidencyMemoryBinds.push_back(imageMemoryBind);
370 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
372 if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
374 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
375 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
377 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
379 imageMipTailBinds.push_back(imageMipTailMemoryBind);
383 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
385 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
386 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
388 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
390 imageMipTailBinds.push_back(imageMipTailMemoryBind);
394 for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
396 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
400 VkBindSparseInfo bindSparseInfo =
402 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
403 DE_NULL, //const void* pNext;
404 0u, //deUint32 waitSemaphoreCount;
405 DE_NULL, //const VkSemaphore* pWaitSemaphores;
406 0u, //deUint32 bufferBindCount;
407 DE_NULL, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
408 0u, //deUint32 imageOpaqueBindCount;
409 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
410 0u, //deUint32 imageBindCount;
411 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
412 1u, //deUint32 signalSemaphoreCount;
413 &memoryBindSemaphore.get() //const VkSemaphore* pSignalSemaphores;
416 VkSparseImageMemoryBindInfo imageResidencyBindInfo;
417 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
419 if (imageResidencyMemoryBinds.size() > 0)
421 imageResidencyBindInfo.image = *imageSparse;
422 imageResidencyBindInfo.bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
423 imageResidencyBindInfo.pBinds = &imageResidencyMemoryBinds[0];
425 bindSparseInfo.imageBindCount = 1u;
426 bindSparseInfo.pImageBinds = &imageResidencyBindInfo;
429 if (imageMipTailBinds.size() > 0)
431 imageMipTailBindInfo.image = *imageSparse;
432 imageMipTailBindInfo.bindCount = static_cast<deUint32>(imageMipTailBinds.size());
433 imageMipTailBindInfo.pBinds = &imageMipTailBinds[0];
435 bindSparseInfo.imageOpaqueBindCount = 1u;
436 bindSparseInfo.pImageOpaqueBinds = &imageMipTailBindInfo;
439 // Submit sparse bind commands for execution
440 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
443 // Create image to store texels copied from sparse image
444 imageTexelsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
445 imageTexelsInfo.pNext = DE_NULL;
446 imageTexelsInfo.flags = 0u;
447 imageTexelsInfo.imageType = imageSparseInfo.imageType;
448 imageTexelsInfo.format = imageSparseInfo.format;
449 imageTexelsInfo.extent = imageSparseInfo.extent;
450 imageTexelsInfo.arrayLayers = imageSparseInfo.arrayLayers;
451 imageTexelsInfo.mipLevels = imageSparseInfo.mipLevels;
452 imageTexelsInfo.samples = imageSparseInfo.samples;
453 imageTexelsInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
454 imageTexelsInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
455 imageTexelsInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | imageOutputUsageFlags();
456 imageTexelsInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
457 imageTexelsInfo.queueFamilyIndexCount = 0u;
458 imageTexelsInfo.pQueueFamilyIndices = DE_NULL;
460 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
462 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
465 const de::UniquePtr<Image> imageTexels(new Image(deviceInterface, *m_logicalDevice, *allocator, imageTexelsInfo, MemoryRequirement::Any));
467 // Create image to store residency info copied from sparse image
468 imageResidencyInfo = imageTexelsInfo;
469 imageResidencyInfo.format = mapTextureFormat(m_residencyFormat);
471 const de::UniquePtr<Image> imageResidency(new Image(deviceInterface, *m_logicalDevice, *allocator, imageResidencyInfo, MemoryRequirement::Any));
473 // Create command buffer for compute and transfer oparations
474 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, *m_logicalDevice, extractQueue.queueFamilyIndex));
475 const Unique<VkCommandBuffer> commandBuffer(makeCommandBuffer(deviceInterface, *m_logicalDevice, *commandPool));
477 std::vector <VkBufferImageCopy> bufferImageSparseCopy(imageSparseInfo.mipLevels);
480 deUint32 bufferOffset = 0u;
481 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
483 bufferImageSparseCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
484 bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
488 // Start recording commands
489 beginCommandBuffer(deviceInterface, *commandBuffer);
491 // Create input buffer
492 const VkBufferCreateInfo inputBufferCreateInfo = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
493 const de::UniquePtr<Buffer> inputBuffer(new Buffer(deviceInterface, *m_logicalDevice, *allocator, inputBufferCreateInfo, MemoryRequirement::HostVisible));
495 // Fill input buffer with reference data
496 std::vector<deUint8> referenceData(imageSparseSizeInBytes);
498 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
500 const deUint32 mipLevelSizeinBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx);
501 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipLevelNdx].bufferOffset);
503 for (deUint32 byteNdx = 0u; byteNdx < mipLevelSizeinBytes; ++byteNdx)
505 referenceData[bufferOffset + byteNdx] = (deUint8)(mipLevelNdx + byteNdx);
509 deMemcpy(inputBuffer->getAllocation().getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
510 flushMappedMemoryRange(deviceInterface, *m_logicalDevice, inputBuffer->getAllocation().getMemory(), inputBuffer->getAllocation().getOffset(), imageSparseSizeInBytes);
513 // Prepare input buffer for data transfer operation
514 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
516 VK_ACCESS_HOST_WRITE_BIT,
517 VK_ACCESS_TRANSFER_READ_BIT,
520 imageSparseSizeInBytes
523 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
526 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers);
529 // Prepare sparse image for data transfer operation
530 const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
533 VK_ACCESS_TRANSFER_WRITE_BIT,
534 VK_IMAGE_LAYOUT_UNDEFINED,
535 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
536 sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
537 sparseQueue.queueFamilyIndex != extractQueue.queueFamilyIndex ? extractQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
539 fullImageSubresourceRange
542 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageSparseTransferDstBarrier);
545 // Copy reference data from input buffer to sparse image
546 deviceInterface.cmdCopyBufferToImage(*commandBuffer, inputBuffer->get(), *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
548 recordCommands(*allocator, *commandBuffer, imageSparseInfo, *imageSparse, imageTexels->get(), imageResidency->get());
550 const VkBufferCreateInfo bufferTexelsInfo = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
551 const de::UniquePtr<Buffer> bufferTexels(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferTexelsInfo, MemoryRequirement::HostVisible));
553 // Copy data from texels image to buffer
554 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageTexels->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferTexels->get(), static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
556 const deUint32 imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
558 const VkBufferCreateInfo bufferResidencyInfo = makeBufferCreateInfo(imageResidencySizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
559 const de::UniquePtr<Buffer> bufferResidency(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferResidencyInfo, MemoryRequirement::HostVisible));
561 // Copy data from residency image to buffer
562 std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
565 deUint32 bufferOffset = 0u;
566 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
568 bufferImageResidencyCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
569 bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipLevelNdx, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
573 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageResidency->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferResidency->get(), static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
576 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
578 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
580 VK_ACCESS_TRANSFER_WRITE_BIT,
581 VK_ACCESS_HOST_READ_BIT,
584 imageSparseSizeInBytes
587 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
589 VK_ACCESS_TRANSFER_WRITE_BIT,
590 VK_ACCESS_HOST_READ_BIT,
591 bufferResidency->get(),
593 imageResidencySizeInBytes
596 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
599 // End recording commands
600 endCommandBuffer(deviceInterface, *commandBuffer);
602 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
604 // Submit commands for execution and wait for completion
605 submitCommandsAndWait(deviceInterface, *m_logicalDevice, extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
607 // Wait for sparse queue to become idle
608 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
610 // Retrieve data from residency buffer to host memory
611 const Allocation& bufferResidencyAllocation = bufferResidency->getAllocation();
612 invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferResidencyAllocation.getMemory(), bufferResidencyAllocation.getOffset(), imageResidencySizeInBytes);
614 const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAllocation.getHostPtr());
616 deUint32 pixelOffsetNotAligned = 0u;
617 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
619 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipmapNdx);
620 const deUint32 pixelOffsetAligned = static_cast<deUint32>(bufferImageResidencyCopy[mipmapNdx].bufferOffset) / tcu::getPixelSize(m_residencyFormat);
622 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
623 return tcu::TestStatus::fail("Failed");
625 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
628 // Retrieve data from texels buffer to host memory
629 const Allocation& bufferTexelsAllocation = bufferTexels->getAllocation();
630 invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferTexelsAllocation.getMemory(), bufferTexelsAllocation.getOffset(), imageSparseSizeInBytes);
632 const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAllocation.getHostPtr());
634 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
636 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
637 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipmapNdx].bufferOffset);
639 if (mipmapNdx < aspectRequirements.imageMipTailFirstLod)
641 if (mipmapNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_BOUND)
643 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
644 return tcu::TestStatus::fail("Failed");
646 else if (getPhysicalDeviceProperties(instance, physicalDevice).sparseProperties.residencyNonResidentStrict)
648 std::vector<deUint8> zeroData;
649 zeroData.assign(mipLevelSizeInBytes, 0u);
651 if (deMemCmp(&bufferTexelsData[bufferOffset], &zeroData[0], mipLevelSizeInBytes) != 0)
652 return tcu::TestStatus::fail("Failed");
657 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
658 return tcu::TestStatus::fail("Failed");
662 return tcu::TestStatus::pass("Passed");