1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file  vktSparseResourcesMipmapSparseResidency.cpp
21  * \brief Sparse partially resident images with mipmaps tests
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vktSparseResourcesMipmapSparseResidency.hpp"
25 #include "vktSparseResourcesTestsUtil.hpp"
26 #include "vktSparseResourcesBase.hpp"
27 #include "vktTestCaseUtil.hpp"
28 
29 #include "vkDefs.hpp"
30 #include "vkRef.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkPlatform.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkMemUtil.hpp"
35 #include "vkBarrierUtil.hpp"
36 #include "vkBuilderUtil.hpp"
37 #include "vkImageUtil.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "vkTypeUtil.hpp"
40 #include "vkCmdUtil.hpp"
41 
42 #include "deUniquePtr.hpp"
43 #include "deStringUtil.hpp"
44 
45 #include <string>
46 #include <vector>
47 
48 using namespace vk;
49 
50 namespace vkt
51 {
52 namespace sparse
53 {
54 namespace
55 {
56 
57 class MipmapSparseResidencyCase : public TestCase
58 {
59 public:
60 					MipmapSparseResidencyCase	(tcu::TestContext&			testCtx,
61 												 const std::string&			name,
62 												 const std::string&			description,
63 												 const ImageType			imageType,
64 												 const tcu::UVec3&			imageSize,
65 												 const tcu::TextureFormat&	format,
66 												 const bool					useDeviceGroups);
67 
68 
69 	TestInstance*	createInstance				(Context&					context) const;
70 
71 private:
72 	const bool					m_useDeviceGroups;
73 	const ImageType				m_imageType;
74 	const tcu::UVec3			m_imageSize;
75 	const tcu::TextureFormat	m_format;
76 };
77 
MipmapSparseResidencyCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format,const bool useDeviceGroups)78 MipmapSparseResidencyCase::MipmapSparseResidencyCase (tcu::TestContext&			testCtx,
79 													  const std::string&		name,
80 													  const std::string&		description,
81 													  const ImageType			imageType,
82 													  const tcu::UVec3&			imageSize,
83 													  const tcu::TextureFormat&	format,
84 													  const bool				useDeviceGroups)
85 	: TestCase				(testCtx, name, description)
86 	, m_useDeviceGroups		(useDeviceGroups)
87 	, m_imageType			(imageType)
88 	, m_imageSize			(imageSize)
89 	, m_format				(format)
90 {
91 }
92 
93 class MipmapSparseResidencyInstance : public SparseResourcesBaseInstance
94 {
95 public:
96 					MipmapSparseResidencyInstance	(Context&									 context,
97 													 const ImageType							 imageType,
98 													 const tcu::UVec3&							 imageSize,
99 													 const tcu::TextureFormat&					 format,
100 													 const bool									 useDeviceGroups);
101 
102 
103 	tcu::TestStatus	iterate							(void);
104 
105 private:
106 	const bool					m_useDeviceGroups;
107 	const ImageType				m_imageType;
108 	const tcu::UVec3			m_imageSize;
109 	const tcu::TextureFormat	m_format;
110 };
111 
MipmapSparseResidencyInstance(Context & context,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format,const bool useDeviceGroups)112 MipmapSparseResidencyInstance::MipmapSparseResidencyInstance (Context&					context,
113 															  const ImageType			imageType,
114 															  const tcu::UVec3&			imageSize,
115 															  const tcu::TextureFormat&	format,
116 															  const bool				useDeviceGroups)
117 	: SparseResourcesBaseInstance	(context, useDeviceGroups)
118 	, m_useDeviceGroups				(useDeviceGroups)
119 	, m_imageType					(imageType)
120 	, m_imageSize					(imageSize)
121 	, m_format						(format)
122 {
123 }
124 
iterate(void)125 tcu::TestStatus MipmapSparseResidencyInstance::iterate (void)
126 {
127 	const InstanceInterface&	instance		= m_context.getInstanceInterface();
128 	{
129 		// Create logical device supporting both sparse and compute operations
130 		QueueRequirementsVec queueRequirements;
131 		queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
132 		queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
133 
134 		createDeviceSupportingQueues(queueRequirements);
135 	}
136 
137 	const VkPhysicalDevice		physicalDevice	= getPhysicalDevice();
138 	VkImageCreateInfo			imageSparseInfo;
139 	std::vector<DeviceMemorySp>	deviceMemUniquePtrVec;
140 
141 	// Check if image size does not exceed device limits
142 	if (!isImageSizeSupported(instance, physicalDevice, m_imageType, m_imageSize))
143 		TCU_THROW(NotSupportedError, "Image size not supported for device");
144 
145 	// Check if device supports sparse operations for image type
146 	if (!checkSparseSupportForImageType(instance, physicalDevice, m_imageType))
147 		TCU_THROW(NotSupportedError, "Sparse residency for image type is not supported");
148 
149 	const DeviceInterface&	deviceInterface	= getDeviceInterface();
150 	const Queue&			sparseQueue		= getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
151 	const Queue&			computeQueue	= getQueue(VK_QUEUE_COMPUTE_BIT, 0);
152 
153 	// Go through all physical devices
154 	for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
155 	{
156 		const deUint32	firstDeviceID			= physDevID;
157 		const deUint32	secondDeviceID			= (firstDeviceID + 1) % m_numPhysicalDevices;
158 
159 		imageSparseInfo.sType					= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
160 		imageSparseInfo.pNext					= DE_NULL;
161 		imageSparseInfo.flags					= VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_BINDING_BIT;
162 		imageSparseInfo.imageType				= mapImageType(m_imageType);
163 		imageSparseInfo.format					= mapTextureFormat(m_format);
164 		imageSparseInfo.extent					= makeExtent3D(getLayerSize(m_imageType, m_imageSize));
165 		imageSparseInfo.arrayLayers				= getNumLayers(m_imageType, m_imageSize);
166 		imageSparseInfo.samples					= VK_SAMPLE_COUNT_1_BIT;
167 		imageSparseInfo.tiling					= VK_IMAGE_TILING_OPTIMAL;
168 		imageSparseInfo.initialLayout			= VK_IMAGE_LAYOUT_UNDEFINED;
169 		imageSparseInfo.usage					= VK_IMAGE_USAGE_TRANSFER_DST_BIT |
170 												  VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
171 		imageSparseInfo.sharingMode				= VK_SHARING_MODE_EXCLUSIVE;
172 		imageSparseInfo.queueFamilyIndexCount	= 0u;
173 		imageSparseInfo.pQueueFamilyIndices		= DE_NULL;
174 
175 		if (m_imageType == IMAGE_TYPE_CUBE || m_imageType == IMAGE_TYPE_CUBE_ARRAY)
176 		{
177 			imageSparseInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
178 		}
179 
180 		{
181 			VkImageFormatProperties imageFormatProperties;
182 			instance.getPhysicalDeviceImageFormatProperties(physicalDevice,
183 				imageSparseInfo.format,
184 				imageSparseInfo.imageType,
185 				imageSparseInfo.tiling,
186 				imageSparseInfo.usage,
187 				imageSparseInfo.flags,
188 				&imageFormatProperties);
189 
190 			imageSparseInfo.mipLevels = getImageMaxMipLevels(imageFormatProperties, imageSparseInfo.extent);
191 		}
192 
193 		// Check if device supports sparse operations for image format
194 		if (!checkSparseSupportForImageFormat(instance, physicalDevice, imageSparseInfo))
195 			TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
196 
197 		// Create sparse image
198 		const Unique<VkImage> imageSparse(createImage(deviceInterface, getDevice(), &imageSparseInfo));
199 
200 		// Create sparse image memory bind semaphore
201 		const Unique<VkSemaphore> imageMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
202 
203 		{
204 			// Get sparse image general memory requirements
205 			const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
206 
207 			// Check if required image memory size does not exceed device limits
208 			if (imageMemoryRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
209 				TCU_THROW(NotSupportedError, "Required memory size for sparse resource exceeds device limits");
210 
211 			DE_ASSERT((imageMemoryRequirements.size % imageMemoryRequirements.alignment) == 0);
212 
213 			// Get sparse image sparse memory requirements
214 			const std::vector<VkSparseImageMemoryRequirements> sparseMemoryRequirements = getImageSparseMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
215 
216 			DE_ASSERT(sparseMemoryRequirements.size() != 0);
217 
218 			const deUint32 colorAspectIndex		= getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_COLOR_BIT);
219 			const deUint32 metadataAspectIndex	= getSparseAspectRequirementsIndex(sparseMemoryRequirements, VK_IMAGE_ASPECT_METADATA_BIT);
220 
221 			if (colorAspectIndex == NO_MATCH_FOUND)
222 				TCU_THROW(NotSupportedError, "Not supported image aspect - the test supports currently only VK_IMAGE_ASPECT_COLOR_BIT");
223 
224 			const VkSparseImageMemoryRequirements	aspectRequirements	= sparseMemoryRequirements[colorAspectIndex];
225 			const VkImageAspectFlags				aspectMask			= aspectRequirements.formatProperties.aspectMask;
226 			const VkExtent3D						imageGranularity	= aspectRequirements.formatProperties.imageGranularity;
227 
228 			DE_ASSERT((aspectRequirements.imageMipTailSize % imageMemoryRequirements.alignment) == 0);
229 
230 			std::vector<VkSparseImageMemoryBind>	imageResidencyMemoryBinds;
231 			std::vector<VkSparseMemoryBind>			imageMipTailMemoryBinds;
232 
233 			const deUint32							memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), imageMemoryRequirements, MemoryRequirement::Any);
234 
235 			if (memoryType == NO_MATCH_FOUND)
236 				return tcu::TestStatus::fail("No matching memory type found");
237 
238 			if (firstDeviceID != secondDeviceID)
239 			{
240 				VkPeerMemoryFeatureFlags	peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
241 				const deUint32				heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
242 				deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
243 
244 				if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) == 0) ||
245 					((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) == 0))
246 				{
247 					TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC and COPY_DST");
248 				}
249 			}
250 
251 			// Bind memory for each layer
252 			for (deUint32 layerNdx = 0; layerNdx < imageSparseInfo.arrayLayers; ++layerNdx)
253 			{
254 				for (deUint32 mipLevelNdx = 0; mipLevelNdx < aspectRequirements.imageMipTailFirstLod; ++mipLevelNdx)
255 				{
256 					const VkExtent3D			mipExtent			= mipLevelExtents(imageSparseInfo.extent, mipLevelNdx);
257 					const tcu::UVec3			sparseBlocks		= alignedDivide(mipExtent, imageGranularity);
258 					const deUint32				numSparseBlocks		= sparseBlocks.x() * sparseBlocks.y() * sparseBlocks.z();
259 					const VkImageSubresource	subresource			= { aspectMask, mipLevelNdx, layerNdx };
260 
261 					const VkSparseImageMemoryBind imageMemoryBind = makeSparseImageMemoryBind(deviceInterface, getDevice(),
262 						imageMemoryRequirements.alignment * numSparseBlocks, memoryType, subresource, makeOffset3D(0u, 0u, 0u), mipExtent);
263 
264 					deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
265 
266 					imageResidencyMemoryBinds.push_back(imageMemoryBind);
267 				}
268 
269 				if (!(aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
270 				{
271 					const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
272 						aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset + layerNdx * aspectRequirements.imageMipTailStride);
273 
274 					deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
275 
276 					imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
277 				}
278 
279 				// Metadata
280 				if (metadataAspectIndex != NO_MATCH_FOUND)
281 				{
282 					const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
283 
284 					if (!(metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT))
285 					{
286 						const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
287 							metadataAspectRequirements.imageMipTailSize, memoryType,
288 							metadataAspectRequirements.imageMipTailOffset + layerNdx * metadataAspectRequirements.imageMipTailStride,
289 							VK_SPARSE_MEMORY_BIND_METADATA_BIT);
290 
291 						deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
292 
293 						imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
294 					}
295 				}
296 			}
297 
298 			if ((aspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && aspectRequirements.imageMipTailFirstLod < imageSparseInfo.mipLevels)
299 			{
300 				const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
301 					aspectRequirements.imageMipTailSize, memoryType, aspectRequirements.imageMipTailOffset);
302 
303 				deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
304 
305 				imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
306 			}
307 
308 			// Metadata
309 			if (metadataAspectIndex != NO_MATCH_FOUND)
310 			{
311 				const VkSparseImageMemoryRequirements metadataAspectRequirements = sparseMemoryRequirements[metadataAspectIndex];
312 
313 				if (metadataAspectRequirements.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT)
314 				{
315 					const VkSparseMemoryBind imageMipTailMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(),
316 						metadataAspectRequirements.imageMipTailSize, memoryType, metadataAspectRequirements.imageMipTailOffset,
317 						VK_SPARSE_MEMORY_BIND_METADATA_BIT);
318 
319 					deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(imageMipTailMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
320 
321 					imageMipTailMemoryBinds.push_back(imageMipTailMemoryBind);
322 				}
323 			}
324 
325 			const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
326 			{
327 				VK_STRUCTURE_TYPE_DEVICE_GROUP_BIND_SPARSE_INFO_KHR,	//VkStructureType							sType;
328 				DE_NULL,												//const void*								pNext;
329 				firstDeviceID,											//deUint32									resourceDeviceIndex;
330 				secondDeviceID,											//deUint32									memoryDeviceIndex;
331 			};
332 
333 			VkBindSparseInfo bindSparseInfo =
334 			{
335 				VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,						//VkStructureType							sType;
336 				m_useDeviceGroups ? &devGroupBindSparseInfo : DE_NULL,	//const void*								pNext;
337 				0u,														//deUint32									waitSemaphoreCount;
338 				DE_NULL,												//const VkSemaphore*						pWaitSemaphores;
339 				0u,														//deUint32									bufferBindCount;
340 				DE_NULL,												//const VkSparseBufferMemoryBindInfo*		pBufferBinds;
341 				0u,														//deUint32									imageOpaqueBindCount;
342 				DE_NULL,												//const VkSparseImageOpaqueMemoryBindInfo*	pImageOpaqueBinds;
343 				0u,														//deUint32									imageBindCount;
344 				DE_NULL,												//const VkSparseImageMemoryBindInfo*		pImageBinds;
345 				1u,														//deUint32									signalSemaphoreCount;
346 				&imageMemoryBindSemaphore.get()							//const VkSemaphore*						pSignalSemaphores;
347 			};
348 
349 			VkSparseImageMemoryBindInfo			imageResidencyBindInfo;
350 			VkSparseImageOpaqueMemoryBindInfo	imageMipTailBindInfo;
351 
352 			if (imageResidencyMemoryBinds.size() > 0)
353 			{
354 				imageResidencyBindInfo.image		= *imageSparse;
355 				imageResidencyBindInfo.bindCount	= static_cast<deUint32>(imageResidencyMemoryBinds.size());
356 				imageResidencyBindInfo.pBinds		= &imageResidencyMemoryBinds[0];
357 
358 				bindSparseInfo.imageBindCount		= 1u;
359 				bindSparseInfo.pImageBinds			= &imageResidencyBindInfo;
360 			}
361 
362 			if (imageMipTailMemoryBinds.size() > 0)
363 			{
364 				imageMipTailBindInfo.image			= *imageSparse;
365 				imageMipTailBindInfo.bindCount		= static_cast<deUint32>(imageMipTailMemoryBinds.size());
366 				imageMipTailBindInfo.pBinds			= &imageMipTailMemoryBinds[0];
367 
368 				bindSparseInfo.imageOpaqueBindCount	= 1u;
369 				bindSparseInfo.pImageOpaqueBinds	= &imageMipTailBindInfo;
370 			}
371 
372 			// Submit sparse bind commands for execution
373 			VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
374 		}
375 
376 		// Create command buffer for compute and transfer oparations
377 		const Unique<VkCommandPool>	  commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
378 		const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
379 
380 		std::vector <VkBufferImageCopy> bufferImageCopy(imageSparseInfo.mipLevels);
381 
382 		{
383 			deUint32 bufferOffset = 0;
384 			for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; mipmapNdx++)
385 			{
386 				bufferImageCopy[mipmapNdx] = makeBufferImageCopy(mipLevelExtents(imageSparseInfo.extent, mipmapNdx), imageSparseInfo.arrayLayers, mipmapNdx, static_cast<VkDeviceSize>(bufferOffset));
387 				bufferOffset += getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
388 			}
389 		}
390 
391 		// Start recording commands
392 		beginCommandBuffer(deviceInterface, *commandBuffer);
393 
394 		const deUint32					imageSizeInBytes		= getImageSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, imageSparseInfo.mipLevels, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
395 		const VkBufferCreateInfo		inputBufferCreateInfo	= makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
396 		const Unique<VkBuffer>			inputBuffer				(createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
397 		const de::UniquePtr<Allocation>	inputBufferAlloc		(bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
398 
399 		std::vector<deUint8> referenceData(imageSizeInBytes);
400 
401 		const VkMemoryRequirements imageMemoryRequirements = getImageMemoryRequirements(deviceInterface, getDevice(), *imageSparse);
402 
403 		for (deUint32 valueNdx = 0; valueNdx < imageSizeInBytes; ++valueNdx)
404 		{
405 			referenceData[valueNdx] = static_cast<deUint8>((valueNdx % imageMemoryRequirements.alignment) + 1u);
406 		}
407 
408 		deMemcpy(inputBufferAlloc->getHostPtr(), &referenceData[0], imageSizeInBytes);
409 
410 		flushAlloc(deviceInterface, getDevice(), *inputBufferAlloc);
411 
412 		{
413 			const VkBufferMemoryBarrier inputBufferBarrier = makeBufferMemoryBarrier
414 			(
415 				VK_ACCESS_HOST_WRITE_BIT,
416 				VK_ACCESS_TRANSFER_READ_BIT,
417 				*inputBuffer,
418 				0u,
419 				imageSizeInBytes
420 			);
421 
422 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
423 		}
424 
425 		{
426 			const VkImageMemoryBarrier imageSparseTransferDstBarrier = makeImageMemoryBarrier
427 			(
428 				0u,
429 				VK_ACCESS_TRANSFER_WRITE_BIT,
430 				VK_IMAGE_LAYOUT_UNDEFINED,
431 				VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
432 				*imageSparse,
433 				makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers),
434 				sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? sparseQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED,
435 				sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex ? computeQueue.queueFamilyIndex : VK_QUEUE_FAMILY_IGNORED
436 				);
437 
438 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageSparseTransferDstBarrier);
439 		}
440 
441 		deviceInterface.cmdCopyBufferToImage(*commandBuffer, *inputBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, static_cast<deUint32>(bufferImageCopy.size()), &bufferImageCopy[0]);
442 
443 		{
444 			const VkImageMemoryBarrier imageSparseTransferSrcBarrier = makeImageMemoryBarrier
445 			(
446 				VK_ACCESS_TRANSFER_WRITE_BIT,
447 				VK_ACCESS_TRANSFER_READ_BIT,
448 				VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
449 				VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
450 				*imageSparse,
451 				makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, imageSparseInfo.mipLevels, 0u, imageSparseInfo.arrayLayers)
452 			);
453 
454 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageSparseTransferSrcBarrier);
455 		}
456 
457 		const VkBufferCreateInfo		outputBufferCreateInfo	= makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
458 		const Unique<VkBuffer>			outputBuffer			(createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
459 		const de::UniquePtr<Allocation>	outputBufferAlloc		(bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
460 
461 		deviceInterface.cmdCopyImageToBuffer(*commandBuffer, *imageSparse, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *outputBuffer, static_cast<deUint32>(bufferImageCopy.size()), &bufferImageCopy[0]);
462 
463 		{
464 			const VkBufferMemoryBarrier outputBufferBarrier = makeBufferMemoryBarrier
465 			(
466 				VK_ACCESS_TRANSFER_WRITE_BIT,
467 				VK_ACCESS_HOST_READ_BIT,
468 				*outputBuffer,
469 				0u,
470 				imageSizeInBytes
471 			);
472 
473 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
474 		}
475 
476 		// End recording commands
477 		endCommandBuffer(deviceInterface, *commandBuffer);
478 
479 		const VkPipelineStageFlags stageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
480 
481 		// Submit commands for execution and wait for completion
482 		submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 1u, &imageMemoryBindSemaphore.get(), stageBits,
483 			0, DE_NULL, m_useDeviceGroups, firstDeviceID);
484 
485 		// Retrieve data from buffer to host memory
486 		invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
487 
488 		const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
489 
490 		// Wait for sparse queue to become idle
491 		deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
492 
493 		for (deUint32 mipmapNdx = 0; mipmapNdx < imageSparseInfo.mipLevels; ++mipmapNdx)
494 		{
495 			const deUint32 mipLevelSizeInBytes	= getImageMipLevelSizeInBytes(imageSparseInfo.extent, imageSparseInfo.arrayLayers, m_format, mipmapNdx);
496 			const deUint32 bufferOffset			= static_cast<deUint32>(bufferImageCopy[mipmapNdx].bufferOffset);
497 
498 			if (deMemCmp(outputData + bufferOffset, &referenceData[bufferOffset], mipLevelSizeInBytes) != 0)
499 				return tcu::TestStatus::fail("Failed");
500 		}
501 	}
502 	return tcu::TestStatus::pass("Passed");
503 }
504 
createInstance(Context & context) const505 TestInstance* MipmapSparseResidencyCase::createInstance (Context& context) const
506 {
507 	return new MipmapSparseResidencyInstance(context, m_imageType, m_imageSize, m_format, m_useDeviceGroups);
508 }
509 
510 } // anonymous ns
511 
createMipmapSparseResidencyTestsCommon(tcu::TestContext & testCtx,de::MovePtr<tcu::TestCaseGroup> testGroup,const bool useDeviceGroup=false)512 tcu::TestCaseGroup* createMipmapSparseResidencyTestsCommon (tcu::TestContext& testCtx, de::MovePtr<tcu::TestCaseGroup> testGroup, const bool useDeviceGroup = false)
513 {
514 	static const deUint32 sizeCountPerImageType = 3u;
515 
516 	struct ImageParameters
517 	{
518 		ImageType	imageType;
519 		tcu::UVec3	imageSizes[sizeCountPerImageType];
520 	};
521 
522 	static const ImageParameters imageParametersArray[] =
523 	{
524 		{ IMAGE_TYPE_2D,		 { tcu::UVec3(512u, 256u, 1u),  tcu::UVec3(1024u, 128u, 1u), tcu::UVec3(11u,  137u, 1u) } },
525 		{ IMAGE_TYPE_2D_ARRAY,	 { tcu::UVec3(512u, 256u, 6u),	tcu::UVec3(1024u, 128u, 8u), tcu::UVec3(11u,  137u, 3u) } },
526 		{ IMAGE_TYPE_CUBE,		 { tcu::UVec3(256u, 256u, 1u),	tcu::UVec3(128u,  128u, 1u), tcu::UVec3(137u, 137u, 1u) } },
527 		{ IMAGE_TYPE_CUBE_ARRAY, { tcu::UVec3(256u, 256u, 6u),	tcu::UVec3(128u,  128u, 8u), tcu::UVec3(137u, 137u, 3u) } },
528 		{ IMAGE_TYPE_3D,		 { tcu::UVec3(256u, 256u, 16u), tcu::UVec3(1024u, 128u, 8u), tcu::UVec3(11u,  137u, 3u) } }
529 	};
530 
531 	static const tcu::TextureFormat formats[] =
532 	{
533 		tcu::TextureFormat(tcu::TextureFormat::R,	 tcu::TextureFormat::SIGNED_INT32),
534 		tcu::TextureFormat(tcu::TextureFormat::R,	 tcu::TextureFormat::SIGNED_INT16),
535 		tcu::TextureFormat(tcu::TextureFormat::R,	 tcu::TextureFormat::SIGNED_INT8),
536 		tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT32),
537 		tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT16),
538 		tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNSIGNED_INT8)
539 	};
540 
541 	for (deInt32 imageTypeNdx = 0; imageTypeNdx < DE_LENGTH_OF_ARRAY(imageParametersArray); ++imageTypeNdx)
542 	{
543 		const ImageType					imageType = imageParametersArray[imageTypeNdx].imageType;
544 		de::MovePtr<tcu::TestCaseGroup> imageTypeGroup(new tcu::TestCaseGroup(testCtx, getImageTypeName(imageType).c_str(), ""));
545 
546 		for (deInt32 formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(formats); ++formatNdx)
547 		{
548 			const tcu::TextureFormat&		format = formats[formatNdx];
549 			de::MovePtr<tcu::TestCaseGroup> formatGroup(new tcu::TestCaseGroup(testCtx, getShaderImageFormatQualifier(format).c_str(), ""));
550 
551 			for (deInt32 imageSizeNdx = 0; imageSizeNdx < DE_LENGTH_OF_ARRAY(imageParametersArray[imageTypeNdx].imageSizes); ++imageSizeNdx)
552 			{
553 				const tcu::UVec3 imageSize = imageParametersArray[imageTypeNdx].imageSizes[imageSizeNdx];
554 
555 				std::ostringstream stream;
556 				stream << imageSize.x() << "_" << imageSize.y() << "_" << imageSize.z();
557 
558 				formatGroup->addChild(new MipmapSparseResidencyCase(testCtx, stream.str(), "", imageType, imageSize, format, useDeviceGroup));
559 			}
560 			imageTypeGroup->addChild(formatGroup.release());
561 		}
562 		testGroup->addChild(imageTypeGroup.release());
563 	}
564 
565 	return testGroup.release();
566 }
567 
createMipmapSparseResidencyTests(tcu::TestContext & testCtx)568 tcu::TestCaseGroup* createMipmapSparseResidencyTests (tcu::TestContext& testCtx)
569 {
570 	de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "mipmap_sparse_residency", "Mipmap Sparse Residency"));
571 	return createMipmapSparseResidencyTestsCommon(testCtx, testGroup);
572 }
573 
createDeviceGroupMipmapSparseResidencyTests(tcu::TestContext & testCtx)574 tcu::TestCaseGroup* createDeviceGroupMipmapSparseResidencyTests (tcu::TestContext& testCtx)
575 {
576 	de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "device_group_mipmap_sparse_residency", "Mipmap Sparse Residency"));
577 	return createMipmapSparseResidencyTestsCommon(testCtx, testGroup, true);
578 }
579 
580 } // sparse
581 } // vkt
582