• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
71 void						imageUtilSelfTest			(void);
72 
73 float						getRepresentableDiffUnorm	(const VkFormat format, const deUint32 componentNdx);
74 float						getRepresentableDiffSnorm	(const VkFormat format, const deUint32 componentNdx);
75 deUint32					getFormatComponentWidth		(const VkFormat format, const deUint32 componentNdx);
76 deUint32					getBlockSizeInBytes			(const VkFormat compressedFormat);
77 deUint32					getBlockWidth				(const VkFormat compressedFormat);
78 deUint32					getBlockHeight				(const VkFormat compressedFormat);
79 
80 const deUint32 BUFFER_IMAGE_COPY_OFFSET_GRANULARITY = 4u;
81 
82 // \todo [2017-05-18 pyry] Consider moving this to tcu
83 struct PlanarFormatDescription
84 {
85 	enum
86 	{
87 		MAX_CHANNELS	= 4,
88 		MAX_PLANES		= 3
89 	};
90 
91 	enum ChannelFlags
92 	{
93 		CHANNEL_R	= (1u<<0),	// Has "R" (0) channel
94 		CHANNEL_G	= (1u<<1),	// Has "G" (1) channel
95 		CHANNEL_B	= (1u<<2),	// Has "B" (2) channel
96 		CHANNEL_A	= (1u<<3),	// Has "A" (3) channel
97 	};
98 
99 	struct Plane
100 	{
101 		deUint8		elementSizeBytes;
102 		deUint8		widthDivisor;
103 		deUint8		heightDivisor;
104 		VkFormat	planeCompatibleFormat;
105 	};
106 
107 	struct Channel
108 	{
109 		deUint8		planeNdx;
110 		deUint8		type;				// tcu::TextureChannelClass value
111 		deUint8		offsetBits;			// Offset in element in bits
112 		deUint8		sizeBits;			// Value size in bits
113 		deUint8		strideBytes;		// Pixel stride (in bytes), usually plane elementSize
114 	};
115 
116 	deUint8		numPlanes;
117 	deUint8		presentChannels;
118 	deUint8		blockWidth;
119 	deUint8		blockHeight;
120 	Plane		planes[MAX_PLANES];
121 	Channel		channels[MAX_CHANNELS];
122 
hasChannelNdxvk::PlanarFormatDescription123 	inline bool hasChannelNdx (deUint32 ndx) const
124 	{
125 		DE_ASSERT(de::inBounds(ndx, 0u, 4u));
126 		return (presentChannels & (1u<<ndx)) != 0;
127 	}
128 };
129 
130 bool							isYCbCrFormat					(VkFormat						format);
131 PlanarFormatDescription			getPlanarFormatDescription		(VkFormat						format);
132 int								getPlaneCount					(VkFormat						format);
133 deUint32						getMipmapCount					(VkFormat						format,
134 																 const vk::PlanarFormatDescription&	formatDescription,
135 																 const vk::VkImageFormatProperties& imageFormatProperties,
136 																 const vk::VkExtent3D&				extent);
137 
138 deUint32						getPlaneSizeInBytes				(const PlanarFormatDescription&	formatInfo,
139 																 const VkExtent3D&				baseExtents,
140 																 const deUint32					planeNdx,
141 																 const deUint32					mipmapLevel,
142 																 const deUint32					mipmapMemoryAlignment);
143 deUint32						getPlaneSizeInBytes				(const PlanarFormatDescription&	formatInfo,
144 																 const tcu::UVec2&				baseExtents,
145 																 const deUint32					planeNdx,
146 																 const deUint32					mipmapLevel,
147 																 const deUint32					mipmapMemoryAlignment);
148 VkExtent3D						getPlaneExtent					(const PlanarFormatDescription&	formatInfo,
149 																 const VkExtent3D&				baseExtents,
150 																 const deUint32					planeNdx,
151 																 const deUint32					mipmapLevel);
152 tcu::UVec2						getPlaneExtent					(const PlanarFormatDescription&	formatInfo,
153 																 const tcu::UVec2&				baseExtents,
154 																 const deUint32					planeNdx,
155 																 const deUint32					mipmapLevel);
156 tcu::UVec3						getImageSizeAlignment			(VkFormat						format);
157 tcu::UVec3						getImageSizeAlignment			(const PlanarFormatDescription&	formatInfo);
158 tcu::UVec2						getBlockExtent					(VkFormat						format);
159 tcu::UVec2						getBlockExtent					(const PlanarFormatDescription&	formatInfo);
160 VkFormat						getPlaneCompatibleFormat		(VkFormat						format,
161 																 deUint32						planeNdx);
162 VkFormat						getPlaneCompatibleFormat		(const PlanarFormatDescription&	formatInfo,
163 																 deUint32						planeNdx);
164 
165 VkImageAspectFlagBits			getPlaneAspect					(deUint32						planeNdx);
166 deUint32						getAspectPlaneNdx				(VkImageAspectFlagBits			planeAspect);
167 bool							isChromaSubsampled				(VkFormat						format);
168 bool							isYCbCr422Format				(VkFormat						format);
169 bool							isYCbCr420Format				(VkFormat						format);
170 
171 tcu::PixelBufferAccess			getChannelAccess				(const PlanarFormatDescription&	formatInfo,
172 																 const tcu::UVec2&				size,
173 																 const deUint32*				planeRowPitches,
174 																 void* const*					planePtrs,
175 																 deUint32						channelNdx);
176 tcu::ConstPixelBufferAccess		getChannelAccess				(const PlanarFormatDescription&	formatInfo,
177 																 const tcu::UVec2&				size,
178 																 const deUint32*				planeRowPitches,
179 																 const void* const*				planePtrs,
180 																 deUint32						channelNdx);
181 tcu::PixelBufferAccess			getChannelAccess				(const PlanarFormatDescription&	formatInfo,
182 																 const tcu::UVec3&				size,
183 																 const deUint32*				planeRowPitches,
184 																 void* const*					planePtrs,
185 																 deUint32						channelNdx);
186 tcu::ConstPixelBufferAccess		getChannelAccess				(const PlanarFormatDescription&	formatInfo,
187 																 const tcu::UVec3&				size,
188 																 const deUint32*				planeRowPitches,
189 																 const void* const*				planePtrs,
190 																 deUint32						channelNdx);
191 VkImageAspectFlags				getImageAspectFlags				(const tcu::TextureFormat		textureFormat);
192 VkExtent3D						mipLevelExtents					(const VkExtent3D&				baseExtents,
193 																 const deUint32					mipLevel);
194 tcu::UVec3						alignedDivide					(const VkExtent3D&				extent,
195 																 const VkExtent3D&				divisor);
196 
197 /*--------------------------------------------------------------------*//*!
198  * Copies buffer data into an image. The buffer is expected to be
199  * in a state after host write.
200 *//*--------------------------------------------------------------------*/
201 void	copyBufferToImage						(const DeviceInterface&							vk,
202 												 vk::VkDevice									device,
203 												 vk::VkQueue									queue,
204 												 deUint32										queueFamilyIndex,
205 												 const vk::VkBuffer&							buffer,
206 												 vk::VkDeviceSize								bufferSize,
207 												 const std::vector<vk::VkBufferImageCopy>&		copyRegions,
208 												 const vk::VkSemaphore*							waitSemaphore,
209 												 vk::VkImageAspectFlags							imageAspectFlags,
210 												 deUint32										mipLevels,
211 												 deUint32										arrayLayers,
212 												 vk::VkImage									destImage,
213 												 VkImageLayout									destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
214 												 VkPipelineStageFlags							destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
215 
216 void	copyBufferToImage						(const DeviceInterface&							vk,
217 												 const VkCommandBuffer&							cmdBuffer,
218 												 const VkBuffer&								buffer,
219 												 vk::VkDeviceSize								bufferSize,
220 												 const std::vector<VkBufferImageCopy>&			copyRegions,
221 												 VkImageAspectFlags								imageAspectFlags,
222 												 deUint32										mipLevels,
223 												 deUint32										arrayLayers,
224 												 VkImage										destImage,
225 												 VkImageLayout									destImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
226 												 VkPipelineStageFlags							destImageDstStageFlags = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
227 
228 /*--------------------------------------------------------------------*//*!
229  * Copies image data into a buffer. The buffer is expected to be
230  * read by the host.
231 *//*--------------------------------------------------------------------*/
232 void	copyImageToBuffer						(const DeviceInterface&							vk,
233 												 vk::VkCommandBuffer							cmdBuffer,
234 												 vk::VkImage									image,
235 												 vk::VkBuffer									buffer,
236 												 tcu::IVec2										size,
237 												 vk::VkAccessFlags								srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
238 												 vk::VkImageLayout								oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
239 												 deUint32										numLayers = 1u,
240 												 VkImageAspectFlags								barrierAspect = VK_IMAGE_ASPECT_COLOR_BIT,
241 												 VkImageAspectFlags								copyAspect = VK_IMAGE_ASPECT_COLOR_BIT);
242 
243 /*--------------------------------------------------------------------*//*!
244  * Clear a color image
245 *//*--------------------------------------------------------------------*/
246 void	clearColorImage							(const DeviceInterface&							vk,
247 												 const vk::VkDevice								device,
248 												 const vk::VkQueue								queue,
249 												 deUint32										queueFamilyIndex,
250 												 vk::VkImage									image,
251 												 tcu::Vec4										clearColor,
252 												 vk::VkImageLayout								oldLayout,
253 												 vk::VkImageLayout								newLayout,
254 												 vk::VkPipelineStageFlags						dstStageFlags);
255 
256 /*--------------------------------------------------------------------*//*!
257  * Initialize color image with a chessboard pattern
258 *//*--------------------------------------------------------------------*/
259 void	initColorImageChessboardPattern			(const DeviceInterface&							vk,
260 												 const vk::VkDevice								device,
261 												 const vk::VkQueue								queue,
262 												 deUint32										queueFamilyIndex,
263 												 Allocator&										allocator,
264 												 vk::VkImage									image,
265 												 vk::VkFormat									format,
266 												 tcu::Vec4										colorValue0,
267 												 tcu::Vec4										colorValue1,
268 												 deUint32										imageWidth,
269 												 deUint32										imageHeight,
270 												 deUint32										tileSize,
271 												 vk::VkImageLayout								oldLayout,
272 												 vk::VkImageLayout								newLayout,
273 												 vk::VkPipelineStageFlags						dstStageFlags);
274 
275 /*--------------------------------------------------------------------*//*!
276  * Copies depth/stencil image data into two separate buffers.
277  * The buffers are expected to be read by the host.
278 *//*--------------------------------------------------------------------*/
279 void	copyDepthStencilImageToBuffers			(const DeviceInterface&							vk,
280 												 vk::VkCommandBuffer							cmdBuffer,
281 												 vk::VkImage									image,
282 												 vk::VkBuffer									depthBuffer,
283 												 vk::VkBuffer									stencilBuffer,
284 												 tcu::IVec2										size,
285 												 vk::VkAccessFlags								srcAccessMask,
286 												 vk::VkImageLayout								oldLayout,
287 												 deUint32										numLayers = 1u);
288 
289 /*--------------------------------------------------------------------*//*!
290  * Clear a depth/stencil image
291 *//*--------------------------------------------------------------------*/
292 void	clearDepthStencilImage					(const DeviceInterface&							vk,
293 												 const vk::VkDevice								device,
294 												 const vk::VkQueue								queue,
295 												 deUint32										queueFamilyIndex,
296 												 vk::VkImage									image,
297 												 float											depthValue,
298 												 deUint32										stencilValue,
299 												 vk::VkImageLayout								oldLayout,
300 												 vk::VkImageLayout								newLayout,
301 												 vk::VkPipelineStageFlags						dstStageFlags);
302 
303 /*--------------------------------------------------------------------*//*!
304  * Initialize depth and stencil channels with a chessboard pattern
305 *//*--------------------------------------------------------------------*/
306 void	initDepthStencilImageChessboardPattern	(const DeviceInterface&							vk,
307 												 const vk::VkDevice								device,
308 												 const vk::VkQueue								queue,
309 												 deUint32										queueFamilyIndex,
310 												 Allocator&										allocator,
311 												 vk::VkImage									image,
312 												 vk::VkFormat									format,
313 												 float											depthValue0,
314 												 float											depthValue1,
315 												 deUint32										stencilValue0,
316 												 deUint32										stencilValue1,
317 												 deUint32										imageWidth,
318 												 deUint32										imageHeight,
319 												 deUint32										tileSize,
320 												 vk::VkImageLayout								oldLayout,
321 												 vk::VkImageLayout								newLayout,
322 												 vk::VkPipelineStageFlags						dstStageFlags);
323 
324 /*--------------------------------------------------------------------*//*!
325  * Checks if the physical device supports creation of the specified
326  * image format.
327  *//*--------------------------------------------------------------------*/
328 bool	checkSparseImageFormatSupport			(const vk::VkPhysicalDevice						physicalDevice,
329 												 const vk::InstanceInterface&					instance,
330 												 const vk::VkImageCreateInfo&					imageCreateInfo);
331 
332 /*--------------------------------------------------------------------*//*!
333  * Allocates memory for a sparse image and handles the memory binding.
334  *//*--------------------------------------------------------------------*/
335 void	allocateAndBindSparseImage				(const vk::DeviceInterface&						vk,
336 												 vk::VkDevice									device,
337 												 const vk::VkPhysicalDevice						physicalDevice,
338 												 const vk::InstanceInterface&					instance,
339 												 const vk::VkImageCreateInfo&					imageCreateInfo,
340 												 const vk::VkSemaphore&							signalSemaphore,
341 												 vk::VkQueue									queue,
342 												 vk::Allocator&									allocator,
343 												 std::vector<de::SharedPtr<vk::Allocation> >&	allocations,
344 												 tcu::TextureFormat								format,
345 												 vk::VkImage									destImage);
346 
347 } // vk
348 
349 #endif // _VKIMAGEUTIL_HPP
350