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
54typedef de::SharedPtr<vk::Allocation>				AllocationSp;
55typedef de::SharedPtr<vk::Unique<vk::VkBuffer> >	VkBufferSp;
56
57class MultiPlaneImageData
58{
59public:
60										MultiPlaneImageData		(vk::VkFormat format, const tcu::UVec2& size);
61										MultiPlaneImageData		(const MultiPlaneImageData&);
62										~MultiPlaneImageData	(void);
63
64	vk::VkFormat						getFormat				(void) const				{ return m_format;						}
65	const vk::PlanarFormatDescription&	getDescription			(void) const				{ return m_description;					}
66	const tcu::UVec2&					getSize					(void) const				{ return m_size;						}
67
68	size_t								getPlaneSize			(deUint32 planeNdx) const	{ return m_planeData[planeNdx].size();	}
69	void*								getPlanePtr				(deUint32 planeNdx)			{ return &m_planeData[planeNdx][0];		}
70	const void*							getPlanePtr				(deUint32 planeNdx) const	{ return &m_planeData[planeNdx][0];		}
71
72	tcu::PixelBufferAccess				getChannelAccess		(deUint32 channelNdx);
73	tcu::ConstPixelBufferAccess			getChannelAccess		(deUint32 channelNdx) const;
74
75private:
76	MultiPlaneImageData&				operator=				(const MultiPlaneImageData&);
77
78	const vk::VkFormat					m_format;
79	const vk::PlanarFormatDescription	m_description;
80	const tcu::UVec2					m_size;
81
82	std::vector<deUint8>				m_planeData[vk::PlanarFormatDescription::MAX_PLANES];
83};
84
85void										checkImageSupport			(Context& context, vk::VkFormat format, vk::VkImageCreateFlags createFlags, vk::VkImageTiling tiling = vk::VK_IMAGE_TILING_OPTIMAL);
86
87void										fillRandomNoNaN				(de::Random* randomGen, deUint8* const data, deUint32 size, const vk::VkFormat format);
88void										fillRandom					(de::Random* randomGen, MultiPlaneImageData* imageData, const vk::VkFormat format = vk::VK_FORMAT_UNDEFINED, bool noNan = false);
89void										fillGradient				(MultiPlaneImageData* imageData, const tcu::Vec4& minVal, const tcu::Vec4& maxVal);
90void										fillZero					(MultiPlaneImageData* imageData);
91
92std::vector<de::SharedPtr<vk::Allocation> >	allocateAndBindImageMemory	(const vk::DeviceInterface&	vkd,
93																		 vk::VkDevice				device,
94																		 vk::Allocator&				allocator,
95																		 vk::VkImage				image,
96																		 vk::VkFormat				format,
97																		 vk::VkImageCreateFlags		createFlags,
98																		 vk::MemoryRequirement		requirement = vk::MemoryRequirement::Any);
99
100void										uploadImage					(const vk::DeviceInterface&	vkd,
101																		 vk::VkDevice				device,
102																		 deUint32					queueFamilyNdx,
103																		 vk::Allocator&				allocator,
104																		 vk::VkImage				image,
105																		 const MultiPlaneImageData&	imageData,
106																		 vk::VkAccessFlags			nextAccess,
107																		 vk::VkImageLayout			finalLayout,
108																		 deUint32					arrayLayer = 0u);
109
110void										fillImageMemory				(const vk::DeviceInterface&							vkd,
111																		 vk::VkDevice										device,
112																		 deUint32											queueFamilyNdx,
113																		 vk::VkImage										image,
114																		 const std::vector<de::SharedPtr<vk::Allocation> >&	memory,
115																		 const MultiPlaneImageData&							imageData,
116																		 vk::VkAccessFlags									nextAccess,
117																		 vk::VkImageLayout									finalLayout,
118																		 deUint32											arrayLayer = 0u);
119
120void										downloadImage				(const vk::DeviceInterface&	vkd,
121																		 vk::VkDevice				device,
122																		 deUint32					queueFamilyNdx,
123																		 vk::Allocator&				allocator,
124																		 vk::VkImage				image,
125																		 MultiPlaneImageData*		imageData,
126																		 vk::VkAccessFlags			prevAccess,
127																		 vk::VkImageLayout			initialLayout,
128																		 uint32_t				baseArrayLayer = 0);
129
130void										readImageMemory				(const vk::DeviceInterface&							vkd,
131																		 vk::VkDevice										device,
132																		 deUint32											queueFamilyNdx,
133																		 vk::VkImage										image,
134																		 const std::vector<de::SharedPtr<vk::Allocation> >&	memory,
135																		 MultiPlaneImageData*								imageData,
136																		 vk::VkAccessFlags									prevAccess,
137																		 vk::VkImageLayout									initialLayout);
138
139class ChannelAccess
140{
141public:
142						ChannelAccess	(tcu::TextureChannelClass	channelClass,
143										 deUint8					channelSize,
144										 const tcu::IVec3&			size,
145										 const tcu::IVec3&			bitPitch,
146										 void*						data,
147										 deUint32					bitOffset);
148
149	const tcu::IVec3&	getSize			(void) const { return m_size; }
150	const tcu::IVec3&	getBitPitch		(void) const { return m_bitPitch; }
151	void*				getDataPtr		(void) const { return m_data; }
152
153	tcu::Interval		getChannel		(const tcu::FloatFormat&	conversionFormat,
154										 const tcu::IVec3&			pos) const;
155	deUint32			getChannelUint	(const tcu::IVec3&			pos) const;
156	float				getChannel		(const tcu::IVec3&			pos) const;
157	void				setChannel		(const tcu::IVec3&			pos,
158										 deUint32					x);
159	void				setChannel		(const tcu::IVec3&			pos,
160										 float						x);
161
162private:
163	const tcu::TextureChannelClass	m_channelClass;
164	const deUint8					m_channelSize;
165	const tcu::IVec3				m_size;
166	const tcu::IVec3				m_bitPitch;
167	void* const						m_data;
168	const deInt32					m_bitOffset;
169};
170
171ChannelAccess					getChannelAccess			(ycbcr::MultiPlaneImageData&			data,
172															 const vk::PlanarFormatDescription&		formatInfo,
173															 const tcu::UVec2&						size,
174															 int									channelNdx);
175
176bool							isYChromaSubsampled			(vk::VkFormat							format);
177
178bool							isXChromaSubsampled			(vk::VkFormat							format);
179
180bool							areLsb6BitsDontCare			(vk::VkFormat							srcFormat,
181															 vk::VkFormat							dstFormat);
182
183bool							areLsb4BitsDontCare			(vk::VkFormat							srcFormat,
184															 vk::VkFormat							dstFormat);
185
186tcu::UVec4						getYCbCrBitDepth			(vk::VkFormat							format);
187
188std::vector<tcu::FloatFormat>	getPrecision				(vk::VkFormat							format);
189
190deUint32						getYCbCrFormatChannelCount	(vk::VkFormat							format);
191
192int								wrap						(vk::VkSamplerAddressMode				addressMode,
193															 int									coord,
194															 int									size);
195
196int								divFloor					(int									a,
197															 int									b);
198
199void							calculateBounds				(const ChannelAccess&					rPlane,
200															 const ChannelAccess&					gPlane,
201															 const ChannelAccess&					bPlane,
202															 const ChannelAccess&					aPlane,
203															 const tcu::UVec4&						bitDepth,
204															 const std::vector<tcu::Vec2>&			sts,
205															 const std::vector<tcu::FloatFormat>&	filteringFormat,
206															 const std::vector<tcu::FloatFormat>&	conversionFormat,
207															 const deUint32							subTexelPrecisionBits,
208															 vk::VkFilter							filter,
209															 vk::VkSamplerYcbcrModelConversion		colorModel,
210															 vk::VkSamplerYcbcrRange				range,
211															 vk::VkFilter							chromaFilter,
212															 vk::VkChromaLocation					xChromaOffset,
213															 vk::VkChromaLocation					yChromaOffset,
214															 const vk::VkComponentMapping&			componentMapping,
215															 bool									explicitReconstruction,
216															 vk::VkSamplerAddressMode				addressModeU,
217															 vk::VkSamplerAddressMode				addressModeV,
218															 std::vector<tcu::Vec4>&				minBounds,
219															 std::vector<tcu::Vec4>&				maxBounds,
220															 std::vector<tcu::Vec4>&				uvBounds,
221															 std::vector<tcu::IVec4>&				ijBounds);
222
223} // ycbcr
224} // vkt
225
226#endif // _VKTYCBCRUTIL_HPP
227