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 * Everything else that doesn't fit in any of the above layouts.
84 UTIL_FORMAT_LAYOUT_OTHER
= 7
88 struct util_format_block
90 /** Block width in pixels */
93 /** Block height in pixels */
96 /** Block size in bits */
101 enum util_format_type
{
102 UTIL_FORMAT_TYPE_VOID
= 0,
103 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
104 UTIL_FORMAT_TYPE_SIGNED
= 2,
105 UTIL_FORMAT_TYPE_FIXED
= 3,
106 UTIL_FORMAT_TYPE_FLOAT
= 4
110 enum util_format_swizzle
{
111 UTIL_FORMAT_SWIZZLE_X
= 0,
112 UTIL_FORMAT_SWIZZLE_Y
= 1,
113 UTIL_FORMAT_SWIZZLE_Z
= 2,
114 UTIL_FORMAT_SWIZZLE_W
= 3,
115 UTIL_FORMAT_SWIZZLE_0
= 4,
116 UTIL_FORMAT_SWIZZLE_1
= 5,
117 UTIL_FORMAT_SWIZZLE_NONE
= 6,
118 UTIL_FORMAT_SWIZZLE_MAX
= 7 /**< Number of enums counter (must be last) */
122 enum util_format_colorspace
{
123 UTIL_FORMAT_COLORSPACE_RGB
= 0,
124 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
125 UTIL_FORMAT_COLORSPACE_YUV
= 2,
126 UTIL_FORMAT_COLORSPACE_ZS
= 3
130 struct util_format_channel_description
132 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
133 unsigned normalized
:1;
134 unsigned pure_integer
:1;
135 unsigned size
:9; /**< bits per channel */
136 unsigned shift
:16; /** number of bits from lsb */
140 struct util_format_description
142 enum pipe_format format
;
147 * Short name, striped of the prefix, lower case.
149 const char *short_name
;
152 * Pixel block dimensions.
154 struct util_format_block block
;
156 enum util_format_layout layout
;
159 * The number of channels.
161 unsigned nr_channels
:3;
164 * Whether all channels have the same number of (whole) bytes and type.
169 * Whether the pixel format can be described as a bitfield structure.
172 * - pixel depth must be 8, 16, or 32 bits;
173 * - all channels must be unsigned, signed, or void
175 unsigned is_bitmask
:1;
178 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
183 * Input channel description, in the order XYZW.
185 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
187 * If each channel is accessed as an individual N-byte value, X is always
188 * at the lowest address in memory, Y is always next, and so on. For all
189 * currently-defined formats, the N-byte value has native endianness.
191 * If instead a group of channels is accessed as a single N-byte value,
192 * the order of the channels within that value depends on endianness.
193 * For big-endian targets, X is the most significant subvalue,
194 * otherwise it is the least significant one.
196 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
199 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
200 * big-endian: X Yu Ym Yl
202 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
206 * little-endian: YYYXXXXX WZZZZZYY
207 * big-endian: XXXXXYYY YYZZZZZW
209 struct util_format_channel_description channel
[4];
212 * Output channel swizzle.
214 * The order is either:
218 * depending on the colorspace.
220 unsigned char swizzle
[4];
223 * Colorspace transformation.
225 enum util_format_colorspace colorspace
;
228 * Unpack pixel blocks to R8G8B8A8_UNORM.
229 * Note: strides are in bytes.
231 * Only defined for non-depth-stencil formats.
234 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
235 const uint8_t *src
, unsigned src_stride
,
236 unsigned width
, unsigned height
);
239 * Pack pixel blocks from R8G8B8A8_UNORM.
240 * Note: strides are in bytes.
242 * Only defined for non-depth-stencil formats.
245 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
246 const uint8_t *src
, unsigned src_stride
,
247 unsigned width
, unsigned height
);
250 * Fetch a single pixel (i, j) from a block.
252 * XXX: Only defined for a very few select formats.
255 (*fetch_rgba_8unorm
)(uint8_t *dst
,
257 unsigned i
, unsigned j
);
260 * Unpack pixel blocks to R32G32B32A32_FLOAT.
261 * Note: strides are in bytes.
263 * Only defined for non-depth-stencil formats.
266 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
267 const uint8_t *src
, unsigned src_stride
,
268 unsigned width
, unsigned height
);
271 * Pack pixel blocks from R32G32B32A32_FLOAT.
272 * Note: strides are in bytes.
274 * Only defined for non-depth-stencil formats.
277 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
278 const float *src
, unsigned src_stride
,
279 unsigned width
, unsigned height
);
282 * Fetch a single pixel (i, j) from a block.
284 * Only defined for non-depth-stencil and non-integer formats.
287 (*fetch_rgba_float
)(float *dst
,
289 unsigned i
, unsigned j
);
292 * Unpack pixels to Z32_UNORM.
293 * Note: strides are in bytes.
295 * Only defined for depth formats.
298 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
299 const uint8_t *src
, unsigned src_stride
,
300 unsigned width
, unsigned height
);
303 * Pack pixels from Z32_FLOAT.
304 * Note: strides are in bytes.
306 * Only defined for depth formats.
309 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
310 const uint32_t *src
, unsigned src_stride
,
311 unsigned width
, unsigned height
);
314 * Unpack pixels to Z32_FLOAT.
315 * Note: strides are in bytes.
317 * Only defined for depth formats.
320 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
321 const uint8_t *src
, unsigned src_stride
,
322 unsigned width
, unsigned height
);
325 * Pack pixels from Z32_FLOAT.
326 * Note: strides are in bytes.
328 * Only defined for depth formats.
331 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
332 const float *src
, unsigned src_stride
,
333 unsigned width
, unsigned height
);
336 * Unpack pixels to S8_UINT.
337 * Note: strides are in bytes.
339 * Only defined for stencil formats.
342 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
343 const uint8_t *src
, unsigned src_stride
,
344 unsigned width
, unsigned height
);
347 * Pack pixels from S8_UINT.
348 * Note: strides are in bytes.
350 * Only defined for stencil formats.
353 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
354 const uint8_t *src
, unsigned src_stride
,
355 unsigned width
, unsigned height
);
358 * Unpack pixel blocks to R32G32B32A32_UINT.
359 * Note: strides are in bytes.
361 * Only defined for INT formats.
364 (*unpack_rgba_uint
)(uint32_t *dst
, unsigned dst_stride
,
365 const uint8_t *src
, unsigned src_stride
,
366 unsigned width
, unsigned height
);
369 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
370 const uint32_t *src
, unsigned src_stride
,
371 unsigned width
, unsigned height
);
374 * Unpack pixel blocks to R32G32B32A32_SINT.
375 * Note: strides are in bytes.
377 * Only defined for INT formats.
380 (*unpack_rgba_sint
)(int32_t *dst
, unsigned dst_stride
,
381 const uint8_t *src
, unsigned src_stride
,
382 unsigned width
, unsigned height
);
385 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
386 const int32_t *src
, unsigned src_stride
,
387 unsigned width
, unsigned height
);
390 * Fetch a single pixel (i, j) from a block.
392 * Only defined for unsigned (pure) integer formats.
395 (*fetch_rgba_uint
)(uint32_t *dst
,
397 unsigned i
, unsigned j
);
400 * Fetch a single pixel (i, j) from a block.
402 * Only defined for signed (pure) integer formats.
405 (*fetch_rgba_sint
)(int32_t *dst
,
407 unsigned i
, unsigned j
);
411 extern const struct util_format_description
412 util_format_description_table
[];
415 const struct util_format_description
*
416 util_format_description(enum pipe_format format
);
420 * Format query functions.
423 static INLINE
const char *
424 util_format_name(enum pipe_format format
)
426 const struct util_format_description
*desc
= util_format_description(format
);
430 return "PIPE_FORMAT_???";
436 static INLINE
const char *
437 util_format_short_name(enum pipe_format format
)
439 const struct util_format_description
*desc
= util_format_description(format
);
446 return desc
->short_name
;
450 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
452 static INLINE boolean
453 util_format_is_plain(enum pipe_format format
)
455 const struct util_format_description
*desc
= util_format_description(format
);
461 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
464 static INLINE boolean
465 util_format_is_compressed(enum pipe_format format
)
467 const struct util_format_description
*desc
= util_format_description(format
);
474 switch (desc
->layout
) {
475 case UTIL_FORMAT_LAYOUT_S3TC
:
476 case UTIL_FORMAT_LAYOUT_RGTC
:
477 case UTIL_FORMAT_LAYOUT_ETC
:
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_srgb(enum pipe_format format
)
501 const struct util_format_description
*desc
= util_format_description(format
);
502 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
505 static INLINE boolean
506 util_format_has_depth(const struct util_format_description
*desc
)
508 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
509 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
512 static INLINE boolean
513 util_format_has_stencil(const struct util_format_description
*desc
)
515 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
516 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
519 static INLINE boolean
520 util_format_is_depth_or_stencil(enum pipe_format format
)
522 const struct util_format_description
*desc
= util_format_description(format
);
529 return util_format_has_depth(desc
) ||
530 util_format_has_stencil(desc
);
533 static INLINE boolean
534 util_format_is_depth_and_stencil(enum pipe_format format
)
536 const struct util_format_description
*desc
= util_format_description(format
);
543 return util_format_has_depth(desc
) &&
544 util_format_has_stencil(desc
);
549 * Calculates the depth format type based upon the incoming format description.
551 static INLINE
unsigned
552 util_get_depth_format_type(const struct util_format_description
*desc
)
554 unsigned depth_channel
= desc
->swizzle
[0];
555 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
556 depth_channel
!= UTIL_FORMAT_SWIZZLE_NONE
) {
557 return desc
->channel
[depth_channel
].type
;
559 return UTIL_FORMAT_TYPE_VOID
;
565 * Calculates the MRD for the depth format. MRD is used in depth bias
566 * for UNORM and unbound depth buffers. When the depth buffer is floating
567 * point, the depth bias calculation does not use the MRD. However, the
568 * default MRD will be 1.0 / ((1 << 24) - 1).
571 util_get_depth_format_mrd(const struct util_format_description
*desc
);
575 * Return whether this is an RGBA, Z, S, or combined ZS format.
576 * Useful for initializing pipe_blit_info::mask.
578 static INLINE
unsigned
579 util_format_get_mask(enum pipe_format format
)
581 const struct util_format_description
*desc
=
582 util_format_description(format
);
587 if (util_format_has_depth(desc
)) {
588 if (util_format_has_stencil(desc
)) {
594 if (util_format_has_stencil(desc
)) {
597 return PIPE_MASK_RGBA
;
603 * Give the RGBA colormask of the channels that can be represented in this
606 * That is, the channels whose values are preserved.
608 static INLINE
unsigned
609 util_format_colormask(const struct util_format_description
*desc
)
614 switch (desc
->colorspace
) {
615 case UTIL_FORMAT_COLORSPACE_RGB
:
616 case UTIL_FORMAT_COLORSPACE_SRGB
:
617 case UTIL_FORMAT_COLORSPACE_YUV
:
619 for (chan
= 0; chan
< 4; ++chan
) {
620 if (desc
->swizzle
[chan
] < 4) {
621 colormask
|= (1 << chan
);
625 case UTIL_FORMAT_COLORSPACE_ZS
:
635 * Checks if color mask covers every channel for the specified format
637 * @param desc a format description to check colormask with
638 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
640 static INLINE boolean
641 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
643 return (~colormask
& util_format_colormask(desc
)) == 0;
648 util_format_is_float(enum pipe_format format
);
652 util_format_has_alpha(enum pipe_format format
);
656 util_format_is_luminance(enum pipe_format format
);
660 util_format_is_luminance_alpha(enum pipe_format format
);
664 util_format_is_intensity(enum pipe_format format
);
667 util_format_is_subsampled_422(enum pipe_format format
);
670 util_format_is_pure_integer(enum pipe_format format
);
673 util_format_is_pure_sint(enum pipe_format format
);
676 util_format_is_pure_uint(enum pipe_format format
);
679 util_format_is_snorm(enum pipe_format format
);
682 * Check if the src format can be blitted to the destination format with
683 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
687 util_is_format_compatible(const struct util_format_description
*src_desc
,
688 const struct util_format_description
*dst_desc
);
691 * Whether the format is supported by Gallium for the given bindings.
692 * This covers S3TC textures and floating-point render targets.
695 util_format_is_supported(enum pipe_format format
, unsigned bind
);
698 * Whether this format is a rgab8 variant.
700 * That is, any format that matches the
702 * PIPE_FORMAT_?8?8?8?8_UNORM
704 static INLINE boolean
705 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
709 if(desc
->block
.width
!= 1 ||
710 desc
->block
.height
!= 1 ||
711 desc
->block
.bits
!= 32)
714 for(chan
= 0; chan
< 4; ++chan
) {
715 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
716 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
718 if(desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
719 !desc
->channel
[chan
].normalized
)
721 if(desc
->channel
[chan
].size
!= 8)
730 * Return total bits needed for the pixel format per block.
733 util_format_get_blocksizebits(enum pipe_format format
)
735 const struct util_format_description
*desc
= util_format_description(format
);
742 return desc
->block
.bits
;
746 * Return bytes per block (not pixel) for the given format.
749 util_format_get_blocksize(enum pipe_format format
)
751 uint bits
= util_format_get_blocksizebits(format
);
752 uint bytes
= bits
/ 8;
754 assert(bits
% 8 == 0);
764 util_format_get_blockwidth(enum pipe_format format
)
766 const struct util_format_description
*desc
= util_format_description(format
);
773 return desc
->block
.width
;
777 util_format_get_blockheight(enum pipe_format format
)
779 const struct util_format_description
*desc
= util_format_description(format
);
786 return desc
->block
.height
;
789 static INLINE
unsigned
790 util_format_get_nblocksx(enum pipe_format format
,
793 unsigned blockwidth
= util_format_get_blockwidth(format
);
794 return (x
+ blockwidth
- 1) / blockwidth
;
797 static INLINE
unsigned
798 util_format_get_nblocksy(enum pipe_format format
,
801 unsigned blockheight
= util_format_get_blockheight(format
);
802 return (y
+ blockheight
- 1) / blockheight
;
805 static INLINE
unsigned
806 util_format_get_nblocks(enum pipe_format format
,
810 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
814 util_format_get_stride(enum pipe_format format
,
817 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
821 util_format_get_2d_size(enum pipe_format format
,
825 return util_format_get_nblocksy(format
, height
) * stride
;
829 util_format_get_component_bits(enum pipe_format format
,
830 enum util_format_colorspace colorspace
,
833 const struct util_format_description
*desc
= util_format_description(format
);
834 enum util_format_colorspace desc_colorspace
;
841 assert(component
< 4);
843 /* Treat RGB and SRGB as equivalent. */
844 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
845 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
847 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
848 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
850 desc_colorspace
= desc
->colorspace
;
853 if (desc_colorspace
!= colorspace
) {
857 switch (desc
->swizzle
[component
]) {
858 case UTIL_FORMAT_SWIZZLE_X
:
859 return desc
->channel
[0].size
;
860 case UTIL_FORMAT_SWIZZLE_Y
:
861 return desc
->channel
[1].size
;
862 case UTIL_FORMAT_SWIZZLE_Z
:
863 return desc
->channel
[2].size
;
864 case UTIL_FORMAT_SWIZZLE_W
:
865 return desc
->channel
[3].size
;
872 * Given a linear RGB colorspace format, return the corresponding SRGB
873 * format, or PIPE_FORMAT_NONE if none.
875 static INLINE
enum pipe_format
876 util_format_srgb(enum pipe_format format
)
878 if (util_format_is_srgb(format
))
882 case PIPE_FORMAT_L8_UNORM
:
883 return PIPE_FORMAT_L8_SRGB
;
884 case PIPE_FORMAT_L8A8_UNORM
:
885 return PIPE_FORMAT_L8A8_SRGB
;
886 case PIPE_FORMAT_R8G8B8_UNORM
:
887 return PIPE_FORMAT_R8G8B8_SRGB
;
888 case PIPE_FORMAT_A8B8G8R8_UNORM
:
889 return PIPE_FORMAT_A8B8G8R8_SRGB
;
890 case PIPE_FORMAT_X8B8G8R8_UNORM
:
891 return PIPE_FORMAT_X8B8G8R8_SRGB
;
892 case PIPE_FORMAT_B8G8R8A8_UNORM
:
893 return PIPE_FORMAT_B8G8R8A8_SRGB
;
894 case PIPE_FORMAT_B8G8R8X8_UNORM
:
895 return PIPE_FORMAT_B8G8R8X8_SRGB
;
896 case PIPE_FORMAT_A8R8G8B8_UNORM
:
897 return PIPE_FORMAT_A8R8G8B8_SRGB
;
898 case PIPE_FORMAT_X8R8G8B8_UNORM
:
899 return PIPE_FORMAT_X8R8G8B8_SRGB
;
900 case PIPE_FORMAT_R8G8B8A8_UNORM
:
901 return PIPE_FORMAT_R8G8B8A8_SRGB
;
902 case PIPE_FORMAT_R8G8B8X8_UNORM
:
903 return PIPE_FORMAT_R8G8B8X8_SRGB
;
904 case PIPE_FORMAT_DXT1_RGB
:
905 return PIPE_FORMAT_DXT1_SRGB
;
906 case PIPE_FORMAT_DXT1_RGBA
:
907 return PIPE_FORMAT_DXT1_SRGBA
;
908 case PIPE_FORMAT_DXT3_RGBA
:
909 return PIPE_FORMAT_DXT3_SRGBA
;
910 case PIPE_FORMAT_DXT5_RGBA
:
911 return PIPE_FORMAT_DXT5_SRGBA
;
912 case PIPE_FORMAT_B5G6R5_UNORM
:
913 return PIPE_FORMAT_B5G6R5_SRGB
;
915 return PIPE_FORMAT_NONE
;
920 * Given an sRGB format, return the corresponding linear colorspace format.
921 * For non sRGB formats, return the format unchanged.
923 static INLINE
enum pipe_format
924 util_format_linear(enum pipe_format format
)
927 case PIPE_FORMAT_L8_SRGB
:
928 return PIPE_FORMAT_L8_UNORM
;
929 case PIPE_FORMAT_L8A8_SRGB
:
930 return PIPE_FORMAT_L8A8_UNORM
;
931 case PIPE_FORMAT_R8G8B8_SRGB
:
932 return PIPE_FORMAT_R8G8B8_UNORM
;
933 case PIPE_FORMAT_A8B8G8R8_SRGB
:
934 return PIPE_FORMAT_A8B8G8R8_UNORM
;
935 case PIPE_FORMAT_X8B8G8R8_SRGB
:
936 return PIPE_FORMAT_X8B8G8R8_UNORM
;
937 case PIPE_FORMAT_B8G8R8A8_SRGB
:
938 return PIPE_FORMAT_B8G8R8A8_UNORM
;
939 case PIPE_FORMAT_B8G8R8X8_SRGB
:
940 return PIPE_FORMAT_B8G8R8X8_UNORM
;
941 case PIPE_FORMAT_A8R8G8B8_SRGB
:
942 return PIPE_FORMAT_A8R8G8B8_UNORM
;
943 case PIPE_FORMAT_X8R8G8B8_SRGB
:
944 return PIPE_FORMAT_X8R8G8B8_UNORM
;
945 case PIPE_FORMAT_R8G8B8A8_SRGB
:
946 return PIPE_FORMAT_R8G8B8A8_UNORM
;
947 case PIPE_FORMAT_R8G8B8X8_SRGB
:
948 return PIPE_FORMAT_R8G8B8X8_UNORM
;
949 case PIPE_FORMAT_DXT1_SRGB
:
950 return PIPE_FORMAT_DXT1_RGB
;
951 case PIPE_FORMAT_DXT1_SRGBA
:
952 return PIPE_FORMAT_DXT1_RGBA
;
953 case PIPE_FORMAT_DXT3_SRGBA
:
954 return PIPE_FORMAT_DXT3_RGBA
;
955 case PIPE_FORMAT_DXT5_SRGBA
:
956 return PIPE_FORMAT_DXT5_RGBA
;
957 case PIPE_FORMAT_B5G6R5_SRGB
:
958 return PIPE_FORMAT_B5G6R5_UNORM
;
965 * Given a depth-stencil format, return the corresponding stencil-only format.
966 * For stencil-only formats, return the format unchanged.
968 static INLINE
enum pipe_format
969 util_format_stencil_only(enum pipe_format format
)
972 /* mask out the depth component */
973 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
974 return PIPE_FORMAT_X24S8_UINT
;
975 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
976 return PIPE_FORMAT_S8X24_UINT
;
977 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
978 return PIPE_FORMAT_X32_S8X24_UINT
;
980 /* stencil only formats */
981 case PIPE_FORMAT_X24S8_UINT
:
982 case PIPE_FORMAT_S8X24_UINT
:
983 case PIPE_FORMAT_X32_S8X24_UINT
:
984 case PIPE_FORMAT_S8_UINT
:
989 return PIPE_FORMAT_NONE
;
994 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
995 * This is identity for non-intensity formats.
997 static INLINE
enum pipe_format
998 util_format_intensity_to_red(enum pipe_format format
)
1001 case PIPE_FORMAT_I8_UNORM
:
1002 return PIPE_FORMAT_R8_UNORM
;
1003 case PIPE_FORMAT_I8_SNORM
:
1004 return PIPE_FORMAT_R8_SNORM
;
1005 case PIPE_FORMAT_I16_UNORM
:
1006 return PIPE_FORMAT_R16_UNORM
;
1007 case PIPE_FORMAT_I16_SNORM
:
1008 return PIPE_FORMAT_R16_SNORM
;
1009 case PIPE_FORMAT_I16_FLOAT
:
1010 return PIPE_FORMAT_R16_FLOAT
;
1011 case PIPE_FORMAT_I32_FLOAT
:
1012 return PIPE_FORMAT_R32_FLOAT
;
1013 case PIPE_FORMAT_I8_UINT
:
1014 return PIPE_FORMAT_R8_UINT
;
1015 case PIPE_FORMAT_I8_SINT
:
1016 return PIPE_FORMAT_R8_SINT
;
1017 case PIPE_FORMAT_I16_UINT
:
1018 return PIPE_FORMAT_R16_UINT
;
1019 case PIPE_FORMAT_I16_SINT
:
1020 return PIPE_FORMAT_R16_SINT
;
1021 case PIPE_FORMAT_I32_UINT
:
1022 return PIPE_FORMAT_R32_UINT
;
1023 case PIPE_FORMAT_I32_SINT
:
1024 return PIPE_FORMAT_R32_SINT
;
1026 assert(!util_format_is_intensity(format
));
1032 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1033 * This is identity for non-luminance formats.
1035 static INLINE
enum pipe_format
1036 util_format_luminance_to_red(enum pipe_format format
)
1039 case PIPE_FORMAT_L8_UNORM
:
1040 return PIPE_FORMAT_R8_UNORM
;
1041 case PIPE_FORMAT_L8_SNORM
:
1042 return PIPE_FORMAT_R8_SNORM
;
1043 case PIPE_FORMAT_L16_UNORM
:
1044 return PIPE_FORMAT_R16_UNORM
;
1045 case PIPE_FORMAT_L16_SNORM
:
1046 return PIPE_FORMAT_R16_SNORM
;
1047 case PIPE_FORMAT_L16_FLOAT
:
1048 return PIPE_FORMAT_R16_FLOAT
;
1049 case PIPE_FORMAT_L32_FLOAT
:
1050 return PIPE_FORMAT_R32_FLOAT
;
1051 case PIPE_FORMAT_L8_UINT
:
1052 return PIPE_FORMAT_R8_UINT
;
1053 case PIPE_FORMAT_L8_SINT
:
1054 return PIPE_FORMAT_R8_SINT
;
1055 case PIPE_FORMAT_L16_UINT
:
1056 return PIPE_FORMAT_R16_UINT
;
1057 case PIPE_FORMAT_L16_SINT
:
1058 return PIPE_FORMAT_R16_SINT
;
1059 case PIPE_FORMAT_L32_UINT
:
1060 return PIPE_FORMAT_R32_UINT
;
1061 case PIPE_FORMAT_L32_SINT
:
1062 return PIPE_FORMAT_R32_SINT
;
1064 case PIPE_FORMAT_LATC1_UNORM
:
1065 return PIPE_FORMAT_RGTC1_UNORM
;
1066 case PIPE_FORMAT_LATC1_SNORM
:
1067 return PIPE_FORMAT_RGTC1_SNORM
;
1069 case PIPE_FORMAT_L4A4_UNORM
:
1070 return PIPE_FORMAT_R4A4_UNORM
;
1072 case PIPE_FORMAT_L8A8_UNORM
:
1073 return PIPE_FORMAT_R8A8_UNORM
;
1074 case PIPE_FORMAT_L8A8_SNORM
:
1075 return PIPE_FORMAT_R8A8_SNORM
;
1076 case PIPE_FORMAT_L16A16_UNORM
:
1077 return PIPE_FORMAT_R16A16_UNORM
;
1078 case PIPE_FORMAT_L16A16_SNORM
:
1079 return PIPE_FORMAT_R16A16_SNORM
;
1080 case PIPE_FORMAT_L16A16_FLOAT
:
1081 return PIPE_FORMAT_R16A16_FLOAT
;
1082 case PIPE_FORMAT_L32A32_FLOAT
:
1083 return PIPE_FORMAT_R32A32_FLOAT
;
1084 case PIPE_FORMAT_L8A8_UINT
:
1085 return PIPE_FORMAT_R8A8_UINT
;
1086 case PIPE_FORMAT_L8A8_SINT
:
1087 return PIPE_FORMAT_R8A8_SINT
;
1088 case PIPE_FORMAT_L16A16_UINT
:
1089 return PIPE_FORMAT_R16A16_UINT
;
1090 case PIPE_FORMAT_L16A16_SINT
:
1091 return PIPE_FORMAT_R16A16_SINT
;
1092 case PIPE_FORMAT_L32A32_UINT
:
1093 return PIPE_FORMAT_R32A32_UINT
;
1094 case PIPE_FORMAT_L32A32_SINT
:
1095 return PIPE_FORMAT_R32A32_SINT
;
1097 /* We don't have compressed red-alpha variants for these. */
1098 case PIPE_FORMAT_LATC2_UNORM
:
1099 case PIPE_FORMAT_LATC2_SNORM
:
1100 return PIPE_FORMAT_NONE
;
1103 assert(!util_format_is_luminance(format
) &&
1104 !util_format_is_luminance_alpha(format
));
1110 * Return the number of components stored.
1111 * Formats with block size != 1x1 will always have 1 component (the block).
1113 static INLINE
unsigned
1114 util_format_get_nr_components(enum pipe_format format
)
1116 const struct util_format_description
*desc
= util_format_description(format
);
1117 return desc
->nr_channels
;
1121 * Return the index of the first non-void channel
1122 * -1 if no non-void channels
1125 util_format_get_first_non_void_channel(enum pipe_format format
)
1127 const struct util_format_description
*desc
= util_format_description(format
);
1130 for (i
= 0; i
< 4; i
++)
1131 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1141 * Format access functions.
1145 util_format_read_4f(enum pipe_format format
,
1146 float *dst
, unsigned dst_stride
,
1147 const void *src
, unsigned src_stride
,
1148 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1151 util_format_write_4f(enum pipe_format format
,
1152 const float *src
, unsigned src_stride
,
1153 void *dst
, unsigned dst_stride
,
1154 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1157 util_format_read_4ub(enum pipe_format format
,
1158 uint8_t *dst
, unsigned dst_stride
,
1159 const void *src
, unsigned src_stride
,
1160 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1163 util_format_write_4ub(enum pipe_format format
,
1164 const uint8_t *src
, unsigned src_stride
,
1165 void *dst
, unsigned dst_stride
,
1166 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1169 util_format_read_4ui(enum pipe_format format
,
1170 unsigned *dst
, unsigned dst_stride
,
1171 const void *src
, unsigned src_stride
,
1172 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1175 util_format_write_4ui(enum pipe_format format
,
1176 const unsigned int *src
, unsigned src_stride
,
1177 void *dst
, unsigned dst_stride
,
1178 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1181 util_format_read_4i(enum pipe_format format
,
1182 int *dst
, unsigned dst_stride
,
1183 const void *src
, unsigned src_stride
,
1184 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1187 util_format_write_4i(enum pipe_format format
,
1188 const int *src
, unsigned src_stride
,
1189 void *dst
, unsigned dst_stride
,
1190 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1193 * Generic format conversion;
1197 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1200 util_format_translate(enum pipe_format dst_format
,
1201 void *dst
, unsigned dst_stride
,
1202 unsigned dst_x
, unsigned dst_y
,
1203 enum pipe_format src_format
,
1204 const void *src
, unsigned src_stride
,
1205 unsigned src_x
, unsigned src_y
,
1206 unsigned width
, unsigned height
);
1209 * Swizzle operations.
1212 /* Compose two sets of swizzles.
1213 * If V is a 4D vector and the function parameters represent functions that
1214 * swizzle vector components, this holds:
1215 * swz2(swz1(V)) = dst(V)
1217 void util_format_compose_swizzles(const unsigned char swz1
[4],
1218 const unsigned char swz2
[4],
1219 unsigned char dst
[4]);
1221 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1222 * to \param src and store the result in \param dst.
1223 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1225 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1226 const union pipe_color_union
*src
,
1227 const unsigned char swz
[4],
1228 const boolean is_integer
);
1230 void util_format_swizzle_4f(float *dst
, const float *src
,
1231 const unsigned char swz
[4]);
1233 void util_format_unswizzle_4f(float *dst
, const float *src
,
1234 const unsigned char swz
[4]);
1240 #endif /* ! U_FORMAT_H */