1 #ifndef _VKIMAGEUTIL_HPP
2 #define _VKIMAGEUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2015 The Khronos Group Inc.
8  * Copyright (c) 2015 Imagination Technologies Ltd.
9  * Copyright (c) 2015 Google Inc.
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  *//*!
24  * \file
25  * \brief Utilities for images.
26  *//*--------------------------------------------------------------------*/
27 
28 #include "vkDefs.hpp"
29 #include "vkMemUtil.hpp"
30 #include "tcuTexture.hpp"
31 #include "tcuCompressedTexture.hpp"
32 #include "deSharedPtr.hpp"
33 
34 namespace vk
35 {
36 
37 bool						isFloatFormat				(VkFormat format);
38 bool						isUfloatFormat				(VkFormat format);
39 bool						isSfloatFormat				(VkFormat format);
40 bool						isUnormFormat				(VkFormat format);
41 bool						isSnormFormat				(VkFormat format);
42 bool						isIntFormat					(VkFormat format);
43 bool						isUintFormat				(VkFormat format);
44 bool						isDepthStencilFormat		(VkFormat format);
45 bool						isCompressedFormat			(VkFormat format);
46 bool						isSrgbFormat				(VkFormat format);
47 
48 bool						isSupportedByFramework		(VkFormat format);
49 void						checkImageSupport			(const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const VkImageCreateInfo& imageCreateInfo);
50 
51 tcu::TextureFormat			mapVkFormat					(VkFormat format);
52 tcu::CompressedTexFormat	mapVkCompressedFormat		(VkFormat format);
53 tcu::TextureFormat			getDepthCopyFormat			(VkFormat combinedFormat);
54 tcu::TextureFormat			getStencilCopyFormat		(VkFormat combinedFormat);
55 
56 tcu::Sampler				mapVkSampler				(const VkSamplerCreateInfo& samplerCreateInfo);
57 tcu::Sampler::CompareMode	mapVkSamplerCompareOp		(VkCompareOp compareOp);
58 tcu::Sampler::WrapMode		mapVkSamplerAddressMode		(VkSamplerAddressMode addressMode);
59 tcu::Sampler::ReductionMode mapVkSamplerReductionMode	(VkSamplerReductionMode reductionMode);
60 tcu::Sampler::FilterMode	mapVkMinTexFilter			(VkFilter filter, VkSamplerMipmapMode mipMode);
61 tcu::Sampler::FilterMode	mapVkMagTexFilter			(VkFilter filter);
62 
63 VkFilter					mapFilterMode				(tcu::Sampler::FilterMode filterMode);
64 VkSamplerMipmapMode			mapMipmapMode				(tcu::Sampler::FilterMode filterMode);
65 VkSamplerAddressMode		mapWrapMode					(tcu::Sampler::WrapMode wrapMode);
66 VkCompareOp					mapCompareMode				(tcu::Sampler::CompareMode mode);
67 VkFormat					mapTextureFormat			(const tcu::TextureFormat& format);
68 VkFormat					mapCompressedTextureFormat	(const tcu::CompressedTexFormat format);
69 VkSamplerCreateInfo			mapSampler					(const tcu::Sampler& sampler, const tcu::TextureFormat& format, float minLod = 0.0f, float maxLod = 1000.0f, bool unnormal = false);
70 rr::GenericVec4				mapVkColor					(const VkClearColorValue& color);
71 VkClearColorValue			mapVkColor					(const rr::GenericVec4& color);
72 
73 void						imageUtilSelfTest			(void);
74 
75 float						getRepresentableDiffUnorm	(const VkFormat format, const deUint32 componentNdx);
76 float						getRepresentableDiffSnorm	(const VkFormat format, const deUint32 componentNdx);
77 deUint32					getFormatComponentWidth		(const VkFormat format, const deUint32 componentNdx);
78 deUint32					getBlockSizeInBytes			(const VkFormat compressedFormat);
79 deUint32					getBlockWidth				(const VkFormat compressedFormat);
80 deUint32					getBlockHeight				(const VkFormat compressedFormat);
81 
82 const deUint32 BUFFER_IMAGE_COPY_OFFSET_GRANULARITY = 4u;
83 
84 // \todo [2017-05-18 pyry] Consider moving this to tcu
85 struct PlanarFormatDescription
86 {
87 	enum
88 	{
89 		MAX_CHANNELS	= 4,
90 		MAX_PLANES		= 3
91 	};
92 
93 	enum ChannelFlags
94 	{
95 		CHANNEL_R	= (1u<<0),	// Has "R" (0) channel
96 		CHANNEL_G	= (1u<<1),	// Has "G" (1) channel
97 		CHANNEL_B	= (1u<<2),	// Has "B" (2) channel
98 		CHANNEL_A	= (1u<<3),	// Has "A" (3) channel
99 	};
100 
101 	struct Plane
102 	{
103 		deUint8		elementSizeBytes;
104 		deUint8		widthDivisor;
105 		deUint8		heightDivisor;
106 		VkFormat	planeCompatibleFormat;
107 	};
108 
109 	struct Channel
110 	{
111 		deUint8		planeNdx;
112 		deUint8		type;				// tcu::TextureChannelClass value
113 		deUint8		offsetBits;			// Offset in element in bits
114 		deUint8		sizeBits;			// Value size in bits
115 		deUint8		strideBytes;		// Pixel stride (in bytes), usually plane elementSize
116 	};
117 
118 	deUint8		numPlanes;
119 	deUint8		presentChannels;
120 	deUint8		blockWidth;
121 	deUint8		blockHeight;
122 	Plane		planes[MAX_PLANES];
123 	Channel		channels[MAX_CHANNELS];
124 
hasChannelNdxvk::PlanarFormatDescription125 	inline bool hasChannelNdx (deUint32 ndx) const
126 	{
127 		DE_ASSERT(de::inBounds(ndx, 0u, 4u));
128 		return (presentChannels & (1u<<ndx)) != 0;
129 	}
130 };
131 
132 bool							isYCbCrFormat					(VkFormat						format);
133 bool							isYCbCrExtensionFormat			(VkFormat						format);
134 PlanarFormatDescription			getPlanarFormatDescription		(VkFormat						format);
135 int								getPlaneCount					(VkFormat						format);
136 deUint32						getMipmapCount					(VkFormat						format,
137 																 const vk::PlanarFormatDescription&	formatDescription,
138 																 const vk::VkImageFormatProperties& imageFormatProperties,
139 																 const vk::VkExtent3D&				extent);
140 
141 deUint32						getPlaneSizeInBytes				(const PlanarFormatDescription&	formatInfo,
142 																 const VkExtent3D&				baseExtents,
143 																 const deUint32					planeNdx,
144 																 const deUint32					mipmapLevel,
145 																 const deUint32					mipmapMemoryAlignment);
146 deUint32						getPlaneSizeInBytes				(const PlanarFormatDescription&	formatInfo,
147 																 const tcu::UVec2&				baseExtents,
148 																 const deUint32					planeNdx,
149 																 const deUint32					mipmapLevel,
150 																 const deUint32					mipmapMemoryAlignment);
151 VkExtent3D						getPlaneExtent					(const PlanarFormatDescription&	formatInfo,
152 																 const VkExtent3D&				baseExtents,
153 																 const deUint32					planeNdx,
154 																 const deUint32					mipmapLevel);
155 tcu::UVec2						getPlaneExtent					(const PlanarFormatDescription&	formatInfo,
156 																 const tcu::UVec2&				baseExtents,
157 																 const deUint32					planeNdx,
158 																 const deUint32					mipmapLevel);
159 tcu::UVec3						getImageSizeAlignment			(VkFormat						format);
160 tcu::UVec3						getImageSizeAlignment			(const PlanarFormatDescription&	formatInfo);
161 tcu::UVec2						getBlockExtent					(VkFormat						format);
162 tcu::UVec2						getBlockExtent					(const PlanarFormatDescription&	formatInfo);
163 VkFormat						getPlaneCompatibleFormat		(VkFormat						format,
164 																 deUint32						planeNdx);
165 VkFormat						getPlaneCompatibleFormat		(const PlanarFormatDescription&	formatInfo,
166 																 deUint32						planeNdx);
167 
168 VkImageAspectFlagBits			getPlaneAspect					(deUint32						planeNdx);
169 deUint32						getAspectPlaneNdx				(VkImageAspectFlagBits			planeAspect);
170 bool							isChromaSubsampled				(VkFormat						format);
171 bool							isYCbCr422Format				(VkFormat						format);
172 bool							isYCbCr420Format				(VkFormat						format);
173 
174 tcu::PixelBufferAccess			getChannelAccess				(const PlanarFormatDescription&	formatInfo,
175 																 const tcu::UVec2&				size,
176 																 const deUint32*				planeRowPitches,
177 																 void* const*					planePtrs,
178 																 deUint32						channelNdx);
179 tcu::ConstPixelBufferAccess		getChannelAccess				(const PlanarFormatDescription&	formatInfo,
180 																 const tcu::UVec2&				size,
181 																 const deUint32*				planeRowPitches,
182 																 const void* const*				planePtrs,
183 																 deUint32						channelNdx);
184 tcu::PixelBufferAccess			getChannelAccess				(const PlanarFormatDescription&	formatInfo,
185 																 const tcu::UVec3&				size,
186 																 const deUint32*				planeRowPitches,
187 																 void* const*					planePtrs,
188 																 deUint32						channelNdx);
189 tcu::ConstPixelBufferAccess		getChannelAccess				(const PlanarFormatDescription&	formatInfo,
190 																 const tcu::UVec3&				size,
191 																 const deUint32*				planeRowPitches,
192 																 const void* const*				planePtrs,
193 																 deUint32						channelNdx);
194 VkImageAspectFlags				getImageAspectFlags				(const tcu::TextureFormat		textureFormat);
195 VkExtent3D						mipLevelExtents					(const VkExtent3D&				baseExtents,
196 																 const deUint32					mipLevel);
197 tcu::UVec3						alignedDivide					(const VkExtent3D&				extent,
198 																 const VkExtent3D&				divisor);
199 
200 /*--------------------------------------------------------------------*//*!
201  * Copies buffer data into an image. The buffer is expected to be
202  * in a state after host write.
203 *//*--------------------------------------------------------------------*/
204 void	copyBufferToImage						(const DeviceInterface&							vk,
205 												 vk::VkDevice									device,
206 												 vk::VkQueue									queue,
207 												 deUint32										queueFamilyIndex,
208 												 const vk::VkBuffer&							buffer,
209 												 vk::VkDeviceSize								bufferSize,
210 												 const std::vector<vk::VkBufferImageCopy>&		copyRegions,
211 												 const vk::VkSemaphore*							waitSemaphore,
212 												 vk::VkImageAspectFlags							imageAspectFlags,
213 												 deUint32										mipLevels,
214 												 deUint32										arrayLayers,
215 												 vk::VkImage									destImage,
216 												 VkImageLayout									destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
217 												 VkPipelineStageFlags							destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
218 
219 void	copyBufferToImage						(const DeviceInterface&							vk,
220 												 const VkCommandBuffer&							cmdBuffer,
221 												 const VkBuffer&								buffer,
222 												 vk::VkDeviceSize								bufferSize,
223 												 const std::vector<VkBufferImageCopy>&			copyRegions,
224 												 VkImageAspectFlags								imageAspectFlags,
225 												 deUint32										mipLevels,
226 												 deUint32										arrayLayers,
227 												 VkImage										destImage,
228 												 VkImageLayout									destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
229 												 VkPipelineStageFlags							destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
230 
231 /*--------------------------------------------------------------------*//*!
232  * Copies image data into a buffer. The buffer is expected to be
233  * read by the host.
234 *//*--------------------------------------------------------------------*/
235 void	copyImageToBuffer						(const DeviceInterface&							vk,
236 												 vk::VkCommandBuffer							cmdBuffer,
237 												 vk::VkImage									image,
238 												 vk::VkBuffer									buffer,
239 												 tcu::IVec2										size,
240 												 vk::VkAccessFlags								srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
241 												 vk::VkImageLayout								oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
242 												 deUint32										numLayers = 1u,
243 												 VkImageAspectFlags								barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT,
244 												 VkImageAspectFlags								copyAspect = VK_IMAGE_ASPECT_COLOR_BIT);
245 
246 /*--------------------------------------------------------------------*//*!
247  * Clear a color image
248 *//*--------------------------------------------------------------------*/
249 void	clearColorImage							(const DeviceInterface&							vk,
250 												 const vk::VkDevice								device,
251 												 const vk::VkQueue								queue,
252 												 deUint32										queueFamilyIndex,
253 												 vk::VkImage									image,
254 												 tcu::Vec4										clearColor,
255 												 vk::VkImageLayout								oldLayout,
256 												 vk::VkImageLayout								newLayout,
257 												 vk::VkPipelineStageFlags						dstStageFlags);
258 
259 /*--------------------------------------------------------------------*//*!
260  * Initialize color image with a chessboard pattern
261 *//*--------------------------------------------------------------------*/
262 void	initColorImageChessboardPattern			(const DeviceInterface&							vk,
263 												 const vk::VkDevice								device,
264 												 const vk::VkQueue								queue,
265 												 deUint32										queueFamilyIndex,
266 												 Allocator&										allocator,
267 												 vk::VkImage									image,
268 												 vk::VkFormat									format,
269 												 tcu::Vec4										colorValue0,
270 												 tcu::Vec4										colorValue1,
271 												 deUint32										imageWidth,
272 												 deUint32										imageHeight,
273 												 deUint32										tileSize,
274 												 vk::VkImageLayout								oldLayout,
275 												 vk::VkImageLayout								newLayout,
276 												 vk::VkPipelineStageFlags						dstStageFlags);
277 
278 /*--------------------------------------------------------------------*//*!
279  * Copies depth/stencil image data into two separate buffers.
280  * The buffers are expected to be read by the host.
281 *//*--------------------------------------------------------------------*/
282 void	copyDepthStencilImageToBuffers			(const DeviceInterface&							vk,
283 												 vk::VkCommandBuffer							cmdBuffer,
284 												 vk::VkImage									image,
285 												 vk::VkBuffer									depthBuffer,
286 												 vk::VkBuffer									stencilBuffer,
287 												 tcu::IVec2										size,
288 												 vk::VkAccessFlags								srcAccessMask,
289 												 vk::VkImageLayout								oldLayout,
290 												 deUint32										numLayers = 1u);
291 
292 /*--------------------------------------------------------------------*//*!
293  * Clear a depth/stencil image
294 *//*--------------------------------------------------------------------*/
295 void	clearDepthStencilImage					(const DeviceInterface&							vk,
296 												 const vk::VkDevice								device,
297 												 const vk::VkQueue								queue,
298 												 deUint32										queueFamilyIndex,
299 												 vk::VkImage									image,
300 												 float											depthValue,
301 												 deUint32										stencilValue,
302 												 vk::VkImageLayout								oldLayout,
303 												 vk::VkImageLayout								newLayout,
304 												 vk::VkPipelineStageFlags						dstStageFlags);
305 
306 /*--------------------------------------------------------------------*//*!
307  * Initialize depth and stencil channels with a chessboard pattern
308 *//*--------------------------------------------------------------------*/
309 void	initDepthStencilImageChessboardPattern	(const DeviceInterface&							vk,
310 												 const vk::VkDevice								device,
311 												 const vk::VkQueue								queue,
312 												 deUint32										queueFamilyIndex,
313 												 Allocator&										allocator,
314 												 vk::VkImage									image,
315 												 vk::VkFormat									format,
316 												 float											depthValue0,
317 												 float											depthValue1,
318 												 deUint32										stencilValue0,
319 												 deUint32										stencilValue1,
320 												 deUint32										imageWidth,
321 												 deUint32										imageHeight,
322 												 deUint32										tileSize,
323 												 vk::VkImageLayout								oldLayout,
324 												 vk::VkImageLayout								newLayout,
325 												 vk::VkPipelineStageFlags						dstStageFlags);
326 
327 /*--------------------------------------------------------------------*//*!
328  * Checks if the physical device supports creation of the specified
329  * image format.
330  *//*--------------------------------------------------------------------*/
331 bool	checkSparseImageFormatSupport			(const VkPhysicalDevice							physicalDevice,
332 												 const InstanceInterface&						instance,
333 												 const VkFormat									format,
334 												 const VkImageType								imageType,
335 												 const VkSampleCountFlagBits					sampleCount,
336 												 const VkImageUsageFlags						usageFlags,
337 												 const VkImageTiling							imageTiling);
338 
339 bool	checkSparseImageFormatSupport			(const vk::VkPhysicalDevice						physicalDevice,
340 												 const vk::InstanceInterface&					instance,
341 												 const vk::VkImageCreateInfo&					imageCreateInfo);
342 
343 /*--------------------------------------------------------------------*//*!
344  * Allocates memory for a sparse image and handles the memory binding.
345  *//*--------------------------------------------------------------------*/
346 void	allocateAndBindSparseImage				(const vk::DeviceInterface&						vk,
347 												 vk::VkDevice									device,
348 												 const vk::VkPhysicalDevice						physicalDevice,
349 												 const vk::InstanceInterface&					instance,
350 												 const vk::VkImageCreateInfo&					imageCreateInfo,
351 												 const vk::VkSemaphore&							signalSemaphore,
352 												 vk::VkQueue									queue,
353 												 vk::Allocator&									allocator,
354 												 std::vector<de::SharedPtr<vk::Allocation> >&	allocations,
355 												 tcu::TextureFormat								format,
356 												 vk::VkImage									destImage);
357 
358 } // vk
359 
360 #endif // _VKIMAGEUTIL_HPP
361