1#ifndef _VKTYCBCRUTIL_HPP
2#define _VKTYCBCRUTIL_HPP
3/*-------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
6 *
7 * Copyright (c) 2017 Google Inc.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief YCbCr Test Utilities
24 *//*--------------------------------------------------------------------*/
25
26#include "tcuDefs.hpp"
27
28#include "vktTestCase.hpp"
29
30#include "vkImageUtil.hpp"
31#include "vkMemUtil.hpp"
32#include "vkRef.hpp"
33
34#include "deSharedPtr.hpp"
35#include "deRandom.hpp"
36
37#include "tcuTextureUtil.hpp"
38#include "tcuFloatFormat.hpp"
39#include "tcuFloat.hpp"
40#include "tcuInterval.hpp"
41#include "tcuFloatFormat.hpp"
42#include "tcuFloat.hpp"
43
44#include <vector>
45
46namespace vkt
47{
48namespace ycbcr
49{
50
51#define VK_YCBCR_FORMAT_FIRST	VK_FORMAT_G8B8G8R8_422_UNORM
52#define VK_YCBCR_FORMAT_LAST	((vk::VkFormat)(VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM+1))
53
54class MultiPlaneImageData
55{
56public:
57										MultiPlaneImageData		(vk::VkFormat format, const tcu::UVec2& size);
58										MultiPlaneImageData		(const MultiPlaneImageData&);
59										~MultiPlaneImageData	(void);
60
61	vk::VkFormat						getFormat				(void) const				{ return m_format;						}
62	const vk::PlanarFormatDescription&	getDescription			(void) const				{ return m_description;					}
63	const tcu::UVec2&					getSize					(void) const				{ return m_size;						}
64
65	size_t								getPlaneSize			(deUint32 planeNdx) const	{ return m_planeData[planeNdx].size();	}
66	void*								getPlanePtr				(deUint32 planeNdx)			{ return &m_planeData[planeNdx][0];		}
67	const void*							getPlanePtr				(deUint32 planeNdx) const	{ return &m_planeData[planeNdx][0];		}
68
69	tcu::PixelBufferAccess				getChannelAccess		(deUint32 channelNdx);
70	tcu::ConstPixelBufferAccess			getChannelAccess		(deUint32 channelNdx) const;
71
72private:
73	MultiPlaneImageData&				operator=				(const MultiPlaneImageData&);
74
75	const vk::VkFormat					m_format;
76	const vk::PlanarFormatDescription	m_description;
77	const tcu::UVec2					m_size;
78
79	std::vector<deUint8>				m_planeData[vk::PlanarFormatDescription::MAX_PLANES];
80};
81
82void										checkImageSupport			(Context& context, vk::VkFormat format, vk::VkImageCreateFlags createFlags, vk::VkImageTiling tiling = vk::VK_IMAGE_TILING_OPTIMAL);
83
84void										fillRandom					(de::Random* randomGen, MultiPlaneImageData* imageData);
85void										fillGradient				(MultiPlaneImageData* imageData, const tcu::Vec4& minVal, const tcu::Vec4& maxVal);
86
87std::vector<de::SharedPtr<vk::Allocation> >	allocateAndBindImageMemory	(const vk::DeviceInterface&	vkd,
88																		 vk::VkDevice				device,
89																		 vk::Allocator&				allocator,
90																		 vk::VkImage				image,
91																		 vk::VkFormat				format,
92																		 vk::VkImageCreateFlags		createFlags,
93																		 vk::MemoryRequirement		requirement = vk::MemoryRequirement::Any);
94
95void										uploadImage					(const vk::DeviceInterface&	vkd,
96																		 vk::VkDevice				device,
97																		 deUint32					queueFamilyNdx,
98																		 vk::Allocator&				allocator,
99																		 vk::VkImage				image,
100																		 const MultiPlaneImageData&	imageData,
101																		 vk::VkAccessFlags			nextAccess,
102																		 vk::VkImageLayout			finalLayout);
103
104void										fillImageMemory				(const vk::DeviceInterface&							vkd,
105																		 vk::VkDevice										device,
106																		 deUint32											queueFamilyNdx,
107																		 vk::VkImage										image,
108																		 const std::vector<de::SharedPtr<vk::Allocation> >&	memory,
109																		 const MultiPlaneImageData&							imageData,
110																		 vk::VkAccessFlags									nextAccess,
111																		 vk::VkImageLayout									finalLayout);
112
113void										downloadImage				(const vk::DeviceInterface&	vkd,
114																		 vk::VkDevice				device,
115																		 deUint32					queueFamilyNdx,
116																		 vk::Allocator&				allocator,
117																		 vk::VkImage				image,
118																		 MultiPlaneImageData*		imageData,
119																		 vk::VkAccessFlags			prevAccess,
120																		 vk::VkImageLayout			initialLayout);
121
122void										readImageMemory				(const vk::DeviceInterface&							vkd,
123																		 vk::VkDevice										device,
124																		 deUint32											queueFamilyNdx,
125																		 vk::VkImage										image,
126																		 const std::vector<de::SharedPtr<vk::Allocation> >&	memory,
127																		 MultiPlaneImageData*								imageData,
128																		 vk::VkAccessFlags									prevAccess,
129																		 vk::VkImageLayout									initialLayout);
130
131class ChannelAccess
132{
133public:
134						ChannelAccess	(tcu::TextureChannelClass	channelClass,
135										 deUint8					channelSize,
136										 const tcu::IVec3&			size,
137										 const tcu::IVec3&			bitPitch,
138										 void*						data,
139										 deUint32					bitOffset);
140
141	const tcu::IVec3&	getSize			(void) const { return m_size; }
142	const tcu::IVec3&	getBitPitch		(void) const { return m_bitPitch; }
143	void*				getDataPtr		(void) const { return m_data; }
144
145	tcu::Interval		getChannel		(const tcu::FloatFormat&	conversionFormat,
146										 const tcu::IVec3&			pos) const;
147	deUint32			getChannelUint	(const tcu::IVec3&			pos) const;
148	float				getChannel		(const tcu::IVec3&			pos) const;
149	void				setChannel		(const tcu::IVec3&			pos,
150										 deUint32					x);
151	void				setChannel		(const tcu::IVec3&			pos,
152										 float						x);
153
154private:
155	const tcu::TextureChannelClass	m_channelClass;
156	const deUint8					m_channelSize;
157	const tcu::IVec3				m_size;
158	const tcu::IVec3				m_bitPitch;
159	void* const						m_data;
160	const deInt32					m_bitOffset;
161};
162
163ChannelAccess			getChannelAccess			(ycbcr::MultiPlaneImageData&			data,
164													 const vk::PlanarFormatDescription&		formatInfo,
165													 const tcu::UVec2&						size,
166													 int									channelNdx);
167bool					isYChromaSubsampled			(vk::VkFormat							format);
168bool					isXChromaSubsampled			(vk::VkFormat							format);
169
170tcu::UVec4				getYCbCrBitDepth			(vk::VkFormat							format);
171tcu::FloatFormat		getYCbCrFilteringPrecision	(vk::VkFormat							format);
172tcu::FloatFormat		getYCbCrConversionPrecision	(vk::VkFormat							format);
173deUint32				getYCbCrFormatChannelCount	(vk::VkFormat							format);
174
175int						wrap						(vk::VkSamplerAddressMode addressMode, int coord, int size);
176int						divFloor					(int a, int b);
177
178void					calculateBounds				(const ChannelAccess&					rPlane,
179													 const ChannelAccess&					gPlane,
180													 const ChannelAccess&					bPlane,
181													 const ChannelAccess&					aPlane,
182													 const tcu::UVec4&						bitDepth,
183													 const std::vector<tcu::Vec2>&			sts,
184													 const tcu::FloatFormat&				filteringFormat,
185													 const tcu::FloatFormat&				conversionFormat,
186													 const deUint32							subTexelPrecisionBits,
187													 vk::VkFilter							filter,
188													 vk::VkSamplerYcbcrModelConversion		colorModel,
189													 vk::VkSamplerYcbcrRange				range,
190													 vk::VkFilter							chromaFilter,
191													 vk::VkChromaLocation					xChromaOffset,
192													 vk::VkChromaLocation					yChromaOffset,
193													 const vk::VkComponentMapping&			componentMapping,
194													 bool									explicitReconstruction,
195													 vk::VkSamplerAddressMode				addressModeU,
196													 vk::VkSamplerAddressMode				addressModeV,
197													 std::vector<tcu::Vec4>&				minBounds,
198													 std::vector<tcu::Vec4>&				maxBounds,
199													 std::vector<tcu::Vec4>&				uvBounds,
200													 std::vector<tcu::IVec4>&				ijBounds);
201
202} // ycbcr
203} // vkt
204
205#endif // _VKTYCBCRUTIL_HPP
206