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,
87 * Everything else that doesn't fit in any of the above layouts.
89 UTIL_FORMAT_LAYOUT_OTHER
= 8
93 struct util_format_block
95 /** Block width in pixels */
98 /** Block height in pixels */
101 /** Block size in bits */
106 enum util_format_type
{
107 UTIL_FORMAT_TYPE_VOID
= 0,
108 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
109 UTIL_FORMAT_TYPE_SIGNED
= 2,
110 UTIL_FORMAT_TYPE_FIXED
= 3,
111 UTIL_FORMAT_TYPE_FLOAT
= 4
115 enum util_format_swizzle
{
116 UTIL_FORMAT_SWIZZLE_X
= 0,
117 UTIL_FORMAT_SWIZZLE_Y
= 1,
118 UTIL_FORMAT_SWIZZLE_Z
= 2,
119 UTIL_FORMAT_SWIZZLE_W
= 3,
120 UTIL_FORMAT_SWIZZLE_0
= 4,
121 UTIL_FORMAT_SWIZZLE_1
= 5,
122 UTIL_FORMAT_SWIZZLE_NONE
= 6,
123 UTIL_FORMAT_SWIZZLE_MAX
= 7 /**< Number of enums counter (must be last) */
127 enum util_format_colorspace
{
128 UTIL_FORMAT_COLORSPACE_RGB
= 0,
129 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
130 UTIL_FORMAT_COLORSPACE_YUV
= 2,
131 UTIL_FORMAT_COLORSPACE_ZS
= 3
135 struct util_format_channel_description
137 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
138 unsigned normalized
:1;
139 unsigned pure_integer
:1;
140 unsigned size
:9; /**< bits per channel */
141 unsigned shift
:16; /** number of bits from lsb */
145 struct util_format_description
147 enum pipe_format format
;
152 * Short name, striped of the prefix, lower case.
154 const char *short_name
;
157 * Pixel block dimensions.
159 struct util_format_block block
;
161 enum util_format_layout layout
;
164 * The number of channels.
166 unsigned nr_channels
:3;
169 * Whether all channels have the same number of (whole) bytes and type.
174 * Whether the pixel format can be described as a bitfield structure.
177 * - pixel depth must be 8, 16, or 32 bits;
178 * - all channels must be unsigned, signed, or void
180 unsigned is_bitmask
:1;
183 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
188 * Input channel description, in the order XYZW.
190 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
192 * If each channel is accessed as an individual N-byte value, X is always
193 * at the lowest address in memory, Y is always next, and so on. For all
194 * currently-defined formats, the N-byte value has native endianness.
196 * If instead a group of channels is accessed as a single N-byte value,
197 * the order of the channels within that value depends on endianness.
198 * For big-endian targets, X is the most significant subvalue,
199 * otherwise it is the least significant one.
201 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
204 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
205 * big-endian: X Yu Ym Yl
207 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
211 * little-endian: YYYXXXXX WZZZZZYY
212 * big-endian: XXXXXYYY YYZZZZZW
214 struct util_format_channel_description channel
[4];
217 * Output channel swizzle.
219 * The order is either:
223 * depending on the colorspace.
225 unsigned char swizzle
[4];
228 * Colorspace transformation.
230 enum util_format_colorspace colorspace
;
233 * Unpack pixel blocks to R8G8B8A8_UNORM.
234 * Note: strides are in bytes.
236 * Only defined for non-depth-stencil formats.
239 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
240 const uint8_t *src
, unsigned src_stride
,
241 unsigned width
, unsigned height
);
244 * Pack pixel blocks from R8G8B8A8_UNORM.
245 * Note: strides are in bytes.
247 * Only defined for non-depth-stencil formats.
250 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
251 const uint8_t *src
, unsigned src_stride
,
252 unsigned width
, unsigned height
);
255 * Fetch a single pixel (i, j) from a block.
257 * XXX: Only defined for a very few select formats.
260 (*fetch_rgba_8unorm
)(uint8_t *dst
,
262 unsigned i
, unsigned j
);
265 * Unpack pixel blocks to R32G32B32A32_FLOAT.
266 * Note: strides are in bytes.
268 * Only defined for non-depth-stencil formats.
271 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
272 const uint8_t *src
, unsigned src_stride
,
273 unsigned width
, unsigned height
);
276 * Pack pixel blocks from R32G32B32A32_FLOAT.
277 * Note: strides are in bytes.
279 * Only defined for non-depth-stencil formats.
282 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
283 const float *src
, unsigned src_stride
,
284 unsigned width
, unsigned height
);
287 * Fetch a single pixel (i, j) from a block.
289 * Only defined for non-depth-stencil and non-integer formats.
292 (*fetch_rgba_float
)(float *dst
,
294 unsigned i
, unsigned j
);
297 * Unpack pixels to Z32_UNORM.
298 * Note: strides are in bytes.
300 * Only defined for depth formats.
303 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
304 const uint8_t *src
, unsigned src_stride
,
305 unsigned width
, unsigned height
);
308 * Pack pixels from Z32_FLOAT.
309 * Note: strides are in bytes.
311 * Only defined for depth formats.
314 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
315 const uint32_t *src
, unsigned src_stride
,
316 unsigned width
, unsigned height
);
319 * Unpack pixels to Z32_FLOAT.
320 * Note: strides are in bytes.
322 * Only defined for depth formats.
325 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
326 const uint8_t *src
, unsigned src_stride
,
327 unsigned width
, unsigned height
);
330 * Pack pixels from Z32_FLOAT.
331 * Note: strides are in bytes.
333 * Only defined for depth formats.
336 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
337 const float *src
, unsigned src_stride
,
338 unsigned width
, unsigned height
);
341 * Unpack pixels to S8_UINT.
342 * Note: strides are in bytes.
344 * Only defined for stencil formats.
347 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
348 const uint8_t *src
, unsigned src_stride
,
349 unsigned width
, unsigned height
);
352 * Pack pixels from S8_UINT.
353 * Note: strides are in bytes.
355 * Only defined for stencil formats.
358 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
359 const uint8_t *src
, unsigned src_stride
,
360 unsigned width
, unsigned height
);
363 * Unpack pixel blocks to R32G32B32A32_UINT.
364 * Note: strides are in bytes.
366 * Only defined for INT formats.
369 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
370 const uint8_t *src
, unsigned src_stride
,
371 unsigned width
, unsigned height
);
374 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
375 const uint32_t *src
, unsigned src_stride
,
376 unsigned width
, unsigned height
);
379 * Unpack pixel blocks to R32G32B32A32_SINT.
380 * Note: strides are in bytes.
382 * Only defined for INT formats.
385 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
386 const uint8_t *src
, unsigned src_stride
,
387 unsigned width
, unsigned height
);
390 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
391 const int32_t *src
, unsigned src_stride
,
392 unsigned width
, unsigned height
);
395 * Fetch a single pixel (i, j) from a block.
397 * Only defined for unsigned (pure) integer formats.
400 (*fetch_rgba_uint
)(uint32_t *dst
,
402 unsigned i
, unsigned j
);
405 * Fetch a single pixel (i, j) from a block.
407 * Only defined for signed (pure) integer formats.
410 (*fetch_rgba_sint
)(int32_t *dst
,
412 unsigned i
, unsigned j
);
416 extern const struct util_format_description
417 util_format_description_table
[];
420 const struct util_format_description
*
421 util_format_description(enum pipe_format format
);
425 * Format query functions.
428 static inline const char *
429 util_format_name(enum pipe_format format
)
431 const struct util_format_description
*desc
= util_format_description(format
);
435 return "PIPE_FORMAT_???";
441 static inline const char *
442 util_format_short_name(enum pipe_format format
)
444 const struct util_format_description
*desc
= util_format_description(format
);
451 return desc
->short_name
;
455 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
457 static inline boolean
458 util_format_is_plain(enum pipe_format format
)
460 const struct util_format_description
*desc
= util_format_description(format
);
466 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
469 static inline boolean
470 util_format_is_compressed(enum pipe_format format
)
472 const struct util_format_description
*desc
= util_format_description(format
);
479 switch (desc
->layout
) {
480 case UTIL_FORMAT_LAYOUT_S3TC
:
481 case UTIL_FORMAT_LAYOUT_RGTC
:
482 case UTIL_FORMAT_LAYOUT_ETC
:
483 case UTIL_FORMAT_LAYOUT_BPTC
:
484 /* XXX add other formats in the future */
491 static inline boolean
492 util_format_is_s3tc(enum pipe_format format
)
494 const struct util_format_description
*desc
= util_format_description(format
);
501 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
504 static inline boolean
505 util_format_is_srgb(enum pipe_format format
)
507 const struct util_format_description
*desc
= util_format_description(format
);
508 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
511 static inline boolean
512 util_format_has_depth(const struct util_format_description
*desc
)
514 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
515 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
518 static inline boolean
519 util_format_has_stencil(const struct util_format_description
*desc
)
521 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
522 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
525 static inline boolean
526 util_format_is_depth_or_stencil(enum pipe_format format
)
528 const struct util_format_description
*desc
= util_format_description(format
);
535 return util_format_has_depth(desc
) ||
536 util_format_has_stencil(desc
);
539 static inline boolean
540 util_format_is_depth_and_stencil(enum pipe_format format
)
542 const struct util_format_description
*desc
= util_format_description(format
);
549 return util_format_has_depth(desc
) &&
550 util_format_has_stencil(desc
);
555 * Calculates the depth format type based upon the incoming format description.
557 static inline unsigned
558 util_get_depth_format_type(const struct util_format_description
*desc
)
560 unsigned depth_channel
= desc
->swizzle
[0];
561 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
562 depth_channel
!= UTIL_FORMAT_SWIZZLE_NONE
) {
563 return desc
->channel
[depth_channel
].type
;
565 return UTIL_FORMAT_TYPE_VOID
;
571 * Calculates the MRD for the depth format. MRD is used in depth bias
572 * for UNORM and unbound depth buffers. When the depth buffer is floating
573 * point, the depth bias calculation does not use the MRD. However, the
574 * default MRD will be 1.0 / ((1 << 24) - 1).
577 util_get_depth_format_mrd(const struct util_format_description
*desc
);
581 * Return whether this is an RGBA, Z, S, or combined ZS format.
582 * Useful for initializing pipe_blit_info::mask.
584 static inline unsigned
585 util_format_get_mask(enum pipe_format format
)
587 const struct util_format_description
*desc
=
588 util_format_description(format
);
593 if (util_format_has_depth(desc
)) {
594 if (util_format_has_stencil(desc
)) {
600 if (util_format_has_stencil(desc
)) {
603 return PIPE_MASK_RGBA
;
609 * Give the RGBA colormask of the channels that can be represented in this
612 * That is, the channels whose values are preserved.
614 static inline unsigned
615 util_format_colormask(const struct util_format_description
*desc
)
620 switch (desc
->colorspace
) {
621 case UTIL_FORMAT_COLORSPACE_RGB
:
622 case UTIL_FORMAT_COLORSPACE_SRGB
:
623 case UTIL_FORMAT_COLORSPACE_YUV
:
625 for (chan
= 0; chan
< 4; ++chan
) {
626 if (desc
->swizzle
[chan
] < 4) {
627 colormask
|= (1 << chan
);
631 case UTIL_FORMAT_COLORSPACE_ZS
:
641 * Checks if color mask covers every channel for the specified format
643 * @param desc a format description to check colormask with
644 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
646 static inline boolean
647 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
649 return (~colormask
& util_format_colormask(desc
)) == 0;
654 util_format_is_float(enum pipe_format format
);
658 util_format_has_alpha(enum pipe_format format
);
662 util_format_is_luminance(enum pipe_format format
);
665 util_format_is_alpha(enum pipe_format format
);
668 util_format_is_luminance_alpha(enum pipe_format format
);
672 util_format_is_intensity(enum pipe_format format
);
675 util_format_is_subsampled_422(enum pipe_format format
);
678 util_format_is_pure_integer(enum pipe_format format
);
681 util_format_is_pure_sint(enum pipe_format format
);
684 util_format_is_pure_uint(enum pipe_format format
);
687 util_format_is_snorm(enum pipe_format format
);
690 util_format_is_snorm8(enum pipe_format format
);
693 * Check if the src format can be blitted to the destination format with
694 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
698 util_is_format_compatible(const struct util_format_description
*src_desc
,
699 const struct util_format_description
*dst_desc
);
702 * Whether the format is supported by Gallium for the given bindings.
703 * This covers S3TC textures and floating-point render targets.
706 util_format_is_supported(enum pipe_format format
, unsigned bind
);
709 * Whether this format is a rgab8 variant.
711 * That is, any format that matches the
713 * PIPE_FORMAT_?8?8?8?8_UNORM
715 static inline boolean
716 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
720 if(desc
->block
.width
!= 1 ||
721 desc
->block
.height
!= 1 ||
722 desc
->block
.bits
!= 32)
725 for(chan
= 0; chan
< 4; ++chan
) {
726 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
727 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
729 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
730 !desc
->channel
[chan
].normalized
)
732 if(desc
->channel
[chan
].size
!= 8)
741 * Return total bits needed for the pixel format per block.
744 util_format_get_blocksizebits(enum pipe_format format
)
746 const struct util_format_description
*desc
= util_format_description(format
);
753 return desc
->block
.bits
;
757 * Return bytes per block (not pixel) for the given format.
760 util_format_get_blocksize(enum pipe_format format
)
762 uint bits
= util_format_get_blocksizebits(format
);
763 uint bytes
= bits
/ 8;
765 assert(bits
% 8 == 0);
775 util_format_get_blockwidth(enum pipe_format format
)
777 const struct util_format_description
*desc
= util_format_description(format
);
784 return desc
->block
.width
;
788 util_format_get_blockheight(enum pipe_format format
)
790 const struct util_format_description
*desc
= util_format_description(format
);
797 return desc
->block
.height
;
800 static inline unsigned
801 util_format_get_nblocksx(enum pipe_format format
,
804 unsigned blockwidth
= util_format_get_blockwidth(format
);
805 return (x
+ blockwidth
- 1) / blockwidth
;
808 static inline unsigned
809 util_format_get_nblocksy(enum pipe_format format
,
812 unsigned blockheight
= util_format_get_blockheight(format
);
813 return (y
+ blockheight
- 1) / blockheight
;
816 static inline unsigned
817 util_format_get_nblocks(enum pipe_format format
,
821 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
825 util_format_get_stride(enum pipe_format format
,
828 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
832 util_format_get_2d_size(enum pipe_format format
,
836 return util_format_get_nblocksy(format
, height
) * stride
;
840 util_format_get_component_bits(enum pipe_format format
,
841 enum util_format_colorspace colorspace
,
844 const struct util_format_description
*desc
= util_format_description(format
);
845 enum util_format_colorspace desc_colorspace
;
852 assert(component
< 4);
854 /* Treat RGB and SRGB as equivalent. */
855 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
856 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
858 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
859 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
861 desc_colorspace
= desc
->colorspace
;
864 if (desc_colorspace
!= colorspace
) {
868 switch (desc
->swizzle
[component
]) {
869 case UTIL_FORMAT_SWIZZLE_X
:
870 return desc
->channel
[0].size
;
871 case UTIL_FORMAT_SWIZZLE_Y
:
872 return desc
->channel
[1].size
;
873 case UTIL_FORMAT_SWIZZLE_Z
:
874 return desc
->channel
[2].size
;
875 case UTIL_FORMAT_SWIZZLE_W
:
876 return desc
->channel
[3].size
;
883 * Given a linear RGB colorspace format, return the corresponding SRGB
884 * format, or PIPE_FORMAT_NONE if none.
886 static inline enum pipe_format
887 util_format_srgb(enum pipe_format format
)
889 if (util_format_is_srgb(format
))
893 case PIPE_FORMAT_L8_UNORM
:
894 return PIPE_FORMAT_L8_SRGB
;
895 case PIPE_FORMAT_L8A8_UNORM
:
896 return PIPE_FORMAT_L8A8_SRGB
;
897 case PIPE_FORMAT_R8G8B8_UNORM
:
898 return PIPE_FORMAT_R8G8B8_SRGB
;
899 case PIPE_FORMAT_A8B8G8R8_UNORM
:
900 return PIPE_FORMAT_A8B8G8R8_SRGB
;
901 case PIPE_FORMAT_X8B8G8R8_UNORM
:
902 return PIPE_FORMAT_X8B8G8R8_SRGB
;
903 case PIPE_FORMAT_B8G8R8A8_UNORM
:
904 return PIPE_FORMAT_B8G8R8A8_SRGB
;
905 case PIPE_FORMAT_B8G8R8X8_UNORM
:
906 return PIPE_FORMAT_B8G8R8X8_SRGB
;
907 case PIPE_FORMAT_A8R8G8B8_UNORM
:
908 return PIPE_FORMAT_A8R8G8B8_SRGB
;
909 case PIPE_FORMAT_X8R8G8B8_UNORM
:
910 return PIPE_FORMAT_X8R8G8B8_SRGB
;
911 case PIPE_FORMAT_R8G8B8A8_UNORM
:
912 return PIPE_FORMAT_R8G8B8A8_SRGB
;
913 case PIPE_FORMAT_R8G8B8X8_UNORM
:
914 return PIPE_FORMAT_R8G8B8X8_SRGB
;
915 case PIPE_FORMAT_DXT1_RGB
:
916 return PIPE_FORMAT_DXT1_SRGB
;
917 case PIPE_FORMAT_DXT1_RGBA
:
918 return PIPE_FORMAT_DXT1_SRGBA
;
919 case PIPE_FORMAT_DXT3_RGBA
:
920 return PIPE_FORMAT_DXT3_SRGBA
;
921 case PIPE_FORMAT_DXT5_RGBA
:
922 return PIPE_FORMAT_DXT5_SRGBA
;
923 case PIPE_FORMAT_B5G6R5_UNORM
:
924 return PIPE_FORMAT_B5G6R5_SRGB
;
925 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
926 return PIPE_FORMAT_BPTC_SRGBA
;
928 return PIPE_FORMAT_NONE
;
933 * Given an sRGB format, return the corresponding linear colorspace format.
934 * For non sRGB formats, return the format unchanged.
936 static inline enum pipe_format
937 util_format_linear(enum pipe_format format
)
940 case PIPE_FORMAT_L8_SRGB
:
941 return PIPE_FORMAT_L8_UNORM
;
942 case PIPE_FORMAT_L8A8_SRGB
:
943 return PIPE_FORMAT_L8A8_UNORM
;
944 case PIPE_FORMAT_R8G8B8_SRGB
:
945 return PIPE_FORMAT_R8G8B8_UNORM
;
946 case PIPE_FORMAT_A8B8G8R8_SRGB
:
947 return PIPE_FORMAT_A8B8G8R8_UNORM
;
948 case PIPE_FORMAT_X8B8G8R8_SRGB
:
949 return PIPE_FORMAT_X8B8G8R8_UNORM
;
950 case PIPE_FORMAT_B8G8R8A8_SRGB
:
951 return PIPE_FORMAT_B8G8R8A8_UNORM
;
952 case PIPE_FORMAT_B8G8R8X8_SRGB
:
953 return PIPE_FORMAT_B8G8R8X8_UNORM
;
954 case PIPE_FORMAT_A8R8G8B8_SRGB
:
955 return PIPE_FORMAT_A8R8G8B8_UNORM
;
956 case PIPE_FORMAT_X8R8G8B8_SRGB
:
957 return PIPE_FORMAT_X8R8G8B8_UNORM
;
958 case PIPE_FORMAT_R8G8B8A8_SRGB
:
959 return PIPE_FORMAT_R8G8B8A8_UNORM
;
960 case PIPE_FORMAT_R8G8B8X8_SRGB
:
961 return PIPE_FORMAT_R8G8B8X8_UNORM
;
962 case PIPE_FORMAT_DXT1_SRGB
:
963 return PIPE_FORMAT_DXT1_RGB
;
964 case PIPE_FORMAT_DXT1_SRGBA
:
965 return PIPE_FORMAT_DXT1_RGBA
;
966 case PIPE_FORMAT_DXT3_SRGBA
:
967 return PIPE_FORMAT_DXT3_RGBA
;
968 case PIPE_FORMAT_DXT5_SRGBA
:
969 return PIPE_FORMAT_DXT5_RGBA
;
970 case PIPE_FORMAT_B5G6R5_SRGB
:
971 return PIPE_FORMAT_B5G6R5_UNORM
;
972 case PIPE_FORMAT_BPTC_SRGBA
:
973 return PIPE_FORMAT_BPTC_RGBA_UNORM
;
980 * Given a depth-stencil format, return the corresponding stencil-only format.
981 * For stencil-only formats, return the format unchanged.
983 static inline enum pipe_format
984 util_format_stencil_only(enum pipe_format format
)
987 /* mask out the depth component */
988 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
989 return PIPE_FORMAT_X24S8_UINT
;
990 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
991 return PIPE_FORMAT_S8X24_UINT
;
992 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
993 return PIPE_FORMAT_X32_S8X24_UINT
;
995 /* stencil only formats */
996 case PIPE_FORMAT_X24S8_UINT
:
997 case PIPE_FORMAT_S8X24_UINT
:
998 case PIPE_FORMAT_X32_S8X24_UINT
:
999 case PIPE_FORMAT_S8_UINT
:
1004 return PIPE_FORMAT_NONE
;
1009 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1010 * This is identity for non-intensity formats.
1012 static inline enum pipe_format
1013 util_format_intensity_to_red(enum pipe_format format
)
1016 case PIPE_FORMAT_I8_UNORM
:
1017 return PIPE_FORMAT_R8_UNORM
;
1018 case PIPE_FORMAT_I8_SNORM
:
1019 return PIPE_FORMAT_R8_SNORM
;
1020 case PIPE_FORMAT_I16_UNORM
:
1021 return PIPE_FORMAT_R16_UNORM
;
1022 case PIPE_FORMAT_I16_SNORM
:
1023 return PIPE_FORMAT_R16_SNORM
;
1024 case PIPE_FORMAT_I16_FLOAT
:
1025 return PIPE_FORMAT_R16_FLOAT
;
1026 case PIPE_FORMAT_I32_FLOAT
:
1027 return PIPE_FORMAT_R32_FLOAT
;
1028 case PIPE_FORMAT_I8_UINT
:
1029 return PIPE_FORMAT_R8_UINT
;
1030 case PIPE_FORMAT_I8_SINT
:
1031 return PIPE_FORMAT_R8_SINT
;
1032 case PIPE_FORMAT_I16_UINT
:
1033 return PIPE_FORMAT_R16_UINT
;
1034 case PIPE_FORMAT_I16_SINT
:
1035 return PIPE_FORMAT_R16_SINT
;
1036 case PIPE_FORMAT_I32_UINT
:
1037 return PIPE_FORMAT_R32_UINT
;
1038 case PIPE_FORMAT_I32_SINT
:
1039 return PIPE_FORMAT_R32_SINT
;
1041 assert(!util_format_is_intensity(format
));
1047 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1048 * This is identity for non-luminance formats.
1050 static inline enum pipe_format
1051 util_format_luminance_to_red(enum pipe_format format
)
1054 case PIPE_FORMAT_L8_UNORM
:
1055 return PIPE_FORMAT_R8_UNORM
;
1056 case PIPE_FORMAT_L8_SNORM
:
1057 return PIPE_FORMAT_R8_SNORM
;
1058 case PIPE_FORMAT_L16_UNORM
:
1059 return PIPE_FORMAT_R16_UNORM
;
1060 case PIPE_FORMAT_L16_SNORM
:
1061 return PIPE_FORMAT_R16_SNORM
;
1062 case PIPE_FORMAT_L16_FLOAT
:
1063 return PIPE_FORMAT_R16_FLOAT
;
1064 case PIPE_FORMAT_L32_FLOAT
:
1065 return PIPE_FORMAT_R32_FLOAT
;
1066 case PIPE_FORMAT_L8_UINT
:
1067 return PIPE_FORMAT_R8_UINT
;
1068 case PIPE_FORMAT_L8_SINT
:
1069 return PIPE_FORMAT_R8_SINT
;
1070 case PIPE_FORMAT_L16_UINT
:
1071 return PIPE_FORMAT_R16_UINT
;
1072 case PIPE_FORMAT_L16_SINT
:
1073 return PIPE_FORMAT_R16_SINT
;
1074 case PIPE_FORMAT_L32_UINT
:
1075 return PIPE_FORMAT_R32_UINT
;
1076 case PIPE_FORMAT_L32_SINT
:
1077 return PIPE_FORMAT_R32_SINT
;
1079 case PIPE_FORMAT_LATC1_UNORM
:
1080 return PIPE_FORMAT_RGTC1_UNORM
;
1081 case PIPE_FORMAT_LATC1_SNORM
:
1082 return PIPE_FORMAT_RGTC1_SNORM
;
1084 case PIPE_FORMAT_L4A4_UNORM
:
1085 return PIPE_FORMAT_R4A4_UNORM
;
1087 case PIPE_FORMAT_L8A8_UNORM
:
1088 return PIPE_FORMAT_R8A8_UNORM
;
1089 case PIPE_FORMAT_L8A8_SNORM
:
1090 return PIPE_FORMAT_R8A8_SNORM
;
1091 case PIPE_FORMAT_L16A16_UNORM
:
1092 return PIPE_FORMAT_R16A16_UNORM
;
1093 case PIPE_FORMAT_L16A16_SNORM
:
1094 return PIPE_FORMAT_R16A16_SNORM
;
1095 case PIPE_FORMAT_L16A16_FLOAT
:
1096 return PIPE_FORMAT_R16A16_FLOAT
;
1097 case PIPE_FORMAT_L32A32_FLOAT
:
1098 return PIPE_FORMAT_R32A32_FLOAT
;
1099 case PIPE_FORMAT_L8A8_UINT
:
1100 return PIPE_FORMAT_R8A8_UINT
;
1101 case PIPE_FORMAT_L8A8_SINT
:
1102 return PIPE_FORMAT_R8A8_SINT
;
1103 case PIPE_FORMAT_L16A16_UINT
:
1104 return PIPE_FORMAT_R16A16_UINT
;
1105 case PIPE_FORMAT_L16A16_SINT
:
1106 return PIPE_FORMAT_R16A16_SINT
;
1107 case PIPE_FORMAT_L32A32_UINT
:
1108 return PIPE_FORMAT_R32A32_UINT
;
1109 case PIPE_FORMAT_L32A32_SINT
:
1110 return PIPE_FORMAT_R32A32_SINT
;
1112 /* We don't have compressed red-alpha variants for these. */
1113 case PIPE_FORMAT_LATC2_UNORM
:
1114 case PIPE_FORMAT_LATC2_SNORM
:
1115 return PIPE_FORMAT_NONE
;
1118 assert(!util_format_is_luminance(format
) &&
1119 !util_format_is_luminance_alpha(format
));
1125 * Return the number of components stored.
1126 * Formats with block size != 1x1 will always have 1 component (the block).
1128 static inline unsigned
1129 util_format_get_nr_components(enum pipe_format format
)
1131 const struct util_format_description
*desc
= util_format_description(format
);
1132 return desc
->nr_channels
;
1136 * Return the index of the first non-void channel
1137 * -1 if no non-void channels
1140 util_format_get_first_non_void_channel(enum pipe_format format
)
1142 const struct util_format_description
*desc
= util_format_description(format
);
1145 for (i
= 0; i
< 4; i
++)
1146 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1156 * Format access functions.
1160 util_format_read_4f(enum pipe_format format
,
1161 float *dst
, unsigned dst_stride
,
1162 const void *src
, unsigned src_stride
,
1163 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1166 util_format_write_4f(enum pipe_format format
,
1167 const float *src
, unsigned src_stride
,
1168 void *dst
, unsigned dst_stride
,
1169 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1172 util_format_read_4ub(enum pipe_format format
,
1173 uint8_t *dst
, unsigned dst_stride
,
1174 const void *src
, unsigned src_stride
,
1175 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1178 util_format_write_4ub(enum pipe_format format
,
1179 const uint8_t *src
, unsigned src_stride
,
1180 void *dst
, unsigned dst_stride
,
1181 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1184 util_format_read_4ui(enum pipe_format format
,
1185 unsigned *dst
, unsigned dst_stride
,
1186 const void *src
, unsigned src_stride
,
1187 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1190 util_format_write_4ui(enum pipe_format format
,
1191 const unsigned int *src
, unsigned src_stride
,
1192 void *dst
, unsigned dst_stride
,
1193 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1196 util_format_read_4i(enum pipe_format format
,
1197 int *dst
, unsigned dst_stride
,
1198 const void *src
, unsigned src_stride
,
1199 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1202 util_format_write_4i(enum pipe_format format
,
1203 const int *src
, unsigned src_stride
,
1204 void *dst
, unsigned dst_stride
,
1205 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1208 * Generic format conversion;
1212 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1215 util_format_translate(enum pipe_format dst_format
,
1216 void *dst
, unsigned dst_stride
,
1217 unsigned dst_x
, unsigned dst_y
,
1218 enum pipe_format src_format
,
1219 const void *src
, unsigned src_stride
,
1220 unsigned src_x
, unsigned src_y
,
1221 unsigned width
, unsigned height
);
1224 * Swizzle operations.
1227 /* Compose two sets of swizzles.
1228 * If V is a 4D vector and the function parameters represent functions that
1229 * swizzle vector components, this holds:
1230 * swz2(swz1(V)) = dst(V)
1232 void util_format_compose_swizzles(const unsigned char swz1
[4],
1233 const unsigned char swz2
[4],
1234 unsigned char dst
[4]);
1236 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1237 * to \param src and store the result in \param dst.
1238 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1240 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1241 const union pipe_color_union
*src
,
1242 const unsigned char swz
[4],
1243 const boolean is_integer
);
1245 void util_format_swizzle_4f(float *dst
, const float *src
,
1246 const unsigned char swz
[4]);
1248 void util_format_unswizzle_4f(float *dst
, const float *src
,
1249 const unsigned char swz
[4]);
1255 #endif /* ! U_FORMAT_H */