• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group Inc.
6  * Copyright (c) 2016 The Android Open Source Project
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Memory qualifiers tests
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktImageQualifiersTests.hpp"
26 #include "vktImageLoadStoreTests.hpp"
27 #include "vktImageTestsUtil.hpp"
28 
29 #include "vkDefs.hpp"
30 #include "vkImageUtil.hpp"
31 #include "vkRef.hpp"
32 #include "vkRefUtil.hpp"
33 #include "vktTestCase.hpp"
34 #include "vktTestCaseUtil.hpp"
35 #include "vkBarrierUtil.hpp"
36 #include "vkPlatform.hpp"
37 #include "vkPrograms.hpp"
38 #include "vkMemUtil.hpp"
39 #include "vkBuilderUtil.hpp"
40 #include "vkQueryUtil.hpp"
41 #include "vkTypeUtil.hpp"
42 #include "vkCmdUtil.hpp"
43 #include "vkObjUtil.hpp"
44 
45 #include "deDefs.hpp"
46 #include "deStringUtil.hpp"
47 #include "deUniquePtr.hpp"
48 
49 #include "tcuImageCompare.hpp"
50 #include "tcuTexture.hpp"
51 #include "tcuTextureUtil.hpp"
52 #include "tcuVectorType.hpp"
53 
54 using namespace vk;
55 
56 namespace vkt
57 {
58 namespace image
59 {
60 namespace
61 {
62 
63 static const tcu::UVec3		g_localWorkGroupSizeBase	= tcu::UVec3(8, 8, 2);
64 static const deInt32		g_ShaderReadOffsetsX[4]		= { 1, 4, 7, 10 };
65 static const deInt32		g_ShaderReadOffsetsY[4]		= { 2, 5, 8, 11 };
66 static const deInt32		g_ShaderReadOffsetsZ[4]		= { 3, 6, 9, 12 };
67 static const char* const	g_ShaderReadOffsetsXStr		= "int[]( 1, 4, 7, 10 )";
68 static const char* const	g_ShaderReadOffsetsYStr		= "int[]( 2, 5, 8, 11 )";
69 static const char* const	g_ShaderReadOffsetsZStr		= "int[]( 3, 6, 9, 12 )";
70 
getLocalWorkGroupSize(const ImageType imageType,const tcu::UVec3 & imageSize)71 const tcu::UVec3 getLocalWorkGroupSize (const ImageType imageType, const tcu::UVec3& imageSize)
72 {
73 	const tcu::UVec3 computeGridSize	= getShaderGridSize(imageType, imageSize);
74 
75 	const tcu::UVec3 localWorkGroupSize = tcu::UVec3(de::min(g_localWorkGroupSizeBase.x(), computeGridSize.x()),
76 													 de::min(g_localWorkGroupSizeBase.y(), computeGridSize.y()),
77 													 de::min(g_localWorkGroupSizeBase.z(), computeGridSize.z()));
78 	return localWorkGroupSize;
79 }
80 
getNumWorkGroups(const ImageType imageType,const tcu::UVec3 & imageSize)81 const tcu::UVec3 getNumWorkGroups (const ImageType imageType, const tcu::UVec3& imageSize)
82 {
83 	const tcu::UVec3 computeGridSize	= getShaderGridSize(imageType, imageSize);
84 	const tcu::UVec3 localWorkGroupSize = getLocalWorkGroupSize(imageType, imageSize);
85 
86 	return computeGridSize / localWorkGroupSize;
87 }
88 
getLayerOrSlice(const ImageType imageType,const tcu::ConstPixelBufferAccess & access,const deUint32 layer)89 tcu::ConstPixelBufferAccess getLayerOrSlice (const ImageType					imageType,
90 											 const tcu::ConstPixelBufferAccess&	access,
91 											 const deUint32						layer)
92 {
93 	switch (imageType)
94 	{
95 		case IMAGE_TYPE_1D:
96 		case IMAGE_TYPE_2D:
97 		case IMAGE_TYPE_BUFFER:
98 			DE_ASSERT(layer == 0);
99 			return access;
100 
101 		case IMAGE_TYPE_1D_ARRAY:
102 			return tcu::getSubregion(access, 0, layer, access.getWidth(), 1);
103 
104 		case IMAGE_TYPE_2D_ARRAY:
105 		case IMAGE_TYPE_3D:
106 		case IMAGE_TYPE_CUBE:
107 		case IMAGE_TYPE_CUBE_ARRAY:
108 			return tcu::getSubregion(access, 0, 0, layer, access.getWidth(), access.getHeight(), 1);
109 
110 		default:
111 			DE_FATAL("Unknown image type");
112 			return tcu::ConstPixelBufferAccess();
113 	}
114 }
115 
comparePixelBuffers(tcu::TestContext & testCtx,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format,const tcu::ConstPixelBufferAccess & reference,const tcu::ConstPixelBufferAccess & result)116 bool comparePixelBuffers (tcu::TestContext&						testCtx,
117 						  const ImageType						imageType,
118 						  const tcu::UVec3&						imageSize,
119 						  const tcu::TextureFormat&				format,
120 						  const tcu::ConstPixelBufferAccess&	reference,
121 						  const tcu::ConstPixelBufferAccess&	result)
122 {
123 	DE_ASSERT(reference.getFormat() == result.getFormat());
124 	DE_ASSERT(reference.getSize() == result.getSize());
125 
126 	const bool		 intFormat			= isIntFormat(mapTextureFormat(format)) || isUintFormat(mapTextureFormat(format));
127 	deUint32		 passedLayers		= 0;
128 
129 	for (deUint32 layerNdx = 0; layerNdx < getNumLayers(imageType, imageSize); ++layerNdx)
130 	{
131 		const std::string comparisonName = "Comparison" + de::toString(layerNdx);
132 
133 		std::string comparisonDesc = "Image Comparison, ";
134 		switch (imageType)
135 		{
136 			case IMAGE_TYPE_3D:
137 				comparisonDesc = comparisonDesc + "slice " + de::toString(layerNdx);
138 				break;
139 
140 			case IMAGE_TYPE_CUBE:
141 			case IMAGE_TYPE_CUBE_ARRAY:
142 				comparisonDesc = comparisonDesc + "face " + de::toString(layerNdx % 6) + ", cube " + de::toString(layerNdx / 6);
143 				break;
144 
145 			default:
146 				comparisonDesc = comparisonDesc + "layer " + de::toString(layerNdx);
147 				break;
148 		}
149 
150 		const tcu::ConstPixelBufferAccess refLayer		= getLayerOrSlice(imageType, reference, layerNdx);
151 		const tcu::ConstPixelBufferAccess resultLayer	= getLayerOrSlice(imageType, result, layerNdx);
152 
153 		bool ok = false;
154 		if (intFormat)
155 			ok = tcu::intThresholdCompare(testCtx.getLog(), comparisonName.c_str(), comparisonDesc.c_str(), refLayer, resultLayer, tcu::UVec4(0), tcu::COMPARE_LOG_RESULT);
156 		else
157 			ok = tcu::floatThresholdCompare(testCtx.getLog(), comparisonName.c_str(), comparisonDesc.c_str(), refLayer, resultLayer, tcu::Vec4(0.01f), tcu::COMPARE_LOG_RESULT);
158 
159 		if (ok)
160 			++passedLayers;
161 	}
162 
163 	return passedLayers == getNumLayers(imageType, imageSize);
164 }
165 
getCoordStr(const ImageType imageType,const std::string & x,const std::string & y,const std::string & z)166 const std::string getCoordStr (const ImageType		imageType,
167 							   const std::string&	x,
168 							   const std::string&	y,
169 							   const std::string&	z)
170 {
171 	switch (imageType)
172 	{
173 		case IMAGE_TYPE_1D:
174 		case IMAGE_TYPE_BUFFER:
175 			return x;
176 
177 		case IMAGE_TYPE_1D_ARRAY:
178 		case IMAGE_TYPE_2D:
179 			return "ivec2(" + x + "," + y + ")";
180 
181 		case IMAGE_TYPE_2D_ARRAY:
182 		case IMAGE_TYPE_3D:
183 		case IMAGE_TYPE_CUBE:
184 		case IMAGE_TYPE_CUBE_ARRAY:
185 			return "ivec3(" + x + "," + y + "," + z + ")";
186 
187 		default:
188 			DE_ASSERT(false);
189 			return "";
190 	}
191 }
192 
193 class MemoryQualifierTestCase : public vkt::TestCase
194 {
195 public:
196 
197 	enum Qualifier
198 	{
199 		QUALIFIER_COHERENT = 0,
200 		QUALIFIER_VOLATILE,
201 		QUALIFIER_RESTRICT,
202 		QUALIFIER_LAST
203 	};
204 
205 								MemoryQualifierTestCase		(tcu::TestContext&			testCtx,
206 															 const std::string&			name,
207 															 const std::string&			description,
208 															 const Qualifier			qualifier,
209 															 const ImageType			imageType,
210 															 const tcu::UVec3&			imageSize,
211 															 const tcu::TextureFormat&	format,
212 															 const glu::GLSLVersion		glslVersion);
213 
~MemoryQualifierTestCase(void)214 	virtual						~MemoryQualifierTestCase	(void) {}
215 
216 	virtual void				initPrograms				(SourceCollections&			programCollection) const;
217 	virtual TestInstance*		createInstance				(Context&					context) const;
218 	virtual void				checkSupport				(Context&					context) const;
219 
220 protected:
221 
222 	const Qualifier				m_qualifier;
223 	const ImageType				m_imageType;
224 	const tcu::UVec3			m_imageSize;
225 	const tcu::TextureFormat	m_format;
226 	const glu::GLSLVersion		m_glslVersion;
227 };
228 
MemoryQualifierTestCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const Qualifier qualifier,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format,const glu::GLSLVersion glslVersion)229 MemoryQualifierTestCase::MemoryQualifierTestCase (tcu::TestContext&			testCtx,
230 												  const std::string&		name,
231 												  const std::string&		description,
232 												  const Qualifier			qualifier,
233 												  const ImageType			imageType,
234 												  const tcu::UVec3&			imageSize,
235 												  const tcu::TextureFormat&	format,
236 												  const glu::GLSLVersion	glslVersion)
237 	: vkt::TestCase(testCtx, name, description)
238 	, m_qualifier(qualifier)
239 	, m_imageType(imageType)
240 	, m_imageSize(imageSize)
241 	, m_format(format)
242 	, m_glslVersion(glslVersion)
243 {
244 }
245 
checkSupport(Context & context) const246 void MemoryQualifierTestCase::checkSupport (Context& context) const
247 {
248 	if (m_imageType == IMAGE_TYPE_CUBE_ARRAY)
249 		context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_IMAGE_CUBE_ARRAY);
250 }
251 
initPrograms(SourceCollections & programCollection) const252 void MemoryQualifierTestCase::initPrograms (SourceCollections& programCollection) const
253 {
254 	const char* const	versionDecl			= glu::getGLSLVersionDeclaration(m_glslVersion);
255 
256 	const char* const	qualifierName		= m_qualifier == QUALIFIER_COHERENT ? "coherent"
257 											: m_qualifier == QUALIFIER_VOLATILE ? "volatile"
258 											: DE_NULL;
259 
260 	const bool			uintFormat			= isUintFormat(mapTextureFormat(m_format));
261 	const bool			intFormat			= isIntFormat(mapTextureFormat(m_format));
262 	const std::string	colorVecTypeName	= std::string(uintFormat ? "u"	: intFormat ? "i" : "") + "vec4";
263 	const std::string	colorScalarTypeName = std::string(uintFormat ? "uint" : intFormat ? "int" : "float");
264 	const std::string	invocationCoord		= getCoordStr(m_imageType, "gx", "gy", "gz");
265 	const std::string	shaderImageFormat	= getShaderImageFormatQualifier(m_format);
266 	const std::string	shaderImageType		= getShaderImageType(m_format, m_imageType);
267 
268 	const tcu::UVec3	localWorkGroupSize	= getLocalWorkGroupSize(m_imageType, m_imageSize);
269 	const std::string	localSizeX			= de::toString(localWorkGroupSize.x());
270 	const std::string	localSizeY			= de::toString(localWorkGroupSize.y());
271 	const std::string	localSizeZ			= de::toString(localWorkGroupSize.z());
272 
273 	std::ostringstream	programBuffer;
274 
275 	programBuffer
276 		<< versionDecl << "\n"
277 		<< "\n"
278 		<< "precision highp " << shaderImageType << ";\n"
279 		<< "\n"
280 		<< "layout (local_size_x = " << localSizeX << ", local_size_y = " << localSizeY << ", local_size_z = " + localSizeZ << ") in;\n"
281 		<< "layout (" << shaderImageFormat << ", binding=0) " << qualifierName << " uniform " << shaderImageType << " u_image;\n"
282 		<< "void main (void)\n"
283 		<< "{\n"
284 		<< "	int gx = int(gl_GlobalInvocationID.x);\n"
285 		<< "	int gy = int(gl_GlobalInvocationID.y);\n"
286 		<< "	int gz = int(gl_GlobalInvocationID.z);\n"
287 		<< "	imageStore(u_image, " << invocationCoord << ", " << colorVecTypeName << "(gx^gy^gz));\n"
288 		<< "\n"
289 		<< "	memoryBarrier();\n"
290 		<< "	barrier();\n"
291 		<< "\n"
292 		<< "	" << colorScalarTypeName << " sum = " << colorScalarTypeName << "(0);\n"
293 		<< "	int groupBaseX = gx/" << localSizeX << "*" << localSizeX << ";\n"
294 		<< "	int groupBaseY = gy/" << localSizeY << "*" << localSizeY << ";\n"
295 		<< "	int groupBaseZ = gz/" << localSizeZ << "*" << localSizeZ << ";\n"
296 		<< "	int xOffsets[] = " << g_ShaderReadOffsetsXStr << ";\n"
297 		<< "	int yOffsets[] = " << g_ShaderReadOffsetsYStr << ";\n"
298 		<< "	int zOffsets[] = " << g_ShaderReadOffsetsZStr << ";\n"
299 		<< "	for (int i = 0; i < " << de::toString(DE_LENGTH_OF_ARRAY(g_ShaderReadOffsetsX)) << "; i++)\n"
300 		<< "	{\n"
301 		<< "		int readX = groupBaseX + (gx + xOffsets[i]) % " + localSizeX + ";\n"
302 		<< "		int readY = groupBaseY + (gy + yOffsets[i]) % " + localSizeY + ";\n"
303 		<< "		int readZ = groupBaseZ + (gz + zOffsets[i]) % " + localSizeZ + ";\n"
304 		<< "		sum += imageLoad(u_image, " << getCoordStr(m_imageType, "readX", "readY", "readZ") << ").x;\n"
305 		<< "	}\n"
306 		<< "\n"
307 		<< "	memoryBarrier();\n"
308 		<< "	barrier();\n"
309 		<< "\n"
310 		<< "	imageStore(u_image, " + invocationCoord + ", " + colorVecTypeName + "(sum));\n"
311 		<< "}\n";
312 
313 	programCollection.glslSources.add(m_name) << glu::ComputeSource(programBuffer.str());
314 }
315 
316 class MemoryQualifierInstanceBase : public vkt::TestInstance
317 {
318 public:
319 									MemoryQualifierInstanceBase		(Context&					context,
320 																	 const std::string&			name,
321 																	 const ImageType			imageType,
322 																	 const tcu::UVec3&			imageSize,
323 																	 const tcu::TextureFormat&	format);
324 
~MemoryQualifierInstanceBase(void)325 	virtual							~MemoryQualifierInstanceBase	(void) {}
326 
327 	virtual tcu::TestStatus			iterate							(void);
328 
329 	virtual void					prepareResources				(const VkDeviceSize			bufferSizeInBytes) = 0;
330 
331 	virtual void					prepareDescriptors				(void) = 0;
332 
333 	virtual void					commandsBeforeCompute			(const VkCommandBuffer		cmdBuffer,
334 																	 const VkDeviceSize			bufferSizeInBytes) const = 0;
335 
336 	virtual void					commandsAfterCompute			(const VkCommandBuffer		cmdBuffer,
337 																	 const VkDeviceSize			bufferSizeInBytes) const = 0;
338 
339 protected:
340 
341 	tcu::TextureLevel				generateReferenceImage			(void) const;
342 
343 	const std::string				m_name;
344 	const ImageType					m_imageType;
345 	const tcu::UVec3				m_imageSize;
346 	const tcu::TextureFormat		m_format;
347 
348 	de::MovePtr<Buffer>				m_buffer;
349 	Move<VkDescriptorPool>			m_descriptorPool;
350 	Move<VkDescriptorSetLayout>		m_descriptorSetLayout;
351 	Move<VkDescriptorSet>			m_descriptorSet;
352 };
353 
MemoryQualifierInstanceBase(Context & context,const std::string & name,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format)354 MemoryQualifierInstanceBase::MemoryQualifierInstanceBase (Context&					context,
355 														  const std::string&		name,
356 														  const ImageType			imageType,
357 														  const tcu::UVec3&			imageSize,
358 														  const tcu::TextureFormat&	format)
359 	: vkt::TestInstance(context)
360 	, m_name(name)
361 	, m_imageType(imageType)
362 	, m_imageSize(imageSize)
363 	, m_format(format)
364 {
365 }
366 
iterate(void)367 tcu::TestStatus	MemoryQualifierInstanceBase::iterate (void)
368 {
369 	const VkDevice			device				= m_context.getDevice();
370 	const DeviceInterface&	deviceInterface		= m_context.getDeviceInterface();
371 	const VkQueue			queue				= m_context.getUniversalQueue();
372 	const deUint32			queueFamilyIndex	= m_context.getUniversalQueueFamilyIndex();
373 
374 	const VkDeviceSize	bufferSizeInBytes = getNumPixels(m_imageType, m_imageSize) * tcu::getPixelSize(m_format);
375 
376 	// Prepare resources for the test
377 	prepareResources(bufferSizeInBytes);
378 
379 	// Prepare descriptor sets
380 	prepareDescriptors();
381 
382 	// Create compute shader
383 	const vk::Unique<VkShaderModule> shaderModule(createShaderModule(deviceInterface, device, m_context.getBinaryCollection().get(m_name), 0u));
384 
385 	// Create compute pipeline
386 	const vk::Unique<VkPipelineLayout> pipelineLayout(makePipelineLayout(deviceInterface, device, *m_descriptorSetLayout));
387 	const vk::Unique<VkPipeline> pipeline(makeComputePipeline(deviceInterface, device, *pipelineLayout, *shaderModule));
388 
389 	// Create command buffer
390 	const Unique<VkCommandPool> cmdPool(createCommandPool(deviceInterface, device, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queueFamilyIndex));
391 	const Unique<VkCommandBuffer> cmdBuffer(allocateCommandBuffer(deviceInterface, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
392 
393 	// Start recording commands
394 	beginCommandBuffer(deviceInterface, *cmdBuffer);
395 
396 	deviceInterface.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
397 	deviceInterface.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &m_descriptorSet.get(), 0u, DE_NULL);
398 
399 	commandsBeforeCompute(*cmdBuffer, bufferSizeInBytes);
400 
401 	const tcu::UVec3 numGroups = getNumWorkGroups(m_imageType, m_imageSize);
402 	deviceInterface.cmdDispatch(*cmdBuffer, numGroups.x(), numGroups.y(), numGroups.z());
403 
404 	commandsAfterCompute(*cmdBuffer, bufferSizeInBytes);
405 
406 	endCommandBuffer(deviceInterface, *cmdBuffer);
407 
408 	// Submit and wait for completion
409 	submitCommandsAndWait(deviceInterface, device, queue, *cmdBuffer);
410 
411 	// Retrieve data from buffer to host memory
412 	const Allocation& allocation = m_buffer->getAllocation();
413 	invalidateAlloc(deviceInterface, device, allocation);
414 
415 	const tcu::UVec3 computeGridSize = getShaderGridSize(m_imageType, m_imageSize);
416 	tcu::ConstPixelBufferAccess resultPixelBuffer(m_format, computeGridSize.x(), computeGridSize.y(), computeGridSize.z(), allocation.getHostPtr());
417 
418 	// Create a reference image
419 	tcu::TextureLevel referenceImage = generateReferenceImage();
420 	tcu::ConstPixelBufferAccess referencePixelBuffer = referenceImage.getAccess();
421 
422 	// Validate the result
423 	if (comparePixelBuffers(m_context.getTestContext(), m_imageType, m_imageSize, m_format, referencePixelBuffer, resultPixelBuffer))
424 		return tcu::TestStatus::pass("Passed");
425 	else
426 		return tcu::TestStatus::fail("Image comparison failed");
427 }
428 
generateReferenceImage(void) const429 tcu::TextureLevel MemoryQualifierInstanceBase::generateReferenceImage (void) const
430 {
431 	// Generate a reference image data using the storage format
432 	const tcu::UVec3 computeGridSize = getShaderGridSize(m_imageType, m_imageSize);
433 
434 	tcu::TextureLevel base(m_format, computeGridSize.x(), computeGridSize.y(), computeGridSize.z());
435 	tcu::PixelBufferAccess baseAccess = base.getAccess();
436 
437 	tcu::TextureLevel reference(m_format, computeGridSize.x(), computeGridSize.y(), computeGridSize.z());
438 	tcu::PixelBufferAccess referenceAccess = reference.getAccess();
439 
440 	for (deInt32 z = 0; z < baseAccess.getDepth(); ++z)
441 		for (deInt32 y = 0; y < baseAccess.getHeight(); ++y)
442 			for (deInt32 x = 0; x < baseAccess.getWidth(); ++x)
443 			{
444 				baseAccess.setPixel(tcu::IVec4(x^y^z), x, y, z);
445 			}
446 
447 	const tcu::UVec3 localWorkGroupSize = getLocalWorkGroupSize(m_imageType, m_imageSize);
448 
449 	for (deInt32 z = 0; z < referenceAccess.getDepth(); ++z)
450 		for (deInt32 y = 0; y < referenceAccess.getHeight(); ++y)
451 			for (deInt32 x = 0; x < referenceAccess.getWidth(); ++x)
452 			{
453 				const deInt32	groupBaseX	= x / localWorkGroupSize.x() * localWorkGroupSize.x();
454 				const deInt32	groupBaseY	= y / localWorkGroupSize.y() * localWorkGroupSize.y();
455 				const deInt32	groupBaseZ	= z / localWorkGroupSize.z() * localWorkGroupSize.z();
456 				deInt32			sum			= 0;
457 
458 				for (deInt32 i = 0; i < DE_LENGTH_OF_ARRAY(g_ShaderReadOffsetsX); i++)
459 				{
460 					sum += baseAccess.getPixelInt(
461 						groupBaseX + (x + g_ShaderReadOffsetsX[i]) % localWorkGroupSize.x(),
462 						groupBaseY + (y + g_ShaderReadOffsetsY[i]) % localWorkGroupSize.y(),
463 						groupBaseZ + (z + g_ShaderReadOffsetsZ[i]) % localWorkGroupSize.z()).x();
464 				}
465 
466 				referenceAccess.setPixel(tcu::IVec4(sum), x, y, z);
467 			}
468 
469 	return reference;
470 }
471 
472 class MemoryQualifierInstanceImage : public MemoryQualifierInstanceBase
473 {
474 public:
MemoryQualifierInstanceImage(Context & context,const std::string & name,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format)475 						MemoryQualifierInstanceImage	(Context&					context,
476 														 const std::string&			name,
477 														 const ImageType			imageType,
478 														 const tcu::UVec3&			imageSize,
479 														 const tcu::TextureFormat&	format)
480 							: MemoryQualifierInstanceBase(context, name, imageType, imageSize, format) {}
481 
~MemoryQualifierInstanceImage(void)482 	virtual				~MemoryQualifierInstanceImage	(void) {}
483 
484 	virtual void		prepareResources				(const VkDeviceSize			bufferSizeInBytes);
485 
486 	virtual void		prepareDescriptors				(void);
487 
488 	virtual void		commandsBeforeCompute			(const VkCommandBuffer		cmdBuffer,
489 														 const VkDeviceSize			bufferSizeInBytes) const;
490 
491 	virtual void		commandsAfterCompute			(const VkCommandBuffer		cmdBuffer,
492 														 const VkDeviceSize			bufferSizeInBytes) const;
493 protected:
494 
495 	de::MovePtr<Image>	m_image;
496 	Move<VkImageView>	m_imageView;
497 };
498 
prepareResources(const VkDeviceSize bufferSizeInBytes)499 void MemoryQualifierInstanceImage::prepareResources (const VkDeviceSize bufferSizeInBytes)
500 {
501 	const VkDevice			device			= m_context.getDevice();
502 	const DeviceInterface&	deviceInterface = m_context.getDeviceInterface();
503 	Allocator&				allocator		= m_context.getDefaultAllocator();
504 
505 	// Create image
506 	const VkImageCreateInfo imageCreateInfo =
507 	{
508 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,							// VkStructureType			sType;
509 		DE_NULL,														// const void*				pNext;
510 		m_imageType == IMAGE_TYPE_CUBE ||
511 		m_imageType	== IMAGE_TYPE_CUBE_ARRAY
512 		? (VkImageCreateFlags)VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0u,	// VkImageCreateFlags		flags;
513 		mapImageType(m_imageType),										// VkImageType				imageType;
514 		mapTextureFormat(m_format),										// VkFormat					format;
515 		makeExtent3D(getLayerSize(m_imageType, m_imageSize)),			// VkExtent3D				extent;
516 		1u,																// deUint32					mipLevels;
517 		getNumLayers(m_imageType, m_imageSize),							// deUint32					arrayLayers;
518 		VK_SAMPLE_COUNT_1_BIT,											// VkSampleCountFlagBits	samples;
519 		VK_IMAGE_TILING_OPTIMAL,										// VkImageTiling			tiling;
520 		VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_STORAGE_BIT,	// VkImageUsageFlags		usage;
521 		VK_SHARING_MODE_EXCLUSIVE,										// VkSharingMode			sharingMode;
522 		0u,																// deUint32					queueFamilyIndexCount;
523 		DE_NULL,														// const deUint32*			pQueueFamilyIndices;
524 		VK_IMAGE_LAYOUT_UNDEFINED,										// VkImageLayout			initialLayout;
525 	};
526 
527 	m_image = de::MovePtr<Image>(new Image(deviceInterface, device, allocator, imageCreateInfo, MemoryRequirement::Any));
528 
529 	// Create imageView
530 	const VkImageSubresourceRange subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, getNumLayers(m_imageType, m_imageSize));
531 	m_imageView = makeImageView(deviceInterface, device, m_image->get(), mapImageViewType(m_imageType), mapTextureFormat(m_format), subresourceRange);
532 
533 	// Create a buffer to store shader output (copied from image data)
534 	const VkBufferCreateInfo	bufferCreateInfo = makeBufferCreateInfo(bufferSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
535 	m_buffer = de::MovePtr<Buffer>(new Buffer(deviceInterface, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible));
536 }
537 
prepareDescriptors(void)538 void MemoryQualifierInstanceImage::prepareDescriptors (void)
539 {
540 	const VkDevice			device			= m_context.getDevice();
541 	const DeviceInterface&	deviceInterface = m_context.getDeviceInterface();
542 
543 	// Create descriptor pool
544 	m_descriptorPool =
545 		DescriptorPoolBuilder()
546 		.addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
547 		.build(deviceInterface, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
548 
549 	// Create descriptor set layout
550 	m_descriptorSetLayout =
551 		DescriptorSetLayoutBuilder()
552 		.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
553 		.build(deviceInterface, device);
554 
555 	// Allocate descriptor set
556 	m_descriptorSet = makeDescriptorSet(deviceInterface, device, *m_descriptorPool, *m_descriptorSetLayout);
557 
558 	// Set the bindings
559 	const VkDescriptorImageInfo descriptorImageInfo = makeDescriptorImageInfo(DE_NULL, *m_imageView, VK_IMAGE_LAYOUT_GENERAL);
560 
561 	DescriptorSetUpdateBuilder()
562 		.writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorImageInfo)
563 		.update(deviceInterface, device);
564 }
565 
commandsBeforeCompute(const VkCommandBuffer cmdBuffer,const VkDeviceSize bufferSizeInBytes) const566 void MemoryQualifierInstanceImage::commandsBeforeCompute (const VkCommandBuffer cmdBuffer, const VkDeviceSize bufferSizeInBytes) const
567 {
568 	DE_UNREF(bufferSizeInBytes);
569 
570 	const DeviceInterface&			deviceInterface	 = m_context.getDeviceInterface();
571 	const VkImageSubresourceRange	subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, getNumLayers(m_imageType, m_imageSize));
572 
573 	const VkImageMemoryBarrier imageLayoutBarrier
574 		= makeImageMemoryBarrier(0u,
575 								 VK_ACCESS_SHADER_WRITE_BIT,
576 								 VK_IMAGE_LAYOUT_UNDEFINED,
577 								 VK_IMAGE_LAYOUT_GENERAL,
578 								 m_image->get(),
579 								 subresourceRange);
580 
581 	deviceInterface.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageLayoutBarrier);
582 }
583 
commandsAfterCompute(const VkCommandBuffer cmdBuffer,const VkDeviceSize bufferSizeInBytes) const584 void MemoryQualifierInstanceImage::commandsAfterCompute (const VkCommandBuffer cmdBuffer, const VkDeviceSize bufferSizeInBytes) const
585 {
586 	const DeviceInterface&			deviceInterface	 = m_context.getDeviceInterface();
587 	const VkImageSubresourceRange	subresourceRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, getNumLayers(m_imageType, m_imageSize));
588 
589 	const VkImageMemoryBarrier imagePreCopyBarrier
590 		= makeImageMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT,
591 								 VK_ACCESS_TRANSFER_READ_BIT,
592 								 VK_IMAGE_LAYOUT_GENERAL,
593 								 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
594 								 m_image->get(),
595 								 subresourceRange);
596 
597 	deviceInterface.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imagePreCopyBarrier);
598 
599 	const VkBufferImageCopy copyParams = makeBufferImageCopy(makeExtent3D(getLayerSize(m_imageType, m_imageSize)), getNumLayers(m_imageType, m_imageSize));
600 	deviceInterface.cmdCopyImageToBuffer(cmdBuffer, m_image->get(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_buffer->get(), 1u, &copyParams);
601 
602 	const VkBufferMemoryBarrier bufferPostCopyBarrier
603 		= makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT,
604 								  VK_ACCESS_HOST_READ_BIT,
605 								  m_buffer->get(),
606 								  0ull,
607 								  bufferSizeInBytes);
608 
609 	deviceInterface.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &bufferPostCopyBarrier, 0u, DE_NULL);
610 }
611 
612 class MemoryQualifierInstanceBuffer : public MemoryQualifierInstanceBase
613 {
614 public:
MemoryQualifierInstanceBuffer(Context & context,const std::string & name,const ImageType imageType,const tcu::UVec3 & imageSize,const tcu::TextureFormat & format)615 						MemoryQualifierInstanceBuffer	(Context&					context,
616 														 const std::string&			name,
617 														 const ImageType			imageType,
618 														 const tcu::UVec3&			imageSize,
619 														 const tcu::TextureFormat&	format)
620 							: MemoryQualifierInstanceBase(context, name, imageType, imageSize, format) {}
621 
~MemoryQualifierInstanceBuffer(void)622 	virtual				~MemoryQualifierInstanceBuffer	(void) {}
623 
624 	virtual void		prepareResources				(const VkDeviceSize			bufferSizeInBytes);
625 
626 	virtual void		prepareDescriptors				(void);
627 
commandsBeforeCompute(const VkCommandBuffer,const VkDeviceSize) const628 	virtual void		commandsBeforeCompute			(const VkCommandBuffer,
629 														 const VkDeviceSize) const {}
630 
631 	virtual void		commandsAfterCompute			(const VkCommandBuffer		cmdBuffer,
632 														 const VkDeviceSize			bufferSizeInBytes) const;
633 protected:
634 
635 	Move<VkBufferView>	m_bufferView;
636 };
637 
prepareResources(const VkDeviceSize bufferSizeInBytes)638 void MemoryQualifierInstanceBuffer::prepareResources (const VkDeviceSize bufferSizeInBytes)
639 {
640 	const VkDevice			device			= m_context.getDevice();
641 	const DeviceInterface&	deviceInterface = m_context.getDeviceInterface();
642 	Allocator&				allocator		= m_context.getDefaultAllocator();
643 
644 	// Create a buffer to store shader output
645 	const VkBufferCreateInfo bufferCreateInfo = makeBufferCreateInfo(bufferSizeInBytes, VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT);
646 	m_buffer = de::MovePtr<Buffer>(new Buffer(deviceInterface, device, allocator, bufferCreateInfo, MemoryRequirement::HostVisible));
647 
648 	m_bufferView = makeBufferView(deviceInterface, device, m_buffer->get(), mapTextureFormat(m_format), 0ull, bufferSizeInBytes);
649 }
650 
prepareDescriptors(void)651 void MemoryQualifierInstanceBuffer::prepareDescriptors (void)
652 {
653 	const VkDevice			device			= m_context.getDevice();
654 	const DeviceInterface&	deviceInterface = m_context.getDeviceInterface();
655 
656 	// Create descriptor pool
657 	m_descriptorPool =
658 		DescriptorPoolBuilder()
659 		.addType(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
660 		.build(deviceInterface, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
661 
662 	// Create descriptor set layout
663 	m_descriptorSetLayout =
664 		DescriptorSetLayoutBuilder()
665 		.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
666 		.build(deviceInterface, device);
667 
668 	// Allocate descriptor set
669 	m_descriptorSet = makeDescriptorSet(deviceInterface, device, *m_descriptorPool, *m_descriptorSetLayout);
670 
671 	// Set the bindings
672 	DescriptorSetUpdateBuilder()
673 		.writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, &m_bufferView.get())
674 		.update(deviceInterface, device);
675 }
676 
commandsAfterCompute(const VkCommandBuffer cmdBuffer,const VkDeviceSize bufferSizeInBytes) const677 void MemoryQualifierInstanceBuffer::commandsAfterCompute (const VkCommandBuffer cmdBuffer, const VkDeviceSize bufferSizeInBytes) const
678 {
679 	const DeviceInterface&	deviceInterface = m_context.getDeviceInterface();
680 
681 	const VkBufferMemoryBarrier shaderWriteBarrier
682 		= makeBufferMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT,
683 								  VK_ACCESS_HOST_READ_BIT,
684 								  m_buffer->get(),
685 								  0ull,
686 								  bufferSizeInBytes);
687 
688 	deviceInterface.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &shaderWriteBarrier, 0u, DE_NULL);
689 }
690 
createInstance(Context & context) const691 TestInstance* MemoryQualifierTestCase::createInstance (Context& context) const
692 {
693 	if ( m_imageType == IMAGE_TYPE_BUFFER )
694 		return new MemoryQualifierInstanceBuffer(context, m_name, m_imageType, m_imageSize, m_format);
695 	else
696 		return new MemoryQualifierInstanceImage(context, m_name, m_imageType, m_imageSize, m_format);
697 }
698 
699 } // anonymous ns
700 
createImageQualifiersTests(tcu::TestContext & testCtx)701 tcu::TestCaseGroup* createImageQualifiersTests (tcu::TestContext& testCtx)
702 {
703 	de::MovePtr<tcu::TestCaseGroup> imageQualifiersTests(new tcu::TestCaseGroup(testCtx, "qualifiers", "Coherent, volatile and restrict"));
704 
705 	struct ImageParams
706 	{
707 		ImageParams(const ImageType imageType, const tcu::UVec3& imageSize)
708 			: m_imageType	(imageType)
709 			, m_imageSize	(imageSize)
710 		{
711 		}
712 		ImageType	m_imageType;
713 		tcu::UVec3	m_imageSize;
714 	};
715 
716 	static const ImageParams imageParamsArray[] =
717 	{
718 		ImageParams(IMAGE_TYPE_1D,			tcu::UVec3(64u, 1u,  1u)),
719 		ImageParams(IMAGE_TYPE_1D_ARRAY,	tcu::UVec3(64u, 1u,  8u)),
720 		ImageParams(IMAGE_TYPE_2D,			tcu::UVec3(64u, 64u, 1u)),
721 		ImageParams(IMAGE_TYPE_2D_ARRAY,	tcu::UVec3(64u, 64u, 8u)),
722 		ImageParams(IMAGE_TYPE_3D,			tcu::UVec3(64u, 64u, 8u)),
723 		ImageParams(IMAGE_TYPE_CUBE,		tcu::UVec3(64u, 64u, 1u)),
724 		ImageParams(IMAGE_TYPE_CUBE_ARRAY,	tcu::UVec3(64u, 64u, 2u)),
725 		ImageParams(IMAGE_TYPE_BUFFER,		tcu::UVec3(64u, 1u,  1u))
726 	};
727 
728 	static const tcu::TextureFormat formats[] =
729 	{
730 		tcu::TextureFormat(tcu::TextureFormat::R, tcu::TextureFormat::FLOAT),
731 		tcu::TextureFormat(tcu::TextureFormat::R, tcu::TextureFormat::UNSIGNED_INT32),
732 		tcu::TextureFormat(tcu::TextureFormat::R, tcu::TextureFormat::SIGNED_INT32),
733 	};
734 
735 	for (deUint32 qualifierI = 0; qualifierI < MemoryQualifierTestCase::QUALIFIER_LAST; ++qualifierI)
736 	{
737 		const MemoryQualifierTestCase::Qualifier	memoryQualifier		= (MemoryQualifierTestCase::Qualifier)qualifierI;
738 		const char* const							memoryQualifierName =
739 			memoryQualifier == MemoryQualifierTestCase::QUALIFIER_COHERENT ? "coherent" :
740 			memoryQualifier == MemoryQualifierTestCase::QUALIFIER_VOLATILE ? "volatile" :
741 			memoryQualifier == MemoryQualifierTestCase::QUALIFIER_RESTRICT ? "restrict" :
742 			DE_NULL;
743 
744 		de::MovePtr<tcu::TestCaseGroup> qualifierGroup(new tcu::TestCaseGroup(testCtx, memoryQualifierName, ""));
745 
746 		for (deInt32 imageTypeNdx = 0; imageTypeNdx < DE_LENGTH_OF_ARRAY(imageParamsArray); imageTypeNdx++)
747 		{
748 			const ImageType		imageType = imageParamsArray[imageTypeNdx].m_imageType;
749 			const tcu::UVec3	imageSize = imageParamsArray[imageTypeNdx].m_imageSize;
750 
751 			if (memoryQualifier == MemoryQualifierTestCase::QUALIFIER_RESTRICT)
752 			{
753 				de::MovePtr<TestCase> restrictCase = createImageQualifierRestrictCase(testCtx, imageType, getImageTypeName(imageType));
754 				qualifierGroup->addChild(restrictCase.release());
755 			}
756 			else
757 			{
758 				de::MovePtr<tcu::TestCaseGroup> imageTypeGroup(new tcu::TestCaseGroup(testCtx, getImageTypeName(imageType).c_str(), ""));
759 
760 				for (deInt32 formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(formats); formatNdx++)
761 				{
762 					const tcu::TextureFormat&	format		= formats[formatNdx];
763 					const std::string			formatName	= getShaderImageFormatQualifier(formats[formatNdx]);
764 
765 					imageTypeGroup->addChild(
766 						new MemoryQualifierTestCase(testCtx, formatName, "", memoryQualifier, imageType, imageSize, format, glu::GLSL_VERSION_440));
767 				}
768 
769 				qualifierGroup->addChild(imageTypeGroup.release());
770 			}
771 		}
772 
773 		imageQualifiersTests->addChild(qualifierGroup.release());
774 	}
775 
776 	return imageQualifiersTests.release();
777 }
778 
779 } // image
780 } // vkt
781