1 /**************************************************************************
3 * Copyright 2009-2010 Vmware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
37 union pipe_color_union
;
46 * Describe how to pack/unpack pixels into/from the prescribed format.
48 * XXX: This could be renamed to something like util_format_pack, or broke down
49 * in flags inside util_format_block that said exactly what we want.
51 enum util_format_layout
{
53 * Formats with util_format_block::width == util_format_block::height == 1
54 * that can be described as an ordinary data structure.
56 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
59 * Formats with sub-sampled channels.
61 * This is for formats like YVYU where there is less than one sample per
64 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
67 * S3 Texture Compression formats.
69 UTIL_FORMAT_LAYOUT_S3TC
= 4,
72 * Red-Green Texture Compression formats.
74 UTIL_FORMAT_LAYOUT_RGTC
= 5,
77 * Ericsson Texture Compression
79 UTIL_FORMAT_LAYOUT_ETC
= 6,
82 * BC6/7 Texture Compression
84 UTIL_FORMAT_LAYOUT_BPTC
= 7,
86 UTIL_FORMAT_LAYOUT_ASTC
= 8,
88 UTIL_FORMAT_LAYOUT_ATC
= 9,
91 * Everything else that doesn't fit in any of the above layouts.
93 UTIL_FORMAT_LAYOUT_OTHER
= 10
97 struct util_format_block
99 /** Block width in pixels */
102 /** Block height in pixels */
105 /** Block depth in pixels */
108 /** Block size in bits */
113 enum util_format_type
{
114 UTIL_FORMAT_TYPE_VOID
= 0,
115 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
116 UTIL_FORMAT_TYPE_SIGNED
= 2,
117 UTIL_FORMAT_TYPE_FIXED
= 3,
118 UTIL_FORMAT_TYPE_FLOAT
= 4
122 enum util_format_colorspace
{
123 UTIL_FORMAT_COLORSPACE_RGB
= 0,
124 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
125 UTIL_FORMAT_COLORSPACE_YUV
= 2,
126 UTIL_FORMAT_COLORSPACE_ZS
= 3
130 struct util_format_channel_description
132 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
133 unsigned normalized
:1;
134 unsigned pure_integer
:1;
135 unsigned size
:9; /**< bits per channel */
136 unsigned shift
:16; /** number of bits from lsb */
140 struct util_format_description
142 enum pipe_format format
;
147 * Short name, striped of the prefix, lower case.
149 const char *short_name
;
152 * Pixel block dimensions.
154 struct util_format_block block
;
156 enum util_format_layout layout
;
159 * The number of channels.
161 unsigned nr_channels
:3;
164 * Whether all channels have the same number of (whole) bytes and type.
169 * Whether the pixel format can be described as a bitfield structure.
172 * - pixel depth must be 8, 16, or 32 bits;
173 * - all channels must be unsigned, signed, or void
175 unsigned is_bitmask
:1;
178 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
183 * Whether the format contains UNORM channels
188 * Whether the format contains SNORM channels
193 * Input channel description, in the order XYZW.
195 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
197 * If each channel is accessed as an individual N-byte value, X is always
198 * at the lowest address in memory, Y is always next, and so on. For all
199 * currently-defined formats, the N-byte value has native endianness.
201 * If instead a group of channels is accessed as a single N-byte value,
202 * the order of the channels within that value depends on endianness.
203 * For big-endian targets, X is the most significant subvalue,
204 * otherwise it is the least significant one.
206 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
209 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
210 * big-endian: X Yu Ym Yl
212 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
216 * little-endian: YYYXXXXX WZZZZZYY
217 * big-endian: XXXXXYYY YYZZZZZW
219 struct util_format_channel_description channel
[4];
222 * Output channel swizzle.
224 * The order is either:
228 * depending on the colorspace.
230 unsigned char swizzle
[4];
233 * Colorspace transformation.
235 enum util_format_colorspace colorspace
;
238 * Unpack pixel blocks to R8G8B8A8_UNORM.
239 * Note: strides are in bytes.
241 * Only defined for non-depth-stencil formats.
244 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
245 const uint8_t *src
, unsigned src_stride
,
246 unsigned width
, unsigned height
);
249 * Pack pixel blocks from R8G8B8A8_UNORM.
250 * Note: strides are in bytes.
252 * Only defined for non-depth-stencil formats.
255 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
256 const uint8_t *src
, unsigned src_stride
,
257 unsigned width
, unsigned height
);
260 * Fetch a single pixel (i, j) from a block.
262 * XXX: Only defined for a very few select formats.
265 (*fetch_rgba_8unorm
)(uint8_t *dst
,
267 unsigned i
, unsigned j
);
270 * Unpack pixel blocks to R32G32B32A32_FLOAT.
271 * Note: strides are in bytes.
273 * Only defined for non-depth-stencil formats.
276 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
277 const uint8_t *src
, unsigned src_stride
,
278 unsigned width
, unsigned height
);
281 * Pack pixel blocks from R32G32B32A32_FLOAT.
282 * Note: strides are in bytes.
284 * Only defined for non-depth-stencil formats.
287 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
288 const float *src
, unsigned src_stride
,
289 unsigned width
, unsigned height
);
292 * Fetch a single pixel (i, j) from a block.
294 * Only defined for non-depth-stencil and non-integer formats.
297 (*fetch_rgba_float
)(float *dst
,
299 unsigned i
, unsigned j
);
302 * Unpack pixels to Z32_UNORM.
303 * Note: strides are in bytes.
305 * Only defined for depth formats.
308 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
309 const uint8_t *src
, unsigned src_stride
,
310 unsigned width
, unsigned height
);
313 * Pack pixels from Z32_FLOAT.
314 * Note: strides are in bytes.
316 * Only defined for depth formats.
319 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
320 const uint32_t *src
, unsigned src_stride
,
321 unsigned width
, unsigned height
);
324 * Unpack pixels to Z32_FLOAT.
325 * Note: strides are in bytes.
327 * Only defined for depth formats.
330 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
331 const uint8_t *src
, unsigned src_stride
,
332 unsigned width
, unsigned height
);
335 * Pack pixels from Z32_FLOAT.
336 * Note: strides are in bytes.
338 * Only defined for depth formats.
341 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
342 const float *src
, unsigned src_stride
,
343 unsigned width
, unsigned height
);
346 * Unpack pixels to S8_UINT.
347 * Note: strides are in bytes.
349 * Only defined for stencil formats.
352 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
353 const uint8_t *src
, unsigned src_stride
,
354 unsigned width
, unsigned height
);
357 * Pack pixels from S8_UINT.
358 * Note: strides are in bytes.
360 * Only defined for stencil formats.
363 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
364 const uint8_t *src
, unsigned src_stride
,
365 unsigned width
, unsigned height
);
368 * Unpack pixel blocks to R32G32B32A32_UINT.
369 * Note: strides are in bytes.
371 * Only defined for INT formats.
374 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
375 const uint8_t *src
, unsigned src_stride
,
376 unsigned width
, unsigned height
);
379 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
380 const uint32_t *src
, unsigned src_stride
,
381 unsigned width
, unsigned height
);
384 * Unpack pixel blocks to R32G32B32A32_SINT.
385 * Note: strides are in bytes.
387 * Only defined for INT formats.
390 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
391 const uint8_t *src
, unsigned src_stride
,
392 unsigned width
, unsigned height
);
395 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
396 const int32_t *src
, unsigned src_stride
,
397 unsigned width
, unsigned height
);
400 * Fetch a single pixel (i, j) from a block.
402 * Only defined for unsigned (pure) integer formats.
405 (*fetch_rgba_uint
)(uint32_t *dst
,
407 unsigned i
, unsigned j
);
410 * Fetch a single pixel (i, j) from a block.
412 * Only defined for signed (pure) integer formats.
415 (*fetch_rgba_sint
)(int32_t *dst
,
417 unsigned i
, unsigned j
);
421 extern const struct util_format_description
422 util_format_description_table
[];
425 const struct util_format_description
*
426 util_format_description(enum pipe_format format
);
430 * Format query functions.
433 static inline const char *
434 util_format_name(enum pipe_format format
)
436 const struct util_format_description
*desc
= util_format_description(format
);
440 return "PIPE_FORMAT_???";
446 static inline const char *
447 util_format_short_name(enum pipe_format format
)
449 const struct util_format_description
*desc
= util_format_description(format
);
456 return desc
->short_name
;
460 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
462 static inline boolean
463 util_format_is_plain(enum pipe_format format
)
465 const struct util_format_description
*desc
= util_format_description(format
);
471 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
474 static inline boolean
475 util_format_is_compressed(enum pipe_format format
)
477 const struct util_format_description
*desc
= util_format_description(format
);
484 switch (desc
->layout
) {
485 case UTIL_FORMAT_LAYOUT_S3TC
:
486 case UTIL_FORMAT_LAYOUT_RGTC
:
487 case UTIL_FORMAT_LAYOUT_ETC
:
488 case UTIL_FORMAT_LAYOUT_BPTC
:
489 case UTIL_FORMAT_LAYOUT_ASTC
:
490 case UTIL_FORMAT_LAYOUT_ATC
:
491 /* XXX add other formats in the future */
498 static inline boolean
499 util_format_is_s3tc(enum pipe_format format
)
501 const struct util_format_description
*desc
= util_format_description(format
);
508 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
511 static inline boolean
512 util_format_is_etc(enum pipe_format format
)
514 const struct util_format_description
*desc
= util_format_description(format
);
521 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
524 static inline boolean
525 util_format_is_srgb(enum pipe_format format
)
527 const struct util_format_description
*desc
= util_format_description(format
);
528 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
531 static inline boolean
532 util_format_has_depth(const struct util_format_description
*desc
)
534 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
535 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
538 static inline boolean
539 util_format_has_stencil(const struct util_format_description
*desc
)
541 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
542 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
545 static inline boolean
546 util_format_is_depth_or_stencil(enum pipe_format format
)
548 const struct util_format_description
*desc
= util_format_description(format
);
555 return util_format_has_depth(desc
) ||
556 util_format_has_stencil(desc
);
559 static inline boolean
560 util_format_is_depth_and_stencil(enum pipe_format format
)
562 const struct util_format_description
*desc
= util_format_description(format
);
569 return util_format_has_depth(desc
) &&
570 util_format_has_stencil(desc
);
574 * For depth-stencil formats, return the equivalent depth-only format.
576 static inline enum pipe_format
577 util_format_get_depth_only(enum pipe_format format
)
580 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
581 return PIPE_FORMAT_Z24X8_UNORM
;
583 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
584 return PIPE_FORMAT_X8Z24_UNORM
;
586 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
587 return PIPE_FORMAT_Z32_FLOAT
;
594 static inline boolean
595 util_format_is_yuv(enum pipe_format format
)
597 const struct util_format_description
*desc
= util_format_description(format
);
604 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
608 * Calculates the depth format type based upon the incoming format description.
610 static inline unsigned
611 util_get_depth_format_type(const struct util_format_description
*desc
)
613 unsigned depth_channel
= desc
->swizzle
[0];
614 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
615 depth_channel
!= PIPE_SWIZZLE_NONE
) {
616 return desc
->channel
[depth_channel
].type
;
618 return UTIL_FORMAT_TYPE_VOID
;
624 * Calculates the MRD for the depth format. MRD is used in depth bias
625 * for UNORM and unbound depth buffers. When the depth buffer is floating
626 * point, the depth bias calculation does not use the MRD. However, the
627 * default MRD will be 1.0 / ((1 << 24) - 1).
630 util_get_depth_format_mrd(const struct util_format_description
*desc
);
634 * Return whether this is an RGBA, Z, S, or combined ZS format.
635 * Useful for initializing pipe_blit_info::mask.
637 static inline unsigned
638 util_format_get_mask(enum pipe_format format
)
640 const struct util_format_description
*desc
=
641 util_format_description(format
);
646 if (util_format_has_depth(desc
)) {
647 if (util_format_has_stencil(desc
)) {
653 if (util_format_has_stencil(desc
)) {
656 return PIPE_MASK_RGBA
;
662 * Give the RGBA colormask of the channels that can be represented in this
665 * That is, the channels whose values are preserved.
667 static inline unsigned
668 util_format_colormask(const struct util_format_description
*desc
)
673 switch (desc
->colorspace
) {
674 case UTIL_FORMAT_COLORSPACE_RGB
:
675 case UTIL_FORMAT_COLORSPACE_SRGB
:
676 case UTIL_FORMAT_COLORSPACE_YUV
:
678 for (chan
= 0; chan
< 4; ++chan
) {
679 if (desc
->swizzle
[chan
] < 4) {
680 colormask
|= (1 << chan
);
684 case UTIL_FORMAT_COLORSPACE_ZS
:
694 * Checks if color mask covers every channel for the specified format
696 * @param desc a format description to check colormask with
697 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
699 static inline boolean
700 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
702 return (~colormask
& util_format_colormask(desc
)) == 0;
707 util_format_is_float(enum pipe_format format
);
711 util_format_has_alpha(enum pipe_format format
);
715 util_format_is_luminance(enum pipe_format format
);
718 util_format_is_alpha(enum pipe_format format
);
721 util_format_is_luminance_alpha(enum pipe_format format
);
725 util_format_is_intensity(enum pipe_format format
);
728 util_format_is_subsampled_422(enum pipe_format format
);
731 util_format_is_pure_integer(enum pipe_format format
);
734 util_format_is_pure_sint(enum pipe_format format
);
737 util_format_is_pure_uint(enum pipe_format format
);
740 util_format_is_snorm(enum pipe_format format
);
743 util_format_is_unorm(enum pipe_format format
);
746 util_format_is_snorm8(enum pipe_format format
);
749 * Check if the src format can be blitted to the destination format with
750 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
754 util_is_format_compatible(const struct util_format_description
*src_desc
,
755 const struct util_format_description
*dst_desc
);
758 * Whether this format is a rgab8 variant.
760 * That is, any format that matches the
762 * PIPE_FORMAT_?8?8?8?8_UNORM
764 static inline boolean
765 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
769 if(desc
->block
.width
!= 1 ||
770 desc
->block
.height
!= 1 ||
771 desc
->block
.bits
!= 32)
774 for(chan
= 0; chan
< 4; ++chan
) {
775 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
776 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
778 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
779 !desc
->channel
[chan
].normalized
)
781 if(desc
->channel
[chan
].size
!= 8)
789 * Return total bits needed for the pixel format per block.
792 util_format_get_blocksizebits(enum pipe_format format
)
794 const struct util_format_description
*desc
= util_format_description(format
);
801 return desc
->block
.bits
;
805 * Return bytes per block (not pixel) for the given format.
808 util_format_get_blocksize(enum pipe_format format
)
810 uint bits
= util_format_get_blocksizebits(format
);
811 uint bytes
= bits
/ 8;
813 assert(bits
% 8 == 0);
823 util_format_get_blockwidth(enum pipe_format format
)
825 const struct util_format_description
*desc
= util_format_description(format
);
832 return desc
->block
.width
;
836 util_format_get_blockheight(enum pipe_format format
)
838 const struct util_format_description
*desc
= util_format_description(format
);
845 return desc
->block
.height
;
849 util_format_get_blockdepth(enum pipe_format format
)
851 const struct util_format_description
*desc
= util_format_description(format
);
858 return desc
->block
.depth
;
861 static inline unsigned
862 util_format_get_nblocksx(enum pipe_format format
,
865 unsigned blockwidth
= util_format_get_blockwidth(format
);
866 return (x
+ blockwidth
- 1) / blockwidth
;
869 static inline unsigned
870 util_format_get_nblocksy(enum pipe_format format
,
873 unsigned blockheight
= util_format_get_blockheight(format
);
874 return (y
+ blockheight
- 1) / blockheight
;
877 static inline unsigned
878 util_format_get_nblocksz(enum pipe_format format
,
881 unsigned blockdepth
= util_format_get_blockdepth(format
);
882 return (z
+ blockdepth
- 1) / blockdepth
;
885 static inline unsigned
886 util_format_get_nblocks(enum pipe_format format
,
890 assert(util_format_get_blockdepth(format
) == 1);
891 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
895 util_format_get_stride(enum pipe_format format
,
898 return (size_t)util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
902 util_format_get_2d_size(enum pipe_format format
,
906 return util_format_get_nblocksy(format
, height
) * stride
;
910 util_format_get_component_bits(enum pipe_format format
,
911 enum util_format_colorspace colorspace
,
914 const struct util_format_description
*desc
= util_format_description(format
);
915 enum util_format_colorspace desc_colorspace
;
922 assert(component
< 4);
924 /* Treat RGB and SRGB as equivalent. */
925 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
926 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
928 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
929 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
931 desc_colorspace
= desc
->colorspace
;
934 if (desc_colorspace
!= colorspace
) {
938 switch (desc
->swizzle
[component
]) {
940 return desc
->channel
[0].size
;
942 return desc
->channel
[1].size
;
944 return desc
->channel
[2].size
;
946 return desc
->channel
[3].size
;
953 * Given a linear RGB colorspace format, return the corresponding SRGB
954 * format, or PIPE_FORMAT_NONE if none.
956 static inline enum pipe_format
957 util_format_srgb(enum pipe_format format
)
959 if (util_format_is_srgb(format
))
963 case PIPE_FORMAT_L8_UNORM
:
964 return PIPE_FORMAT_L8_SRGB
;
965 case PIPE_FORMAT_R8_UNORM
:
966 return PIPE_FORMAT_R8_SRGB
;
967 case PIPE_FORMAT_L8A8_UNORM
:
968 return PIPE_FORMAT_L8A8_SRGB
;
969 case PIPE_FORMAT_R8G8B8_UNORM
:
970 return PIPE_FORMAT_R8G8B8_SRGB
;
971 case PIPE_FORMAT_A8B8G8R8_UNORM
:
972 return PIPE_FORMAT_A8B8G8R8_SRGB
;
973 case PIPE_FORMAT_X8B8G8R8_UNORM
:
974 return PIPE_FORMAT_X8B8G8R8_SRGB
;
975 case PIPE_FORMAT_B8G8R8A8_UNORM
:
976 return PIPE_FORMAT_B8G8R8A8_SRGB
;
977 case PIPE_FORMAT_B8G8R8X8_UNORM
:
978 return PIPE_FORMAT_B8G8R8X8_SRGB
;
979 case PIPE_FORMAT_A8R8G8B8_UNORM
:
980 return PIPE_FORMAT_A8R8G8B8_SRGB
;
981 case PIPE_FORMAT_X8R8G8B8_UNORM
:
982 return PIPE_FORMAT_X8R8G8B8_SRGB
;
983 case PIPE_FORMAT_R8G8B8A8_UNORM
:
984 return PIPE_FORMAT_R8G8B8A8_SRGB
;
985 case PIPE_FORMAT_R8G8B8X8_UNORM
:
986 return PIPE_FORMAT_R8G8B8X8_SRGB
;
987 case PIPE_FORMAT_DXT1_RGB
:
988 return PIPE_FORMAT_DXT1_SRGB
;
989 case PIPE_FORMAT_DXT1_RGBA
:
990 return PIPE_FORMAT_DXT1_SRGBA
;
991 case PIPE_FORMAT_DXT3_RGBA
:
992 return PIPE_FORMAT_DXT3_SRGBA
;
993 case PIPE_FORMAT_DXT5_RGBA
:
994 return PIPE_FORMAT_DXT5_SRGBA
;
995 case PIPE_FORMAT_B5G6R5_UNORM
:
996 return PIPE_FORMAT_B5G6R5_SRGB
;
997 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
998 return PIPE_FORMAT_BPTC_SRGBA
;
999 case PIPE_FORMAT_ETC2_RGB8
:
1000 return PIPE_FORMAT_ETC2_SRGB8
;
1001 case PIPE_FORMAT_ETC2_RGB8A1
:
1002 return PIPE_FORMAT_ETC2_SRGB8A1
;
1003 case PIPE_FORMAT_ETC2_RGBA8
:
1004 return PIPE_FORMAT_ETC2_SRGBA8
;
1005 case PIPE_FORMAT_ASTC_4x4
:
1006 return PIPE_FORMAT_ASTC_4x4_SRGB
;
1007 case PIPE_FORMAT_ASTC_5x4
:
1008 return PIPE_FORMAT_ASTC_5x4_SRGB
;
1009 case PIPE_FORMAT_ASTC_5x5
:
1010 return PIPE_FORMAT_ASTC_5x5_SRGB
;
1011 case PIPE_FORMAT_ASTC_6x5
:
1012 return PIPE_FORMAT_ASTC_6x5_SRGB
;
1013 case PIPE_FORMAT_ASTC_6x6
:
1014 return PIPE_FORMAT_ASTC_6x6_SRGB
;
1015 case PIPE_FORMAT_ASTC_8x5
:
1016 return PIPE_FORMAT_ASTC_8x5_SRGB
;
1017 case PIPE_FORMAT_ASTC_8x6
:
1018 return PIPE_FORMAT_ASTC_8x6_SRGB
;
1019 case PIPE_FORMAT_ASTC_8x8
:
1020 return PIPE_FORMAT_ASTC_8x8_SRGB
;
1021 case PIPE_FORMAT_ASTC_10x5
:
1022 return PIPE_FORMAT_ASTC_10x5_SRGB
;
1023 case PIPE_FORMAT_ASTC_10x6
:
1024 return PIPE_FORMAT_ASTC_10x6_SRGB
;
1025 case PIPE_FORMAT_ASTC_10x8
:
1026 return PIPE_FORMAT_ASTC_10x8_SRGB
;
1027 case PIPE_FORMAT_ASTC_10x10
:
1028 return PIPE_FORMAT_ASTC_10x10_SRGB
;
1029 case PIPE_FORMAT_ASTC_12x10
:
1030 return PIPE_FORMAT_ASTC_12x10_SRGB
;
1031 case PIPE_FORMAT_ASTC_12x12
:
1032 return PIPE_FORMAT_ASTC_12x12_SRGB
;
1035 return PIPE_FORMAT_NONE
;
1040 * Given an sRGB format, return the corresponding linear colorspace format.
1041 * For non sRGB formats, return the format unchanged.
1043 static inline enum pipe_format
1044 util_format_linear(enum pipe_format format
)
1047 case PIPE_FORMAT_L8_SRGB
:
1048 return PIPE_FORMAT_L8_UNORM
;
1049 case PIPE_FORMAT_R8_SRGB
:
1050 return PIPE_FORMAT_R8_UNORM
;
1051 case PIPE_FORMAT_L8A8_SRGB
:
1052 return PIPE_FORMAT_L8A8_UNORM
;
1053 case PIPE_FORMAT_R8G8B8_SRGB
:
1054 return PIPE_FORMAT_R8G8B8_UNORM
;
1055 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1056 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1057 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1058 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1059 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1060 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1061 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1062 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1063 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1064 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1065 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1066 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1067 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1068 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1069 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1070 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1071 case PIPE_FORMAT_DXT1_SRGB
:
1072 return PIPE_FORMAT_DXT1_RGB
;
1073 case PIPE_FORMAT_DXT1_SRGBA
:
1074 return PIPE_FORMAT_DXT1_RGBA
;
1075 case PIPE_FORMAT_DXT3_SRGBA
:
1076 return PIPE_FORMAT_DXT3_RGBA
;
1077 case PIPE_FORMAT_DXT5_SRGBA
:
1078 return PIPE_FORMAT_DXT5_RGBA
;
1079 case PIPE_FORMAT_B5G6R5_SRGB
:
1080 return PIPE_FORMAT_B5G6R5_UNORM
;
1081 case PIPE_FORMAT_BPTC_SRGBA
:
1082 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1083 case PIPE_FORMAT_ETC2_SRGB8
:
1084 return PIPE_FORMAT_ETC2_RGB8
;
1085 case PIPE_FORMAT_ETC2_SRGB8A1
:
1086 return PIPE_FORMAT_ETC2_RGB8A1
;
1087 case PIPE_FORMAT_ETC2_SRGBA8
:
1088 return PIPE_FORMAT_ETC2_RGBA8
;
1089 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1090 return PIPE_FORMAT_ASTC_4x4
;
1091 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1092 return PIPE_FORMAT_ASTC_5x4
;
1093 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1094 return PIPE_FORMAT_ASTC_5x5
;
1095 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1096 return PIPE_FORMAT_ASTC_6x5
;
1097 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1098 return PIPE_FORMAT_ASTC_6x6
;
1099 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1100 return PIPE_FORMAT_ASTC_8x5
;
1101 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1102 return PIPE_FORMAT_ASTC_8x6
;
1103 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1104 return PIPE_FORMAT_ASTC_8x8
;
1105 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1106 return PIPE_FORMAT_ASTC_10x5
;
1107 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1108 return PIPE_FORMAT_ASTC_10x6
;
1109 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1110 return PIPE_FORMAT_ASTC_10x8
;
1111 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1112 return PIPE_FORMAT_ASTC_10x10
;
1113 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1114 return PIPE_FORMAT_ASTC_12x10
;
1115 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1116 return PIPE_FORMAT_ASTC_12x12
;
1123 * Given a depth-stencil format, return the corresponding stencil-only format.
1124 * For stencil-only formats, return the format unchanged.
1126 static inline enum pipe_format
1127 util_format_stencil_only(enum pipe_format format
)
1130 /* mask out the depth component */
1131 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1132 return PIPE_FORMAT_X24S8_UINT
;
1133 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1134 return PIPE_FORMAT_S8X24_UINT
;
1135 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1136 return PIPE_FORMAT_X32_S8X24_UINT
;
1138 /* stencil only formats */
1139 case PIPE_FORMAT_X24S8_UINT
:
1140 case PIPE_FORMAT_S8X24_UINT
:
1141 case PIPE_FORMAT_X32_S8X24_UINT
:
1142 case PIPE_FORMAT_S8_UINT
:
1147 return PIPE_FORMAT_NONE
;
1152 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1153 * This is identity for non-intensity formats.
1155 static inline enum pipe_format
1156 util_format_intensity_to_red(enum pipe_format format
)
1159 case PIPE_FORMAT_I8_UNORM
:
1160 return PIPE_FORMAT_R8_UNORM
;
1161 case PIPE_FORMAT_I8_SNORM
:
1162 return PIPE_FORMAT_R8_SNORM
;
1163 case PIPE_FORMAT_I16_UNORM
:
1164 return PIPE_FORMAT_R16_UNORM
;
1165 case PIPE_FORMAT_I16_SNORM
:
1166 return PIPE_FORMAT_R16_SNORM
;
1167 case PIPE_FORMAT_I16_FLOAT
:
1168 return PIPE_FORMAT_R16_FLOAT
;
1169 case PIPE_FORMAT_I32_FLOAT
:
1170 return PIPE_FORMAT_R32_FLOAT
;
1171 case PIPE_FORMAT_I8_UINT
:
1172 return PIPE_FORMAT_R8_UINT
;
1173 case PIPE_FORMAT_I8_SINT
:
1174 return PIPE_FORMAT_R8_SINT
;
1175 case PIPE_FORMAT_I16_UINT
:
1176 return PIPE_FORMAT_R16_UINT
;
1177 case PIPE_FORMAT_I16_SINT
:
1178 return PIPE_FORMAT_R16_SINT
;
1179 case PIPE_FORMAT_I32_UINT
:
1180 return PIPE_FORMAT_R32_UINT
;
1181 case PIPE_FORMAT_I32_SINT
:
1182 return PIPE_FORMAT_R32_SINT
;
1184 assert(!util_format_is_intensity(format
));
1190 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1191 * This is identity for non-luminance formats.
1193 static inline enum pipe_format
1194 util_format_luminance_to_red(enum pipe_format format
)
1197 case PIPE_FORMAT_L8_UNORM
:
1198 return PIPE_FORMAT_R8_UNORM
;
1199 case PIPE_FORMAT_L8_SNORM
:
1200 return PIPE_FORMAT_R8_SNORM
;
1201 case PIPE_FORMAT_L16_UNORM
:
1202 return PIPE_FORMAT_R16_UNORM
;
1203 case PIPE_FORMAT_L16_SNORM
:
1204 return PIPE_FORMAT_R16_SNORM
;
1205 case PIPE_FORMAT_L16_FLOAT
:
1206 return PIPE_FORMAT_R16_FLOAT
;
1207 case PIPE_FORMAT_L32_FLOAT
:
1208 return PIPE_FORMAT_R32_FLOAT
;
1209 case PIPE_FORMAT_L8_UINT
:
1210 return PIPE_FORMAT_R8_UINT
;
1211 case PIPE_FORMAT_L8_SINT
:
1212 return PIPE_FORMAT_R8_SINT
;
1213 case PIPE_FORMAT_L16_UINT
:
1214 return PIPE_FORMAT_R16_UINT
;
1215 case PIPE_FORMAT_L16_SINT
:
1216 return PIPE_FORMAT_R16_SINT
;
1217 case PIPE_FORMAT_L32_UINT
:
1218 return PIPE_FORMAT_R32_UINT
;
1219 case PIPE_FORMAT_L32_SINT
:
1220 return PIPE_FORMAT_R32_SINT
;
1222 case PIPE_FORMAT_LATC1_UNORM
:
1223 return PIPE_FORMAT_RGTC1_UNORM
;
1224 case PIPE_FORMAT_LATC1_SNORM
:
1225 return PIPE_FORMAT_RGTC1_SNORM
;
1227 case PIPE_FORMAT_L4A4_UNORM
:
1228 return PIPE_FORMAT_R4A4_UNORM
;
1230 case PIPE_FORMAT_L8A8_UNORM
:
1231 return PIPE_FORMAT_R8A8_UNORM
;
1232 case PIPE_FORMAT_L8A8_SNORM
:
1233 return PIPE_FORMAT_R8A8_SNORM
;
1234 case PIPE_FORMAT_L16A16_UNORM
:
1235 return PIPE_FORMAT_R16A16_UNORM
;
1236 case PIPE_FORMAT_L16A16_SNORM
:
1237 return PIPE_FORMAT_R16A16_SNORM
;
1238 case PIPE_FORMAT_L16A16_FLOAT
:
1239 return PIPE_FORMAT_R16A16_FLOAT
;
1240 case PIPE_FORMAT_L32A32_FLOAT
:
1241 return PIPE_FORMAT_R32A32_FLOAT
;
1242 case PIPE_FORMAT_L8A8_UINT
:
1243 return PIPE_FORMAT_R8A8_UINT
;
1244 case PIPE_FORMAT_L8A8_SINT
:
1245 return PIPE_FORMAT_R8A8_SINT
;
1246 case PIPE_FORMAT_L16A16_UINT
:
1247 return PIPE_FORMAT_R16A16_UINT
;
1248 case PIPE_FORMAT_L16A16_SINT
:
1249 return PIPE_FORMAT_R16A16_SINT
;
1250 case PIPE_FORMAT_L32A32_UINT
:
1251 return PIPE_FORMAT_R32A32_UINT
;
1252 case PIPE_FORMAT_L32A32_SINT
:
1253 return PIPE_FORMAT_R32A32_SINT
;
1255 /* We don't have compressed red-alpha variants for these. */
1256 case PIPE_FORMAT_LATC2_UNORM
:
1257 case PIPE_FORMAT_LATC2_SNORM
:
1258 return PIPE_FORMAT_NONE
;
1261 assert(!util_format_is_luminance(format
) &&
1262 !util_format_is_luminance_alpha(format
));
1268 * Return the number of components stored.
1269 * Formats with block size != 1x1 will always have 1 component (the block).
1271 static inline unsigned
1272 util_format_get_nr_components(enum pipe_format format
)
1274 const struct util_format_description
*desc
= util_format_description(format
);
1275 return desc
->nr_channels
;
1279 * Return the index of the first non-void channel
1280 * -1 if no non-void channels
1283 util_format_get_first_non_void_channel(enum pipe_format format
)
1285 const struct util_format_description
*desc
= util_format_description(format
);
1288 for (i
= 0; i
< 4; i
++)
1289 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1299 * Whether this format is any 8-bit UNORM variant. Looser than
1300 * util_is_rgba8_variant (also includes alpha textures, for instance).
1304 util_format_is_unorm8(const struct util_format_description
*desc
)
1306 int c
= util_format_get_first_non_void_channel(desc
->format
);
1311 return desc
->is_unorm
&& desc
->is_array
&& desc
->channel
[c
].size
== 8;
1315 * Format access functions.
1319 util_format_read_4f(enum pipe_format format
,
1320 float *dst
, unsigned dst_stride
,
1321 const void *src
, unsigned src_stride
,
1322 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1325 util_format_write_4f(enum pipe_format format
,
1326 const float *src
, unsigned src_stride
,
1327 void *dst
, unsigned dst_stride
,
1328 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1331 util_format_read_4ub(enum pipe_format format
,
1332 uint8_t *dst
, unsigned dst_stride
,
1333 const void *src
, unsigned src_stride
,
1334 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1337 util_format_write_4ub(enum pipe_format format
,
1338 const uint8_t *src
, unsigned src_stride
,
1339 void *dst
, unsigned dst_stride
,
1340 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1343 util_format_read_4ui(enum pipe_format format
,
1344 unsigned *dst
, unsigned dst_stride
,
1345 const void *src
, unsigned src_stride
,
1346 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1349 util_format_write_4ui(enum pipe_format format
,
1350 const unsigned int *src
, unsigned src_stride
,
1351 void *dst
, unsigned dst_stride
,
1352 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1355 util_format_read_4i(enum pipe_format format
,
1356 int *dst
, unsigned dst_stride
,
1357 const void *src
, unsigned src_stride
,
1358 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1361 util_format_write_4i(enum pipe_format format
,
1362 const int *src
, unsigned src_stride
,
1363 void *dst
, unsigned dst_stride
,
1364 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1367 * Generic format conversion;
1371 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1374 util_format_translate(enum pipe_format dst_format
,
1375 void *dst
, unsigned dst_stride
,
1376 unsigned dst_x
, unsigned dst_y
,
1377 enum pipe_format src_format
,
1378 const void *src
, unsigned src_stride
,
1379 unsigned src_x
, unsigned src_y
,
1380 unsigned width
, unsigned height
);
1383 util_format_translate_3d(enum pipe_format dst_format
,
1384 void *dst
, unsigned dst_stride
,
1385 unsigned dst_slice_stride
,
1386 unsigned dst_x
, unsigned dst_y
,
1388 enum pipe_format src_format
,
1389 const void *src
, unsigned src_stride
,
1390 unsigned src_slice_stride
,
1391 unsigned src_x
, unsigned src_y
,
1392 unsigned src_z
, unsigned width
,
1393 unsigned height
, unsigned depth
);
1396 * Swizzle operations.
1399 /* Compose two sets of swizzles.
1400 * If V is a 4D vector and the function parameters represent functions that
1401 * swizzle vector components, this holds:
1402 * swz2(swz1(V)) = dst(V)
1404 void util_format_compose_swizzles(const unsigned char swz1
[4],
1405 const unsigned char swz2
[4],
1406 unsigned char dst
[4]);
1408 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1409 * to \param src and store the result in \param dst.
1410 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1412 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1413 const union pipe_color_union
*src
,
1414 const unsigned char swz
[4],
1415 const boolean is_integer
);
1417 void pipe_swizzle_4f(float *dst
, const float *src
,
1418 const unsigned char swz
[4]);
1420 void util_format_unswizzle_4f(float *dst
, const float *src
,
1421 const unsigned char swz
[4]);
1424 util_format_snorm8_to_sint8(enum pipe_format format
);
1430 #endif /* ! U_FORMAT_H */