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 vktSparseResourcesMipmapSparseResidency.cpp
21 * \brief Sparse partially resident images with mipmaps tests
22 *//*--------------------------------------------------------------------*/
24 #include "vktSparseResourcesMipmapSparseResidency.hpp"
25 #include "vktSparseResourcesTestsUtil.hpp"
26 #include "vktSparseResourcesBase.hpp"
27 #include "vktTestCaseUtil.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkPlatform.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkMemUtil.hpp"
35 #include "vkBarrierUtil.hpp"
36 #include "vkBuilderUtil.hpp"
37 #include "vkImageUtil.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "vkTypeUtil.hpp"
40 #include "vkCmdUtil.hpp"
42 #include "deUniquePtr.hpp"
43 #include "deStringUtil.hpp"
44 #include "tcuTextureUtil.hpp"
58 class MipmapSparseResidencyCase : public TestCase
61 MipmapSparseResidencyCase (tcu::TestContext& testCtx,
62 const std::string& name,
63 const std::string& description,
64 const ImageType imageType,
65 const tcu::UVec3& imageSize,
66 const VkFormat format,
67 const bool useDeviceGroups);
69 TestInstance* createInstance (Context& context) const;
70 virtual void checkSupport (Context& context) const;
73 const bool m_useDeviceGroups;
74 const ImageType m_imageType;
75 const tcu::UVec3 m_imageSize;
76 const VkFormat m_format;
79 MipmapSparseResidencyCase::MipmapSparseResidencyCase (tcu::TestContext& testCtx,
80 const std::string& name,
81 const std::string& description,
82 const ImageType imageType,
83 const tcu::UVec3& imageSize,
84 const VkFormat format,
85 const bool useDeviceGroups)
86 : TestCase (testCtx, name, description)
87 , m_useDeviceGroups (useDeviceGroups)
88 , m_imageType (imageType)
89 , m_imageSize (imageSize)
94 void MipmapSparseResidencyCase::checkSupport (Context& context) const
96 const InstanceInterface& instance = context.getInstanceInterface();
97 const VkPhysicalDevice physicalDevice = context.getPhysicalDevice();
99 // Check if image size does not exceed device limits
100 if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
101 TCU_THROW(NotSupportedError, "Image size not supported for device");
103 // Check if device supports sparse operations for image type
104 if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
105 TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
107 if (formatIsR64(m_format))
109 context.requireDeviceFunctionality("VK_EXT_shader_image_atomic_int64");
111 if (context.getShaderImageAtomicInt64FeaturesEXT().sparseImageInt64Atomics == VK_FALSE)
113 TCU_THROW(NotSupportedError, "sparseImageInt64Atomics is not supported for device");
118 class MipmapSparseResidencyInstance : public SparseResourcesBaseInstance
121 MipmapSparseResidencyInstance (Context& context,
122 const ImageType imageType,
123 const tcu::UVec3& imageSize,
124 const VkFormat format,
125 const bool useDeviceGroups);
128 tcu::TestStatus iterate (void);
131 const bool m_useDeviceGroups;
132 const ImageType m_imageType;
133 const tcu::UVec3 m_imageSize;
134 const VkFormat m_format;
137 MipmapSparseResidencyInstance::MipmapSparseResidencyInstance (Context& context,
138 const ImageType imageType,
139 const tcu::UVec3& imageSize,
140 const VkFormat format,
141 const bool useDeviceGroups)
142 : SparseResourcesBaseInstance (context, useDeviceGroups)
143 , m_useDeviceGroups (useDeviceGroups)
144 , m_imageType (imageType)
145 , m_imageSize (imageSize)
150 tcu::TestStatus MipmapSparseResidencyInstance::iterate (void)
152 const InstanceInterface& instance = m_context.getInstanceInterface();
154 // Create logical device supporting both sparse and compute operations
155 QueueRequirementsVec queueRequirements;
156 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
157 queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
159 createDeviceSupportingQueues(queueRequirements);
162 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
163 VkImageCreateInfo imageSparseInfo;
164 std::vector<DeviceMemorySp> deviceMemUniquePtrVec;
166 const DeviceInterface& deviceInterface = getDeviceInterface();
167 const Queue& sparseQueue = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
168 const Queue& computeQueue = getQueue(VK_QUEUE_COMPUTE_BIT, 0);
169 const PlanarFormatDescription formatDescription = getPlanarFormatDescription(m_format);
171 // Go through all physical devices
172 for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
174 const deUint32 firstDeviceID = physDevID;
175 const deUint32 secondDeviceID = (firstDeviceID + 1) % m_numPhysicalDevices;
177 imageSparseInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
178 imageSparseInfo.pNext = DE_NULL;
179 imageSparseInfo.flags = VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
180 imageSparseInfo.imageType = mapImageType(m_imageType);
181 imageSparseInfo.format = m_format;
182 imageSparseInfo.extent = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
183 imageSparseInfo.arrayLayers = getNumLayers(m_imageType, m_imageSize);
184 imageSparseInfo.samples = VK_SAMPLE_COUNT_1_BIT;
185 imageSparseInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
186 imageSparseInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
187 imageSparseInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT |
188 VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
189 imageSparseInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
190 imageSparseInfo.queueFamilyIndexCount = 0u;
191 imageSparseInfo.pQueueFamilyIndices = DE_NULL;
193 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
195 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
198 // Check if device supports sparse operations for image format
199 if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
200 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
203 VkImageFormatProperties imageFormatProperties;
204 if (instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
205 imageSparseInfo.format,
206 imageSparseInfo.imageType,
207 imageSparseInfo.tiling,
208 imageSparseInfo.usage,
209 imageSparseInfo.flags,
210 &imageFormatProperties) == VK_ERROR_FORMAT_NOT_SUPPORTED)
212 TCU_THROW(NotSupportedError, "Image format does not support sparse operations");
215 imageSparseInfo.mipLevels = getMipmapCount(m_format, formatDescription, imageFormatProperties, imageSparseInfo.extent);
218 // Create sparse image
219 const Unique<VkImage> imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
221 // Create sparse image memory bind semaphore
222 const Unique<VkSemaphore> imageMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
224 std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements;
227 // Get sparse image general memory requirements
228 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
230 // Check if required image memory size does not exceed device limits
231 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
232 TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
234 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
236 const deUint32 memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), imageMemoryRequirements, MemoryRequirement::Any);
238 if (memoryType == NO_MATCH_FOUND)
239 return tcu::TestStatus::fail("No matching memory type found");
241 if (firstDeviceID != secondDeviceID)
243 VkPeerMemoryFeatureFlags peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
244 const deUint32 heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
245 deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
247 if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) == 0) ||
248 ((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) == 0))
250 TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC and COPY_DST");
254 // Get sparse image sparse memory requirements
255 sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
256 DE_ASSERT(sparseMemoryRequirements.size() != 0);
258 const deUint32 metadataAspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_METADATA_BIT);
260 std::vector<VkSparseImageMemoryBind> imageResidencyMemoryBinds;
261 std::vector<VkSparseMemoryBind> imageMipTailMemoryBinds;
263 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
265 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
266 const deUint32 aspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, aspect);
268 if (aspectIndex == NO_MATCH_FOUND)
269 TCU_THROW(NotSupportedError, "Not supported image aspect");
271 VkSparseImageMemoryRequirements aspectRequirements = sparseMemoryRequirements[aspectIndex];
273 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
275 VkExtent3D imageGranularity = aspectRequirements.formatProperties.imageGranularity;
277 // Bind memory for each layer
278 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
280 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
282 const VkExtent3D mipExtent = getPlaneExtent(formatDescription, imageSparseInfo.extent, planeNdx, mipLevelNdx);
283 const tcu::UVec3 sparseBlocks = alignedDivide(mipExtent, imageGranularity);
284 const deUint32 numSparseBlocks = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
285 const VkImageSubresource subresource = { aspect, mipLevelNdx, layerNdx };
287 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
288 imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
290 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
292 imageResidencyMemoryBinds.push_back(imageMemoryBind);
295 if (!(aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
297 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
298 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
300 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
302 imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
306 if (metadataAspectIndex != NO_MATCH_FOUND)
308 const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
310 if (!(metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT))
312 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
313 metadataAspectRequirements.imageMipTailSize, memoryType,
314 metadataAspectRequirements.imageMipTailOffset + layerNdx * metadataAspectRequirements.imageMipTailStride,
315 VK_SPARSE_MEMORY_BIND_METADATA_BIT);
317 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
319 imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
324 if ((aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
326 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
327 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
329 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
331 imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
336 if (metadataAspectIndex != NO_MATCH_FOUND)
338 const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
340 if (metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
342 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
343 metadataAspectRequirements.imageMipTailSize, memoryType, metadataAspectRequirements.imageMipTailOffset,
344 VK_SPARSE_MEMORY_BIND_METADATA_BIT);
346 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
348 imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
352 const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
354 VK_STRUCTURE_TYPE_DEVICE_GROUP_BIND_SPARSE_INFO, //VkStructureType sType;
355 DE_NULL, //const void* pNext;
356 firstDeviceID, //deUint32 resourceDeviceIndex;
357 secondDeviceID, //deUint32 memoryDeviceIndex;
360 VkBindSparseInfo bindSparseInfo =
362 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
363 m_useDeviceGroups ? &devGroupBindSparseInfo : DE_NULL, //const void* pNext;
364 0u, //deUint32 waitSemaphoreCount;
365 DE_NULL, //const VkSemaphore* pWaitSemaphores;
366 0u, //deUint32 bufferBindCount;
367 DE_NULL, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
368 0u, //deUint32 imageOpaqueBindCount;
369 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
370 0u, //deUint32 imageBindCount;
371 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
372 1u, //deUint32 signalSemaphoreCount;
373 &imageMemoryBindSemaphore.get() //const VkSemaphore* pSignalSemaphores;
376 VkSparseImageMemoryBindInfo imageResidencyBindInfo;
377 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo;
379 if (imageResidencyMemoryBinds.size() > 0)
381 imageResidencyBindInfo.image = *imageSparse;
382 imageResidencyBindInfo.bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
383 imageResidencyBindInfo.pBinds = imageResidencyMemoryBinds.data();
385 bindSparseInfo.imageBindCount = 1u;
386 bindSparseInfo.pImageBinds = &imageResidencyBindInfo;
389 if (imageMipTailMemoryBinds.size() > 0)
391 imageMipTailBindInfo.image = *imageSparse;
392 imageMipTailBindInfo.bindCount = static_cast<deUint32>(imageMipTailMemoryBinds.size());
393 imageMipTailBindInfo.pBinds = imageMipTailMemoryBinds.data();
395 bindSparseInfo.imageOpaqueBindCount = 1u;
396 bindSparseInfo.pImageOpaqueBinds = &imageMipTailBindInfo;
399 // Submit sparse bind commands for execution
400 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
403 deUint32 imageSizeInBytes = 0;
405 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
406 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
407 imageSizeInBytes += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
409 std::vector <VkBufferImageCopy> bufferImageCopy(formatDescription.numPlanes*imageSparseInfo.mipLevels);
411 deUint32 bufferOffset = 0;
413 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
415 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
417 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
419 bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx] =
421 bufferOffset, // VkDeviceSize bufferOffset;
422 0u, // deUint32 bufferRowLength;
423 0u, // deUint32 bufferImageHeight;
424 makeImageSubresourceLayers(aspect, mipmapNdx, 0u, imageSparseInfo.arrayLayers), // VkImageSubresourceLayers imageSubresource;
425 makeOffset3D(0, 0, 0), // VkOffset3D imageOffset;
426 vk::getPlaneExtent(formatDescription, imageSparseInfo.extent, planeNdx, mipmapNdx) // VkExtent3D imageExtent;
428 bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
433 // Create command buffer for compute and transfer operations
434 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
435 const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
437 // Start recording commands
438 beginCommandBuffer(deviceInterface, *commandBuffer);
440 const VkBufferCreateInfo inputBufferCreateInfo = makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
441 const Unique<VkBuffer> inputBuffer (createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
442 const de::UniquePtr<Allocation> inputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
444 std::vector<deUint8> referenceData(imageSizeInBytes);
446 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
448 for (deUint32 valueNdx = 0; valueNdx < imageSizeInBytes; ++valueNdx)
450 referenceData[valueNdx] = static_cast<deUint8>((valueNdx % imageMemoryRequirements.alignment) + 1u);
454 deMemcpy(inputBufferAlloc->getHostPtr(), referenceData.data(), imageSizeInBytes);
455 flushAlloc(deviceInterface, getDevice(), *inputBufferAlloc);
457 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
459 VK_ACCESS_HOST_WRITE_BIT,
460 VK_ACCESS_TRANSFER_READ_BIT,
466 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
470 std::vector<VkImageMemoryBarrier> imageSparseTransferDstBarriers;
472 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
474 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
476 imageSparseTransferDstBarriers.emplace_back ( makeImageMemoryBarrier
479 VK_ACCESS_TRANSFER_WRITE_BIT,
480 VK_IMAGE_LAYOUT_UNDEFINED,
481 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
483 makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers),
484 sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
485 sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? computeQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED
488 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, static_cast<deUint32>(imageSparseTransferDstBarriers.size()), imageSparseTransferDstBarriers.data());
491 deviceInterface.cmdCopyBufferToImage(*commandBuffer, *inputBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageCopy.size()), &bufferImageCopy[0]);
494 std::vector<VkImageMemoryBarrier> imageSparseTransferSrcBarriers;
496 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
498 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
500 imageSparseTransferSrcBarriers.emplace_back(makeImageMemoryBarrier
502 VK_ACCESS_TRANSFER_WRITE_BIT,
503 VK_ACCESS_TRANSFER_READ_BIT,
504 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
505 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
507 makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers)
511 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, static_cast<deUint32>(imageSparseTransferSrcBarriers.size()), imageSparseTransferSrcBarriers.data());
514 const VkBufferCreateInfo outputBufferCreateInfo = makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
515 const Unique<VkBuffer> outputBuffer (createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
516 const de::UniquePtr<Allocation> outputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
518 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *outputBuffer, static_cast<deUint32>(bufferImageCopy.size()), bufferImageCopy.data());
521 const VkBufferMemoryBarrier outputBufferBarrier = makeBufferMemoryBarrier
523 VK_ACCESS_TRANSFER_WRITE_BIT,
524 VK_ACCESS_HOST_READ_BIT,
530 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
533 // End recording commands
534 endCommandBuffer(deviceInterface, *commandBuffer);
536 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
538 // Submit commands for execution and wait for completion
539 submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 1u, &imageMemoryBindSemaphore.get(), stageBits,
540 0, DE_NULL, m_useDeviceGroups, firstDeviceID);
542 // Retrieve data from buffer to host memory
543 invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
545 const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
547 // Wait for sparse queue to become idle
548 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
550 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
552 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
554 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx);
555 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx].bufferOffset);
557 if (deMemCmp(outputData + bufferOffset, &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
558 return tcu::TestStatus::fail("Failed");
562 return tcu::TestStatus::pass("Passed");
565 TestInstance* MipmapSparseResidencyCase::createInstance (Context& context) const
567 return new MipmapSparseResidencyInstance(context, m_imageType, m_imageSize, m_format, m_useDeviceGroups);
572 tcu::TestCaseGroup* createMipmapSparseResidencyTestsCommon (tcu::TestContext& testCtx, de::MovePtr<tcu::TestCaseGroup> testGroup, const bool useDeviceGroup = false)
574 const std::vector<TestImageParameters> imageParameters
576 { IMAGE_TYPE_2D, { tcu::UVec3(512u, 256u, 1u), tcu::UVec3(1024u, 128u, 1u), tcu::UVec3(11u, 137u, 1u) }, getTestFormats(IMAGE_TYPE_2D) },
577 { IMAGE_TYPE_2D_ARRAY, { tcu::UVec3(512u, 256u, 6u), tcu::UVec3(1024u, 128u, 8u), tcu::UVec3(11u, 137u, 3u) }, getTestFormats(IMAGE_TYPE_2D_ARRAY) },
578 { IMAGE_TYPE_CUBE, { tcu::UVec3(256u, 256u, 1u), tcu::UVec3(128u, 128u, 1u), tcu::UVec3(137u, 137u, 1u) }, getTestFormats(IMAGE_TYPE_CUBE) },
579 { IMAGE_TYPE_CUBE_ARRAY, { tcu::UVec3(256u, 256u, 6u), tcu::UVec3(128u, 128u, 8u), tcu::UVec3(137u, 137u, 3u) }, getTestFormats(IMAGE_TYPE_CUBE_ARRAY) },
580 { IMAGE_TYPE_3D, { tcu::UVec3(256u, 256u, 16u), tcu::UVec3(1024u, 128u, 8u), tcu::UVec3(11u, 137u, 3u) }, getTestFormats(IMAGE_TYPE_3D) }
583 for (size_t imageTypeNdx = 0; imageTypeNdx < imageParameters.size(); ++imageTypeNdx)
585 const ImageType imageType = imageParameters[imageTypeNdx].imageType;
586 de::MovePtr<tcu::TestCaseGroup> imageTypeGroup(new tcu::TestCaseGroup(testCtx, getImageTypeName(imageType).c_str(), ""));
588 for (size_t formatNdx = 0; formatNdx < imageParameters[imageTypeNdx].formats.size(); ++formatNdx)
590 VkFormat format = imageParameters[imageTypeNdx].formats[formatNdx].format;
591 tcu::UVec3 imageSizeAlignment = getImageSizeAlignment(format);
592 de::MovePtr<tcu::TestCaseGroup> formatGroup (new tcu::TestCaseGroup(testCtx, getImageFormatID(format).c_str(), ""));
594 for (size_t imageSizeNdx = 0; imageSizeNdx < imageParameters[imageTypeNdx].imageSizes.size(); ++imageSizeNdx)
596 const tcu::UVec3 imageSize = imageParameters[imageTypeNdx].imageSizes[imageSizeNdx];
598 // skip test for images with odd sizes for some YCbCr formats
599 if ((imageSize.x() % imageSizeAlignment.x()) != 0)
601 if ((imageSize.y() % imageSizeAlignment.y()) != 0)
604 std::ostringstream stream;
605 stream << imageSize.x() << "_" << imageSize.y() << "_" << imageSize.z();
607 formatGroup->addChild(new MipmapSparseResidencyCase(testCtx, stream.str(), "", imageType, imageSize, format, useDeviceGroup));
609 imageTypeGroup->addChild(formatGroup.release());
611 testGroup->addChild(imageTypeGroup.release());
614 return testGroup.release();
617 tcu::TestCaseGroup* createMipmapSparseResidencyTests (tcu::TestContext& testCtx)
619 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "mipmap_sparse_residency", "Mipmap Sparse Residency"));
620 return createMipmapSparseResidencyTestsCommon(testCtx, testGroup);
623 tcu::TestCaseGroup* createDeviceGroupMipmapSparseResidencyTests (tcu::TestContext& testCtx)
625 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "device_group_mipmap_sparse_residency", "Mipmap Sparse Residency"));
626 return createMipmapSparseResidencyTestsCommon(testCtx, testGroup, true);