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 "util/u_debug.h"
42 * Describe how to pack/unpack pixels into/from the prescribed format.
44 * XXX: This could be renamed to something like util_format_pack, or broke down
45 * in flags inside util_format_block that said exactly what we want.
47 enum util_format_layout
{
49 * Formats with util_format_block::width == util_format_block::height == 1
50 * that can be described as an ordinary data structure.
52 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
55 * Formats with sub-sampled channels.
57 * This is for formats like YVYU where there is less than one sample per
60 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
63 * S3 Texture Compression formats.
65 UTIL_FORMAT_LAYOUT_S3TC
= 4,
68 * Red-Green Texture Compression formats.
70 UTIL_FORMAT_LAYOUT_RGTC
= 5,
73 * Ericsson Texture Compression
75 UTIL_FORMAT_LAYOUT_ETC
= 6,
78 * Everything else that doesn't fit in any of the above layouts.
80 UTIL_FORMAT_LAYOUT_OTHER
= 7
84 struct util_format_block
86 /** Block width in pixels */
89 /** Block height in pixels */
92 /** Block size in bits */
97 enum util_format_type
{
98 UTIL_FORMAT_TYPE_VOID
= 0,
99 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
100 UTIL_FORMAT_TYPE_SIGNED
= 2,
101 UTIL_FORMAT_TYPE_FIXED
= 3,
102 UTIL_FORMAT_TYPE_FLOAT
= 4
106 enum util_format_swizzle
{
107 UTIL_FORMAT_SWIZZLE_X
= 0,
108 UTIL_FORMAT_SWIZZLE_Y
= 1,
109 UTIL_FORMAT_SWIZZLE_Z
= 2,
110 UTIL_FORMAT_SWIZZLE_W
= 3,
111 UTIL_FORMAT_SWIZZLE_0
= 4,
112 UTIL_FORMAT_SWIZZLE_1
= 5,
113 UTIL_FORMAT_SWIZZLE_NONE
= 6,
114 UTIL_FORMAT_SWIZZLE_MAX
= 7 /**< Number of enums counter (must be last) */
118 enum util_format_colorspace
{
119 UTIL_FORMAT_COLORSPACE_RGB
= 0,
120 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
121 UTIL_FORMAT_COLORSPACE_YUV
= 2,
122 UTIL_FORMAT_COLORSPACE_ZS
= 3
126 struct util_format_channel_description
128 unsigned type
:5; /**< UTIL_FORMAT_TYPE_x */
129 unsigned normalized
:1;
130 unsigned pure_integer
:1;
131 unsigned size
:9; /**< bits per channel */
135 struct util_format_description
137 enum pipe_format format
;
142 * Short name, striped of the prefix, lower case.
144 const char *short_name
;
147 * Pixel block dimensions.
149 struct util_format_block block
;
151 enum util_format_layout layout
;
154 * The number of channels.
156 unsigned nr_channels
:3;
159 * Whether all channels have the same number of (whole) bytes and type.
164 * Whether the pixel format can be described as a bitfield structure.
167 * - pixel depth must be 8, 16, or 32 bits;
168 * - all channels must be unsigned, signed, or void
170 unsigned is_bitmask
:1;
173 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
178 * Input channel description.
180 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
182 struct util_format_channel_description channel
[4];
185 * Output channel swizzle.
187 * The order is either:
191 * depending on the colorspace.
193 unsigned char swizzle
[4];
196 * Colorspace transformation.
198 enum util_format_colorspace colorspace
;
201 * Unpack pixel blocks to R8G8B8A8_UNORM.
202 * Note: strides are in bytes.
204 * Only defined for non-depth-stencil formats.
207 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
208 const uint8_t *src
, unsigned src_stride
,
209 unsigned width
, unsigned height
);
212 * Pack pixel blocks from R8G8B8A8_UNORM.
213 * Note: strides are in bytes.
215 * Only defined for non-depth-stencil formats.
218 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
219 const uint8_t *src
, unsigned src_stride
,
220 unsigned width
, unsigned height
);
223 * Fetch a single pixel (i, j) from a block.
225 * XXX: Only defined for a very few select formats.
228 (*fetch_rgba_8unorm
)(uint8_t *dst
,
230 unsigned i
, unsigned j
);
233 * Unpack pixel blocks to R32G32B32A32_FLOAT.
234 * Note: strides are in bytes.
236 * Only defined for non-depth-stencil formats.
239 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
240 const uint8_t *src
, unsigned src_stride
,
241 unsigned width
, unsigned height
);
244 * Pack pixel blocks from R32G32B32A32_FLOAT.
245 * Note: strides are in bytes.
247 * Only defined for non-depth-stencil formats.
250 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
251 const float *src
, unsigned src_stride
,
252 unsigned width
, unsigned height
);
255 * Fetch a single pixel (i, j) from a block.
257 * Only defined for non-depth-stencil and non-integer formats.
260 (*fetch_rgba_float
)(float *dst
,
262 unsigned i
, unsigned j
);
265 * Unpack pixels to Z32_UNORM.
266 * Note: strides are in bytes.
268 * Only defined for depth formats.
271 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
272 const uint8_t *src
, unsigned src_stride
,
273 unsigned width
, unsigned height
);
276 * Pack pixels from Z32_FLOAT.
277 * Note: strides are in bytes.
279 * Only defined for depth formats.
282 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
283 const uint32_t *src
, unsigned src_stride
,
284 unsigned width
, unsigned height
);
287 * Unpack pixels to Z32_FLOAT.
288 * Note: strides are in bytes.
290 * Only defined for depth formats.
293 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
294 const uint8_t *src
, unsigned src_stride
,
295 unsigned width
, unsigned height
);
298 * Pack pixels from Z32_FLOAT.
299 * Note: strides are in bytes.
301 * Only defined for depth formats.
304 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
305 const float *src
, unsigned src_stride
,
306 unsigned width
, unsigned height
);
309 * Unpack pixels to S8_UINT.
310 * Note: strides are in bytes.
312 * Only defined for stencil formats.
315 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
316 const uint8_t *src
, unsigned src_stride
,
317 unsigned width
, unsigned height
);
320 * Pack pixels from S8_UINT.
321 * Note: strides are in bytes.
323 * Only defined for stencil formats.
326 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
327 const uint8_t *src
, unsigned src_stride
,
328 unsigned width
, unsigned height
);
331 * Unpack pixel blocks to R32G32B32A32_UINT.
332 * Note: strides are in bytes.
334 * Only defined for INT formats.
337 (*unpack_rgba_uint
)(unsigned *dst
, unsigned dst_stride
,
338 const uint8_t *src
, unsigned src_stride
,
339 unsigned width
, unsigned height
);
342 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
343 const unsigned *src
, unsigned src_stride
,
344 unsigned width
, unsigned height
);
347 * Unpack pixel blocks to R32G32B32A32_SINT.
348 * Note: strides are in bytes.
350 * Only defined for INT formats.
353 (*unpack_rgba_sint
)(signed *dst
, unsigned dst_stride
,
354 const uint8_t *src
, unsigned src_stride
,
355 unsigned width
, unsigned height
);
358 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
359 const int *src
, unsigned src_stride
,
360 unsigned width
, unsigned height
);
363 * Fetch a single pixel (i, j) from a block.
365 * Only defined for unsigned (pure) integer formats.
368 (*fetch_rgba_uint
)(uint32_t *dst
,
370 unsigned i
, unsigned j
);
373 * Fetch a single pixel (i, j) from a block.
375 * Only defined for signed (pure) integer formats.
378 (*fetch_rgba_sint
)(int32_t *dst
,
380 unsigned i
, unsigned j
);
384 extern const struct util_format_description
385 util_format_description_table
[];
388 const struct util_format_description
*
389 util_format_description(enum pipe_format format
);
393 * Format query functions.
396 static INLINE
const char *
397 util_format_name(enum pipe_format format
)
399 const struct util_format_description
*desc
= util_format_description(format
);
403 return "PIPE_FORMAT_???";
409 static INLINE
const char *
410 util_format_short_name(enum pipe_format format
)
412 const struct util_format_description
*desc
= util_format_description(format
);
419 return desc
->short_name
;
423 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
425 static INLINE boolean
426 util_format_is_plain(enum pipe_format format
)
428 const struct util_format_description
*desc
= util_format_description(format
);
434 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
437 static INLINE boolean
438 util_format_is_compressed(enum pipe_format format
)
440 const struct util_format_description
*desc
= util_format_description(format
);
447 switch (desc
->layout
) {
448 case UTIL_FORMAT_LAYOUT_S3TC
:
449 case UTIL_FORMAT_LAYOUT_RGTC
:
450 /* XXX add other formats in the future */
457 static INLINE boolean
458 util_format_is_s3tc(enum pipe_format format
)
460 const struct util_format_description
*desc
= util_format_description(format
);
467 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
470 static INLINE boolean
471 util_format_is_srgb(enum pipe_format format
)
473 const struct util_format_description
*desc
= util_format_description(format
);
474 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
477 static INLINE boolean
478 util_format_has_depth(const struct util_format_description
*desc
)
480 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
481 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
484 static INLINE boolean
485 util_format_has_stencil(const struct util_format_description
*desc
)
487 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
488 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
491 static INLINE boolean
492 util_format_is_depth_or_stencil(enum pipe_format format
)
494 const struct util_format_description
*desc
= util_format_description(format
);
501 return util_format_has_depth(desc
) ||
502 util_format_has_stencil(desc
);
505 static INLINE boolean
506 util_format_is_depth_and_stencil(enum pipe_format format
)
508 const struct util_format_description
*desc
= util_format_description(format
);
515 return util_format_has_depth(desc
) &&
516 util_format_has_stencil(desc
);
521 * Give the RGBA colormask of the channels that can be represented in this
524 * That is, the channels whose values are preserved.
526 static INLINE
unsigned
527 util_format_colormask(const struct util_format_description
*desc
)
532 switch (desc
->colorspace
) {
533 case UTIL_FORMAT_COLORSPACE_RGB
:
534 case UTIL_FORMAT_COLORSPACE_SRGB
:
535 case UTIL_FORMAT_COLORSPACE_YUV
:
537 for (chan
= 0; chan
< 4; ++chan
) {
538 if (desc
->swizzle
[chan
] < 4) {
539 colormask
|= (1 << chan
);
543 case UTIL_FORMAT_COLORSPACE_ZS
:
553 * Checks if color mask covers every channel for the specified format
555 * @param desc a format description to check colormask with
556 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
558 static INLINE boolean
559 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
561 return (~colormask
& util_format_colormask(desc
)) == 0;
566 util_format_is_float(enum pipe_format format
);
570 util_format_has_alpha(enum pipe_format format
);
574 util_format_is_luminance(enum pipe_format format
);
578 util_format_is_luminance_alpha(enum pipe_format format
);
582 util_format_is_intensity(enum pipe_format format
);
585 util_format_is_pure_integer(enum pipe_format format
);
588 util_format_is_pure_sint(enum pipe_format format
);
591 util_format_is_pure_uint(enum pipe_format format
);
594 * Check if the src format can be blitted to the destination format with
595 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
599 util_is_format_compatible(const struct util_format_description
*src_desc
,
600 const struct util_format_description
*dst_desc
);
603 * Whether the format is supported by Gallium for the given bindings.
604 * This covers S3TC textures and floating-point render targets.
607 util_format_is_supported(enum pipe_format format
, unsigned bind
);
610 * Whether this format is a rgab8 variant.
612 * That is, any format that matches the
614 * PIPE_FORMAT_?8?8?8?8_UNORM
616 static INLINE boolean
617 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
621 if(desc
->block
.width
!= 1 ||
622 desc
->block
.height
!= 1 ||
623 desc
->block
.bits
!= 32)
626 for(chan
= 0; chan
< 4; ++chan
) {
627 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
628 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
630 if(desc
->channel
[chan
].size
!= 8)
639 * Return total bits needed for the pixel format per block.
642 util_format_get_blocksizebits(enum pipe_format format
)
644 const struct util_format_description
*desc
= util_format_description(format
);
651 return desc
->block
.bits
;
655 * Return bytes per block (not pixel) for the given format.
658 util_format_get_blocksize(enum pipe_format format
)
660 uint bits
= util_format_get_blocksizebits(format
);
662 assert(bits
% 8 == 0);
668 util_format_get_blockwidth(enum pipe_format format
)
670 const struct util_format_description
*desc
= util_format_description(format
);
677 return desc
->block
.width
;
681 util_format_get_blockheight(enum pipe_format format
)
683 const struct util_format_description
*desc
= util_format_description(format
);
690 return desc
->block
.height
;
693 static INLINE
unsigned
694 util_format_get_nblocksx(enum pipe_format format
,
697 unsigned blockwidth
= util_format_get_blockwidth(format
);
698 return (x
+ blockwidth
- 1) / blockwidth
;
701 static INLINE
unsigned
702 util_format_get_nblocksy(enum pipe_format format
,
705 unsigned blockheight
= util_format_get_blockheight(format
);
706 return (y
+ blockheight
- 1) / blockheight
;
709 static INLINE
unsigned
710 util_format_get_nblocks(enum pipe_format format
,
714 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
718 util_format_get_stride(enum pipe_format format
,
721 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
725 util_format_get_2d_size(enum pipe_format format
,
729 return util_format_get_nblocksy(format
, height
) * stride
;
733 util_format_get_component_bits(enum pipe_format format
,
734 enum util_format_colorspace colorspace
,
737 const struct util_format_description
*desc
= util_format_description(format
);
738 enum util_format_colorspace desc_colorspace
;
745 assert(component
< 4);
747 /* Treat RGB and SRGB as equivalent. */
748 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
749 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
751 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
752 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
754 desc_colorspace
= desc
->colorspace
;
757 if (desc_colorspace
!= colorspace
) {
761 switch (desc
->swizzle
[component
]) {
762 case UTIL_FORMAT_SWIZZLE_X
:
763 return desc
->channel
[0].size
;
764 case UTIL_FORMAT_SWIZZLE_Y
:
765 return desc
->channel
[1].size
;
766 case UTIL_FORMAT_SWIZZLE_Z
:
767 return desc
->channel
[2].size
;
768 case UTIL_FORMAT_SWIZZLE_W
:
769 return desc
->channel
[3].size
;
776 * Given a linear RGB colorspace format, return the corresponding SRGB
777 * format, or PIPE_FORMAT_NONE if none.
779 static INLINE
enum pipe_format
780 util_format_srgb(enum pipe_format format
)
783 case PIPE_FORMAT_L8_UNORM
:
784 return PIPE_FORMAT_L8_SRGB
;
785 case PIPE_FORMAT_L8A8_UNORM
:
786 return PIPE_FORMAT_L8A8_SRGB
;
787 case PIPE_FORMAT_R8G8B8_UNORM
:
788 return PIPE_FORMAT_R8G8B8_SRGB
;
789 case PIPE_FORMAT_A8B8G8R8_UNORM
:
790 return PIPE_FORMAT_A8B8G8R8_SRGB
;
791 case PIPE_FORMAT_X8B8G8R8_UNORM
:
792 return PIPE_FORMAT_X8B8G8R8_SRGB
;
793 case PIPE_FORMAT_B8G8R8A8_UNORM
:
794 return PIPE_FORMAT_B8G8R8A8_SRGB
;
795 case PIPE_FORMAT_B8G8R8X8_UNORM
:
796 return PIPE_FORMAT_B8G8R8X8_SRGB
;
797 case PIPE_FORMAT_A8R8G8B8_UNORM
:
798 return PIPE_FORMAT_A8R8G8B8_SRGB
;
799 case PIPE_FORMAT_X8R8G8B8_UNORM
:
800 return PIPE_FORMAT_X8R8G8B8_SRGB
;
801 case PIPE_FORMAT_R8G8B8A8_UNORM
:
802 return PIPE_FORMAT_R8G8B8A8_SRGB
;
803 case PIPE_FORMAT_R8G8B8X8_UNORM
:
804 return PIPE_FORMAT_R8G8B8X8_SRGB
;
805 case PIPE_FORMAT_DXT1_RGB
:
806 return PIPE_FORMAT_DXT1_SRGB
;
807 case PIPE_FORMAT_DXT1_RGBA
:
808 return PIPE_FORMAT_DXT1_SRGBA
;
809 case PIPE_FORMAT_DXT3_RGBA
:
810 return PIPE_FORMAT_DXT3_SRGBA
;
811 case PIPE_FORMAT_DXT5_RGBA
:
812 return PIPE_FORMAT_DXT5_SRGBA
;
814 return PIPE_FORMAT_NONE
;
819 * Given an sRGB format, return the corresponding linear colorspace format.
820 * For non sRGB formats, return the format unchanged.
822 static INLINE
enum pipe_format
823 util_format_linear(enum pipe_format format
)
826 case PIPE_FORMAT_L8_SRGB
:
827 return PIPE_FORMAT_L8_UNORM
;
828 case PIPE_FORMAT_L8A8_SRGB
:
829 return PIPE_FORMAT_L8A8_UNORM
;
830 case PIPE_FORMAT_R8G8B8_SRGB
:
831 return PIPE_FORMAT_R8G8B8_UNORM
;
832 case PIPE_FORMAT_A8B8G8R8_SRGB
:
833 return PIPE_FORMAT_A8B8G8R8_UNORM
;
834 case PIPE_FORMAT_X8B8G8R8_SRGB
:
835 return PIPE_FORMAT_X8B8G8R8_UNORM
;
836 case PIPE_FORMAT_B8G8R8A8_SRGB
:
837 return PIPE_FORMAT_B8G8R8A8_UNORM
;
838 case PIPE_FORMAT_B8G8R8X8_SRGB
:
839 return PIPE_FORMAT_B8G8R8X8_UNORM
;
840 case PIPE_FORMAT_A8R8G8B8_SRGB
:
841 return PIPE_FORMAT_A8R8G8B8_UNORM
;
842 case PIPE_FORMAT_X8R8G8B8_SRGB
:
843 return PIPE_FORMAT_X8R8G8B8_UNORM
;
844 case PIPE_FORMAT_R8G8B8A8_SRGB
:
845 return PIPE_FORMAT_R8G8B8A8_UNORM
;
846 case PIPE_FORMAT_R8G8B8X8_SRGB
:
847 return PIPE_FORMAT_R8G8B8X8_UNORM
;
848 case PIPE_FORMAT_DXT1_SRGB
:
849 return PIPE_FORMAT_DXT1_RGB
;
850 case PIPE_FORMAT_DXT1_SRGBA
:
851 return PIPE_FORMAT_DXT1_RGBA
;
852 case PIPE_FORMAT_DXT3_SRGBA
:
853 return PIPE_FORMAT_DXT3_RGBA
;
854 case PIPE_FORMAT_DXT5_SRGBA
:
855 return PIPE_FORMAT_DXT5_RGBA
;
862 * Given a depth-stencil format, return the corresponding stencil-only format.
863 * For stencil-only formats, return the format unchanged.
865 static INLINE
enum pipe_format
866 util_format_stencil_only(enum pipe_format format
)
869 /* mask out the depth component */
870 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
871 return PIPE_FORMAT_X24S8_UINT
;
872 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
873 return PIPE_FORMAT_S8X24_UINT
;
874 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
875 return PIPE_FORMAT_X32_S8X24_UINT
;
877 /* stencil only formats */
878 case PIPE_FORMAT_X24S8_UINT
:
879 case PIPE_FORMAT_S8X24_UINT
:
880 case PIPE_FORMAT_X32_S8X24_UINT
:
881 case PIPE_FORMAT_S8_UINT
:
886 return PIPE_FORMAT_NONE
;
891 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
892 * This is identity for non-intensity formats.
894 static INLINE
enum pipe_format
895 util_format_intensity_to_red(enum pipe_format format
)
898 case PIPE_FORMAT_I8_UNORM
:
899 return PIPE_FORMAT_R8_UNORM
;
900 case PIPE_FORMAT_I8_SNORM
:
901 return PIPE_FORMAT_R8_SNORM
;
902 case PIPE_FORMAT_I16_UNORM
:
903 return PIPE_FORMAT_R16_UNORM
;
904 case PIPE_FORMAT_I16_SNORM
:
905 return PIPE_FORMAT_R16_SNORM
;
906 case PIPE_FORMAT_I16_FLOAT
:
907 return PIPE_FORMAT_R16_FLOAT
;
908 case PIPE_FORMAT_I32_FLOAT
:
909 return PIPE_FORMAT_R32_FLOAT
;
910 case PIPE_FORMAT_I8_UINT
:
911 return PIPE_FORMAT_R8_UINT
;
912 case PIPE_FORMAT_I8_SINT
:
913 return PIPE_FORMAT_R8_SINT
;
914 case PIPE_FORMAT_I16_UINT
:
915 return PIPE_FORMAT_R16_UINT
;
916 case PIPE_FORMAT_I16_SINT
:
917 return PIPE_FORMAT_R16_SINT
;
918 case PIPE_FORMAT_I32_UINT
:
919 return PIPE_FORMAT_R32_UINT
;
920 case PIPE_FORMAT_I32_SINT
:
921 return PIPE_FORMAT_R32_SINT
;
923 assert(!util_format_is_intensity(format
));
929 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
930 * This is identity for non-luminance formats.
932 static INLINE
enum pipe_format
933 util_format_luminance_to_red(enum pipe_format format
)
936 case PIPE_FORMAT_L8_UNORM
:
937 return PIPE_FORMAT_R8_UNORM
;
938 case PIPE_FORMAT_L8_SNORM
:
939 return PIPE_FORMAT_R8_SNORM
;
940 case PIPE_FORMAT_L16_UNORM
:
941 return PIPE_FORMAT_R16_UNORM
;
942 case PIPE_FORMAT_L16_SNORM
:
943 return PIPE_FORMAT_R16_SNORM
;
944 case PIPE_FORMAT_L16_FLOAT
:
945 return PIPE_FORMAT_R16_FLOAT
;
946 case PIPE_FORMAT_L32_FLOAT
:
947 return PIPE_FORMAT_R32_FLOAT
;
948 case PIPE_FORMAT_L8_UINT
:
949 return PIPE_FORMAT_R8_UINT
;
950 case PIPE_FORMAT_L8_SINT
:
951 return PIPE_FORMAT_R8_SINT
;
952 case PIPE_FORMAT_L16_UINT
:
953 return PIPE_FORMAT_R16_UINT
;
954 case PIPE_FORMAT_L16_SINT
:
955 return PIPE_FORMAT_R16_SINT
;
956 case PIPE_FORMAT_L32_UINT
:
957 return PIPE_FORMAT_R32_UINT
;
958 case PIPE_FORMAT_L32_SINT
:
959 return PIPE_FORMAT_R32_SINT
;
961 case PIPE_FORMAT_LATC1_UNORM
:
962 return PIPE_FORMAT_RGTC1_UNORM
;
963 case PIPE_FORMAT_LATC1_SNORM
:
964 return PIPE_FORMAT_RGTC1_SNORM
;
966 case PIPE_FORMAT_L4A4_UNORM
:
967 /* XXX A4R4 is defined as x00y in u_format.csv */
968 return PIPE_FORMAT_A4R4_UNORM
;
970 case PIPE_FORMAT_L8A8_UNORM
:
971 return PIPE_FORMAT_R8A8_UNORM
;
972 case PIPE_FORMAT_L8A8_SNORM
:
973 return PIPE_FORMAT_R8A8_SNORM
;
974 case PIPE_FORMAT_L16A16_UNORM
:
975 return PIPE_FORMAT_R16A16_UNORM
;
976 case PIPE_FORMAT_L16A16_SNORM
:
977 return PIPE_FORMAT_R16A16_SNORM
;
978 case PIPE_FORMAT_L16A16_FLOAT
:
979 return PIPE_FORMAT_R16A16_FLOAT
;
980 case PIPE_FORMAT_L32A32_FLOAT
:
981 return PIPE_FORMAT_R32A32_FLOAT
;
982 case PIPE_FORMAT_L8A8_UINT
:
983 return PIPE_FORMAT_R8A8_UINT
;
984 case PIPE_FORMAT_L8A8_SINT
:
985 return PIPE_FORMAT_R8A8_SINT
;
986 case PIPE_FORMAT_L16A16_UINT
:
987 return PIPE_FORMAT_R16A16_UINT
;
988 case PIPE_FORMAT_L16A16_SINT
:
989 return PIPE_FORMAT_R16A16_SINT
;
990 case PIPE_FORMAT_L32A32_UINT
:
991 return PIPE_FORMAT_R32A32_UINT
;
992 case PIPE_FORMAT_L32A32_SINT
:
993 return PIPE_FORMAT_R32A32_SINT
;
995 /* We don't have compressed red-alpha variants for these. */
996 case PIPE_FORMAT_LATC2_UNORM
:
997 case PIPE_FORMAT_LATC2_SNORM
:
998 return PIPE_FORMAT_NONE
;
1001 assert(!util_format_is_luminance(format
) &&
1002 !util_format_is_luminance_alpha(format
));
1008 * Return the number of components stored.
1009 * Formats with block size != 1x1 will always have 1 component (the block).
1011 static INLINE
unsigned
1012 util_format_get_nr_components(enum pipe_format format
)
1014 const struct util_format_description
*desc
= util_format_description(format
);
1015 return desc
->nr_channels
;
1019 * Return the index of the first non-void channel
1020 * -1 if no non-void channels
1023 util_format_get_first_non_void_channel(enum pipe_format format
)
1025 const struct util_format_description
*desc
= util_format_description(format
);
1028 for (i
= 0; i
< 4; i
++)
1029 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1039 * Format access functions.
1043 util_format_read_4f(enum pipe_format format
,
1044 float *dst
, unsigned dst_stride
,
1045 const void *src
, unsigned src_stride
,
1046 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1049 util_format_write_4f(enum pipe_format format
,
1050 const float *src
, unsigned src_stride
,
1051 void *dst
, unsigned dst_stride
,
1052 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1055 util_format_read_4ub(enum pipe_format format
,
1056 uint8_t *dst
, unsigned dst_stride
,
1057 const void *src
, unsigned src_stride
,
1058 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1061 util_format_write_4ub(enum pipe_format format
,
1062 const uint8_t *src
, unsigned src_stride
,
1063 void *dst
, unsigned dst_stride
,
1064 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1067 util_format_read_4ui(enum pipe_format format
,
1068 unsigned *dst
, unsigned dst_stride
,
1069 const void *src
, unsigned src_stride
,
1070 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1073 util_format_write_4ui(enum pipe_format format
,
1074 const unsigned int *src
, unsigned src_stride
,
1075 void *dst
, unsigned dst_stride
,
1076 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1079 util_format_read_4i(enum pipe_format format
,
1080 int *dst
, unsigned dst_stride
,
1081 const void *src
, unsigned src_stride
,
1082 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1085 util_format_write_4i(enum pipe_format format
,
1086 const int *src
, unsigned src_stride
,
1087 void *dst
, unsigned dst_stride
,
1088 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1091 * Generic format conversion;
1095 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1098 util_format_translate(enum pipe_format dst_format
,
1099 void *dst
, unsigned dst_stride
,
1100 unsigned dst_x
, unsigned dst_y
,
1101 enum pipe_format src_format
,
1102 const void *src
, unsigned src_stride
,
1103 unsigned src_x
, unsigned src_y
,
1104 unsigned width
, unsigned height
);
1107 * Swizzle operations.
1110 /* Compose two sets of swizzles.
1111 * If V is a 4D vector and the function parameters represent functions that
1112 * swizzle vector components, this holds:
1113 * swz2(swz1(V)) = dst(V)
1115 void util_format_compose_swizzles(const unsigned char swz1
[4],
1116 const unsigned char swz2
[4],
1117 unsigned char dst
[4]);
1119 void util_format_swizzle_4f(float *dst
, const float *src
,
1120 const unsigned char swz
[4]);
1122 void util_format_unswizzle_4f(float *dst
, const float *src
,
1123 const unsigned char swz
[4]);
1129 #endif /* ! U_FORMAT_H */