2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * Based on u_format.h which is:
6 * Copyright 2009-2010 Vmware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
31 #include <util/macros.h>
32 #include <util/format/u_format.h>
33 #include <vulkan/util/vk_format.h>
35 #include <vulkan/vulkan.h>
37 static inline const struct util_format_description
*
38 vk_format_description(VkFormat format
)
40 return util_format_description(vk_format_to_pipe_format(format
));
44 * Return total bits needed for the pixel format per block.
46 static inline unsigned
47 vk_format_get_blocksizebits(VkFormat format
)
49 return util_format_get_blocksizebits(vk_format_to_pipe_format(format
));
53 * Return bytes per block (not pixel) for the given format.
55 static inline unsigned
56 vk_format_get_blocksize(VkFormat format
)
58 return util_format_get_blocksize(vk_format_to_pipe_format(format
));
61 static inline unsigned
62 vk_format_get_blockwidth(VkFormat format
)
64 return util_format_get_blockwidth(vk_format_to_pipe_format(format
));
67 static inline unsigned
68 vk_format_get_blockheight(VkFormat format
)
70 return util_format_get_blockheight(vk_format_to_pipe_format(format
));
73 static inline unsigned
74 vk_format_get_block_count_width(VkFormat format
, unsigned width
)
76 return util_format_get_nblocksx(vk_format_to_pipe_format(format
), width
);
79 static inline unsigned
80 vk_format_get_block_count_height(VkFormat format
, unsigned height
)
82 return util_format_get_nblocksy(vk_format_to_pipe_format(format
), height
);
85 static inline unsigned
86 vk_format_get_block_count(VkFormat format
, unsigned width
, unsigned height
)
88 return util_format_get_nblocks(vk_format_to_pipe_format(format
),
92 static inline VkImageAspectFlags
93 vk_format_aspects(VkFormat format
)
96 case VK_FORMAT_UNDEFINED
:
99 case VK_FORMAT_S8_UINT
:
100 return VK_IMAGE_ASPECT_STENCIL_BIT
;
102 case VK_FORMAT_D16_UNORM_S8_UINT
:
103 case VK_FORMAT_D24_UNORM_S8_UINT
:
104 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
105 return VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
;
107 case VK_FORMAT_D16_UNORM
:
108 case VK_FORMAT_X8_D24_UNORM_PACK32
:
109 case VK_FORMAT_D32_SFLOAT
:
110 return VK_IMAGE_ASPECT_DEPTH_BIT
;
113 return VK_IMAGE_ASPECT_COLOR_BIT
;
117 static inline enum pipe_swizzle
118 tu_swizzle_conv(VkComponentSwizzle component
,
119 const unsigned char chan
[4],
120 VkComponentSwizzle vk_swiz
)
124 if (vk_swiz
== VK_COMPONENT_SWIZZLE_IDENTITY
)
127 case VK_COMPONENT_SWIZZLE_ZERO
:
128 return PIPE_SWIZZLE_0
;
129 case VK_COMPONENT_SWIZZLE_ONE
:
130 return PIPE_SWIZZLE_1
;
131 case VK_COMPONENT_SWIZZLE_R
:
132 for (x
= 0; x
< 4; x
++)
135 return PIPE_SWIZZLE_0
;
136 case VK_COMPONENT_SWIZZLE_G
:
137 for (x
= 0; x
< 4; x
++)
140 return PIPE_SWIZZLE_0
;
141 case VK_COMPONENT_SWIZZLE_B
:
142 for (x
= 0; x
< 4; x
++)
145 return PIPE_SWIZZLE_0
;
146 case VK_COMPONENT_SWIZZLE_A
:
147 for (x
= 0; x
< 4; x
++)
150 return PIPE_SWIZZLE_1
;
152 unreachable("Illegal swizzle");
157 vk_format_compose_swizzles(const VkComponentMapping
*mapping
,
158 const unsigned char swz
[4],
159 enum pipe_swizzle dst
[4])
161 dst
[0] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_R
, swz
, mapping
->r
);
162 dst
[1] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_G
, swz
, mapping
->g
);
163 dst
[2] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_B
, swz
, mapping
->b
);
164 dst
[3] = tu_swizzle_conv(VK_COMPONENT_SWIZZLE_A
, swz
, mapping
->a
);
168 vk_format_is_compressed(VkFormat format
)
170 return util_format_is_compressed(vk_format_to_pipe_format(format
));
174 vk_format_has_depth(VkFormat format
)
176 const struct util_format_description
*desc
= vk_format_description(format
);
178 return util_format_has_depth(desc
);
182 vk_format_has_stencil(VkFormat format
)
184 const struct util_format_description
*desc
= vk_format_description(format
);
186 return util_format_has_stencil(desc
);
190 vk_format_is_depth_or_stencil(VkFormat format
)
192 return vk_format_has_depth(format
) || vk_format_has_stencil(format
);
196 vk_format_is_color(VkFormat format
)
198 return !vk_format_is_depth_or_stencil(format
);
202 vk_format_has_alpha(VkFormat format
)
204 return util_format_has_alpha(vk_format_to_pipe_format(format
));
207 static inline VkFormat
208 vk_format_depth_only(VkFormat format
)
211 case VK_FORMAT_D16_UNORM_S8_UINT
:
212 return VK_FORMAT_D16_UNORM
;
213 case VK_FORMAT_D24_UNORM_S8_UINT
:
214 return VK_FORMAT_X8_D24_UNORM_PACK32
;
215 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
216 return VK_FORMAT_D32_SFLOAT
;
223 vk_format_is_int(VkFormat format
)
225 return util_format_is_pure_integer(vk_format_to_pipe_format(format
));
229 vk_format_is_uint(VkFormat format
)
231 return util_format_is_pure_uint(vk_format_to_pipe_format(format
));
235 vk_format_is_sint(VkFormat format
)
237 return util_format_is_pure_sint(vk_format_to_pipe_format(format
));
241 vk_format_is_srgb(VkFormat format
)
243 return util_format_is_srgb(vk_format_to_pipe_format(format
));
246 static inline VkFormat
247 vk_format_no_srgb(VkFormat format
)
250 case VK_FORMAT_R8_SRGB
:
251 return VK_FORMAT_R8_UNORM
;
252 case VK_FORMAT_R8G8_SRGB
:
253 return VK_FORMAT_R8G8_UNORM
;
254 case VK_FORMAT_R8G8B8_SRGB
:
255 return VK_FORMAT_R8G8B8_UNORM
;
256 case VK_FORMAT_B8G8R8_SRGB
:
257 return VK_FORMAT_B8G8R8_UNORM
;
258 case VK_FORMAT_R8G8B8A8_SRGB
:
259 return VK_FORMAT_R8G8B8A8_UNORM
;
260 case VK_FORMAT_B8G8R8A8_SRGB
:
261 return VK_FORMAT_B8G8R8A8_UNORM
;
262 case VK_FORMAT_A8B8G8R8_SRGB_PACK32
:
263 return VK_FORMAT_A8B8G8R8_UNORM_PACK32
;
264 case VK_FORMAT_BC1_RGB_SRGB_BLOCK
:
265 return VK_FORMAT_BC1_RGB_UNORM_BLOCK
;
266 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK
:
267 return VK_FORMAT_BC1_RGBA_UNORM_BLOCK
;
268 case VK_FORMAT_BC2_SRGB_BLOCK
:
269 return VK_FORMAT_BC2_UNORM_BLOCK
;
270 case VK_FORMAT_BC3_SRGB_BLOCK
:
271 return VK_FORMAT_BC3_UNORM_BLOCK
;
272 case VK_FORMAT_BC7_SRGB_BLOCK
:
273 return VK_FORMAT_BC7_UNORM_BLOCK
;
274 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK
:
275 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
;
276 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK
:
277 return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK
;
278 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK
:
279 return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK
;
281 assert(!vk_format_is_srgb(format
));
286 static inline VkFormat
287 vk_format_stencil_only(VkFormat format
)
289 return VK_FORMAT_S8_UINT
;
292 static inline unsigned
293 vk_format_get_component_bits(VkFormat format
,
294 enum util_format_colorspace colorspace
,
297 return util_format_get_component_bits(vk_format_to_pipe_format(format
),
298 colorspace
, component
);
301 static inline unsigned
302 vk_format_get_nr_components(VkFormat format
)
304 return util_format_get_nr_components(vk_format_to_pipe_format(format
));
307 #endif /* VK_FORMAT_H */