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