1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 Google 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
21  * \brief Testing compute shader writing to separate planes of a multiplanar format
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vktYCbCrStorageImageWriteTests.hpp"
25 #include "vktTestCaseUtil.hpp"
26 #include "vktTestGroupUtil.hpp"
27 #include "vktYCbCrUtil.hpp"
28 #include "vkBuilderUtil.hpp"
29 #include "vkObjUtil.hpp"
30 #include "vkCmdUtil.hpp"
31 #include "vkBarrierUtil.hpp"
32 #include "vkImageUtil.hpp"
33 #include "tcuTexVerifierUtil.hpp"
34 #include "vkTypeUtil.hpp"
35 #include "vkRefUtil.hpp"
36 #include "vkQueryUtil.hpp"
37 #include "tcuTestLog.hpp"
38 
39 namespace vkt
40 {
41 namespace ycbcr
42 {
43 namespace
44 {
45 
46 using namespace vk;
47 
48 struct TestParameters
49 {
50 	VkFormat			format;
51 	tcu::UVec3			size;
52 	VkImageCreateFlags	flags;
53 
TestParametersvkt::ycbcr::__anon30155::TestParameters54 	TestParameters (VkFormat			format_,
55 					const tcu::UVec3&	size_,
56 					VkImageCreateFlags	flags_)
57 		: format			(format_)
58 		, size				(size_)
59 		, flags				(flags_)
60 	{
61 	}
62 
TestParametersvkt::ycbcr::__anon30155::TestParameters63 	TestParameters (void)
64 		: format			(VK_FORMAT_UNDEFINED)
65 		, flags				(0u)
66 	{
67 	}
68 };
69 
checkSupport(Context& context, const TestParameters params)70 void checkSupport (Context& context, const TestParameters params)
71 {
72 	const bool							disjoint = (params.flags & VK_IMAGE_CREATE_DISJOINT_BIT) != 0;
73 	std::vector<std::string>			reqExts;
74 
75 	if (disjoint)
76 	{
77 		if (!isCoreDeviceExtension(context.getUsedApiVersion(), "VK_KHR_bind_memory2"))
78 			reqExts.push_back("VK_KHR_bind_memory2");
79 		if (!isCoreDeviceExtension(context.getUsedApiVersion(), "VK_KHR_get_memory_requirements2"))
80 			reqExts.push_back("VK_KHR_get_memory_requirements2");
81 	}
82 
83 	for ( const auto& extIter : reqExts )
84 	{
85 		if (!context.isDeviceFunctionalitySupported(extIter))
86 			TCU_THROW(NotSupportedError, (extIter + " is not supported").c_str());
87 	}
88 
89 	{
90 		const VkFormatProperties	formatProperties = getPhysicalDeviceFormatProperties(context.getInstanceInterface(),
91 			context.getPhysicalDevice(),
92 			params.format);
93 
94 		if ((formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) == 0)
95 			TCU_THROW(NotSupportedError, "Storage images are not supported for this format");
96 
97 		if (disjoint && ((formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DISJOINT_BIT) == 0))
98 			TCU_THROW(NotSupportedError, "Disjoint planes are not supported for this format");
99 	}
100 }
101 
102 template<typename T>
makeVkSharedPtr(vk::Move<T> vkMove)103 inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr(vk::Move<T> vkMove)
104 {
105 	return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove));
106 }
107 
computeWorkGroupSize(const VkExtent3D& planeExtent)108 tcu::UVec3 computeWorkGroupSize(const VkExtent3D& planeExtent)
109 {
110 	const deUint32		maxComputeWorkGroupInvocations	= 128u;
111 	const tcu::UVec3	maxComputeWorkGroupSize			= tcu::UVec3(128u, 128u, 64u);
112 
113 	const deUint32		xWorkGroupSize					= std::min(std::min(planeExtent.width, maxComputeWorkGroupSize.x()), maxComputeWorkGroupInvocations);
114 	const deUint32		yWorkGroupSize					= std::min(std::min(planeExtent.height, maxComputeWorkGroupSize.y()), maxComputeWorkGroupInvocations / xWorkGroupSize);
115 	const deUint32		zWorkGroupSize					= std::min(std::min(planeExtent.depth, maxComputeWorkGroupSize.z()), maxComputeWorkGroupInvocations / (xWorkGroupSize*yWorkGroupSize));
116 
117 	return tcu::UVec3(xWorkGroupSize, yWorkGroupSize, zWorkGroupSize);
118 }
119 
getPlaneCompatibleFormatForWriting(const vk::PlanarFormatDescription& formatInfo, deUint32 planeNdx)120 vk::VkFormat getPlaneCompatibleFormatForWriting(const vk::PlanarFormatDescription& formatInfo, deUint32 planeNdx)
121 {
122 	DE_ASSERT(planeNdx < formatInfo.numPlanes);
123 	vk::VkFormat result = formatInfo.planes[planeNdx].planeCompatibleFormat;
124 
125 	// redirect result for some of the YCbCr image formats
126 	static const std::pair<vk::VkFormat, vk::VkFormat> ycbcrFormats[] =
127 	{
128 		{ VK_FORMAT_G8B8G8R8_422_UNORM,						VK_FORMAT_R8G8B8A8_UNORM		},
129 		{ VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16,	VK_FORMAT_R16G16B16A16_UNORM	},
130 		{ VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16,	VK_FORMAT_R16G16B16A16_UNORM	},
131 		{ VK_FORMAT_G16B16G16R16_422_UNORM,					VK_FORMAT_R16G16B16A16_UNORM	},
132 		{ VK_FORMAT_B8G8R8G8_422_UNORM,						VK_FORMAT_R8G8B8A8_UNORM		},
133 		{ VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16,	VK_FORMAT_R16G16B16A16_UNORM	},
134 		{ VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16,	VK_FORMAT_R16G16B16A16_UNORM	},
135 		{ VK_FORMAT_B16G16R16G16_422_UNORM,					VK_FORMAT_R16G16B16A16_UNORM	}
136 	};
137 	auto it = std::find_if(std::begin(ycbcrFormats), std::end(ycbcrFormats), [result](const std::pair<vk::VkFormat, vk::VkFormat>& p) { return p.first == result; });
138 	if (it != std::end(ycbcrFormats))
139 		result = it->second;
140 	return result;
141 }
142 
143 tcu::TestStatus testStorageImageWrite (Context& context, TestParameters params)
144 {
145 	const DeviceInterface&						vkd						= context.getDeviceInterface();
146 	const VkDevice								device					= context.getDevice();
147 	const deUint32								queueFamilyIndex		= context.getUniversalQueueFamilyIndex();
148 	const VkQueue								queue					= context.getUniversalQueue();
149 	const PlanarFormatDescription				formatDescription		= getPlanarFormatDescription(params.format);
150 
151 	VkImageCreateInfo							imageCreateInfo =
152 	{
153 		VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
154 		DE_NULL,
155 		params.flags,
156 		VK_IMAGE_TYPE_2D,
157 		params.format,
158 		makeExtent3D(params.size.x(), params.size.y(), params.size.z()),
159 		1u,			// mipLevels
160 		1u,			// arrayLayers
161 		VK_SAMPLE_COUNT_1_BIT,
162 		VK_IMAGE_TILING_OPTIMAL,
163 		VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
164 		VK_SHARING_MODE_EXCLUSIVE,
165 		0u,
166 		(const deUint32*)DE_NULL,
167 		VK_IMAGE_LAYOUT_UNDEFINED,
168 	};
169 
170 	// check if we need to create VkImageView with different VkFormat than VkImage format
171 	VkFormat planeCompatibleFormat0 = getPlaneCompatibleFormatForWriting(formatDescription, 0);
172 	if (planeCompatibleFormat0 != getPlaneCompatibleFormat(formatDescription, 0))
173 	{
174 		imageCreateInfo.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
175 	}
176 
177 	const Unique<VkImage>						image					(createImage(vkd, device, &imageCreateInfo));
178 	// allocate memory for the whole image, or for each separate plane ( if the params.flags include VK_IMAGE_CREATE_DISJOINT_BIT )
179 	const std::vector<AllocationSp>				allocations				(allocateAndBindImageMemory(vkd, device, context.getDefaultAllocator(), *image, params.format, params.flags, MemoryRequirement::Any));
180 
181 	// Create descriptor set layout
182 	const Unique<VkDescriptorSetLayout>			descriptorSetLayout		(DescriptorSetLayoutBuilder()
183 		.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT)
184 		.build(vkd, device));
185 	const Unique<VkPipelineLayout>				pipelineLayout			(makePipelineLayout(vkd, device, *descriptorSetLayout));
186 
187 	// Create descriptor sets
188 	const Unique<VkDescriptorPool>				descriptorPool			(DescriptorPoolBuilder()
189 		.addType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1u)
190 		.build(vkd, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, vk::PlanarFormatDescription::MAX_PLANES));
191 
192 	// Create command buffer for compute and transfer operations
193 	const Unique<VkCommandPool>					commandPool				(makeCommandPool(vkd, device, queueFamilyIndex));
194 	const Unique<VkCommandBuffer>				commandBuffer			(allocateCommandBuffer(vkd, device, *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
195 
196 	std::vector<de::SharedPtr<vk::Unique<vk::VkShaderModule>>>			shaderModules;
197 	std::vector<de::SharedPtr<vk::Unique<vk::VkPipeline>>>				computePipelines;
198 	std::vector<de::SharedPtr<vk::Unique<vk::VkDescriptorSet>>>			descriptorSets;
199 	std::vector<de::SharedPtr<vk::Unique<vk::VkImageView>>>				imageViews;
200 
201 	deUint32									imageSizeInBytes		= 0;
202 	deUint32									planeOffsets[PlanarFormatDescription::MAX_PLANES];
203 	deUint32									planeRowPitches[PlanarFormatDescription::MAX_PLANES];
204 	void*										planePointers[PlanarFormatDescription::MAX_PLANES];
205 
206 	{
207 		// Start recording commands
208 		beginCommandBuffer(vkd, *commandBuffer);
209 
210 		for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
211 		{
212 			const VkImageAspectFlags		aspect						= (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
213 			const VkImageSubresourceRange	subresourceRange			= makeImageSubresourceRange(aspect, 0u, 1u, 0u, 1u);
214 			VkFormat						planeCompatibleFormat		= getPlaneCompatibleFormatForWriting(formatDescription, planeNdx);
215 			vk::PlanarFormatDescription		compatibleFormatDescription = (planeCompatibleFormat != getPlaneCompatibleFormat(formatDescription, planeNdx)) ? getPlanarFormatDescription(planeCompatibleFormat) : formatDescription;
216 			const tcu::UVec3				compatibleShaderGridSize	( params.size.x() / formatDescription.blockWidth, params.size.y() / formatDescription.blockHeight, params.size.z() / 1u);
217 			VkExtent3D						shaderExtent				= getPlaneExtent(compatibleFormatDescription, VkExtent3D{ compatibleShaderGridSize.x(), compatibleShaderGridSize.y(), compatibleShaderGridSize.z() }, planeNdx, 0u);
218 
219 			// Create and bind compute pipeline
220 			std::ostringstream shaderName;
221 			shaderName << "comp" << planeNdx;
222 			auto							shaderModule			= makeVkSharedPtr(createShaderModule(vkd, device, context.getBinaryCollection().get(shaderName.str()), DE_NULL));
223 			shaderModules.push_back(shaderModule);
224 			auto							computePipeline			= makeVkSharedPtr(makeComputePipeline(vkd, device, *pipelineLayout, (VkPipelineCreateFlags) 0u, shaderModule->get(), (VkPipelineShaderStageCreateFlags) 0u, DE_NULL));
225 			computePipelines.push_back(computePipeline);
226 			vkd.cmdBindPipeline(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipeline->get());
227 
228 			auto							descriptorSet			= makeVkSharedPtr(makeDescriptorSet(vkd, device, *descriptorPool, *descriptorSetLayout));
229 			descriptorSets.push_back(descriptorSet);
230 
231 			auto							imageView				= makeVkSharedPtr(makeImageView(vkd, device, *image, VK_IMAGE_VIEW_TYPE_2D, planeCompatibleFormat, subresourceRange));
232 			imageViews.push_back(imageView);
233 			const VkDescriptorImageInfo		imageInfo				= makeDescriptorImageInfo(DE_NULL, imageView->get(), VK_IMAGE_LAYOUT_GENERAL);
234 
235 			DescriptorSetUpdateBuilder()
236 				.writeSingle(descriptorSet->get(), DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, &imageInfo)
237 				.update(vkd, device);
238 
239 			vkd.cmdBindDescriptorSets(*commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, *pipelineLayout, 0u, 1u, &descriptorSet->get(), 0u, DE_NULL);
240 
241 			{
242 				const VkImageMemoryBarrier imageLayoutChangeBarrier = makeImageMemoryBarrier(0u, VK_ACCESS_SHADER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, *image, subresourceRange, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED);
243 				vkd.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageLayoutChangeBarrier);
244 			}
245 
246 			{
247 				const tcu::UVec3 workGroupSize = computeWorkGroupSize(shaderExtent);
248 
249 				const deUint32 xWorkGroupCount = shaderExtent.width / workGroupSize.x() + (shaderExtent.width % workGroupSize.x() ? 1u : 0u);
250 				const deUint32 yWorkGroupCount = shaderExtent.height / workGroupSize.y() + (shaderExtent.height % workGroupSize.y() ? 1u : 0u);
251 				const deUint32 zWorkGroupCount = shaderExtent.depth / workGroupSize.z() + (shaderExtent.depth % workGroupSize.z() ? 1u : 0u);
252 
253 				const tcu::UVec3 maxComputeWorkGroupCount = tcu::UVec3(65535u, 65535u, 65535u);
254 
255 				if (maxComputeWorkGroupCount.x() < xWorkGroupCount ||
256 					maxComputeWorkGroupCount.y() < yWorkGroupCount ||
257 					maxComputeWorkGroupCount.z() < zWorkGroupCount)
258 				{
259 					TCU_THROW(NotSupportedError, "Image size is not supported");
260 				}
261 
262 				vkd.cmdDispatch(*commandBuffer, xWorkGroupCount, yWorkGroupCount, zWorkGroupCount);
263 			}
264 
265 			{
266 				const VkImageMemoryBarrier imageTransferBarrier = makeImageMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *image, subresourceRange);
267 				vkd.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 0u, DE_NULL, 1u, &imageTransferBarrier);
268 			}
269 		}
270 
271 		for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
272 		{
273 			planeOffsets[planeNdx]		= imageSizeInBytes;
274 			const deUint32	planeW		= imageCreateInfo.extent.width / (formatDescription.blockWidth * formatDescription.planes[planeNdx].widthDivisor);
275 			planeRowPitches[planeNdx]	= formatDescription.planes[planeNdx].elementSizeBytes * planeW;
276 			imageSizeInBytes			+= getPlaneSizeInBytes(formatDescription, makeExtent3D( params.size.x(), params.size.y(), params.size.z()) , planeNdx, 0u, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY);
277 		}
278 
279 		const VkBufferCreateInfo		outputBufferCreateInfo	= makeBufferCreateInfo(imageSizeInBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
280 		const Unique<VkBuffer>			outputBuffer			( createBuffer(vkd, device, &outputBufferCreateInfo) );
281 		const de::UniquePtr<Allocation>	outputBufferAlloc		( bindBuffer(vkd, device, context.getDefaultAllocator(), *outputBuffer, MemoryRequirement::HostVisible) );
282 		std::vector<VkBufferImageCopy>	bufferImageCopy			( formatDescription.numPlanes );
283 
284 		for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
285 		{
286 			const VkImageAspectFlags	aspect = (formatDescription.numPlanes > 1) ? getPlaneAspect(planeNdx) : VK_IMAGE_ASPECT_COLOR_BIT;
287 
288 			bufferImageCopy[planeNdx] =
289 			{
290 				planeOffsets[planeNdx],																								//	VkDeviceSize				bufferOffset;
291 				0u,																													//	deUint32					bufferRowLength;
292 				0u,																													//	deUint32					bufferImageHeight;
293 				makeImageSubresourceLayers(aspect, 0u, 0u, 1u),																		//	VkImageSubresourceLayers	imageSubresource;
294 				makeOffset3D(0, 0, 0),																								//	VkOffset3D					imageOffset;
295 				getPlaneExtent(formatDescription, makeExtent3D(params.size.x(), params.size.y(), params.size.z()), planeNdx, 0u)	//	VkExtent3D					imageExtent;
296 			};
297 		}
298 		vkd.cmdCopyImageToBuffer(*commandBuffer, *image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, *outputBuffer, static_cast<deUint32>(bufferImageCopy.size()), bufferImageCopy.data());
299 
300 		{
301 			const VkBufferMemoryBarrier outputBufferHostReadBarrier = makeBufferMemoryBarrier
302 			(
303 				VK_ACCESS_TRANSFER_WRITE_BIT,
304 				VK_ACCESS_HOST_READ_BIT,
305 				*outputBuffer,
306 				0u,
307 				imageSizeInBytes
308 			);
309 
310 			vkd.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferHostReadBarrier, 0u, DE_NULL);
311 		}
312 
313 		// End recording commands
314 		endCommandBuffer(vkd, *commandBuffer);
315 
316 		// Submit commands for execution and wait for completion
317 		submitCommandsAndWait(vkd, device, queue, *commandBuffer);
318 
319 		// Retrieve data from buffer to host memory
320 		invalidateAlloc(vkd, device, *outputBufferAlloc);
321 		deUint8*					outputData = static_cast<deUint8*>(outputBufferAlloc->getHostPtr());
322 
323 		for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
324 			planePointers[planeNdx] = outputData + static_cast<size_t>(planeOffsets[planeNdx]);
325 
326 		// write result images to log file
327 		for (deUint32 channelNdx = 0; channelNdx < 4; ++channelNdx)
328 		{
329 			if (!formatDescription.hasChannelNdx(channelNdx))
330 				continue;
331 			deUint32					planeNdx					= formatDescription.channels[channelNdx].planeNdx;
332 			vk::VkFormat				planeCompatibleFormat		= getPlaneCompatibleFormatForWriting(formatDescription, planeNdx);
333 			vk::PlanarFormatDescription	compatibleFormatDescription	= (planeCompatibleFormat != getPlaneCompatibleFormat(formatDescription, planeNdx)) ? getPlanarFormatDescription(planeCompatibleFormat) : formatDescription;
334 			const tcu::UVec3			compatibleShaderGridSize	( params.size.x() / formatDescription.blockWidth, params.size.y() / formatDescription.blockHeight, params.size.z() / 1u );
335 			tcu::ConstPixelBufferAccess	pixelBuffer					= vk::getChannelAccess(compatibleFormatDescription, compatibleShaderGridSize, planeRowPitches, (const void* const*)planePointers, channelNdx);
336 			std::ostringstream str;
337 			str << "image" << channelNdx;
338 			context.getTestContext().getLog() << tcu::LogImage(str.str(), str.str(), pixelBuffer);
339 		}
340 
341 		// verify data
342 		const float					epsilon = 1e-5f;
343 		for (deUint32 channelNdx = 0; channelNdx < 4; ++channelNdx)
344 		{
345 			if (!formatDescription.hasChannelNdx(channelNdx))
346 				continue;
347 
348 			deUint32							planeNdx					= formatDescription.channels[channelNdx].planeNdx;
349 			vk::VkFormat						planeCompatibleFormat		= getPlaneCompatibleFormatForWriting(formatDescription, planeNdx);
350 			vk::PlanarFormatDescription			compatibleFormatDescription	= (planeCompatibleFormat != getPlaneCompatibleFormat(formatDescription, planeNdx)) ? getPlanarFormatDescription(planeCompatibleFormat) : formatDescription;
351 			const tcu::UVec3					compatibleShaderGridSize	( params.size.x() / formatDescription.blockWidth, params.size.y() / formatDescription.blockHeight, params.size.z() / 1u );
352 			VkExtent3D							compatibleImageSize			{ imageCreateInfo.extent.width / formatDescription.blockWidth, imageCreateInfo.extent.height / formatDescription.blockHeight, imageCreateInfo.extent.depth / 1u };
353 			tcu::ConstPixelBufferAccess			pixelBuffer					= vk::getChannelAccess(compatibleFormatDescription, compatibleShaderGridSize, planeRowPitches, (const void* const*)planePointers, channelNdx);
354 			VkExtent3D							planeExtent					= getPlaneExtent(compatibleFormatDescription, compatibleImageSize, planeNdx, 0u);
355 			tcu::IVec3							pixelDivider				= pixelBuffer.getDivider();
356 
357 			for (deUint32 offsetZ = 0u; offsetZ < planeExtent.depth; ++offsetZ)
358 			for (deUint32 offsetY = 0u; offsetY < planeExtent.height; ++offsetY)
359 			for (deUint32 offsetX = 0u; offsetX < planeExtent.width; ++offsetX)
360 			{
361 				deUint32	iReferenceValue;
362 				float		fReferenceValue;
363 				switch (channelNdx)
364 				{
365 					case 0:
366 						iReferenceValue = offsetX % 127u;
367 						fReferenceValue = static_cast<float>(iReferenceValue) / 127.f;
368 						break;
369 					case 1:
370 						iReferenceValue = offsetY % 127u;
371 						fReferenceValue = static_cast<float>(iReferenceValue) / 127.f;
372 						break;
373 					case 2:
374 						iReferenceValue = offsetZ % 127u;
375 						fReferenceValue = static_cast<float>(iReferenceValue) / 127.f;
376 						break;
377 					case 3:
378 						iReferenceValue = 1u;
379 						fReferenceValue = 1.f;
380 						break;
381 					default:	DE_FATAL("Unexpected channel index");	break;
382 				}
383 				float acceptableError = epsilon;
384 
385 				switch (formatDescription.channels[channelNdx].type)
386 				{
387 					case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
388 					case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
389 					{
390 						tcu::UVec4 outputValue = pixelBuffer.getPixelUint(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), 0);
391 
392 						if (outputValue.x() != iReferenceValue)
393 							return tcu::TestStatus::fail("Failed");
394 
395 						break;
396 					}
397 					case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
398 					case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
399 					{
400 						float fixedPointError = tcu::TexVerifierUtil::computeFixedPointError(formatDescription.channels[channelNdx].sizeBits);
401 						acceptableError += fixedPointError;
402 						tcu::Vec4 outputValue = pixelBuffer.getPixel(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), 0);
403 
404 						if (deAbs(outputValue.x() - fReferenceValue) > acceptableError)
405 							return tcu::TestStatus::fail("Failed");
406 
407 						break;
408 					}
409 					case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
410 					{
411 						const tcu::Vec4 outputValue = pixelBuffer.getPixel(offsetX * pixelDivider.x(), offsetY * pixelDivider.y(), 0);
412 
413 						if (deAbs( outputValue.x() - fReferenceValue) > acceptableError)
414 							return tcu::TestStatus::fail("Failed");
415 
416 						break;
417 					}
418 					default:	DE_FATAL("Unexpected channel type");	break;
419 				}
420 			}
421 		}
422 	}
423 	return tcu::TestStatus::pass("Passed");
424 }
425 
426 std::string getShaderImageType (const vk::PlanarFormatDescription& description)
427 {
428 	std::string	formatPart;
429 
430 	// all PlanarFormatDescription types have at least one channel ( 0 ) and all channel types are the same :
431 	switch (description.channels[0].type)
432 	{
433 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
434 			formatPart = "i";
435 			break;
436 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
437 			formatPart = "u";
438 			break;
439 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
440 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
441 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
442 			break;
443 
444 		default:
445 			DE_FATAL("Unexpected channel type");
446 	}
447 
448 	return formatPart + "image2D";
449 }
450 
451 std::string getShaderImageDataType (const vk::PlanarFormatDescription& description)
452 {
453 	switch (description.channels[0].type)
454 	{
455 	case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
456 		return "uvec4";
457 	case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
458 		return "ivec4";
459 	case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
460 	case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
461 	case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
462 		return "vec4";
463 	default:
464 		DE_FATAL("Unexpected channel type");
465 		return "";
466 	}
467 }
468 
469 std::string getFormatValueString	(const std::vector<std::pair<deUint32, deUint32>>& channelsOnPlane,
470 									 const std::vector<std::string>& formatValueStrings)
471 {
472 	std::string result = "( ";
473 	deUint32 i;
474 	for (i=0; i<channelsOnPlane.size(); ++i)
475 	{
476 		result += formatValueStrings[channelsOnPlane[i].first];
477 		if (i < 3)
478 			result += ", ";
479 	}
480 	for (; i < 4; ++i)
481 	{
482 		result += "0";
483 		if (i < 3)
484 			result += ", ";
485 	}
486 	result += " )";
487 	return result;
488 }
489 
490 std::string getShaderImageFormatQualifier (VkFormat format)
491 {
492 	switch (format)
493 	{
494 		case VK_FORMAT_R8_SINT:										return "r8i";
495 		case VK_FORMAT_R16_SINT:									return "r16i";
496 		case VK_FORMAT_R32_SINT:									return "r32i";
497 		case VK_FORMAT_R8_UINT:										return "r8ui";
498 		case VK_FORMAT_R16_UINT:									return "r16ui";
499 		case VK_FORMAT_R32_UINT:									return "r32ui";
500 		case VK_FORMAT_R8_SNORM:									return "r8_snorm";
501 		case VK_FORMAT_R16_SNORM:									return "r16_snorm";
502 		case VK_FORMAT_R8_UNORM:									return "r8";
503 		case VK_FORMAT_R16_UNORM:									return "r16";
504 
505 		case VK_FORMAT_R8G8_SINT:									return "rg8i";
506 		case VK_FORMAT_R16G16_SINT:									return "rg16i";
507 		case VK_FORMAT_R32G32_SINT:									return "rg32i";
508 		case VK_FORMAT_R8G8_UINT:									return "rg8ui";
509 		case VK_FORMAT_R16G16_UINT:									return "rg16ui";
510 		case VK_FORMAT_R32G32_UINT:									return "rg32ui";
511 		case VK_FORMAT_R8G8_SNORM:									return "rg8_snorm";
512 		case VK_FORMAT_R16G16_SNORM:								return "rg16_snorm";
513 		case VK_FORMAT_R8G8_UNORM:									return "rg8";
514 		case VK_FORMAT_R16G16_UNORM:								return "rg16";
515 
516 		case VK_FORMAT_R8G8B8A8_SINT:								return "rgba8i";
517 		case VK_FORMAT_R16G16B16A16_SINT:							return "rgba16i";
518 		case VK_FORMAT_R32G32B32A32_SINT:							return "rgba32i";
519 		case VK_FORMAT_R8G8B8A8_UINT:								return "rgba8ui";
520 		case VK_FORMAT_R16G16B16A16_UINT:							return "rgba16ui";
521 		case VK_FORMAT_R32G32B32A32_UINT:							return "rgba32ui";
522 		case VK_FORMAT_R8G8B8A8_SNORM:								return "rgba8_snorm";
523 		case VK_FORMAT_R16G16B16A16_SNORM:							return "rgba16_snorm";
524 		case VK_FORMAT_R8G8B8A8_UNORM:								return "rgba8";
525 		case VK_FORMAT_R16G16B16A16_UNORM:							return "rgba16";
526 
527 		case VK_FORMAT_G8B8G8R8_422_UNORM:							return "rgba8";
528 		case VK_FORMAT_B8G8R8G8_422_UNORM:							return "rgba8";
529 		case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:					return "rgba8";
530 		case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:					return "rgba8";
531 		case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:					return "rgba8";
532 		case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:					return "rgba8";
533 		case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:					return "rgba8";
534 		case VK_FORMAT_R10X6_UNORM_PACK16:							return "r16";
535 		case VK_FORMAT_R10X6G10X6_UNORM_2PACK16:					return "rg16";
536 		case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:			return "rgba16";
537 		case VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:		return "rgba16";
538 		case VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:		return "rgba16";
539 		case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:	return "rgba16";
540 		case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:	return "rgba16";
541 		case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:	return "rgba16";
542 		case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:	return "rgba16";
543 		case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:	return "rgba16";
544 		case VK_FORMAT_R12X4_UNORM_PACK16:							return "r16";
545 		case VK_FORMAT_R12X4G12X4_UNORM_2PACK16:					return "rg16";
546 		case VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16:			return "rgba16";
547 		case VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:		return "rgba16";
548 		case VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:		return "rgba16";
549 		case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:	return "rgba16";
550 		case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:	return "rgba16";
551 		case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:	return "rgba16";
552 		case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:	return "rgba16";
553 		case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:	return "rgba16";
554 		case VK_FORMAT_G16B16G16R16_422_UNORM:						return "rgba16";
555 		case VK_FORMAT_B16G16R16G16_422_UNORM:						return "rgba16";
556 		case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:				return "rgba16";
557 		case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:					return "rgba16";
558 		case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:				return "rgba16";
559 		case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:					return "rgba16";
560 		case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:				return "rgba16";
561 		case VK_FORMAT_G8_B8R8_2PLANE_444_UNORM_EXT:				return "rgba8";
562 		case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16_EXT:return "rgba16";
563 		case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16_EXT:return "rgba16";
564 		case VK_FORMAT_G16_B16R16_2PLANE_444_UNORM_EXT:				return "rgba16";
565 
566 		default:
567 			DE_FATAL("Unexpected texture format");
568 			return "error";
569 	}
570 }
571 
572 void initPrograms (SourceCollections& sourceCollections, TestParameters params)
573 {
574 	// Create compute program
575 	const char* const				versionDecl			= glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_440);
576 	const PlanarFormatDescription	formatDescription	= getPlanarFormatDescription(params.format);
577 	const std::string				imageTypeStr		= getShaderImageType(formatDescription);
578 	const std::string				formatDataStr		= getShaderImageDataType(formatDescription);
579 	const tcu::UVec3				shaderGridSize		( params.size.x(), params.size.y(), params.size.z() );
580 
581 	std::vector<std::string>		formatValueStrings;
582 	switch (formatDescription.channels[0].type)
583 	{
584 	case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
585 	case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
586 		formatValueStrings = {
587 			"int(gl_GlobalInvocationID.x) % 127",
588 			"int(gl_GlobalInvocationID.y) % 127",
589 			"int(gl_GlobalInvocationID.z) % 127",
590 			"1"
591 		};
592 		break;
593 	case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
594 	case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
595 	case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
596 		formatValueStrings = {
597 			"float(int(gl_GlobalInvocationID.x) % 127) / 127.0" ,
598 			"float(int(gl_GlobalInvocationID.y) % 127) / 127.0",
599 			"float(int(gl_GlobalInvocationID.z) % 127) / 127.0",
600 			"1.0"
601 		};
602 		break;
603 	default:	DE_ASSERT(false);	break;
604 	}
605 
606 	for (deUint32 planeNdx = 0; planeNdx < formatDescription.numPlanes; ++planeNdx)
607 	{
608 		VkFormat						planeCompatibleFormat		= getPlaneCompatibleFormatForWriting(formatDescription, planeNdx);
609 		vk::PlanarFormatDescription		compatibleFormatDescription	= (planeCompatibleFormat != getPlaneCompatibleFormat(formatDescription, planeNdx)) ? getPlanarFormatDescription(planeCompatibleFormat) : formatDescription;
610 		VkExtent3D						compatibleShaderGridSize	{ shaderGridSize.x() / formatDescription.blockWidth, shaderGridSize.y() / formatDescription.blockHeight, shaderGridSize.z() / 1u };
611 
612 		std::vector<std::pair<deUint32, deUint32>> channelsOnPlane;
613 		for (deUint32 channelNdx = 0; channelNdx < 4; ++channelNdx)
614 		{
615 			if (!formatDescription.hasChannelNdx(channelNdx))
616 				continue;
617 			if (formatDescription.channels[channelNdx].planeNdx != planeNdx)
618 				continue;
619 			channelsOnPlane.push_back({ channelNdx,formatDescription.channels[channelNdx].offsetBits });
620 		}
621 		// reorder channels for multi-planar images
622 		if (formatDescription.numPlanes > 1)
623 			std::sort(begin(channelsOnPlane), end(channelsOnPlane), [](const std::pair<deUint32, deUint32>& lhs, const std::pair<deUint32, deUint32>& rhs) { return lhs.second < rhs.second; });
624 		std::string			formatValueStr		= getFormatValueString(channelsOnPlane, formatValueStrings);
625 		VkExtent3D			shaderExtent		= getPlaneExtent(compatibleFormatDescription, compatibleShaderGridSize, planeNdx, 0);
626 		const std::string	formatQualifierStr	= getShaderImageFormatQualifier(formatDescription.planes[planeNdx].planeCompatibleFormat);
627 		const tcu::UVec3	workGroupSize		= computeWorkGroupSize(shaderExtent);
628 
629 		std::ostringstream src;
630 		src << versionDecl << "\n"
631 			<< "layout (local_size_x = " << workGroupSize.x() << ", local_size_y = " << workGroupSize.y() << ", local_size_z = " << workGroupSize.z() << ") in; \n"
632 			<< "layout (binding = 0, " << formatQualifierStr << ") writeonly uniform highp " << imageTypeStr << " u_image;\n"
633 			<< "void main (void)\n"
634 			<< "{\n"
635 			<< "	if( gl_GlobalInvocationID.x < " << shaderExtent.width << " ) \n"
636 			<< "	if( gl_GlobalInvocationID.y < " << shaderExtent.height << " ) \n"
637 			<< "	if( gl_GlobalInvocationID.z < " << shaderExtent.depth << " ) \n"
638 			<< "	{\n"
639 			<< "		imageStore(u_image, ivec2( gl_GlobalInvocationID.x, gl_GlobalInvocationID.y ) ,"
640 			<< formatDataStr << formatValueStr << ");\n"
641 			<< "	}\n"
642 			<< "}\n";
643 		std::ostringstream shaderName;
644 		shaderName << "comp" << planeNdx;
645 		sourceCollections.glslSources.add(shaderName.str()) << glu::ComputeSource(src.str());
646 	}
647 }
648 
649 tcu::TestCaseGroup* populateStorageImageWriteFormatGroup (tcu::TestContext& testCtx, de::MovePtr<tcu::TestCaseGroup> testGroup)
650 {
651 	const std::vector<tcu::UVec3>	availableSizes{ tcu::UVec3(512u, 512u, 1u), tcu::UVec3(1024u, 128u, 1u), tcu::UVec3(66u, 32u, 1u) };
652 
653 	auto addTests = [&](int formatNdx)
654 	{
655 		const VkFormat					format				= (VkFormat)formatNdx;
656 		tcu::UVec3						imageSizeAlignment	= getImageSizeAlignment(format);
657 		std::string						formatName			= de::toLower(de::toString(format).substr(10));
658 		de::MovePtr<tcu::TestCaseGroup> formatGroup			( new tcu::TestCaseGroup(testCtx, formatName.c_str(), "") );
659 
660 		for (size_t sizeNdx = 0; sizeNdx < availableSizes.size(); sizeNdx++)
661 		{
662 			const tcu::UVec3 imageSize = availableSizes[sizeNdx];
663 
664 			// skip test for images with odd sizes for some YCbCr formats
665 			if ((imageSize.x() % imageSizeAlignment.x()) != 0)
666 				continue;
667 			if ((imageSize.y() % imageSizeAlignment.y()) != 0)
668 				continue;
669 
670 			std::ostringstream stream;
671 			stream << imageSize.x() << "_" << imageSize.y() << "_" << imageSize.z();
672 			de::MovePtr<tcu::TestCaseGroup> sizeGroup(new tcu::TestCaseGroup(testCtx, stream.str().c_str(), ""));
673 
674 			addFunctionCaseWithPrograms(sizeGroup.get(), "joint", "", checkSupport, initPrograms, testStorageImageWrite, TestParameters(format, imageSize, 0u));
675 			addFunctionCaseWithPrograms(sizeGroup.get(), "disjoint", "", checkSupport, initPrograms, testStorageImageWrite, TestParameters(format, imageSize, (VkImageCreateFlags)VK_IMAGE_CREATE_DISJOINT_BIT));
676 
677 			formatGroup->addChild(sizeGroup.release());
678 		}
679 		testGroup->addChild(formatGroup.release());
680 	};
681 
682 	for (int formatNdx = VK_YCBCR_FORMAT_FIRST; formatNdx < VK_YCBCR_FORMAT_LAST; formatNdx++)
683 	{
684 		addTests(formatNdx);
685 	}
686 
687 	for (int formatNdx = VK_FORMAT_G8_B8R8_2PLANE_444_UNORM_EXT; formatNdx <= VK_FORMAT_G16_B16R16_2PLANE_444_UNORM_EXT; formatNdx++)
688 	{
689 		addTests(formatNdx);
690 	}
691 
692 	return testGroup.release();
693 }
694 
695 } // namespace
696 
697 tcu::TestCaseGroup* createStorageImageWriteTests (tcu::TestContext& testCtx)
698 {
699 	de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "storage_image_write", "Writing to YCbCr storage images"));
700 	return populateStorageImageWriteFormatGroup(testCtx, testGroup);
701 }
702 
703 } // ycbcr
704 } // vkt
705