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
;
47 * Describe how to pack/unpack pixels into/from the prescribed format.
49 * XXX: This could be renamed to something like util_format_pack, or broke down
50 * in flags inside util_format_block that said exactly what we want.
52 enum util_format_layout
{
54 * Formats with util_format_block::width == util_format_block::height == 1
55 * that can be described as an ordinary data structure.
57 UTIL_FORMAT_LAYOUT_PLAIN
,
60 * Formats with sub-sampled channels.
62 * This is for formats like YVYU where there is less than one sample per
65 UTIL_FORMAT_LAYOUT_SUBSAMPLED
,
68 * S3 Texture Compression formats.
70 UTIL_FORMAT_LAYOUT_S3TC
,
73 * Red-Green Texture Compression formats.
75 UTIL_FORMAT_LAYOUT_RGTC
,
78 * Ericsson Texture Compression
80 UTIL_FORMAT_LAYOUT_ETC
,
83 * BC6/7 Texture Compression
85 UTIL_FORMAT_LAYOUT_BPTC
,
87 UTIL_FORMAT_LAYOUT_ASTC
,
89 UTIL_FORMAT_LAYOUT_ATC
,
91 /** Formats with 2 or more planes. */
92 UTIL_FORMAT_LAYOUT_PLANAR2
,
93 UTIL_FORMAT_LAYOUT_PLANAR3
,
95 UTIL_FORMAT_LAYOUT_FXT1
= 10,
98 * Everything else that doesn't fit in any of the above layouts.
100 UTIL_FORMAT_LAYOUT_OTHER
,
104 struct util_format_block
106 /** Block width in pixels */
109 /** Block height in pixels */
112 /** Block depth in pixels */
115 /** Block size in bits */
120 enum util_format_type
{
121 UTIL_FORMAT_TYPE_VOID
= 0,
122 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
123 UTIL_FORMAT_TYPE_SIGNED
= 2,
124 UTIL_FORMAT_TYPE_FIXED
= 3,
125 UTIL_FORMAT_TYPE_FLOAT
= 4
129 enum util_format_colorspace
{
130 UTIL_FORMAT_COLORSPACE_RGB
= 0,
131 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
132 UTIL_FORMAT_COLORSPACE_YUV
= 2,
133 UTIL_FORMAT_COLORSPACE_ZS
= 3
137 struct util_format_channel_description
139 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
140 unsigned normalized
:1;
141 unsigned pure_integer
:1;
142 unsigned size
:9; /**< bits per channel */
143 unsigned shift
:16; /** number of bits from lsb */
147 struct util_format_description
149 enum pipe_format format
;
154 * Short name, striped of the prefix, lower case.
156 const char *short_name
;
159 * Pixel block dimensions.
161 struct util_format_block block
;
163 enum util_format_layout layout
;
166 * The number of channels.
168 unsigned nr_channels
:3;
171 * Whether all channels have the same number of (whole) bytes and type.
176 * Whether the pixel format can be described as a bitfield structure.
179 * - pixel depth must be 8, 16, or 32 bits;
180 * - all channels must be unsigned, signed, or void
182 unsigned is_bitmask
:1;
185 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
190 * Whether the format contains UNORM channels
195 * Whether the format contains SNORM channels
200 * Input channel description, in the order XYZW.
202 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
204 * If each channel is accessed as an individual N-byte value, X is always
205 * at the lowest address in memory, Y is always next, and so on. For all
206 * currently-defined formats, the N-byte value has native endianness.
208 * If instead a group of channels is accessed as a single N-byte value,
209 * the order of the channels within that value depends on endianness.
210 * For big-endian targets, X is the most significant subvalue,
211 * otherwise it is the least significant one.
213 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
216 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
217 * big-endian: X Yu Ym Yl
219 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
223 * little-endian: YYYXXXXX WZZZZZYY
224 * big-endian: XXXXXYYY YYZZZZZW
226 struct util_format_channel_description channel
[4];
229 * Output channel swizzle.
231 * The order is either:
235 * depending on the colorspace.
237 unsigned char swizzle
[4];
240 * Colorspace transformation.
242 enum util_format_colorspace colorspace
;
245 * Unpack pixel blocks to R8G8B8A8_UNORM.
246 * Note: strides are in bytes.
248 * Only defined for non-depth-stencil formats.
251 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
252 const uint8_t *src
, unsigned src_stride
,
253 unsigned width
, unsigned height
);
256 * Pack pixel blocks from R8G8B8A8_UNORM.
257 * Note: strides are in bytes.
259 * Only defined for non-depth-stencil formats.
262 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
263 const uint8_t *src
, unsigned src_stride
,
264 unsigned width
, unsigned height
);
267 * Fetch a single pixel (i, j) from a block.
269 * XXX: Only defined for a very few select formats.
272 (*fetch_rgba_8unorm
)(uint8_t *dst
,
274 unsigned i
, unsigned j
);
277 * Unpack pixel blocks to R32G32B32A32_FLOAT.
278 * Note: strides are in bytes.
280 * Only defined for non-depth-stencil formats.
283 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
284 const uint8_t *src
, unsigned src_stride
,
285 unsigned width
, unsigned height
);
288 * Pack pixel blocks from R32G32B32A32_FLOAT.
289 * Note: strides are in bytes.
291 * Only defined for non-depth-stencil formats.
294 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
295 const float *src
, unsigned src_stride
,
296 unsigned width
, unsigned height
);
299 * Fetch a single pixel (i, j) from a block.
301 * Only defined for non-depth-stencil and non-integer formats.
304 (*fetch_rgba_float
)(float *dst
,
306 unsigned i
, unsigned j
);
309 * Unpack pixels to Z32_UNORM.
310 * Note: strides are in bytes.
312 * Only defined for depth formats.
315 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
316 const uint8_t *src
, unsigned src_stride
,
317 unsigned width
, unsigned height
);
320 * Pack pixels from Z32_FLOAT.
321 * Note: strides are in bytes.
323 * Only defined for depth formats.
326 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
327 const uint32_t *src
, unsigned src_stride
,
328 unsigned width
, unsigned height
);
331 * Unpack pixels to Z32_FLOAT.
332 * Note: strides are in bytes.
334 * Only defined for depth formats.
337 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
338 const uint8_t *src
, unsigned src_stride
,
339 unsigned width
, unsigned height
);
342 * Pack pixels from Z32_FLOAT.
343 * Note: strides are in bytes.
345 * Only defined for depth formats.
348 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
349 const float *src
, unsigned src_stride
,
350 unsigned width
, unsigned height
);
353 * Unpack pixels to S8_UINT.
354 * Note: strides are in bytes.
356 * Only defined for stencil formats.
359 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
360 const uint8_t *src
, unsigned src_stride
,
361 unsigned width
, unsigned height
);
364 * Pack pixels from S8_UINT.
365 * Note: strides are in bytes.
367 * Only defined for stencil formats.
370 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
371 const uint8_t *src
, unsigned src_stride
,
372 unsigned width
, unsigned height
);
375 * Unpack pixel blocks to R32G32B32A32_UINT.
376 * Note: strides are in bytes.
378 * Only defined for INT formats.
381 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
382 const uint8_t *src
, unsigned src_stride
,
383 unsigned width
, unsigned height
);
386 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
387 const uint32_t *src
, unsigned src_stride
,
388 unsigned width
, unsigned height
);
391 * Unpack pixel blocks to R32G32B32A32_SINT.
392 * Note: strides are in bytes.
394 * Only defined for INT formats.
397 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
398 const uint8_t *src
, unsigned src_stride
,
399 unsigned width
, unsigned height
);
402 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
403 const int32_t *src
, unsigned src_stride
,
404 unsigned width
, unsigned height
);
407 * Fetch a single pixel (i, j) from a block.
409 * Only defined for unsigned (pure) integer formats.
412 (*fetch_rgba_uint
)(uint32_t *dst
,
414 unsigned i
, unsigned j
);
417 * Fetch a single pixel (i, j) from a block.
419 * Only defined for signed (pure) integer formats.
422 (*fetch_rgba_sint
)(int32_t *dst
,
424 unsigned i
, unsigned j
);
428 extern const struct util_format_description
429 util_format_description_table
[];
432 const struct util_format_description
*
433 util_format_description(enum pipe_format format
);
437 * Format query functions.
440 static inline const char *
441 util_format_name(enum pipe_format format
)
443 const struct util_format_description
*desc
= util_format_description(format
);
447 return "PIPE_FORMAT_???";
453 static inline const char *
454 util_format_short_name(enum pipe_format format
)
456 const struct util_format_description
*desc
= util_format_description(format
);
463 return desc
->short_name
;
467 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
469 static inline boolean
470 util_format_is_plain(enum pipe_format format
)
472 const struct util_format_description
*desc
= util_format_description(format
);
478 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
481 static inline boolean
482 util_format_is_compressed(enum pipe_format format
)
484 const struct util_format_description
*desc
= util_format_description(format
);
491 switch (desc
->layout
) {
492 case UTIL_FORMAT_LAYOUT_S3TC
:
493 case UTIL_FORMAT_LAYOUT_RGTC
:
494 case UTIL_FORMAT_LAYOUT_ETC
:
495 case UTIL_FORMAT_LAYOUT_BPTC
:
496 case UTIL_FORMAT_LAYOUT_ASTC
:
497 case UTIL_FORMAT_LAYOUT_ATC
:
498 /* XXX add other formats in the future */
505 static inline boolean
506 util_format_is_s3tc(enum pipe_format format
)
508 const struct util_format_description
*desc
= util_format_description(format
);
515 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
518 static inline boolean
519 util_format_is_etc(enum pipe_format format
)
521 const struct util_format_description
*desc
= util_format_description(format
);
528 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
531 static inline boolean
532 util_format_is_srgb(enum pipe_format format
)
534 const struct util_format_description
*desc
= util_format_description(format
);
535 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
538 static inline boolean
539 util_format_has_depth(const struct util_format_description
*desc
)
541 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
542 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
545 static inline boolean
546 util_format_has_stencil(const struct util_format_description
*desc
)
548 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
549 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
552 static inline boolean
553 util_format_is_depth_or_stencil(enum pipe_format format
)
555 const struct util_format_description
*desc
= util_format_description(format
);
562 return util_format_has_depth(desc
) ||
563 util_format_has_stencil(desc
);
566 static inline boolean
567 util_format_is_depth_and_stencil(enum pipe_format format
)
569 const struct util_format_description
*desc
= util_format_description(format
);
576 return util_format_has_depth(desc
) &&
577 util_format_has_stencil(desc
);
581 * For depth-stencil formats, return the equivalent depth-only format.
583 static inline enum pipe_format
584 util_format_get_depth_only(enum pipe_format format
)
587 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
588 return PIPE_FORMAT_Z24X8_UNORM
;
590 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
591 return PIPE_FORMAT_X8Z24_UNORM
;
593 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
594 return PIPE_FORMAT_Z32_FLOAT
;
601 static inline boolean
602 util_format_is_yuv(enum pipe_format format
)
604 const struct util_format_description
*desc
= util_format_description(format
);
611 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
615 * Calculates the depth format type based upon the incoming format description.
617 static inline unsigned
618 util_get_depth_format_type(const struct util_format_description
*desc
)
620 unsigned depth_channel
= desc
->swizzle
[0];
621 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
622 depth_channel
!= PIPE_SWIZZLE_NONE
) {
623 return desc
->channel
[depth_channel
].type
;
625 return UTIL_FORMAT_TYPE_VOID
;
631 * Calculates the MRD for the depth format. MRD is used in depth bias
632 * for UNORM and unbound depth buffers. When the depth buffer is floating
633 * point, the depth bias calculation does not use the MRD. However, the
634 * default MRD will be 1.0 / ((1 << 24) - 1).
637 util_get_depth_format_mrd(const struct util_format_description
*desc
);
641 * Return whether this is an RGBA, Z, S, or combined ZS format.
642 * Useful for initializing pipe_blit_info::mask.
644 static inline unsigned
645 util_format_get_mask(enum pipe_format format
)
647 const struct util_format_description
*desc
=
648 util_format_description(format
);
653 if (util_format_has_depth(desc
)) {
654 if (util_format_has_stencil(desc
)) {
660 if (util_format_has_stencil(desc
)) {
663 return PIPE_MASK_RGBA
;
669 * Give the RGBA colormask of the channels that can be represented in this
672 * That is, the channels whose values are preserved.
674 static inline unsigned
675 util_format_colormask(const struct util_format_description
*desc
)
680 switch (desc
->colorspace
) {
681 case UTIL_FORMAT_COLORSPACE_RGB
:
682 case UTIL_FORMAT_COLORSPACE_SRGB
:
683 case UTIL_FORMAT_COLORSPACE_YUV
:
685 for (chan
= 0; chan
< 4; ++chan
) {
686 if (desc
->swizzle
[chan
] < 4) {
687 colormask
|= (1 << chan
);
691 case UTIL_FORMAT_COLORSPACE_ZS
:
701 * Checks if color mask covers every channel for the specified format
703 * @param desc a format description to check colormask with
704 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
706 static inline boolean
707 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
709 return (~colormask
& util_format_colormask(desc
)) == 0;
714 util_format_is_float(enum pipe_format format
);
718 util_format_has_alpha(enum pipe_format format
);
722 util_format_is_luminance(enum pipe_format format
);
725 util_format_is_alpha(enum pipe_format format
);
728 util_format_is_luminance_alpha(enum pipe_format format
);
732 util_format_is_intensity(enum pipe_format format
);
735 util_format_is_subsampled_422(enum pipe_format format
);
738 util_format_is_pure_integer(enum pipe_format format
);
741 util_format_is_pure_sint(enum pipe_format format
);
744 util_format_is_pure_uint(enum pipe_format format
);
747 util_format_is_snorm(enum pipe_format format
);
750 util_format_is_unorm(enum pipe_format format
);
753 util_format_is_snorm8(enum pipe_format format
);
756 * Check if the src format can be blitted to the destination format with
757 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
761 util_is_format_compatible(const struct util_format_description
*src_desc
,
762 const struct util_format_description
*dst_desc
);
765 * Whether this format is a rgab8 variant.
767 * That is, any format that matches the
769 * PIPE_FORMAT_?8?8?8?8_UNORM
771 static inline boolean
772 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
776 if(desc
->block
.width
!= 1 ||
777 desc
->block
.height
!= 1 ||
778 desc
->block
.bits
!= 32)
781 for(chan
= 0; chan
< 4; ++chan
) {
782 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
783 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
785 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
786 !desc
->channel
[chan
].normalized
)
788 if(desc
->channel
[chan
].size
!= 8)
796 * Return total bits needed for the pixel format per block.
799 util_format_get_blocksizebits(enum pipe_format format
)
801 const struct util_format_description
*desc
= util_format_description(format
);
808 return desc
->block
.bits
;
812 * Return bytes per block (not pixel) for the given format.
815 util_format_get_blocksize(enum pipe_format format
)
817 uint bits
= util_format_get_blocksizebits(format
);
818 uint bytes
= bits
/ 8;
820 assert(bits
% 8 == 0);
830 util_format_get_blockwidth(enum pipe_format format
)
832 const struct util_format_description
*desc
= util_format_description(format
);
839 return desc
->block
.width
;
843 util_format_get_blockheight(enum pipe_format format
)
845 const struct util_format_description
*desc
= util_format_description(format
);
852 return desc
->block
.height
;
856 util_format_get_blockdepth(enum pipe_format format
)
858 const struct util_format_description
*desc
= util_format_description(format
);
865 return desc
->block
.depth
;
868 static inline unsigned
869 util_format_get_nblocksx(enum pipe_format format
,
872 unsigned blockwidth
= util_format_get_blockwidth(format
);
873 return (x
+ blockwidth
- 1) / blockwidth
;
876 static inline unsigned
877 util_format_get_nblocksy(enum pipe_format format
,
880 unsigned blockheight
= util_format_get_blockheight(format
);
881 return (y
+ blockheight
- 1) / blockheight
;
884 static inline unsigned
885 util_format_get_nblocksz(enum pipe_format format
,
888 unsigned blockdepth
= util_format_get_blockdepth(format
);
889 return (z
+ blockdepth
- 1) / blockdepth
;
892 static inline unsigned
893 util_format_get_nblocks(enum pipe_format format
,
897 assert(util_format_get_blockdepth(format
) == 1);
898 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
902 util_format_get_stride(enum pipe_format format
,
905 return (size_t)util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
909 util_format_get_2d_size(enum pipe_format format
,
913 return util_format_get_nblocksy(format
, height
) * stride
;
917 util_format_get_component_bits(enum pipe_format format
,
918 enum util_format_colorspace colorspace
,
921 const struct util_format_description
*desc
= util_format_description(format
);
922 enum util_format_colorspace desc_colorspace
;
929 assert(component
< 4);
931 /* Treat RGB and SRGB as equivalent. */
932 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
933 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
935 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
936 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
938 desc_colorspace
= desc
->colorspace
;
941 if (desc_colorspace
!= colorspace
) {
945 switch (desc
->swizzle
[component
]) {
947 return desc
->channel
[0].size
;
949 return desc
->channel
[1].size
;
951 return desc
->channel
[2].size
;
953 return desc
->channel
[3].size
;
960 * Given a linear RGB colorspace format, return the corresponding SRGB
961 * format, or PIPE_FORMAT_NONE if none.
963 static inline enum pipe_format
964 util_format_srgb(enum pipe_format format
)
966 if (util_format_is_srgb(format
))
970 case PIPE_FORMAT_L8_UNORM
:
971 return PIPE_FORMAT_L8_SRGB
;
972 case PIPE_FORMAT_R8_UNORM
:
973 return PIPE_FORMAT_R8_SRGB
;
974 case PIPE_FORMAT_L8A8_UNORM
:
975 return PIPE_FORMAT_L8A8_SRGB
;
976 case PIPE_FORMAT_R8G8B8_UNORM
:
977 return PIPE_FORMAT_R8G8B8_SRGB
;
978 case PIPE_FORMAT_A8B8G8R8_UNORM
:
979 return PIPE_FORMAT_A8B8G8R8_SRGB
;
980 case PIPE_FORMAT_X8B8G8R8_UNORM
:
981 return PIPE_FORMAT_X8B8G8R8_SRGB
;
982 case PIPE_FORMAT_B8G8R8A8_UNORM
:
983 return PIPE_FORMAT_B8G8R8A8_SRGB
;
984 case PIPE_FORMAT_B8G8R8X8_UNORM
:
985 return PIPE_FORMAT_B8G8R8X8_SRGB
;
986 case PIPE_FORMAT_A8R8G8B8_UNORM
:
987 return PIPE_FORMAT_A8R8G8B8_SRGB
;
988 case PIPE_FORMAT_X8R8G8B8_UNORM
:
989 return PIPE_FORMAT_X8R8G8B8_SRGB
;
990 case PIPE_FORMAT_R8G8B8A8_UNORM
:
991 return PIPE_FORMAT_R8G8B8A8_SRGB
;
992 case PIPE_FORMAT_R8G8B8X8_UNORM
:
993 return PIPE_FORMAT_R8G8B8X8_SRGB
;
994 case PIPE_FORMAT_DXT1_RGB
:
995 return PIPE_FORMAT_DXT1_SRGB
;
996 case PIPE_FORMAT_DXT1_RGBA
:
997 return PIPE_FORMAT_DXT1_SRGBA
;
998 case PIPE_FORMAT_DXT3_RGBA
:
999 return PIPE_FORMAT_DXT3_SRGBA
;
1000 case PIPE_FORMAT_DXT5_RGBA
:
1001 return PIPE_FORMAT_DXT5_SRGBA
;
1002 case PIPE_FORMAT_B5G6R5_UNORM
:
1003 return PIPE_FORMAT_B5G6R5_SRGB
;
1004 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1005 return PIPE_FORMAT_BPTC_SRGBA
;
1006 case PIPE_FORMAT_ETC2_RGB8
:
1007 return PIPE_FORMAT_ETC2_SRGB8
;
1008 case PIPE_FORMAT_ETC2_RGB8A1
:
1009 return PIPE_FORMAT_ETC2_SRGB8A1
;
1010 case PIPE_FORMAT_ETC2_RGBA8
:
1011 return PIPE_FORMAT_ETC2_SRGBA8
;
1012 case PIPE_FORMAT_ASTC_4x4
:
1013 return PIPE_FORMAT_ASTC_4x4_SRGB
;
1014 case PIPE_FORMAT_ASTC_5x4
:
1015 return PIPE_FORMAT_ASTC_5x4_SRGB
;
1016 case PIPE_FORMAT_ASTC_5x5
:
1017 return PIPE_FORMAT_ASTC_5x5_SRGB
;
1018 case PIPE_FORMAT_ASTC_6x5
:
1019 return PIPE_FORMAT_ASTC_6x5_SRGB
;
1020 case PIPE_FORMAT_ASTC_6x6
:
1021 return PIPE_FORMAT_ASTC_6x6_SRGB
;
1022 case PIPE_FORMAT_ASTC_8x5
:
1023 return PIPE_FORMAT_ASTC_8x5_SRGB
;
1024 case PIPE_FORMAT_ASTC_8x6
:
1025 return PIPE_FORMAT_ASTC_8x6_SRGB
;
1026 case PIPE_FORMAT_ASTC_8x8
:
1027 return PIPE_FORMAT_ASTC_8x8_SRGB
;
1028 case PIPE_FORMAT_ASTC_10x5
:
1029 return PIPE_FORMAT_ASTC_10x5_SRGB
;
1030 case PIPE_FORMAT_ASTC_10x6
:
1031 return PIPE_FORMAT_ASTC_10x6_SRGB
;
1032 case PIPE_FORMAT_ASTC_10x8
:
1033 return PIPE_FORMAT_ASTC_10x8_SRGB
;
1034 case PIPE_FORMAT_ASTC_10x10
:
1035 return PIPE_FORMAT_ASTC_10x10_SRGB
;
1036 case PIPE_FORMAT_ASTC_12x10
:
1037 return PIPE_FORMAT_ASTC_12x10_SRGB
;
1038 case PIPE_FORMAT_ASTC_12x12
:
1039 return PIPE_FORMAT_ASTC_12x12_SRGB
;
1042 return PIPE_FORMAT_NONE
;
1047 * Given an sRGB format, return the corresponding linear colorspace format.
1048 * For non sRGB formats, return the format unchanged.
1050 static inline enum pipe_format
1051 util_format_linear(enum pipe_format format
)
1054 case PIPE_FORMAT_L8_SRGB
:
1055 return PIPE_FORMAT_L8_UNORM
;
1056 case PIPE_FORMAT_R8_SRGB
:
1057 return PIPE_FORMAT_R8_UNORM
;
1058 case PIPE_FORMAT_L8A8_SRGB
:
1059 return PIPE_FORMAT_L8A8_UNORM
;
1060 case PIPE_FORMAT_R8G8B8_SRGB
:
1061 return PIPE_FORMAT_R8G8B8_UNORM
;
1062 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1063 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1064 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1065 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1066 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1067 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1068 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1069 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1070 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1071 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1072 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1073 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1074 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1075 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1076 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1077 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1078 case PIPE_FORMAT_DXT1_SRGB
:
1079 return PIPE_FORMAT_DXT1_RGB
;
1080 case PIPE_FORMAT_DXT1_SRGBA
:
1081 return PIPE_FORMAT_DXT1_RGBA
;
1082 case PIPE_FORMAT_DXT3_SRGBA
:
1083 return PIPE_FORMAT_DXT3_RGBA
;
1084 case PIPE_FORMAT_DXT5_SRGBA
:
1085 return PIPE_FORMAT_DXT5_RGBA
;
1086 case PIPE_FORMAT_B5G6R5_SRGB
:
1087 return PIPE_FORMAT_B5G6R5_UNORM
;
1088 case PIPE_FORMAT_BPTC_SRGBA
:
1089 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1090 case PIPE_FORMAT_ETC2_SRGB8
:
1091 return PIPE_FORMAT_ETC2_RGB8
;
1092 case PIPE_FORMAT_ETC2_SRGB8A1
:
1093 return PIPE_FORMAT_ETC2_RGB8A1
;
1094 case PIPE_FORMAT_ETC2_SRGBA8
:
1095 return PIPE_FORMAT_ETC2_RGBA8
;
1096 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1097 return PIPE_FORMAT_ASTC_4x4
;
1098 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1099 return PIPE_FORMAT_ASTC_5x4
;
1100 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1101 return PIPE_FORMAT_ASTC_5x5
;
1102 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1103 return PIPE_FORMAT_ASTC_6x5
;
1104 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1105 return PIPE_FORMAT_ASTC_6x6
;
1106 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1107 return PIPE_FORMAT_ASTC_8x5
;
1108 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1109 return PIPE_FORMAT_ASTC_8x6
;
1110 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1111 return PIPE_FORMAT_ASTC_8x8
;
1112 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1113 return PIPE_FORMAT_ASTC_10x5
;
1114 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1115 return PIPE_FORMAT_ASTC_10x6
;
1116 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1117 return PIPE_FORMAT_ASTC_10x8
;
1118 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1119 return PIPE_FORMAT_ASTC_10x10
;
1120 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1121 return PIPE_FORMAT_ASTC_12x10
;
1122 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1123 return PIPE_FORMAT_ASTC_12x12
;
1130 * Given a depth-stencil format, return the corresponding stencil-only format.
1131 * For stencil-only formats, return the format unchanged.
1133 static inline enum pipe_format
1134 util_format_stencil_only(enum pipe_format format
)
1137 /* mask out the depth component */
1138 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1139 return PIPE_FORMAT_X24S8_UINT
;
1140 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1141 return PIPE_FORMAT_S8X24_UINT
;
1142 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1143 return PIPE_FORMAT_X32_S8X24_UINT
;
1145 /* stencil only formats */
1146 case PIPE_FORMAT_X24S8_UINT
:
1147 case PIPE_FORMAT_S8X24_UINT
:
1148 case PIPE_FORMAT_X32_S8X24_UINT
:
1149 case PIPE_FORMAT_S8_UINT
:
1154 return PIPE_FORMAT_NONE
;
1159 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1160 * This is identity for non-intensity formats.
1162 static inline enum pipe_format
1163 util_format_intensity_to_red(enum pipe_format format
)
1166 case PIPE_FORMAT_I8_UNORM
:
1167 return PIPE_FORMAT_R8_UNORM
;
1168 case PIPE_FORMAT_I8_SNORM
:
1169 return PIPE_FORMAT_R8_SNORM
;
1170 case PIPE_FORMAT_I16_UNORM
:
1171 return PIPE_FORMAT_R16_UNORM
;
1172 case PIPE_FORMAT_I16_SNORM
:
1173 return PIPE_FORMAT_R16_SNORM
;
1174 case PIPE_FORMAT_I16_FLOAT
:
1175 return PIPE_FORMAT_R16_FLOAT
;
1176 case PIPE_FORMAT_I32_FLOAT
:
1177 return PIPE_FORMAT_R32_FLOAT
;
1178 case PIPE_FORMAT_I8_UINT
:
1179 return PIPE_FORMAT_R8_UINT
;
1180 case PIPE_FORMAT_I8_SINT
:
1181 return PIPE_FORMAT_R8_SINT
;
1182 case PIPE_FORMAT_I16_UINT
:
1183 return PIPE_FORMAT_R16_UINT
;
1184 case PIPE_FORMAT_I16_SINT
:
1185 return PIPE_FORMAT_R16_SINT
;
1186 case PIPE_FORMAT_I32_UINT
:
1187 return PIPE_FORMAT_R32_UINT
;
1188 case PIPE_FORMAT_I32_SINT
:
1189 return PIPE_FORMAT_R32_SINT
;
1191 assert(!util_format_is_intensity(format
));
1197 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1198 * This is identity for non-luminance formats.
1200 static inline enum pipe_format
1201 util_format_luminance_to_red(enum pipe_format format
)
1204 case PIPE_FORMAT_L8_UNORM
:
1205 return PIPE_FORMAT_R8_UNORM
;
1206 case PIPE_FORMAT_L8_SNORM
:
1207 return PIPE_FORMAT_R8_SNORM
;
1208 case PIPE_FORMAT_L16_UNORM
:
1209 return PIPE_FORMAT_R16_UNORM
;
1210 case PIPE_FORMAT_L16_SNORM
:
1211 return PIPE_FORMAT_R16_SNORM
;
1212 case PIPE_FORMAT_L16_FLOAT
:
1213 return PIPE_FORMAT_R16_FLOAT
;
1214 case PIPE_FORMAT_L32_FLOAT
:
1215 return PIPE_FORMAT_R32_FLOAT
;
1216 case PIPE_FORMAT_L8_UINT
:
1217 return PIPE_FORMAT_R8_UINT
;
1218 case PIPE_FORMAT_L8_SINT
:
1219 return PIPE_FORMAT_R8_SINT
;
1220 case PIPE_FORMAT_L16_UINT
:
1221 return PIPE_FORMAT_R16_UINT
;
1222 case PIPE_FORMAT_L16_SINT
:
1223 return PIPE_FORMAT_R16_SINT
;
1224 case PIPE_FORMAT_L32_UINT
:
1225 return PIPE_FORMAT_R32_UINT
;
1226 case PIPE_FORMAT_L32_SINT
:
1227 return PIPE_FORMAT_R32_SINT
;
1229 case PIPE_FORMAT_LATC1_UNORM
:
1230 return PIPE_FORMAT_RGTC1_UNORM
;
1231 case PIPE_FORMAT_LATC1_SNORM
:
1232 return PIPE_FORMAT_RGTC1_SNORM
;
1234 case PIPE_FORMAT_L4A4_UNORM
:
1235 return PIPE_FORMAT_R4A4_UNORM
;
1237 case PIPE_FORMAT_L8A8_UNORM
:
1238 return PIPE_FORMAT_R8A8_UNORM
;
1239 case PIPE_FORMAT_L8A8_SNORM
:
1240 return PIPE_FORMAT_R8A8_SNORM
;
1241 case PIPE_FORMAT_L16A16_UNORM
:
1242 return PIPE_FORMAT_R16A16_UNORM
;
1243 case PIPE_FORMAT_L16A16_SNORM
:
1244 return PIPE_FORMAT_R16A16_SNORM
;
1245 case PIPE_FORMAT_L16A16_FLOAT
:
1246 return PIPE_FORMAT_R16A16_FLOAT
;
1247 case PIPE_FORMAT_L32A32_FLOAT
:
1248 return PIPE_FORMAT_R32A32_FLOAT
;
1249 case PIPE_FORMAT_L8A8_UINT
:
1250 return PIPE_FORMAT_R8A8_UINT
;
1251 case PIPE_FORMAT_L8A8_SINT
:
1252 return PIPE_FORMAT_R8A8_SINT
;
1253 case PIPE_FORMAT_L16A16_UINT
:
1254 return PIPE_FORMAT_R16A16_UINT
;
1255 case PIPE_FORMAT_L16A16_SINT
:
1256 return PIPE_FORMAT_R16A16_SINT
;
1257 case PIPE_FORMAT_L32A32_UINT
:
1258 return PIPE_FORMAT_R32A32_UINT
;
1259 case PIPE_FORMAT_L32A32_SINT
:
1260 return PIPE_FORMAT_R32A32_SINT
;
1262 /* We don't have compressed red-alpha variants for these. */
1263 case PIPE_FORMAT_LATC2_UNORM
:
1264 case PIPE_FORMAT_LATC2_SNORM
:
1265 return PIPE_FORMAT_NONE
;
1268 assert(!util_format_is_luminance(format
) &&
1269 !util_format_is_luminance_alpha(format
));
1274 static inline unsigned
1275 util_format_get_num_planes(enum pipe_format format
)
1277 switch (util_format_description(format
)->layout
) {
1278 case UTIL_FORMAT_LAYOUT_PLANAR3
:
1280 case UTIL_FORMAT_LAYOUT_PLANAR2
:
1287 static inline enum pipe_format
1288 util_format_get_plane_format(enum pipe_format format
, unsigned plane
)
1291 case PIPE_FORMAT_YV12
:
1292 case PIPE_FORMAT_YV16
:
1293 case PIPE_FORMAT_IYUV
:
1294 return PIPE_FORMAT_R8_UNORM
;
1295 case PIPE_FORMAT_NV12
:
1296 return !plane
? PIPE_FORMAT_R8_UNORM
: PIPE_FORMAT_RG88_UNORM
;
1297 case PIPE_FORMAT_NV21
:
1298 return !plane
? PIPE_FORMAT_R8_UNORM
: PIPE_FORMAT_GR88_UNORM
;
1299 case PIPE_FORMAT_P016
:
1300 return !plane
? PIPE_FORMAT_R16_UNORM
: PIPE_FORMAT_R16G16_UNORM
;
1306 static inline unsigned
1307 util_format_get_plane_width(enum pipe_format format
, unsigned plane
,
1311 case PIPE_FORMAT_YV12
:
1312 case PIPE_FORMAT_YV16
:
1313 case PIPE_FORMAT_IYUV
:
1314 case PIPE_FORMAT_NV12
:
1315 case PIPE_FORMAT_NV21
:
1316 case PIPE_FORMAT_P016
:
1317 return !plane
? width
: (width
+ 1) / 2;
1323 static inline unsigned
1324 util_format_get_plane_height(enum pipe_format format
, unsigned plane
,
1328 case PIPE_FORMAT_YV12
:
1329 case PIPE_FORMAT_IYUV
:
1330 case PIPE_FORMAT_NV12
:
1331 case PIPE_FORMAT_NV21
:
1332 case PIPE_FORMAT_P016
:
1333 return !plane
? height
: (height
+ 1) / 2;
1334 case PIPE_FORMAT_YV16
:
1340 bool util_format_planar_is_supported(struct pipe_screen
*screen
,
1341 enum pipe_format format
,
1342 enum pipe_texture_target target
,
1343 unsigned sample_count
,
1344 unsigned storage_sample_count
,
1348 * Return the number of components stored.
1349 * Formats with block size != 1x1 will always have 1 component (the block).
1351 static inline unsigned
1352 util_format_get_nr_components(enum pipe_format format
)
1354 const struct util_format_description
*desc
= util_format_description(format
);
1355 return desc
->nr_channels
;
1359 * Return the index of the first non-void channel
1360 * -1 if no non-void channels
1363 util_format_get_first_non_void_channel(enum pipe_format format
)
1365 const struct util_format_description
*desc
= util_format_description(format
);
1368 for (i
= 0; i
< 4; i
++)
1369 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1379 * Whether this format is any 8-bit UNORM variant. Looser than
1380 * util_is_rgba8_variant (also includes alpha textures, for instance).
1384 util_format_is_unorm8(const struct util_format_description
*desc
)
1386 int c
= util_format_get_first_non_void_channel(desc
->format
);
1391 return desc
->is_unorm
&& desc
->is_array
&& desc
->channel
[c
].size
== 8;
1395 * Format access functions.
1399 util_format_read_4f(enum pipe_format format
,
1400 float *dst
, unsigned dst_stride
,
1401 const void *src
, unsigned src_stride
,
1402 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1405 util_format_write_4f(enum pipe_format format
,
1406 const float *src
, unsigned src_stride
,
1407 void *dst
, unsigned dst_stride
,
1408 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1411 util_format_read_4ub(enum pipe_format format
,
1412 uint8_t *dst
, unsigned dst_stride
,
1413 const void *src
, unsigned src_stride
,
1414 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1417 util_format_write_4ub(enum pipe_format format
,
1418 const uint8_t *src
, unsigned src_stride
,
1419 void *dst
, unsigned dst_stride
,
1420 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1423 util_format_read_4ui(enum pipe_format format
,
1424 unsigned *dst
, unsigned dst_stride
,
1425 const void *src
, unsigned src_stride
,
1426 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1429 util_format_write_4ui(enum pipe_format format
,
1430 const unsigned int *src
, unsigned src_stride
,
1431 void *dst
, unsigned dst_stride
,
1432 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1435 util_format_read_4i(enum pipe_format format
,
1436 int *dst
, unsigned dst_stride
,
1437 const void *src
, unsigned src_stride
,
1438 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1441 util_format_write_4i(enum pipe_format format
,
1442 const int *src
, unsigned src_stride
,
1443 void *dst
, unsigned dst_stride
,
1444 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1447 * Generic format conversion;
1451 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1454 util_format_translate(enum pipe_format dst_format
,
1455 void *dst
, unsigned dst_stride
,
1456 unsigned dst_x
, unsigned dst_y
,
1457 enum pipe_format src_format
,
1458 const void *src
, unsigned src_stride
,
1459 unsigned src_x
, unsigned src_y
,
1460 unsigned width
, unsigned height
);
1463 util_format_translate_3d(enum pipe_format dst_format
,
1464 void *dst
, unsigned dst_stride
,
1465 unsigned dst_slice_stride
,
1466 unsigned dst_x
, unsigned dst_y
,
1468 enum pipe_format src_format
,
1469 const void *src
, unsigned src_stride
,
1470 unsigned src_slice_stride
,
1471 unsigned src_x
, unsigned src_y
,
1472 unsigned src_z
, unsigned width
,
1473 unsigned height
, unsigned depth
);
1476 * Swizzle operations.
1479 /* Compose two sets of swizzles.
1480 * If V is a 4D vector and the function parameters represent functions that
1481 * swizzle vector components, this holds:
1482 * swz2(swz1(V)) = dst(V)
1484 void util_format_compose_swizzles(const unsigned char swz1
[4],
1485 const unsigned char swz2
[4],
1486 unsigned char dst
[4]);
1488 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1489 * to \param src and store the result in \param dst.
1490 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1492 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1493 const union pipe_color_union
*src
,
1494 const unsigned char swz
[4],
1495 const boolean is_integer
);
1497 void pipe_swizzle_4f(float *dst
, const float *src
,
1498 const unsigned char swz
[4]);
1500 void util_format_unswizzle_4f(float *dst
, const float *src
,
1501 const unsigned char swz
[4]);
1504 util_format_snorm8_to_sint8(enum pipe_format format
);
1510 #endif /* ! U_FORMAT_H */