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  vktSparseResourcesBufferMemoryAliasing.cpp
21  * \brief Sparse buffer memory aliasing tests
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vktSparseResourcesBufferMemoryAliasing.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 
42 #include "deStringUtil.hpp"
43 #include "deUniquePtr.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 enum ShaderParameters
58 {
59 	SIZE_OF_UINT_IN_SHADER	= 4u,
60 	MODULO_DIVISOR			= 1024u
61 };
62 
computeWorkGroupSize(const deUint32 numInvocations)63 tcu::UVec3 computeWorkGroupSize (const deUint32 numInvocations)
64 {
65 	const deUint32		maxComputeWorkGroupInvocations	= 128u;
66 	const tcu::UVec3	maxComputeWorkGroupSize			= tcu::UVec3(128u, 128u, 64u);
67 	deUint32			numInvocationsLeft				= numInvocations;
68 
69 	const deUint32 xWorkGroupSize = std::min(std::min(numInvocationsLeft, maxComputeWorkGroupSize.x()), maxComputeWorkGroupInvocations);
70 	numInvocationsLeft = numInvocationsLeft / xWorkGroupSize + ((numInvocationsLeft % xWorkGroupSize) ? 1u : 0u);
71 
72 	const deUint32 yWorkGroupSize = std::min(std::min(numInvocationsLeft, maxComputeWorkGroupSize.y()), maxComputeWorkGroupInvocations / xWorkGroupSize);
73 	numInvocationsLeft = numInvocationsLeft / yWorkGroupSize + ((numInvocationsLeft % yWorkGroupSize) ? 1u : 0u);
74 
75 	const deUint32 zWorkGroupSize = std::min(std::min(numInvocationsLeft, maxComputeWorkGroupSize.z()), maxComputeWorkGroupInvocations / (xWorkGroupSize*yWorkGroupSize));
76 	numInvocationsLeft = numInvocationsLeft / zWorkGroupSize + ((numInvocationsLeft % zWorkGroupSize) ? 1u : 0u);
77 
78 	return tcu::UVec3(xWorkGroupSize, yWorkGroupSize, zWorkGroupSize);
79 }
80 
81 class BufferSparseMemoryAliasingCase : public TestCase
82 {
83 public:
84 					BufferSparseMemoryAliasingCase	(tcu::TestContext&		testCtx,
85 													 const std::string&		name,
86 													 const std::string&		description,
87 													 const deUint32			bufferSize,
88 													 const glu::GLSLVersion	glslVersion,
89 													 const bool				useDeviceGroups);
90 
91 	void			initPrograms					(SourceCollections&		sourceCollections) const;
92 	TestInstance*	createInstance					(Context&				context) const;
93 
94 private:
95 	const	deUint32			m_bufferSizeInBytes;
96 	const	glu::GLSLVersion	m_glslVersion;
97 	const	bool				m_useDeviceGroups;
98 };
99 
BufferSparseMemoryAliasingCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const deUint32 bufferSize,const glu::GLSLVersion glslVersion,const bool useDeviceGroups)100 BufferSparseMemoryAliasingCase::BufferSparseMemoryAliasingCase (tcu::TestContext&		testCtx,
101 																const std::string&		name,
102 																const std::string&		description,
103 																const deUint32			bufferSize,
104 																const glu::GLSLVersion	glslVersion,
105 																const bool				useDeviceGroups)
106 	: TestCase				(testCtx, name, description)
107 	, m_bufferSizeInBytes	(bufferSize)
108 	, m_glslVersion			(glslVersion)
109 	, m_useDeviceGroups		(useDeviceGroups)
110 {
111 }
112 
initPrograms(SourceCollections & sourceCollections) const113 void BufferSparseMemoryAliasingCase::initPrograms (SourceCollections& sourceCollections) const
114 {
115 	// Create compute program
116 	const char* const versionDecl		= glu::getGLSLVersionDeclaration(m_glslVersion);
117 	const deUint32	  numInvocations	= m_bufferSizeInBytes / SIZE_OF_UINT_IN_SHADER;
118 	const tcu::UVec3  workGroupSize		= computeWorkGroupSize(numInvocations);
119 
120 	std::ostringstream src;
121 	src << versionDecl << "\n"
122 		<< "layout (local_size_x = " << workGroupSize.x() << ", local_size_y = " << workGroupSize.y() << ", local_size_z = " << workGroupSize.z() << ") in;\n"
123 		<< "layout(set = 0, binding = 0, std430) writeonly buffer Output\n"
124 		<< "{\n"
125 		<< "	uint result[];\n"
126 		<< "} sb_out;\n"
127 		<< "\n"
128 		<< "void main (void)\n"
129 		<< "{\n"
130 		<< "	uint index = gl_GlobalInvocationID.x + (gl_GlobalInvocationID.y + gl_GlobalInvocationID.z*gl_NumWorkGroups.y*gl_WorkGroupSize.y)*gl_NumWorkGroups.x*gl_WorkGroupSize.x;\n"
131 		<< "	if ( index < " << m_bufferSizeInBytes / SIZE_OF_UINT_IN_SHADER << "u )\n"
132 		<< "	{\n"
133 		<< "		sb_out.result[index] = index % " << MODULO_DIVISOR << "u;\n"
134 		<< "	}\n"
135 		<< "}\n";
136 
137 	sourceCollections.glslSources.add("comp") << glu::ComputeSource(src.str());
138 }
139 
140 class BufferSparseMemoryAliasingInstance : public SparseResourcesBaseInstance
141 {
142 public:
143 					BufferSparseMemoryAliasingInstance	(Context&					context,
144 														 const deUint32				bufferSize,
145 														 const bool					useDeviceGroups);
146 
147 	tcu::TestStatus	iterate								(void);
148 
149 private:
150 	const deUint32			m_bufferSizeInBytes;
151 	const deUint32			m_useDeviceGroups;
152 
153 };
154 
BufferSparseMemoryAliasingInstance(Context & context,const deUint32 bufferSize,const bool useDeviceGroups)155 BufferSparseMemoryAliasingInstance::BufferSparseMemoryAliasingInstance (Context&		context,
156 																		const deUint32	bufferSize,
157 																		const bool		useDeviceGroups)
158 	: SparseResourcesBaseInstance	(context, useDeviceGroups)
159 	, m_bufferSizeInBytes			(bufferSize)
160 	, m_useDeviceGroups				(useDeviceGroups)
161 {
162 }
163 
iterate(void)164 tcu::TestStatus BufferSparseMemoryAliasingInstance::iterate (void)
165 {
166 	const InstanceInterface&		instance		= m_context.getInstanceInterface();
167 	{
168 		// Create logical device supporting both sparse and compute operations
169 		QueueRequirementsVec queueRequirements;
170 		queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
171 		queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
172 
173 		createDeviceSupportingQueues(queueRequirements);
174 	}
175 	const vk::VkPhysicalDevice&	physicalDevice		= getPhysicalDevice();
176 
177 	if (!getPhysicalDeviceFeatures(instance, physicalDevice).sparseBinding)
178 		TCU_THROW(NotSupportedError, "Sparse binding not supported");
179 
180 	if (!getPhysicalDeviceFeatures(instance, physicalDevice).sparseResidencyAliased)
181 		TCU_THROW(NotSupportedError, "Sparse memory aliasing not supported");
182 
183 	const DeviceInterface&	deviceInterface	= getDeviceInterface();
184 	const Queue&			sparseQueue		= getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
185 	const Queue&			computeQueue	= getQueue(VK_QUEUE_COMPUTE_BIT, 0);
186 
187 	// Go through all physical devices
188 	for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
189 	{
190 		const deUint32	firstDeviceID = physDevID;
191 		const deUint32	secondDeviceID = (firstDeviceID + 1) % m_numPhysicalDevices;
192 
193 		VkBufferCreateInfo bufferCreateInfo =
194 		{
195 			VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,	// VkStructureType		sType;
196 			DE_NULL,								// const void*			pNext;
197 			VK_BUFFER_CREATE_SPARSE_BINDING_BIT |
198 			VK_BUFFER_CREATE_SPARSE_ALIASED_BIT,	// VkBufferCreateFlags	flags;
199 			m_bufferSizeInBytes,					// VkDeviceSize			size;
200 			VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
201 			VK_BUFFER_USAGE_TRANSFER_SRC_BIT,		// VkBufferUsageFlags	usage;
202 			VK_SHARING_MODE_EXCLUSIVE,				// VkSharingMode		sharingMode;
203 			0u,										// deUint32				queueFamilyIndexCount;
204 			DE_NULL									// const deUint32*		pQueueFamilyIndices;
205 		};
206 
207 		const deUint32 queueFamilyIndices[] = { sparseQueue.queueFamilyIndex, computeQueue.queueFamilyIndex };
208 
209 		if (sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex)
210 		{
211 			bufferCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT;
212 			bufferCreateInfo.queueFamilyIndexCount = 2u;
213 			bufferCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
214 		}
215 
216 		// Create sparse buffers
217 		const Unique<VkBuffer> sparseBufferWrite(createBuffer(deviceInterface, getDevice(), &bufferCreateInfo));
218 		const Unique<VkBuffer> sparseBufferRead(createBuffer(deviceInterface, getDevice(), &bufferCreateInfo));
219 
220 		// Create sparse buffers memory bind semaphore
221 		const Unique<VkSemaphore> bufferMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
222 
223 		const VkMemoryRequirements	bufferMemRequirements = getBufferMemoryRequirements(deviceInterface, getDevice(), *sparseBufferWrite);
224 
225 		if (bufferMemRequirements.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
226 			TCU_THROW(NotSupportedError, "Required memory size for sparse resources exceeds device limits");
227 
228 		DE_ASSERT((bufferMemRequirements.size % bufferMemRequirements.alignment) == 0);
229 
230 		const deUint32 memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), bufferMemRequirements, MemoryRequirement::Any);
231 
232 		if (memoryType == NO_MATCH_FOUND)
233 			return tcu::TestStatus::fail("No matching memory type found");
234 
235 		if (firstDeviceID != secondDeviceID)
236 		{
237 			VkPeerMemoryFeatureFlags	peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
238 			const deUint32				heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
239 			deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
240 
241 			if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT)    == 0) ||
242 				((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT) == 0))
243 			{
244 				TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC and GENERIC_DST");
245 			}
246 		}
247 
248 		const VkSparseMemoryBind sparseMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(), bufferMemRequirements.size, memoryType, 0u);
249 
250 		Move<VkDeviceMemory> deviceMemoryPtr(check<VkDeviceMemory>(sparseMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL));
251 
252 		{
253 			const VkSparseBufferMemoryBindInfo sparseBufferMemoryBindInfo[2] =
254 			{
255 				makeSparseBufferMemoryBindInfo
256 				(*sparseBufferWrite,	//VkBuffer					buffer;
257 				1u,						//deUint32					bindCount;
258 				&sparseMemoryBind		//const VkSparseMemoryBind*	Binds;
259 				),
260 
261 				makeSparseBufferMemoryBindInfo
262 				(*sparseBufferRead,		//VkBuffer					buffer;
263 				1u,						//deUint32					bindCount;
264 				&sparseMemoryBind		//const VkSparseMemoryBind*	Binds;
265 				)
266 			};
267 
268 			const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
269 			{
270 				VK_STRUCTURE_TYPE_DEVICE_GROUP_BIND_SPARSE_INFO_KHR,	//VkStructureType							sType;
271 				DE_NULL,												//const void*								pNext;
272 				firstDeviceID,											//deUint32									resourceDeviceIndex;
273 				secondDeviceID,											//deUint32									memoryDeviceIndex;
274 			};
275 
276 			const VkBindSparseInfo bindSparseInfo =
277 			{
278 				VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,						//VkStructureType							sType;
279 				m_useDeviceGroups ? &devGroupBindSparseInfo : DE_NULL,	//const void*								pNext;
280 				0u,														//deUint32									waitSemaphoreCount;
281 				DE_NULL,												//const VkSemaphore*						pWaitSemaphores;
282 				2u,														//deUint32									bufferBindCount;
283 				sparseBufferMemoryBindInfo,								//const VkSparseBufferMemoryBindInfo*		pBufferBinds;
284 				0u,														//deUint32									imageOpaqueBindCount;
285 				DE_NULL,												//const VkSparseImageOpaqueMemoryBindInfo*	pImageOpaqueBinds;
286 				0u,														//deUint32									imageBindCount;
287 				DE_NULL,												//const VkSparseImageMemoryBindInfo*		pImageBinds;
288 				1u,														//deUint32									signalSemaphoreCount;
289 				&bufferMemoryBindSemaphore.get()						//const VkSemaphore*						pSignalSemaphores;
290 			};
291 
292 			// Submit sparse bind commands for execution
293 			VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
294 		}
295 
296 		// Create output buffer
297 		const VkBufferCreateInfo		outputBufferCreateInfo = makeBufferCreateInfo(m_bufferSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
298 		const Unique<VkBuffer>			outputBuffer(createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
299 		const de::UniquePtr<Allocation>	outputBufferAlloc(bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
300 
301 		// Create command buffer for compute and data transfer oparations
302 		const Unique<VkCommandPool>	  commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
303 		const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
304 
305 		// Start recording commands
306 		beginCommandBuffer(deviceInterface, *commandBuffer);
307 
308 		// Create descriptor set
309 		const Unique<VkDescriptorSetLayout> descriptorSetLayout(
310 			DescriptorSetLayoutBuilder()
311 			.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
312 			.build(deviceInterface, getDevice()));
313 
314 		// Create compute pipeline
315 		const Unique<VkShaderModule>	shaderModule(createShaderModule(deviceInterface, getDevice(), m_context.getBinaryCollection().get("comp"), DE_NULL));
316 		const Unique<VkPipelineLayout>	pipelineLayout(makePipelineLayout(deviceInterface, getDevice(), *descriptorSetLayout));
317 		const Unique<VkPipeline>		computePipeline(makeComputePipeline(deviceInterface, getDevice(), *pipelineLayout, *shaderModule));
318 
319 		deviceInterface.cmdBindPipeline(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *computePipeline);
320 
321 		// Create descriptor set
322 		const Unique<VkDescriptorPool> descriptorPool(
323 			DescriptorPoolBuilder()
324 			.addType(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u)
325 			.build(deviceInterface, getDevice(), VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u));
326 
327 		const Unique<VkDescriptorSet> descriptorSet(makeDescriptorSet(deviceInterface, getDevice(), *descriptorPool, *descriptorSetLayout));
328 
329 		{
330 			const VkDescriptorBufferInfo sparseBufferInfo = makeDescriptorBufferInfo(*sparseBufferWrite, 0u, m_bufferSizeInBytes);
331 
332 			DescriptorSetUpdateBuilder()
333 				.writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &sparseBufferInfo)
334 				.update(deviceInterface, getDevice());
335 		}
336 
337 		deviceInterface.cmdBindDescriptorSets(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet.get(), 0u, DE_NULL);
338 
339 		{
340 			deUint32		 numInvocationsLeft = m_bufferSizeInBytes / SIZE_OF_UINT_IN_SHADER;
341 			const tcu::UVec3 workGroupSize = computeWorkGroupSize(numInvocationsLeft);
342 			const tcu::UVec3 maxComputeWorkGroupCount = tcu::UVec3(65535u, 65535u, 65535u);
343 
344 			numInvocationsLeft -= workGroupSize.x()*workGroupSize.y()*workGroupSize.z();
345 
346 			const deUint32	xWorkGroupCount = std::min(numInvocationsLeft, maxComputeWorkGroupCount.x());
347 			numInvocationsLeft = numInvocationsLeft / xWorkGroupCount + ((numInvocationsLeft % xWorkGroupCount) ? 1u : 0u);
348 			const deUint32	yWorkGroupCount = std::min(numInvocationsLeft, maxComputeWorkGroupCount.y());
349 			numInvocationsLeft = numInvocationsLeft / yWorkGroupCount + ((numInvocationsLeft % yWorkGroupCount) ? 1u : 0u);
350 			const deUint32	zWorkGroupCount = std::min(numInvocationsLeft, maxComputeWorkGroupCount.z());
351 			numInvocationsLeft = numInvocationsLeft / zWorkGroupCount + ((numInvocationsLeft % zWorkGroupCount) ? 1u : 0u);
352 
353 			if (numInvocationsLeft != 1u)
354 				TCU_THROW(NotSupportedError, "Buffer size is not supported");
355 
356 			deviceInterface.cmdDispatch(*commandBuffer, xWorkGroupCount, yWorkGroupCount, zWorkGroupCount);
357 		}
358 
359 		{
360 			const VkBufferMemoryBarrier sparseBufferWriteBarrier
361 				= makeBufferMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT,
362 					VK_ACCESS_TRANSFER_READ_BIT,
363 					*sparseBufferWrite,
364 					0ull,
365 					m_bufferSizeInBytes);
366 
367 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &sparseBufferWriteBarrier, 0u, DE_NULL);
368 		}
369 
370 		{
371 			const VkBufferCopy bufferCopy = makeBufferCopy(0u, 0u, m_bufferSizeInBytes);
372 
373 			deviceInterface.cmdCopyBuffer(*commandBuffer, *sparseBufferRead, *outputBuffer, 1u, &bufferCopy);
374 		}
375 
376 		{
377 			const VkBufferMemoryBarrier outputBufferHostBarrier
378 				= makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT,
379 					VK_ACCESS_HOST_READ_BIT,
380 					*outputBuffer,
381 					0ull,
382 					m_bufferSizeInBytes);
383 
384 			deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferHostBarrier, 0u, DE_NULL);
385 		}
386 
387 		// End recording commands
388 		endCommandBuffer(deviceInterface, *commandBuffer);
389 
390 		// The stage at which execution is going to wait for finish of sparse binding operations
391 		const VkPipelineStageFlags waitStageBits[] = { VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT };
392 
393 		// Submit commands for execution and wait for completion
394 		// In case of device groups, submit on the physical device with the resource
395 		submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 1u, &bufferMemoryBindSemaphore.get(),
396 			waitStageBits, 0, DE_NULL, m_useDeviceGroups, firstDeviceID);
397 
398 		// Retrieve data from output buffer to host memory
399 		invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
400 
401 		const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
402 
403 		// Wait for sparse queue to become idle
404 		deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
405 
406 		// Prepare reference data
407 		std::vector<deUint8> referenceData;
408 		referenceData.resize(m_bufferSizeInBytes);
409 
410 		std::vector<deUint32> referenceDataBlock;
411 		referenceDataBlock.resize(MODULO_DIVISOR);
412 
413 		for (deUint32 valueNdx = 0; valueNdx < MODULO_DIVISOR; ++valueNdx)
414 		{
415 			referenceDataBlock[valueNdx] = valueNdx % MODULO_DIVISOR;
416 		}
417 
418 		const deUint32 fullBlockSizeInBytes = MODULO_DIVISOR * SIZE_OF_UINT_IN_SHADER;
419 		const deUint32 lastBlockSizeInBytes = m_bufferSizeInBytes % fullBlockSizeInBytes;
420 		const deUint32 numberOfBlocks = m_bufferSizeInBytes / fullBlockSizeInBytes + (lastBlockSizeInBytes ? 1u : 0u);
421 
422 		for (deUint32 blockNdx = 0; blockNdx < numberOfBlocks; ++blockNdx)
423 		{
424 			const deUint32 offset = blockNdx * fullBlockSizeInBytes;
425 			deMemcpy(&referenceData[0] + offset, &referenceDataBlock[0], ((offset + fullBlockSizeInBytes) <= m_bufferSizeInBytes) ? fullBlockSizeInBytes : lastBlockSizeInBytes);
426 		}
427 
428 		// Compare reference data with output data
429 		if (deMemCmp(&referenceData[0], outputData, m_bufferSizeInBytes) != 0)
430 			return tcu::TestStatus::fail("Failed");
431 	}
432 	return tcu::TestStatus::pass("Passed");
433 }
434 
createInstance(Context & context) const435 TestInstance* BufferSparseMemoryAliasingCase::createInstance (Context& context) const
436 {
437 	return new BufferSparseMemoryAliasingInstance(context, m_bufferSizeInBytes, m_useDeviceGroups);
438 }
439 
440 } // anonymous ns
441 
addBufferSparseMemoryAliasingTests(tcu::TestCaseGroup * group,const bool useDeviceGroups)442 void addBufferSparseMemoryAliasingTests(tcu::TestCaseGroup* group, const bool useDeviceGroups)
443 {
444 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_10", "", 1 << 10, glu::GLSL_VERSION_440, useDeviceGroups));
445 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_12", "", 1 << 12, glu::GLSL_VERSION_440, useDeviceGroups));
446 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_16", "", 1 << 16, glu::GLSL_VERSION_440, useDeviceGroups));
447 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_17", "", 1 << 17, glu::GLSL_VERSION_440, useDeviceGroups));
448 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_20", "", 1 << 20, glu::GLSL_VERSION_440, useDeviceGroups));
449 	group->addChild(new BufferSparseMemoryAliasingCase(group->getTestContext(), "buffer_size_2_24", "", 1 << 24, glu::GLSL_VERSION_440, useDeviceGroups));
450 }
451 
452 } // sparse
453 } // vkt
454