• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2019 Google Inc.
6  * Copyright (c) 2019 The Khronos Group Inc.
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 YCbCr Test Utilities
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktYCbCrUtil.hpp"
26 
27 #include "vkQueryUtil.hpp"
28 #include "vkRefUtil.hpp"
29 #include "vkTypeUtil.hpp"
30 #include "vkCmdUtil.hpp"
31 
32 #include "tcuTextureUtil.hpp"
33 #include "deMath.h"
34 #include "deFloat16.h"
35 #include "tcuVector.hpp"
36 #include "tcuVectorUtil.hpp"
37 
38 #include "deSTLUtil.hpp"
39 #include "deUniquePtr.hpp"
40 
41 #include <limits>
42 
43 namespace vkt
44 {
45 namespace ycbcr
46 {
47 
48 using namespace vk;
49 
50 using de::MovePtr;
51 using tcu::FloatFormat;
52 using tcu::Interval;
53 using tcu::IVec2;
54 using tcu::IVec4;
55 using tcu::UVec2;
56 using tcu::UVec4;
57 using tcu::Vec2;
58 using tcu::Vec4;
59 using std::vector;
60 using std::string;
61 
62 // MultiPlaneImageData
63 
MultiPlaneImageData(VkFormat format,const UVec2 & size)64 MultiPlaneImageData::MultiPlaneImageData (VkFormat format, const UVec2& size)
65 	: m_format		(format)
66 	, m_description	(getPlanarFormatDescription(format))
67 	, m_size		(size)
68 {
69 	for (deUint32 planeNdx = 0; planeNdx < m_description.numPlanes; ++planeNdx)
70 		m_planeData[planeNdx].resize(getPlaneSizeInBytes(m_description, size, planeNdx, 0, BUFFER_IMAGE_COPY_OFFSET_GRANULARITY));
71 }
72 
MultiPlaneImageData(const MultiPlaneImageData & other)73 MultiPlaneImageData::MultiPlaneImageData (const MultiPlaneImageData& other)
74 	: m_format		(other.m_format)
75 	, m_description	(other.m_description)
76 	, m_size		(other.m_size)
77 {
78 	for (deUint32 planeNdx = 0; planeNdx < m_description.numPlanes; ++planeNdx)
79 		m_planeData[planeNdx] = other.m_planeData[planeNdx];
80 }
81 
~MultiPlaneImageData(void)82 MultiPlaneImageData::~MultiPlaneImageData (void)
83 {
84 }
85 
getChannelAccess(deUint32 channelNdx)86 tcu::PixelBufferAccess MultiPlaneImageData::getChannelAccess (deUint32 channelNdx)
87 {
88 	void*		planePtrs[PlanarFormatDescription::MAX_PLANES];
89 	deUint32	planeRowPitches[PlanarFormatDescription::MAX_PLANES];
90 
91 	for (deUint32 planeNdx = 0; planeNdx < m_description.numPlanes; ++planeNdx)
92 	{
93 		const deUint32	planeW		= m_size.x() / ( m_description.blockWidth * m_description.planes[planeNdx].widthDivisor);
94 		planeRowPitches[planeNdx]	= m_description.planes[planeNdx].elementSizeBytes * planeW;
95 		planePtrs[planeNdx]			= &m_planeData[planeNdx][0];
96 	}
97 
98 	return vk::getChannelAccess(m_description,
99 								m_size,
100 								planeRowPitches,
101 								planePtrs,
102 								channelNdx);
103 }
104 
getChannelAccess(deUint32 channelNdx) const105 tcu::ConstPixelBufferAccess MultiPlaneImageData::getChannelAccess (deUint32 channelNdx) const
106 {
107 	const void*	planePtrs[PlanarFormatDescription::MAX_PLANES];
108 	deUint32	planeRowPitches[PlanarFormatDescription::MAX_PLANES];
109 
110 	for (deUint32 planeNdx = 0; planeNdx < m_description.numPlanes; ++planeNdx)
111 	{
112 		const deUint32	planeW		= m_size.x() / (m_description.blockWidth * m_description.planes[planeNdx].widthDivisor);
113 		planeRowPitches[planeNdx]	= m_description.planes[planeNdx].elementSizeBytes * planeW;
114 		planePtrs[planeNdx]			= &m_planeData[planeNdx][0];
115 	}
116 
117 	return vk::getChannelAccess(m_description,
118 								m_size,
119 								planeRowPitches,
120 								planePtrs,
121 								channelNdx);
122 }
123 
124 // Misc utilities
125 
126 namespace
127 {
128 
allocateStagingBuffers(const DeviceInterface & vkd,VkDevice device,Allocator & allocator,const MultiPlaneImageData & imageData,vector<VkBufferSp> * buffers,vector<AllocationSp> * allocations)129 void allocateStagingBuffers (const DeviceInterface&			vkd,
130 							 VkDevice						device,
131 							 Allocator&						allocator,
132 							 const MultiPlaneImageData&		imageData,
133 							 vector<VkBufferSp>*			buffers,
134 							 vector<AllocationSp>*			allocations)
135 {
136 	for (deUint32 planeNdx = 0; planeNdx < imageData.getDescription().numPlanes; ++planeNdx)
137 	{
138 		const VkBufferCreateInfo	bufferInfo	=
139 		{
140 			VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
141 			DE_NULL,
142 			(VkBufferCreateFlags)0u,
143 			(VkDeviceSize)imageData.getPlaneSize(planeNdx),
144 			VK_BUFFER_USAGE_TRANSFER_SRC_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT,
145 			VK_SHARING_MODE_EXCLUSIVE,
146 			0u,
147 			(const deUint32*)DE_NULL,
148 		};
149 		Move<VkBuffer>				buffer		(createBuffer(vkd, device, &bufferInfo));
150 		MovePtr<Allocation>			allocation	(allocator.allocate(getBufferMemoryRequirements(vkd, device, *buffer),
151 																	MemoryRequirement::HostVisible|MemoryRequirement::Any));
152 
153 		VK_CHECK(vkd.bindBufferMemory(device, *buffer, allocation->getMemory(), allocation->getOffset()));
154 
155 		buffers->push_back(VkBufferSp(new Unique<VkBuffer>(buffer)));
156 		allocations->push_back(AllocationSp(allocation.release()));
157 	}
158 }
159 
allocateAndWriteStagingBuffers(const DeviceInterface & vkd,VkDevice device,Allocator & allocator,const MultiPlaneImageData & imageData,vector<VkBufferSp> * buffers,vector<AllocationSp> * allocations)160 void allocateAndWriteStagingBuffers (const DeviceInterface&		vkd,
161 									  VkDevice						device,
162 									  Allocator&					allocator,
163 									  const MultiPlaneImageData&	imageData,
164 									  vector<VkBufferSp>*			buffers,
165 									  vector<AllocationSp>*			allocations)
166 {
167 	allocateStagingBuffers(vkd, device, allocator, imageData, buffers, allocations);
168 
169 	for (deUint32 planeNdx = 0; planeNdx < imageData.getDescription().numPlanes; ++planeNdx)
170 	{
171 		deMemcpy((*allocations)[planeNdx]->getHostPtr(), imageData.getPlanePtr(planeNdx), imageData.getPlaneSize(planeNdx));
172 		flushMappedMemoryRange(vkd, device, (*allocations)[planeNdx]->getMemory(), 0u, VK_WHOLE_SIZE);
173 	}
174 }
175 
readStagingBuffers(MultiPlaneImageData * imageData,const DeviceInterface & vkd,VkDevice device,const vector<AllocationSp> & allocations)176 void readStagingBuffers (MultiPlaneImageData*			imageData,
177 						 const DeviceInterface&			vkd,
178 						 VkDevice						device,
179 						 const vector<AllocationSp>&	allocations)
180 {
181 	for (deUint32 planeNdx = 0; planeNdx < imageData->getDescription().numPlanes; ++planeNdx)
182 	{
183 		invalidateMappedMemoryRange(vkd, device, allocations[planeNdx]->getMemory(), 0u, VK_WHOLE_SIZE);
184 		deMemcpy(imageData->getPlanePtr(planeNdx), allocations[planeNdx]->getHostPtr(), imageData->getPlaneSize(planeNdx));
185 	}
186 }
187 
188 } // anonymous
189 
checkImageSupport(Context & context,VkFormat format,VkImageCreateFlags createFlags,VkImageTiling tiling)190 void checkImageSupport (Context& context, VkFormat format, VkImageCreateFlags createFlags, VkImageTiling tiling)
191 {
192 	const bool													disjoint	= (createFlags & VK_IMAGE_CREATE_DISJOINT_BIT) != 0;
193 	const VkPhysicalDeviceSamplerYcbcrConversionFeatures		features	= context.getSamplerYcbcrConversionFeatures();
194 
195 	if (features.samplerYcbcrConversion == VK_FALSE)
196 		TCU_THROW(NotSupportedError, "samplerYcbcrConversion is not supported");
197 
198 	if (disjoint)
199 	{
200 		context.requireDeviceFunctionality("VK_KHR_bind_memory2");
201 		context.requireDeviceFunctionality("VK_KHR_get_memory_requirements2");
202 	}
203 
204 	{
205 		const VkFormatProperties	formatProperties	= getPhysicalDeviceFormatProperties(context.getInstanceInterface(),
206 																							context.getPhysicalDevice(),
207 																							format);
208 		const VkFormatFeatureFlags	featureFlags		= tiling == VK_IMAGE_TILING_OPTIMAL
209 														? formatProperties.optimalTilingFeatures
210 														: formatProperties.linearTilingFeatures;
211 
212 		if ((featureFlags & (VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT | VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT)) == 0)
213 			TCU_THROW(NotSupportedError, "YCbCr conversion is not supported for format");
214 
215 		if (disjoint && ((featureFlags & VK_FORMAT_FEATURE_DISJOINT_BIT) == 0))
216 			TCU_THROW(NotSupportedError, "Disjoint planes are not supported for format");
217 	}
218 }
219 
fillRandomNoNaN(de::Random * randomGen,deUint8 * const data,deUint32 size,const vk::VkFormat format)220 void fillRandomNoNaN(de::Random* randomGen, deUint8* const data, deUint32 size, const vk::VkFormat format)
221 {
222 	bool isFloat = false;
223 	deUint32 stride = 1;
224 
225 	switch (format)
226 	{
227 	case vk::VK_FORMAT_B10G11R11_UFLOAT_PACK32:
228 		isFloat = true;
229 		stride = 1;
230 		break;
231 	case vk::VK_FORMAT_R16_SFLOAT:
232 	case vk::VK_FORMAT_R16G16_SFLOAT:
233 	case vk::VK_FORMAT_R16G16B16_SFLOAT:
234 	case vk::VK_FORMAT_R16G16B16A16_SFLOAT:
235 		isFloat = true;
236 		stride = 2;
237 		break;
238 	case vk::VK_FORMAT_R32_SFLOAT:
239 	case vk::VK_FORMAT_R32G32_SFLOAT:
240 	case vk::VK_FORMAT_R32G32B32_SFLOAT:
241 	case vk::VK_FORMAT_R32G32B32A32_SFLOAT:
242 		isFloat = true;
243 		stride = 4;
244 		break;
245 	case vk::VK_FORMAT_R64_SFLOAT:
246 	case vk::VK_FORMAT_R64G64_SFLOAT:
247 	case vk::VK_FORMAT_R64G64B64_SFLOAT:
248 	case vk::VK_FORMAT_R64G64B64A64_SFLOAT:
249 		isFloat = true;
250 		stride = 8;
251 		break;
252 	default:
253 		stride = 1;
254 		break;
255 	}
256 
257 	if (isFloat) {
258 		deUint32 ndx = 0;
259 		for (; ndx < size - stride + 1; ndx += stride)
260 		{
261 			if (stride == 1) {
262 				// Set first bit of each channel to 0 to avoid NaNs, only format is B10G11R11
263 				const deUint8 mask[] = { 0x7F, 0xDF, 0xFB, 0xFF };
264 				// Apply mask for both endians
265 				data[ndx] = (randomGen->getUint8() & mask[ndx % 4]) & mask[3 - ndx % 4];
266 			}
267 			else if (stride == 2)
268 			{
269 				deFloat16* ptr = reinterpret_cast<deFloat16*>(&data[ndx]);
270 				*ptr = deFloat32To16(randomGen->getFloat());
271 			}
272 			else if (stride == 4)
273 			{
274 				float* ptr = reinterpret_cast<float*>(&data[ndx]);
275 				*ptr = randomGen->getFloat();
276 			}
277 			else if (stride == 8)
278 			{
279 				double* ptr = reinterpret_cast<double*>(&data[ndx]);
280 				*ptr = randomGen->getDouble();
281 			}
282 		}
283 		while (ndx < size) {
284 			data[ndx] = 0;
285 		}
286 	}
287 	else
288 	{
289 		for (deUint32 ndx = 0; ndx < size; ++ndx)
290 		{
291 			data[ndx] = randomGen->getUint8();
292 		}
293 	}
294 }
295 
296 // When noNan is true, fillRandom does not generate NaNs in float formats.
fillRandom(de::Random * randomGen,MultiPlaneImageData * imageData,const vk::VkFormat format,const bool noNan)297 void fillRandom (de::Random* randomGen, MultiPlaneImageData* imageData, const vk::VkFormat format, const bool noNan)
298 {
299 	for (deUint32 planeNdx = 0; planeNdx < imageData->getDescription().numPlanes; ++planeNdx)
300 	{
301 		const size_t	planeSize	= imageData->getPlaneSize(planeNdx);
302 		deUint8* const	planePtr	= (deUint8*)imageData->getPlanePtr(planeNdx);
303 
304 		if (noNan) {
305 			fillRandomNoNaN(randomGen, planePtr, (deUint32)planeSize, format);
306 		}
307 		else
308 		{
309 			for (size_t ndx = 0; ndx < planeSize; ++ndx)
310 			{
311 				planePtr[ndx] = randomGen->getUint8();
312 			}
313 		}
314 	}
315 }
316 
fillGradient(MultiPlaneImageData * imageData,const tcu::Vec4 & minVal,const tcu::Vec4 & maxVal)317 void fillGradient (MultiPlaneImageData* imageData, const tcu::Vec4& minVal, const tcu::Vec4& maxVal)
318 {
319 	const PlanarFormatDescription&	formatInfo	= imageData->getDescription();
320 
321 	// \todo [pyry] Optimize: no point in re-rendering source gradient for each channel.
322 
323 	for (deUint32 channelNdx = 0; channelNdx < 4; channelNdx++)
324 	{
325 		if (formatInfo.hasChannelNdx(channelNdx))
326 		{
327 			const tcu::PixelBufferAccess		channelAccess	= imageData->getChannelAccess(channelNdx);
328 			tcu::TextureLevel					tmpTexture		(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::FLOAT),  channelAccess.getWidth(), channelAccess.getHeight());
329 			const tcu::ConstPixelBufferAccess	tmpAccess		= tmpTexture.getAccess();
330 
331 			tcu::fillWithComponentGradients(tmpTexture, minVal, maxVal);
332 
333 			for (int y = 0; y < channelAccess.getHeight(); ++y)
334 			for (int x = 0; x < channelAccess.getWidth(); ++x)
335 			{
336 				channelAccess.setPixel(tcu::Vec4(tmpAccess.getPixel(x, y)[channelNdx]), x, y);
337 			}
338 		}
339 	}
340 }
341 
fillZero(MultiPlaneImageData * imageData)342 void fillZero (MultiPlaneImageData* imageData)
343 {
344 	for (deUint32 planeNdx = 0; planeNdx < imageData->getDescription().numPlanes; ++planeNdx)
345 		deMemset(imageData->getPlanePtr(planeNdx), 0, imageData->getPlaneSize(planeNdx));
346 }
347 
allocateAndBindImageMemory(const DeviceInterface & vkd,VkDevice device,Allocator & allocator,VkImage image,VkFormat format,VkImageCreateFlags createFlags,vk::MemoryRequirement requirement)348 vector<AllocationSp> allocateAndBindImageMemory (const DeviceInterface&	vkd,
349 												 VkDevice				device,
350 												 Allocator&				allocator,
351 												 VkImage				image,
352 												 VkFormat				format,
353 												 VkImageCreateFlags		createFlags,
354 												 vk::MemoryRequirement	requirement)
355 {
356 	vector<AllocationSp> allocations;
357 
358 	if ((createFlags & VK_IMAGE_CREATE_DISJOINT_BIT) != 0)
359 	{
360 		const deUint32	numPlanes	= getPlaneCount(format);
361 
362 		bindImagePlanesMemory(vkd, device, image, numPlanes, allocations, allocator, requirement);
363 	}
364 	else
365 	{
366 		const VkMemoryRequirements	reqs	= getImageMemoryRequirements(vkd, device, image);
367 
368 		allocations.push_back(AllocationSp(allocator.allocate(reqs, requirement).release()));
369 
370 		VK_CHECK(vkd.bindImageMemory(device, image, allocations.back()->getMemory(), allocations.back()->getOffset()));
371 	}
372 
373 	return allocations;
374 }
375 
uploadImage(const DeviceInterface & vkd,VkDevice device,deUint32 queueFamilyNdx,Allocator & allocator,VkImage image,const MultiPlaneImageData & imageData,VkAccessFlags nextAccess,VkImageLayout finalLayout,deUint32 arrayLayer)376 void uploadImage (const DeviceInterface&		vkd,
377 				  VkDevice						device,
378 				  deUint32						queueFamilyNdx,
379 				  Allocator&					allocator,
380 				  VkImage						image,
381 				  const MultiPlaneImageData&	imageData,
382 				  VkAccessFlags					nextAccess,
383 				  VkImageLayout					finalLayout,
384 				  deUint32						arrayLayer)
385 {
386 	const VkQueue					queue			= getDeviceQueue(vkd, device, queueFamilyNdx, 0u);
387 	const Unique<VkCommandPool>		cmdPool			(createCommandPool(vkd, device, (VkCommandPoolCreateFlags)0, queueFamilyNdx));
388 	const Unique<VkCommandBuffer>	cmdBuffer		(allocateCommandBuffer(vkd, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
389 	vector<VkBufferSp>				stagingBuffers;
390 	vector<AllocationSp>			stagingMemory;
391 
392 	const PlanarFormatDescription&	formatDesc		= imageData.getDescription();
393 
394 	allocateAndWriteStagingBuffers(vkd, device, allocator, imageData, &stagingBuffers, &stagingMemory);
395 
396 	beginCommandBuffer(vkd, *cmdBuffer);
397 
398 	for (deUint32 planeNdx = 0; planeNdx < imageData.getDescription().numPlanes; ++planeNdx)
399 	{
400 		const VkImageAspectFlagBits	aspect	= (formatDesc.numPlanes > 1)
401 											? getPlaneAspect(planeNdx)
402 											: VK_IMAGE_ASPECT_COLOR_BIT;
403 		const VkExtent3D imageExtent		= makeExtent3D(imageData.getSize().x(), imageData.getSize().y(), 1u);
404 		const VkExtent3D planeExtent		= getPlaneExtent(formatDesc, imageExtent, planeNdx, 0);
405 		const VkBufferImageCopy		copy	=
406 		{
407 			0u,		// bufferOffset
408 			0u,		// bufferRowLength
409 			0u,		// bufferImageHeight
410 			{ (VkImageAspectFlags)aspect, 0u, arrayLayer, 1u },
411 			makeOffset3D(0u, 0u, 0u),
412 			planeExtent
413 		};
414 
415 		{
416 			const VkImageMemoryBarrier		preCopyBarrier	=
417 				{
418 					VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
419 					DE_NULL,
420 					(VkAccessFlags)0,
421 					VK_ACCESS_TRANSFER_WRITE_BIT,
422 					VK_IMAGE_LAYOUT_UNDEFINED,
423 					VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
424 					VK_QUEUE_FAMILY_IGNORED,
425 					VK_QUEUE_FAMILY_IGNORED,
426 					image,
427 					{ (VkImageAspectFlags)aspect, 0u, 1u, arrayLayer, 1u }
428 				};
429 
430 			vkd.cmdPipelineBarrier(*cmdBuffer,
431 								   (VkPipelineStageFlags)VK_PIPELINE_STAGE_HOST_BIT,
432 								   (VkPipelineStageFlags)VK_PIPELINE_STAGE_TRANSFER_BIT,
433 								   (VkDependencyFlags)0u,
434 								   0u,
435 								   (const VkMemoryBarrier*)DE_NULL,
436 								   0u,
437 								   (const VkBufferMemoryBarrier*)DE_NULL,
438 								   1u,
439 								   &preCopyBarrier);
440 		}
441 
442 		vkd.cmdCopyBufferToImage(*cmdBuffer, **stagingBuffers[planeNdx], image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1u, &copy);
443 
444 		{
445 			const VkImageMemoryBarrier		postCopyBarrier	=
446 				{
447 					VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
448 					DE_NULL,
449 					VK_ACCESS_TRANSFER_WRITE_BIT,
450 					nextAccess,
451 					VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
452 					finalLayout,
453 					VK_QUEUE_FAMILY_IGNORED,
454 					VK_QUEUE_FAMILY_IGNORED,
455 					image,
456 					{ (VkImageAspectFlags)aspect, 0u, 1u, arrayLayer, 1u }
457 				};
458 
459 			vkd.cmdPipelineBarrier(*cmdBuffer,
460 								   (VkPipelineStageFlags)VK_PIPELINE_STAGE_TRANSFER_BIT,
461 								   (VkPipelineStageFlags)VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
462 								   (VkDependencyFlags)0u,
463 								   0u,
464 								   (const VkMemoryBarrier*)DE_NULL,
465 								   0u,
466 								   (const VkBufferMemoryBarrier*)DE_NULL,
467 								   1u,
468 								   &postCopyBarrier);
469 		}
470 
471 	}
472 
473 	endCommandBuffer(vkd, *cmdBuffer);
474 
475 	submitCommandsAndWait(vkd, device, queue, *cmdBuffer);
476 }
477 
fillImageMemory(const vk::DeviceInterface & vkd,vk::VkDevice device,deUint32 queueFamilyNdx,vk::VkImage image,const std::vector<de::SharedPtr<vk::Allocation>> & allocations,const MultiPlaneImageData & imageData,vk::VkAccessFlags nextAccess,vk::VkImageLayout finalLayout,deUint32 arrayLayer)478 void fillImageMemory (const vk::DeviceInterface&							vkd,
479 					  vk::VkDevice											device,
480 					  deUint32												queueFamilyNdx,
481 					  vk::VkImage											image,
482 					  const std::vector<de::SharedPtr<vk::Allocation> >&	allocations,
483 					  const MultiPlaneImageData&							imageData,
484 					  vk::VkAccessFlags										nextAccess,
485 					  vk::VkImageLayout										finalLayout,
486 					  deUint32												arrayLayer)
487 {
488 	const VkQueue					queue			= getDeviceQueue(vkd, device, queueFamilyNdx, 0u);
489 	const Unique<VkCommandPool>		cmdPool			(createCommandPool(vkd, device, (VkCommandPoolCreateFlags)0, queueFamilyNdx));
490 	const Unique<VkCommandBuffer>	cmdBuffer		(allocateCommandBuffer(vkd, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
491 	const PlanarFormatDescription&	formatDesc		= imageData.getDescription();
492 
493 	for (deUint32 planeNdx = 0; planeNdx < formatDesc.numPlanes; ++planeNdx)
494 	{
495 		const VkImageAspectFlagBits			aspect		= (formatDesc.numPlanes > 1)
496 														? getPlaneAspect(planeNdx)
497 														: VK_IMAGE_ASPECT_COLOR_BIT;
498 		const de::SharedPtr<Allocation>&	allocation	= allocations.size() > 1
499 														? allocations[planeNdx]
500 														: allocations[0];
501 		const size_t						planeSize	= imageData.getPlaneSize(planeNdx);
502 		const deUint32						planeH		= imageData.getSize().y() / formatDesc.planes[planeNdx].heightDivisor;
503 		const VkImageSubresource			subresource	=
504 		{
505 			static_cast<vk::VkImageAspectFlags>(aspect),
506 			0u,
507 			arrayLayer,
508 		};
509 		VkSubresourceLayout			layout;
510 
511 		vkd.getImageSubresourceLayout(device, image, &subresource, &layout);
512 
513 		for (deUint32 row = 0; row < planeH; ++row)
514 		{
515 			const size_t		rowSize		= planeSize / planeH;
516 			void* const			dstPtr		= ((deUint8*)allocation->getHostPtr()) + layout.offset + layout.rowPitch * row;
517 			const void* const	srcPtr		= ((const deUint8*)imageData.getPlanePtr(planeNdx)) + row * rowSize;
518 
519 			deMemcpy(dstPtr, srcPtr, rowSize);
520 		}
521 		flushMappedMemoryRange(vkd, device, allocation->getMemory(), 0u, VK_WHOLE_SIZE);
522 	}
523 
524 	beginCommandBuffer(vkd, *cmdBuffer);
525 
526 	{
527 		const VkImageMemoryBarrier		postCopyBarrier	=
528 		{
529 			VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
530 			DE_NULL,
531 			0u,
532 			nextAccess,
533 			VK_IMAGE_LAYOUT_PREINITIALIZED,
534 			finalLayout,
535 			VK_QUEUE_FAMILY_IGNORED,
536 			VK_QUEUE_FAMILY_IGNORED,
537 			image,
538 			{ VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, arrayLayer, 1u }
539 		};
540 
541 		vkd.cmdPipelineBarrier(*cmdBuffer,
542 								(VkPipelineStageFlags)VK_PIPELINE_STAGE_HOST_BIT,
543 								(VkPipelineStageFlags)VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
544 								(VkDependencyFlags)0u,
545 								0u,
546 								(const VkMemoryBarrier*)DE_NULL,
547 								0u,
548 								(const VkBufferMemoryBarrier*)DE_NULL,
549 								1u,
550 								&postCopyBarrier);
551 	}
552 
553 	endCommandBuffer(vkd, *cmdBuffer);
554 
555 	submitCommandsAndWait(vkd, device, queue, *cmdBuffer);
556 }
557 
downloadImage(const DeviceInterface & vkd,VkDevice device,deUint32 queueFamilyNdx,Allocator & allocator,VkImage image,MultiPlaneImageData * imageData,VkAccessFlags prevAccess,VkImageLayout initialLayout)558 void downloadImage (const DeviceInterface&	vkd,
559 					VkDevice				device,
560 					deUint32				queueFamilyNdx,
561 					Allocator&				allocator,
562 					VkImage					image,
563 					MultiPlaneImageData*	imageData,
564 					VkAccessFlags			prevAccess,
565 					VkImageLayout			initialLayout)
566 {
567 	const VkQueue					queue			= getDeviceQueue(vkd, device, queueFamilyNdx, 0u);
568 	const Unique<VkCommandPool>		cmdPool			(createCommandPool(vkd, device, (VkCommandPoolCreateFlags)0, queueFamilyNdx));
569 	const Unique<VkCommandBuffer>	cmdBuffer		(allocateCommandBuffer(vkd, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
570 	vector<VkBufferSp>				stagingBuffers;
571 	vector<AllocationSp>			stagingMemory;
572 
573 	const PlanarFormatDescription&	formatDesc		= imageData->getDescription();
574 
575 	allocateStagingBuffers(vkd, device, allocator, *imageData, &stagingBuffers, &stagingMemory);
576 
577 	beginCommandBuffer(vkd, *cmdBuffer);
578 
579 	for (deUint32 planeNdx = 0; planeNdx < imageData->getDescription().numPlanes; ++planeNdx)
580 	{
581 		const VkImageAspectFlagBits	aspect	= (formatDesc.numPlanes > 1)
582 											? getPlaneAspect(planeNdx)
583 											: VK_IMAGE_ASPECT_COLOR_BIT;
584 		{
585 			const VkImageMemoryBarrier		preCopyBarrier	=
586 			{
587 				VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
588 				DE_NULL,
589 				prevAccess,
590 				VK_ACCESS_TRANSFER_READ_BIT,
591 				initialLayout,
592 				VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
593 				VK_QUEUE_FAMILY_IGNORED,
594 				VK_QUEUE_FAMILY_IGNORED,
595 				image,
596 				{
597 					static_cast<vk::VkImageAspectFlags>(aspect),
598 					0u,
599 					1u,
600 					0u,
601 					1u
602 				}
603 			};
604 
605 			vkd.cmdPipelineBarrier(*cmdBuffer,
606 									(VkPipelineStageFlags)VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
607 									(VkPipelineStageFlags)VK_PIPELINE_STAGE_TRANSFER_BIT,
608 									(VkDependencyFlags)0u,
609 									0u,
610 									(const VkMemoryBarrier*)DE_NULL,
611 									0u,
612 									(const VkBufferMemoryBarrier*)DE_NULL,
613 									1u,
614 									&preCopyBarrier);
615 		}
616 		{
617 			const VkExtent3D imageExtent		= makeExtent3D(imageData->getSize().x(), imageData->getSize().y(), 1u);
618 			const VkExtent3D planeExtent		= getPlaneExtent(formatDesc, imageExtent, planeNdx, 0);
619 			const VkBufferImageCopy		copy	=
620 			{
621 				0u,		// bufferOffset
622 				0u,		// bufferRowLength
623 				0u,		// bufferImageHeight
624 				{ (VkImageAspectFlags)aspect, 0u, 0u, 1u },
625 				makeOffset3D(0u, 0u, 0u),
626 				planeExtent
627 			};
628 
629 			vkd.cmdCopyImageToBuffer(*cmdBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, **stagingBuffers[planeNdx], 1u, &copy);
630 		}
631 		{
632 			const VkBufferMemoryBarrier		postCopyBarrier	=
633 			{
634 				VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
635 				DE_NULL,
636 				VK_ACCESS_TRANSFER_WRITE_BIT,
637 				VK_ACCESS_HOST_READ_BIT,
638 				VK_QUEUE_FAMILY_IGNORED,
639 				VK_QUEUE_FAMILY_IGNORED,
640 				**stagingBuffers[planeNdx],
641 				0u,
642 				VK_WHOLE_SIZE
643 			};
644 
645 			vkd.cmdPipelineBarrier(*cmdBuffer,
646 									(VkPipelineStageFlags)VK_PIPELINE_STAGE_TRANSFER_BIT,
647 									(VkPipelineStageFlags)VK_PIPELINE_STAGE_HOST_BIT,
648 									(VkDependencyFlags)0u,
649 									0u,
650 									(const VkMemoryBarrier*)DE_NULL,
651 									1u,
652 									&postCopyBarrier,
653 									0u,
654 									(const VkImageMemoryBarrier*)DE_NULL);
655 		}
656 	}
657 
658 	endCommandBuffer(vkd, *cmdBuffer);
659 
660 	submitCommandsAndWait(vkd, device, queue, *cmdBuffer);
661 
662 	readStagingBuffers(imageData, vkd, device, stagingMemory);
663 }
664 
readImageMemory(const vk::DeviceInterface & vkd,vk::VkDevice device,deUint32 queueFamilyNdx,vk::VkImage image,const std::vector<de::SharedPtr<vk::Allocation>> & allocations,MultiPlaneImageData * imageData,vk::VkAccessFlags prevAccess,vk::VkImageLayout initialLayout)665 void readImageMemory (const vk::DeviceInterface&							vkd,
666 					  vk::VkDevice											device,
667 					  deUint32												queueFamilyNdx,
668 					  vk::VkImage											image,
669 					  const std::vector<de::SharedPtr<vk::Allocation> >&	allocations,
670 					  MultiPlaneImageData*									imageData,
671 					  vk::VkAccessFlags										prevAccess,
672 					  vk::VkImageLayout										initialLayout)
673 {
674 	const VkQueue					queue			= getDeviceQueue(vkd, device, queueFamilyNdx, 0u);
675 	const Unique<VkCommandPool>		cmdPool			(createCommandPool(vkd, device, (VkCommandPoolCreateFlags)0, queueFamilyNdx));
676 	const Unique<VkCommandBuffer>	cmdBuffer		(allocateCommandBuffer(vkd, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
677 	const PlanarFormatDescription&	formatDesc		= imageData->getDescription();
678 
679 	beginCommandBuffer(vkd, *cmdBuffer);
680 
681 	{
682 		const VkImageMemoryBarrier		preCopyBarrier	=
683 		{
684 			VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
685 			DE_NULL,
686 			prevAccess,
687 			vk::VK_ACCESS_HOST_READ_BIT,
688 			initialLayout,
689 			VK_IMAGE_LAYOUT_GENERAL,
690 			VK_QUEUE_FAMILY_IGNORED,
691 			VK_QUEUE_FAMILY_IGNORED,
692 			image,
693 			{ VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u }
694 		};
695 
696 		vkd.cmdPipelineBarrier(*cmdBuffer,
697 								(VkPipelineStageFlags)VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
698 								(VkPipelineStageFlags)VK_PIPELINE_STAGE_HOST_BIT,
699 								(VkDependencyFlags)0u,
700 								0u,
701 								(const VkMemoryBarrier*)DE_NULL,
702 								0u,
703 								(const VkBufferMemoryBarrier*)DE_NULL,
704 								1u,
705 								&preCopyBarrier);
706 	}
707 
708 	endCommandBuffer(vkd, *cmdBuffer);
709 
710 	submitCommandsAndWait(vkd, device, queue, *cmdBuffer);
711 
712 	for (deUint32 planeNdx = 0; planeNdx < formatDesc.numPlanes; ++planeNdx)
713 	{
714 		const VkImageAspectFlagBits			aspect		= (formatDesc.numPlanes > 1)
715 														? getPlaneAspect(planeNdx)
716 														: VK_IMAGE_ASPECT_COLOR_BIT;
717 		const de::SharedPtr<Allocation>&	allocation	= allocations.size() > 1
718 														? allocations[planeNdx]
719 														: allocations[0];
720 		const size_t						planeSize	= imageData->getPlaneSize(planeNdx);
721 		const deUint32						planeH		= imageData->getSize().y() / formatDesc.planes[planeNdx].heightDivisor;
722 		const VkImageSubresource			subresource	=
723 		{
724 			static_cast<vk::VkImageAspectFlags>(aspect),
725 			0u,
726 			0u,
727 		};
728 		VkSubresourceLayout			layout;
729 
730 		vkd.getImageSubresourceLayout(device, image, &subresource, &layout);
731 
732 		invalidateMappedMemoryRange(vkd, device, allocation->getMemory(), 0u, VK_WHOLE_SIZE);
733 
734 		for (deUint32 row = 0; row < planeH; ++row)
735 		{
736 			const size_t		rowSize	= planeSize / planeH;
737 			const void* const	srcPtr	= ((const deUint8*)allocation->getHostPtr()) + layout.offset + layout.rowPitch * row;
738 			void* const			dstPtr	= ((deUint8*)imageData->getPlanePtr(planeNdx)) + row * rowSize;
739 
740 			deMemcpy(dstPtr, srcPtr, rowSize);
741 		}
742 	}
743 }
744 
745 // ChannelAccess utilities
746 namespace
747 {
748 
749 //! Extend < 32b signed integer to 32b
signExtend(deUint32 src,int bits)750 inline deInt32 signExtend (deUint32 src, int bits)
751 {
752 	const deUint32 signBit = 1u << (bits-1);
753 
754 	src |= ~((src & signBit) - 1);
755 
756 	return (deInt32)src;
757 }
758 
divRoundUp(deUint32 a,deUint32 b)759 deUint32 divRoundUp (deUint32 a, deUint32 b)
760 {
761 	if (a % b == 0)
762 		return a / b;
763 	else
764 		return (a / b) + 1;
765 }
766 
767 // \todo Taken from tcuTexture.cpp
768 // \todo [2011-09-21 pyry] Move to tcutil?
769 template <typename T>
convertSatRte(float f)770 inline T convertSatRte (float f)
771 {
772 	// \note Doesn't work for 64-bit types
773 	DE_STATIC_ASSERT(sizeof(T) < sizeof(deUint64));
774 	DE_STATIC_ASSERT((-3 % 2 != 0) && (-4 % 2 == 0));
775 
776 	deInt64	minVal	= std::numeric_limits<T>::min();
777 	deInt64 maxVal	= std::numeric_limits<T>::max();
778 	float	q		= deFloatFrac(f);
779 	deInt64 intVal	= (deInt64)(f-q);
780 
781 	// Rounding.
782 	if (q == 0.5f)
783 	{
784 		if (intVal % 2 != 0)
785 			intVal++;
786 	}
787 	else if (q > 0.5f)
788 		intVal++;
789 	// else Don't add anything
790 
791 	// Saturate.
792 	intVal = de::max(minVal, de::min(maxVal, intVal));
793 
794 	return (T)intVal;
795 }
796 
797 } // anonymous
798 
ChannelAccess(tcu::TextureChannelClass channelClass,deUint8 channelSize,const tcu::IVec3 & size,const tcu::IVec3 & bitPitch,void * data,deUint32 bitOffset)799 ChannelAccess::ChannelAccess (tcu::TextureChannelClass	channelClass,
800 							  deUint8					channelSize,
801 							  const tcu::IVec3&			size,
802 							  const tcu::IVec3&			bitPitch,
803 							  void*						data,
804 							  deUint32					bitOffset)
805 	: m_channelClass	(channelClass)
806 	, m_channelSize		(channelSize)
807 	, m_size			(size)
808 	, m_bitPitch		(bitPitch)
809 	, m_data			((deUint8*)data + (bitOffset / 8))
810 	, m_bitOffset		(bitOffset % 8)
811 {
812 }
813 
getChannelUint(const tcu::IVec3 & pos) const814 deUint32 ChannelAccess::getChannelUint (const tcu::IVec3& pos) const
815 {
816 	DE_ASSERT(pos[0] < m_size[0]);
817 	DE_ASSERT(pos[1] < m_size[1]);
818 	DE_ASSERT(pos[2] < m_size[2]);
819 
820 	const deInt32			bitOffset	(m_bitOffset + tcu::dot(m_bitPitch, pos));
821 	const deUint8* const	firstByte	= ((const deUint8*)m_data) + (bitOffset / 8);
822 	const deUint32			byteCount	= divRoundUp((bitOffset + m_channelSize) - 8u * (bitOffset / 8u), 8u);
823 	const deUint32			mask		(m_channelSize == 32u ? ~0x0u : (0x1u << m_channelSize) - 1u);
824 	const deUint32			offset		= bitOffset % 8;
825 	deUint32				bits		= 0u;
826 
827 	deMemcpy(&bits, firstByte, byteCount);
828 
829 	return (bits >> offset) & mask;
830 }
831 
setChannel(const tcu::IVec3 & pos,deUint32 x)832 void ChannelAccess::setChannel (const tcu::IVec3& pos, deUint32 x)
833 {
834 	DE_ASSERT(pos[0] < m_size[0]);
835 	DE_ASSERT(pos[1] < m_size[1]);
836 	DE_ASSERT(pos[2] < m_size[2]);
837 
838 	const deInt32	bitOffset	(m_bitOffset + tcu::dot(m_bitPitch, pos));
839 	deUint8* const	firstByte	= ((deUint8*)m_data) + (bitOffset / 8);
840 	const deUint32	byteCount	= divRoundUp((bitOffset + m_channelSize) - 8u * (bitOffset / 8u), 8u);
841 	const deUint32	mask		(m_channelSize == 32u ? ~0x0u : (0x1u << m_channelSize) - 1u);
842 	const deUint32	offset		= bitOffset % 8;
843 
844 	const deUint32	bits		= (x & mask) << offset;
845 	deUint32		oldBits		= 0;
846 
847 	deMemcpy(&oldBits, firstByte, byteCount);
848 
849 	{
850 		const deUint32	newBits	= bits | (oldBits & (~(mask << offset)));
851 
852 		deMemcpy(firstByte, &newBits,  byteCount);
853 	}
854 }
855 
getChannel(const tcu::IVec3 & pos) const856 float ChannelAccess::getChannel (const tcu::IVec3& pos) const
857 {
858 	const deUint32	bits	(getChannelUint(pos));
859 
860 	switch (m_channelClass)
861 	{
862 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
863 			return (float)bits / (float)(m_channelSize == 32 ? ~0x0u : ((0x1u << m_channelSize) - 1u));
864 
865 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
866 			return (float)bits;
867 
868 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
869 			return de::max(-1.0f, (float)signExtend(bits, m_channelSize) / (float)((0x1u << (m_channelSize - 1u)) - 1u));
870 
871 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
872 			return (float)signExtend(bits, m_channelSize);
873 
874 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
875 			if (m_channelSize == 32)
876 				return tcu::Float32(bits).asFloat();
877 			else
878 			{
879 				DE_FATAL("Float type not supported");
880 				return -1.0f;
881 			}
882 
883 		default:
884 			DE_FATAL("Unknown texture channel class");
885 			return -1.0f;
886 	}
887 }
888 
getChannel(const tcu::FloatFormat & conversionFormat,const tcu::IVec3 & pos) const889 tcu::Interval ChannelAccess::getChannel (const tcu::FloatFormat&	conversionFormat,
890 										 const tcu::IVec3&			pos) const
891 {
892 	const deUint32	bits	(getChannelUint(pos));
893 
894 	switch (m_channelClass)
895 	{
896 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
897 			return conversionFormat.roundOut(conversionFormat.roundOut((double)bits, false)
898 											/ conversionFormat.roundOut((double)(m_channelSize == 32 ? ~0x0u : ((0x1u << m_channelSize) - 1u)), false), false);
899 
900 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
901 			return conversionFormat.roundOut((double)bits, false);
902 
903 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
904 		{
905 			const tcu::Interval result (conversionFormat.roundOut(conversionFormat.roundOut((double)signExtend(bits, m_channelSize), false)
906 																/ conversionFormat.roundOut((double)((0x1u << (m_channelSize - 1u)) - 1u), false), false));
907 
908 			return tcu::Interval(de::max(-1.0, result.lo()), de::max(-1.0, result.hi()));
909 		}
910 
911 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
912 			return conversionFormat.roundOut((double)signExtend(bits, m_channelSize), false);
913 
914 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
915 			if (m_channelSize == 32)
916 				return conversionFormat.roundOut(tcu::Float32(bits).asFloat(), false);
917 			else
918 			{
919 				DE_FATAL("Float type not supported");
920 				return tcu::Interval();
921 			}
922 
923 		default:
924 			DE_FATAL("Unknown texture channel class");
925 			return tcu::Interval();
926 	}
927 }
928 
setChannel(const tcu::IVec3 & pos,float x)929 void ChannelAccess::setChannel (const tcu::IVec3& pos, float x)
930 {
931 	DE_ASSERT(pos[0] < m_size[0]);
932 	DE_ASSERT(pos[1] < m_size[1]);
933 	DE_ASSERT(pos[2] < m_size[2]);
934 
935 	const deUint32	mask	(m_channelSize == 32u ? ~0x0u : (0x1u << m_channelSize) - 1u);
936 
937 	switch (m_channelClass)
938 	{
939 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
940 		{
941 			const deUint32	maxValue	(mask);
942 			const deUint32	value		(de::min(maxValue, (deUint32)convertSatRte<deUint32>(x * (float)maxValue)));
943 			setChannel(pos, value);
944 			break;
945 		}
946 
947 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
948 		{
949 			const deInt32	range	((0x1u << (m_channelSize - 1u)) - 1u);
950 			const deUint32	value	((deUint32)de::clamp<deInt32>(convertSatRte<deInt32>(x * (float)range), -range, range));
951 			setChannel(pos, value);
952 			break;
953 		}
954 
955 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
956 		{
957 			const deUint32	maxValue	(mask);
958 			const deUint32	value		(de::min(maxValue, (deUint32)x));
959 			setChannel(pos, value);
960 			break;
961 		}
962 
963 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
964 		{
965 			const deInt32	minValue	(-(deInt32)(1u << (m_channelSize - 1u)));
966 			const deInt32	maxValue	((deInt32)((1u << (m_channelSize - 1u)) - 1u));
967 			const deUint32	value		((deUint32)de::clamp((deInt32)x, minValue, maxValue));
968 			setChannel(pos, value);
969 			break;
970 		}
971 
972 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
973 		{
974 			if (m_channelSize == 32)
975 			{
976 				const deUint32	value		= tcu::Float32(x).bits();
977 				setChannel(pos, value);
978 			}
979 			else
980 				DE_FATAL("Float type not supported");
981 			break;
982 		}
983 
984 		default:
985 			DE_FATAL("Unknown texture channel class");
986 	}
987 }
988 
getChannelAccess(MultiPlaneImageData & data,const vk::PlanarFormatDescription & formatInfo,const UVec2 & size,int channelNdx)989 ChannelAccess getChannelAccess (MultiPlaneImageData&				data,
990 								const vk::PlanarFormatDescription&	formatInfo,
991 								const UVec2&						size,
992 								int									channelNdx)
993 {
994 	DE_ASSERT(formatInfo.hasChannelNdx(channelNdx));
995 
996 	const deUint32	planeNdx			= formatInfo.channels[channelNdx].planeNdx;
997 	const deUint32	valueOffsetBits		= formatInfo.channels[channelNdx].offsetBits;
998 	const deUint32	pixelStrideBytes	= formatInfo.channels[channelNdx].strideBytes;
999 	const deUint32	pixelStrideBits		= pixelStrideBytes * 8;
1000 	const deUint8	sizeBits			= formatInfo.channels[channelNdx].sizeBits;
1001 
1002 	DE_ASSERT(size.x() % (formatInfo.blockWidth * formatInfo.planes[planeNdx].widthDivisor) == 0);
1003 	DE_ASSERT(size.y() % (formatInfo.blockHeight * formatInfo.planes[planeNdx].heightDivisor) == 0);
1004 
1005 	deUint32		accessWidth			= size.x() / ( formatInfo.blockWidth * formatInfo.planes[planeNdx].widthDivisor );
1006 	const deUint32	accessHeight		= size.y() / ( formatInfo.blockHeight * formatInfo.planes[planeNdx].heightDivisor );
1007 	const deUint32	elementSizeBytes	= formatInfo.planes[planeNdx].elementSizeBytes;
1008 	const deUint32	rowPitch			= formatInfo.planes[planeNdx].elementSizeBytes * accessWidth;
1009 	const deUint32	rowPitchBits		= rowPitch * 8;
1010 
1011 	if (pixelStrideBytes != elementSizeBytes)
1012 	{
1013 		DE_ASSERT(elementSizeBytes % pixelStrideBytes == 0);
1014 		accessWidth *= elementSizeBytes/pixelStrideBytes;
1015 	}
1016 
1017 	return ChannelAccess((tcu::TextureChannelClass)formatInfo.channels[channelNdx].type, sizeBits, tcu::IVec3(accessWidth, accessHeight, 1u), tcu::IVec3((int)pixelStrideBits, (int)rowPitchBits, 0), data.getPlanePtr(planeNdx), (deUint32)valueOffsetBits);
1018 }
1019 
isXChromaSubsampled(vk::VkFormat format)1020 bool isXChromaSubsampled (vk::VkFormat format)
1021 {
1022 	switch (format)
1023 	{
1024 		case vk::VK_FORMAT_G8B8G8R8_422_UNORM:
1025 		case vk::VK_FORMAT_B8G8R8G8_422_UNORM:
1026 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1027 		case vk::VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1028 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
1029 		case vk::VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
1030 		case vk::VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:
1031 		case vk::VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:
1032 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
1033 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1034 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
1035 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
1036 		case vk::VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:
1037 		case vk::VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:
1038 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
1039 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
1040 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
1041 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
1042 		case vk::VK_FORMAT_G16B16G16R16_422_UNORM:
1043 		case vk::VK_FORMAT_B16G16R16G16_422_UNORM:
1044 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
1045 		case vk::VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
1046 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
1047 		case vk::VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
1048 			return true;
1049 
1050 		default:
1051 			return false;
1052 	}
1053 }
1054 
isYChromaSubsampled(vk::VkFormat format)1055 bool isYChromaSubsampled (vk::VkFormat format)
1056 {
1057 	switch (format)
1058 	{
1059 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1060 		case vk::VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1061 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
1062 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1063 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
1064 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
1065 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
1066 		case vk::VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
1067 			return true;
1068 
1069 		default:
1070 			return false;
1071 	}
1072 }
1073 
areLsb6BitsDontCare(vk::VkFormat srcFormat,vk::VkFormat dstFormat)1074 bool areLsb6BitsDontCare(vk::VkFormat srcFormat, vk::VkFormat dstFormat)
1075 {
1076 	if ((srcFormat == vk::VK_FORMAT_R10X6_UNORM_PACK16)	                        ||
1077 		(dstFormat == vk::VK_FORMAT_R10X6_UNORM_PACK16)                         ||
1078 		(srcFormat == vk::VK_FORMAT_R10X6G10X6_UNORM_2PACK16)                   ||
1079 		(dstFormat == vk::VK_FORMAT_R10X6G10X6_UNORM_2PACK16)                   ||
1080 		(srcFormat == vk::VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16)         ||
1081 		(dstFormat == vk::VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16)         ||
1082 		(srcFormat == vk::VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16)     ||
1083 		(dstFormat == vk::VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16)     ||
1084 		(srcFormat == vk::VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16)     ||
1085 		(dstFormat == vk::VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16)     ||
1086 		(srcFormat == vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16)  ||
1087 		(dstFormat == vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16)  ||
1088 		(srcFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16) ||
1089 		(dstFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16) ||
1090 		(srcFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16) ||
1091 		(dstFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16) ||
1092 		(srcFormat == vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16)  ||
1093 		(dstFormat == vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16)  ||
1094 		(srcFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16) ||
1095 		(dstFormat == vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16))
1096 	{
1097 		return true;
1098 	}
1099 
1100 	return false;
1101 }
1102 
areLsb4BitsDontCare(vk::VkFormat srcFormat,vk::VkFormat dstFormat)1103 bool areLsb4BitsDontCare(vk::VkFormat srcFormat, vk::VkFormat dstFormat)
1104 {
1105 	if ((srcFormat == vk::VK_FORMAT_R12X4_UNORM_PACK16)                         ||
1106 		(dstFormat == vk::VK_FORMAT_R12X4_UNORM_PACK16)                         ||
1107 		(srcFormat == vk::VK_FORMAT_R12X4G12X4_UNORM_2PACK16)                   ||
1108 		(dstFormat == vk::VK_FORMAT_R12X4G12X4_UNORM_2PACK16)                   ||
1109 		(srcFormat == vk::VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16)         ||
1110 		(dstFormat == vk::VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16)         ||
1111 		(srcFormat == vk::VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16)     ||
1112 		(dstFormat == vk::VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16)     ||
1113 		(srcFormat == vk::VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16)     ||
1114 		(dstFormat == vk::VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16)     ||
1115 		(srcFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16) ||
1116 		(dstFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16) ||
1117 		(srcFormat == vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16)  ||
1118 		(dstFormat == vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16)  ||
1119 		(srcFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16) ||
1120 		(dstFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16) ||
1121 		(srcFormat == vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16)  ||
1122 		(dstFormat == vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16)  ||
1123 		(srcFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16) ||
1124 		(dstFormat == vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16))
1125 	{
1126 		return true;
1127 	}
1128 
1129 	return false;
1130 }
1131 
1132 // \note Used for range expansion
getYCbCrBitDepth(vk::VkFormat format)1133 tcu::UVec4 getYCbCrBitDepth (vk::VkFormat format)
1134 {
1135 	switch (format)
1136 	{
1137 		case vk::VK_FORMAT_G8B8G8R8_422_UNORM:
1138 		case vk::VK_FORMAT_B8G8R8G8_422_UNORM:
1139 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1140 		case vk::VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1141 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
1142 		case vk::VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
1143 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
1144 		case vk::VK_FORMAT_G8_B8R8_2PLANE_444_UNORM_EXT:
1145 			return tcu::UVec4(8, 8, 8, 0);
1146 
1147 		case vk::VK_FORMAT_R10X6_UNORM_PACK16:
1148 			return tcu::UVec4(10, 0, 0, 0);
1149 
1150 		case vk::VK_FORMAT_R10X6G10X6_UNORM_2PACK16:
1151 			return tcu::UVec4(10, 10, 0, 0);
1152 
1153 		case vk::VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
1154 			return tcu::UVec4(10, 10, 10, 10);
1155 
1156 		case vk::VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:
1157 		case vk::VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:
1158 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
1159 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1160 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
1161 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
1162 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
1163 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16_EXT:
1164 			return tcu::UVec4(10, 10, 10, 0);
1165 
1166 		case vk::VK_FORMAT_R12X4_UNORM_PACK16:
1167 			return tcu::UVec4(12, 0, 0, 0);
1168 
1169 		case vk::VK_FORMAT_R12X4G12X4_UNORM_2PACK16:
1170 			return tcu::UVec4(12, 12, 0, 0);
1171 
1172 		case vk::VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16:
1173 		case vk::VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:
1174 		case vk::VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:
1175 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
1176 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
1177 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
1178 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
1179 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
1180 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16_EXT:
1181 			return tcu::UVec4(12, 12, 12, 12);
1182 
1183 		case vk::VK_FORMAT_G16B16G16R16_422_UNORM:
1184 		case vk::VK_FORMAT_B16G16R16G16_422_UNORM:
1185 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
1186 		case vk::VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
1187 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
1188 		case vk::VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
1189 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
1190 		case vk::VK_FORMAT_G16_B16R16_2PLANE_444_UNORM_EXT:
1191 			return tcu::UVec4(16, 16, 16, 0);
1192 
1193 		default:
1194 			return tcu::getTextureFormatBitDepth(vk::mapVkFormat(format)).cast<deUint32>();
1195 	}
1196 }
1197 
getPrecision(VkFormat format)1198 std::vector<tcu::FloatFormat> getPrecision (VkFormat format)
1199 {
1200 	std::vector<FloatFormat>	floatFormats;
1201 	UVec4						channelDepth	= getYCbCrBitDepth (format);
1202 
1203 	for (deUint32 channelIdx = 0; channelIdx < 4; channelIdx++)
1204 		floatFormats.push_back(tcu::FloatFormat(0, 0, channelDepth[channelIdx], false, tcu::YES));
1205 
1206 	return floatFormats;
1207 }
1208 
getYCbCrFormatChannelCount(vk::VkFormat format)1209 deUint32 getYCbCrFormatChannelCount (vk::VkFormat format)
1210 {
1211 	switch (format)
1212 	{
1213 		case vk::VK_FORMAT_A1R5G5B5_UNORM_PACK16:
1214 		case vk::VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1215 		case vk::VK_FORMAT_A2R10G10B10_UNORM_PACK32:
1216 		case vk::VK_FORMAT_A8B8G8R8_UNORM_PACK32:
1217 		case vk::VK_FORMAT_B4G4R4A4_UNORM_PACK16:
1218 		case vk::VK_FORMAT_B5G5R5A1_UNORM_PACK16:
1219 		case vk::VK_FORMAT_B8G8R8A8_UNORM:
1220 		case vk::VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
1221 		case vk::VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16:
1222 		case vk::VK_FORMAT_R16G16B16A16_UNORM:
1223 		case vk::VK_FORMAT_R4G4B4A4_UNORM_PACK16:
1224 		case vk::VK_FORMAT_R5G5B5A1_UNORM_PACK16:
1225 		case vk::VK_FORMAT_R8G8B8A8_UNORM:
1226 			return 4;
1227 
1228 		case vk::VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:
1229 		case vk::VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:
1230 		case vk::VK_FORMAT_B16G16R16G16_422_UNORM:
1231 		case vk::VK_FORMAT_B5G6R5_UNORM_PACK16:
1232 		case vk::VK_FORMAT_B8G8R8G8_422_UNORM:
1233 		case vk::VK_FORMAT_B8G8R8_UNORM:
1234 		case vk::VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:
1235 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1236 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
1237 		case vk::VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16_EXT:
1238 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
1239 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
1240 		case vk::VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
1241 		case vk::VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:
1242 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
1243 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
1244 		case vk::VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16_EXT:
1245 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
1246 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
1247 		case vk::VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
1248 		case vk::VK_FORMAT_G16B16G16R16_422_UNORM:
1249 		case vk::VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
1250 		case vk::VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
1251 		case vk::VK_FORMAT_G16_B16R16_2PLANE_444_UNORM_EXT:
1252 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
1253 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
1254 		case vk::VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
1255 		case vk::VK_FORMAT_G8B8G8R8_422_UNORM:
1256 		case vk::VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1257 		case vk::VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
1258 		case vk::VK_FORMAT_G8_B8R8_2PLANE_444_UNORM_EXT:
1259 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1260 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
1261 		case vk::VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
1262 		case vk::VK_FORMAT_R16G16B16_UNORM:
1263 		case vk::VK_FORMAT_R5G6B5_UNORM_PACK16:
1264 		case vk::VK_FORMAT_R8G8B8_UNORM:
1265 			return 3;
1266 
1267 		case vk::VK_FORMAT_R10X6G10X6_UNORM_2PACK16:
1268 		case vk::VK_FORMAT_R12X4G12X4_UNORM_2PACK16:
1269 			return 2;
1270 
1271 		case vk::VK_FORMAT_R10X6_UNORM_PACK16:
1272 		case vk::VK_FORMAT_R12X4_UNORM_PACK16:
1273 			return 1;
1274 
1275 		default:
1276 			DE_FATAL("Unknown number of channels");
1277 			return -1;
1278 	}
1279 }
1280 
1281 // YCbCr color conversion utilities
1282 namespace
1283 {
1284 
rangeExpandChroma(vk::VkSamplerYcbcrRange range,const tcu::FloatFormat & conversionFormat,const deUint32 bits,const tcu::Interval & sample)1285 tcu::Interval rangeExpandChroma (vk::VkSamplerYcbcrRange		range,
1286 								 const tcu::FloatFormat&		conversionFormat,
1287 								 const deUint32					bits,
1288 								 const tcu::Interval&			sample)
1289 {
1290 	const deUint32	values	(0x1u << bits);
1291 
1292 	switch (range)
1293 	{
1294 		case vk::VK_SAMPLER_YCBCR_RANGE_ITU_FULL:
1295 			return conversionFormat.roundOut(sample - conversionFormat.roundOut(tcu::Interval((double)(0x1u << (bits - 1u)) / (double)((0x1u << bits) - 1u)), false), false);
1296 
1297 		case vk::VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:
1298 		{
1299 			const tcu::Interval	a			(conversionFormat.roundOut(sample * tcu::Interval((double)(values - 1u)), false));
1300 			const tcu::Interval	dividend	(conversionFormat.roundOut(a - tcu::Interval((double)(128u * (0x1u << (bits - 8u)))), false));
1301 			const tcu::Interval	divisor		((double)(224u * (0x1u << (bits - 8u))));
1302 			const tcu::Interval	result		(conversionFormat.roundOut(dividend / divisor, false));
1303 
1304 			return result;
1305 		}
1306 
1307 		default:
1308 			DE_FATAL("Unknown YCbCrRange");
1309 			return tcu::Interval();
1310 	}
1311 }
1312 
rangeExpandLuma(vk::VkSamplerYcbcrRange range,const tcu::FloatFormat & conversionFormat,const deUint32 bits,const tcu::Interval & sample)1313 tcu::Interval rangeExpandLuma (vk::VkSamplerYcbcrRange		range,
1314 							   const tcu::FloatFormat&		conversionFormat,
1315 							   const deUint32				bits,
1316 							   const tcu::Interval&			sample)
1317 {
1318 	const deUint32	values	(0x1u << bits);
1319 
1320 	switch (range)
1321 	{
1322 		case vk::VK_SAMPLER_YCBCR_RANGE_ITU_FULL:
1323 			return conversionFormat.roundOut(sample, false);
1324 
1325 		case vk::VK_SAMPLER_YCBCR_RANGE_ITU_NARROW:
1326 		{
1327 			const tcu::Interval	a			(conversionFormat.roundOut(sample * tcu::Interval((double)(values - 1u)), false));
1328 			const tcu::Interval	dividend	(conversionFormat.roundOut(a - tcu::Interval((double)(16u * (0x1u << (bits - 8u)))), false));
1329 			const tcu::Interval	divisor		((double)(219u * (0x1u << (bits - 8u))));
1330 			const tcu::Interval	result		(conversionFormat.roundOut(dividend / divisor, false));
1331 
1332 			return result;
1333 		}
1334 
1335 		default:
1336 			DE_FATAL("Unknown YCbCrRange");
1337 			return tcu::Interval();
1338 	}
1339 }
1340 
clampMaybe(const tcu::Interval & x,double min,double max)1341 tcu::Interval clampMaybe (const tcu::Interval&	x,
1342 						  double				min,
1343 						  double				max)
1344 {
1345 	tcu::Interval result = x;
1346 
1347 	DE_ASSERT(min <= max);
1348 
1349 	if (x.lo() < min)
1350 		result = result | tcu::Interval(min);
1351 
1352 	if (x.hi() > max)
1353 		result = result | tcu::Interval(max);
1354 
1355 	return result;
1356 }
1357 
convertColor(vk::VkSamplerYcbcrModelConversion colorModel,vk::VkSamplerYcbcrRange range,const vector<tcu::FloatFormat> & conversionFormat,const tcu::UVec4 & bitDepth,const tcu::Interval input[4],tcu::Interval output[4])1358 void convertColor (vk::VkSamplerYcbcrModelConversion	colorModel,
1359 				   vk::VkSamplerYcbcrRange				range,
1360 				   const vector<tcu::FloatFormat>&		conversionFormat,
1361 				   const tcu::UVec4&					bitDepth,
1362 				   const tcu::Interval					input[4],
1363 				   tcu::Interval						output[4])
1364 {
1365 	switch (colorModel)
1366 	{
1367 		case vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY:
1368 		{
1369 			for (size_t ndx = 0; ndx < 4; ndx++)
1370 				output[ndx] = input[ndx];
1371 			break;
1372 		}
1373 
1374 		case vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY:
1375 		{
1376 			output[0] = clampMaybe(rangeExpandChroma(range, conversionFormat[0], bitDepth[0], input[0]), -0.5, 0.5);
1377 			output[1] = clampMaybe(rangeExpandLuma(range, conversionFormat[1], bitDepth[1], input[1]), 0.0, 1.0);
1378 			output[2] = clampMaybe(rangeExpandChroma(range, conversionFormat[2], bitDepth[2], input[2]), -0.5, 0.5);
1379 			output[3] = input[3];
1380 			break;
1381 		}
1382 
1383 		case vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601:
1384 		case vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709:
1385 		case vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020:
1386 		{
1387 			const tcu::Interval	y			(rangeExpandLuma(range, conversionFormat[1], bitDepth[1], input[1]));
1388 			const tcu::Interval	cr			(rangeExpandChroma(range, conversionFormat[0], bitDepth[0], input[0]));
1389 			const tcu::Interval	cb			(rangeExpandChroma(range, conversionFormat[2], bitDepth[2], input[2]));
1390 
1391 			const tcu::Interval	yClamped	(clampMaybe(y,   0.0, 1.0));
1392 			const tcu::Interval	crClamped	(clampMaybe(cr, -0.5, 0.5));
1393 			const tcu::Interval	cbClamped	(clampMaybe(cb, -0.5, 0.5));
1394 
1395 			if (colorModel == vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601)
1396 			{
1397 				output[0] = conversionFormat[0].roundOut(yClamped + conversionFormat[0].roundOut(1.402 * crClamped, false), false);
1398 				output[1] = conversionFormat[1].roundOut(conversionFormat[1].roundOut(yClamped - conversionFormat[1].roundOut((0.202008 / 0.587) * cbClamped, false), false) - conversionFormat[1].roundOut((0.419198 / 0.587) * crClamped, false), false);
1399 				output[2] = conversionFormat[2].roundOut(yClamped + conversionFormat[2].roundOut(1.772 * cbClamped, false), false);
1400 			}
1401 			else if (colorModel == vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709)
1402 			{
1403 				output[0] = conversionFormat[0].roundOut(yClamped + conversionFormat[0].roundOut(1.5748 * crClamped, false), false);
1404 				output[1] = conversionFormat[1].roundOut(conversionFormat[1].roundOut(yClamped - conversionFormat[1].roundOut((0.13397432 / 0.7152) * cbClamped, false), false) - conversionFormat[1].roundOut((0.33480248 / 0.7152) * crClamped, false), false);
1405 				output[2] = conversionFormat[2].roundOut(yClamped + conversionFormat[2].roundOut(1.8556 * cbClamped, false), false);
1406 			}
1407 			else
1408 			{
1409 				output[0] = conversionFormat[0].roundOut(yClamped + conversionFormat[0].roundOut(1.4746 * crClamped, false), false);
1410 				output[1] = conversionFormat[1].roundOut(conversionFormat[1].roundOut(yClamped - conversionFormat[1].roundOut(conversionFormat[1].roundOut(0.11156702 / 0.6780, false) * cbClamped, false), false) - conversionFormat[1].roundOut(conversionFormat[1].roundOut(0.38737742 / 0.6780, false) * crClamped, false), false);
1411 				output[2] = conversionFormat[2].roundOut(yClamped + conversionFormat[2].roundOut(1.8814 * cbClamped, false), false);
1412 			}
1413 			output[3] = input[3];
1414 			break;
1415 		}
1416 
1417 		default:
1418 			DE_FATAL("Unknown YCbCrModel");
1419 	}
1420 
1421 	if (colorModel != vk::VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
1422 	{
1423 		for (int ndx = 0; ndx < 3; ndx++)
1424 			output[ndx] = clampMaybe(output[ndx], 0.0, 1.0);
1425 	}
1426 }
1427 
mirror(int coord)1428 int mirror (int coord)
1429 {
1430 	return coord >= 0 ? coord : -(1 + coord);
1431 }
1432 
imod(int a,int b)1433 int imod (int a, int b)
1434 {
1435 	int m = a % b;
1436 	return m < 0 ? m + b : m;
1437 }
1438 
frac(const tcu::Interval & x)1439 tcu::Interval frac (const tcu::Interval& x)
1440 {
1441 	if (x.hi() - x.lo() >= 1.0)
1442 		return tcu::Interval(0.0, 1.0);
1443 	else
1444 	{
1445 		const tcu::Interval ret (deFrac(x.lo()), deFrac(x.hi()));
1446 
1447 		return ret;
1448 	}
1449 }
1450 
calculateUV(const tcu::FloatFormat & coordFormat,const tcu::Interval & st,const int size)1451 tcu::Interval calculateUV (const tcu::FloatFormat&	coordFormat,
1452 						   const tcu::Interval&		st,
1453 						   const int				size)
1454 {
1455 	return coordFormat.roundOut(coordFormat.roundOut(st, false) * tcu::Interval((double)size), false);
1456 }
1457 
calculateNearestIJRange(const tcu::FloatFormat & coordFormat,const tcu::Interval & uv)1458 tcu::IVec2 calculateNearestIJRange (const tcu::FloatFormat&	coordFormat,
1459 								    const tcu::Interval&	uv)
1460 {
1461 	const tcu::Interval	ij	(coordFormat.roundOut(coordFormat.roundOut(uv, false) - tcu::Interval(0.5), false));
1462 
1463 	return tcu::IVec2(deRoundToInt32(ij.lo() - coordFormat.ulp(ij.lo(), 1)), deRoundToInt32(ij.hi() + coordFormat.ulp(ij.hi(), 1)));
1464 }
1465 
1466 // Calculate range of pixel coordinates that can be used as lower coordinate for linear sampling
calculateLinearIJRange(const tcu::FloatFormat & coordFormat,const tcu::Interval & uv)1467 tcu::IVec2 calculateLinearIJRange (const tcu::FloatFormat&	coordFormat,
1468 								   const tcu::Interval&		uv)
1469 {
1470 	const tcu::Interval	ij	(coordFormat.roundOut(uv - tcu::Interval(0.5), false));
1471 
1472 	return tcu::IVec2(deFloorToInt32(ij.lo()), deFloorToInt32(ij.hi()));
1473 }
1474 
calculateIJRange(vk::VkFilter filter,const tcu::FloatFormat & coordFormat,const tcu::Interval & uv)1475 tcu::IVec2 calculateIJRange (vk::VkFilter				filter,
1476 							 const tcu::FloatFormat&	coordFormat,
1477 							 const tcu::Interval&		uv)
1478 {
1479 	DE_ASSERT(filter == vk::VK_FILTER_NEAREST || filter == vk::VK_FILTER_LINEAR);
1480 	return (filter == vk::VK_FILTER_LINEAR)	? calculateLinearIJRange(coordFormat, uv)
1481 											: calculateNearestIJRange(coordFormat, uv);
1482 }
1483 
calculateAB(const deUint32 subTexelPrecisionBits,const tcu::Interval & uv,int ij)1484 tcu::Interval calculateAB (const deUint32		subTexelPrecisionBits,
1485 						   const tcu::Interval&	uv,
1486 						   int					ij)
1487 {
1488 	const deUint32		subdivisions	= 0x1u << subTexelPrecisionBits;
1489 	const tcu::Interval	ab				(frac((uv - 0.5) & tcu::Interval((double)ij, (double)(ij + 1))));
1490 	const tcu::Interval	gridAB			(ab * tcu::Interval(subdivisions));
1491 	const tcu::Interval	rounded			(de::max(deFloor(gridAB.lo()) / subdivisions, 0.0) , de::min(deCeil(gridAB.hi()) / subdivisions, 1.0));
1492 
1493 	return rounded;
1494 }
1495 
lookupWrapped(const ChannelAccess & access,const tcu::FloatFormat & conversionFormat,vk::VkSamplerAddressMode addressModeU,vk::VkSamplerAddressMode addressModeV,const tcu::IVec2 & coord)1496 tcu::Interval lookupWrapped (const ChannelAccess&		access,
1497 							 const tcu::FloatFormat&	conversionFormat,
1498 							 vk::VkSamplerAddressMode	addressModeU,
1499 							 vk::VkSamplerAddressMode	addressModeV,
1500 							 const tcu::IVec2&			coord)
1501 {
1502 	return access.getChannel(conversionFormat,
1503 							 tcu::IVec3(wrap(addressModeU, coord.x(), access.getSize().x()), wrap(addressModeV, coord.y(), access.getSize().y()), 0));
1504 }
1505 
linearInterpolate(const tcu::FloatFormat & filteringFormat,const tcu::Interval & a,const tcu::Interval & b,const tcu::Interval & p00,const tcu::Interval & p10,const tcu::Interval & p01,const tcu::Interval & p11)1506 tcu::Interval linearInterpolate (const tcu::FloatFormat&	filteringFormat,
1507 								 const tcu::Interval&		a,
1508 								 const tcu::Interval&		b,
1509 								 const tcu::Interval&		p00,
1510 								 const tcu::Interval&		p10,
1511 								 const tcu::Interval&		p01,
1512 								 const tcu::Interval&		p11)
1513 {
1514 	const tcu::Interval	p[4] =
1515 	{
1516 		p00,
1517 		p10,
1518 		p01,
1519 		p11
1520 	};
1521 	tcu::Interval		result	(0.0);
1522 
1523 	for (size_t ndx = 0; ndx < 4; ndx++)
1524 	{
1525 		const tcu::Interval	weightA	(filteringFormat.roundOut((ndx % 2) == 0 ? (1.0 - a) : a, false));
1526 		const tcu::Interval	weightB	(filteringFormat.roundOut((ndx / 2) == 0 ? (1.0 - b) : b, false));
1527 		const tcu::Interval	weight	(filteringFormat.roundOut(weightA * weightB, false));
1528 
1529 		result = filteringFormat.roundOut(result + filteringFormat.roundOut(p[ndx] * weight, false), false);
1530 	}
1531 
1532 	return result;
1533 }
1534 
calculateImplicitChromaUV(const tcu::FloatFormat & coordFormat,vk::VkChromaLocation offset,const tcu::Interval & uv)1535 tcu::Interval calculateImplicitChromaUV (const tcu::FloatFormat&	coordFormat,
1536 										 vk::VkChromaLocation		offset,
1537 										 const tcu::Interval&		uv)
1538 {
1539 	if (offset == vk::VK_CHROMA_LOCATION_COSITED_EVEN)
1540 		return coordFormat.roundOut(0.5 * coordFormat.roundOut(uv + 0.5, false), false);
1541 	else
1542 		return coordFormat.roundOut(0.5 * uv, false);
1543 }
1544 
linearSample(const ChannelAccess & access,const tcu::FloatFormat & conversionFormat,const tcu::FloatFormat & filteringFormat,vk::VkSamplerAddressMode addressModeU,vk::VkSamplerAddressMode addressModeV,const tcu::IVec2 & coord,const tcu::Interval & a,const tcu::Interval & b)1545 tcu::Interval linearSample (const ChannelAccess&		access,
1546 						    const tcu::FloatFormat&		conversionFormat,
1547 						    const tcu::FloatFormat&		filteringFormat,
1548 						    vk::VkSamplerAddressMode	addressModeU,
1549 						    vk::VkSamplerAddressMode	addressModeV,
1550 						    const tcu::IVec2&			coord,
1551 						    const tcu::Interval&		a,
1552 						    const tcu::Interval&		b)
1553 {
1554 	return linearInterpolate(filteringFormat, a, b,
1555 									lookupWrapped(access, conversionFormat, addressModeU, addressModeV, coord + tcu::IVec2(0, 0)),
1556 									lookupWrapped(access, conversionFormat, addressModeU, addressModeV, coord + tcu::IVec2(1, 0)),
1557 									lookupWrapped(access, conversionFormat, addressModeU, addressModeV, coord + tcu::IVec2(0, 1)),
1558 									lookupWrapped(access, conversionFormat, addressModeU, addressModeV, coord + tcu::IVec2(1, 1)));
1559 }
1560 
reconstructLinearXChromaSample(const tcu::FloatFormat & filteringFormat,const tcu::FloatFormat & conversionFormat,vk::VkChromaLocation offset,vk::VkSamplerAddressMode addressModeU,vk::VkSamplerAddressMode addressModeV,const ChannelAccess & access,int i,int j)1561 tcu::Interval reconstructLinearXChromaSample (const tcu::FloatFormat&	filteringFormat,
1562 											  const tcu::FloatFormat&	conversionFormat,
1563 											  vk::VkChromaLocation		offset,
1564 											  vk::VkSamplerAddressMode	addressModeU,
1565 											  vk::VkSamplerAddressMode	addressModeV,
1566 											  const ChannelAccess&		access,
1567 											  int						i,
1568 											  int						j)
1569 {
1570 	const int subI	= offset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1571 					? divFloor(i, 2)
1572 					: (i % 2 == 0 ? divFloor(i, 2) - 1 : divFloor(i, 2));
1573 	const double a	= offset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1574 					? (i % 2 == 0 ? 0.0 : 0.5)
1575 					: (i % 2 == 0 ? 0.25 : 0.75);
1576 
1577 	const tcu::Interval A (filteringFormat.roundOut(       a  * lookupWrapped(access, conversionFormat, addressModeU, addressModeV, tcu::IVec2(subI, j)), false));
1578 	const tcu::Interval B (filteringFormat.roundOut((1.0 - a) * lookupWrapped(access, conversionFormat, addressModeU, addressModeV, tcu::IVec2(subI + 1, j)), false));
1579 	return filteringFormat.roundOut(A + B, false);
1580 }
1581 
reconstructLinearXYChromaSample(const tcu::FloatFormat & filteringFormat,const tcu::FloatFormat & conversionFormat,vk::VkChromaLocation xOffset,vk::VkChromaLocation yOffset,vk::VkSamplerAddressMode addressModeU,vk::VkSamplerAddressMode addressModeV,const ChannelAccess & access,int i,int j)1582 tcu::Interval reconstructLinearXYChromaSample (const tcu::FloatFormat&	filteringFormat,
1583 										  const tcu::FloatFormat&		conversionFormat,
1584 										  vk::VkChromaLocation			xOffset,
1585 										  vk::VkChromaLocation			yOffset,
1586 										  vk::VkSamplerAddressMode		addressModeU,
1587 										  vk::VkSamplerAddressMode		addressModeV,
1588 										  const ChannelAccess&			access,
1589 										  int							i,
1590 										  int							j)
1591 {
1592 	const int		subI	= xOffset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1593 							? divFloor(i, 2)
1594 							: (i % 2 == 0 ? divFloor(i, 2) - 1 : divFloor(i, 2));
1595 	const int		subJ	= yOffset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1596 							? divFloor(j, 2)
1597 							: (j % 2 == 0 ? divFloor(j, 2) - 1 : divFloor(j, 2));
1598 
1599 	const double	a		= xOffset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1600 							? (i % 2 == 0 ? 0.0 : 0.5)
1601 							: (i % 2 == 0 ? 0.25 : 0.75);
1602 	const double	b		= yOffset == vk::VK_CHROMA_LOCATION_COSITED_EVEN
1603 							? (j % 2 == 0 ? 0.0 : 0.5)
1604 							: (j % 2 == 0 ? 0.25 : 0.75);
1605 
1606 	return linearSample(access, conversionFormat, filteringFormat, addressModeU, addressModeV, tcu::IVec2(subI, subJ), a, b);
1607 }
1608 
swizzle(vk::VkComponentSwizzle swizzle,const ChannelAccess & identityPlane,const ChannelAccess & rPlane,const ChannelAccess & gPlane,const ChannelAccess & bPlane,const ChannelAccess & aPlane)1609 const ChannelAccess& swizzle (vk::VkComponentSwizzle	swizzle,
1610 							  const ChannelAccess&		identityPlane,
1611 							  const ChannelAccess&		rPlane,
1612 							  const ChannelAccess&		gPlane,
1613 							  const ChannelAccess&		bPlane,
1614 							  const ChannelAccess&		aPlane)
1615 {
1616 	switch (swizzle)
1617 	{
1618 		case vk::VK_COMPONENT_SWIZZLE_IDENTITY:	return identityPlane;
1619 		case vk::VK_COMPONENT_SWIZZLE_R:		return rPlane;
1620 		case vk::VK_COMPONENT_SWIZZLE_G:		return gPlane;
1621 		case vk::VK_COMPONENT_SWIZZLE_B:		return bPlane;
1622 		case vk::VK_COMPONENT_SWIZZLE_A:		return aPlane;
1623 
1624 		default:
1625 			DE_FATAL("Unsupported swizzle");
1626 			return identityPlane;
1627 	}
1628 }
1629 
1630 } // anonymous
1631 
wrap(vk::VkSamplerAddressMode addressMode,int coord,int size)1632 int wrap (vk::VkSamplerAddressMode	addressMode,
1633 		  int						coord,
1634 		  int						size)
1635 {
1636 	switch (addressMode)
1637 	{
1638 		case vk::VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
1639 			return (size - 1) - mirror(imod(coord, 2 * size) - size);
1640 
1641 		case vk::VK_SAMPLER_ADDRESS_MODE_REPEAT:
1642 			return imod(coord, size);
1643 
1644 		case vk::VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
1645 			return de::clamp(coord, 0, size - 1);
1646 
1647 		case vk::VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
1648 			return de::clamp(mirror(coord), 0, size - 1);
1649 
1650 		default:
1651 			DE_FATAL("Unknown wrap mode");
1652 			return ~0;
1653 	}
1654 }
1655 
divFloor(int a,int b)1656 int divFloor (int a, int b)
1657 {
1658 	if (a % b == 0)
1659 		return a / b;
1660 	else if (a > 0)
1661 		return a / b;
1662 	else
1663 		return (a / b) - 1;
1664 }
1665 
calculateBounds(const ChannelAccess & rPlane,const ChannelAccess & gPlane,const ChannelAccess & bPlane,const ChannelAccess & aPlane,const UVec4 & bitDepth,const vector<Vec2> & sts,const vector<FloatFormat> & filteringFormat,const vector<FloatFormat> & conversionFormat,const deUint32 subTexelPrecisionBits,vk::VkFilter filter,vk::VkSamplerYcbcrModelConversion colorModel,vk::VkSamplerYcbcrRange range,vk::VkFilter chromaFilter,vk::VkChromaLocation xChromaOffset,vk::VkChromaLocation yChromaOffset,const vk::VkComponentMapping & componentMapping,bool explicitReconstruction,vk::VkSamplerAddressMode addressModeU,vk::VkSamplerAddressMode addressModeV,std::vector<Vec4> & minBounds,std::vector<Vec4> & maxBounds,std::vector<Vec4> & uvBounds,std::vector<IVec4> & ijBounds)1666 void calculateBounds (const ChannelAccess&				rPlane,
1667 					  const ChannelAccess&				gPlane,
1668 					  const ChannelAccess&				bPlane,
1669 					  const ChannelAccess&				aPlane,
1670 					  const UVec4&						bitDepth,
1671 					  const vector<Vec2>&				sts,
1672 					  const vector<FloatFormat>&		filteringFormat,
1673 					  const vector<FloatFormat>&		conversionFormat,
1674 					  const deUint32					subTexelPrecisionBits,
1675 					  vk::VkFilter						filter,
1676 					  vk::VkSamplerYcbcrModelConversion	colorModel,
1677 					  vk::VkSamplerYcbcrRange			range,
1678 					  vk::VkFilter						chromaFilter,
1679 					  vk::VkChromaLocation				xChromaOffset,
1680 					  vk::VkChromaLocation				yChromaOffset,
1681 					  const vk::VkComponentMapping&		componentMapping,
1682 					  bool								explicitReconstruction,
1683 					  vk::VkSamplerAddressMode			addressModeU,
1684 					  vk::VkSamplerAddressMode			addressModeV,
1685 					  std::vector<Vec4>&				minBounds,
1686 					  std::vector<Vec4>&				maxBounds,
1687 					  std::vector<Vec4>&				uvBounds,
1688 					  std::vector<IVec4>&				ijBounds)
1689 {
1690 	const FloatFormat		highp			(-126, 127, 23, true,
1691 											 tcu::MAYBE,	// subnormals
1692 											 tcu::YES,		// infinities
1693 											 tcu::MAYBE);	// NaN
1694 	const FloatFormat		coordFormat		(-32, 32, 16, true);
1695 	const ChannelAccess&	rAccess			(swizzle(componentMapping.r, rPlane, rPlane, gPlane, bPlane, aPlane));
1696 	const ChannelAccess&	gAccess			(swizzle(componentMapping.g, gPlane, rPlane, gPlane, bPlane, aPlane));
1697 	const ChannelAccess&	bAccess			(swizzle(componentMapping.b, bPlane, rPlane, gPlane, bPlane, aPlane));
1698 	const ChannelAccess&	aAccess			(swizzle(componentMapping.a, aPlane, rPlane, gPlane, bPlane, aPlane));
1699 
1700 	const bool				subsampledX		= gAccess.getSize().x() > rAccess.getSize().x();
1701 	const bool				subsampledY		= gAccess.getSize().y() > rAccess.getSize().y();
1702 
1703 	minBounds.resize(sts.size(), Vec4(TCU_INFINITY));
1704 	maxBounds.resize(sts.size(), Vec4(-TCU_INFINITY));
1705 
1706 	uvBounds.resize(sts.size(), Vec4(TCU_INFINITY, -TCU_INFINITY, TCU_INFINITY, -TCU_INFINITY));
1707 	ijBounds.resize(sts.size(), IVec4(0x7FFFFFFF, -1 -0x7FFFFFFF, 0x7FFFFFFF, -1 -0x7FFFFFFF));
1708 
1709 	// Chroma plane sizes must match
1710 	DE_ASSERT(rAccess.getSize() == bAccess.getSize());
1711 
1712 	// Luma plane sizes must match
1713 	DE_ASSERT(gAccess.getSize() == aAccess.getSize());
1714 
1715 	// Luma plane size must match chroma plane or be twice as big
1716 	DE_ASSERT(rAccess.getSize().x() == gAccess.getSize().x() || 2 * rAccess.getSize().x() == gAccess.getSize().x());
1717 	DE_ASSERT(rAccess.getSize().y() == gAccess.getSize().y() || 2 * rAccess.getSize().y() == gAccess.getSize().y());
1718 
1719 	DE_ASSERT(filter == vk::VK_FILTER_NEAREST || filter == vk::VK_FILTER_LINEAR);
1720 	DE_ASSERT(chromaFilter == vk::VK_FILTER_NEAREST || chromaFilter == vk::VK_FILTER_LINEAR);
1721 	DE_ASSERT(subsampledX || !subsampledY);
1722 
1723 
1724 	for (size_t ndx = 0; ndx < sts.size(); ndx++)
1725 	{
1726 		const Vec2	st		(sts[ndx]);
1727 		Interval	bounds[4];
1728 
1729 		const Interval	u	(calculateUV(coordFormat, st[0], gAccess.getSize().x()));
1730 		const Interval	v	(calculateUV(coordFormat, st[1], gAccess.getSize().y()));
1731 
1732 		uvBounds[ndx][0] = (float)u.lo();
1733 		uvBounds[ndx][1] = (float)u.hi();
1734 
1735 		uvBounds[ndx][2] = (float)v.lo();
1736 		uvBounds[ndx][3] = (float)v.hi();
1737 
1738 		const IVec2	iRange	(calculateIJRange(filter, coordFormat, u));
1739 		const IVec2	jRange	(calculateIJRange(filter, coordFormat, v));
1740 
1741 		ijBounds[ndx][0] = iRange[0];
1742 		ijBounds[ndx][1] = iRange[1];
1743 
1744 		ijBounds[ndx][2] = jRange[0];
1745 		ijBounds[ndx][3] = jRange[1];
1746 
1747 		for (int j = jRange.x(); j <= jRange.y(); j++)
1748 		for (int i = iRange.x(); i <= iRange.y(); i++)
1749 		{
1750 			if (filter == vk::VK_FILTER_NEAREST)
1751 			{
1752 				const Interval	gValue	(lookupWrapped(gAccess, conversionFormat[1], addressModeU, addressModeV, IVec2(i, j)));
1753 				const Interval	aValue	(lookupWrapped(aAccess, conversionFormat[3], addressModeU, addressModeV, IVec2(i, j)));
1754 
1755 				if (explicitReconstruction || !(subsampledX || subsampledY))
1756 				{
1757 					Interval rValue, bValue;
1758 					if (chromaFilter == vk::VK_FILTER_NEAREST || !subsampledX)
1759 					{
1760 						// Reconstruct using nearest if needed, otherwise, just take what's already there.
1761 						const int subI = subsampledX ? i / 2 : i;
1762 						const int subJ = subsampledY ? j / 2 : j;
1763 						rValue = lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2(subI, subJ));
1764 						bValue = lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2(subI, subJ));
1765 					}
1766 					else // vk::VK_FILTER_LINEAR
1767 					{
1768 						if (subsampledY)
1769 						{
1770 							rValue = reconstructLinearXYChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, yChromaOffset, addressModeU, addressModeV, rAccess, i, j);
1771 							bValue = reconstructLinearXYChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, yChromaOffset, addressModeU, addressModeV, bAccess, i, j);
1772 						}
1773 						else
1774 						{
1775 							rValue = reconstructLinearXChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, addressModeU, addressModeV, rAccess, i, j);
1776 							bValue = reconstructLinearXChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, addressModeU, addressModeV, bAccess, i, j);
1777 						}
1778 					}
1779 
1780 					const Interval srcColor[] =
1781 					{
1782 						rValue,
1783 						gValue,
1784 						bValue,
1785 						aValue
1786 					};
1787 					Interval dstColor[4];
1788 
1789 					convertColor(colorModel, range, conversionFormat, bitDepth, srcColor, dstColor);
1790 
1791 					for (size_t compNdx = 0; compNdx < 4; compNdx++)
1792 						bounds[compNdx] |= highp.roundOut(dstColor[compNdx], false);
1793 				}
1794 				else
1795 				{
1796 					const Interval	chromaU	(subsampledX ? calculateImplicitChromaUV(coordFormat, xChromaOffset, u) : u);
1797 					const Interval	chromaV	(subsampledY ? calculateImplicitChromaUV(coordFormat, yChromaOffset, v) : v);
1798 
1799 					// Reconstructed chroma samples with implicit filtering
1800 					const IVec2	chromaIRange	(subsampledX ? calculateIJRange(chromaFilter, coordFormat, chromaU) : IVec2(i, i));
1801 					const IVec2	chromaJRange	(subsampledY ? calculateIJRange(chromaFilter, coordFormat, chromaV) : IVec2(j, j));
1802 
1803 					for (int chromaJ = chromaJRange.x(); chromaJ <= chromaJRange.y(); chromaJ++)
1804 					for (int chromaI = chromaIRange.x(); chromaI <= chromaIRange.y(); chromaI++)
1805 					{
1806 						Interval rValue, bValue;
1807 
1808 						if (chromaFilter == vk::VK_FILTER_NEAREST)
1809 						{
1810 							rValue = lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2(chromaI, chromaJ));
1811 							bValue = lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2(chromaI, chromaJ));
1812 						}
1813 						else // vk::VK_FILTER_LINEAR
1814 						{
1815 							const Interval	chromaA	(calculateAB(subTexelPrecisionBits, chromaU, chromaI));
1816 							const Interval	chromaB	(calculateAB(subTexelPrecisionBits, chromaV, chromaJ));
1817 
1818 							rValue = linearSample(rAccess, conversionFormat[0], filteringFormat[0], addressModeU, addressModeV, IVec2(chromaI, chromaJ), chromaA, chromaB);
1819 							bValue = linearSample(bAccess, conversionFormat[2], filteringFormat[2], addressModeU, addressModeV, IVec2(chromaI, chromaJ), chromaA, chromaB);
1820 						}
1821 
1822 						const Interval	srcColor[]	=
1823 						{
1824 							rValue,
1825 							gValue,
1826 							bValue,
1827 							aValue
1828 						};
1829 
1830 						Interval dstColor[4];
1831 						convertColor(colorModel, range, conversionFormat, bitDepth, srcColor, dstColor);
1832 
1833 						for (size_t compNdx = 0; compNdx < 4; compNdx++)
1834 							bounds[compNdx] |= highp.roundOut(dstColor[compNdx], false);
1835 					}
1836 				}
1837 			}
1838 			else // filter == vk::VK_FILTER_LINEAR
1839 			{
1840 				const Interval	lumaA		(calculateAB(subTexelPrecisionBits, u, i));
1841 				const Interval	lumaB		(calculateAB(subTexelPrecisionBits, v, j));
1842 
1843 				const Interval	gValue		(linearSample(gAccess, conversionFormat[1], filteringFormat[1], addressModeU, addressModeV, IVec2(i, j), lumaA, lumaB));
1844 				const Interval	aValue		(linearSample(aAccess, conversionFormat[3], filteringFormat[3], addressModeU, addressModeV, IVec2(i, j), lumaA, lumaB));
1845 
1846 				if (explicitReconstruction || !(subsampledX || subsampledY))
1847 				{
1848 					Interval rValue, bValue;
1849 					if (chromaFilter == vk::VK_FILTER_NEAREST || !subsampledX)
1850 					{
1851 						rValue = linearInterpolate(filteringFormat[0], lumaA, lumaB,
1852 													lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2(i       / (subsampledX ? 2 : 1), j       / (subsampledY ? 2 : 1))),
1853 													lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2((i + 1) / (subsampledX ? 2 : 1), j       / (subsampledY ? 2 : 1))),
1854 													lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2(i       / (subsampledX ? 2 : 1), (j + 1) / (subsampledY ? 2 : 1))),
1855 													lookupWrapped(rAccess, conversionFormat[0], addressModeU, addressModeV, IVec2((i + 1) / (subsampledX ? 2 : 1), (j + 1) / (subsampledY ? 2 : 1))));
1856 						bValue = linearInterpolate(filteringFormat[2], lumaA, lumaB,
1857 													lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2(i       / (subsampledX ? 2 : 1), j       / (subsampledY ? 2 : 1))),
1858 													lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2((i + 1) / (subsampledX ? 2 : 1), j       / (subsampledY ? 2 : 1))),
1859 													lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2(i       / (subsampledX ? 2 : 1), (j + 1) / (subsampledY ? 2 : 1))),
1860 													lookupWrapped(bAccess, conversionFormat[2], addressModeU, addressModeV, IVec2((i + 1) / (subsampledX ? 2 : 1), (j + 1) / (subsampledY ? 2 : 1))));
1861 					}
1862 					else // vk::VK_FILTER_LINEAR
1863 					{
1864 						if (subsampledY)
1865 						{
1866 							// Linear, Reconstructed xx chroma samples with explicit linear filtering
1867 							rValue = linearInterpolate(filteringFormat[0], lumaA, lumaB,
1868 														reconstructLinearXYChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, yChromaOffset, addressModeU, addressModeV, rAccess, i, j),
1869 														reconstructLinearXYChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, yChromaOffset, addressModeU, addressModeV, rAccess, i + 1, j),
1870 														reconstructLinearXYChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, yChromaOffset, addressModeU, addressModeV, rAccess, i , j + 1),
1871 														reconstructLinearXYChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, yChromaOffset, addressModeU, addressModeV, rAccess, i + 1, j + 1));
1872 							bValue = linearInterpolate(filteringFormat[2], lumaA, lumaB,
1873 														reconstructLinearXYChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, yChromaOffset, addressModeU, addressModeV, bAccess, i, j),
1874 														reconstructLinearXYChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, yChromaOffset, addressModeU, addressModeV, bAccess, i + 1, j),
1875 														reconstructLinearXYChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, yChromaOffset, addressModeU, addressModeV, bAccess, i , j + 1),
1876 														reconstructLinearXYChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, yChromaOffset, addressModeU, addressModeV, bAccess, i + 1, j + 1));
1877 						}
1878 						else
1879 						{
1880 							// Linear, Reconstructed x chroma samples with explicit linear filtering
1881 							rValue = linearInterpolate(filteringFormat[0], lumaA, lumaB,
1882 														reconstructLinearXChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, addressModeU, addressModeV, rAccess, i, j),
1883 														reconstructLinearXChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, addressModeU, addressModeV, rAccess, i + 1, j),
1884 														reconstructLinearXChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, addressModeU, addressModeV, rAccess, i , j + 1),
1885 														reconstructLinearXChromaSample(filteringFormat[0], conversionFormat[0], xChromaOffset, addressModeU, addressModeV, rAccess, i + 1, j + 1));
1886 							bValue = linearInterpolate(filteringFormat[2], lumaA, lumaB,
1887 														reconstructLinearXChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, addressModeU, addressModeV, bAccess, i, j),
1888 														reconstructLinearXChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, addressModeU, addressModeV, bAccess, i + 1, j),
1889 														reconstructLinearXChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, addressModeU, addressModeV, bAccess, i , j + 1),
1890 														reconstructLinearXChromaSample(filteringFormat[2], conversionFormat[2], xChromaOffset, addressModeU, addressModeV, bAccess, i + 1, j + 1));
1891 						}
1892 					}
1893 
1894 					const Interval	srcColor[]	=
1895 					{
1896 						rValue,
1897 						gValue,
1898 						bValue,
1899 						aValue
1900 					};
1901 					Interval dstColor[4];
1902 
1903 					convertColor(colorModel, range, conversionFormat, bitDepth, srcColor, dstColor);
1904 
1905 					for (size_t compNdx = 0; compNdx < 4; compNdx++)
1906 						bounds[compNdx] |= highp.roundOut(dstColor[compNdx], false);
1907 				}
1908 				else
1909 				{
1910 					const Interval	chromaU	(subsampledX ? calculateImplicitChromaUV(coordFormat, xChromaOffset, u) : u);
1911 					const Interval	chromaV	(subsampledY ? calculateImplicitChromaUV(coordFormat, yChromaOffset, v) : v);
1912 
1913 					// TODO: It looks incorrect to ignore the chroma filter here. Is it?
1914 					const IVec2	chromaIRange	(calculateNearestIJRange(coordFormat, chromaU));
1915 					const IVec2	chromaJRange	(calculateNearestIJRange(coordFormat, chromaV));
1916 
1917 					for (int chromaJ = chromaJRange.x(); chromaJ <= chromaJRange.y(); chromaJ++)
1918 					for (int chromaI = chromaIRange.x(); chromaI <= chromaIRange.y(); chromaI++)
1919 					{
1920 						Interval rValue, bValue;
1921 
1922 						if (chromaFilter == vk::VK_FILTER_NEAREST)
1923 						{
1924 							rValue = lookupWrapped(rAccess, conversionFormat[1], addressModeU, addressModeV, IVec2(chromaI, chromaJ));
1925 							bValue = lookupWrapped(bAccess, conversionFormat[3], addressModeU, addressModeV, IVec2(chromaI, chromaJ));
1926 						}
1927 						else // vk::VK_FILTER_LINEAR
1928 						{
1929 							const Interval	chromaA	(calculateAB(subTexelPrecisionBits, chromaU, chromaI));
1930 							const Interval	chromaB	(calculateAB(subTexelPrecisionBits, chromaV, chromaJ));
1931 
1932 							rValue = linearSample(rAccess, conversionFormat[0], filteringFormat[0], addressModeU, addressModeV, IVec2(chromaI, chromaJ), chromaA, chromaB);
1933 							bValue = linearSample(bAccess, conversionFormat[2], filteringFormat[2], addressModeU, addressModeV, IVec2(chromaI, chromaJ), chromaA, chromaB);
1934 						}
1935 
1936 						const Interval	srcColor[]	=
1937 						{
1938 							rValue,
1939 							gValue,
1940 							bValue,
1941 							aValue
1942 						};
1943 						Interval dstColor[4];
1944 						convertColor(colorModel, range, conversionFormat, bitDepth, srcColor, dstColor);
1945 
1946 						for (size_t compNdx = 0; compNdx < 4; compNdx++)
1947 							bounds[compNdx] |= highp.roundOut(dstColor[compNdx], false);
1948 					}
1949 				}
1950 			}
1951 		}
1952 
1953 		minBounds[ndx] = Vec4((float)bounds[0].lo(), (float)bounds[1].lo(), (float)bounds[2].lo(), (float)bounds[3].lo());
1954 		maxBounds[ndx] = Vec4((float)bounds[0].hi(), (float)bounds[1].hi(), (float)bounds[2].hi(), (float)bounds[3].hi());
1955 	}
1956 }
1957 
1958 } // ycbcr
1959 
1960 } // vkt
1961