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
34 #include <vulkan/vulkan.h>
35 #include <util/macros.h>
36 enum vk_format_layout
{
38 * Formats with vk_format_block::width == vk_format_block::height == 1
39 * that can be described as an ordinary data structure.
41 VK_FORMAT_LAYOUT_PLAIN
= 0,
44 * Formats with sub-sampled channels.
46 * This is for formats like YVYU where there is less than one sample per
49 VK_FORMAT_LAYOUT_SUBSAMPLED
= 3,
52 * S3 Texture Compression formats.
54 VK_FORMAT_LAYOUT_S3TC
= 4,
57 * Red-Green Texture Compression formats.
59 VK_FORMAT_LAYOUT_RGTC
= 5,
62 * Ericsson Texture Compression
64 VK_FORMAT_LAYOUT_ETC
= 6,
67 * BC6/7 Texture Compression
69 VK_FORMAT_LAYOUT_BPTC
= 7,
74 VK_FORMAT_LAYOUT_ASTC
= 8,
77 * Everything else that doesn't fit in any of the above layouts.
79 VK_FORMAT_LAYOUT_OTHER
= 9
82 struct vk_format_block
84 /** Block width in pixels */
87 /** Block height in pixels */
90 /** Block size in bits */
95 VK_FORMAT_TYPE_VOID
= 0,
96 VK_FORMAT_TYPE_UNSIGNED
= 1,
97 VK_FORMAT_TYPE_SIGNED
= 2,
98 VK_FORMAT_TYPE_FIXED
= 3,
99 VK_FORMAT_TYPE_FLOAT
= 4
103 enum vk_format_colorspace
{
104 VK_FORMAT_COLORSPACE_RGB
= 0,
105 VK_FORMAT_COLORSPACE_SRGB
= 1,
106 VK_FORMAT_COLORSPACE_YUV
= 2,
107 VK_FORMAT_COLORSPACE_ZS
= 3
110 struct vk_format_channel_description
{
112 unsigned normalized
:1;
113 unsigned pure_integer
:1;
119 struct vk_format_description
123 const char *short_name
;
125 struct vk_format_block block
;
126 enum vk_format_layout layout
;
128 unsigned nr_channels
:3;
130 unsigned is_bitmask
:1;
133 struct vk_format_channel_description channel
[4];
135 unsigned char swizzle
[4];
137 enum vk_format_colorspace colorspace
;
140 extern const struct vk_format_description vk_format_description_table
[];
142 const struct vk_format_description
*vk_format_description(VkFormat format
);
145 * Return total bits needed for the pixel format per block.
147 static inline unsigned
148 vk_format_get_blocksizebits(VkFormat format
)
150 const struct vk_format_description
*desc
= vk_format_description(format
);
157 return desc
->block
.bits
;
161 * Return bytes per block (not pixel) for the given format.
163 static inline unsigned
164 vk_format_get_blocksize(VkFormat format
)
166 unsigned bits
= vk_format_get_blocksizebits(format
);
167 unsigned bytes
= bits
/ 8;
169 assert(bits
% 8 == 0);
178 static inline unsigned
179 vk_format_get_blockwidth(VkFormat format
)
181 const struct vk_format_description
*desc
= vk_format_description(format
);
188 return desc
->block
.width
;
191 static inline unsigned
192 vk_format_get_blockheight(VkFormat format
)
194 const struct vk_format_description
*desc
= vk_format_description(format
);
201 return desc
->block
.height
;
205 * Return the index of the first non-void channel
206 * -1 if no non-void channels
209 vk_format_get_first_non_void_channel(VkFormat format
)
211 const struct vk_format_description
*desc
= vk_format_description(format
);
214 for (i
= 0; i
< 4; i
++)
215 if (desc
->channel
[i
].type
!= VK_FORMAT_TYPE_VOID
)
232 VK_SWIZZLE_MAX
, /**< Number of enums counter (must be last) */
235 static inline VkImageAspectFlags
236 vk_format_aspects(VkFormat format
)
239 case VK_FORMAT_UNDEFINED
:
242 case VK_FORMAT_S8_UINT
:
243 return VK_IMAGE_ASPECT_STENCIL_BIT
;
245 case VK_FORMAT_D16_UNORM_S8_UINT
:
246 case VK_FORMAT_D24_UNORM_S8_UINT
:
247 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
248 return VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
;
250 case VK_FORMAT_D16_UNORM
:
251 case VK_FORMAT_X8_D24_UNORM_PACK32
:
252 case VK_FORMAT_D32_SFLOAT
:
253 return VK_IMAGE_ASPECT_DEPTH_BIT
;
256 return VK_IMAGE_ASPECT_COLOR_BIT
;
260 static inline enum vk_swizzle
261 radv_swizzle_conv(VkComponentSwizzle component
, const unsigned char chan
[4], VkComponentSwizzle vk_swiz
)
265 if (vk_swiz
== VK_COMPONENT_SWIZZLE_IDENTITY
)
268 case VK_COMPONENT_SWIZZLE_ZERO
:
270 case VK_COMPONENT_SWIZZLE_ONE
:
272 case VK_COMPONENT_SWIZZLE_R
:
273 for (x
= 0; x
< 4; x
++)
277 case VK_COMPONENT_SWIZZLE_G
:
278 for (x
= 0; x
< 4; x
++)
282 case VK_COMPONENT_SWIZZLE_B
:
283 for (x
= 0; x
< 4; x
++)
287 case VK_COMPONENT_SWIZZLE_A
:
288 for (x
= 0; x
< 4; x
++)
293 unreachable("Illegal swizzle");
297 static inline void vk_format_compose_swizzles(const VkComponentMapping
*mapping
,
298 const unsigned char swz
[4],
299 enum vk_swizzle dst
[4])
301 dst
[0] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_R
, swz
, mapping
->r
);
302 dst
[1] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_G
, swz
, mapping
->g
);
303 dst
[2] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_B
, swz
, mapping
->b
);
304 dst
[3] = radv_swizzle_conv(VK_COMPONENT_SWIZZLE_A
, swz
, mapping
->a
);
308 vk_format_is_compressed(VkFormat format
)
310 const struct vk_format_description
*desc
= vk_format_description(format
);
317 switch (desc
->layout
) {
318 case VK_FORMAT_LAYOUT_S3TC
:
319 case VK_FORMAT_LAYOUT_RGTC
:
320 case VK_FORMAT_LAYOUT_ETC
:
321 case VK_FORMAT_LAYOUT_BPTC
:
322 case VK_FORMAT_LAYOUT_ASTC
:
323 /* XXX add other formats in the future */
331 vk_format_has_depth(const struct vk_format_description
*desc
)
333 return desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
&&
334 desc
->swizzle
[0] != VK_SWIZZLE_NONE
;
338 vk_format_has_stencil(const struct vk_format_description
*desc
)
340 return desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
&&
341 desc
->swizzle
[1] != VK_SWIZZLE_NONE
;
345 vk_format_is_depth_or_stencil(VkFormat format
)
347 const struct vk_format_description
*desc
= vk_format_description(format
);
354 return vk_format_has_depth(desc
) ||
355 vk_format_has_stencil(desc
);
359 vk_format_is_depth(VkFormat format
)
361 const struct vk_format_description
*desc
= vk_format_description(format
);
368 return vk_format_has_depth(desc
);
372 vk_format_is_color(VkFormat format
)
374 return !vk_format_is_depth_or_stencil(format
);
377 static inline VkFormat
378 vk_format_depth_only(VkFormat format
)
381 case VK_FORMAT_D16_UNORM_S8_UINT
:
382 return VK_FORMAT_D16_UNORM
;
383 case VK_FORMAT_D24_UNORM_S8_UINT
:
384 return VK_FORMAT_X8_D24_UNORM_PACK32
;
385 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
386 return VK_FORMAT_D32_SFLOAT
;
393 vk_format_is_int(VkFormat format
)
395 const struct vk_format_description
*desc
= vk_format_description(format
);
396 int channel
= vk_format_get_first_non_void_channel(format
);
398 return channel
>= 0 && desc
->channel
[channel
].pure_integer
;
401 static inline VkFormat
402 vk_format_stencil_only(VkFormat format
)
404 return VK_FORMAT_S8_UINT
;
407 static inline unsigned
408 vk_format_get_component_bits(VkFormat format
,
409 enum vk_format_colorspace colorspace
,
412 const struct vk_format_description
*desc
= vk_format_description(format
);
413 enum vk_format_colorspace desc_colorspace
;
420 assert(component
< 4);
422 /* Treat RGB and SRGB as equivalent. */
423 if (colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
424 colorspace
= VK_FORMAT_COLORSPACE_RGB
;
426 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
427 desc_colorspace
= VK_FORMAT_COLORSPACE_RGB
;
429 desc_colorspace
= desc
->colorspace
;
432 if (desc_colorspace
!= colorspace
) {
436 switch (desc
->swizzle
[component
]) {
438 return desc
->channel
[0].size
;
440 return desc
->channel
[1].size
;
442 return desc
->channel
[2].size
;
444 return desc
->channel
[3].size
;