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 getOpTypeImageSparse (const ImageType imageType,
59 const tcu::TextureFormat& format,
60 const std::string& componentType,
61 const bool requiresSampler)
63 std::ostringstream src;
65 src << "OpTypeImage " << componentType << " ";
72 case IMAGE_TYPE_1D_ARRAY :
78 case IMAGE_TYPE_2D_ARRAY :
84 case IMAGE_TYPE_CUBE :
87 case IMAGE_TYPE_CUBE_ARRAY :
100 switch (format.order)
102 case tcu::TextureFormat::R:
105 case tcu::TextureFormat::RG:
108 case tcu::TextureFormat::RGB:
111 case tcu::TextureFormat::RGBA:
121 case tcu::TextureFormat::SIGNED_INT8:
124 case tcu::TextureFormat::SIGNED_INT16:
127 case tcu::TextureFormat::SIGNED_INT32:
130 case tcu::TextureFormat::UNSIGNED_INT8:
133 case tcu::TextureFormat::UNSIGNED_INT16:
136 case tcu::TextureFormat::UNSIGNED_INT32:
147 std::string getOpTypeImageResidency (const ImageType imageType)
149 std::ostringstream src;
151 src << "OpTypeImage %type_uint ";
156 src << "1D 0 0 0 2 R32ui";
158 case IMAGE_TYPE_1D_ARRAY :
159 src << "1D 0 1 0 2 R32ui";
162 src << "2D 0 0 0 2 R32ui";
164 case IMAGE_TYPE_2D_ARRAY :
165 src << "2D 0 1 0 2 R32ui";
168 src << "3D 0 0 0 2 R32ui";
170 case IMAGE_TYPE_CUBE :
171 src << "Cube 0 0 0 2 R32ui";
173 case IMAGE_TYPE_CUBE_ARRAY :
174 src << "Cube 0 1 0 2 R32ui";
184 tcu::TestStatus SparseShaderIntrinsicsInstanceBase::iterate (void)
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;
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");
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");
203 if (!getPhysicalDeviceFeatures(instance, physicalDevice).shaderResourceResidency)
204 TCU_THROW(NotSupportedError, "Sparse resource residency information not supported in shader code.");
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;
221 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
223 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
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);
237 imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
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");
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));
250 createDeviceSupportingQueues(queueRequirements);
253 const DeviceInterface& deviceInterface = getDeviceInterface();
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);
259 // Create sparse image
260 const Unique<VkImage> imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
262 // Create sparse image memory bind semaphore
263 const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, getDevice()));
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);
268 residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
271 // Get sparse image general memory requirements
272 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
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");
278 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
280 // Get sparse image sparse memory requirements
281 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
283 DE_ASSERT(sparseMemoryRequirements.size() != 0);
285 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
287 if (colorAspectIndex == NO_MATCH_FOUND)
288 TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
290 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
292 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
294 const VkImageAspectFlags aspectMask = aspectRequirements.formatProperties.aspectMask;
295 const VkExtent3D imageGranularity = aspectRequirements.formatProperties.imageGranularity;
296 const deUint32 memoryType = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
298 if (memoryType == NO_MATCH_FOUND)
299 return tcu::TestStatus::fail("No matching memory type found");
301 deUint32 pixelOffset = 0u;
303 std::vector<VkSparseImageMemoryBind> imageResidencyMemoryBinds;
304 std::vector<VkSparseMemoryBind> imageMipTailBinds;
306 // Bind memory for each mipmap level
307 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
309 const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
311 if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
313 pixelOffset += mipLevelSizeInPixels;
317 for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
319 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
322 pixelOffset += mipLevelSizeInPixels;
324 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
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 };
331 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
332 imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
334 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
336 imageResidencyMemoryBinds.push_back(imageMemoryBind);
340 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
342 if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
344 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
345 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
347 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
349 imageMipTailBinds.push_back(imageMipTailMemoryBind);
353 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
355 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
356 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
358 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
360 imageMipTailBinds.push_back(imageMipTailMemoryBind);
364 for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
366 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
370 VkBindSparseInfo bindSparseInfo =
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;
386 VkSparseImageMemoryBindInfo imageResidencyBindInfo;
387 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
389 if (imageResidencyMemoryBinds.size() > 0)
391 imageResidencyBindInfo.image = *imageSparse;
392 imageResidencyBindInfo.bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
393 imageResidencyBindInfo.pBinds = &imageResidencyMemoryBinds[0];
395 bindSparseInfo.imageBindCount = 1u;
396 bindSparseInfo.pImageBinds = &imageResidencyBindInfo;
399 if (imageMipTailBinds.size() > 0)
401 imageMipTailBindInfo.image = *imageSparse;
402 imageMipTailBindInfo.bindCount = static_cast<deUint32>(imageMipTailBinds.size());
403 imageMipTailBindInfo.pBinds = &imageMipTailBinds[0];
405 bindSparseInfo.imageOpaqueBindCount = 1u;
406 bindSparseInfo.pImageOpaqueBinds = &imageMipTailBindInfo;
409 // Submit sparse bind commands for execution
410 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
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;
430 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
432 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
435 const Unique<VkImage> imageTexels (createImage(deviceInterface, getDevice(), &imageTexelsInfo));
436 const de::UniquePtr<Allocation> imageTexelsAlloc (bindImage(deviceInterface, getDevice(), getAllocator(), *imageTexels, MemoryRequirement::Any));
438 // Create image to store residency info copied from sparse image
439 imageResidencyInfo = imageTexelsInfo;
440 imageResidencyInfo.format = mapTextureFormat(m_residencyFormat);
442 const Unique<VkImage> imageResidency (createImage(deviceInterface, getDevice(), &imageResidencyInfo));
443 const de::UniquePtr<Allocation> imageResidencyAlloc (bindImage(deviceInterface, getDevice(), getAllocator(), *imageResidency, MemoryRequirement::Any));
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));
449 std::vector <VkBufferImageCopy> bufferImageSparseCopy(imageSparseInfo.mipLevels);
452 deUint32 bufferOffset = 0u;
453 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
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);
460 // Start recording commands
461 beginCommandBuffer(deviceInterface, *commandBuffer);
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));
468 // Fill input buffer with reference data
469 std::vector<deUint8> referenceData(imageSparseSizeInBytes);
471 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
473 const deUint32 mipLevelSizeinBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx);
474 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipLevelNdx].bufferOffset);
476 for (deUint32 byteNdx = 0u; byteNdx < mipLevelSizeinBytes; ++byteNdx)
478 referenceData[bufferOffset + byteNdx] = (deUint8)(mipLevelNdx + byteNdx);
482 deMemcpy(inputBufferAlloc->getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
483 flushMappedMemoryRange(deviceInterface, getDevice(), inputBufferAlloc->getMemory(), inputBufferAlloc->getOffset(), imageSparseSizeInBytes);
486 // Prepare input buffer for data transfer operation
487 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
489 VK_ACCESS_HOST_WRITE_BIT,
490 VK_ACCESS_TRANSFER_READ_BIT,
493 imageSparseSizeInBytes
496 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
499 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers);
502 // Prepare sparse image for data transfer operation
503 const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
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,
512 fullImageSubresourceRange
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);
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]);
521 recordCommands(*commandBuffer, imageSparseInfo, *imageSparse, *imageTexels, *imageResidency);
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));
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]);
530 const deUint32 imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
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));
536 // Copy data from residency image to buffer
537 std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
540 deUint32 bufferOffset = 0u;
541 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
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);
548 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageResidency, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *bufferResidency, static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
551 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
553 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
555 VK_ACCESS_TRANSFER_WRITE_BIT,
556 VK_ACCESS_HOST_READ_BIT,
559 imageSparseSizeInBytes
562 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
564 VK_ACCESS_TRANSFER_WRITE_BIT,
565 VK_ACCESS_HOST_READ_BIT,
568 imageResidencySizeInBytes
571 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
574 // End recording commands
575 endCommandBuffer(deviceInterface, *commandBuffer);
577 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
579 // Submit commands for execution and wait for completion
580 submitCommandsAndWait(deviceInterface, getDevice(), extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
582 // Wait for sparse queue to become idle
583 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
585 // Retrieve data from residency buffer to host memory
586 invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferResidencyAlloc->getMemory(), bufferResidencyAlloc->getOffset(), imageResidencySizeInBytes);
588 const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAlloc->getHostPtr());
590 deUint32 pixelOffsetNotAligned = 0u;
591 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
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);
596 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
597 return tcu::TestStatus::fail("Failed");
599 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
602 // Retrieve data from texels buffer to host memory
603 invalidateMappedMemoryRange(deviceInterface, getDevice(), bufferTexelsAlloc->getMemory(), bufferTexelsAlloc->getOffset(), imageSparseSizeInBytes);
605 const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAlloc->getHostPtr());
607 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
609 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
610 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipmapNdx].bufferOffset);
612 if (mipmapNdx < aspectRequirements.imageMipTailFirstLod)
614 if (mipmapNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_BOUND)
616 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
617 return tcu::TestStatus::fail("Failed");
619 else if (getPhysicalDeviceProperties(instance, physicalDevice).sparseProperties.residencyNonResidentStrict)
621 std::vector<deUint8> zeroData;
622 zeroData.assign(mipLevelSizeInBytes, 0u);
624 if (deMemCmp(&bufferTexelsData[bufferOffset], &zeroData[0], mipLevelSizeInBytes) != 0)
625 return tcu::TestStatus::fail("Failed");
630 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
631 return tcu::TestStatus::fail("Failed");
635 return tcu::TestStatus::pass("Passed");