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 case UTIL_FORMAT_LAYOUT_FXT1
:
499 /* XXX add other formats in the future */
506 static inline boolean
507 util_format_is_s3tc(enum pipe_format format
)
509 const struct util_format_description
*desc
= util_format_description(format
);
516 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
519 static inline boolean
520 util_format_is_etc(enum pipe_format format
)
522 const struct util_format_description
*desc
= util_format_description(format
);
529 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
532 static inline boolean
533 util_format_is_srgb(enum pipe_format format
)
535 const struct util_format_description
*desc
= util_format_description(format
);
536 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
539 static inline boolean
540 util_format_has_depth(const struct util_format_description
*desc
)
542 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
543 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
546 static inline boolean
547 util_format_has_stencil(const struct util_format_description
*desc
)
549 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
550 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
553 static inline boolean
554 util_format_is_depth_or_stencil(enum pipe_format format
)
556 const struct util_format_description
*desc
= util_format_description(format
);
563 return util_format_has_depth(desc
) ||
564 util_format_has_stencil(desc
);
567 static inline boolean
568 util_format_is_depth_and_stencil(enum pipe_format format
)
570 const struct util_format_description
*desc
= util_format_description(format
);
577 return util_format_has_depth(desc
) &&
578 util_format_has_stencil(desc
);
582 * For depth-stencil formats, return the equivalent depth-only format.
584 static inline enum pipe_format
585 util_format_get_depth_only(enum pipe_format format
)
588 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
589 return PIPE_FORMAT_Z24X8_UNORM
;
591 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
592 return PIPE_FORMAT_X8Z24_UNORM
;
594 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
595 return PIPE_FORMAT_Z32_FLOAT
;
602 static inline boolean
603 util_format_is_yuv(enum pipe_format format
)
605 const struct util_format_description
*desc
= util_format_description(format
);
612 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
616 * Calculates the depth format type based upon the incoming format description.
618 static inline unsigned
619 util_get_depth_format_type(const struct util_format_description
*desc
)
621 unsigned depth_channel
= desc
->swizzle
[0];
622 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
623 depth_channel
!= PIPE_SWIZZLE_NONE
) {
624 return desc
->channel
[depth_channel
].type
;
626 return UTIL_FORMAT_TYPE_VOID
;
632 * Calculates the MRD for the depth format. MRD is used in depth bias
633 * for UNORM and unbound depth buffers. When the depth buffer is floating
634 * point, the depth bias calculation does not use the MRD. However, the
635 * default MRD will be 1.0 / ((1 << 24) - 1).
638 util_get_depth_format_mrd(const struct util_format_description
*desc
);
642 * Return whether this is an RGBA, Z, S, or combined ZS format.
643 * Useful for initializing pipe_blit_info::mask.
645 static inline unsigned
646 util_format_get_mask(enum pipe_format format
)
648 const struct util_format_description
*desc
=
649 util_format_description(format
);
654 if (util_format_has_depth(desc
)) {
655 if (util_format_has_stencil(desc
)) {
661 if (util_format_has_stencil(desc
)) {
664 return PIPE_MASK_RGBA
;
670 * Give the RGBA colormask of the channels that can be represented in this
673 * That is, the channels whose values are preserved.
675 static inline unsigned
676 util_format_colormask(const struct util_format_description
*desc
)
681 switch (desc
->colorspace
) {
682 case UTIL_FORMAT_COLORSPACE_RGB
:
683 case UTIL_FORMAT_COLORSPACE_SRGB
:
684 case UTIL_FORMAT_COLORSPACE_YUV
:
686 for (chan
= 0; chan
< 4; ++chan
) {
687 if (desc
->swizzle
[chan
] < 4) {
688 colormask
|= (1 << chan
);
692 case UTIL_FORMAT_COLORSPACE_ZS
:
702 * Checks if color mask covers every channel for the specified format
704 * @param desc a format description to check colormask with
705 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
707 static inline boolean
708 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
710 return (~colormask
& util_format_colormask(desc
)) == 0;
715 util_format_is_float(enum pipe_format format
);
719 util_format_has_alpha(enum pipe_format format
);
723 util_format_is_luminance(enum pipe_format format
);
726 util_format_is_alpha(enum pipe_format format
);
729 util_format_is_luminance_alpha(enum pipe_format format
);
733 util_format_is_intensity(enum pipe_format format
);
736 util_format_is_subsampled_422(enum pipe_format format
);
739 util_format_is_pure_integer(enum pipe_format format
);
742 util_format_is_pure_sint(enum pipe_format format
);
745 util_format_is_pure_uint(enum pipe_format format
);
748 util_format_is_snorm(enum pipe_format format
);
751 util_format_is_unorm(enum pipe_format format
);
754 util_format_is_snorm8(enum pipe_format format
);
757 * Check if the src format can be blitted to the destination format with
758 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
762 util_is_format_compatible(const struct util_format_description
*src_desc
,
763 const struct util_format_description
*dst_desc
);
766 * Whether this format is a rgab8 variant.
768 * That is, any format that matches the
770 * PIPE_FORMAT_?8?8?8?8_UNORM
772 static inline boolean
773 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
777 if(desc
->block
.width
!= 1 ||
778 desc
->block
.height
!= 1 ||
779 desc
->block
.bits
!= 32)
782 for(chan
= 0; chan
< 4; ++chan
) {
783 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
784 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
786 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
787 !desc
->channel
[chan
].normalized
)
789 if(desc
->channel
[chan
].size
!= 8)
797 * Return total bits needed for the pixel format per block.
800 util_format_get_blocksizebits(enum pipe_format format
)
802 const struct util_format_description
*desc
= util_format_description(format
);
809 return desc
->block
.bits
;
813 * Return bytes per block (not pixel) for the given format.
816 util_format_get_blocksize(enum pipe_format format
)
818 uint bits
= util_format_get_blocksizebits(format
);
819 uint bytes
= bits
/ 8;
821 assert(bits
% 8 == 0);
831 util_format_get_blockwidth(enum pipe_format format
)
833 const struct util_format_description
*desc
= util_format_description(format
);
840 return desc
->block
.width
;
844 util_format_get_blockheight(enum pipe_format format
)
846 const struct util_format_description
*desc
= util_format_description(format
);
853 return desc
->block
.height
;
857 util_format_get_blockdepth(enum pipe_format format
)
859 const struct util_format_description
*desc
= util_format_description(format
);
866 return desc
->block
.depth
;
869 static inline unsigned
870 util_format_get_nblocksx(enum pipe_format format
,
873 unsigned blockwidth
= util_format_get_blockwidth(format
);
874 return (x
+ blockwidth
- 1) / blockwidth
;
877 static inline unsigned
878 util_format_get_nblocksy(enum pipe_format format
,
881 unsigned blockheight
= util_format_get_blockheight(format
);
882 return (y
+ blockheight
- 1) / blockheight
;
885 static inline unsigned
886 util_format_get_nblocksz(enum pipe_format format
,
889 unsigned blockdepth
= util_format_get_blockdepth(format
);
890 return (z
+ blockdepth
- 1) / blockdepth
;
893 static inline unsigned
894 util_format_get_nblocks(enum pipe_format format
,
898 assert(util_format_get_blockdepth(format
) == 1);
899 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
903 util_format_get_stride(enum pipe_format format
,
906 return (size_t)util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
910 util_format_get_2d_size(enum pipe_format format
,
914 return util_format_get_nblocksy(format
, height
) * stride
;
918 util_format_get_component_bits(enum pipe_format format
,
919 enum util_format_colorspace colorspace
,
922 const struct util_format_description
*desc
= util_format_description(format
);
923 enum util_format_colorspace desc_colorspace
;
930 assert(component
< 4);
932 /* Treat RGB and SRGB as equivalent. */
933 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
934 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
936 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
937 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
939 desc_colorspace
= desc
->colorspace
;
942 if (desc_colorspace
!= colorspace
) {
946 switch (desc
->swizzle
[component
]) {
948 return desc
->channel
[0].size
;
950 return desc
->channel
[1].size
;
952 return desc
->channel
[2].size
;
954 return desc
->channel
[3].size
;
961 * Given a linear RGB colorspace format, return the corresponding SRGB
962 * format, or PIPE_FORMAT_NONE if none.
964 static inline enum pipe_format
965 util_format_srgb(enum pipe_format format
)
967 if (util_format_is_srgb(format
))
971 case PIPE_FORMAT_L8_UNORM
:
972 return PIPE_FORMAT_L8_SRGB
;
973 case PIPE_FORMAT_R8_UNORM
:
974 return PIPE_FORMAT_R8_SRGB
;
975 case PIPE_FORMAT_L8A8_UNORM
:
976 return PIPE_FORMAT_L8A8_SRGB
;
977 case PIPE_FORMAT_R8G8B8_UNORM
:
978 return PIPE_FORMAT_R8G8B8_SRGB
;
979 case PIPE_FORMAT_A8B8G8R8_UNORM
:
980 return PIPE_FORMAT_A8B8G8R8_SRGB
;
981 case PIPE_FORMAT_X8B8G8R8_UNORM
:
982 return PIPE_FORMAT_X8B8G8R8_SRGB
;
983 case PIPE_FORMAT_B8G8R8A8_UNORM
:
984 return PIPE_FORMAT_B8G8R8A8_SRGB
;
985 case PIPE_FORMAT_B8G8R8X8_UNORM
:
986 return PIPE_FORMAT_B8G8R8X8_SRGB
;
987 case PIPE_FORMAT_A8R8G8B8_UNORM
:
988 return PIPE_FORMAT_A8R8G8B8_SRGB
;
989 case PIPE_FORMAT_X8R8G8B8_UNORM
:
990 return PIPE_FORMAT_X8R8G8B8_SRGB
;
991 case PIPE_FORMAT_R8G8B8A8_UNORM
:
992 return PIPE_FORMAT_R8G8B8A8_SRGB
;
993 case PIPE_FORMAT_R8G8B8X8_UNORM
:
994 return PIPE_FORMAT_R8G8B8X8_SRGB
;
995 case PIPE_FORMAT_DXT1_RGB
:
996 return PIPE_FORMAT_DXT1_SRGB
;
997 case PIPE_FORMAT_DXT1_RGBA
:
998 return PIPE_FORMAT_DXT1_SRGBA
;
999 case PIPE_FORMAT_DXT3_RGBA
:
1000 return PIPE_FORMAT_DXT3_SRGBA
;
1001 case PIPE_FORMAT_DXT5_RGBA
:
1002 return PIPE_FORMAT_DXT5_SRGBA
;
1003 case PIPE_FORMAT_B5G6R5_UNORM
:
1004 return PIPE_FORMAT_B5G6R5_SRGB
;
1005 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1006 return PIPE_FORMAT_BPTC_SRGBA
;
1007 case PIPE_FORMAT_ETC2_RGB8
:
1008 return PIPE_FORMAT_ETC2_SRGB8
;
1009 case PIPE_FORMAT_ETC2_RGB8A1
:
1010 return PIPE_FORMAT_ETC2_SRGB8A1
;
1011 case PIPE_FORMAT_ETC2_RGBA8
:
1012 return PIPE_FORMAT_ETC2_SRGBA8
;
1013 case PIPE_FORMAT_ASTC_4x4
:
1014 return PIPE_FORMAT_ASTC_4x4_SRGB
;
1015 case PIPE_FORMAT_ASTC_5x4
:
1016 return PIPE_FORMAT_ASTC_5x4_SRGB
;
1017 case PIPE_FORMAT_ASTC_5x5
:
1018 return PIPE_FORMAT_ASTC_5x5_SRGB
;
1019 case PIPE_FORMAT_ASTC_6x5
:
1020 return PIPE_FORMAT_ASTC_6x5_SRGB
;
1021 case PIPE_FORMAT_ASTC_6x6
:
1022 return PIPE_FORMAT_ASTC_6x6_SRGB
;
1023 case PIPE_FORMAT_ASTC_8x5
:
1024 return PIPE_FORMAT_ASTC_8x5_SRGB
;
1025 case PIPE_FORMAT_ASTC_8x6
:
1026 return PIPE_FORMAT_ASTC_8x6_SRGB
;
1027 case PIPE_FORMAT_ASTC_8x8
:
1028 return PIPE_FORMAT_ASTC_8x8_SRGB
;
1029 case PIPE_FORMAT_ASTC_10x5
:
1030 return PIPE_FORMAT_ASTC_10x5_SRGB
;
1031 case PIPE_FORMAT_ASTC_10x6
:
1032 return PIPE_FORMAT_ASTC_10x6_SRGB
;
1033 case PIPE_FORMAT_ASTC_10x8
:
1034 return PIPE_FORMAT_ASTC_10x8_SRGB
;
1035 case PIPE_FORMAT_ASTC_10x10
:
1036 return PIPE_FORMAT_ASTC_10x10_SRGB
;
1037 case PIPE_FORMAT_ASTC_12x10
:
1038 return PIPE_FORMAT_ASTC_12x10_SRGB
;
1039 case PIPE_FORMAT_ASTC_12x12
:
1040 return PIPE_FORMAT_ASTC_12x12_SRGB
;
1043 return PIPE_FORMAT_NONE
;
1048 * Given an sRGB format, return the corresponding linear colorspace format.
1049 * For non sRGB formats, return the format unchanged.
1051 static inline enum pipe_format
1052 util_format_linear(enum pipe_format format
)
1055 case PIPE_FORMAT_L8_SRGB
:
1056 return PIPE_FORMAT_L8_UNORM
;
1057 case PIPE_FORMAT_R8_SRGB
:
1058 return PIPE_FORMAT_R8_UNORM
;
1059 case PIPE_FORMAT_L8A8_SRGB
:
1060 return PIPE_FORMAT_L8A8_UNORM
;
1061 case PIPE_FORMAT_R8G8B8_SRGB
:
1062 return PIPE_FORMAT_R8G8B8_UNORM
;
1063 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1064 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1065 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1066 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1067 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1068 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1069 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1070 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1071 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1072 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1073 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1074 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1075 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1076 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1077 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1078 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1079 case PIPE_FORMAT_DXT1_SRGB
:
1080 return PIPE_FORMAT_DXT1_RGB
;
1081 case PIPE_FORMAT_DXT1_SRGBA
:
1082 return PIPE_FORMAT_DXT1_RGBA
;
1083 case PIPE_FORMAT_DXT3_SRGBA
:
1084 return PIPE_FORMAT_DXT3_RGBA
;
1085 case PIPE_FORMAT_DXT5_SRGBA
:
1086 return PIPE_FORMAT_DXT5_RGBA
;
1087 case PIPE_FORMAT_B5G6R5_SRGB
:
1088 return PIPE_FORMAT_B5G6R5_UNORM
;
1089 case PIPE_FORMAT_BPTC_SRGBA
:
1090 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1091 case PIPE_FORMAT_ETC2_SRGB8
:
1092 return PIPE_FORMAT_ETC2_RGB8
;
1093 case PIPE_FORMAT_ETC2_SRGB8A1
:
1094 return PIPE_FORMAT_ETC2_RGB8A1
;
1095 case PIPE_FORMAT_ETC2_SRGBA8
:
1096 return PIPE_FORMAT_ETC2_RGBA8
;
1097 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1098 return PIPE_FORMAT_ASTC_4x4
;
1099 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1100 return PIPE_FORMAT_ASTC_5x4
;
1101 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1102 return PIPE_FORMAT_ASTC_5x5
;
1103 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1104 return PIPE_FORMAT_ASTC_6x5
;
1105 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1106 return PIPE_FORMAT_ASTC_6x6
;
1107 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1108 return PIPE_FORMAT_ASTC_8x5
;
1109 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1110 return PIPE_FORMAT_ASTC_8x6
;
1111 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1112 return PIPE_FORMAT_ASTC_8x8
;
1113 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1114 return PIPE_FORMAT_ASTC_10x5
;
1115 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1116 return PIPE_FORMAT_ASTC_10x6
;
1117 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1118 return PIPE_FORMAT_ASTC_10x8
;
1119 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1120 return PIPE_FORMAT_ASTC_10x10
;
1121 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1122 return PIPE_FORMAT_ASTC_12x10
;
1123 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1124 return PIPE_FORMAT_ASTC_12x12
;
1131 * Given a depth-stencil format, return the corresponding stencil-only format.
1132 * For stencil-only formats, return the format unchanged.
1134 static inline enum pipe_format
1135 util_format_stencil_only(enum pipe_format format
)
1138 /* mask out the depth component */
1139 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1140 return PIPE_FORMAT_X24S8_UINT
;
1141 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1142 return PIPE_FORMAT_S8X24_UINT
;
1143 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1144 return PIPE_FORMAT_X32_S8X24_UINT
;
1146 /* stencil only formats */
1147 case PIPE_FORMAT_X24S8_UINT
:
1148 case PIPE_FORMAT_S8X24_UINT
:
1149 case PIPE_FORMAT_X32_S8X24_UINT
:
1150 case PIPE_FORMAT_S8_UINT
:
1155 return PIPE_FORMAT_NONE
;
1160 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1161 * This is identity for non-intensity formats.
1163 static inline enum pipe_format
1164 util_format_intensity_to_red(enum pipe_format format
)
1167 case PIPE_FORMAT_I8_UNORM
:
1168 return PIPE_FORMAT_R8_UNORM
;
1169 case PIPE_FORMAT_I8_SNORM
:
1170 return PIPE_FORMAT_R8_SNORM
;
1171 case PIPE_FORMAT_I16_UNORM
:
1172 return PIPE_FORMAT_R16_UNORM
;
1173 case PIPE_FORMAT_I16_SNORM
:
1174 return PIPE_FORMAT_R16_SNORM
;
1175 case PIPE_FORMAT_I16_FLOAT
:
1176 return PIPE_FORMAT_R16_FLOAT
;
1177 case PIPE_FORMAT_I32_FLOAT
:
1178 return PIPE_FORMAT_R32_FLOAT
;
1179 case PIPE_FORMAT_I8_UINT
:
1180 return PIPE_FORMAT_R8_UINT
;
1181 case PIPE_FORMAT_I8_SINT
:
1182 return PIPE_FORMAT_R8_SINT
;
1183 case PIPE_FORMAT_I16_UINT
:
1184 return PIPE_FORMAT_R16_UINT
;
1185 case PIPE_FORMAT_I16_SINT
:
1186 return PIPE_FORMAT_R16_SINT
;
1187 case PIPE_FORMAT_I32_UINT
:
1188 return PIPE_FORMAT_R32_UINT
;
1189 case PIPE_FORMAT_I32_SINT
:
1190 return PIPE_FORMAT_R32_SINT
;
1192 assert(!util_format_is_intensity(format
));
1198 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1199 * This is identity for non-luminance formats.
1201 static inline enum pipe_format
1202 util_format_luminance_to_red(enum pipe_format format
)
1205 case PIPE_FORMAT_L8_UNORM
:
1206 return PIPE_FORMAT_R8_UNORM
;
1207 case PIPE_FORMAT_L8_SNORM
:
1208 return PIPE_FORMAT_R8_SNORM
;
1209 case PIPE_FORMAT_L16_UNORM
:
1210 return PIPE_FORMAT_R16_UNORM
;
1211 case PIPE_FORMAT_L16_SNORM
:
1212 return PIPE_FORMAT_R16_SNORM
;
1213 case PIPE_FORMAT_L16_FLOAT
:
1214 return PIPE_FORMAT_R16_FLOAT
;
1215 case PIPE_FORMAT_L32_FLOAT
:
1216 return PIPE_FORMAT_R32_FLOAT
;
1217 case PIPE_FORMAT_L8_UINT
:
1218 return PIPE_FORMAT_R8_UINT
;
1219 case PIPE_FORMAT_L8_SINT
:
1220 return PIPE_FORMAT_R8_SINT
;
1221 case PIPE_FORMAT_L16_UINT
:
1222 return PIPE_FORMAT_R16_UINT
;
1223 case PIPE_FORMAT_L16_SINT
:
1224 return PIPE_FORMAT_R16_SINT
;
1225 case PIPE_FORMAT_L32_UINT
:
1226 return PIPE_FORMAT_R32_UINT
;
1227 case PIPE_FORMAT_L32_SINT
:
1228 return PIPE_FORMAT_R32_SINT
;
1230 case PIPE_FORMAT_LATC1_UNORM
:
1231 return PIPE_FORMAT_RGTC1_UNORM
;
1232 case PIPE_FORMAT_LATC1_SNORM
:
1233 return PIPE_FORMAT_RGTC1_SNORM
;
1235 case PIPE_FORMAT_L4A4_UNORM
:
1236 return PIPE_FORMAT_R4A4_UNORM
;
1238 case PIPE_FORMAT_L8A8_UNORM
:
1239 return PIPE_FORMAT_R8A8_UNORM
;
1240 case PIPE_FORMAT_L8A8_SNORM
:
1241 return PIPE_FORMAT_R8A8_SNORM
;
1242 case PIPE_FORMAT_L16A16_UNORM
:
1243 return PIPE_FORMAT_R16A16_UNORM
;
1244 case PIPE_FORMAT_L16A16_SNORM
:
1245 return PIPE_FORMAT_R16A16_SNORM
;
1246 case PIPE_FORMAT_L16A16_FLOAT
:
1247 return PIPE_FORMAT_R16A16_FLOAT
;
1248 case PIPE_FORMAT_L32A32_FLOAT
:
1249 return PIPE_FORMAT_R32A32_FLOAT
;
1250 case PIPE_FORMAT_L8A8_UINT
:
1251 return PIPE_FORMAT_R8A8_UINT
;
1252 case PIPE_FORMAT_L8A8_SINT
:
1253 return PIPE_FORMAT_R8A8_SINT
;
1254 case PIPE_FORMAT_L16A16_UINT
:
1255 return PIPE_FORMAT_R16A16_UINT
;
1256 case PIPE_FORMAT_L16A16_SINT
:
1257 return PIPE_FORMAT_R16A16_SINT
;
1258 case PIPE_FORMAT_L32A32_UINT
:
1259 return PIPE_FORMAT_R32A32_UINT
;
1260 case PIPE_FORMAT_L32A32_SINT
:
1261 return PIPE_FORMAT_R32A32_SINT
;
1263 /* We don't have compressed red-alpha variants for these. */
1264 case PIPE_FORMAT_LATC2_UNORM
:
1265 case PIPE_FORMAT_LATC2_SNORM
:
1266 return PIPE_FORMAT_NONE
;
1269 assert(!util_format_is_luminance(format
) &&
1270 !util_format_is_luminance_alpha(format
));
1275 static inline unsigned
1276 util_format_get_num_planes(enum pipe_format format
)
1278 switch (util_format_description(format
)->layout
) {
1279 case UTIL_FORMAT_LAYOUT_PLANAR3
:
1281 case UTIL_FORMAT_LAYOUT_PLANAR2
:
1288 static inline enum pipe_format
1289 util_format_get_plane_format(enum pipe_format format
, unsigned plane
)
1292 case PIPE_FORMAT_YV12
:
1293 case PIPE_FORMAT_YV16
:
1294 case PIPE_FORMAT_IYUV
:
1295 return PIPE_FORMAT_R8_UNORM
;
1296 case PIPE_FORMAT_NV12
:
1297 return !plane
? PIPE_FORMAT_R8_UNORM
: PIPE_FORMAT_RG88_UNORM
;
1298 case PIPE_FORMAT_NV21
:
1299 return !plane
? PIPE_FORMAT_R8_UNORM
: PIPE_FORMAT_GR88_UNORM
;
1300 case PIPE_FORMAT_P016
:
1301 return !plane
? PIPE_FORMAT_R16_UNORM
: PIPE_FORMAT_R16G16_UNORM
;
1307 static inline unsigned
1308 util_format_get_plane_width(enum pipe_format format
, unsigned plane
,
1312 case PIPE_FORMAT_YV12
:
1313 case PIPE_FORMAT_YV16
:
1314 case PIPE_FORMAT_IYUV
:
1315 case PIPE_FORMAT_NV12
:
1316 case PIPE_FORMAT_NV21
:
1317 case PIPE_FORMAT_P016
:
1318 return !plane
? width
: (width
+ 1) / 2;
1324 static inline unsigned
1325 util_format_get_plane_height(enum pipe_format format
, unsigned plane
,
1329 case PIPE_FORMAT_YV12
:
1330 case PIPE_FORMAT_IYUV
:
1331 case PIPE_FORMAT_NV12
:
1332 case PIPE_FORMAT_NV21
:
1333 case PIPE_FORMAT_P016
:
1334 return !plane
? height
: (height
+ 1) / 2;
1335 case PIPE_FORMAT_YV16
:
1341 bool util_format_planar_is_supported(struct pipe_screen
*screen
,
1342 enum pipe_format format
,
1343 enum pipe_texture_target target
,
1344 unsigned sample_count
,
1345 unsigned storage_sample_count
,
1349 * Return the number of components stored.
1350 * Formats with block size != 1x1 will always have 1 component (the block).
1352 static inline unsigned
1353 util_format_get_nr_components(enum pipe_format format
)
1355 const struct util_format_description
*desc
= util_format_description(format
);
1356 return desc
->nr_channels
;
1360 * Return the index of the first non-void channel
1361 * -1 if no non-void channels
1364 util_format_get_first_non_void_channel(enum pipe_format format
)
1366 const struct util_format_description
*desc
= util_format_description(format
);
1369 for (i
= 0; i
< 4; i
++)
1370 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1380 * Whether this format is any 8-bit UNORM variant. Looser than
1381 * util_is_rgba8_variant (also includes alpha textures, for instance).
1385 util_format_is_unorm8(const struct util_format_description
*desc
)
1387 int c
= util_format_get_first_non_void_channel(desc
->format
);
1392 return desc
->is_unorm
&& desc
->is_array
&& desc
->channel
[c
].size
== 8;
1396 * Format access functions.
1400 util_format_read_4f(enum pipe_format format
,
1401 float *dst
, unsigned dst_stride
,
1402 const void *src
, unsigned src_stride
,
1403 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1406 util_format_write_4f(enum pipe_format format
,
1407 const float *src
, unsigned src_stride
,
1408 void *dst
, unsigned dst_stride
,
1409 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1412 util_format_read_4ub(enum pipe_format format
,
1413 uint8_t *dst
, unsigned dst_stride
,
1414 const void *src
, unsigned src_stride
,
1415 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1418 util_format_write_4ub(enum pipe_format format
,
1419 const uint8_t *src
, unsigned src_stride
,
1420 void *dst
, unsigned dst_stride
,
1421 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1424 util_format_read_4ui(enum pipe_format format
,
1425 unsigned *dst
, unsigned dst_stride
,
1426 const void *src
, unsigned src_stride
,
1427 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1430 util_format_write_4ui(enum pipe_format format
,
1431 const unsigned int *src
, unsigned src_stride
,
1432 void *dst
, unsigned dst_stride
,
1433 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1436 util_format_read_4i(enum pipe_format format
,
1437 int *dst
, unsigned dst_stride
,
1438 const void *src
, unsigned src_stride
,
1439 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1442 util_format_write_4i(enum pipe_format format
,
1443 const int *src
, unsigned src_stride
,
1444 void *dst
, unsigned dst_stride
,
1445 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1448 * Generic format conversion;
1452 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1455 util_format_translate(enum pipe_format dst_format
,
1456 void *dst
, unsigned dst_stride
,
1457 unsigned dst_x
, unsigned dst_y
,
1458 enum pipe_format src_format
,
1459 const void *src
, unsigned src_stride
,
1460 unsigned src_x
, unsigned src_y
,
1461 unsigned width
, unsigned height
);
1464 util_format_translate_3d(enum pipe_format dst_format
,
1465 void *dst
, unsigned dst_stride
,
1466 unsigned dst_slice_stride
,
1467 unsigned dst_x
, unsigned dst_y
,
1469 enum pipe_format src_format
,
1470 const void *src
, unsigned src_stride
,
1471 unsigned src_slice_stride
,
1472 unsigned src_x
, unsigned src_y
,
1473 unsigned src_z
, unsigned width
,
1474 unsigned height
, unsigned depth
);
1477 * Swizzle operations.
1480 /* Compose two sets of swizzles.
1481 * If V is a 4D vector and the function parameters represent functions that
1482 * swizzle vector components, this holds:
1483 * swz2(swz1(V)) = dst(V)
1485 void util_format_compose_swizzles(const unsigned char swz1
[4],
1486 const unsigned char swz2
[4],
1487 unsigned char dst
[4]);
1489 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1490 * to \param src and store the result in \param dst.
1491 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1493 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1494 const union pipe_color_union
*src
,
1495 const unsigned char swz
[4],
1496 const boolean is_integer
);
1498 void pipe_swizzle_4f(float *dst
, const float *src
,
1499 const unsigned char swz
[4]);
1501 void util_format_unswizzle_4f(float *dst
, const float *src
,
1502 const unsigned char swz
[4]);
1505 util_format_snorm8_to_sint8(enum pipe_format format
);
1509 util_copy_rect(ubyte
* dst
, enum pipe_format format
,
1510 unsigned dst_stride
, unsigned dst_x
, unsigned dst_y
,
1511 unsigned width
, unsigned height
, const ubyte
* src
,
1512 int src_stride
, unsigned src_x
, unsigned src_y
);
1518 #endif /* ! U_FORMAT_H */