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 vktSparseResourcesImageMemoryAliasing.cpp
21 * \brief Sparse image memory aliasing tests
22 *//*--------------------------------------------------------------------*/
23
24 #include "vktSparseResourcesImageMemoryAliasing.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 "vkRefUtil.hpp"
35 #include "vkMemUtil.hpp"
36 #include "vkBarrierUtil.hpp"
37 #include "vkQueryUtil.hpp"
38 #include "vkBuilderUtil.hpp"
39 #include "vkTypeUtil.hpp"
40 #include "vkCmdUtil.hpp"
41 #include "vkObjUtil.hpp"
42
43 #include "deStringUtil.hpp"
44 #include "deUniquePtr.hpp"
45 #include "deSharedPtr.hpp"
46
47 #include "tcuTexture.hpp"
48 #include "tcuTextureUtil.hpp"
49 #include "tcuTexVerifierUtil.hpp"
50
51 #include <deMath.h>
52 #include <string>
53 #include <vector>
54
55 using namespace vk;
56
57 namespace vkt
58 {
59 namespace sparse
60 {
61 namespace
62 {
63
64 const deUint32 MODULO_DIVISOR = 127;
65
getCoordStr(const ImageType imageType,const std::string & x,const std::string & y,const std::string & z)66 const std::string getCoordStr (const ImageType imageType,
67 const std::string& x,
68 const std::string& y,
69 const std::string& z)
70 {
71 switch (imageType)
72 {
73 case IMAGE_TYPE_1D:
74 case IMAGE_TYPE_BUFFER:
75 return x;
76
77 case IMAGE_TYPE_1D_ARRAY:
78 case IMAGE_TYPE_2D:
79 return "ivec2(" + x + "," + y + ")";
80
81 case IMAGE_TYPE_2D_ARRAY:
82 case IMAGE_TYPE_3D:
83 case IMAGE_TYPE_CUBE:
84 case IMAGE_TYPE_CUBE_ARRAY:
85 return "ivec3(" + x + "," + y + "," + z + ")";
86
87 default:
88 DE_FATAL("Unexpected image type");
89 return "";
90 }
91 }
92
93 class ImageSparseMemoryAliasingCase : public TestCase
94 {
95 public:
96 ImageSparseMemoryAliasingCase (tcu::TestContext& testCtx,
97 const std::string& name,
98 const ImageType imageType,
99 const tcu::UVec3& imageSize,
100 const VkFormat format,
101 const glu::GLSLVersion glslVersion,
102 const bool useDeviceGroups);
103
104 void initPrograms (SourceCollections& sourceCollections) const;
105 TestInstance* createInstance (Context& context) const;
106 virtual void checkSupport (Context& context) const;
107
108
109 private:
110 const bool m_useDeviceGroups;
111 const ImageType m_imageType;
112 const tcu::UVec3 m_imageSize;
113 const VkFormat m_format;
114 const glu::GLSLVersion m_glslVersion;
115 };
116
ImageSparseMemoryAliasingCase(tcu::TestContext & testCtx,const std::string & name,const ImageType imageType,const tcu::UVec3 & imageSize,const VkFormat format,const glu::GLSLVersion glslVersion,const bool useDeviceGroups)117 ImageSparseMemoryAliasingCase::ImageSparseMemoryAliasingCase (tcu::TestContext& testCtx,
118 const std::string& name,
119 const ImageType imageType,
120 const tcu::UVec3& imageSize,
121 const VkFormat format,
122 const glu::GLSLVersion glslVersion,
123 const bool useDeviceGroups)
124 : TestCase (testCtx, name)
125 , m_useDeviceGroups (useDeviceGroups)
126 , m_imageType (imageType)
127 , m_imageSize (imageSize)
128 , m_format (format)
129 , m_glslVersion (glslVersion)
130 {
131 }
132
checkSupport(Context & context) const133 void ImageSparseMemoryAliasingCase::checkSupport (Context& context) const
134 {
135 const InstanceInterface& instance = context.getInstanceInterface();
136 const VkPhysicalDevice physicalDevice = context.getPhysicalDevice();
137
138 context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_SPARSE_RESIDENCY_ALIASED);
139
140 // Check if image size does not exceed device limits
141 if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
142 TCU_THROW(NotSupportedError, "Image size not supported for device");
143
144 // Check if device supports sparse operations for image type
145 if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
146 TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
147
148 if (formatIsR64(m_format))
149 {
150 context.requireDeviceFunctionality("VK_EXT_shader_image_atomic_int64");
151
152 if (context.getShaderImageAtomicInt64FeaturesEXT().shaderImageInt64Atomics == VK_FALSE)
153 {
154 TCU_THROW(NotSupportedError, "shaderImageInt64Atomics is not supported");
155 }
156
157 if (context.getShaderImageAtomicInt64FeaturesEXT().sparseImageInt64Atomics == VK_FALSE)
158 {
159 TCU_THROW(NotSupportedError, "sparseImageInt64Atomics is not supported for device");
160 }
161 }
162 }
163
164 class ImageSparseMemoryAliasingInstance : public SparseResourcesBaseInstance
165 {
166 public:
167 ImageSparseMemoryAliasingInstance (Context& context,
168 const ImageType imageType,
169 const tcu::UVec3& imageSize,
170 const VkFormat format,
171 const bool useDeviceGroups);
172
173 tcu::TestStatus iterate (void);
174
175 private:
176 const bool m_useDeviceGroups;
177 const ImageType m_imageType;
178 const tcu::UVec3 m_imageSize;
179 const VkFormat m_format;
180 };
181
ImageSparseMemoryAliasingInstance(Context & context,const ImageType imageType,const tcu::UVec3 & imageSize,const VkFormat format,const bool useDeviceGroups)182 ImageSparseMemoryAliasingInstance::ImageSparseMemoryAliasingInstance (Context& context,
183 const ImageType imageType,
184 const tcu::UVec3& imageSize,
185 const VkFormat format,
186 const bool useDeviceGroups)
187 : SparseResourcesBaseInstance (context, useDeviceGroups)
188 , m_useDeviceGroups (useDeviceGroups)
189 , m_imageType (imageType)
190 , m_imageSize (imageSize)
191 , m_format (format)
192 {
193 }
194
iterate(void)195 tcu::TestStatus ImageSparseMemoryAliasingInstance::iterate (void)
196 {
197 const float epsilon = 1e-5f;
198 const InstanceInterface& instance = m_context.getInstanceInterface();
199
200 {
201 // Create logical device supporting both sparse and compute queues
202 QueueRequirementsVec queueRequirements;
203 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
204 queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
205
206 createDeviceSupportingQueues(queueRequirements, formatIsR64(m_format));
207 }
208
209 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
210 const tcu::UVec3 maxWorkGroupSize = tcu::UVec3(128u, 128u, 64u);
211 const tcu::UVec3 maxWorkGroupCount = tcu::UVec3(65535u, 65535u, 65535u);
212 const deUint32 maxWorkGroupInvocations = 128u;
213 VkImageCreateInfo imageSparseInfo;
214 std::vector<DeviceMemorySp> deviceMemUniquePtrVec;
215
216 //vsk getting queues should be outside the loop
217 //see these in all image files
218
219 const DeviceInterface& deviceInterface = getDeviceInterface();
220 const Queue& sparseQueue = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
221 const Queue& computeQueue = getQueue(VK_QUEUE_COMPUTE_BIT, 0);
222 const PlanarFormatDescription formatDescription = getPlanarFormatDescription(m_format);
223
224 // Go through all physical devices
225 for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
226 {
227 const deUint32 firstDeviceID = physDevID;
228 const deUint32 secondDeviceID = (firstDeviceID + 1) % m_numPhysicalDevices;
229
230 imageSparseInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
231 imageSparseInfo.pNext = DE_NULL;
232 imageSparseInfo.flags = VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT |
233 VK_IMAGE_CREATE_SPARSE_ALIASED_BIT |
234 VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
235 imageSparseInfo.imageType = mapImageType(m_imageType);
236 imageSparseInfo.format = m_format;
237 imageSparseInfo.extent = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
238 imageSparseInfo.arrayLayers = getNumLayers(m_imageType, m_imageSize);
239 imageSparseInfo.samples = VK_SAMPLE_COUNT_1_BIT;
240 imageSparseInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
241 imageSparseInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
242 imageSparseInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT |
243 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
244 VK_IMAGE_USAGE_STORAGE_BIT;
245 imageSparseInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
246 imageSparseInfo.queueFamilyIndexCount = 0u;
247 imageSparseInfo.pQueueFamilyIndices = DE_NULL;
248
249 if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
250 imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
251
252 // Check if device supports sparse operations for image format
253 if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
254 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
255
256 {
257 // Assign maximum allowed mipmap levels to image
258 VkImageFormatProperties imageFormatProperties;
259 if (instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
260 imageSparseInfo.format,
261 imageSparseInfo.imageType,
262 imageSparseInfo.tiling,
263 imageSparseInfo.usage,
264 imageSparseInfo.flags,
265 &imageFormatProperties) == VK_ERROR_FORMAT_NOT_SUPPORTED)
266 {
267 TCU_THROW(NotSupportedError, "Image format does not support sparse operations");
268 }
269
270 imageSparseInfo.mipLevels = getMipmapCount(m_format, formatDescription, imageFormatProperties, imageSparseInfo.extent);
271 }
272
273 // Create sparse image
274 const Unique<VkImage> imageRead(createImage(deviceInterface, getDevice(), &imageSparseInfo));
275 const Unique<VkImage> imageWrite(createImage(deviceInterface, getDevice(), &imageSparseInfo));
276
277 // Create semaphores to synchronize sparse binding operations with other operations on the sparse images
278 const Unique<VkSemaphore> memoryBindSemaphoreTransfer(createSemaphore(deviceInterface, getDevice()));
279 const Unique<VkSemaphore> memoryBindSemaphoreCompute(createSemaphore(deviceInterface, getDevice()));
280
281 const VkSemaphore imageMemoryBindSemaphores[] = { memoryBindSemaphoreTransfer.get(), memoryBindSemaphoreCompute.get() };
282
283 std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements;
284
285 {
286 // Get sparse image general memory requirements
287 const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageRead);
288
289 // Check if required image memory size does not exceed device limits
290 if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, getPhysicalDevice(secondDeviceID)).limits.sparseAddressSpaceSize)
291 TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
292
293 DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
294
295 const deUint32 memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), imageMemoryRequirements, MemoryRequirement::Any);
296
297 if (memoryType == NO_MATCH_FOUND)
298 return tcu::TestStatus::fail("No matching memory type found");
299
300 if (firstDeviceID != secondDeviceID)
301 {
302 VkPeerMemoryFeatureFlags peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
303 const deUint32 heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
304 deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
305
306 if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) == 0) ||
307 ((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) == 0) ||
308 ((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT) == 0))
309 {
310 TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC, COPY_DST, and GENERIC_DST");
311 }
312 }
313
314 // Get sparse image sparse memory requirements
315 sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageRead);
316
317 DE_ASSERT(sparseMemoryRequirements.size() != 0);
318
319 std::vector<VkSparseImageMemoryBind> imageResidencyMemoryBinds;
320 std::vector<VkSparseMemoryBind> imageReadMipTailBinds;
321 std::vector<VkSparseMemoryBind> imageWriteMipTailBinds;
322
323 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
324 {
325 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
326 const deUint32 aspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, aspect);
327
328 if (aspectIndex == NO_MATCH_FOUND)
329 TCU_THROW(NotSupportedError, "Not supported image aspect");
330
331 VkSparseImageMemoryRequirements aspectRequirements = sparseMemoryRequirements[aspectIndex];
332
333 DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
334
335 VkExtent3D imageGranularity = aspectRequirements.formatProperties.imageGranularity;
336
337 // Bind memory for each layer
338 for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
339 {
340 for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
341 {
342 const VkExtent3D mipExtent = getPlaneExtent(formatDescription, imageSparseInfo.extent, planeNdx, mipLevelNdx);
343 const tcu::UVec3 sparseBlocks = alignedDivide(mipExtent, imageGranularity);
344 const deUint32 numSparseBlocks = sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
345 const VkImageSubresource subresource = { aspect, mipLevelNdx, layerNdx };
346
347 const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
348 imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
349
350 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
351
352 imageResidencyMemoryBinds.push_back(imageMemoryBind);
353 }
354
355 if (!(aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
356 {
357 const VkSparseMemoryBind imageReadMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
358 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
359
360 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageReadMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
361
362 imageReadMipTailBinds.push_back(imageReadMipTailMemoryBind);
363
364 const VkSparseMemoryBind imageWriteMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
365 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
366
367 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageWriteMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
368
369 imageWriteMipTailBinds.push_back(imageWriteMipTailMemoryBind);
370 }
371 }
372
373 if ((aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
374 {
375 const VkSparseMemoryBind imageReadMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
376 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
377
378 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageReadMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
379
380 imageReadMipTailBinds.push_back(imageReadMipTailMemoryBind);
381
382 const VkSparseMemoryBind imageWriteMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
383 aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
384
385 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageWriteMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
386
387 imageWriteMipTailBinds.push_back(imageWriteMipTailMemoryBind);
388 }
389 }
390
391 const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
392 {
393 VK_STRUCTURE_TYPE_DEVICE_GROUP_BIND_SPARSE_INFO, //VkStructureType sType;
394 DE_NULL, //const void* pNext;
395 firstDeviceID, //deUint32 resourceDeviceIndex;
396 secondDeviceID, //deUint32 memoryDeviceIndex;
397 };
398
399 VkBindSparseInfo bindSparseInfo =
400 {
401 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
402 m_useDeviceGroups ? &devGroupBindSparseInfo : DE_NULL, //const void* pNext;
403 0u, //deUint32 waitSemaphoreCount;
404 DE_NULL, //const VkSemaphore* pWaitSemaphores;
405 0u, //deUint32 bufferBindCount;
406 DE_NULL, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
407 0u, //deUint32 imageOpaqueBindCount;
408 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
409 0u, //deUint32 imageBindCount;
410 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
411 2u, //deUint32 signalSemaphoreCount;
412 imageMemoryBindSemaphores //const VkSemaphore* pSignalSemaphores;
413 };
414
415 VkSparseImageMemoryBindInfo imageResidencyBindInfo[2];
416 VkSparseImageOpaqueMemoryBindInfo imageMipTailBindInfo[2];
417
418 if (imageResidencyMemoryBinds.size() > 0)
419 {
420 imageResidencyBindInfo[0].image = *imageRead;
421 imageResidencyBindInfo[0].bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
422 imageResidencyBindInfo[0].pBinds = imageResidencyMemoryBinds.data();
423
424 imageResidencyBindInfo[1].image = *imageWrite;
425 imageResidencyBindInfo[1].bindCount = static_cast<deUint32>(imageResidencyMemoryBinds.size());
426 imageResidencyBindInfo[1].pBinds = imageResidencyMemoryBinds.data();
427
428 bindSparseInfo.imageBindCount = 2u;
429 bindSparseInfo.pImageBinds = imageResidencyBindInfo;
430 }
431
432 if (imageReadMipTailBinds.size() > 0)
433 {
434 imageMipTailBindInfo[0].image = *imageRead;
435 imageMipTailBindInfo[0].bindCount = static_cast<deUint32>(imageReadMipTailBinds.size());
436 imageMipTailBindInfo[0].pBinds = imageReadMipTailBinds.data();
437
438 imageMipTailBindInfo[1].image = *imageWrite;
439 imageMipTailBindInfo[1].bindCount = static_cast<deUint32>(imageWriteMipTailBinds.size());
440 imageMipTailBindInfo[1].pBinds = imageWriteMipTailBinds.data();
441
442 bindSparseInfo.imageOpaqueBindCount = 2u;
443 bindSparseInfo.pImageOpaqueBinds = imageMipTailBindInfo;
444 }
445
446 // Submit sparse bind commands for execution
447 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
448 }
449
450 deUint32 imageSizeInBytes = 0;
451 std::vector<std::vector<deUint32>> planeOffsets( imageSparseInfo.mipLevels );
452 std::vector<std::vector<deUint32>> planeRowPitches( imageSparseInfo.mipLevels );
453
454 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
455 {
456 planeOffsets[mipmapNdx].resize(formatDescription.numPlanes, 0);
457 planeRowPitches[mipmapNdx].resize(formatDescription.numPlanes, 0);
458 }
459
460 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
461 {
462 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
463 {
464 const tcu::UVec3 gridSize = getShaderGridSize(m_imageType, m_imageSize, mipmapNdx);
465 planeOffsets[mipmapNdx][planeNdx] = imageSizeInBytes;
466 const deUint32 planeW = gridSize.x() / (formatDescription.blockWidth * formatDescription.planes[planeNdx].widthDivisor);
467 planeRowPitches[mipmapNdx][planeNdx] = formatDescription.planes[planeNdx].elementSizeBytes * planeW;
468 imageSizeInBytes += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
469 }
470 }
471
472 std::vector <VkBufferImageCopy> bufferImageCopy(formatDescription.numPlanes * imageSparseInfo.mipLevels);
473 {
474 deUint32 bufferOffset = 0;
475
476 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
477 {
478 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
479
480 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
481 {
482 bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx] =
483 {
484 bufferOffset, // VkDeviceSize bufferOffset;
485 0u, // deUint32 bufferRowLength;
486 0u, // deUint32 bufferImageHeight;
487 makeImageSubresourceLayers(aspect, mipmapNdx, 0u, imageSparseInfo.arrayLayers), // VkImageSubresourceLayers imageSubresource;
488 makeOffset3D(0, 0, 0), // VkOffset3D imageOffset;
489 vk::getPlaneExtent(formatDescription, imageSparseInfo.extent, planeNdx, mipmapNdx) // VkExtent3D imageExtent;
490 };
491 bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
492 }
493 }
494 }
495
496 // Create command buffer for compute and transfer operations
497 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
498 const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
499
500 // Start recording commands
501 beginCommandBuffer(deviceInterface, *commandBuffer);
502
503 const VkBufferCreateInfo inputBufferCreateInfo = makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
504 const Unique<VkBuffer> inputBuffer (createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
505 const de::UniquePtr<Allocation> inputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
506
507 std::vector<deUint8> referenceData(imageSizeInBytes);
508
509 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
510 for (deUint32 mipmapNdx = 0u; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
511 {
512 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
513 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx].bufferOffset);
514
515 deMemset(&referenceData[bufferOffset], mipmapNdx + 1u, mipLevelSizeInBytes);
516 }
517
518 deMemcpy(inputBufferAlloc->getHostPtr(), referenceData.data(), imageSizeInBytes);
519
520 flushAlloc(deviceInterface, getDevice(), *inputBufferAlloc);
521
522 {
523 const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
524 (
525 VK_ACCESS_HOST_WRITE_BIT,
526 VK_ACCESS_TRANSFER_READ_BIT,
527 *inputBuffer,
528 0u,
529 imageSizeInBytes
530 );
531
532 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
533 }
534
535 {
536 std::vector<VkImageMemoryBarrier> imageSparseTransferDstBarriers;
537
538 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
539 {
540 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
541
542 imageSparseTransferDstBarriers.emplace_back(makeImageMemoryBarrier
543 (
544 0u,
545 VK_ACCESS_TRANSFER_WRITE_BIT,
546 VK_IMAGE_LAYOUT_UNDEFINED,
547 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
548 *imageRead,
549 makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers),
550 sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
551 sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? computeQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED
552 ));
553 }
554
555 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());
556 }
557
558 deviceInterface.cmdCopyBufferToImage(*commandBuffer, *inputBuffer, *imageRead, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageCopy.size()), bufferImageCopy.data());
559
560 {
561 std::vector<VkImageMemoryBarrier> imageSparseTransferSrcBarriers;
562
563 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
564 {
565 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
566
567 imageSparseTransferSrcBarriers.emplace_back(makeImageMemoryBarrier
568 (
569 VK_ACCESS_TRANSFER_WRITE_BIT,
570 VK_ACCESS_TRANSFER_READ_BIT,
571 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
572 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
573 *imageRead,
574 makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers)
575 ));
576 }
577
578 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());
579 }
580
581 {
582 std::vector<VkImageMemoryBarrier> imageSparseShaderStorageBarriers;
583
584 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
585 {
586 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
587
588 imageSparseShaderStorageBarriers.emplace_back(makeImageMemoryBarrier
589 (
590 0u,
591 VK_ACCESS_SHADER_WRITE_BIT,
592 VK_IMAGE_LAYOUT_UNDEFINED,
593 VK_IMAGE_LAYOUT_GENERAL,
594 *imageWrite,
595 makeImageSubresourceRange(aspect, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers)
596 ));
597 }
598
599 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, static_cast<deUint32>(imageSparseShaderStorageBarriers.size()), imageSparseShaderStorageBarriers.data());
600 }
601
602 // Create descriptor set layout
603 const Unique<VkDescriptorSetLayout> descriptorSetLayout(
604 DescriptorSetLayoutBuilder()
605 .addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
606 .build(deviceInterface, getDevice()));
607
608 Unique<VkPipelineLayout> pipelineLayout(makePipelineLayout(deviceInterface, getDevice(), *descriptorSetLayout));
609
610 Unique<VkDescriptorPool> descriptorPool(
611 DescriptorPoolBuilder()
612 .addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, imageSparseInfo.mipLevels)
613 .build(deviceInterface, getDevice(), VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, imageSparseInfo.mipLevels));
614
615 typedef de::SharedPtr< Unique<VkImageView> > SharedVkImageView;
616 std::vector<SharedVkImageView> imageViews;
617 imageViews.resize(imageSparseInfo.mipLevels);
618
619 typedef de::SharedPtr< Unique<VkDescriptorSet> > SharedVkDescriptorSet;
620 std::vector<SharedVkDescriptorSet> descriptorSets;
621 descriptorSets.resize(imageSparseInfo.mipLevels);
622
623 typedef de::SharedPtr< Unique<VkPipeline> > SharedVkPipeline;
624 std::vector<SharedVkPipeline> computePipelines;
625 computePipelines.resize(imageSparseInfo.mipLevels);
626
627 for (deUint32 mipLevelNdx = 0u; mipLevelNdx < imageSparseInfo.mipLevels; ++mipLevelNdx)
628 {
629 std::ostringstream name;
630 name << "comp" << mipLevelNdx;
631
632 // Create and bind compute pipeline
633 Unique<VkShaderModule> shaderModule(createShaderModule(deviceInterface, getDevice(), m_context.getBinaryCollection().get(name.str()), DE_NULL));
634
635 computePipelines[mipLevelNdx] = makeVkSharedPtr(makeComputePipeline(deviceInterface, getDevice(), *pipelineLayout, *shaderModule));
636 VkPipeline computePipeline = **computePipelines[mipLevelNdx];
637
638 deviceInterface.cmdBindPipeline(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipeline);
639
640 // Create and bind descriptor set
641 descriptorSets[mipLevelNdx] = makeVkSharedPtr(makeDescriptorSet(deviceInterface, getDevice(), *descriptorPool, *descriptorSetLayout));
642 VkDescriptorSet descriptorSet = **descriptorSets[mipLevelNdx];
643
644 // Select which mipmap level to bind
645 const VkImageSubresourceRange subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, mipLevelNdx, 1u, 0u, imageSparseInfo.arrayLayers);
646
647 imageViews[mipLevelNdx] = makeVkSharedPtr(makeImageView(deviceInterface, getDevice(), *imageWrite, mapImageViewType(m_imageType), imageSparseInfo.format, subresourceRange));
648 VkImageView imageView = **imageViews[mipLevelNdx];
649
650 const VkDescriptorImageInfo descriptorImageSparseInfo = makeDescriptorImageInfo(DE_NULL, imageView, VK_IMAGE_LAYOUT_GENERAL);
651
652 DescriptorSetUpdateBuilder()
653 .writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorImageSparseInfo)
654 .update(deviceInterface, getDevice());
655
656 deviceInterface.cmdBindDescriptorSets(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet, 0u, DE_NULL);
657
658 const tcu::UVec3 gridSize = getShaderGridSize(m_imageType, m_imageSize, mipLevelNdx);
659 const deUint32 xWorkGroupSize = std::min(std::min(gridSize.x(), maxWorkGroupSize.x()), maxWorkGroupInvocations);
660 const deUint32 yWorkGroupSize = std::min(std::min(gridSize.y(), maxWorkGroupSize.y()), maxWorkGroupInvocations / xWorkGroupSize);
661 const deUint32 zWorkGroupSize = std::min(std::min(gridSize.z(), maxWorkGroupSize.z()), maxWorkGroupInvocations / (xWorkGroupSize * yWorkGroupSize));
662
663 const deUint32 xWorkGroupCount = gridSize.x() / xWorkGroupSize + (gridSize.x() % xWorkGroupSize ? 1u : 0u);
664 const deUint32 yWorkGroupCount = gridSize.y() / yWorkGroupSize + (gridSize.y() % yWorkGroupSize ? 1u : 0u);
665 const deUint32 zWorkGroupCount = gridSize.z() / zWorkGroupSize + (gridSize.z() % zWorkGroupSize ? 1u : 0u);
666
667 if (maxWorkGroupCount.x() < xWorkGroupCount ||
668 maxWorkGroupCount.y() < yWorkGroupCount ||
669 maxWorkGroupCount.z() < zWorkGroupCount)
670 {
671 TCU_THROW(NotSupportedError, "Image size is not supported");
672 }
673
674 deviceInterface.cmdDispatch(*commandBuffer, xWorkGroupCount, yWorkGroupCount, zWorkGroupCount);
675 }
676
677 {
678 const VkMemoryBarrier memoryBarrier = makeMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT);
679
680 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 1u, &memoryBarrier, 0u, DE_NULL, 0u, DE_NULL);
681 }
682
683 const VkBufferCreateInfo outputBufferCreateInfo = makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
684 const Unique<VkBuffer> outputBuffer (createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
685 const de::UniquePtr<Allocation> outputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
686
687 deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageRead, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *outputBuffer, static_cast<deUint32>(bufferImageCopy.size()), bufferImageCopy.data());
688
689 {
690 const VkBufferMemoryBarrier outputBufferBarrier = makeBufferMemoryBarrier
691 (
692 VK_ACCESS_TRANSFER_WRITE_BIT,
693 VK_ACCESS_HOST_READ_BIT,
694 *outputBuffer,
695 0u,
696 imageSizeInBytes
697 );
698
699 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
700 }
701
702 // End recording commands
703 endCommandBuffer(deviceInterface, *commandBuffer);
704
705 const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT };
706
707 // Submit commands for execution and wait for completion
708 submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 2u, imageMemoryBindSemaphores, stageBits,
709 0, DE_NULL, m_useDeviceGroups, firstDeviceID);
710
711 // Retrieve data from buffer to host memory
712 invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
713
714 deUint8* outputData = static_cast<deUint8*>(outputBufferAlloc->getHostPtr());
715
716 std::vector<std::vector<void*>> planePointers(imageSparseInfo.mipLevels);
717
718 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
719 planePointers[mipmapNdx].resize(formatDescription.numPlanes);
720
721 for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
722 for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
723 planePointers[mipmapNdx][planeNdx] = outputData + static_cast<size_t>(planeOffsets[mipmapNdx][planeNdx]);
724
725 // Wait for sparse queue to become idle
726 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
727
728 for (deUint32 channelNdx = 0; channelNdx < 4; ++channelNdx)
729 {
730 if (!formatDescription.hasChannelNdx(channelNdx))
731 continue;
732
733 deUint32 planeNdx = formatDescription.channels[channelNdx].planeNdx;
734 const VkImageAspectFlags aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
735 const deUint32 aspectIndex = getSparseAspectRequirementsIndex(sparseMemoryRequirements, aspect);
736
737 if (aspectIndex == NO_MATCH_FOUND)
738 TCU_THROW(NotSupportedError, "Not supported image aspect");
739
740 VkSparseImageMemoryRequirements aspectRequirements = sparseMemoryRequirements[aspectIndex];
741
742 for (deUint32 mipmapNdx = 0; mipmapNdx < aspectRequirements.imageMipTailFirstLod; ++mipmapNdx)
743 {
744 const tcu::UVec3 gridSize = getShaderGridSize(m_imageType, m_imageSize, mipmapNdx);
745 const tcu::ConstPixelBufferAccess pixelBuffer = vk::getChannelAccess(formatDescription, gridSize, planeRowPitches[mipmapNdx].data(), (const void* const*)planePointers[mipmapNdx].data(), channelNdx);
746 tcu::IVec3 pixelDivider = pixelBuffer.getDivider();
747
748 for (deUint32 offsetZ = 0u; offsetZ < gridSize.z(); ++offsetZ)
749 for (deUint32 offsetY = 0u; offsetY < gridSize.y(); ++offsetY)
750 for (deUint32 offsetX = 0u; offsetX < gridSize.x(); ++offsetX)
751 {
752 const deUint32 index = offsetX + gridSize.x() * offsetY + gridSize.x() * gridSize.y() * offsetZ;
753 deUint32 iReferenceValue;
754 float fReferenceValue;
755 float acceptableError = epsilon;
756
757 switch (channelNdx)
758 {
759 case 0:
760 case 1:
761 case 2:
762 iReferenceValue = index % MODULO_DIVISOR;
763 fReferenceValue = static_cast<float>(iReferenceValue) / static_cast<float>(MODULO_DIVISOR);
764 break;
765 case 3:
766 iReferenceValue = 1u;
767 fReferenceValue = 1.f;
768 break;
769 default: DE_FATAL("Unexpected channel index"); break;
770 }
771
772 switch (formatDescription.channels[channelNdx].type)
773 {
774 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
775 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
776 {
777 const tcu::UVec4 outputValue = pixelBuffer.getPixelUint(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), offsetZ * pixelDivider.z());
778
779 if (outputValue.x() != iReferenceValue)
780 return tcu::TestStatus::fail("Failed");
781
782 break;
783 }
784 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
785 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
786 {
787 float fixedPointError = tcu::TexVerifierUtil::computeFixedPointError(formatDescription.channels[channelNdx].sizeBits);
788 acceptableError += fixedPointError;
789 const tcu::Vec4 outputValue = pixelBuffer.getPixel(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), offsetZ * pixelDivider.z());
790
791 if (deAbs(outputValue.x() - fReferenceValue) > acceptableError)
792 return tcu::TestStatus::fail("Failed");
793
794 break;
795 }
796 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
797 {
798 const tcu::Vec4 outputValue = pixelBuffer.getPixel(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), offsetZ * pixelDivider.z());
799
800 if (deAbs(outputValue.x() - fReferenceValue) > acceptableError)
801 return tcu::TestStatus::fail("Failed");
802
803 break;
804 }
805 default: DE_FATAL("Unexpected channel type"); break;
806 }
807 }
808 }
809
810 for (deUint32 mipmapNdx = aspectRequirements.imageMipTailFirstLod; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
811 {
812 const deUint32 mipLevelSizeInBytes = getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, formatDescription, planeNdx, mipmapNdx);
813 const deUint32 bufferOffset = static_cast<deUint32>(bufferImageCopy[planeNdx*imageSparseInfo.mipLevels + mipmapNdx].bufferOffset);
814
815 if (deMemCmp(outputData + bufferOffset, &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
816 return tcu::TestStatus::fail("Failed");
817 }
818 }
819 }
820
821 return tcu::TestStatus::pass("Passed");
822 }
823
initPrograms(SourceCollections & sourceCollections) const824 void ImageSparseMemoryAliasingCase::initPrograms(SourceCollections& sourceCollections) const
825 {
826 const char* const versionDecl = glu::getGLSLVersionDeclaration(m_glslVersion);
827 const PlanarFormatDescription formatDescription = getPlanarFormatDescription(m_format);
828 const std::string imageTypeStr = getShaderImageType(formatDescription, m_imageType);
829 const std::string formatQualifierStr = getShaderImageFormatQualifier(m_format);
830 const std::string formatDataStr = getShaderImageDataType(formatDescription);
831 const deUint32 maxWorkGroupInvocations = 128u;
832 const tcu::UVec3 maxWorkGroupSize = tcu::UVec3(128u, 128u, 64u);
833 VkExtent3D layerExtent = makeExtent3D(getLayerSize(m_imageType, m_imageSize));
834 VkImageFormatProperties imageFormatProperties;
835 imageFormatProperties.maxMipLevels = 20;
836 const deUint32 mipLevels = getMipmapCount(m_format, formatDescription, imageFormatProperties, layerExtent);
837
838 std::ostringstream formatValueStr;
839 switch (formatDescription.channels[0].type)
840 {
841 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
842 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
843 formatValueStr << "( index % " << MODULO_DIVISOR << ", index % " << MODULO_DIVISOR << ", index % " << MODULO_DIVISOR << ", 1)";
844 break;
845 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
846 case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
847 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
848 formatValueStr << "( float( index % " << MODULO_DIVISOR << ") / " << MODULO_DIVISOR << ".0, float( index % " << MODULO_DIVISOR << ") / " << MODULO_DIVISOR << ".0, float( index % " << MODULO_DIVISOR << ") / " << MODULO_DIVISOR << ".0, 1.0)";
849 break;
850 default: DE_FATAL("Unexpected channel type"); break;
851 }
852
853
854 for (deUint32 mipLevelNdx = 0; mipLevelNdx < mipLevels; ++mipLevelNdx)
855 {
856 // Create compute program
857 const tcu::UVec3 gridSize = getShaderGridSize(m_imageType, m_imageSize, mipLevelNdx);
858 const deUint32 xWorkGroupSize = std::min(std::min(gridSize.x(), maxWorkGroupSize.x()), maxWorkGroupInvocations);
859 const deUint32 yWorkGroupSize = std::min(std::min(gridSize.y(), maxWorkGroupSize.y()), maxWorkGroupInvocations / xWorkGroupSize);
860 const deUint32 zWorkGroupSize = std::min(std::min(gridSize.z(), maxWorkGroupSize.z()), maxWorkGroupInvocations / (xWorkGroupSize * yWorkGroupSize));
861
862 std::ostringstream src;
863
864 src << versionDecl << "\n";
865 if (formatIsR64(m_format))
866 {
867 src << "#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require\n"
868 << "#extension GL_EXT_shader_image_int64 : require\n";
869 }
870 src << "layout (local_size_x = " << xWorkGroupSize << ", local_size_y = " << yWorkGroupSize << ", local_size_z = " << zWorkGroupSize << ") in; \n"
871 << "layout (binding = 0, " << formatQualifierStr << ") writeonly uniform highp " << imageTypeStr << " u_image;\n"
872 << "void main (void)\n"
873 << "{\n"
874 << " if( gl_GlobalInvocationID.x < " << gridSize.x() << " ) \n"
875 << " if( gl_GlobalInvocationID.y < " << gridSize.y() << " ) \n"
876 << " if( gl_GlobalInvocationID.z < " << gridSize.z() << " ) \n"
877 << " {\n"
878 << " int index = int( gl_GlobalInvocationID.x + "<< gridSize.x() << " * gl_GlobalInvocationID.y + " << gridSize.x() << " * " << gridSize.y() << " * gl_GlobalInvocationID.z );\n"
879 << " imageStore(u_image, " << getCoordStr(m_imageType, "gl_GlobalInvocationID.x", "gl_GlobalInvocationID.y", "gl_GlobalInvocationID.z") << ","
880 << formatDataStr << formatValueStr.str() <<"); \n"
881 << " }\n"
882 << "}\n";
883
884 std::ostringstream name;
885 name << "comp" << mipLevelNdx;
886 sourceCollections.glslSources.add(name.str()) << glu::ComputeSource(src.str());
887 }
888 }
889
createInstance(Context & context) const890 TestInstance* ImageSparseMemoryAliasingCase::createInstance (Context& context) const
891 {
892 return new ImageSparseMemoryAliasingInstance(context, m_imageType, m_imageSize, m_format, m_useDeviceGroups);
893 }
894
895 } // anonymous ns
896
createImageSparseMemoryAliasingTestsCommon(tcu::TestContext & testCtx,de::MovePtr<tcu::TestCaseGroup> testGroup,const bool useDeviceGroup=false)897 tcu::TestCaseGroup* createImageSparseMemoryAliasingTestsCommon(tcu::TestContext& testCtx, de::MovePtr<tcu::TestCaseGroup> testGroup, const bool useDeviceGroup = false)
898 {
899
900 const std::vector<TestImageParameters> imageParameters
901 {
902 { IMAGE_TYPE_2D, { tcu::UVec3(512u, 256u, 1u), tcu::UVec3(128u, 128u, 1u), tcu::UVec3(503u, 137u, 1u), tcu::UVec3(11u, 37u, 1u) }, getTestFormats(IMAGE_TYPE_2D) },
903 { IMAGE_TYPE_2D_ARRAY, { tcu::UVec3(512u, 256u, 6u), tcu::UVec3(128u, 128u, 8u), tcu::UVec3(503u, 137u, 3u), tcu::UVec3(11u, 37u, 3u) }, getTestFormats(IMAGE_TYPE_2D_ARRAY) },
904 { IMAGE_TYPE_CUBE, { tcu::UVec3(256u, 256u, 1u), tcu::UVec3(128u, 128u, 1u), tcu::UVec3(137u, 137u, 1u), tcu::UVec3(11u, 11u, 1u) }, getTestFormats(IMAGE_TYPE_CUBE) },
905 { IMAGE_TYPE_CUBE_ARRAY,{ tcu::UVec3(256u, 256u, 6u), tcu::UVec3(128u, 128u, 8u), tcu::UVec3(137u, 137u, 3u), tcu::UVec3(11u, 11u, 3u) }, getTestFormats(IMAGE_TYPE_CUBE_ARRAY) },
906 { IMAGE_TYPE_3D, { tcu::UVec3(256u, 256u, 16u), tcu::UVec3(128u, 128u, 8u), tcu::UVec3(503u, 137u, 3u), tcu::UVec3(11u, 37u, 3u) }, getTestFormats(IMAGE_TYPE_3D) }
907 };
908
909 for (size_t imageTypeNdx = 0; imageTypeNdx < imageParameters.size(); ++imageTypeNdx)
910 {
911 const ImageType imageType = imageParameters[imageTypeNdx].imageType;
912 de::MovePtr<tcu::TestCaseGroup> imageTypeGroup(new tcu::TestCaseGroup(testCtx, getImageTypeName(imageType).c_str()));
913
914 for (size_t formatNdx = 0; formatNdx < imageParameters[imageTypeNdx].formats.size(); ++formatNdx)
915 {
916 VkFormat format = imageParameters[imageTypeNdx].formats[formatNdx].format;
917 tcu::UVec3 imageSizeAlignment = getImageSizeAlignment(format);
918 de::MovePtr<tcu::TestCaseGroup> formatGroup (new tcu::TestCaseGroup(testCtx, getImageFormatID(format).c_str()));
919
920 for (size_t imageSizeNdx = 0; imageSizeNdx < imageParameters[imageTypeNdx].imageSizes.size(); ++imageSizeNdx)
921 {
922 const tcu::UVec3 imageSize = imageParameters[imageTypeNdx].imageSizes[imageSizeNdx];
923
924 // skip test for images with odd sizes for some YCbCr formats
925 if ((imageSize.x() % imageSizeAlignment.x()) != 0)
926 continue;
927 if ((imageSize.y() % imageSizeAlignment.y()) != 0)
928 continue;
929
930 std::ostringstream stream;
931 stream << imageSize.x() << "_" << imageSize.y() << "_" << imageSize.z();
932
933 formatGroup->addChild(new ImageSparseMemoryAliasingCase(testCtx, stream.str(), imageType, imageSize, format, glu::GLSL_VERSION_440, useDeviceGroup));
934 }
935 imageTypeGroup->addChild(formatGroup.release());
936 }
937 testGroup->addChild(imageTypeGroup.release());
938 }
939
940 return testGroup.release();
941 }
942
createImageSparseMemoryAliasingTests(tcu::TestContext & testCtx)943 tcu::TestCaseGroup* createImageSparseMemoryAliasingTests(tcu::TestContext& testCtx)
944 {
945 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "image_sparse_memory_aliasing"));
946 return createImageSparseMemoryAliasingTestsCommon(testCtx, testGroup);
947 }
948
createDeviceGroupImageSparseMemoryAliasingTests(tcu::TestContext & testCtx)949 tcu::TestCaseGroup* createDeviceGroupImageSparseMemoryAliasingTests(tcu::TestContext& testCtx)
950 {
951 de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "device_group_image_sparse_memory_aliasing"));
952 return createImageSparseMemoryAliasingTestsCommon(testCtx, testGroup, true);
953 }
954
955 } // sparse
956 } // vkt
957