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 DEALINGS
31 #include <vulkan/vulkan.h>
32 #include <util/macros.h>
34 enum vk_format_layout
{
36 * Formats with vk_format_block::width == vk_format_block::height == 1
37 * that can be described as an ordinary data structure.
39 VK_FORMAT_LAYOUT_PLAIN
= 0,
42 * Formats with sub-sampled channels.
44 * This is for formats like YVYU where there is less than one sample per
47 VK_FORMAT_LAYOUT_SUBSAMPLED
= 3,
50 * S3 Texture Compression formats.
52 VK_FORMAT_LAYOUT_S3TC
= 4,
55 * Red-Green Texture Compression formats.
57 VK_FORMAT_LAYOUT_RGTC
= 5,
60 * Ericsson Texture Compression
62 VK_FORMAT_LAYOUT_ETC
= 6,
65 * BC6/7 Texture Compression
67 VK_FORMAT_LAYOUT_BPTC
= 7,
72 VK_FORMAT_LAYOUT_ASTC
= 8,
75 * Everything else that doesn't fit in any of the above layouts.
77 VK_FORMAT_LAYOUT_OTHER
= 9
80 struct vk_format_block
82 /** Block width in pixels */
85 /** Block height in pixels */
88 /** Block size in bits */
93 VK_FORMAT_TYPE_VOID
= 0,
94 VK_FORMAT_TYPE_UNSIGNED
= 1,
95 VK_FORMAT_TYPE_SIGNED
= 2,
96 VK_FORMAT_TYPE_FIXED
= 3,
97 VK_FORMAT_TYPE_FLOAT
= 4
101 enum vk_format_colorspace
{
102 VK_FORMAT_COLORSPACE_RGB
= 0,
103 VK_FORMAT_COLORSPACE_SRGB
= 1,
104 VK_FORMAT_COLORSPACE_YUV
= 2,
105 VK_FORMAT_COLORSPACE_ZS
= 3
108 struct vk_format_channel_description
{
110 unsigned normalized
:1;
111 unsigned pure_integer
:1;
117 struct vk_format_description
121 const char *short_name
;
123 struct vk_format_block block
;
124 enum vk_format_layout layout
;
126 unsigned nr_channels
:3;
128 unsigned is_bitmask
:1;
131 struct vk_format_channel_description channel
[4];
133 unsigned char swizzle
[4];
135 enum vk_format_colorspace colorspace
;
138 extern const struct vk_format_description vk_format_description_table
[];
140 const struct vk_format_description
*vk_format_description(VkFormat format
);
143 * Return total bits needed for the pixel format per block.
145 static inline unsigned
146 vk_format_get_blocksizebits(VkFormat format
)
148 const struct vk_format_description
*desc
= vk_format_description(format
);
155 return desc
->block
.bits
;
159 * Return bytes per block (not pixel) for the given format.
161 static inline unsigned
162 vk_format_get_blocksize(VkFormat format
)
164 unsigned bits
= vk_format_get_blocksizebits(format
);
165 unsigned bytes
= bits
/ 8;
167 assert(bits
% 8 == 0);
176 static inline unsigned
177 vk_format_get_blockwidth(VkFormat format
)
179 const struct vk_format_description
*desc
= vk_format_description(format
);
186 return desc
->block
.width
;
189 static inline unsigned
190 vk_format_get_blockheight(VkFormat format
)
192 const struct vk_format_description
*desc
= vk_format_description(format
);
199 return desc
->block
.height
;
203 * Return the index of the first non-void channel
204 * -1 if no non-void channels
207 vk_format_get_first_non_void_channel(VkFormat format
)
209 const struct vk_format_description
*desc
= vk_format_description(format
);
212 for (i
= 0; i
< 4; i
++)
213 if (desc
->channel
[i
].type
!= VK_FORMAT_TYPE_VOID
)
230 VK_SWIZZLE_MAX
, /**< Number of enums counter (must be last) */
233 static inline VkImageAspectFlags
234 vk_format_aspects(VkFormat format
)
237 case VK_FORMAT_UNDEFINED
:
240 case VK_FORMAT_S8_UINT
:
241 return VK_IMAGE_ASPECT_STENCIL_BIT
;
243 case VK_FORMAT_D16_UNORM_S8_UINT
:
244 case VK_FORMAT_D24_UNORM_S8_UINT
:
245 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
246 return VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
;
248 case VK_FORMAT_D16_UNORM
:
249 case VK_FORMAT_X8_D24_UNORM_PACK32
:
250 case VK_FORMAT_D32_SFLOAT
:
251 return VK_IMAGE_ASPECT_DEPTH_BIT
;
254 return VK_IMAGE_ASPECT_COLOR_BIT
;
258 static inline enum vk_swizzle
259 radv_swizzle_conv(VkComponentSwizzle component
, const unsigned char chan
[4], VkComponentSwizzle vk_swiz
)
263 if (vk_swiz
== VK_COMPONENT_SWIZZLE_IDENTITY
)
266 case VK_COMPONENT_SWIZZLE_ZERO
:
268 case VK_COMPONENT_SWIZZLE_ONE
:
270 case VK_COMPONENT_SWIZZLE_R
:
271 for (x
= 0; x
< 4; x
++)
275 case VK_COMPONENT_SWIZZLE_G
:
276 for (x
= 0; x
< 4; x
++)
280 case VK_COMPONENT_SWIZZLE_B
:
281 for (x
= 0; x
< 4; x
++)
285 case VK_COMPONENT_SWIZZLE_A
:
286 for (x
= 0; x
< 4; x
++)
291 unreachable("Illegal swizzle");
295 static inline void vk_format_compose_swizzles(const VkComponentMapping
*mapping
,
296 const unsigned char swz
[4],
297 enum vk_swizzle dst
[4])
299 dst
[0] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_R
, swz
, mapping
->r
);
300 dst
[1] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_G
, swz
, mapping
->g
);
301 dst
[2] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_B
, swz
, mapping
->b
);
302 dst
[3] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_A
, swz
, mapping
->a
);
306 vk_format_is_compressed(VkFormat format
)
308 const struct vk_format_description
*desc
= vk_format_description(format
);
315 switch (desc
->layout
) {
316 case VK_FORMAT_LAYOUT_S3TC
:
317 case VK_FORMAT_LAYOUT_RGTC
:
318 case VK_FORMAT_LAYOUT_ETC
:
319 case VK_FORMAT_LAYOUT_BPTC
:
320 case VK_FORMAT_LAYOUT_ASTC
:
321 /* XXX add other formats in the future */
329 vk_format_has_depth(const struct vk_format_description
*desc
)
331 return desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
&&
332 desc
->swizzle
[0] != VK_SWIZZLE_NONE
;
336 vk_format_has_stencil(const struct vk_format_description
*desc
)
338 return desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
&&
339 desc
->swizzle
[1] != VK_SWIZZLE_NONE
;
343 vk_format_is_depth_or_stencil(VkFormat format
)
345 const struct vk_format_description
*desc
= vk_format_description(format
);
352 return vk_format_has_depth(desc
) ||
353 vk_format_has_stencil(desc
);
357 vk_format_is_depth(VkFormat format
)
359 const struct vk_format_description
*desc
= vk_format_description(format
);
366 return vk_format_has_depth(desc
);
370 vk_format_is_stencil(VkFormat format
)
372 const struct vk_format_description
*desc
= vk_format_description(format
);
379 return vk_format_has_stencil(desc
);
383 vk_format_is_color(VkFormat format
)
385 return !vk_format_is_depth_or_stencil(format
);
388 static inline VkFormat
389 vk_format_depth_only(VkFormat format
)
392 case VK_FORMAT_D16_UNORM_S8_UINT
:
393 return VK_FORMAT_D16_UNORM
;
394 case VK_FORMAT_D24_UNORM_S8_UINT
:
395 return VK_FORMAT_X8_D24_UNORM_PACK32
;
396 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
397 return VK_FORMAT_D32_SFLOAT
;
404 vk_format_is_int(VkFormat format
)
406 const struct vk_format_description
*desc
= vk_format_description(format
);
407 int channel
= vk_format_get_first_non_void_channel(format
);
409 return channel
>= 0 && desc
->channel
[channel
].pure_integer
;
413 vk_format_is_srgb(VkFormat format
)
415 const struct vk_format_description
*desc
= vk_format_description(format
);
416 return desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
;
419 static inline VkFormat
420 vk_format_stencil_only(VkFormat format
)
422 return VK_FORMAT_S8_UINT
;
425 static inline unsigned
426 vk_format_get_component_bits(VkFormat format
,
427 enum vk_format_colorspace colorspace
,
430 const struct vk_format_description
*desc
= vk_format_description(format
);
431 enum vk_format_colorspace desc_colorspace
;
438 assert(component
< 4);
440 /* Treat RGB and SRGB as equivalent. */
441 if (colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
442 colorspace
= VK_FORMAT_COLORSPACE_RGB
;
444 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
445 desc_colorspace
= VK_FORMAT_COLORSPACE_RGB
;
447 desc_colorspace
= desc
->colorspace
;
450 if (desc_colorspace
!= colorspace
) {
454 switch (desc
->swizzle
[component
]) {
456 return desc
->channel
[0].size
;
458 return desc
->channel
[1].size
;
460 return desc
->channel
[2].size
;
462 return desc
->channel
[3].size
;
468 static inline VkFormat
469 vk_to_non_srgb_format(VkFormat format
)
472 case VK_FORMAT_R8_SRGB
:
473 return VK_FORMAT_R8_UNORM
;
474 case VK_FORMAT_R8G8_SRGB
:
475 return VK_FORMAT_R8G8_UNORM
;
476 case VK_FORMAT_R8G8B8_SRGB
:
477 return VK_FORMAT_R8G8B8_UNORM
;
478 case VK_FORMAT_B8G8R8_SRGB
:
479 return VK_FORMAT_B8G8R8_UNORM
;
480 case VK_FORMAT_R8G8B8A8_SRGB
:
481 return VK_FORMAT_R8G8B8A8_UNORM
;
482 case VK_FORMAT_B8G8R8A8_SRGB
:
483 return VK_FORMAT_B8G8R8A8_UNORM
;
484 case VK_FORMAT_A8B8G8R8_SRGB_PACK32
:
485 return VK_FORMAT_A8B8G8R8_UNORM_PACK32
;
491 #endif /* VK_FORMAT_H */