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 
46 namespace vkt
47 {
48 namespace 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 
54 typedef de::SharedPtr<vk::Allocation>				AllocationSp;
55 typedef de::SharedPtr<vk::Unique<vk::VkBuffer> >	VkBufferSp;
56 
57 class MultiPlaneImageData
58 {
59 public:
60 										MultiPlaneImageData		(vk::VkFormat format, const tcu::UVec2& size);
61 										MultiPlaneImageData		(const MultiPlaneImageData&);
62 										~MultiPlaneImageData	(void);
63 
getFormat(void) const64 	vk::VkFormat						getFormat				(void) const				{ return m_format;						}
getDescription(void) const65 	const vk::PlanarFormatDescription&	getDescription			(void) const				{ return m_description;					}
getSize(void) const66 	const tcu::UVec2&					getSize					(void) const				{ return m_size;						}
67 
getPlaneSize(deUint32 planeNdx) const68 	size_t								getPlaneSize			(deUint32 planeNdx) const	{ return m_planeData[planeNdx].size();	}
getPlanePtr(deUint32 planeNdx)69 	void*								getPlanePtr				(deUint32 planeNdx)			{ return &m_planeData[planeNdx][0];		}
getPlanePtr(deUint32 planeNdx) const70 	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 
75 private:
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 
85 void										checkImageSupport			(Context& context, vk::VkFormat format, vk::VkImageCreateFlags createFlags, vk::VkImageTiling tiling = vk::VK_IMAGE_TILING_OPTIMAL);
86 
87 void										fillRandomNoNaN				(de::Random* randomGen, deUint8* const data, deUint32 size, const vk::VkFormat format);
88 void										fillRandom					(de::Random* randomGen, MultiPlaneImageData* imageData, const vk::VkFormat format = vk::VK_FORMAT_UNDEFINED, bool noNan = false);
89 void										fillGradient				(MultiPlaneImageData* imageData, const tcu::Vec4& minVal, const tcu::Vec4& maxVal);
90 void										fillZero					(MultiPlaneImageData* imageData);
91 
92 std::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 
100 void										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 
110 void										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 
120 void										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 
129 void										readImageMemory				(const vk::DeviceInterface&							vkd,
130 																		 vk::VkDevice										device,
131 																		 deUint32											queueFamilyNdx,
132 																		 vk::VkImage										image,
133 																		 const std::vector<de::SharedPtr<vk::Allocation> >&	memory,
134 																		 MultiPlaneImageData*								imageData,
135 																		 vk::VkAccessFlags									prevAccess,
136 																		 vk::VkImageLayout									initialLayout);
137 
138 class ChannelAccess
139 {
140 public:
141 						ChannelAccess	(tcu::TextureChannelClass	channelClass,
142 										 deUint8					channelSize,
143 										 const tcu::IVec3&			size,
144 										 const tcu::IVec3&			bitPitch,
145 										 void*						data,
146 										 deUint32					bitOffset);
147 
getSize(void) const148 	const tcu::IVec3&	getSize			(void) const { return m_size; }
getBitPitch(void) const149 	const tcu::IVec3&	getBitPitch		(void) const { return m_bitPitch; }
getDataPtr(void) const150 	void*				getDataPtr		(void) const { return m_data; }
151 
152 	tcu::Interval		getChannel		(const tcu::FloatFormat&	conversionFormat,
153 										 const tcu::IVec3&			pos) const;
154 	deUint32			getChannelUint	(const tcu::IVec3&			pos) const;
155 	float				getChannel		(const tcu::IVec3&			pos) const;
156 	void				setChannel		(const tcu::IVec3&			pos,
157 										 deUint32					x);
158 	void				setChannel		(const tcu::IVec3&			pos,
159 										 float						x);
160 
161 private:
162 	const tcu::TextureChannelClass	m_channelClass;
163 	const deUint8					m_channelSize;
164 	const tcu::IVec3				m_size;
165 	const tcu::IVec3				m_bitPitch;
166 	void* const						m_data;
167 	const deInt32					m_bitOffset;
168 };
169 
170 ChannelAccess					getChannelAccess			(ycbcr::MultiPlaneImageData&			data,
171 															 const vk::PlanarFormatDescription&		formatInfo,
172 															 const tcu::UVec2&						size,
173 															 int									channelNdx);
174 
175 bool							isYChromaSubsampled			(vk::VkFormat							format);
176 
177 bool							isXChromaSubsampled			(vk::VkFormat							format);
178 
179 bool							areLsb6BitsDontCare			(vk::VkFormat							srcFormat,
180 															 vk::VkFormat							dstFormat);
181 
182 bool							areLsb4BitsDontCare			(vk::VkFormat							srcFormat,
183 															 vk::VkFormat							dstFormat);
184 
185 tcu::UVec4						getYCbCrBitDepth			(vk::VkFormat							format);
186 
187 std::vector<tcu::FloatFormat>	getPrecision				(vk::VkFormat							format);
188 
189 deUint32						getYCbCrFormatChannelCount	(vk::VkFormat							format);
190 
191 int								wrap						(vk::VkSamplerAddressMode				addressMode,
192 															 int									coord,
193 															 int									size);
194 
195 int								divFloor					(int									a,
196 															 int									b);
197 
198 void							calculateBounds				(const ChannelAccess&					rPlane,
199 															 const ChannelAccess&					gPlane,
200 															 const ChannelAccess&					bPlane,
201 															 const ChannelAccess&					aPlane,
202 															 const tcu::UVec4&						bitDepth,
203 															 const std::vector<tcu::Vec2>&			sts,
204 															 const std::vector<tcu::FloatFormat>&	filteringFormat,
205 															 const std::vector<tcu::FloatFormat>&	conversionFormat,
206 															 const deUint32							subTexelPrecisionBits,
207 															 vk::VkFilter							filter,
208 															 vk::VkSamplerYcbcrModelConversion		colorModel,
209 															 vk::VkSamplerYcbcrRange				range,
210 															 vk::VkFilter							chromaFilter,
211 															 vk::VkChromaLocation					xChromaOffset,
212 															 vk::VkChromaLocation					yChromaOffset,
213 															 const vk::VkComponentMapping&			componentMapping,
214 															 bool									explicitReconstruction,
215 															 vk::VkSamplerAddressMode				addressModeU,
216 															 vk::VkSamplerAddressMode				addressModeV,
217 															 std::vector<tcu::Vec4>&				minBounds,
218 															 std::vector<tcu::Vec4>&				maxBounds,
219 															 std::vector<tcu::Vec4>&				uvBounds,
220 															 std::vector<tcu::IVec4>&				ijBounds);
221 
222 } // ycbcr
223 } // vkt
224 
225 #endif // _VKTYCBCRUTIL_HPP
226