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_swizzle
{
121 UTIL_FORMAT_SWIZZLE_X
= 0,
122 UTIL_FORMAT_SWIZZLE_Y
= 1,
123 UTIL_FORMAT_SWIZZLE_Z
= 2,
124 UTIL_FORMAT_SWIZZLE_W
= 3,
125 UTIL_FORMAT_SWIZZLE_0
= 4,
126 UTIL_FORMAT_SWIZZLE_1
= 5,
127 UTIL_FORMAT_SWIZZLE_NONE
= 6,
128 UTIL_FORMAT_SWIZZLE_MAX
= 7 /**< Number of enums counter (must be last) */
132 enum util_format_colorspace
{
133 UTIL_FORMAT_COLORSPACE_RGB
= 0,
134 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
135 UTIL_FORMAT_COLORSPACE_YUV
= 2,
136 UTIL_FORMAT_COLORSPACE_ZS
= 3
140 struct util_format_channel_description
142 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
143 unsigned normalized
:1;
144 unsigned pure_integer
:1;
145 unsigned size
:9; /**< bits per channel */
146 unsigned shift
:16; /** number of bits from lsb */
150 struct util_format_description
152 enum pipe_format format
;
157 * Short name, striped of the prefix, lower case.
159 const char *short_name
;
162 * Pixel block dimensions.
164 struct util_format_block block
;
166 enum util_format_layout layout
;
169 * The number of channels.
171 unsigned nr_channels
:3;
174 * Whether all channels have the same number of (whole) bytes and type.
179 * Whether the pixel format can be described as a bitfield structure.
182 * - pixel depth must be 8, 16, or 32 bits;
183 * - all channels must be unsigned, signed, or void
185 unsigned is_bitmask
:1;
188 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
193 * Input channel description, in the order XYZW.
195 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
197 * If each channel is accessed as an individual N-byte value, X is always
198 * at the lowest address in memory, Y is always next, and so on. For all
199 * currently-defined formats, the N-byte value has native endianness.
201 * If instead a group of channels is accessed as a single N-byte value,
202 * the order of the channels within that value depends on endianness.
203 * For big-endian targets, X is the most significant subvalue,
204 * otherwise it is the least significant one.
206 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
209 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
210 * big-endian: X Yu Ym Yl
212 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
216 * little-endian: YYYXXXXX WZZZZZYY
217 * big-endian: XXXXXYYY YYZZZZZW
219 struct util_format_channel_description channel
[4];
222 * Output channel swizzle.
224 * The order is either:
228 * depending on the colorspace.
230 unsigned char swizzle
[4];
233 * Colorspace transformation.
235 enum util_format_colorspace colorspace
;
238 * Unpack pixel blocks to R8G8B8A8_UNORM.
239 * Note: strides are in bytes.
241 * Only defined for non-depth-stencil formats.
244 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
245 const uint8_t *src
, unsigned src_stride
,
246 unsigned width
, unsigned height
);
249 * Pack pixel blocks from R8G8B8A8_UNORM.
250 * Note: strides are in bytes.
252 * Only defined for non-depth-stencil formats.
255 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
256 const uint8_t *src
, unsigned src_stride
,
257 unsigned width
, unsigned height
);
260 * Fetch a single pixel (i, j) from a block.
262 * XXX: Only defined for a very few select formats.
265 (*fetch_rgba_8unorm
)(uint8_t *dst
,
267 unsigned i
, unsigned j
);
270 * Unpack pixel blocks to R32G32B32A32_FLOAT.
271 * Note: strides are in bytes.
273 * Only defined for non-depth-stencil formats.
276 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
277 const uint8_t *src
, unsigned src_stride
,
278 unsigned width
, unsigned height
);
281 * Pack pixel blocks from R32G32B32A32_FLOAT.
282 * Note: strides are in bytes.
284 * Only defined for non-depth-stencil formats.
287 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
288 const float *src
, unsigned src_stride
,
289 unsigned width
, unsigned height
);
292 * Fetch a single pixel (i, j) from a block.
294 * Only defined for non-depth-stencil and non-integer formats.
297 (*fetch_rgba_float
)(float *dst
,
299 unsigned i
, unsigned j
);
302 * Unpack pixels to Z32_UNORM.
303 * Note: strides are in bytes.
305 * Only defined for depth formats.
308 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
309 const uint8_t *src
, unsigned src_stride
,
310 unsigned width
, unsigned height
);
313 * Pack pixels from Z32_FLOAT.
314 * Note: strides are in bytes.
316 * Only defined for depth formats.
319 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
320 const uint32_t *src
, unsigned src_stride
,
321 unsigned width
, unsigned height
);
324 * Unpack pixels to Z32_FLOAT.
325 * Note: strides are in bytes.
327 * Only defined for depth formats.
330 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
331 const uint8_t *src
, unsigned src_stride
,
332 unsigned width
, unsigned height
);
335 * Pack pixels from Z32_FLOAT.
336 * Note: strides are in bytes.
338 * Only defined for depth formats.
341 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
342 const float *src
, unsigned src_stride
,
343 unsigned width
, unsigned height
);
346 * Unpack pixels to S8_UINT.
347 * Note: strides are in bytes.
349 * Only defined for stencil formats.
352 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
353 const uint8_t *src
, unsigned src_stride
,
354 unsigned width
, unsigned height
);
357 * Pack pixels from S8_UINT.
358 * Note: strides are in bytes.
360 * Only defined for stencil formats.
363 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
364 const uint8_t *src
, unsigned src_stride
,
365 unsigned width
, unsigned height
);
368 * Unpack pixel blocks to R32G32B32A32_UINT.
369 * Note: strides are in bytes.
371 * Only defined for INT formats.
374 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
375 const uint8_t *src
, unsigned src_stride
,
376 unsigned width
, unsigned height
);
379 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
380 const uint32_t *src
, unsigned src_stride
,
381 unsigned width
, unsigned height
);
384 * Unpack pixel blocks to R32G32B32A32_SINT.
385 * Note: strides are in bytes.
387 * Only defined for INT formats.
390 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
391 const uint8_t *src
, unsigned src_stride
,
392 unsigned width
, unsigned height
);
395 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
396 const int32_t *src
, unsigned src_stride
,
397 unsigned width
, unsigned height
);
400 * Fetch a single pixel (i, j) from a block.
402 * Only defined for unsigned (pure) integer formats.
405 (*fetch_rgba_uint
)(uint32_t *dst
,
407 unsigned i
, unsigned j
);
410 * Fetch a single pixel (i, j) from a block.
412 * Only defined for signed (pure) integer formats.
415 (*fetch_rgba_sint
)(int32_t *dst
,
417 unsigned i
, unsigned j
);
421 extern const struct util_format_description
422 util_format_description_table
[];
425 const struct util_format_description
*
426 util_format_description(enum pipe_format format
);
430 * Format query functions.
433 static inline const char *
434 util_format_name(enum pipe_format format
)
436 const struct util_format_description
*desc
= util_format_description(format
);
440 return "PIPE_FORMAT_???";
446 static inline const char *
447 util_format_short_name(enum pipe_format format
)
449 const struct util_format_description
*desc
= util_format_description(format
);
456 return desc
->short_name
;
460 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
462 static inline boolean
463 util_format_is_plain(enum pipe_format format
)
465 const struct util_format_description
*desc
= util_format_description(format
);
471 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
474 static inline boolean
475 util_format_is_compressed(enum pipe_format format
)
477 const struct util_format_description
*desc
= util_format_description(format
);
484 switch (desc
->layout
) {
485 case UTIL_FORMAT_LAYOUT_S3TC
:
486 case UTIL_FORMAT_LAYOUT_RGTC
:
487 case UTIL_FORMAT_LAYOUT_ETC
:
488 case UTIL_FORMAT_LAYOUT_BPTC
:
489 case UTIL_FORMAT_LAYOUT_ASTC
:
490 /* XXX add other formats in the future */
497 static inline boolean
498 util_format_is_s3tc(enum pipe_format format
)
500 const struct util_format_description
*desc
= util_format_description(format
);
507 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
510 static inline boolean
511 util_format_is_srgb(enum pipe_format format
)
513 const struct util_format_description
*desc
= util_format_description(format
);
514 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
517 static inline boolean
518 util_format_has_depth(const struct util_format_description
*desc
)
520 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
521 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
524 static inline boolean
525 util_format_has_stencil(const struct util_format_description
*desc
)
527 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
528 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
531 static inline boolean
532 util_format_is_depth_or_stencil(enum pipe_format format
)
534 const struct util_format_description
*desc
= util_format_description(format
);
541 return util_format_has_depth(desc
) ||
542 util_format_has_stencil(desc
);
545 static inline boolean
546 util_format_is_depth_and_stencil(enum pipe_format format
)
548 const struct util_format_description
*desc
= util_format_description(format
);
555 return util_format_has_depth(desc
) &&
556 util_format_has_stencil(desc
);
561 * Calculates the depth format type based upon the incoming format description.
563 static inline unsigned
564 util_get_depth_format_type(const struct util_format_description
*desc
)
566 unsigned depth_channel
= desc
->swizzle
[0];
567 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
568 depth_channel
!= UTIL_FORMAT_SWIZZLE_NONE
) {
569 return desc
->channel
[depth_channel
].type
;
571 return UTIL_FORMAT_TYPE_VOID
;
577 * Calculates the MRD for the depth format. MRD is used in depth bias
578 * for UNORM and unbound depth buffers. When the depth buffer is floating
579 * point, the depth bias calculation does not use the MRD. However, the
580 * default MRD will be 1.0 / ((1 << 24) - 1).
583 util_get_depth_format_mrd(const struct util_format_description
*desc
);
587 * Return whether this is an RGBA, Z, S, or combined ZS format.
588 * Useful for initializing pipe_blit_info::mask.
590 static inline unsigned
591 util_format_get_mask(enum pipe_format format
)
593 const struct util_format_description
*desc
=
594 util_format_description(format
);
599 if (util_format_has_depth(desc
)) {
600 if (util_format_has_stencil(desc
)) {
606 if (util_format_has_stencil(desc
)) {
609 return PIPE_MASK_RGBA
;
615 * Give the RGBA colormask of the channels that can be represented in this
618 * That is, the channels whose values are preserved.
620 static inline unsigned
621 util_format_colormask(const struct util_format_description
*desc
)
626 switch (desc
->colorspace
) {
627 case UTIL_FORMAT_COLORSPACE_RGB
:
628 case UTIL_FORMAT_COLORSPACE_SRGB
:
629 case UTIL_FORMAT_COLORSPACE_YUV
:
631 for (chan
= 0; chan
< 4; ++chan
) {
632 if (desc
->swizzle
[chan
] < 4) {
633 colormask
|= (1 << chan
);
637 case UTIL_FORMAT_COLORSPACE_ZS
:
647 * Checks if color mask covers every channel for the specified format
649 * @param desc a format description to check colormask with
650 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
652 static inline boolean
653 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
655 return (~colormask
& util_format_colormask(desc
)) == 0;
660 util_format_is_float(enum pipe_format format
);
664 util_format_has_alpha(enum pipe_format format
);
668 util_format_is_luminance(enum pipe_format format
);
671 util_format_is_alpha(enum pipe_format format
);
674 util_format_is_luminance_alpha(enum pipe_format format
);
678 util_format_is_intensity(enum pipe_format format
);
681 util_format_is_subsampled_422(enum pipe_format format
);
684 util_format_is_pure_integer(enum pipe_format format
);
687 util_format_is_pure_sint(enum pipe_format format
);
690 util_format_is_pure_uint(enum pipe_format format
);
693 util_format_is_snorm(enum pipe_format format
);
696 util_format_is_snorm8(enum pipe_format format
);
699 * Check if the src format can be blitted to the destination format with
700 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
704 util_is_format_compatible(const struct util_format_description
*src_desc
,
705 const struct util_format_description
*dst_desc
);
708 * Whether the format is supported by Gallium for the given bindings.
709 * This covers S3TC textures and floating-point render targets.
712 util_format_is_supported(enum pipe_format format
, unsigned bind
);
715 * Whether this format is a rgab8 variant.
717 * That is, any format that matches the
719 * PIPE_FORMAT_?8?8?8?8_UNORM
721 static inline boolean
722 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
726 if(desc
->block
.width
!= 1 ||
727 desc
->block
.height
!= 1 ||
728 desc
->block
.bits
!= 32)
731 for(chan
= 0; chan
< 4; ++chan
) {
732 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
733 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
735 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
736 !desc
->channel
[chan
].normalized
)
738 if(desc
->channel
[chan
].size
!= 8)
747 * Return total bits needed for the pixel format per block.
750 util_format_get_blocksizebits(enum pipe_format format
)
752 const struct util_format_description
*desc
= util_format_description(format
);
759 return desc
->block
.bits
;
763 * Return bytes per block (not pixel) for the given format.
766 util_format_get_blocksize(enum pipe_format format
)
768 uint bits
= util_format_get_blocksizebits(format
);
769 uint bytes
= bits
/ 8;
771 assert(bits
% 8 == 0);
781 util_format_get_blockwidth(enum pipe_format format
)
783 const struct util_format_description
*desc
= util_format_description(format
);
790 return desc
->block
.width
;
794 util_format_get_blockheight(enum pipe_format format
)
796 const struct util_format_description
*desc
= util_format_description(format
);
803 return desc
->block
.height
;
806 static inline unsigned
807 util_format_get_nblocksx(enum pipe_format format
,
810 unsigned blockwidth
= util_format_get_blockwidth(format
);
811 return (x
+ blockwidth
- 1) / blockwidth
;
814 static inline unsigned
815 util_format_get_nblocksy(enum pipe_format format
,
818 unsigned blockheight
= util_format_get_blockheight(format
);
819 return (y
+ blockheight
- 1) / blockheight
;
822 static inline unsigned
823 util_format_get_nblocks(enum pipe_format format
,
827 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
831 util_format_get_stride(enum pipe_format format
,
834 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
838 util_format_get_2d_size(enum pipe_format format
,
842 return util_format_get_nblocksy(format
, height
) * stride
;
846 util_format_get_component_bits(enum pipe_format format
,
847 enum util_format_colorspace colorspace
,
850 const struct util_format_description
*desc
= util_format_description(format
);
851 enum util_format_colorspace desc_colorspace
;
858 assert(component
< 4);
860 /* Treat RGB and SRGB as equivalent. */
861 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
862 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
864 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
865 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
867 desc_colorspace
= desc
->colorspace
;
870 if (desc_colorspace
!= colorspace
) {
874 switch (desc
->swizzle
[component
]) {
875 case UTIL_FORMAT_SWIZZLE_X
:
876 return desc
->channel
[0].size
;
877 case UTIL_FORMAT_SWIZZLE_Y
:
878 return desc
->channel
[1].size
;
879 case UTIL_FORMAT_SWIZZLE_Z
:
880 return desc
->channel
[2].size
;
881 case UTIL_FORMAT_SWIZZLE_W
:
882 return desc
->channel
[3].size
;
889 * Given a linear RGB colorspace format, return the corresponding SRGB
890 * format, or PIPE_FORMAT_NONE if none.
892 static inline enum pipe_format
893 util_format_srgb(enum pipe_format format
)
895 if (util_format_is_srgb(format
))
899 case PIPE_FORMAT_L8_UNORM
:
900 return PIPE_FORMAT_L8_SRGB
;
901 case PIPE_FORMAT_L8A8_UNORM
:
902 return PIPE_FORMAT_L8A8_SRGB
;
903 case PIPE_FORMAT_R8G8B8_UNORM
:
904 return PIPE_FORMAT_R8G8B8_SRGB
;
905 case PIPE_FORMAT_A8B8G8R8_UNORM
:
906 return PIPE_FORMAT_A8B8G8R8_SRGB
;
907 case PIPE_FORMAT_X8B8G8R8_UNORM
:
908 return PIPE_FORMAT_X8B8G8R8_SRGB
;
909 case PIPE_FORMAT_B8G8R8A8_UNORM
:
910 return PIPE_FORMAT_B8G8R8A8_SRGB
;
911 case PIPE_FORMAT_B8G8R8X8_UNORM
:
912 return PIPE_FORMAT_B8G8R8X8_SRGB
;
913 case PIPE_FORMAT_A8R8G8B8_UNORM
:
914 return PIPE_FORMAT_A8R8G8B8_SRGB
;
915 case PIPE_FORMAT_X8R8G8B8_UNORM
:
916 return PIPE_FORMAT_X8R8G8B8_SRGB
;
917 case PIPE_FORMAT_R8G8B8A8_UNORM
:
918 return PIPE_FORMAT_R8G8B8A8_SRGB
;
919 case PIPE_FORMAT_R8G8B8X8_UNORM
:
920 return PIPE_FORMAT_R8G8B8X8_SRGB
;
921 case PIPE_FORMAT_DXT1_RGB
:
922 return PIPE_FORMAT_DXT1_SRGB
;
923 case PIPE_FORMAT_DXT1_RGBA
:
924 return PIPE_FORMAT_DXT1_SRGBA
;
925 case PIPE_FORMAT_DXT3_RGBA
:
926 return PIPE_FORMAT_DXT3_SRGBA
;
927 case PIPE_FORMAT_DXT5_RGBA
:
928 return PIPE_FORMAT_DXT5_SRGBA
;
929 case PIPE_FORMAT_B5G6R5_UNORM
:
930 return PIPE_FORMAT_B5G6R5_SRGB
;
931 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
932 return PIPE_FORMAT_BPTC_SRGBA
;
933 case PIPE_FORMAT_ASTC_4x4
:
934 return PIPE_FORMAT_ASTC_4x4_SRGB
;
935 case PIPE_FORMAT_ASTC_5x4
:
936 return PIPE_FORMAT_ASTC_5x4_SRGB
;
937 case PIPE_FORMAT_ASTC_5x5
:
938 return PIPE_FORMAT_ASTC_5x5_SRGB
;
939 case PIPE_FORMAT_ASTC_6x5
:
940 return PIPE_FORMAT_ASTC_6x5_SRGB
;
941 case PIPE_FORMAT_ASTC_6x6
:
942 return PIPE_FORMAT_ASTC_6x6_SRGB
;
943 case PIPE_FORMAT_ASTC_8x5
:
944 return PIPE_FORMAT_ASTC_8x5_SRGB
;
945 case PIPE_FORMAT_ASTC_8x6
:
946 return PIPE_FORMAT_ASTC_8x6_SRGB
;
947 case PIPE_FORMAT_ASTC_8x8
:
948 return PIPE_FORMAT_ASTC_8x8_SRGB
;
949 case PIPE_FORMAT_ASTC_10x5
:
950 return PIPE_FORMAT_ASTC_10x5_SRGB
;
951 case PIPE_FORMAT_ASTC_10x6
:
952 return PIPE_FORMAT_ASTC_10x6_SRGB
;
953 case PIPE_FORMAT_ASTC_10x8
:
954 return PIPE_FORMAT_ASTC_10x8_SRGB
;
955 case PIPE_FORMAT_ASTC_10x10
:
956 return PIPE_FORMAT_ASTC_10x10_SRGB
;
957 case PIPE_FORMAT_ASTC_12x10
:
958 return PIPE_FORMAT_ASTC_12x10_SRGB
;
959 case PIPE_FORMAT_ASTC_12x12
:
960 return PIPE_FORMAT_ASTC_12x12_SRGB
;
963 return PIPE_FORMAT_NONE
;
968 * Given an sRGB format, return the corresponding linear colorspace format.
969 * For non sRGB formats, return the format unchanged.
971 static inline enum pipe_format
972 util_format_linear(enum pipe_format format
)
975 case PIPE_FORMAT_L8_SRGB
:
976 return PIPE_FORMAT_L8_UNORM
;
977 case PIPE_FORMAT_L8A8_SRGB
:
978 return PIPE_FORMAT_L8A8_UNORM
;
979 case PIPE_FORMAT_R8G8B8_SRGB
:
980 return PIPE_FORMAT_R8G8B8_UNORM
;
981 case PIPE_FORMAT_A8B8G8R8_SRGB
:
982 return PIPE_FORMAT_A8B8G8R8_UNORM
;
983 case PIPE_FORMAT_X8B8G8R8_SRGB
:
984 return PIPE_FORMAT_X8B8G8R8_UNORM
;
985 case PIPE_FORMAT_B8G8R8A8_SRGB
:
986 return PIPE_FORMAT_B8G8R8A8_UNORM
;
987 case PIPE_FORMAT_B8G8R8X8_SRGB
:
988 return PIPE_FORMAT_B8G8R8X8_UNORM
;
989 case PIPE_FORMAT_A8R8G8B8_SRGB
:
990 return PIPE_FORMAT_A8R8G8B8_UNORM
;
991 case PIPE_FORMAT_X8R8G8B8_SRGB
:
992 return PIPE_FORMAT_X8R8G8B8_UNORM
;
993 case PIPE_FORMAT_R8G8B8A8_SRGB
:
994 return PIPE_FORMAT_R8G8B8A8_UNORM
;
995 case PIPE_FORMAT_R8G8B8X8_SRGB
:
996 return PIPE_FORMAT_R8G8B8X8_UNORM
;
997 case PIPE_FORMAT_DXT1_SRGB
:
998 return PIPE_FORMAT_DXT1_RGB
;
999 case PIPE_FORMAT_DXT1_SRGBA
:
1000 return PIPE_FORMAT_DXT1_RGBA
;
1001 case PIPE_FORMAT_DXT3_SRGBA
:
1002 return PIPE_FORMAT_DXT3_RGBA
;
1003 case PIPE_FORMAT_DXT5_SRGBA
:
1004 return PIPE_FORMAT_DXT5_RGBA
;
1005 case PIPE_FORMAT_B5G6R5_SRGB
:
1006 return PIPE_FORMAT_B5G6R5_UNORM
;
1007 case PIPE_FORMAT_BPTC_SRGBA
:
1008 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
1009 case PIPE_FORMAT_ASTC_4x4_SRGB
:
1010 return PIPE_FORMAT_ASTC_4x4
;
1011 case PIPE_FORMAT_ASTC_5x4_SRGB
:
1012 return PIPE_FORMAT_ASTC_5x4
;
1013 case PIPE_FORMAT_ASTC_5x5_SRGB
:
1014 return PIPE_FORMAT_ASTC_5x5
;
1015 case PIPE_FORMAT_ASTC_6x5_SRGB
:
1016 return PIPE_FORMAT_ASTC_6x5
;
1017 case PIPE_FORMAT_ASTC_6x6_SRGB
:
1018 return PIPE_FORMAT_ASTC_6x6
;
1019 case PIPE_FORMAT_ASTC_8x5_SRGB
:
1020 return PIPE_FORMAT_ASTC_8x5
;
1021 case PIPE_FORMAT_ASTC_8x6_SRGB
:
1022 return PIPE_FORMAT_ASTC_8x6
;
1023 case PIPE_FORMAT_ASTC_8x8_SRGB
:
1024 return PIPE_FORMAT_ASTC_8x8
;
1025 case PIPE_FORMAT_ASTC_10x5_SRGB
:
1026 return PIPE_FORMAT_ASTC_10x5
;
1027 case PIPE_FORMAT_ASTC_10x6_SRGB
:
1028 return PIPE_FORMAT_ASTC_10x6
;
1029 case PIPE_FORMAT_ASTC_10x8_SRGB
:
1030 return PIPE_FORMAT_ASTC_10x8
;
1031 case PIPE_FORMAT_ASTC_10x10_SRGB
:
1032 return PIPE_FORMAT_ASTC_10x10
;
1033 case PIPE_FORMAT_ASTC_12x10_SRGB
:
1034 return PIPE_FORMAT_ASTC_12x10
;
1035 case PIPE_FORMAT_ASTC_12x12_SRGB
:
1036 return PIPE_FORMAT_ASTC_12x12
;
1043 * Given a depth-stencil format, return the corresponding stencil-only format.
1044 * For stencil-only formats, return the format unchanged.
1046 static inline enum pipe_format
1047 util_format_stencil_only(enum pipe_format format
)
1050 /* mask out the depth component */
1051 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1052 return PIPE_FORMAT_X24S8_UINT
;
1053 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1054 return PIPE_FORMAT_S8X24_UINT
;
1055 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1056 return PIPE_FORMAT_X32_S8X24_UINT
;
1058 /* stencil only formats */
1059 case PIPE_FORMAT_X24S8_UINT
:
1060 case PIPE_FORMAT_S8X24_UINT
:
1061 case PIPE_FORMAT_X32_S8X24_UINT
:
1062 case PIPE_FORMAT_S8_UINT
:
1067 return PIPE_FORMAT_NONE
;
1072 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1073 * This is identity for non-intensity formats.
1075 static inline enum pipe_format
1076 util_format_intensity_to_red(enum pipe_format format
)
1079 case PIPE_FORMAT_I8_UNORM
:
1080 return PIPE_FORMAT_R8_UNORM
;
1081 case PIPE_FORMAT_I8_SNORM
:
1082 return PIPE_FORMAT_R8_SNORM
;
1083 case PIPE_FORMAT_I16_UNORM
:
1084 return PIPE_FORMAT_R16_UNORM
;
1085 case PIPE_FORMAT_I16_SNORM
:
1086 return PIPE_FORMAT_R16_SNORM
;
1087 case PIPE_FORMAT_I16_FLOAT
:
1088 return PIPE_FORMAT_R16_FLOAT
;
1089 case PIPE_FORMAT_I32_FLOAT
:
1090 return PIPE_FORMAT_R32_FLOAT
;
1091 case PIPE_FORMAT_I8_UINT
:
1092 return PIPE_FORMAT_R8_UINT
;
1093 case PIPE_FORMAT_I8_SINT
:
1094 return PIPE_FORMAT_R8_SINT
;
1095 case PIPE_FORMAT_I16_UINT
:
1096 return PIPE_FORMAT_R16_UINT
;
1097 case PIPE_FORMAT_I16_SINT
:
1098 return PIPE_FORMAT_R16_SINT
;
1099 case PIPE_FORMAT_I32_UINT
:
1100 return PIPE_FORMAT_R32_UINT
;
1101 case PIPE_FORMAT_I32_SINT
:
1102 return PIPE_FORMAT_R32_SINT
;
1104 assert(!util_format_is_intensity(format
));
1110 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1111 * This is identity for non-luminance formats.
1113 static inline enum pipe_format
1114 util_format_luminance_to_red(enum pipe_format format
)
1117 case PIPE_FORMAT_L8_UNORM
:
1118 return PIPE_FORMAT_R8_UNORM
;
1119 case PIPE_FORMAT_L8_SNORM
:
1120 return PIPE_FORMAT_R8_SNORM
;
1121 case PIPE_FORMAT_L16_UNORM
:
1122 return PIPE_FORMAT_R16_UNORM
;
1123 case PIPE_FORMAT_L16_SNORM
:
1124 return PIPE_FORMAT_R16_SNORM
;
1125 case PIPE_FORMAT_L16_FLOAT
:
1126 return PIPE_FORMAT_R16_FLOAT
;
1127 case PIPE_FORMAT_L32_FLOAT
:
1128 return PIPE_FORMAT_R32_FLOAT
;
1129 case PIPE_FORMAT_L8_UINT
:
1130 return PIPE_FORMAT_R8_UINT
;
1131 case PIPE_FORMAT_L8_SINT
:
1132 return PIPE_FORMAT_R8_SINT
;
1133 case PIPE_FORMAT_L16_UINT
:
1134 return PIPE_FORMAT_R16_UINT
;
1135 case PIPE_FORMAT_L16_SINT
:
1136 return PIPE_FORMAT_R16_SINT
;
1137 case PIPE_FORMAT_L32_UINT
:
1138 return PIPE_FORMAT_R32_UINT
;
1139 case PIPE_FORMAT_L32_SINT
:
1140 return PIPE_FORMAT_R32_SINT
;
1142 case PIPE_FORMAT_LATC1_UNORM
:
1143 return PIPE_FORMAT_RGTC1_UNORM
;
1144 case PIPE_FORMAT_LATC1_SNORM
:
1145 return PIPE_FORMAT_RGTC1_SNORM
;
1147 case PIPE_FORMAT_L4A4_UNORM
:
1148 return PIPE_FORMAT_R4A4_UNORM
;
1150 case PIPE_FORMAT_L8A8_UNORM
:
1151 return PIPE_FORMAT_R8A8_UNORM
;
1152 case PIPE_FORMAT_L8A8_SNORM
:
1153 return PIPE_FORMAT_R8A8_SNORM
;
1154 case PIPE_FORMAT_L16A16_UNORM
:
1155 return PIPE_FORMAT_R16A16_UNORM
;
1156 case PIPE_FORMAT_L16A16_SNORM
:
1157 return PIPE_FORMAT_R16A16_SNORM
;
1158 case PIPE_FORMAT_L16A16_FLOAT
:
1159 return PIPE_FORMAT_R16A16_FLOAT
;
1160 case PIPE_FORMAT_L32A32_FLOAT
:
1161 return PIPE_FORMAT_R32A32_FLOAT
;
1162 case PIPE_FORMAT_L8A8_UINT
:
1163 return PIPE_FORMAT_R8A8_UINT
;
1164 case PIPE_FORMAT_L8A8_SINT
:
1165 return PIPE_FORMAT_R8A8_SINT
;
1166 case PIPE_FORMAT_L16A16_UINT
:
1167 return PIPE_FORMAT_R16A16_UINT
;
1168 case PIPE_FORMAT_L16A16_SINT
:
1169 return PIPE_FORMAT_R16A16_SINT
;
1170 case PIPE_FORMAT_L32A32_UINT
:
1171 return PIPE_FORMAT_R32A32_UINT
;
1172 case PIPE_FORMAT_L32A32_SINT
:
1173 return PIPE_FORMAT_R32A32_SINT
;
1175 /* We don't have compressed red-alpha variants for these. */
1176 case PIPE_FORMAT_LATC2_UNORM
:
1177 case PIPE_FORMAT_LATC2_SNORM
:
1178 return PIPE_FORMAT_NONE
;
1181 assert(!util_format_is_luminance(format
) &&
1182 !util_format_is_luminance_alpha(format
));
1188 * Return the number of components stored.
1189 * Formats with block size != 1x1 will always have 1 component (the block).
1191 static inline unsigned
1192 util_format_get_nr_components(enum pipe_format format
)
1194 const struct util_format_description
*desc
= util_format_description(format
);
1195 return desc
->nr_channels
;
1199 * Return the index of the first non-void channel
1200 * -1 if no non-void channels
1203 util_format_get_first_non_void_channel(enum pipe_format format
)
1205 const struct util_format_description
*desc
= util_format_description(format
);
1208 for (i
= 0; i
< 4; i
++)
1209 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1219 * Format access functions.
1223 util_format_read_4f(enum pipe_format format
,
1224 float *dst
, unsigned dst_stride
,
1225 const void *src
, unsigned src_stride
,
1226 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1229 util_format_write_4f(enum pipe_format format
,
1230 const float *src
, unsigned src_stride
,
1231 void *dst
, unsigned dst_stride
,
1232 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1235 util_format_read_4ub(enum pipe_format format
,
1236 uint8_t *dst
, unsigned dst_stride
,
1237 const void *src
, unsigned src_stride
,
1238 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1241 util_format_write_4ub(enum pipe_format format
,
1242 const uint8_t *src
, unsigned src_stride
,
1243 void *dst
, unsigned dst_stride
,
1244 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1247 util_format_read_4ui(enum pipe_format format
,
1248 unsigned *dst
, unsigned dst_stride
,
1249 const void *src
, unsigned src_stride
,
1250 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1253 util_format_write_4ui(enum pipe_format format
,
1254 const unsigned int *src
, unsigned src_stride
,
1255 void *dst
, unsigned dst_stride
,
1256 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1259 util_format_read_4i(enum pipe_format format
,
1260 int *dst
, unsigned dst_stride
,
1261 const void *src
, unsigned src_stride
,
1262 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1265 util_format_write_4i(enum pipe_format format
,
1266 const int *src
, unsigned src_stride
,
1267 void *dst
, unsigned dst_stride
,
1268 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1271 * Generic format conversion;
1275 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1278 util_format_translate(enum pipe_format dst_format
,
1279 void *dst
, unsigned dst_stride
,
1280 unsigned dst_x
, unsigned dst_y
,
1281 enum pipe_format src_format
,
1282 const void *src
, unsigned src_stride
,
1283 unsigned src_x
, unsigned src_y
,
1284 unsigned width
, unsigned height
);
1287 * Swizzle operations.
1290 /* Compose two sets of swizzles.
1291 * If V is a 4D vector and the function parameters represent functions that
1292 * swizzle vector components, this holds:
1293 * swz2(swz1(V)) = dst(V)
1295 void util_format_compose_swizzles(const unsigned char swz1
[4],
1296 const unsigned char swz2
[4],
1297 unsigned char dst
[4]);
1299 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1300 * to \param src and store the result in \param dst.
1301 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1303 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1304 const union pipe_color_union
*src
,
1305 const unsigned char swz
[4],
1306 const boolean is_integer
);
1308 void util_format_swizzle_4f(float *dst
, const float *src
,
1309 const unsigned char swz
[4]);
1311 void util_format_unswizzle_4f(float *dst
, const float *src
,
1312 const unsigned char swz
[4]);
1318 #endif /* ! U_FORMAT_H */