• 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 Multisampled image load/store Tests
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktImageMultisampleLoadStoreTests.hpp"
26 #include "vktTestCaseUtil.hpp"
27 #include "vktImageTestsUtil.hpp"
28 #include "vktImageLoadStoreUtil.hpp"
29 #include "vktImageTexture.hpp"
30 
31 #include "vkDefs.hpp"
32 #include "vkRef.hpp"
33 #include "vkRefUtil.hpp"
34 #include "vkPlatform.hpp"
35 #include "vkPrograms.hpp"
36 #include "vkMemUtil.hpp"
37 #include "vkBarrierUtil.hpp"
38 #include "vkBuilderUtil.hpp"
39 #include "vkQueryUtil.hpp"
40 #include "vkImageUtil.hpp"
41 #include "vkCmdUtil.hpp"
42 
43 #include "deUniquePtr.hpp"
44 
45 #include "tcuTextureUtil.hpp"
46 #include "tcuTestLog.hpp"
47 
48 #include <string>
49 #include <vector>
50 
51 namespace vkt
52 {
53 namespace image
54 {
55 namespace
56 {
57 using namespace vk;
58 using de::MovePtr;
59 using de::UniquePtr;
60 using tcu::IVec3;
61 
62 static const VkFormat CHECKSUM_IMAGE_FORMAT = VK_FORMAT_R32_SINT;
63 
64 struct CaseDef
65 {
66 	Texture					texture;
67 	VkFormat				format;
68 	VkSampleCountFlagBits	numSamples;
69 	bool					singleLayerBind;
70 };
71 
72 //  Multisampled storage image test.
73 //
74 //  Pass 1: Write a slightly different color pattern per-sample to the whole image.
75 //  Pass 2: Read samples of the same image and check if color values are in the expected range.
76 //          Write back results as a checksum image and verify them on the host.
77 //  Each checksum image pixel should contain an integer equal to the number of samples.
78 
initPrograms(SourceCollections & programCollection,const CaseDef caseDef)79 void initPrograms (SourceCollections& programCollection, const  CaseDef caseDef)
80 {
81 	const int			dimension			= (caseDef.singleLayerBind ? caseDef.texture.layerDimension() : caseDef.texture.dimension());
82 	const std::string	texelCoordStr		= (dimension == 1 ? "gx" : dimension == 2 ? "ivec2(gx, gy)" : dimension == 3 ? "ivec3(gx, gy, gz)" : "");
83 
84 	const ImageType		usedImageType		= (caseDef.singleLayerBind ? getImageTypeForSingleLayer(caseDef.texture.type()) : caseDef.texture.type());
85 	const std::string	formatQualifierStr	= getShaderImageFormatQualifier(mapVkFormat(caseDef.format));
86 	const std::string	msImageTypeStr		= getShaderImageType(mapVkFormat(caseDef.format), usedImageType, (caseDef.texture.numSamples() > 1));
87 
88 	const std::string	xMax				= de::toString(caseDef.texture.size().x() - 1);
89 	const std::string	yMax				= de::toString(caseDef.texture.size().y() - 1);
90 	const std::string	signednessPrefix	= isUintFormat(caseDef.format) ? "u" : isIntFormat(caseDef.format) ? "i" : "";
91 	const std::string	gvec4Expr			= signednessPrefix + "vec4";
92 	const int			numColorComponents	= tcu::getNumUsedChannels(mapVkFormat(caseDef.format).order);
93 
94 	const float			storeColorScale		= computeStoreColorScale(caseDef.format, caseDef.texture.size());
95 	const float			storeColorBias		= computeStoreColorBias(caseDef.format);
96 	DE_ASSERT(colorScaleAndBiasAreValid(caseDef.format, storeColorScale, storeColorBias));
97 
98 	const std::string	colorScaleExpr		= (storeColorScale == 1.0f ? "" : "*" + de::toString(storeColorScale))
99 											+ (storeColorBias == 0.0f ? "" : " + float(" + de::toString(storeColorBias) + ")");
100 	const std::string	colorExpr			=
101 		gvec4Expr + "("
102 		+                           "gx^gy^gz^(sampleNdx >> 5)^(sampleNdx & 31), "		// we "split" sampleNdx to keep this value in [0, 31] range for numSamples = 64 case
103 		+ (numColorComponents > 1 ? "(" + xMax + "-gx)^gy^gz, "              : "0, ")
104 		+ (numColorComponents > 2 ? "gx^(" + yMax + "-gy)^gz, "              : "0, ")
105 		+ (numColorComponents > 3 ? "(" + xMax + "-gx)^(" + yMax + "-gy)^gz" : "1")
106 		+ ")" + colorScaleExpr;
107 
108 	// Store shader
109 	{
110 		std::ostringstream src;
111 		src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
112 			<< "\n"
113 			<< "layout(local_size_x = 1) in;\n"
114 			<< "layout(set = 0, binding = 1, " << formatQualifierStr << ") writeonly uniform " << msImageTypeStr << " u_msImage;\n";
115 
116 		if (caseDef.singleLayerBind)
117 			src << "layout(set = 0, binding = 0) readonly uniform Constants {\n"
118 				<< "    int u_layerNdx;\n"
119 				<< "};\n";
120 
121 		src << "\n"
122 			<< "void main (void)\n"
123 			<< "{\n"
124 			<< "    int gx = int(gl_GlobalInvocationID.x);\n"
125 			<< "    int gy = int(gl_GlobalInvocationID.y);\n"
126 			<< "    int gz = " << (caseDef.singleLayerBind ? "u_layerNdx" : "int(gl_GlobalInvocationID.z)") << ";\n"
127 			<< "\n"
128 			<< "    for (int sampleNdx = 0; sampleNdx < " << caseDef.texture.numSamples() <<"; ++sampleNdx) {\n"
129 			<< "        imageStore(u_msImage, " << texelCoordStr << ", sampleNdx, " << colorExpr << ");\n"
130 			<< "    }\n"
131 			<< "}\n";
132 
133 		programCollection.glslSources.add("comp_store") << glu::ComputeSource(src.str());
134 	}
135 
136 	// Load shader
137 	{
138 		const tcu::TextureFormat	checksumFormat			= mapVkFormat(CHECKSUM_IMAGE_FORMAT);
139 		const std::string			checksumImageTypeStr	= getShaderImageType(checksumFormat, usedImageType);
140 		const bool					useExactCompare			= isIntegerFormat(caseDef.format);
141 
142 		std::ostringstream src;
143 		src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
144 			<< "\n"
145 			<< "layout(local_size_x = 1) in;\n"
146 			<< "layout(set = 0, binding = 1, " << formatQualifierStr << ") readonly  uniform " << msImageTypeStr << " u_msImage;\n"
147 			<< "layout(set = 0, binding = 2, " << getShaderImageFormatQualifier(checksumFormat) << ") writeonly uniform " << checksumImageTypeStr << " u_checksumImage;\n";
148 
149 		if (caseDef.singleLayerBind)
150 			src << "layout(set = 0, binding = 0) readonly uniform Constants {\n"
151 				<< "    int u_layerNdx;\n"
152 				<< "};\n";
153 
154 		src << "\n"
155 			<< "void main (void)\n"
156 			<< "{\n"
157 			<< "    int gx = int(gl_GlobalInvocationID.x);\n"
158 			<< "    int gy = int(gl_GlobalInvocationID.y);\n"
159 			<< "    int gz = " << (caseDef.singleLayerBind ? "u_layerNdx" : "int(gl_GlobalInvocationID.z)") << ";\n"
160 			<< "\n"
161 			<< "    int checksum = 0;\n"
162 			<< "    for (int sampleNdx = 0; sampleNdx < " << caseDef.texture.numSamples() <<"; ++sampleNdx) {\n"
163 			<< "        " << gvec4Expr << " color = imageLoad(u_msImage, " << texelCoordStr << ", sampleNdx);\n";
164 
165 		if (useExactCompare)
166 			src << "        if (color == " << colorExpr << ")\n"
167 				<< "            ++checksum;\n";
168 		else
169 			src << "        " << gvec4Expr << " diff  = abs(abs(color) - abs(" << colorExpr << "));\n"
170 				<< "        if (all(lessThan(diff, " << gvec4Expr << "(0.02))))\n"
171 				<< "            ++checksum;\n";
172 
173 		src << "    }\n"
174 			<< "\n"
175 			<< "    imageStore(u_checksumImage, " << texelCoordStr << ", ivec4(checksum));\n"
176 			<< "}\n";
177 
178 		programCollection.glslSources.add("comp_load") << glu::ComputeSource(src.str());
179 	}
180 }
181 
checkRequirements(const InstanceInterface & vki,const VkPhysicalDevice physDevice,const CaseDef & caseDef)182 void checkRequirements (const InstanceInterface& vki, const VkPhysicalDevice physDevice, const CaseDef& caseDef)
183 {
184 	VkPhysicalDeviceFeatures	features;
185 	vki.getPhysicalDeviceFeatures(physDevice, &features);
186 
187 	if (!features.shaderStorageImageMultisample)
188 		TCU_THROW(NotSupportedError, "Multisampled storage images are not supported");
189 
190 	VkImageFormatProperties		imageFormatProperties;
191 	const VkResult				imageFormatResult		= vki.getPhysicalDeviceImageFormatProperties(
192 		physDevice, caseDef.format, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_STORAGE_BIT, (VkImageCreateFlags)0, &imageFormatProperties);
193 
194 	if (imageFormatResult == VK_ERROR_FORMAT_NOT_SUPPORTED)
195 		TCU_THROW(NotSupportedError, "Format is not supported");
196 
197 	if ((imageFormatProperties.sampleCounts & caseDef.numSamples) != caseDef.numSamples)
198 		TCU_THROW(NotSupportedError, "Requested sample count is not supported");
199 }
200 
201 //! Helper function to deal with per-layer resources.
insertImageViews(const DeviceInterface & vk,const VkDevice device,const CaseDef & caseDef,const VkFormat format,const VkImage image,std::vector<SharedVkImageView> * const pOutImageViews)202 void insertImageViews (const DeviceInterface& vk, const VkDevice device, const CaseDef& caseDef, const VkFormat format, const VkImage image, std::vector<SharedVkImageView>* const pOutImageViews)
203 {
204 	if (caseDef.singleLayerBind)
205 	{
206 		pOutImageViews->clear();
207 		pOutImageViews->resize(caseDef.texture.numLayers());
208 		for (int layerNdx = 0; layerNdx < caseDef.texture.numLayers(); ++layerNdx)
209 		{
210 			(*pOutImageViews)[layerNdx] = makeVkSharedPtr(makeImageView(
211 				vk, device, image, mapImageViewType(getImageTypeForSingleLayer(caseDef.texture.type())), format,
212 				makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, layerNdx, 1u)));
213 		}
214 	}
215 	else // bind all layers at once
216 	{
217 		pOutImageViews->clear();
218 		pOutImageViews->resize(1);
219 		(*pOutImageViews)[0] = makeVkSharedPtr(makeImageView(
220 			vk, device, image, mapImageViewType(caseDef.texture.type()), format,
221 			makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, caseDef.texture.numLayers())));
222 	}
223 }
224 
225 //! Helper function to deal with per-layer resources.
insertDescriptorSets(const DeviceInterface & vk,const VkDevice device,const CaseDef & caseDef,const VkDescriptorPool descriptorPool,const VkDescriptorSetLayout descriptorSetLayout,std::vector<SharedVkDescriptorSet> * const pOutDescriptorSets)226 void insertDescriptorSets (const DeviceInterface& vk, const VkDevice device, const CaseDef& caseDef, const VkDescriptorPool descriptorPool, const VkDescriptorSetLayout descriptorSetLayout, std::vector<SharedVkDescriptorSet>* const pOutDescriptorSets)
227 {
228 	if (caseDef.singleLayerBind)
229 	{
230 		pOutDescriptorSets->clear();
231 		pOutDescriptorSets->resize(caseDef.texture.numLayers());
232 		for (int layerNdx = 0; layerNdx < caseDef.texture.numLayers(); ++layerNdx)
233 			(*pOutDescriptorSets)[layerNdx] = makeVkSharedPtr(makeDescriptorSet(vk, device, descriptorPool, descriptorSetLayout));
234 	}
235 	else // bind all layers at once
236 	{
237 		pOutDescriptorSets->clear();
238 		pOutDescriptorSets->resize(1);
239 		(*pOutDescriptorSets)[0] = makeVkSharedPtr(makeDescriptorSet(vk, device, descriptorPool, descriptorSetLayout));
240 	}
241 }
242 
test(Context & context,const CaseDef caseDef)243 tcu::TestStatus test (Context& context, const CaseDef caseDef)
244 {
245 	const InstanceInterface&	vki					= context.getInstanceInterface();
246 	const VkPhysicalDevice		physDevice			= context.getPhysicalDevice();
247 	const DeviceInterface&		vk					= context.getDeviceInterface();
248 	const VkDevice				device				= context.getDevice();
249 	const VkQueue				queue				= context.getUniversalQueue();
250 	const deUint32				queueFamilyIndex	= context.getUniversalQueueFamilyIndex();
251 	Allocator&					allocator			= context.getDefaultAllocator();
252 
253 	checkRequirements(vki, physDevice, caseDef);
254 
255 	// Images
256 
257 	const UniquePtr<Image> msImage(new Image(
258 		vk, device, allocator, makeImageCreateInfo(caseDef.texture, caseDef.format, VK_IMAGE_USAGE_STORAGE_BIT, 0u), MemoryRequirement::Any));
259 
260 	const UniquePtr<Image> checksumImage(new Image(
261 		vk, device, allocator,
262 		makeImageCreateInfo(Texture(caseDef.texture, 1), CHECKSUM_IMAGE_FORMAT, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, 0u),
263 		MemoryRequirement::Any));
264 
265 	// Buffer used to pass constants to the shader.
266 
267 	const int			numLayers					= caseDef.texture.numLayers();
268 	const VkDeviceSize	bufferChunkSize				= getOptimalUniformBufferChunkSize(vki, physDevice, sizeof(deInt32));
269 	const VkDeviceSize	constantsBufferSizeBytes	= numLayers * bufferChunkSize;
270 	UniquePtr<Buffer>	constantsBuffer				(new Buffer(vk, device, allocator, makeBufferCreateInfo(constantsBufferSizeBytes, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT),
271 													 MemoryRequirement::HostVisible));
272 
273 	{
274 		const Allocation&	alloc	= constantsBuffer->getAllocation();
275 		deUint8* const		basePtr = static_cast<deUint8*>(alloc.getHostPtr());
276 
277 		deMemset(alloc.getHostPtr(), 0, static_cast<size_t>(constantsBufferSizeBytes));
278 
279 		for (int layerNdx = 0; layerNdx < numLayers; ++layerNdx)
280 		{
281 			deInt32* const valuePtr = reinterpret_cast<deInt32*>(basePtr + layerNdx * bufferChunkSize);
282 			*valuePtr = layerNdx;
283 		}
284 
285 		flushAlloc(vk, device, alloc);
286 	}
287 
288 	const VkDeviceSize	resultBufferSizeBytes	= getImageSizeBytes(caseDef.texture.size(), CHECKSUM_IMAGE_FORMAT);
289 	UniquePtr<Buffer>	resultBuffer			(new Buffer(vk, device, allocator, makeBufferCreateInfo(resultBufferSizeBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT),
290 												 MemoryRequirement::HostVisible));
291 
292 	{
293 		const Allocation& alloc = resultBuffer->getAllocation();
294 		deMemset(alloc.getHostPtr(), 0, static_cast<size_t>(resultBufferSizeBytes));
295 		flushAlloc(vk, device, alloc);
296 	}
297 
298 	// Descriptors
299 
300 	Unique<VkDescriptorSetLayout> descriptorSetLayout(DescriptorSetLayoutBuilder()
301 		.addSingleBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT)
302 		.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
303 		.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
304 		.build(vk, device));
305 
306 	Unique<VkDescriptorPool> descriptorPool(DescriptorPoolBuilder()
307 		.addType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, numLayers)
308 		.addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, numLayers)
309 		.addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, numLayers)
310 		.build(vk, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, numLayers));
311 
312 	std::vector<SharedVkDescriptorSet>	allDescriptorSets;
313 	std::vector<SharedVkImageView>		allMultisampledImageViews;
314 	std::vector<SharedVkImageView>		allChecksumImageViews;
315 
316 	insertDescriptorSets(vk, device, caseDef, *descriptorPool, *descriptorSetLayout, &allDescriptorSets);
317 	insertImageViews	(vk, device, caseDef, caseDef.format, **msImage, &allMultisampledImageViews);
318 	insertImageViews	(vk, device, caseDef, CHECKSUM_IMAGE_FORMAT, **checksumImage, &allChecksumImageViews);
319 
320 	// Prepare commands
321 
322 	const Unique<VkPipelineLayout>	pipelineLayout	(makePipelineLayout(vk, device, *descriptorSetLayout));
323 	const Unique<VkCommandPool>		cmdPool			(createCommandPool(vk, device, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, queueFamilyIndex));
324 	const Unique<VkCommandBuffer>	cmdBuffer		(allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
325 
326 	const tcu::IVec3				workSize				= (caseDef.singleLayerBind ? caseDef.texture.layerSize() : caseDef.texture.size());
327 	const int						loopNumLayers			= (caseDef.singleLayerBind ? numLayers : 1);
328 	const VkImageSubresourceRange	subresourceAllLayers	= makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, caseDef.texture.numLayers());
329 
330 	// Pass 1: Write MS image
331 	{
332 		const Unique<VkShaderModule>	shaderModule	(createShaderModule	(vk, device, context.getBinaryCollection().get("comp_store"), 0));
333 		const Unique<VkPipeline>		pipeline		(makeComputePipeline(vk, device, *pipelineLayout, *shaderModule));
334 
335 		beginCommandBuffer(vk, *cmdBuffer);
336 		vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
337 
338 		{
339 			const VkImageMemoryBarrier barriers[] =
340 			{
341 				makeImageMemoryBarrier((VkAccessFlags)0, VK_ACCESS_SHADER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, **msImage, subresourceAllLayers),
342 				makeImageMemoryBarrier((VkAccessFlags)0, VK_ACCESS_SHADER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, **checksumImage, subresourceAllLayers),
343 			};
344 
345 			vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, (VkDependencyFlags)0,
346 				0u, DE_NULL, 0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers);
347 		}
348 
349 		for (int layerNdx = 0; layerNdx < loopNumLayers; ++layerNdx)
350 		{
351 			const VkDescriptorSet			descriptorSet					= **allDescriptorSets[layerNdx];
352 			const VkDescriptorImageInfo		descriptorMultiImageInfo		= makeDescriptorImageInfo(DE_NULL, **allMultisampledImageViews[layerNdx], VK_IMAGE_LAYOUT_GENERAL);
353 			const VkDescriptorBufferInfo	descriptorConstantsBufferInfo	= makeDescriptorBufferInfo(constantsBuffer->get(), layerNdx*bufferChunkSize, bufferChunkSize);
354 
355 			DescriptorSetUpdateBuilder()
356 				.writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &descriptorConstantsBufferInfo)
357 				.writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorMultiImageInfo)
358 				.update(vk, device);
359 
360 			vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet, 0u, DE_NULL);
361 			vk.cmdDispatch(*cmdBuffer, workSize.x(), workSize.y(), workSize.z());
362 		}
363 
364 		endCommandBuffer(vk, *cmdBuffer);
365 		submitCommandsAndWait(vk, device, queue, *cmdBuffer);
366 	}
367 
368 	// Pass 2: "Resolve" MS image in compute shader
369 	{
370 		const Unique<VkShaderModule>	shaderModule	(createShaderModule	(vk, device, context.getBinaryCollection().get("comp_load"), 0));
371 		const Unique<VkPipeline>		pipeline		(makeComputePipeline(vk, device, *pipelineLayout, *shaderModule));
372 
373 		beginCommandBuffer(vk, *cmdBuffer);
374 		vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipeline);
375 
376 		{
377 			const VkImageMemoryBarrier barriers[] =
378 			{
379 				makeImageMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, **msImage, subresourceAllLayers),
380 			};
381 
382 			vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, (VkDependencyFlags)0,
383 				0u, DE_NULL, 0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers);
384 		}
385 
386 		for (int layerNdx = 0; layerNdx < loopNumLayers; ++layerNdx)
387 		{
388 			const VkDescriptorSet			descriptorSet					= **allDescriptorSets[layerNdx];
389 			const VkDescriptorImageInfo		descriptorMultiImageInfo		= makeDescriptorImageInfo(DE_NULL, **allMultisampledImageViews[layerNdx], VK_IMAGE_LAYOUT_GENERAL);
390 			const VkDescriptorImageInfo		descriptorChecksumImageInfo		= makeDescriptorImageInfo(DE_NULL, **allChecksumImageViews[layerNdx], VK_IMAGE_LAYOUT_GENERAL);
391 			const VkDescriptorBufferInfo	descriptorConstantsBufferInfo	= makeDescriptorBufferInfo(constantsBuffer->get(), layerNdx*bufferChunkSize, bufferChunkSize);
392 
393 			DescriptorSetUpdateBuilder()
394 				.writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &descriptorConstantsBufferInfo)
395 				.writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorMultiImageInfo)
396 				.writeSingle(descriptorSet, DescriptorSetUpdateBuilder::Location::binding(2u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &descriptorChecksumImageInfo)
397 				.update(vk, device);
398 
399 			vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet, 0u, DE_NULL);
400 			vk.cmdDispatch(*cmdBuffer, workSize.x(), workSize.y(), workSize.z());
401 		}
402 
403 		endCommandBuffer(vk, *cmdBuffer);
404 		submitCommandsAndWait(vk, device, queue, *cmdBuffer);
405 	}
406 
407 	// Retrieve result
408 	{
409 		beginCommandBuffer(vk, *cmdBuffer);
410 
411 		{
412 			const VkImageMemoryBarrier barriers[] =
413 			{
414 				makeImageMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, **checksumImage, subresourceAllLayers),
415 			};
416 			vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, (VkDependencyFlags)0,
417 				0u, DE_NULL, 0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers);
418 		}
419 		{
420 			const VkBufferImageCopy copyRegion = makeBufferImageCopy(makeExtent3D(caseDef.texture.layerSize()), caseDef.texture.numLayers());
421 			vk.cmdCopyImageToBuffer(*cmdBuffer, **checksumImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, **resultBuffer, 1u, &copyRegion);
422 		}
423 		{
424 			const VkBufferMemoryBarrier barriers[] =
425 			{
426 				makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT, **resultBuffer, 0ull, resultBufferSizeBytes),
427 			};
428 			vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, (VkDependencyFlags)0,
429 				0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers, 0u, DE_NULL);
430 		}
431 
432 		endCommandBuffer(vk, *cmdBuffer);
433 		submitCommandsAndWait(vk, device, queue, *cmdBuffer);
434 	}
435 
436 	// Verify
437 	{
438 		const Allocation& alloc = resultBuffer->getAllocation();
439 		invalidateAlloc(vk, device, alloc);
440 
441 		const IVec3		imageSize			= caseDef.texture.size();
442 		const deInt32*	pDataPtr			= static_cast<deInt32*>(alloc.getHostPtr());
443 		const deInt32	expectedChecksum	= caseDef.texture.numSamples();
444 
445 		for (int layer = 0; layer < imageSize.z(); ++layer)
446 		for (int y = 0; y < imageSize.y(); ++y)
447 		for (int x = 0; x < imageSize.x(); ++x)
448 		{
449 			if (*pDataPtr != expectedChecksum)
450 			{
451 				context.getTestContext().getLog()
452 					<< tcu::TestLog::Message << "Some sample colors were incorrect at (x, y, layer) = (" << x << ", " << y << ", " << layer << ")"	<< tcu::TestLog::EndMessage
453 					<< tcu::TestLog::Message << "Checksum value is " << *pDataPtr << " but expected " << expectedChecksum << tcu::TestLog::EndMessage;
454 
455 				return tcu::TestStatus::fail("Some sample colors were incorrect");
456 			}
457 			++pDataPtr;
458 		}
459 
460 		return tcu::TestStatus::pass("OK");
461 	}
462 }
463 
464 } // anonymous ns
465 
createImageMultisampleLoadStoreTests(tcu::TestContext & testCtx)466 tcu::TestCaseGroup* createImageMultisampleLoadStoreTests (tcu::TestContext& testCtx)
467 {
468 	const Texture textures[] =
469 	{
470 		// \note Shader code is tweaked to work with image size of 32, take a look if this needs to be modified.
471 		Texture(IMAGE_TYPE_2D,			tcu::IVec3(32,	32,	1),		1),
472 		Texture(IMAGE_TYPE_2D_ARRAY,	tcu::IVec3(32,	32,	1),		4),
473 	};
474 
475 	static const VkFormat formats[] =
476 	{
477 		VK_FORMAT_R32G32B32A32_SFLOAT,
478 		VK_FORMAT_R16G16B16A16_SFLOAT,
479 		VK_FORMAT_R32_SFLOAT,
480 
481 		VK_FORMAT_R32G32B32A32_UINT,
482 		VK_FORMAT_R16G16B16A16_UINT,
483 		VK_FORMAT_R8G8B8A8_UINT,
484 		VK_FORMAT_R32_UINT,
485 
486 		VK_FORMAT_R32G32B32A32_SINT,
487 		VK_FORMAT_R16G16B16A16_SINT,
488 		VK_FORMAT_R8G8B8A8_SINT,
489 		VK_FORMAT_R32_SINT,
490 
491 		VK_FORMAT_R8G8B8A8_UNORM,
492 
493 		VK_FORMAT_R8G8B8A8_SNORM,
494 	};
495 
496 	static const VkSampleCountFlagBits samples[] =
497 	{
498 		VK_SAMPLE_COUNT_2_BIT,
499 		VK_SAMPLE_COUNT_4_BIT,
500 		VK_SAMPLE_COUNT_8_BIT,
501 		VK_SAMPLE_COUNT_16_BIT,
502 		VK_SAMPLE_COUNT_32_BIT,
503 		VK_SAMPLE_COUNT_64_BIT,
504 	};
505 
506 	MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "load_store_multisample", "Multisampled image store and load"));
507 
508 	for (int baseTextureNdx = 0; baseTextureNdx < DE_LENGTH_OF_ARRAY(textures); ++baseTextureNdx)
509 	{
510 		const Texture&				baseTexture			= textures[baseTextureNdx];
511 		MovePtr<tcu::TestCaseGroup>	imageViewGroup		(new tcu::TestCaseGroup(testCtx, getImageTypeName(baseTexture.type()).c_str(), ""));
512 		const int					numLayerBindModes	= (baseTexture.numLayers() == 1 ? 1 : 2);
513 
514 		for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(formats); ++formatNdx)
515 		for (int layerBindMode = 0; layerBindMode < numLayerBindModes; ++layerBindMode)
516 		{
517 			const bool					singleLayerBind	= (layerBindMode != 0);
518 			const std::string			formatGroupName	= getFormatShortString(formats[formatNdx]) + (singleLayerBind ? "_single_layer" : "");
519 			MovePtr<tcu::TestCaseGroup>	formatGroup		(new tcu::TestCaseGroup(testCtx, formatGroupName.c_str(), ""));
520 
521 			for (int samplesNdx = 0; samplesNdx < DE_LENGTH_OF_ARRAY(samples); ++samplesNdx)
522 			{
523 				const std::string	samplesCaseName = "samples_" + de::toString(samples[samplesNdx]);
524 
525 				const CaseDef		caseDef =
526 				{
527 					Texture(baseTexture, samples[samplesNdx]),
528 					formats[formatNdx],
529 					samples[samplesNdx],
530 					singleLayerBind,
531 				};
532 
533 				addFunctionCaseWithPrograms(formatGroup.get(), samplesCaseName, "", initPrograms, test, caseDef);
534 			}
535 			imageViewGroup->addChild(formatGroup.release());
536 		}
537 		testGroup->addChild(imageViewGroup.release());
538 	}
539 
540 	return testGroup.release();
541 }
542 
543 } // image
544 } // vkt
545