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 DeviceInterface& deviceInterface = m_context.getDeviceInterface();
188 const VkPhysicalDevice physicalDevice = m_context.getPhysicalDevice();
189 VkImageCreateInfo imageSparseInfo;
190 VkImageCreateInfo imageTexelsInfo;
191 VkImageCreateInfo imageResidencyInfo;
192 VkSparseImageMemoryRequirements aspectRequirements;
193 std::vector <deUint32> residencyReferenceData;
194 std::vector<DeviceMemoryUniquePtr> deviceMemUniquePtrVec;
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");
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");
204 if (!getPhysicalDeviceFeatures(instance, physicalDevice).shaderResourceResidency)
205 TCU_THROW(NotSupportedError, "Sparse resource residency information not supported in shader code.");
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;
222 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
224 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
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);
238 imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
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");
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));
251 createDeviceSupportingQueues(queueRequirements);
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);
258 // Create memory allocator for logical device
259 const de::UniquePtr<Allocator> allocator(new SimpleAllocator(deviceInterface, *m_logicalDevice, getPhysicalDeviceMemoryProperties(instance, physicalDevice)));
261 // Create sparse image
262 const Unique<VkImage> imageSparse(createImage(deviceInterface, *m_logicalDevice, &imageSparseInfo));
264 // Create sparse image memory bind semaphore
265 const Unique<VkSemaphore> memoryBindSemaphore(makeSemaphore(deviceInterface, *m_logicalDevice));
267 const deUint32 imageSparseSizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
268 const deUint32 imageSizeInPixels = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels) / tcu::getPixelSize(m_format);
270 residencyReferenceData.assign(imageSizeInPixels, MEMORY_BLOCK_NOT_BOUND_VALUE);
273 // Get sparse image general memory requirements
274 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
276 // Check if required image memory size does not exceed device limits
277 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
278 TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
280 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
282 // Get sparse image sparse memory requirements
283 const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, *m_logicalDevice, *imageSparse);
285 DE_ASSERT(sparseMemoryRequirements.size() != 0);
287 const deUint32 colorAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
289 if (colorAspectIndex == NO_MATCH_FOUND)
290 TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
292 aspectRequirements = sparseMemoryRequirements[colorAspectIndex];
294 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
296 const VkImageAspectFlags aspectMask = aspectRequirements.formatProperties.aspectMask;
297 const VkExtent3D imageGranularity = aspectRequirements.formatProperties.imageGranularity;
298 const deUint32 memoryType = findMatchingMemoryType(instance, physicalDevice, imageMemoryRequirements, MemoryRequirement::Any);
300 if (memoryType == NO_MATCH_FOUND)
301 return tcu::TestStatus::fail("No matching memory type found");
303 deUint32 pixelOffset = 0u;
305 std::vector<VkSparseImageMemoryBind> imageResidencyMemoryBinds;
306 std::vector<VkSparseMemoryBind> imageMipTailBinds;
308 // Bind memory for each mipmap level
309 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
311 const deUint32 mipLevelSizeInPixels = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx) / tcu::getPixelSize(m_format);
313 if (mipLevelNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_NOT_BOUND)
315 pixelOffset += mipLevelSizeInPixels;
319 for (deUint32 pixelNdx = 0u; pixelNdx < mipLevelSizeInPixels; ++pixelNdx)
321 residencyReferenceData[pixelOffset + pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
324 pixelOffset += mipLevelSizeInPixels;
326 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
328 const VkExtent3D mipExtent = mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
329 const tcu::UVec3 sparseBlocks = alignedDivide(mipExtent, imageGranularity);
330 const deUint32 numSparseBlocks = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
331 const VkImageSubresource subresource = { aspectMask, mipLevelNdx, layerNdx };
333 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, *m_logicalDevice,
334 imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
336 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
338 imageResidencyMemoryBinds.push_back(imageMemoryBind);
342 if (aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
344 if (aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
346 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
347 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
349 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
351 imageMipTailBinds.push_back(imageMipTailMemoryBind);
355 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
357 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, *m_logicalDevice,
358 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
360 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, *m_logicalDevice, DE_NULL))));
362 imageMipTailBinds.push_back(imageMipTailMemoryBind);
366 for (deUint32 pixelNdx = pixelOffset; pixelNdx < residencyReferenceData.size(); ++pixelNdx)
368 residencyReferenceData[pixelNdx] = MEMORY_BLOCK_BOUND_VALUE;
372 VkBindSparseInfo bindSparseInfo =
374 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
375 DE_NULL, //const void* pNext;
376 0u, //deUint32 waitSemaphoreCount;
377 DE_NULL, //const VkSemaphore* pWaitSemaphores;
378 0u, //deUint32 bufferBindCount;
379 DE_NULL, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
380 0u, //deUint32 imageOpaqueBindCount;
381 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
382 0u, //deUint32 imageBindCount;
383 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
384 1u, //deUint32 signalSemaphoreCount;
385 &memoryBindSemaphore.get() //const VkSemaphore* pSignalSemaphores;
388 VkSparseImageMemoryBindInfo imageResidencyBindInfo;
389 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
391 if (imageResidencyMemoryBinds.size() > 0)
393 imageResidencyBindInfo.image = *imageSparse;
394 imageResidencyBindInfo.bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
395 imageResidencyBindInfo.pBinds = &imageResidencyMemoryBinds[0];
397 bindSparseInfo.imageBindCount = 1u;
398 bindSparseInfo.pImageBinds = &imageResidencyBindInfo;
401 if (imageMipTailBinds.size() > 0)
403 imageMipTailBindInfo.image = *imageSparse;
404 imageMipTailBindInfo.bindCount = static_cast<deUint32>(imageMipTailBinds.size());
405 imageMipTailBindInfo.pBinds = &imageMipTailBinds[0];
407 bindSparseInfo.imageOpaqueBindCount = 1u;
408 bindSparseInfo.pImageOpaqueBinds = &imageMipTailBindInfo;
411 // Submit sparse bind commands for execution
412 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
415 // Create image to store texels copied from sparse image
416 imageTexelsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
417 imageTexelsInfo.pNext = DE_NULL;
418 imageTexelsInfo.flags = 0u;
419 imageTexelsInfo.imageType = imageSparseInfo.imageType;
420 imageTexelsInfo.format = imageSparseInfo.format;
421 imageTexelsInfo.extent = imageSparseInfo.extent;
422 imageTexelsInfo.arrayLayers = imageSparseInfo.arrayLayers;
423 imageTexelsInfo.mipLevels = imageSparseInfo.mipLevels;
424 imageTexelsInfo.samples = imageSparseInfo.samples;
425 imageTexelsInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
426 imageTexelsInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
427 imageTexelsInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | imageOutputUsageFlags();
428 imageTexelsInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
429 imageTexelsInfo.queueFamilyIndexCount = 0u;
430 imageTexelsInfo.pQueueFamilyIndices = DE_NULL;
432 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
434 imageTexelsInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
437 const de::UniquePtr<Image> imageTexels(new Image(deviceInterface, *m_logicalDevice, *allocator, imageTexelsInfo, MemoryRequirement::Any));
439 // Create image to store residency info copied from sparse image
440 imageResidencyInfo = imageTexelsInfo;
441 imageResidencyInfo.format = mapTextureFormat(m_residencyFormat);
443 const de::UniquePtr<Image> imageResidency(new Image(deviceInterface, *m_logicalDevice, *allocator, imageResidencyInfo, MemoryRequirement::Any));
445 // Create command buffer for compute and transfer oparations
446 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, *m_logicalDevice, extractQueue.queueFamilyIndex));
447 const Unique<VkCommandBuffer> commandBuffer(makeCommandBuffer(deviceInterface, *m_logicalDevice, *commandPool));
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, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
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 de::UniquePtr<Buffer> inputBuffer(new Buffer(deviceInterface, *m_logicalDevice, *allocator, inputBufferCreateInfo, MemoryRequirement::HostVisible));
467 // Fill input buffer with reference data
468 std::vector<deUint8> referenceData(imageSparseSizeInBytes);
470 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
472 const deUint32 mipLevelSizeinBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipLevelNdx);
473 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipLevelNdx].bufferOffset);
475 for (deUint32 byteNdx = 0u; byteNdx < mipLevelSizeinBytes; ++byteNdx)
477 referenceData[bufferOffset + byteNdx] = (deUint8)(mipLevelNdx + byteNdx);
481 deMemcpy(inputBuffer->getAllocation().getHostPtr(), &referenceData[0], imageSparseSizeInBytes);
482 flushMappedMemoryRange(deviceInterface, *m_logicalDevice, inputBuffer->getAllocation().getMemory(), inputBuffer->getAllocation().getOffset(), imageSparseSizeInBytes);
485 // Prepare input buffer for data transfer operation
486 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
488 VK_ACCESS_HOST_WRITE_BIT,
489 VK_ACCESS_TRANSFER_READ_BIT,
492 imageSparseSizeInBytes
495 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
498 const VkImageSubresourceRange fullImageSubresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers);
501 // Prepare sparse image for data transfer operation
502 const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
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,
511 fullImageSubresourceRange
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);
517 // Copy reference data from input buffer to sparse image
518 deviceInterface.cmdCopyBufferToImage(*commandBuffer, inputBuffer->get(), *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
520 recordCommands(*allocator, *commandBuffer, imageSparseInfo, *imageSparse, imageTexels->get(), imageResidency->get());
522 const VkBufferCreateInfo bufferTexelsInfo = makeBufferCreateInfo(imageSparseSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
523 const de::UniquePtr<Buffer> bufferTexels(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferTexelsInfo, MemoryRequirement::HostVisible));
525 // Copy data from texels image to buffer
526 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageTexels->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferTexels->get(), static_cast<deUint32>(bufferImageSparseCopy.size()), &bufferImageSparseCopy[0]);
528 const deUint32 imageResidencySizeInBytes = getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, imageSparseInfo.mipLevels, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
530 const VkBufferCreateInfo bufferResidencyInfo = makeBufferCreateInfo(imageResidencySizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
531 const de::UniquePtr<Buffer> bufferResidency(new Buffer(deviceInterface, *m_logicalDevice, *allocator, bufferResidencyInfo, MemoryRequirement::HostVisible));
533 // Copy data from residency image to buffer
534 std::vector <VkBufferImageCopy> bufferImageResidencyCopy(imageSparseInfo.mipLevels);
537 deUint32 bufferOffset = 0u;
538 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
540 bufferImageResidencyCopy[mipLevelNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipLevelNdx), imageSparseInfo.arrayLayers, mipLevelNdx, static_cast<VkDeviceSize>(bufferOffset));
541 bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipLevelNdx, MEM_ALIGN_BUFFERIMAGECOPY_OFFSET);
545 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, imageResidency->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, bufferResidency->get(), static_cast<deUint32>(bufferImageResidencyCopy.size()), &bufferImageResidencyCopy[0]);
548 VkBufferMemoryBarrier bufferOutputHostReadBarriers[2];
550 bufferOutputHostReadBarriers[0] = makeBufferMemoryBarrier
552 VK_ACCESS_TRANSFER_WRITE_BIT,
553 VK_ACCESS_HOST_READ_BIT,
556 imageSparseSizeInBytes
559 bufferOutputHostReadBarriers[1] = makeBufferMemoryBarrier
561 VK_ACCESS_TRANSFER_WRITE_BIT,
562 VK_ACCESS_HOST_READ_BIT,
563 bufferResidency->get(),
565 imageResidencySizeInBytes
568 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 2u, bufferOutputHostReadBarriers, 0u, DE_NULL);
571 // End recording commands
572 endCommandBuffer(deviceInterface, *commandBuffer);
574 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
576 // Submit commands for execution and wait for completion
577 submitCommandsAndWait(deviceInterface, *m_logicalDevice, extractQueue.queueHandle, *commandBuffer, 1u, &memoryBindSemaphore.get(), stageBits);
579 // Wait for sparse queue to become idle
580 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
582 // Retrieve data from residency buffer to host memory
583 const Allocation& bufferResidencyAllocation = bufferResidency->getAllocation();
584 invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferResidencyAllocation.getMemory(), bufferResidencyAllocation.getOffset(), imageResidencySizeInBytes);
586 const deUint32* bufferResidencyData = static_cast<const deUint32*>(bufferResidencyAllocation.getHostPtr());
588 deUint32 pixelOffsetNotAligned = 0u;
589 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
591 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_residencyFormat, mipmapNdx);
592 const deUint32 pixelOffsetAligned = static_cast<deUint32>(bufferImageResidencyCopy[mipmapNdx].bufferOffset) / tcu::getPixelSize(m_residencyFormat);
594 if (deMemCmp(&bufferResidencyData[pixelOffsetAligned], &residencyReferenceData[pixelOffsetNotAligned], mipLevelSizeInBytes) != 0)
595 return tcu::TestStatus::fail("Failed");
597 pixelOffsetNotAligned += mipLevelSizeInBytes / tcu::getPixelSize(m_residencyFormat);
600 // Retrieve data from texels buffer to host memory
601 const Allocation& bufferTexelsAllocation = bufferTexels->getAllocation();
602 invalidateMappedMemoryRange(deviceInterface, *m_logicalDevice, bufferTexelsAllocation.getMemory(), bufferTexelsAllocation.getOffset(), imageSparseSizeInBytes);
604 const deUint8* bufferTexelsData = static_cast<const deUint8*>(bufferTexelsAllocation.getHostPtr());
606 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
608 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
609 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageSparseCopy[mipmapNdx].bufferOffset);
611 if (mipmapNdx < aspectRequirements.imageMipTailFirstLod)
613 if (mipmapNdx % MEMORY_BLOCK_TYPE_COUNT == MEMORY_BLOCK_BOUND)
615 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
616 return tcu::TestStatus::fail("Failed");
618 else if (getPhysicalDeviceProperties(instance, physicalDevice).sparseProperties.residencyNonResidentStrict)
620 std::vector<deUint8> zeroData;
621 zeroData.assign(mipLevelSizeInBytes, 0u);
623 if (deMemCmp(&bufferTexelsData[bufferOffset], &zeroData[0], mipLevelSizeInBytes) != 0)
624 return tcu::TestStatus::fail("Failed");
629 if (deMemCmp(&bufferTexelsData[bufferOffset], &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
630 return tcu::TestStatus::fail("Failed");
634 return tcu::TestStatus::pass("Passed");