1 /**************************************************************************
3 * Copyright 2009-2010 Vmware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
37 union pipe_color_union
;
46 * Describe how to pack/unpack pixels into/from the prescribed format.
48 * XXX: This could be renamed to something like util_format_pack, or broke down
49 * in flags inside util_format_block that said exactly what we want.
51 enum util_format_layout
{
53 * Formats with util_format_block::width == util_format_block::height == 1
54 * that can be described as an ordinary data structure.
56 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
59 * Formats with sub-sampled channels.
61 * This is for formats like YVYU where there is less than one sample per
64 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
67 * S3 Texture Compression formats.
69 UTIL_FORMAT_LAYOUT_S3TC
= 4,
72 * Red-Green Texture Compression formats.
74 UTIL_FORMAT_LAYOUT_RGTC
= 5,
77 * Ericsson Texture Compression
79 UTIL_FORMAT_LAYOUT_ETC
= 6,
82 * BC6/7 Texture Compression
84 UTIL_FORMAT_LAYOUT_BPTC
= 7,
86 UTIL_FORMAT_LAYOUT_ASTC
= 8,
88 UTIL_FORMAT_LAYOUT_ATC
= 9,
91 * Everything else that doesn't fit in any of the above layouts.
93 UTIL_FORMAT_LAYOUT_OTHER
= 10
97 struct util_format_block
99 /** Block width in pixels */
102 /** Block height in pixels */
105 /** Block size in bits */
110 enum util_format_type
{
111 UTIL_FORMAT_TYPE_VOID
= 0,
112 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
113 UTIL_FORMAT_TYPE_SIGNED
= 2,
114 UTIL_FORMAT_TYPE_FIXED
= 3,
115 UTIL_FORMAT_TYPE_FLOAT
= 4
119 enum util_format_colorspace
{
120 UTIL_FORMAT_COLORSPACE_RGB
= 0,
121 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
122 UTIL_FORMAT_COLORSPACE_YUV
= 2,
123 UTIL_FORMAT_COLORSPACE_ZS
= 3
127 struct util_format_channel_description
129 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
130 unsigned normalized
:1;
131 unsigned pure_integer
:1;
132 unsigned size
:9; /**< bits per channel */
133 unsigned shift
:16; /** number of bits from lsb */
137 struct util_format_description
139 enum pipe_format format
;
144 * Short name, striped of the prefix, lower case.
146 const char *short_name
;
149 * Pixel block dimensions.
151 struct util_format_block block
;
153 enum util_format_layout layout
;
156 * The number of channels.
158 unsigned nr_channels
:3;
161 * Whether all channels have the same number of (whole) bytes and type.
166 * Whether the pixel format can be described as a bitfield structure.
169 * - pixel depth must be 8, 16, or 32 bits;
170 * - all channels must be unsigned, signed, or void
172 unsigned is_bitmask
:1;
175 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
180 * Whether the format contains UNORM channels
185 * Whether the format contains SNORM channels
190 * Input channel description, in the order XYZW.
192 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
194 * If each channel is accessed as an individual N-byte value, X is always
195 * at the lowest address in memory, Y is always next, and so on. For all
196 * currently-defined formats, the N-byte value has native endianness.
198 * If instead a group of channels is accessed as a single N-byte value,
199 * the order of the channels within that value depends on endianness.
200 * For big-endian targets, X is the most significant subvalue,
201 * otherwise it is the least significant one.
203 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
206 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
207 * big-endian: X Yu Ym Yl
209 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
213 * little-endian: YYYXXXXX WZZZZZYY
214 * big-endian: XXXXXYYY YYZZZZZW
216 struct util_format_channel_description channel
[4];
219 * Output channel swizzle.
221 * The order is either:
225 * depending on the colorspace.
227 unsigned char swizzle
[4];
230 * Colorspace transformation.
232 enum util_format_colorspace colorspace
;
235 * Unpack pixel blocks to R8G8B8A8_UNORM.
236 * Note: strides are in bytes.
238 * Only defined for non-depth-stencil formats.
241 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
242 const uint8_t *src
, unsigned src_stride
,
243 unsigned width
, unsigned height
);
246 * Pack pixel blocks from R8G8B8A8_UNORM.
247 * Note: strides are in bytes.
249 * Only defined for non-depth-stencil formats.
252 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
253 const uint8_t *src
, unsigned src_stride
,
254 unsigned width
, unsigned height
);
257 * Fetch a single pixel (i, j) from a block.
259 * XXX: Only defined for a very few select formats.
262 (*fetch_rgba_8unorm
)(uint8_t *dst
,
264 unsigned i
, unsigned j
);
267 * Unpack pixel blocks to R32G32B32A32_FLOAT.
268 * Note: strides are in bytes.
270 * Only defined for non-depth-stencil formats.
273 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
274 const uint8_t *src
, unsigned src_stride
,
275 unsigned width
, unsigned height
);
278 * Pack pixel blocks from R32G32B32A32_FLOAT.
279 * Note: strides are in bytes.
281 * Only defined for non-depth-stencil formats.
284 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
285 const float *src
, unsigned src_stride
,
286 unsigned width
, unsigned height
);
289 * Fetch a single pixel (i, j) from a block.
291 * Only defined for non-depth-stencil and non-integer formats.
294 (*fetch_rgba_float
)(float *dst
,
296 unsigned i
, unsigned j
);
299 * Unpack pixels to Z32_UNORM.
300 * Note: strides are in bytes.
302 * Only defined for depth formats.
305 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
306 const uint8_t *src
, unsigned src_stride
,
307 unsigned width
, unsigned height
);
310 * Pack pixels from Z32_FLOAT.
311 * Note: strides are in bytes.
313 * Only defined for depth formats.
316 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
317 const uint32_t *src
, unsigned src_stride
,
318 unsigned width
, unsigned height
);
321 * Unpack pixels to Z32_FLOAT.
322 * Note: strides are in bytes.
324 * Only defined for depth formats.
327 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
328 const uint8_t *src
, unsigned src_stride
,
329 unsigned width
, unsigned height
);
332 * Pack pixels from Z32_FLOAT.
333 * Note: strides are in bytes.
335 * Only defined for depth formats.
338 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
339 const float *src
, unsigned src_stride
,
340 unsigned width
, unsigned height
);
343 * Unpack pixels to S8_UINT.
344 * Note: strides are in bytes.
346 * Only defined for stencil formats.
349 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
350 const uint8_t *src
, unsigned src_stride
,
351 unsigned width
, unsigned height
);
354 * Pack pixels from S8_UINT.
355 * Note: strides are in bytes.
357 * Only defined for stencil formats.
360 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
361 const uint8_t *src
, unsigned src_stride
,
362 unsigned width
, unsigned height
);
365 * Unpack pixel blocks to R32G32B32A32_UINT.
366 * Note: strides are in bytes.
368 * Only defined for INT formats.
371 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
372 const uint8_t *src
, unsigned src_stride
,
373 unsigned width
, unsigned height
);
376 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
377 const uint32_t *src
, unsigned src_stride
,
378 unsigned width
, unsigned height
);
381 * Unpack pixel blocks to R32G32B32A32_SINT.
382 * Note: strides are in bytes.
384 * Only defined for INT formats.
387 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
388 const uint8_t *src
, unsigned src_stride
,
389 unsigned width
, unsigned height
);
392 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
393 const int32_t *src
, unsigned src_stride
,
394 unsigned width
, unsigned height
);
397 * Fetch a single pixel (i, j) from a block.
399 * Only defined for unsigned (pure) integer formats.
402 (*fetch_rgba_uint
)(uint32_t *dst
,
404 unsigned i
, unsigned j
);
407 * Fetch a single pixel (i, j) from a block.
409 * Only defined for signed (pure) integer formats.
412 (*fetch_rgba_sint
)(int32_t *dst
,
414 unsigned i
, unsigned j
);
418 extern const struct util_format_description
419 util_format_description_table
[];
422 const struct util_format_description
*
423 util_format_description(enum pipe_format format
);
427 * Format query functions.
430 static inline const char *
431 util_format_name(enum pipe_format format
)
433 const struct util_format_description
*desc
= util_format_description(format
);
437 return "PIPE_FORMAT_???";
443 static inline const char *
444 util_format_short_name(enum pipe_format format
)
446 const struct util_format_description
*desc
= util_format_description(format
);
453 return desc
->short_name
;
457 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
459 static inline boolean
460 util_format_is_plain(enum pipe_format format
)
462 const struct util_format_description
*desc
= util_format_description(format
);
468 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
471 static inline boolean
472 util_format_is_compressed(enum pipe_format format
)
474 const struct util_format_description
*desc
= util_format_description(format
);
481 switch (desc
->layout
) {
482 case UTIL_FORMAT_LAYOUT_S3TC
:
483 case UTIL_FORMAT_LAYOUT_RGTC
:
484 case UTIL_FORMAT_LAYOUT_ETC
:
485 case UTIL_FORMAT_LAYOUT_BPTC
:
486 case UTIL_FORMAT_LAYOUT_ASTC
:
487 case UTIL_FORMAT_LAYOUT_ATC
:
488 /* XXX add other formats in the future */
495 static inline boolean
496 util_format_is_s3tc(enum pipe_format format
)
498 const struct util_format_description
*desc
= util_format_description(format
);
505 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
508 static inline boolean
509 util_format_is_etc(enum pipe_format format
)
511 const struct util_format_description
*desc
= util_format_description(format
);
518 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
521 static inline boolean
522 util_format_is_srgb(enum pipe_format format
)
524 const struct util_format_description
*desc
= util_format_description(format
);
525 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
528 static inline boolean
529 util_format_has_depth(const struct util_format_description
*desc
)
531 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
532 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
535 static inline boolean
536 util_format_has_stencil(const struct util_format_description
*desc
)
538 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
539 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
542 static inline boolean
543 util_format_is_depth_or_stencil(enum pipe_format format
)
545 const struct util_format_description
*desc
= util_format_description(format
);
552 return util_format_has_depth(desc
) ||
553 util_format_has_stencil(desc
);
556 static inline boolean
557 util_format_is_depth_and_stencil(enum pipe_format format
)
559 const struct util_format_description
*desc
= util_format_description(format
);
566 return util_format_has_depth(desc
) &&
567 util_format_has_stencil(desc
);
571 * For depth-stencil formats, return the equivalent depth-only format.
573 static inline boolean
574 util_format_get_depth_only(enum pipe_format format
)
577 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
578 return PIPE_FORMAT_Z24X8_UNORM
;
580 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
581 return PIPE_FORMAT_X8Z24_UNORM
;
583 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
584 return PIPE_FORMAT_Z32_FLOAT
;
591 static inline boolean
592 util_format_is_yuv(enum pipe_format format
)
594 const struct util_format_description
*desc
= util_format_description(format
);
601 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
605 * Calculates the depth format type based upon the incoming format description.
607 static inline unsigned
608 util_get_depth_format_type(const struct util_format_description
*desc
)
610 unsigned depth_channel
= desc
->swizzle
[0];
611 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
612 depth_channel
!= PIPE_SWIZZLE_NONE
) {
613 return desc
->channel
[depth_channel
].type
;
615 return UTIL_FORMAT_TYPE_VOID
;
621 * Calculates the MRD for the depth format. MRD is used in depth bias
622 * for UNORM and unbound depth buffers. When the depth buffer is floating
623 * point, the depth bias calculation does not use the MRD. However, the
624 * default MRD will be 1.0 / ((1 << 24) - 1).
627 util_get_depth_format_mrd(const struct util_format_description
*desc
);
631 * Return whether this is an RGBA, Z, S, or combined ZS format.
632 * Useful for initializing pipe_blit_info::mask.
634 static inline unsigned
635 util_format_get_mask(enum pipe_format format
)
637 const struct util_format_description
*desc
=
638 util_format_description(format
);
643 if (util_format_has_depth(desc
)) {
644 if (util_format_has_stencil(desc
)) {
650 if (util_format_has_stencil(desc
)) {
653 return PIPE_MASK_RGBA
;
659 * Give the RGBA colormask of the channels that can be represented in this
662 * That is, the channels whose values are preserved.
664 static inline unsigned
665 util_format_colormask(const struct util_format_description
*desc
)
670 switch (desc
->colorspace
) {
671 case UTIL_FORMAT_COLORSPACE_RGB
:
672 case UTIL_FORMAT_COLORSPACE_SRGB
:
673 case UTIL_FORMAT_COLORSPACE_YUV
:
675 for (chan
= 0; chan
< 4; ++chan
) {
676 if (desc
->swizzle
[chan
] < 4) {
677 colormask
|= (1 << chan
);
681 case UTIL_FORMAT_COLORSPACE_ZS
:
691 * Checks if color mask covers every channel for the specified format
693 * @param desc a format description to check colormask with
694 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
696 static inline boolean
697 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
699 return (~colormask
& util_format_colormask(desc
)) == 0;
704 util_format_is_float(enum pipe_format format
);
708 util_format_has_alpha(enum pipe_format format
);
712 util_format_is_luminance(enum pipe_format format
);
715 util_format_is_alpha(enum pipe_format format
);
718 util_format_is_luminance_alpha(enum pipe_format format
);
722 util_format_is_intensity(enum pipe_format format
);
725 util_format_is_subsampled_422(enum pipe_format format
);
728 util_format_is_pure_integer(enum pipe_format format
);
731 util_format_is_pure_sint(enum pipe_format format
);
734 util_format_is_pure_uint(enum pipe_format format
);
737 util_format_is_snorm(enum pipe_format format
);
740 util_format_is_unorm(enum pipe_format format
);
743 util_format_is_snorm8(enum pipe_format format
);
746 * Check if the src format can be blitted to the destination format with
747 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
751 util_is_format_compatible(const struct util_format_description
*src_desc
,
752 const struct util_format_description
*dst_desc
);
755 * Whether this format is a rgab8 variant.
757 * That is, any format that matches the
759 * PIPE_FORMAT_?8?8?8?8_UNORM
761 static inline boolean
762 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
766 if(desc
->block
.width
!= 1 ||
767 desc
->block
.height
!= 1 ||
768 desc
->block
.bits
!= 32)
771 for(chan
= 0; chan
< 4; ++chan
) {
772 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
773 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
775 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
776 !desc
->channel
[chan
].normalized
)
778 if(desc
->channel
[chan
].size
!= 8)
786 * Return total bits needed for the pixel format per block.
789 util_format_get_blocksizebits(enum pipe_format format
)
791 const struct util_format_description
*desc
= util_format_description(format
);
798 return desc
->block
.bits
;
802 * Return bytes per block (not pixel) for the given format.
805 util_format_get_blocksize(enum pipe_format format
)
807 uint bits
= util_format_get_blocksizebits(format
);
808 uint bytes
= bits
/ 8;
810 assert(bits
% 8 == 0);
820 util_format_get_blockwidth(enum pipe_format format
)
822 const struct util_format_description
*desc
= util_format_description(format
);
829 return desc
->block
.width
;
833 util_format_get_blockheight(enum pipe_format format
)
835 const struct util_format_description
*desc
= util_format_description(format
);
842 return desc
->block
.height
;
845 static inline unsigned
846 util_format_get_nblocksx(enum pipe_format format
,
849 unsigned blockwidth
= util_format_get_blockwidth(format
);
850 return (x
+ blockwidth
- 1) / blockwidth
;
853 static inline unsigned
854 util_format_get_nblocksy(enum pipe_format format
,
857 unsigned blockheight
= util_format_get_blockheight(format
);
858 return (y
+ blockheight
- 1) / blockheight
;
861 static inline unsigned
862 util_format_get_nblocks(enum pipe_format format
,
866 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
870 util_format_get_stride(enum pipe_format format
,
873 return (size_t)util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
877 util_format_get_2d_size(enum pipe_format format
,
881 return util_format_get_nblocksy(format
, height
) * stride
;
885 util_format_get_component_bits(enum pipe_format format
,
886 enum util_format_colorspace colorspace
,
889 const struct util_format_description
*desc
= util_format_description(format
);
890 enum util_format_colorspace desc_colorspace
;
897 assert(component
< 4);
899 /* Treat RGB and SRGB as equivalent. */
900 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
901 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
903 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
904 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
906 desc_colorspace
= desc
->colorspace
;
909 if (desc_colorspace
!= colorspace
) {
913 switch (desc
->swizzle
[component
]) {
915 return desc
->channel
[0].size
;
917 return desc
->channel
[1].size
;
919 return desc
->channel
[2].size
;
921 return desc
->channel
[3].size
;
928 * Given a linear RGB colorspace format, return the corresponding SRGB
929 * format, or PIPE_FORMAT_NONE if none.
931 static inline enum pipe_format
932 util_format_srgb(enum pipe_format format
)
934 if (util_format_is_srgb(format
))
938 case PIPE_FORMAT_L8_UNORM
:
939 return PIPE_FORMAT_L8_SRGB
;
940 case PIPE_FORMAT_R8_UNORM
:
941 return PIPE_FORMAT_R8_SRGB
;
942 case PIPE_FORMAT_L8A8_UNORM
:
943 return PIPE_FORMAT_L8A8_SRGB
;
944 case PIPE_FORMAT_R8G8B8_UNORM
:
945 return PIPE_FORMAT_R8G8B8_SRGB
;
946 case PIPE_FORMAT_A8B8G8R8_UNORM
:
947 return PIPE_FORMAT_A8B8G8R8_SRGB
;
948 case PIPE_FORMAT_X8B8G8R8_UNORM
:
949 return PIPE_FORMAT_X8B8G8R8_SRGB
;
950 case PIPE_FORMAT_B8G8R8A8_UNORM
:
951 return PIPE_FORMAT_B8G8R8A8_SRGB
;
952 case PIPE_FORMAT_B8G8R8X8_UNORM
:
953 return PIPE_FORMAT_B8G8R8X8_SRGB
;
954 case PIPE_FORMAT_A8R8G8B8_UNORM
:
955 return PIPE_FORMAT_A8R8G8B8_SRGB
;
956 case PIPE_FORMAT_X8R8G8B8_UNORM
:
957 return PIPE_FORMAT_X8R8G8B8_SRGB
;
958 case PIPE_FORMAT_R8G8B8A8_UNORM
:
959 return PIPE_FORMAT_R8G8B8A8_SRGB
;
960 case PIPE_FORMAT_R8G8B8X8_UNORM
:
961 return PIPE_FORMAT_R8G8B8X8_SRGB
;
962 case PIPE_FORMAT_DXT1_RGB
:
963 return PIPE_FORMAT_DXT1_SRGB
;
964 case PIPE_FORMAT_DXT1_RGBA
:
965 return PIPE_FORMAT_DXT1_SRGBA
;
966 case PIPE_FORMAT_DXT3_RGBA
:
967 return PIPE_FORMAT_DXT3_SRGBA
;
968 case PIPE_FORMAT_DXT5_RGBA
:
969 return PIPE_FORMAT_DXT5_SRGBA
;
970 case PIPE_FORMAT_B5G6R5_UNORM
:
971 return PIPE_FORMAT_B5G6R5_SRGB
;
972 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
973 return PIPE_FORMAT_BPTC_SRGBA
;
974 case PIPE_FORMAT_ASTC_4x4
:
975 return PIPE_FORMAT_ASTC_4x4_SRGB
;
976 case PIPE_FORMAT_ASTC_5x4
:
977 return PIPE_FORMAT_ASTC_5x4_SRGB
;
978 case PIPE_FORMAT_ASTC_5x5
:
979 return PIPE_FORMAT_ASTC_5x5_SRGB
;
980 case PIPE_FORMAT_ASTC_6x5
:
981 return PIPE_FORMAT_ASTC_6x5_SRGB
;
982 case PIPE_FORMAT_ASTC_6x6
:
983 return PIPE_FORMAT_ASTC_6x6_SRGB
;
984 case PIPE_FORMAT_ASTC_8x5
:
985 return PIPE_FORMAT_ASTC_8x5_SRGB
;
986 case PIPE_FORMAT_ASTC_8x6
:
987 return PIPE_FORMAT_ASTC_8x6_SRGB
;
988 case PIPE_FORMAT_ASTC_8x8
:
989 return PIPE_FORMAT_ASTC_8x8_SRGB
;
990 case PIPE_FORMAT_ASTC_10x5
:
991 return PIPE_FORMAT_ASTC_10x5_SRGB
;
992 case PIPE_FORMAT_ASTC_10x6
:
993 return PIPE_FORMAT_ASTC_10x6_SRGB
;
994 case PIPE_FORMAT_ASTC_10x8
:
995 return PIPE_FORMAT_ASTC_10x8_SRGB
;
996 case PIPE_FORMAT_ASTC_10x10
:
997 return PIPE_FORMAT_ASTC_10x10_SRGB
;
998 case PIPE_FORMAT_ASTC_12x10
:
999 return PIPE_FORMAT_ASTC_12x10_SRGB
;
1000 case PIPE_FORMAT_ASTC_12x12
:
1001 return PIPE_FORMAT_ASTC_12x12_SRGB
;
1004 return PIPE_FORMAT_NONE
;
1009 * Given an sRGB format, return the corresponding linear colorspace format.
1010 * For non sRGB formats, return the format unchanged.
1012 static inline enum pipe_format
1013 util_format_linear(enum pipe_format format
)
1016 case PIPE_FORMAT_L8_SRGB
:
1017 return PIPE_FORMAT_L8_UNORM
;
1018 case PIPE_FORMAT_R8_SRGB
:
1019 return PIPE_FORMAT_R8_UNORM
;
1020 case PIPE_FORMAT_L8A8_SRGB
:
1021 return PIPE_FORMAT_L8A8_UNORM
;
1022 case PIPE_FORMAT_R8G8B8_SRGB
:
1023 return PIPE_FORMAT_R8G8B8_UNORM
;
1024 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1025 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1026 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1027 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1028 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1029 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1030 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1031 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1032 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1033 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1034 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1035 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1036 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1037 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1038 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1039 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1040 case PIPE_FORMAT_DXT1_SRGB
:
1041 return PIPE_FORMAT_DXT1_RGB
;
1042 case PIPE_FORMAT_DXT1_SRGBA
:
1043 return PIPE_FORMAT_DXT1_RGBA
;
1044 case PIPE_FORMAT_DXT3_SRGBA
:
1045 return PIPE_FORMAT_DXT3_RGBA
;
1046 case PIPE_FORMAT_DXT5_SRGBA
:
1047 return PIPE_FORMAT_DXT5_RGBA
;
1048 case PIPE_FORMAT_B5G6R5_SRGB
:
1049 return PIPE_FORMAT_B5G6R5_UNORM
;
1050 case PIPE_FORMAT_BPTC_SRGBA
:
1051 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1052 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1053 return PIPE_FORMAT_ASTC_4x4
;
1054 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1055 return PIPE_FORMAT_ASTC_5x4
;
1056 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1057 return PIPE_FORMAT_ASTC_5x5
;
1058 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1059 return PIPE_FORMAT_ASTC_6x5
;
1060 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1061 return PIPE_FORMAT_ASTC_6x6
;
1062 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1063 return PIPE_FORMAT_ASTC_8x5
;
1064 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1065 return PIPE_FORMAT_ASTC_8x6
;
1066 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1067 return PIPE_FORMAT_ASTC_8x8
;
1068 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1069 return PIPE_FORMAT_ASTC_10x5
;
1070 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1071 return PIPE_FORMAT_ASTC_10x6
;
1072 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1073 return PIPE_FORMAT_ASTC_10x8
;
1074 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1075 return PIPE_FORMAT_ASTC_10x10
;
1076 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1077 return PIPE_FORMAT_ASTC_12x10
;
1078 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1079 return PIPE_FORMAT_ASTC_12x12
;
1086 * Given a depth-stencil format, return the corresponding stencil-only format.
1087 * For stencil-only formats, return the format unchanged.
1089 static inline enum pipe_format
1090 util_format_stencil_only(enum pipe_format format
)
1093 /* mask out the depth component */
1094 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1095 return PIPE_FORMAT_X24S8_UINT
;
1096 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1097 return PIPE_FORMAT_S8X24_UINT
;
1098 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1099 return PIPE_FORMAT_X32_S8X24_UINT
;
1101 /* stencil only formats */
1102 case PIPE_FORMAT_X24S8_UINT
:
1103 case PIPE_FORMAT_S8X24_UINT
:
1104 case PIPE_FORMAT_X32_S8X24_UINT
:
1105 case PIPE_FORMAT_S8_UINT
:
1110 return PIPE_FORMAT_NONE
;
1115 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1116 * This is identity for non-intensity formats.
1118 static inline enum pipe_format
1119 util_format_intensity_to_red(enum pipe_format format
)
1122 case PIPE_FORMAT_I8_UNORM
:
1123 return PIPE_FORMAT_R8_UNORM
;
1124 case PIPE_FORMAT_I8_SNORM
:
1125 return PIPE_FORMAT_R8_SNORM
;
1126 case PIPE_FORMAT_I16_UNORM
:
1127 return PIPE_FORMAT_R16_UNORM
;
1128 case PIPE_FORMAT_I16_SNORM
:
1129 return PIPE_FORMAT_R16_SNORM
;
1130 case PIPE_FORMAT_I16_FLOAT
:
1131 return PIPE_FORMAT_R16_FLOAT
;
1132 case PIPE_FORMAT_I32_FLOAT
:
1133 return PIPE_FORMAT_R32_FLOAT
;
1134 case PIPE_FORMAT_I8_UINT
:
1135 return PIPE_FORMAT_R8_UINT
;
1136 case PIPE_FORMAT_I8_SINT
:
1137 return PIPE_FORMAT_R8_SINT
;
1138 case PIPE_FORMAT_I16_UINT
:
1139 return PIPE_FORMAT_R16_UINT
;
1140 case PIPE_FORMAT_I16_SINT
:
1141 return PIPE_FORMAT_R16_SINT
;
1142 case PIPE_FORMAT_I32_UINT
:
1143 return PIPE_FORMAT_R32_UINT
;
1144 case PIPE_FORMAT_I32_SINT
:
1145 return PIPE_FORMAT_R32_SINT
;
1147 assert(!util_format_is_intensity(format
));
1153 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1154 * This is identity for non-luminance formats.
1156 static inline enum pipe_format
1157 util_format_luminance_to_red(enum pipe_format format
)
1160 case PIPE_FORMAT_L8_UNORM
:
1161 return PIPE_FORMAT_R8_UNORM
;
1162 case PIPE_FORMAT_L8_SNORM
:
1163 return PIPE_FORMAT_R8_SNORM
;
1164 case PIPE_FORMAT_L16_UNORM
:
1165 return PIPE_FORMAT_R16_UNORM
;
1166 case PIPE_FORMAT_L16_SNORM
:
1167 return PIPE_FORMAT_R16_SNORM
;
1168 case PIPE_FORMAT_L16_FLOAT
:
1169 return PIPE_FORMAT_R16_FLOAT
;
1170 case PIPE_FORMAT_L32_FLOAT
:
1171 return PIPE_FORMAT_R32_FLOAT
;
1172 case PIPE_FORMAT_L8_UINT
:
1173 return PIPE_FORMAT_R8_UINT
;
1174 case PIPE_FORMAT_L8_SINT
:
1175 return PIPE_FORMAT_R8_SINT
;
1176 case PIPE_FORMAT_L16_UINT
:
1177 return PIPE_FORMAT_R16_UINT
;
1178 case PIPE_FORMAT_L16_SINT
:
1179 return PIPE_FORMAT_R16_SINT
;
1180 case PIPE_FORMAT_L32_UINT
:
1181 return PIPE_FORMAT_R32_UINT
;
1182 case PIPE_FORMAT_L32_SINT
:
1183 return PIPE_FORMAT_R32_SINT
;
1185 case PIPE_FORMAT_LATC1_UNORM
:
1186 return PIPE_FORMAT_RGTC1_UNORM
;
1187 case PIPE_FORMAT_LATC1_SNORM
:
1188 return PIPE_FORMAT_RGTC1_SNORM
;
1190 case PIPE_FORMAT_L4A4_UNORM
:
1191 return PIPE_FORMAT_R4A4_UNORM
;
1193 case PIPE_FORMAT_L8A8_UNORM
:
1194 return PIPE_FORMAT_R8A8_UNORM
;
1195 case PIPE_FORMAT_L8A8_SNORM
:
1196 return PIPE_FORMAT_R8A8_SNORM
;
1197 case PIPE_FORMAT_L16A16_UNORM
:
1198 return PIPE_FORMAT_R16A16_UNORM
;
1199 case PIPE_FORMAT_L16A16_SNORM
:
1200 return PIPE_FORMAT_R16A16_SNORM
;
1201 case PIPE_FORMAT_L16A16_FLOAT
:
1202 return PIPE_FORMAT_R16A16_FLOAT
;
1203 case PIPE_FORMAT_L32A32_FLOAT
:
1204 return PIPE_FORMAT_R32A32_FLOAT
;
1205 case PIPE_FORMAT_L8A8_UINT
:
1206 return PIPE_FORMAT_R8A8_UINT
;
1207 case PIPE_FORMAT_L8A8_SINT
:
1208 return PIPE_FORMAT_R8A8_SINT
;
1209 case PIPE_FORMAT_L16A16_UINT
:
1210 return PIPE_FORMAT_R16A16_UINT
;
1211 case PIPE_FORMAT_L16A16_SINT
:
1212 return PIPE_FORMAT_R16A16_SINT
;
1213 case PIPE_FORMAT_L32A32_UINT
:
1214 return PIPE_FORMAT_R32A32_UINT
;
1215 case PIPE_FORMAT_L32A32_SINT
:
1216 return PIPE_FORMAT_R32A32_SINT
;
1218 /* We don't have compressed red-alpha variants for these. */
1219 case PIPE_FORMAT_LATC2_UNORM
:
1220 case PIPE_FORMAT_LATC2_SNORM
:
1221 return PIPE_FORMAT_NONE
;
1224 assert(!util_format_is_luminance(format
) &&
1225 !util_format_is_luminance_alpha(format
));
1231 * Return the number of components stored.
1232 * Formats with block size != 1x1 will always have 1 component (the block).
1234 static inline unsigned
1235 util_format_get_nr_components(enum pipe_format format
)
1237 const struct util_format_description
*desc
= util_format_description(format
);
1238 return desc
->nr_channels
;
1242 * Return the index of the first non-void channel
1243 * -1 if no non-void channels
1246 util_format_get_first_non_void_channel(enum pipe_format format
)
1248 const struct util_format_description
*desc
= util_format_description(format
);
1251 for (i
= 0; i
< 4; i
++)
1252 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1262 * Whether this format is any 8-bit UNORM variant. Looser than
1263 * util_is_rgba8_variant (also includes alpha textures, for instance).
1267 util_format_is_unorm8(const struct util_format_description
*desc
)
1269 int c
= util_format_get_first_non_void_channel(desc
->format
);
1274 return desc
->is_unorm
&& desc
->is_array
&& desc
->channel
[c
].size
== 8;
1278 * Format access functions.
1282 util_format_read_4f(enum pipe_format format
,
1283 float *dst
, unsigned dst_stride
,
1284 const void *src
, unsigned src_stride
,
1285 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1288 util_format_write_4f(enum pipe_format format
,
1289 const float *src
, unsigned src_stride
,
1290 void *dst
, unsigned dst_stride
,
1291 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1294 util_format_read_4ub(enum pipe_format format
,
1295 uint8_t *dst
, unsigned dst_stride
,
1296 const void *src
, unsigned src_stride
,
1297 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1300 util_format_write_4ub(enum pipe_format format
,
1301 const uint8_t *src
, unsigned src_stride
,
1302 void *dst
, unsigned dst_stride
,
1303 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1306 util_format_read_4ui(enum pipe_format format
,
1307 unsigned *dst
, unsigned dst_stride
,
1308 const void *src
, unsigned src_stride
,
1309 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1312 util_format_write_4ui(enum pipe_format format
,
1313 const unsigned int *src
, unsigned src_stride
,
1314 void *dst
, unsigned dst_stride
,
1315 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1318 util_format_read_4i(enum pipe_format format
,
1319 int *dst
, unsigned dst_stride
,
1320 const void *src
, unsigned src_stride
,
1321 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1324 util_format_write_4i(enum pipe_format format
,
1325 const int *src
, unsigned src_stride
,
1326 void *dst
, unsigned dst_stride
,
1327 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1330 * Generic format conversion;
1334 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1337 util_format_translate(enum pipe_format dst_format
,
1338 void *dst
, unsigned dst_stride
,
1339 unsigned dst_x
, unsigned dst_y
,
1340 enum pipe_format src_format
,
1341 const void *src
, unsigned src_stride
,
1342 unsigned src_x
, unsigned src_y
,
1343 unsigned width
, unsigned height
);
1346 util_format_translate_3d(enum pipe_format dst_format
,
1347 void *dst
, unsigned dst_stride
,
1348 unsigned dst_slice_stride
,
1349 unsigned dst_x
, unsigned dst_y
,
1351 enum pipe_format src_format
,
1352 const void *src
, unsigned src_stride
,
1353 unsigned src_slice_stride
,
1354 unsigned src_x
, unsigned src_y
,
1355 unsigned src_z
, unsigned width
,
1356 unsigned height
, unsigned depth
);
1359 * Swizzle operations.
1362 /* Compose two sets of swizzles.
1363 * If V is a 4D vector and the function parameters represent functions that
1364 * swizzle vector components, this holds:
1365 * swz2(swz1(V)) = dst(V)
1367 void util_format_compose_swizzles(const unsigned char swz1
[4],
1368 const unsigned char swz2
[4],
1369 unsigned char dst
[4]);
1371 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1372 * to \param src and store the result in \param dst.
1373 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1375 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1376 const union pipe_color_union
*src
,
1377 const unsigned char swz
[4],
1378 const boolean is_integer
);
1380 void pipe_swizzle_4f(float *dst
, const float *src
,
1381 const unsigned char swz
[4]);
1383 void util_format_unswizzle_4f(float *dst
, const float *src
,
1384 const unsigned char swz
[4]);
1387 util_format_snorm8_to_sint8(enum pipe_format format
);
1393 #endif /* ! U_FORMAT_H */