1 /**************************************************************************
3 * Copyright 2009-2010 Vmware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
37 union pipe_color_union
;
46 * Describe how to pack/unpack pixels into/from the prescribed format.
48 * XXX: This could be renamed to something like util_format_pack, or broke down
49 * in flags inside util_format_block that said exactly what we want.
51 enum util_format_layout
{
53 * Formats with util_format_block::width == util_format_block::height == 1
54 * that can be described as an ordinary data structure.
56 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
59 * Formats with sub-sampled channels.
61 * This is for formats like YVYU where there is less than one sample per
64 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
67 * S3 Texture Compression formats.
69 UTIL_FORMAT_LAYOUT_S3TC
= 4,
72 * Red-Green Texture Compression formats.
74 UTIL_FORMAT_LAYOUT_RGTC
= 5,
77 * Ericsson Texture Compression
79 UTIL_FORMAT_LAYOUT_ETC
= 6,
82 * BC6/7 Texture Compression
84 UTIL_FORMAT_LAYOUT_BPTC
= 7,
89 UTIL_FORMAT_LAYOUT_ASTC
= 8,
92 * Everything else that doesn't fit in any of the above layouts.
94 UTIL_FORMAT_LAYOUT_OTHER
= 9
98 struct util_format_block
100 /** Block width in pixels */
103 /** Block height in pixels */
106 /** Block size in bits */
111 enum util_format_type
{
112 UTIL_FORMAT_TYPE_VOID
= 0,
113 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
114 UTIL_FORMAT_TYPE_SIGNED
= 2,
115 UTIL_FORMAT_TYPE_FIXED
= 3,
116 UTIL_FORMAT_TYPE_FLOAT
= 4
120 enum util_format_colorspace
{
121 UTIL_FORMAT_COLORSPACE_RGB
= 0,
122 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
123 UTIL_FORMAT_COLORSPACE_YUV
= 2,
124 UTIL_FORMAT_COLORSPACE_ZS
= 3
128 struct util_format_channel_description
130 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
131 unsigned normalized
:1;
132 unsigned pure_integer
:1;
133 unsigned size
:9; /**< bits per channel */
134 unsigned shift
:16; /** number of bits from lsb */
138 struct util_format_description
140 enum pipe_format format
;
145 * Short name, striped of the prefix, lower case.
147 const char *short_name
;
150 * Pixel block dimensions.
152 struct util_format_block block
;
154 enum util_format_layout layout
;
157 * The number of channels.
159 unsigned nr_channels
:3;
162 * Whether all channels have the same number of (whole) bytes and type.
167 * Whether the pixel format can be described as a bitfield structure.
170 * - pixel depth must be 8, 16, or 32 bits;
171 * - all channels must be unsigned, signed, or void
173 unsigned is_bitmask
:1;
176 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
181 * Input channel description, in the order XYZW.
183 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
185 * If each channel is accessed as an individual N-byte value, X is always
186 * at the lowest address in memory, Y is always next, and so on. For all
187 * currently-defined formats, the N-byte value has native endianness.
189 * If instead a group of channels is accessed as a single N-byte value,
190 * the order of the channels within that value depends on endianness.
191 * For big-endian targets, X is the most significant subvalue,
192 * otherwise it is the least significant one.
194 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
197 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
198 * big-endian: X Yu Ym Yl
200 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
204 * little-endian: YYYXXXXX WZZZZZYY
205 * big-endian: XXXXXYYY YYZZZZZW
207 struct util_format_channel_description channel
[4];
210 * Output channel swizzle.
212 * The order is either:
216 * depending on the colorspace.
218 unsigned char swizzle
[4];
221 * Colorspace transformation.
223 enum util_format_colorspace colorspace
;
226 * Unpack pixel blocks to R8G8B8A8_UNORM.
227 * Note: strides are in bytes.
229 * Only defined for non-depth-stencil formats.
232 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
233 const uint8_t *src
, unsigned src_stride
,
234 unsigned width
, unsigned height
);
237 * Pack pixel blocks from R8G8B8A8_UNORM.
238 * Note: strides are in bytes.
240 * Only defined for non-depth-stencil formats.
243 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
244 const uint8_t *src
, unsigned src_stride
,
245 unsigned width
, unsigned height
);
248 * Fetch a single pixel (i, j) from a block.
250 * XXX: Only defined for a very few select formats.
253 (*fetch_rgba_8unorm
)(uint8_t *dst
,
255 unsigned i
, unsigned j
);
258 * Unpack pixel blocks to R32G32B32A32_FLOAT.
259 * Note: strides are in bytes.
261 * Only defined for non-depth-stencil formats.
264 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
265 const uint8_t *src
, unsigned src_stride
,
266 unsigned width
, unsigned height
);
269 * Pack pixel blocks from R32G32B32A32_FLOAT.
270 * Note: strides are in bytes.
272 * Only defined for non-depth-stencil formats.
275 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
276 const float *src
, unsigned src_stride
,
277 unsigned width
, unsigned height
);
280 * Fetch a single pixel (i, j) from a block.
282 * Only defined for non-depth-stencil and non-integer formats.
285 (*fetch_rgba_float
)(float *dst
,
287 unsigned i
, unsigned j
);
290 * Unpack pixels to Z32_UNORM.
291 * Note: strides are in bytes.
293 * Only defined for depth formats.
296 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
297 const uint8_t *src
, unsigned src_stride
,
298 unsigned width
, unsigned height
);
301 * Pack pixels from Z32_FLOAT.
302 * Note: strides are in bytes.
304 * Only defined for depth formats.
307 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
308 const uint32_t *src
, unsigned src_stride
,
309 unsigned width
, unsigned height
);
312 * Unpack pixels to Z32_FLOAT.
313 * Note: strides are in bytes.
315 * Only defined for depth formats.
318 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
319 const uint8_t *src
, unsigned src_stride
,
320 unsigned width
, unsigned height
);
323 * Pack pixels from Z32_FLOAT.
324 * Note: strides are in bytes.
326 * Only defined for depth formats.
329 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
330 const float *src
, unsigned src_stride
,
331 unsigned width
, unsigned height
);
334 * Unpack pixels to S8_UINT.
335 * Note: strides are in bytes.
337 * Only defined for stencil formats.
340 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
341 const uint8_t *src
, unsigned src_stride
,
342 unsigned width
, unsigned height
);
345 * Pack pixels from S8_UINT.
346 * Note: strides are in bytes.
348 * Only defined for stencil formats.
351 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
352 const uint8_t *src
, unsigned src_stride
,
353 unsigned width
, unsigned height
);
356 * Unpack pixel blocks to R32G32B32A32_UINT.
357 * Note: strides are in bytes.
359 * Only defined for INT formats.
362 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
363 const uint8_t *src
, unsigned src_stride
,
364 unsigned width
, unsigned height
);
367 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
368 const uint32_t *src
, unsigned src_stride
,
369 unsigned width
, unsigned height
);
372 * Unpack pixel blocks to R32G32B32A32_SINT.
373 * Note: strides are in bytes.
375 * Only defined for INT formats.
378 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
379 const uint8_t *src
, unsigned src_stride
,
380 unsigned width
, unsigned height
);
383 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
384 const int32_t *src
, unsigned src_stride
,
385 unsigned width
, unsigned height
);
388 * Fetch a single pixel (i, j) from a block.
390 * Only defined for unsigned (pure) integer formats.
393 (*fetch_rgba_uint
)(uint32_t *dst
,
395 unsigned i
, unsigned j
);
398 * Fetch a single pixel (i, j) from a block.
400 * Only defined for signed (pure) integer formats.
403 (*fetch_rgba_sint
)(int32_t *dst
,
405 unsigned i
, unsigned j
);
409 extern const struct util_format_description
410 util_format_description_table
[];
413 const struct util_format_description
*
414 util_format_description(enum pipe_format format
);
418 * Format query functions.
421 static inline const char *
422 util_format_name(enum pipe_format format
)
424 const struct util_format_description
*desc
= util_format_description(format
);
428 return "PIPE_FORMAT_???";
434 static inline const char *
435 util_format_short_name(enum pipe_format format
)
437 const struct util_format_description
*desc
= util_format_description(format
);
444 return desc
->short_name
;
448 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
450 static inline boolean
451 util_format_is_plain(enum pipe_format format
)
453 const struct util_format_description
*desc
= util_format_description(format
);
459 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
462 static inline boolean
463 util_format_is_compressed(enum pipe_format format
)
465 const struct util_format_description
*desc
= util_format_description(format
);
472 switch (desc
->layout
) {
473 case UTIL_FORMAT_LAYOUT_S3TC
:
474 case UTIL_FORMAT_LAYOUT_RGTC
:
475 case UTIL_FORMAT_LAYOUT_ETC
:
476 case UTIL_FORMAT_LAYOUT_BPTC
:
477 case UTIL_FORMAT_LAYOUT_ASTC
:
478 /* XXX add other formats in the future */
485 static inline boolean
486 util_format_is_s3tc(enum pipe_format format
)
488 const struct util_format_description
*desc
= util_format_description(format
);
495 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
498 static inline boolean
499 util_format_is_etc(enum pipe_format format
)
501 const struct util_format_description
*desc
= util_format_description(format
);
508 return desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
? TRUE
: FALSE
;
511 static inline boolean
512 util_format_is_srgb(enum pipe_format format
)
514 const struct util_format_description
*desc
= util_format_description(format
);
515 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
518 static inline boolean
519 util_format_has_depth(const struct util_format_description
*desc
)
521 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
522 desc
->swizzle
[0] != PIPE_SWIZZLE_NONE
;
525 static inline boolean
526 util_format_has_stencil(const struct util_format_description
*desc
)
528 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
529 desc
->swizzle
[1] != PIPE_SWIZZLE_NONE
;
532 static inline boolean
533 util_format_is_depth_or_stencil(enum pipe_format format
)
535 const struct util_format_description
*desc
= util_format_description(format
);
542 return util_format_has_depth(desc
) ||
543 util_format_has_stencil(desc
);
546 static inline boolean
547 util_format_is_depth_and_stencil(enum pipe_format format
)
549 const struct util_format_description
*desc
= util_format_description(format
);
556 return util_format_has_depth(desc
) &&
557 util_format_has_stencil(desc
);
561 * For depth-stencil formats, return the equivalent depth-only format.
563 static inline boolean
564 util_format_get_depth_only(enum pipe_format format
)
567 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
568 return PIPE_FORMAT_Z24X8_UNORM
;
570 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
571 return PIPE_FORMAT_X8Z24_UNORM
;
573 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
574 return PIPE_FORMAT_Z32_FLOAT
;
581 static inline boolean
582 util_format_is_yuv(enum pipe_format format
)
584 const struct util_format_description
*desc
= util_format_description(format
);
591 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_YUV
;
595 * Calculates the depth format type based upon the incoming format description.
597 static inline unsigned
598 util_get_depth_format_type(const struct util_format_description
*desc
)
600 unsigned depth_channel
= desc
->swizzle
[0];
601 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
602 depth_channel
!= PIPE_SWIZZLE_NONE
) {
603 return desc
->channel
[depth_channel
].type
;
605 return UTIL_FORMAT_TYPE_VOID
;
611 * Calculates the MRD for the depth format. MRD is used in depth bias
612 * for UNORM and unbound depth buffers. When the depth buffer is floating
613 * point, the depth bias calculation does not use the MRD. However, the
614 * default MRD will be 1.0 / ((1 << 24) - 1).
617 util_get_depth_format_mrd(const struct util_format_description
*desc
);
621 * Return whether this is an RGBA, Z, S, or combined ZS format.
622 * Useful for initializing pipe_blit_info::mask.
624 static inline unsigned
625 util_format_get_mask(enum pipe_format format
)
627 const struct util_format_description
*desc
=
628 util_format_description(format
);
633 if (util_format_has_depth(desc
)) {
634 if (util_format_has_stencil(desc
)) {
640 if (util_format_has_stencil(desc
)) {
643 return PIPE_MASK_RGBA
;
649 * Give the RGBA colormask of the channels that can be represented in this
652 * That is, the channels whose values are preserved.
654 static inline unsigned
655 util_format_colormask(const struct util_format_description
*desc
)
660 switch (desc
->colorspace
) {
661 case UTIL_FORMAT_COLORSPACE_RGB
:
662 case UTIL_FORMAT_COLORSPACE_SRGB
:
663 case UTIL_FORMAT_COLORSPACE_YUV
:
665 for (chan
= 0; chan
< 4; ++chan
) {
666 if (desc
->swizzle
[chan
] < 4) {
667 colormask
|= (1 << chan
);
671 case UTIL_FORMAT_COLORSPACE_ZS
:
681 * Checks if color mask covers every channel for the specified format
683 * @param desc a format description to check colormask with
684 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
686 static inline boolean
687 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
689 return (~colormask
& util_format_colormask(desc
)) == 0;
694 util_format_is_float(enum pipe_format format
);
698 util_format_has_alpha(enum pipe_format format
);
702 util_format_is_luminance(enum pipe_format format
);
705 util_format_is_alpha(enum pipe_format format
);
708 util_format_is_luminance_alpha(enum pipe_format format
);
712 util_format_is_intensity(enum pipe_format format
);
715 util_format_is_subsampled_422(enum pipe_format format
);
718 util_format_is_pure_integer(enum pipe_format format
);
721 util_format_is_pure_sint(enum pipe_format format
);
724 util_format_is_pure_uint(enum pipe_format format
);
727 util_format_is_snorm(enum pipe_format format
);
730 util_format_is_snorm8(enum pipe_format format
);
733 * Check if the src format can be blitted to the destination format with
734 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
738 util_is_format_compatible(const struct util_format_description
*src_desc
,
739 const struct util_format_description
*dst_desc
);
742 * Whether this format is a rgab8 variant.
744 * That is, any format that matches the
746 * PIPE_FORMAT_?8?8?8?8_UNORM
748 static inline boolean
749 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
753 if(desc
->block
.width
!= 1 ||
754 desc
->block
.height
!= 1 ||
755 desc
->block
.bits
!= 32)
758 for(chan
= 0; chan
< 4; ++chan
) {
759 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
760 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
762 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
763 !desc
->channel
[chan
].normalized
)
765 if(desc
->channel
[chan
].size
!= 8)
774 * Return total bits needed for the pixel format per block.
777 util_format_get_blocksizebits(enum pipe_format format
)
779 const struct util_format_description
*desc
= util_format_description(format
);
786 return desc
->block
.bits
;
790 * Return bytes per block (not pixel) for the given format.
793 util_format_get_blocksize(enum pipe_format format
)
795 uint bits
= util_format_get_blocksizebits(format
);
796 uint bytes
= bits
/ 8;
798 assert(bits
% 8 == 0);
808 util_format_get_blockwidth(enum pipe_format format
)
810 const struct util_format_description
*desc
= util_format_description(format
);
817 return desc
->block
.width
;
821 util_format_get_blockheight(enum pipe_format format
)
823 const struct util_format_description
*desc
= util_format_description(format
);
830 return desc
->block
.height
;
833 static inline unsigned
834 util_format_get_nblocksx(enum pipe_format format
,
837 unsigned blockwidth
= util_format_get_blockwidth(format
);
838 return (x
+ blockwidth
- 1) / blockwidth
;
841 static inline unsigned
842 util_format_get_nblocksy(enum pipe_format format
,
845 unsigned blockheight
= util_format_get_blockheight(format
);
846 return (y
+ blockheight
- 1) / blockheight
;
849 static inline unsigned
850 util_format_get_nblocks(enum pipe_format format
,
854 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
858 util_format_get_stride(enum pipe_format format
,
861 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
865 util_format_get_2d_size(enum pipe_format format
,
869 return util_format_get_nblocksy(format
, height
) * stride
;
873 util_format_get_component_bits(enum pipe_format format
,
874 enum util_format_colorspace colorspace
,
877 const struct util_format_description
*desc
= util_format_description(format
);
878 enum util_format_colorspace desc_colorspace
;
885 assert(component
< 4);
887 /* Treat RGB and SRGB as equivalent. */
888 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
889 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
891 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
892 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
894 desc_colorspace
= desc
->colorspace
;
897 if (desc_colorspace
!= colorspace
) {
901 switch (desc
->swizzle
[component
]) {
903 return desc
->channel
[0].size
;
905 return desc
->channel
[1].size
;
907 return desc
->channel
[2].size
;
909 return desc
->channel
[3].size
;
916 * Given a linear RGB colorspace format, return the corresponding SRGB
917 * format, or PIPE_FORMAT_NONE if none.
919 static inline enum pipe_format
920 util_format_srgb(enum pipe_format format
)
922 if (util_format_is_srgb(format
))
926 case PIPE_FORMAT_L8_UNORM
:
927 return PIPE_FORMAT_L8_SRGB
;
928 case PIPE_FORMAT_L8A8_UNORM
:
929 return PIPE_FORMAT_L8A8_SRGB
;
930 case PIPE_FORMAT_R8G8B8_UNORM
:
931 return PIPE_FORMAT_R8G8B8_SRGB
;
932 case PIPE_FORMAT_A8B8G8R8_UNORM
:
933 return PIPE_FORMAT_A8B8G8R8_SRGB
;
934 case PIPE_FORMAT_X8B8G8R8_UNORM
:
935 return PIPE_FORMAT_X8B8G8R8_SRGB
;
936 case PIPE_FORMAT_B8G8R8A8_UNORM
:
937 return PIPE_FORMAT_B8G8R8A8_SRGB
;
938 case PIPE_FORMAT_B8G8R8X8_UNORM
:
939 return PIPE_FORMAT_B8G8R8X8_SRGB
;
940 case PIPE_FORMAT_A8R8G8B8_UNORM
:
941 return PIPE_FORMAT_A8R8G8B8_SRGB
;
942 case PIPE_FORMAT_X8R8G8B8_UNORM
:
943 return PIPE_FORMAT_X8R8G8B8_SRGB
;
944 case PIPE_FORMAT_R8G8B8A8_UNORM
:
945 return PIPE_FORMAT_R8G8B8A8_SRGB
;
946 case PIPE_FORMAT_R8G8B8X8_UNORM
:
947 return PIPE_FORMAT_R8G8B8X8_SRGB
;
948 case PIPE_FORMAT_DXT1_RGB
:
949 return PIPE_FORMAT_DXT1_SRGB
;
950 case PIPE_FORMAT_DXT1_RGBA
:
951 return PIPE_FORMAT_DXT1_SRGBA
;
952 case PIPE_FORMAT_DXT3_RGBA
:
953 return PIPE_FORMAT_DXT3_SRGBA
;
954 case PIPE_FORMAT_DXT5_RGBA
:
955 return PIPE_FORMAT_DXT5_SRGBA
;
956 case PIPE_FORMAT_B5G6R5_UNORM
:
957 return PIPE_FORMAT_B5G6R5_SRGB
;
958 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
959 return PIPE_FORMAT_BPTC_SRGBA
;
960 case PIPE_FORMAT_ASTC_4x4
:
961 return PIPE_FORMAT_ASTC_4x4_SRGB
;
962 case PIPE_FORMAT_ASTC_5x4
:
963 return PIPE_FORMAT_ASTC_5x4_SRGB
;
964 case PIPE_FORMAT_ASTC_5x5
:
965 return PIPE_FORMAT_ASTC_5x5_SRGB
;
966 case PIPE_FORMAT_ASTC_6x5
:
967 return PIPE_FORMAT_ASTC_6x5_SRGB
;
968 case PIPE_FORMAT_ASTC_6x6
:
969 return PIPE_FORMAT_ASTC_6x6_SRGB
;
970 case PIPE_FORMAT_ASTC_8x5
:
971 return PIPE_FORMAT_ASTC_8x5_SRGB
;
972 case PIPE_FORMAT_ASTC_8x6
:
973 return PIPE_FORMAT_ASTC_8x6_SRGB
;
974 case PIPE_FORMAT_ASTC_8x8
:
975 return PIPE_FORMAT_ASTC_8x8_SRGB
;
976 case PIPE_FORMAT_ASTC_10x5
:
977 return PIPE_FORMAT_ASTC_10x5_SRGB
;
978 case PIPE_FORMAT_ASTC_10x6
:
979 return PIPE_FORMAT_ASTC_10x6_SRGB
;
980 case PIPE_FORMAT_ASTC_10x8
:
981 return PIPE_FORMAT_ASTC_10x8_SRGB
;
982 case PIPE_FORMAT_ASTC_10x10
:
983 return PIPE_FORMAT_ASTC_10x10_SRGB
;
984 case PIPE_FORMAT_ASTC_12x10
:
985 return PIPE_FORMAT_ASTC_12x10_SRGB
;
986 case PIPE_FORMAT_ASTC_12x12
:
987 return PIPE_FORMAT_ASTC_12x12_SRGB
;
990 return PIPE_FORMAT_NONE
;
995 * Given an sRGB format, return the corresponding linear colorspace format.
996 * For non sRGB formats, return the format unchanged.
998 static inline enum pipe_format
999 util_format_linear(enum pipe_format format
)
1002 case PIPE_FORMAT_L8_SRGB
:
1003 return PIPE_FORMAT_L8_UNORM
;
1004 case PIPE_FORMAT_L8A8_SRGB
:
1005 return PIPE_FORMAT_L8A8_UNORM
;
1006 case PIPE_FORMAT_R8G8B8_SRGB
:
1007 return PIPE_FORMAT_R8G8B8_UNORM
;
1008 case PIPE_FORMAT_A8B8G8R8_SRGB
:
1009 return PIPE_FORMAT_A8B8G8R8_UNORM
;
1010 case PIPE_FORMAT_X8B8G8R8_SRGB
:
1011 return PIPE_FORMAT_X8B8G8R8_UNORM
;
1012 case PIPE_FORMAT_B8G8R8A8_SRGB
:
1013 return PIPE_FORMAT_B8G8R8A8_UNORM
;
1014 case PIPE_FORMAT_B8G8R8X8_SRGB
:
1015 return PIPE_FORMAT_B8G8R8X8_UNORM
;
1016 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1017 return PIPE_FORMAT_A8R8G8B8_UNORM
;
1018 case PIPE_FORMAT_X8R8G8B8_SRGB
:
1019 return PIPE_FORMAT_X8R8G8B8_UNORM
;
1020 case PIPE_FORMAT_R8G8B8A8_SRGB
:
1021 return PIPE_FORMAT_R8G8B8A8_UNORM
;
1022 case PIPE_FORMAT_R8G8B8X8_SRGB
:
1023 return PIPE_FORMAT_R8G8B8X8_UNORM
;
1024 case PIPE_FORMAT_DXT1_SRGB
:
1025 return PIPE_FORMAT_DXT1_RGB
;
1026 case PIPE_FORMAT_DXT1_SRGBA
:
1027 return PIPE_FORMAT_DXT1_RGBA
;
1028 case PIPE_FORMAT_DXT3_SRGBA
:
1029 return PIPE_FORMAT_DXT3_RGBA
;
1030 case PIPE_FORMAT_DXT5_SRGBA
:
1031 return PIPE_FORMAT_DXT5_RGBA
;
1032 case PIPE_FORMAT_B5G6R5_SRGB
:
1033 return PIPE_FORMAT_B5G6R5_UNORM
;
1034 case PIPE_FORMAT_BPTC_SRGBA
:
1035 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1036 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1037 return PIPE_FORMAT_ASTC_4x4
;
1038 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1039 return PIPE_FORMAT_ASTC_5x4
;
1040 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1041 return PIPE_FORMAT_ASTC_5x5
;
1042 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1043 return PIPE_FORMAT_ASTC_6x5
;
1044 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1045 return PIPE_FORMAT_ASTC_6x6
;
1046 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1047 return PIPE_FORMAT_ASTC_8x5
;
1048 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1049 return PIPE_FORMAT_ASTC_8x6
;
1050 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1051 return PIPE_FORMAT_ASTC_8x8
;
1052 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1053 return PIPE_FORMAT_ASTC_10x5
;
1054 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1055 return PIPE_FORMAT_ASTC_10x6
;
1056 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1057 return PIPE_FORMAT_ASTC_10x8
;
1058 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1059 return PIPE_FORMAT_ASTC_10x10
;
1060 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1061 return PIPE_FORMAT_ASTC_12x10
;
1062 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1063 return PIPE_FORMAT_ASTC_12x12
;
1070 * Given a depth-stencil format, return the corresponding stencil-only format.
1071 * For stencil-only formats, return the format unchanged.
1073 static inline enum pipe_format
1074 util_format_stencil_only(enum pipe_format format
)
1077 /* mask out the depth component */
1078 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1079 return PIPE_FORMAT_X24S8_UINT
;
1080 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1081 return PIPE_FORMAT_S8X24_UINT
;
1082 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1083 return PIPE_FORMAT_X32_S8X24_UINT
;
1085 /* stencil only formats */
1086 case PIPE_FORMAT_X24S8_UINT
:
1087 case PIPE_FORMAT_S8X24_UINT
:
1088 case PIPE_FORMAT_X32_S8X24_UINT
:
1089 case PIPE_FORMAT_S8_UINT
:
1094 return PIPE_FORMAT_NONE
;
1099 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1100 * This is identity for non-intensity formats.
1102 static inline enum pipe_format
1103 util_format_intensity_to_red(enum pipe_format format
)
1106 case PIPE_FORMAT_I8_UNORM
:
1107 return PIPE_FORMAT_R8_UNORM
;
1108 case PIPE_FORMAT_I8_SNORM
:
1109 return PIPE_FORMAT_R8_SNORM
;
1110 case PIPE_FORMAT_I16_UNORM
:
1111 return PIPE_FORMAT_R16_UNORM
;
1112 case PIPE_FORMAT_I16_SNORM
:
1113 return PIPE_FORMAT_R16_SNORM
;
1114 case PIPE_FORMAT_I16_FLOAT
:
1115 return PIPE_FORMAT_R16_FLOAT
;
1116 case PIPE_FORMAT_I32_FLOAT
:
1117 return PIPE_FORMAT_R32_FLOAT
;
1118 case PIPE_FORMAT_I8_UINT
:
1119 return PIPE_FORMAT_R8_UINT
;
1120 case PIPE_FORMAT_I8_SINT
:
1121 return PIPE_FORMAT_R8_SINT
;
1122 case PIPE_FORMAT_I16_UINT
:
1123 return PIPE_FORMAT_R16_UINT
;
1124 case PIPE_FORMAT_I16_SINT
:
1125 return PIPE_FORMAT_R16_SINT
;
1126 case PIPE_FORMAT_I32_UINT
:
1127 return PIPE_FORMAT_R32_UINT
;
1128 case PIPE_FORMAT_I32_SINT
:
1129 return PIPE_FORMAT_R32_SINT
;
1131 assert(!util_format_is_intensity(format
));
1137 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1138 * This is identity for non-luminance formats.
1140 static inline enum pipe_format
1141 util_format_luminance_to_red(enum pipe_format format
)
1144 case PIPE_FORMAT_L8_UNORM
:
1145 return PIPE_FORMAT_R8_UNORM
;
1146 case PIPE_FORMAT_L8_SNORM
:
1147 return PIPE_FORMAT_R8_SNORM
;
1148 case PIPE_FORMAT_L16_UNORM
:
1149 return PIPE_FORMAT_R16_UNORM
;
1150 case PIPE_FORMAT_L16_SNORM
:
1151 return PIPE_FORMAT_R16_SNORM
;
1152 case PIPE_FORMAT_L16_FLOAT
:
1153 return PIPE_FORMAT_R16_FLOAT
;
1154 case PIPE_FORMAT_L32_FLOAT
:
1155 return PIPE_FORMAT_R32_FLOAT
;
1156 case PIPE_FORMAT_L8_UINT
:
1157 return PIPE_FORMAT_R8_UINT
;
1158 case PIPE_FORMAT_L8_SINT
:
1159 return PIPE_FORMAT_R8_SINT
;
1160 case PIPE_FORMAT_L16_UINT
:
1161 return PIPE_FORMAT_R16_UINT
;
1162 case PIPE_FORMAT_L16_SINT
:
1163 return PIPE_FORMAT_R16_SINT
;
1164 case PIPE_FORMAT_L32_UINT
:
1165 return PIPE_FORMAT_R32_UINT
;
1166 case PIPE_FORMAT_L32_SINT
:
1167 return PIPE_FORMAT_R32_SINT
;
1169 case PIPE_FORMAT_LATC1_UNORM
:
1170 return PIPE_FORMAT_RGTC1_UNORM
;
1171 case PIPE_FORMAT_LATC1_SNORM
:
1172 return PIPE_FORMAT_RGTC1_SNORM
;
1174 case PIPE_FORMAT_L4A4_UNORM
:
1175 return PIPE_FORMAT_R4A4_UNORM
;
1177 case PIPE_FORMAT_L8A8_UNORM
:
1178 return PIPE_FORMAT_R8A8_UNORM
;
1179 case PIPE_FORMAT_L8A8_SNORM
:
1180 return PIPE_FORMAT_R8A8_SNORM
;
1181 case PIPE_FORMAT_L16A16_UNORM
:
1182 return PIPE_FORMAT_R16A16_UNORM
;
1183 case PIPE_FORMAT_L16A16_SNORM
:
1184 return PIPE_FORMAT_R16A16_SNORM
;
1185 case PIPE_FORMAT_L16A16_FLOAT
:
1186 return PIPE_FORMAT_R16A16_FLOAT
;
1187 case PIPE_FORMAT_L32A32_FLOAT
:
1188 return PIPE_FORMAT_R32A32_FLOAT
;
1189 case PIPE_FORMAT_L8A8_UINT
:
1190 return PIPE_FORMAT_R8A8_UINT
;
1191 case PIPE_FORMAT_L8A8_SINT
:
1192 return PIPE_FORMAT_R8A8_SINT
;
1193 case PIPE_FORMAT_L16A16_UINT
:
1194 return PIPE_FORMAT_R16A16_UINT
;
1195 case PIPE_FORMAT_L16A16_SINT
:
1196 return PIPE_FORMAT_R16A16_SINT
;
1197 case PIPE_FORMAT_L32A32_UINT
:
1198 return PIPE_FORMAT_R32A32_UINT
;
1199 case PIPE_FORMAT_L32A32_SINT
:
1200 return PIPE_FORMAT_R32A32_SINT
;
1202 /* We don't have compressed red-alpha variants for these. */
1203 case PIPE_FORMAT_LATC2_UNORM
:
1204 case PIPE_FORMAT_LATC2_SNORM
:
1205 return PIPE_FORMAT_NONE
;
1208 assert(!util_format_is_luminance(format
) &&
1209 !util_format_is_luminance_alpha(format
));
1215 * Return the number of components stored.
1216 * Formats with block size != 1x1 will always have 1 component (the block).
1218 static inline unsigned
1219 util_format_get_nr_components(enum pipe_format format
)
1221 const struct util_format_description
*desc
= util_format_description(format
);
1222 return desc
->nr_channels
;
1226 * Return the index of the first non-void channel
1227 * -1 if no non-void channels
1230 util_format_get_first_non_void_channel(enum pipe_format format
)
1232 const struct util_format_description
*desc
= util_format_description(format
);
1235 for (i
= 0; i
< 4; i
++)
1236 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1246 * Format access functions.
1250 util_format_read_4f(enum pipe_format format
,
1251 float *dst
, unsigned dst_stride
,
1252 const void *src
, unsigned src_stride
,
1253 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1256 util_format_write_4f(enum pipe_format format
,
1257 const float *src
, unsigned src_stride
,
1258 void *dst
, unsigned dst_stride
,
1259 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1262 util_format_read_4ub(enum pipe_format format
,
1263 uint8_t *dst
, unsigned dst_stride
,
1264 const void *src
, unsigned src_stride
,
1265 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1268 util_format_write_4ub(enum pipe_format format
,
1269 const uint8_t *src
, unsigned src_stride
,
1270 void *dst
, unsigned dst_stride
,
1271 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1274 util_format_read_4ui(enum pipe_format format
,
1275 unsigned *dst
, unsigned dst_stride
,
1276 const void *src
, unsigned src_stride
,
1277 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1280 util_format_write_4ui(enum pipe_format format
,
1281 const unsigned int *src
, unsigned src_stride
,
1282 void *dst
, unsigned dst_stride
,
1283 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1286 util_format_read_4i(enum pipe_format format
,
1287 int *dst
, unsigned dst_stride
,
1288 const void *src
, unsigned src_stride
,
1289 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1292 util_format_write_4i(enum pipe_format format
,
1293 const int *src
, unsigned src_stride
,
1294 void *dst
, unsigned dst_stride
,
1295 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1298 * Generic format conversion;
1302 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1305 util_format_translate(enum pipe_format dst_format
,
1306 void *dst
, unsigned dst_stride
,
1307 unsigned dst_x
, unsigned dst_y
,
1308 enum pipe_format src_format
,
1309 const void *src
, unsigned src_stride
,
1310 unsigned src_x
, unsigned src_y
,
1311 unsigned width
, unsigned height
);
1314 util_format_translate_3d(enum pipe_format dst_format
,
1315 void *dst
, unsigned dst_stride
,
1316 unsigned dst_slice_stride
,
1317 unsigned dst_x
, unsigned dst_y
,
1319 enum pipe_format src_format
,
1320 const void *src
, unsigned src_stride
,
1321 unsigned src_slice_stride
,
1322 unsigned src_x
, unsigned src_y
,
1323 unsigned src_z
, unsigned width
,
1324 unsigned height
, unsigned depth
);
1327 * Swizzle operations.
1330 /* Compose two sets of swizzles.
1331 * If V is a 4D vector and the function parameters represent functions that
1332 * swizzle vector components, this holds:
1333 * swz2(swz1(V)) = dst(V)
1335 void util_format_compose_swizzles(const unsigned char swz1
[4],
1336 const unsigned char swz2
[4],
1337 unsigned char dst
[4]);
1339 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1340 * to \param src and store the result in \param dst.
1341 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1343 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1344 const union pipe_color_union
*src
,
1345 const unsigned char swz
[4],
1346 const boolean is_integer
);
1348 void pipe_swizzle_4f(float *dst
, const float *src
,
1349 const unsigned char swz
[4]);
1351 void util_format_unswizzle_4f(float *dst
, const float *src
,
1352 const unsigned char swz
[4]);
1358 #endif /* ! U_FORMAT_H */