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,
89 UTIL_FORMAT_LAYOUT_ASTC
= 8,
94 UTIL_FORMAT_LAYOUT_ATC
= 9,
97 * Everything else that doesn't fit in any of the above layouts.
99 UTIL_FORMAT_LAYOUT_OTHER
= 10
103 struct util_format_block
105 /** Block width in pixels */
108 /** Block height in pixels */
111 /** Block size in bits */
116 enum util_format_type
{
117 UTIL_FORMAT_TYPE_VOID
= 0,
118 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
119 UTIL_FORMAT_TYPE_SIGNED
= 2,
120 UTIL_FORMAT_TYPE_FIXED
= 3,
121 UTIL_FORMAT_TYPE_FLOAT
= 4
125 enum util_format_colorspace
{
126 UTIL_FORMAT_COLORSPACE_RGB
= 0,
127 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
128 UTIL_FORMAT_COLORSPACE_YUV
= 2,
129 UTIL_FORMAT_COLORSPACE_ZS
= 3
133 struct util_format_channel_description
135 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
136 unsigned normalized
:1;
137 unsigned pure_integer
:1;
138 unsigned size
:9; /**< bits per channel */
139 unsigned shift
:16; /** number of bits from lsb */
143 struct util_format_description
145 enum pipe_format format
;
150 * Short name, striped of the prefix, lower case.
152 const char *short_name
;
155 * Pixel block dimensions.
157 struct util_format_block block
;
159 enum util_format_layout layout
;
162 * The number of channels.
164 unsigned nr_channels
:3;
167 * Whether all channels have the same number of (whole) bytes and type.
172 * Whether the pixel format can be described as a bitfield structure.
175 * - pixel depth must be 8, 16, or 32 bits;
176 * - all channels must be unsigned, signed, or void
178 unsigned is_bitmask
:1;
181 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
186 * Whether the format contains UNORM channels
191 * Whether the format contains SNORM channels
196 * Input channel description, in the order XYZW.
198 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
200 * If each channel is accessed as an individual N-byte value, X is always
201 * at the lowest address in memory, Y is always next, and so on. For all
202 * currently-defined formats, the N-byte value has native endianness.
204 * If instead a group of channels is accessed as a single N-byte value,
205 * the order of the channels within that value depends on endianness.
206 * For big-endian targets, X is the most significant subvalue,
207 * otherwise it is the least significant one.
209 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
212 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
213 * big-endian: X Yu Ym Yl
215 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
219 * little-endian: YYYXXXXX WZZZZZYY
220 * big-endian: XXXXXYYY YYZZZZZW
222 struct util_format_channel_description channel
[4];
225 * Output channel swizzle.
227 * The order is either:
231 * depending on the colorspace.
233 unsigned char swizzle
[4];
236 * Colorspace transformation.
238 enum util_format_colorspace colorspace
;
241 * Unpack pixel blocks to R8G8B8A8_UNORM.
242 * Note: strides are in bytes.
244 * Only defined for non-depth-stencil formats.
247 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
248 const uint8_t *src
, unsigned src_stride
,
249 unsigned width
, unsigned height
);
252 * Pack pixel blocks from R8G8B8A8_UNORM.
253 * Note: strides are in bytes.
255 * Only defined for non-depth-stencil formats.
258 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
259 const uint8_t *src
, unsigned src_stride
,
260 unsigned width
, unsigned height
);
263 * Fetch a single pixel (i, j) from a block.
265 * XXX: Only defined for a very few select formats.
268 (*fetch_rgba_8unorm
)(uint8_t *dst
,
270 unsigned i
, unsigned j
);
273 * Unpack pixel blocks to R32G32B32A32_FLOAT.
274 * Note: strides are in bytes.
276 * Only defined for non-depth-stencil formats.
279 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
280 const uint8_t *src
, unsigned src_stride
,
281 unsigned width
, unsigned height
);
284 * Pack pixel blocks from R32G32B32A32_FLOAT.
285 * Note: strides are in bytes.
287 * Only defined for non-depth-stencil formats.
290 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
291 const float *src
, unsigned src_stride
,
292 unsigned width
, unsigned height
);
295 * Fetch a single pixel (i, j) from a block.
297 * Only defined for non-depth-stencil and non-integer formats.
300 (*fetch_rgba_float
)(float *dst
,
302 unsigned i
, unsigned j
);
305 * Unpack pixels to Z32_UNORM.
306 * Note: strides are in bytes.
308 * Only defined for depth formats.
311 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
312 const uint8_t *src
, unsigned src_stride
,
313 unsigned width
, unsigned height
);
316 * Pack pixels from Z32_FLOAT.
317 * Note: strides are in bytes.
319 * Only defined for depth formats.
322 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
323 const uint32_t *src
, unsigned src_stride
,
324 unsigned width
, unsigned height
);
327 * Unpack pixels to Z32_FLOAT.
328 * Note: strides are in bytes.
330 * Only defined for depth formats.
333 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
334 const uint8_t *src
, unsigned src_stride
,
335 unsigned width
, unsigned height
);
338 * Pack pixels from Z32_FLOAT.
339 * Note: strides are in bytes.
341 * Only defined for depth formats.
344 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
345 const float *src
, unsigned src_stride
,
346 unsigned width
, unsigned height
);
349 * Unpack pixels to S8_UINT.
350 * Note: strides are in bytes.
352 * Only defined for stencil formats.
355 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
356 const uint8_t *src
, unsigned src_stride
,
357 unsigned width
, unsigned height
);
360 * Pack pixels from S8_UINT.
361 * Note: strides are in bytes.
363 * Only defined for stencil formats.
366 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
367 const uint8_t *src
, unsigned src_stride
,
368 unsigned width
, unsigned height
);
371 * Unpack pixel blocks to R32G32B32A32_UINT.
372 * Note: strides are in bytes.
374 * Only defined for INT formats.
377 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
378 const uint8_t *src
, unsigned src_stride
,
379 unsigned width
, unsigned height
);
382 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
383 const uint32_t *src
, unsigned src_stride
,
384 unsigned width
, unsigned height
);
387 * Unpack pixel blocks to R32G32B32A32_SINT.
388 * Note: strides are in bytes.
390 * Only defined for INT formats.
393 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
394 const uint8_t *src
, unsigned src_stride
,
395 unsigned width
, unsigned height
);
398 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
399 const int32_t *src
, unsigned src_stride
,
400 unsigned width
, unsigned height
);
403 * Fetch a single pixel (i, j) from a block.
405 * Only defined for unsigned (pure) integer formats.
408 (*fetch_rgba_uint
)(uint32_t *dst
,
410 unsigned i
, unsigned j
);
413 * Fetch a single pixel (i, j) from a block.
415 * Only defined for signed (pure) integer formats.
418 (*fetch_rgba_sint
)(int32_t *dst
,
420 unsigned i
, unsigned j
);
424 extern const struct util_format_description
425 util_format_description_table
[];
428 const struct util_format_description
*
429 util_format_description(enum pipe_format format
);
433 * Format query functions.
436 static inline const char *
437 util_format_name(enum pipe_format format
)
439 const struct util_format_description
*desc
= util_format_description(format
);
443 return "PIPE_FORMAT_???";
449 static inline const char *
450 util_format_short_name(enum pipe_format format
)
452 const struct util_format_description
*desc
= util_format_description(format
);
459 return desc
->short_name
;
463 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
465 static inline boolean
466 util_format_is_plain(enum pipe_format format
)
468 const struct util_format_description
*desc
= util_format_description(format
);
474 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
477 static inline boolean
478 util_format_is_compressed(enum pipe_format format
)
480 const struct util_format_description
*desc
= util_format_description(format
);
487 switch (desc
->layout
) {
488 case UTIL_FORMAT_LAYOUT_S3TC
:
489 case UTIL_FORMAT_LAYOUT_RGTC
:
490 case UTIL_FORMAT_LAYOUT_ETC
:
491 case UTIL_FORMAT_LAYOUT_BPTC
:
492 case UTIL_FORMAT_LAYOUT_ASTC
:
493 case UTIL_FORMAT_LAYOUT_ATC
:
494 /* XXX add other formats in the future */
501 static inline boolean
502 util_format_is_s3tc(enum pipe_format format
)
504 const struct util_format_description
*desc
= util_format_description(format
);
511 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
514 static inline boolean
515 util_format_is_etc(enum pipe_format format
)
517 const struct util_format_description
*desc
= util_format_description(format
);
524 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
527 static inline boolean
528 util_format_is_srgb(enum pipe_format format
)
530 const struct util_format_description
*desc
= util_format_description(format
);
531 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
534 static inline boolean
535 util_format_has_depth(const struct util_format_description
*desc
)
537 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
538 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
541 static inline boolean
542 util_format_has_stencil(const struct util_format_description
*desc
)
544 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
545 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
548 static inline boolean
549 util_format_is_depth_or_stencil(enum pipe_format format
)
551 const struct util_format_description
*desc
= util_format_description(format
);
558 return util_format_has_depth(desc
) ||
559 util_format_has_stencil(desc
);
562 static inline boolean
563 util_format_is_depth_and_stencil(enum pipe_format format
)
565 const struct util_format_description
*desc
= util_format_description(format
);
572 return util_format_has_depth(desc
) &&
573 util_format_has_stencil(desc
);
577 * For depth-stencil formats, return the equivalent depth-only format.
579 static inline boolean
580 util_format_get_depth_only(enum pipe_format format
)
583 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
584 return PIPE_FORMAT_Z24X8_UNORM
;
586 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
587 return PIPE_FORMAT_X8Z24_UNORM
;
589 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
590 return PIPE_FORMAT_Z32_FLOAT
;
597 static inline boolean
598 util_format_is_yuv(enum pipe_format format
)
600 const struct util_format_description
*desc
= util_format_description(format
);
607 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
611 * Calculates the depth format type based upon the incoming format description.
613 static inline unsigned
614 util_get_depth_format_type(const struct util_format_description
*desc
)
616 unsigned depth_channel
= desc
->swizzle
[0];
617 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
618 depth_channel
!= PIPE_SWIZZLE_NONE
) {
619 return desc
->channel
[depth_channel
].type
;
621 return UTIL_FORMAT_TYPE_VOID
;
627 * Calculates the MRD for the depth format. MRD is used in depth bias
628 * for UNORM and unbound depth buffers. When the depth buffer is floating
629 * point, the depth bias calculation does not use the MRD. However, the
630 * default MRD will be 1.0 / ((1 << 24) - 1).
633 util_get_depth_format_mrd(const struct util_format_description
*desc
);
637 * Return whether this is an RGBA, Z, S, or combined ZS format.
638 * Useful for initializing pipe_blit_info::mask.
640 static inline unsigned
641 util_format_get_mask(enum pipe_format format
)
643 const struct util_format_description
*desc
=
644 util_format_description(format
);
649 if (util_format_has_depth(desc
)) {
650 if (util_format_has_stencil(desc
)) {
656 if (util_format_has_stencil(desc
)) {
659 return PIPE_MASK_RGBA
;
665 * Give the RGBA colormask of the channels that can be represented in this
668 * That is, the channels whose values are preserved.
670 static inline unsigned
671 util_format_colormask(const struct util_format_description
*desc
)
676 switch (desc
->colorspace
) {
677 case UTIL_FORMAT_COLORSPACE_RGB
:
678 case UTIL_FORMAT_COLORSPACE_SRGB
:
679 case UTIL_FORMAT_COLORSPACE_YUV
:
681 for (chan
= 0; chan
< 4; ++chan
) {
682 if (desc
->swizzle
[chan
] < 4) {
683 colormask
|= (1 << chan
);
687 case UTIL_FORMAT_COLORSPACE_ZS
:
697 * Checks if color mask covers every channel for the specified format
699 * @param desc a format description to check colormask with
700 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
702 static inline boolean
703 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
705 return (~colormask
& util_format_colormask(desc
)) == 0;
710 util_format_is_float(enum pipe_format format
);
714 util_format_has_alpha(enum pipe_format format
);
718 util_format_is_luminance(enum pipe_format format
);
721 util_format_is_alpha(enum pipe_format format
);
724 util_format_is_luminance_alpha(enum pipe_format format
);
728 util_format_is_intensity(enum pipe_format format
);
731 util_format_is_subsampled_422(enum pipe_format format
);
734 util_format_is_pure_integer(enum pipe_format format
);
737 util_format_is_pure_sint(enum pipe_format format
);
740 util_format_is_pure_uint(enum pipe_format format
);
743 util_format_is_snorm(enum pipe_format format
);
746 util_format_is_unorm(enum pipe_format format
);
749 util_format_is_snorm8(enum pipe_format format
);
752 * Check if the src format can be blitted to the destination format with
753 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
757 util_is_format_compatible(const struct util_format_description
*src_desc
,
758 const struct util_format_description
*dst_desc
);
761 * Whether this format is a rgab8 variant.
763 * That is, any format that matches the
765 * PIPE_FORMAT_?8?8?8?8_UNORM
767 static inline boolean
768 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
772 if(desc
->block
.width
!= 1 ||
773 desc
->block
.height
!= 1 ||
774 desc
->block
.bits
!= 32)
777 for(chan
= 0; chan
< 4; ++chan
) {
778 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
779 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
781 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
782 !desc
->channel
[chan
].normalized
)
784 if(desc
->channel
[chan
].size
!= 8)
792 * Return total bits needed for the pixel format per block.
795 util_format_get_blocksizebits(enum pipe_format format
)
797 const struct util_format_description
*desc
= util_format_description(format
);
804 return desc
->block
.bits
;
808 * Return bytes per block (not pixel) for the given format.
811 util_format_get_blocksize(enum pipe_format format
)
813 uint bits
= util_format_get_blocksizebits(format
);
814 uint bytes
= bits
/ 8;
816 assert(bits
% 8 == 0);
826 util_format_get_blockwidth(enum pipe_format format
)
828 const struct util_format_description
*desc
= util_format_description(format
);
835 return desc
->block
.width
;
839 util_format_get_blockheight(enum pipe_format format
)
841 const struct util_format_description
*desc
= util_format_description(format
);
848 return desc
->block
.height
;
851 static inline unsigned
852 util_format_get_nblocksx(enum pipe_format format
,
855 unsigned blockwidth
= util_format_get_blockwidth(format
);
856 return (x
+ blockwidth
- 1) / blockwidth
;
859 static inline unsigned
860 util_format_get_nblocksy(enum pipe_format format
,
863 unsigned blockheight
= util_format_get_blockheight(format
);
864 return (y
+ blockheight
- 1) / blockheight
;
867 static inline unsigned
868 util_format_get_nblocks(enum pipe_format format
,
872 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
876 util_format_get_stride(enum pipe_format format
,
879 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
883 util_format_get_2d_size(enum pipe_format format
,
887 return util_format_get_nblocksy(format
, height
) * stride
;
891 util_format_get_component_bits(enum pipe_format format
,
892 enum util_format_colorspace colorspace
,
895 const struct util_format_description
*desc
= util_format_description(format
);
896 enum util_format_colorspace desc_colorspace
;
903 assert(component
< 4);
905 /* Treat RGB and SRGB as equivalent. */
906 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
907 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
909 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
910 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
912 desc_colorspace
= desc
->colorspace
;
915 if (desc_colorspace
!= colorspace
) {
919 switch (desc
->swizzle
[component
]) {
921 return desc
->channel
[0].size
;
923 return desc
->channel
[1].size
;
925 return desc
->channel
[2].size
;
927 return desc
->channel
[3].size
;
934 * Given a linear RGB colorspace format, return the corresponding SRGB
935 * format, or PIPE_FORMAT_NONE if none.
937 static inline enum pipe_format
938 util_format_srgb(enum pipe_format format
)
940 if (util_format_is_srgb(format
))
944 case PIPE_FORMAT_L8_UNORM
:
945 return PIPE_FORMAT_L8_SRGB
;
946 case PIPE_FORMAT_R8_UNORM
:
947 return PIPE_FORMAT_R8_SRGB
;
948 case PIPE_FORMAT_L8A8_UNORM
:
949 return PIPE_FORMAT_L8A8_SRGB
;
950 case PIPE_FORMAT_R8G8B8_UNORM
:
951 return PIPE_FORMAT_R8G8B8_SRGB
;
952 case PIPE_FORMAT_A8B8G8R8_UNORM
:
953 return PIPE_FORMAT_A8B8G8R8_SRGB
;
954 case PIPE_FORMAT_X8B8G8R8_UNORM
:
955 return PIPE_FORMAT_X8B8G8R8_SRGB
;
956 case PIPE_FORMAT_B8G8R8A8_UNORM
:
957 return PIPE_FORMAT_B8G8R8A8_SRGB
;
958 case PIPE_FORMAT_B8G8R8X8_UNORM
:
959 return PIPE_FORMAT_B8G8R8X8_SRGB
;
960 case PIPE_FORMAT_A8R8G8B8_UNORM
:
961 return PIPE_FORMAT_A8R8G8B8_SRGB
;
962 case PIPE_FORMAT_X8R8G8B8_UNORM
:
963 return PIPE_FORMAT_X8R8G8B8_SRGB
;
964 case PIPE_FORMAT_R8G8B8A8_UNORM
:
965 return PIPE_FORMAT_R8G8B8A8_SRGB
;
966 case PIPE_FORMAT_R8G8B8X8_UNORM
:
967 return PIPE_FORMAT_R8G8B8X8_SRGB
;
968 case PIPE_FORMAT_DXT1_RGB
:
969 return PIPE_FORMAT_DXT1_SRGB
;
970 case PIPE_FORMAT_DXT1_RGBA
:
971 return PIPE_FORMAT_DXT1_SRGBA
;
972 case PIPE_FORMAT_DXT3_RGBA
:
973 return PIPE_FORMAT_DXT3_SRGBA
;
974 case PIPE_FORMAT_DXT5_RGBA
:
975 return PIPE_FORMAT_DXT5_SRGBA
;
976 case PIPE_FORMAT_B5G6R5_UNORM
:
977 return PIPE_FORMAT_B5G6R5_SRGB
;
978 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
979 return PIPE_FORMAT_BPTC_SRGBA
;
980 case PIPE_FORMAT_ASTC_4x4
:
981 return PIPE_FORMAT_ASTC_4x4_SRGB
;
982 case PIPE_FORMAT_ASTC_5x4
:
983 return PIPE_FORMAT_ASTC_5x4_SRGB
;
984 case PIPE_FORMAT_ASTC_5x5
:
985 return PIPE_FORMAT_ASTC_5x5_SRGB
;
986 case PIPE_FORMAT_ASTC_6x5
:
987 return PIPE_FORMAT_ASTC_6x5_SRGB
;
988 case PIPE_FORMAT_ASTC_6x6
:
989 return PIPE_FORMAT_ASTC_6x6_SRGB
;
990 case PIPE_FORMAT_ASTC_8x5
:
991 return PIPE_FORMAT_ASTC_8x5_SRGB
;
992 case PIPE_FORMAT_ASTC_8x6
:
993 return PIPE_FORMAT_ASTC_8x6_SRGB
;
994 case PIPE_FORMAT_ASTC_8x8
:
995 return PIPE_FORMAT_ASTC_8x8_SRGB
;
996 case PIPE_FORMAT_ASTC_10x5
:
997 return PIPE_FORMAT_ASTC_10x5_SRGB
;
998 case PIPE_FORMAT_ASTC_10x6
:
999 return PIPE_FORMAT_ASTC_10x6_SRGB
;
1000 case PIPE_FORMAT_ASTC_10x8
:
1001 return PIPE_FORMAT_ASTC_10x8_SRGB
;
1002 case PIPE_FORMAT_ASTC_10x10
:
1003 return PIPE_FORMAT_ASTC_10x10_SRGB
;
1004 case PIPE_FORMAT_ASTC_12x10
:
1005 return PIPE_FORMAT_ASTC_12x10_SRGB
;
1006 case PIPE_FORMAT_ASTC_12x12
:
1007 return PIPE_FORMAT_ASTC_12x12_SRGB
;
1010 return PIPE_FORMAT_NONE
;
1015 * Given an sRGB format, return the corresponding linear colorspace format.
1016 * For non sRGB formats, return the format unchanged.
1018 static inline enum pipe_format
1019 util_format_linear(enum pipe_format format
)
1022 case PIPE_FORMAT_L8_SRGB
:
1023 return PIPE_FORMAT_L8_UNORM
;
1024 case PIPE_FORMAT_R8_SRGB
:
1025 return PIPE_FORMAT_R8_UNORM
;
1026 case PIPE_FORMAT_L8A8_SRGB
:
1027 return PIPE_FORMAT_L8A8_UNORM
;
1028 case PIPE_FORMAT_R8G8B8_SRGB
:
1029 return PIPE_FORMAT_R8G8B8_UNORM
;
1030 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1031 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1032 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1033 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1034 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1035 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1036 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1037 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1038 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1039 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1040 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1041 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1042 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1043 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1044 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1045 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1046 case PIPE_FORMAT_DXT1_SRGB
:
1047 return PIPE_FORMAT_DXT1_RGB
;
1048 case PIPE_FORMAT_DXT1_SRGBA
:
1049 return PIPE_FORMAT_DXT1_RGBA
;
1050 case PIPE_FORMAT_DXT3_SRGBA
:
1051 return PIPE_FORMAT_DXT3_RGBA
;
1052 case PIPE_FORMAT_DXT5_SRGBA
:
1053 return PIPE_FORMAT_DXT5_RGBA
;
1054 case PIPE_FORMAT_B5G6R5_SRGB
:
1055 return PIPE_FORMAT_B5G6R5_UNORM
;
1056 case PIPE_FORMAT_BPTC_SRGBA
:
1057 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1058 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1059 return PIPE_FORMAT_ASTC_4x4
;
1060 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1061 return PIPE_FORMAT_ASTC_5x4
;
1062 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1063 return PIPE_FORMAT_ASTC_5x5
;
1064 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1065 return PIPE_FORMAT_ASTC_6x5
;
1066 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1067 return PIPE_FORMAT_ASTC_6x6
;
1068 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1069 return PIPE_FORMAT_ASTC_8x5
;
1070 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1071 return PIPE_FORMAT_ASTC_8x6
;
1072 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1073 return PIPE_FORMAT_ASTC_8x8
;
1074 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1075 return PIPE_FORMAT_ASTC_10x5
;
1076 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1077 return PIPE_FORMAT_ASTC_10x6
;
1078 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1079 return PIPE_FORMAT_ASTC_10x8
;
1080 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1081 return PIPE_FORMAT_ASTC_10x10
;
1082 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1083 return PIPE_FORMAT_ASTC_12x10
;
1084 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1085 return PIPE_FORMAT_ASTC_12x12
;
1092 * Given a depth-stencil format, return the corresponding stencil-only format.
1093 * For stencil-only formats, return the format unchanged.
1095 static inline enum pipe_format
1096 util_format_stencil_only(enum pipe_format format
)
1099 /* mask out the depth component */
1100 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1101 return PIPE_FORMAT_X24S8_UINT
;
1102 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1103 return PIPE_FORMAT_S8X24_UINT
;
1104 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1105 return PIPE_FORMAT_X32_S8X24_UINT
;
1107 /* stencil only formats */
1108 case PIPE_FORMAT_X24S8_UINT
:
1109 case PIPE_FORMAT_S8X24_UINT
:
1110 case PIPE_FORMAT_X32_S8X24_UINT
:
1111 case PIPE_FORMAT_S8_UINT
:
1116 return PIPE_FORMAT_NONE
;
1121 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1122 * This is identity for non-intensity formats.
1124 static inline enum pipe_format
1125 util_format_intensity_to_red(enum pipe_format format
)
1128 case PIPE_FORMAT_I8_UNORM
:
1129 return PIPE_FORMAT_R8_UNORM
;
1130 case PIPE_FORMAT_I8_SNORM
:
1131 return PIPE_FORMAT_R8_SNORM
;
1132 case PIPE_FORMAT_I16_UNORM
:
1133 return PIPE_FORMAT_R16_UNORM
;
1134 case PIPE_FORMAT_I16_SNORM
:
1135 return PIPE_FORMAT_R16_SNORM
;
1136 case PIPE_FORMAT_I16_FLOAT
:
1137 return PIPE_FORMAT_R16_FLOAT
;
1138 case PIPE_FORMAT_I32_FLOAT
:
1139 return PIPE_FORMAT_R32_FLOAT
;
1140 case PIPE_FORMAT_I8_UINT
:
1141 return PIPE_FORMAT_R8_UINT
;
1142 case PIPE_FORMAT_I8_SINT
:
1143 return PIPE_FORMAT_R8_SINT
;
1144 case PIPE_FORMAT_I16_UINT
:
1145 return PIPE_FORMAT_R16_UINT
;
1146 case PIPE_FORMAT_I16_SINT
:
1147 return PIPE_FORMAT_R16_SINT
;
1148 case PIPE_FORMAT_I32_UINT
:
1149 return PIPE_FORMAT_R32_UINT
;
1150 case PIPE_FORMAT_I32_SINT
:
1151 return PIPE_FORMAT_R32_SINT
;
1153 assert(!util_format_is_intensity(format
));
1159 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1160 * This is identity for non-luminance formats.
1162 static inline enum pipe_format
1163 util_format_luminance_to_red(enum pipe_format format
)
1166 case PIPE_FORMAT_L8_UNORM
:
1167 return PIPE_FORMAT_R8_UNORM
;
1168 case PIPE_FORMAT_L8_SNORM
:
1169 return PIPE_FORMAT_R8_SNORM
;
1170 case PIPE_FORMAT_L16_UNORM
:
1171 return PIPE_FORMAT_R16_UNORM
;
1172 case PIPE_FORMAT_L16_SNORM
:
1173 return PIPE_FORMAT_R16_SNORM
;
1174 case PIPE_FORMAT_L16_FLOAT
:
1175 return PIPE_FORMAT_R16_FLOAT
;
1176 case PIPE_FORMAT_L32_FLOAT
:
1177 return PIPE_FORMAT_R32_FLOAT
;
1178 case PIPE_FORMAT_L8_UINT
:
1179 return PIPE_FORMAT_R8_UINT
;
1180 case PIPE_FORMAT_L8_SINT
:
1181 return PIPE_FORMAT_R8_SINT
;
1182 case PIPE_FORMAT_L16_UINT
:
1183 return PIPE_FORMAT_R16_UINT
;
1184 case PIPE_FORMAT_L16_SINT
:
1185 return PIPE_FORMAT_R16_SINT
;
1186 case PIPE_FORMAT_L32_UINT
:
1187 return PIPE_FORMAT_R32_UINT
;
1188 case PIPE_FORMAT_L32_SINT
:
1189 return PIPE_FORMAT_R32_SINT
;
1191 case PIPE_FORMAT_LATC1_UNORM
:
1192 return PIPE_FORMAT_RGTC1_UNORM
;
1193 case PIPE_FORMAT_LATC1_SNORM
:
1194 return PIPE_FORMAT_RGTC1_SNORM
;
1196 case PIPE_FORMAT_L4A4_UNORM
:
1197 return PIPE_FORMAT_R4A4_UNORM
;
1199 case PIPE_FORMAT_L8A8_UNORM
:
1200 return PIPE_FORMAT_R8A8_UNORM
;
1201 case PIPE_FORMAT_L8A8_SNORM
:
1202 return PIPE_FORMAT_R8A8_SNORM
;
1203 case PIPE_FORMAT_L16A16_UNORM
:
1204 return PIPE_FORMAT_R16A16_UNORM
;
1205 case PIPE_FORMAT_L16A16_SNORM
:
1206 return PIPE_FORMAT_R16A16_SNORM
;
1207 case PIPE_FORMAT_L16A16_FLOAT
:
1208 return PIPE_FORMAT_R16A16_FLOAT
;
1209 case PIPE_FORMAT_L32A32_FLOAT
:
1210 return PIPE_FORMAT_R32A32_FLOAT
;
1211 case PIPE_FORMAT_L8A8_UINT
:
1212 return PIPE_FORMAT_R8A8_UINT
;
1213 case PIPE_FORMAT_L8A8_SINT
:
1214 return PIPE_FORMAT_R8A8_SINT
;
1215 case PIPE_FORMAT_L16A16_UINT
:
1216 return PIPE_FORMAT_R16A16_UINT
;
1217 case PIPE_FORMAT_L16A16_SINT
:
1218 return PIPE_FORMAT_R16A16_SINT
;
1219 case PIPE_FORMAT_L32A32_UINT
:
1220 return PIPE_FORMAT_R32A32_UINT
;
1221 case PIPE_FORMAT_L32A32_SINT
:
1222 return PIPE_FORMAT_R32A32_SINT
;
1224 /* We don't have compressed red-alpha variants for these. */
1225 case PIPE_FORMAT_LATC2_UNORM
:
1226 case PIPE_FORMAT_LATC2_SNORM
:
1227 return PIPE_FORMAT_NONE
;
1230 assert(!util_format_is_luminance(format
) &&
1231 !util_format_is_luminance_alpha(format
));
1237 * Return the number of components stored.
1238 * Formats with block size != 1x1 will always have 1 component (the block).
1240 static inline unsigned
1241 util_format_get_nr_components(enum pipe_format format
)
1243 const struct util_format_description
*desc
= util_format_description(format
);
1244 return desc
->nr_channels
;
1248 * Return the index of the first non-void channel
1249 * -1 if no non-void channels
1252 util_format_get_first_non_void_channel(enum pipe_format format
)
1254 const struct util_format_description
*desc
= util_format_description(format
);
1257 for (i
= 0; i
< 4; i
++)
1258 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1268 * Whether this format is any 8-bit UNORM variant. Looser than
1269 * util_is_rgba8_variant (also includes alpha textures, for instance).
1273 util_format_is_unorm8(const struct util_format_description
*desc
)
1275 int c
= util_format_get_first_non_void_channel(desc
->format
);
1280 return desc
->is_unorm
&& desc
->is_array
&& desc
->channel
[c
].size
== 8;
1284 * Format access functions.
1288 util_format_read_4f(enum pipe_format format
,
1289 float *dst
, unsigned dst_stride
,
1290 const void *src
, unsigned src_stride
,
1291 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1294 util_format_write_4f(enum pipe_format format
,
1295 const float *src
, unsigned src_stride
,
1296 void *dst
, unsigned dst_stride
,
1297 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1300 util_format_read_4ub(enum pipe_format format
,
1301 uint8_t *dst
, unsigned dst_stride
,
1302 const void *src
, unsigned src_stride
,
1303 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1306 util_format_write_4ub(enum pipe_format format
,
1307 const uint8_t *src
, unsigned src_stride
,
1308 void *dst
, unsigned dst_stride
,
1309 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1312 util_format_read_4ui(enum pipe_format format
,
1313 unsigned *dst
, unsigned dst_stride
,
1314 const void *src
, unsigned src_stride
,
1315 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1318 util_format_write_4ui(enum pipe_format format
,
1319 const unsigned int *src
, unsigned src_stride
,
1320 void *dst
, unsigned dst_stride
,
1321 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1324 util_format_read_4i(enum pipe_format format
,
1325 int *dst
, unsigned dst_stride
,
1326 const void *src
, unsigned src_stride
,
1327 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1330 util_format_write_4i(enum pipe_format format
,
1331 const int *src
, unsigned src_stride
,
1332 void *dst
, unsigned dst_stride
,
1333 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1336 * Generic format conversion;
1340 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1343 util_format_translate(enum pipe_format dst_format
,
1344 void *dst
, unsigned dst_stride
,
1345 unsigned dst_x
, unsigned dst_y
,
1346 enum pipe_format src_format
,
1347 const void *src
, unsigned src_stride
,
1348 unsigned src_x
, unsigned src_y
,
1349 unsigned width
, unsigned height
);
1352 util_format_translate_3d(enum pipe_format dst_format
,
1353 void *dst
, unsigned dst_stride
,
1354 unsigned dst_slice_stride
,
1355 unsigned dst_x
, unsigned dst_y
,
1357 enum pipe_format src_format
,
1358 const void *src
, unsigned src_stride
,
1359 unsigned src_slice_stride
,
1360 unsigned src_x
, unsigned src_y
,
1361 unsigned src_z
, unsigned width
,
1362 unsigned height
, unsigned depth
);
1365 * Swizzle operations.
1368 /* Compose two sets of swizzles.
1369 * If V is a 4D vector and the function parameters represent functions that
1370 * swizzle vector components, this holds:
1371 * swz2(swz1(V)) = dst(V)
1373 void util_format_compose_swizzles(const unsigned char swz1
[4],
1374 const unsigned char swz2
[4],
1375 unsigned char dst
[4]);
1377 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1378 * to \param src and store the result in \param dst.
1379 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1381 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1382 const union pipe_color_union
*src
,
1383 const unsigned char swz
[4],
1384 const boolean is_integer
);
1386 void pipe_swizzle_4f(float *dst
, const float *src
,
1387 const unsigned char swz
[4]);
1389 void util_format_unswizzle_4f(float *dst
, const float *src
,
1390 const unsigned char swz
[4]);
1393 util_format_snorm8_to_sint8(enum pipe_format format
);
1399 #endif /* ! U_FORMAT_H */