Fix missing dependency on sparse binds
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / sparse_resources / vktSparseResourcesMipmapSparseResidency.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file  vktSparseResourcesMipmapSparseResidency.cpp
21  * \brief Sparse partially resident images with mipmaps tests
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktSparseResourcesMipmapSparseResidency.hpp"
25 #include "vktSparseResourcesTestsUtil.hpp"
26 #include "vktSparseResourcesBase.hpp"
27 #include "vktTestCaseUtil.hpp"
28
29 #include "vkDefs.hpp"
30 #include "vkRef.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"
41
42 #include "deUniquePtr.hpp"
43 #include "deStringUtil.hpp"
44 #include "tcuTextureUtil.hpp"
45
46 #include <string>
47 #include <vector>
48
49 using namespace vk;
50
51 namespace vkt
52 {
53 namespace sparse
54 {
55 namespace
56 {
57
58 class MipmapSparseResidencyCase : public TestCase
59 {
60 public:
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);
68
69         TestInstance*   createInstance                          (Context&                                       context) const;
70         virtual void    checkSupport                            (Context&                                       context) const;
71
72 private:
73         const bool                      m_useDeviceGroups;
74         const ImageType         m_imageType;
75         const tcu::UVec3        m_imageSize;
76         const VkFormat          m_format;
77 };
78
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)
90         , m_format                      (format)
91 {
92 }
93
94 void MipmapSparseResidencyCase::checkSupport (Context& context) const
95 {
96         const InstanceInterface&        instance                = context.getInstanceInterface();
97         const VkPhysicalDevice          physicalDevice  = context.getPhysicalDevice();
98
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");
102
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");
106
107         if (formatIsR64(m_format))
108          {
109                 context.requireDeviceFunctionality("VK_EXT_shader_image_atomic_int64");
110
111                 if (context.getShaderImageAtomicInt64FeaturesEXT().sparseImageInt64Atomics == VK_FALSE)
112                 {
113                         TCU_THROW(NotSupportedError, "sparseImageInt64Atomics is not supported for device");
114                 }
115         }
116 }
117
118 class MipmapSparseResidencyInstance : public SparseResourcesBaseInstance
119 {
120 public:
121         MipmapSparseResidencyInstance   (Context&                       context,
122                                                                          const ImageType        imageType,
123                                                                          const tcu::UVec3&      imageSize,
124                                                                          const VkFormat         format,
125                                                                          const bool                     useDeviceGroups);
126
127
128         tcu::TestStatus iterate                 (void);
129
130 private:
131         const bool                      m_useDeviceGroups;
132         const ImageType         m_imageType;
133         const tcu::UVec3        m_imageSize;
134         const VkFormat          m_format;
135 };
136
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)
146         , m_format                                              (format)
147 {
148 }
149
150 tcu::TestStatus MipmapSparseResidencyInstance::iterate (void)
151 {
152         const InstanceInterface&        instance                = m_context.getInstanceInterface();
153         {
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));
158
159                 createDeviceSupportingQueues(queueRequirements);
160         }
161
162         const VkPhysicalDevice          physicalDevice  = getPhysicalDevice();
163         VkImageCreateInfo                       imageSparseInfo;
164         std::vector<DeviceMemorySp>     deviceMemUniquePtrVec;
165
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);
170
171         // Go through all physical devices
172         for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
173         {
174                 const deUint32  firstDeviceID                   = physDevID;
175                 const deUint32  secondDeviceID                  = (firstDeviceID + 1) % m_numPhysicalDevices;
176
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;
192
193                 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
194                 {
195                         imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
196                 }
197
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");
201
202                 {
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)
211                         {
212                                 TCU_THROW(NotSupportedError, "Image format does not support sparse operations");
213                         }
214
215                         imageSparseInfo.mipLevels = getMipmapCount(m_format, formatDescription, imageFormatProperties, imageSparseInfo.extent);
216                 }
217
218                 // Create sparse image
219                 const Unique<VkImage>                                                   imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
220
221                 // Create sparse image memory bind semaphore
222                 const Unique<VkSemaphore>                                               imageMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
223
224                 std::vector<VkSparseImageMemoryRequirements>    sparseMemoryRequirements;
225
226                 {
227                         // Get sparse image general memory requirements
228                         const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
229
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");
233
234                         DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
235
236                         const deUint32                                                  memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), imageMemoryRequirements, MemoryRequirement::Any);
237
238                         if (memoryType == NO_MATCH_FOUND)
239                                 return tcu::TestStatus::fail("No matching memory type found");
240
241                         if (firstDeviceID != secondDeviceID)
242                         {
243                                 VkPeerMemoryFeatureFlags        peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
244                                 const deUint32                          heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
245                                 deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
246
247                                 if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) == 0) ||
248                                         ((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) == 0))
249                                 {
250                                         TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC and COPY_DST");
251                                 }
252                         }
253
254                         // Get sparse image sparse memory requirements
255                         sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
256                         DE_ASSERT(sparseMemoryRequirements.size() != 0);
257
258                         const deUint32 metadataAspectIndex      = getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_METADATA_BIT);
259
260                         std::vector<VkSparseImageMemoryBind>    imageResidencyMemoryBinds;
261                         std::vector<VkSparseMemoryBind>                 imageMipTailMemoryBinds;
262
263                         for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
264                         {
265                                 const VkImageAspectFlags                aspect                          = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
266                                 const deUint32                                  aspectIndex                     = getSparseAspectRequirementsIndex(sparseMemoryRequirements, aspect);
267
268                                 if (aspectIndex == NO_MATCH_FOUND)
269                                         TCU_THROW(NotSupportedError, "Not supported image aspect");
270
271                                 VkSparseImageMemoryRequirements aspectRequirements      = sparseMemoryRequirements[aspectIndex];
272
273                                 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
274
275                                 VkExtent3D                                              imageGranularity        = aspectRequirements.formatProperties.imageGranularity;
276
277                                 // Bind memory for each layer
278                                 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
279                                 {
280                                         for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
281                                         {
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 };
286
287                                                 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
288                                                         imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
289
290                                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
291
292                                                 imageResidencyMemoryBinds.push_back(imageMemoryBind);
293                                         }
294
295                                         if (!(aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
296                                         {
297                                                 const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
298                                                         aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
299
300                                                 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
301
302                                                 imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
303                                         }
304
305                                         // Metadata
306                                         if (metadataAspectIndex != NO_MATCH_FOUND)
307                                         {
308                                                 const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
309
310                                                 if (!(metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT))
311                                                 {
312                                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
313                                                                 metadataAspectRequirements.imageMipTailSize, memoryType,
314                                                                 metadataAspectRequirements.imageMipTailOffset + layerNdx * metadataAspectRequirements.imageMipTailStride,
315                                                                 VK_SPARSE_MEMORY_BIND_METADATA_BIT);
316
317                                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
318
319                                                         imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
320                                                 }
321                                         }
322                                 }
323
324                                 if ((aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
325                                 {
326                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
327                                                 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
328
329                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
330
331                                         imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
332                                 }
333                         }
334
335                         // Metadata
336                         if (metadataAspectIndex != NO_MATCH_FOUND)
337                         {
338                                 const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
339
340                                 if (metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
341                                 {
342                                         const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
343                                                 metadataAspectRequirements.imageMipTailSize, memoryType, metadataAspectRequirements.imageMipTailOffset,
344                                                 VK_SPARSE_MEMORY_BIND_METADATA_BIT);
345
346                                         deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
347
348                                         imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
349                                 }
350                         }
351
352                         const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
353                         {
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;
358                         };
359
360                         VkBindSparseInfo bindSparseInfo =
361                         {
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;
374                         };
375
376                         VkSparseImageMemoryBindInfo                     imageResidencyBindInfo;
377                         VkSparseImageOpaqueMemoryBindInfo       imageMipTailBindInfo;
378
379                         if (imageResidencyMemoryBinds.size() > 0)
380                         {
381                                 imageResidencyBindInfo.image            = *imageSparse;
382                                 imageResidencyBindInfo.bindCount        = static_cast<deUint32>(imageResidencyMemoryBinds.size());
383                                 imageResidencyBindInfo.pBinds           = imageResidencyMemoryBinds.data();
384
385                                 bindSparseInfo.imageBindCount           = 1u;
386                                 bindSparseInfo.pImageBinds                      = &imageResidencyBindInfo;
387                         }
388
389                         if (imageMipTailMemoryBinds.size() > 0)
390                         {
391                                 imageMipTailBindInfo.image                      = *imageSparse;
392                                 imageMipTailBindInfo.bindCount          = static_cast<deUint32>(imageMipTailMemoryBinds.size());
393                                 imageMipTailBindInfo.pBinds                     = imageMipTailMemoryBinds.data();
394
395                                 bindSparseInfo.imageOpaqueBindCount     = 1u;
396                                 bindSparseInfo.pImageOpaqueBinds        = &imageMipTailBindInfo;
397                         }
398
399                         // Submit sparse bind commands for execution
400                         VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
401                 }
402
403                 deUint32 imageSizeInBytes = 0;
404
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);
408
409                 std::vector <VkBufferImageCopy> bufferImageCopy(formatDescription.numPlanes*imageSparseInfo.mipLevels);
410                 {
411                         deUint32 bufferOffset = 0;
412
413                         for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
414                         {
415                                 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
416
417                                 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
418                                 {
419                                         bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx] =
420                                         {
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;
427                                         };
428                                         bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
429                                 }
430                         }
431                 }
432
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));
436
437                 // Start recording commands
438                 beginCommandBuffer(deviceInterface, *commandBuffer);
439
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));
443
444                 std::vector<deUint8> referenceData(imageSizeInBytes);
445
446                 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
447
448                 for (deUint32 valueNdx = 0; valueNdx < imageSizeInBytes; ++valueNdx)
449                 {
450                         referenceData[valueNdx] = static_cast<deUint8>((valueNdx % imageMemoryRequirements.alignment) + 1u);
451                 }
452
453                 {
454                         deMemcpy(inputBufferAlloc->getHostPtr(), referenceData.data(), imageSizeInBytes);
455                         flushAlloc(deviceInterface, getDevice(), *inputBufferAlloc);
456
457                         const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
458                         (
459                                 VK_ACCESS_HOST_WRITE_BIT,
460                                 VK_ACCESS_TRANSFER_READ_BIT,
461                                 *inputBuffer,
462                                 0u,
463                                 imageSizeInBytes
464                         );
465
466                         deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
467                 }
468
469                 {
470                         std::vector<VkImageMemoryBarrier> imageSparseTransferDstBarriers;
471
472                         for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
473                         {
474                                 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
475
476                                 imageSparseTransferDstBarriers.emplace_back ( makeImageMemoryBarrier
477                                 (
478                                         0u,
479                                         VK_ACCESS_TRANSFER_WRITE_BIT,
480                                         VK_IMAGE_LAYOUT_UNDEFINED,
481                                         VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
482                                         *imageSparse,
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
486                                 ));
487                         }
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());
489                 }
490
491                 deviceInterface.cmdCopyBufferToImage(*commandBuffer, *inputBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageCopy.size()), &bufferImageCopy[0]);
492
493                 {
494                         std::vector<VkImageMemoryBarrier> imageSparseTransferSrcBarriers;
495
496                         for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
497                         {
498                                 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
499
500                                 imageSparseTransferSrcBarriers.emplace_back(makeImageMemoryBarrier
501                                 (
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,
506                                         *imageSparse,
507                                         makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers)
508                                 ));
509                         }
510
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());
512                 }
513
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));
517
518                 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *outputBuffer, static_cast<deUint32>(bufferImageCopy.size()), bufferImageCopy.data());
519
520                 {
521                         const VkBufferMemoryBarrier outputBufferBarrier = makeBufferMemoryBarrier
522                         (
523                                 VK_ACCESS_TRANSFER_WRITE_BIT,
524                                 VK_ACCESS_HOST_READ_BIT,
525                                 *outputBuffer,
526                                 0u,
527                                 imageSizeInBytes
528                         );
529
530                         deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
531                 }
532
533                 // End recording commands
534                 endCommandBuffer(deviceInterface, *commandBuffer);
535
536                 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
537
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);
541
542                 // Retrieve data from buffer to host memory
543                 invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
544
545                 const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
546
547                 // Wait for sparse queue to become idle
548                 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
549
550                 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
551                 {
552                         for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
553                         {
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);
556
557                                 if (deMemCmp(outputData + bufferOffset, &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
558                                         return tcu::TestStatus::fail("Failed");
559                         }
560                 }
561         }
562         return tcu::TestStatus::pass("Passed");
563 }
564
565 TestInstance* MipmapSparseResidencyCase::createInstance (Context& context) const
566 {
567         return new MipmapSparseResidencyInstance(context, m_imageType, m_imageSize, m_format, m_useDeviceGroups);
568 }
569
570 } // anonymous ns
571
572 tcu::TestCaseGroup* createMipmapSparseResidencyTestsCommon (tcu::TestContext& testCtx, de::MovePtr<tcu::TestCaseGroup> testGroup, const bool useDeviceGroup = false)
573 {
574         const std::vector<TestImageParameters> imageParameters
575         {
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) }
581         };
582
583         for (size_t imageTypeNdx = 0; imageTypeNdx < imageParameters.size(); ++imageTypeNdx)
584         {
585                 const ImageType                                 imageType = imageParameters[imageTypeNdx].imageType;
586                 de::MovePtr<tcu::TestCaseGroup> imageTypeGroup(new tcu::TestCaseGroup(testCtx, getImageTypeName(imageType).c_str(), ""));
587
588                 for (size_t formatNdx = 0; formatNdx < imageParameters[imageTypeNdx].formats.size(); ++formatNdx)
589                 {
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(), ""));
593
594                         for (size_t imageSizeNdx = 0; imageSizeNdx < imageParameters[imageTypeNdx].imageSizes.size(); ++imageSizeNdx)
595                         {
596                                 const tcu::UVec3 imageSize = imageParameters[imageTypeNdx].imageSizes[imageSizeNdx];
597
598                                 // skip test for images with odd sizes for some YCbCr formats
599                                 if ((imageSize.x() % imageSizeAlignment.x()) != 0)
600                                         continue;
601                                 if ((imageSize.y() % imageSizeAlignment.y()) != 0)
602                                         continue;
603
604                                 std::ostringstream stream;
605                                 stream << imageSize.x() << "_" << imageSize.y() << "_" << imageSize.z();
606
607                                 formatGroup->addChild(new MipmapSparseResidencyCase(testCtx, stream.str(), "", imageType, imageSize, format, useDeviceGroup));
608                         }
609                         imageTypeGroup->addChild(formatGroup.release());
610                 }
611                 testGroup->addChild(imageTypeGroup.release());
612         }
613
614         return testGroup.release();
615 }
616
617 tcu::TestCaseGroup* createMipmapSparseResidencyTests (tcu::TestContext& testCtx)
618 {
619         de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "mipmap_sparse_residency", "Mipmap Sparse Residency"));
620         return createMipmapSparseResidencyTestsCommon(testCtx, testGroup);
621 }
622
623 tcu::TestCaseGroup* createDeviceGroupMipmapSparseResidencyTests (tcu::TestContext& testCtx)
624 {
625         de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "device_group_mipmap_sparse_residency", "Mipmap Sparse Residency"));
626         return createMipmapSparseResidencyTestsCommon(testCtx, testGroup, true);
627 }
628
629 } // sparse
630 } // vkt