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 */
139 struct util_format_description
141 enum pipe_format format
;
146 * Short name, striped of the prefix, lower case.
148 const char *short_name
;
151 * Pixel block dimensions.
153 struct util_format_block block
;
155 enum util_format_layout layout
;
158 * The number of channels.
160 unsigned nr_channels
:3;
163 * Whether all channels have the same number of (whole) bytes and type.
168 * Whether the pixel format can be described as a bitfield structure.
171 * - pixel depth must be 8, 16, or 32 bits;
172 * - all channels must be unsigned, signed, or void
174 unsigned is_bitmask
:1;
177 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
182 * Input channel description.
184 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
186 struct util_format_channel_description channel
[4];
189 * Output channel swizzle.
191 * The order is either:
195 * depending on the colorspace.
197 unsigned char swizzle
[4];
200 * Colorspace transformation.
202 enum util_format_colorspace colorspace
;
205 * Unpack pixel blocks to R8G8B8A8_UNORM.
206 * Note: strides are in bytes.
208 * Only defined for non-depth-stencil formats.
211 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
212 const uint8_t *src
, unsigned src_stride
,
213 unsigned width
, unsigned height
);
216 * Pack pixel blocks from R8G8B8A8_UNORM.
217 * Note: strides are in bytes.
219 * Only defined for non-depth-stencil formats.
222 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
223 const uint8_t *src
, unsigned src_stride
,
224 unsigned width
, unsigned height
);
227 * Fetch a single pixel (i, j) from a block.
229 * XXX: Only defined for a very few select formats.
232 (*fetch_rgba_8unorm
)(uint8_t *dst
,
234 unsigned i
, unsigned j
);
237 * Unpack pixel blocks to R32G32B32A32_FLOAT.
238 * Note: strides are in bytes.
240 * Only defined for non-depth-stencil formats.
243 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
244 const uint8_t *src
, unsigned src_stride
,
245 unsigned width
, unsigned height
);
248 * Pack pixel blocks from R32G32B32A32_FLOAT.
249 * Note: strides are in bytes.
251 * Only defined for non-depth-stencil formats.
254 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
255 const float *src
, unsigned src_stride
,
256 unsigned width
, unsigned height
);
259 * Fetch a single pixel (i, j) from a block.
261 * Only defined for non-depth-stencil and non-integer formats.
264 (*fetch_rgba_float
)(float *dst
,
266 unsigned i
, unsigned j
);
269 * Unpack pixels to Z32_UNORM.
270 * Note: strides are in bytes.
272 * Only defined for depth formats.
275 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
276 const uint8_t *src
, unsigned src_stride
,
277 unsigned width
, unsigned height
);
280 * Pack pixels from Z32_FLOAT.
281 * Note: strides are in bytes.
283 * Only defined for depth formats.
286 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
287 const uint32_t *src
, unsigned src_stride
,
288 unsigned width
, unsigned height
);
291 * Unpack pixels to Z32_FLOAT.
292 * Note: strides are in bytes.
294 * Only defined for depth formats.
297 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
298 const uint8_t *src
, unsigned src_stride
,
299 unsigned width
, unsigned height
);
302 * Pack pixels from Z32_FLOAT.
303 * Note: strides are in bytes.
305 * Only defined for depth formats.
308 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
309 const float *src
, unsigned src_stride
,
310 unsigned width
, unsigned height
);
313 * Unpack pixels to S8_UINT.
314 * Note: strides are in bytes.
316 * Only defined for stencil formats.
319 (*unpack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
320 const uint8_t *src
, unsigned src_stride
,
321 unsigned width
, unsigned height
);
324 * Pack pixels from S8_UINT.
325 * Note: strides are in bytes.
327 * Only defined for stencil formats.
330 (*pack_s_8uint
)(uint8_t *dst
, unsigned dst_stride
,
331 const uint8_t *src
, unsigned src_stride
,
332 unsigned width
, unsigned height
);
335 * Unpack pixel blocks to R32G32B32A32_UINT.
336 * Note: strides are in bytes.
338 * Only defined for INT formats.
341 (*unpack_rgba_uint
)(unsigned *dst
, unsigned dst_stride
,
342 const uint8_t *src
, unsigned src_stride
,
343 unsigned width
, unsigned height
);
346 (*pack_rgba_uint
)(uint8_t *dst
, unsigned dst_stride
,
347 const unsigned *src
, unsigned src_stride
,
348 unsigned width
, unsigned height
);
351 * Unpack pixel blocks to R32G32B32A32_SINT.
352 * Note: strides are in bytes.
354 * Only defined for INT formats.
357 (*unpack_rgba_sint
)(signed *dst
, unsigned dst_stride
,
358 const uint8_t *src
, unsigned src_stride
,
359 unsigned width
, unsigned height
);
362 (*pack_rgba_sint
)(uint8_t *dst
, unsigned dst_stride
,
363 const int *src
, unsigned src_stride
,
364 unsigned width
, unsigned height
);
367 * Fetch a single pixel (i, j) from a block.
369 * Only defined for unsigned (pure) integer formats.
372 (*fetch_rgba_uint
)(uint32_t *dst
,
374 unsigned i
, unsigned j
);
377 * Fetch a single pixel (i, j) from a block.
379 * Only defined for signed (pure) integer formats.
382 (*fetch_rgba_sint
)(int32_t *dst
,
384 unsigned i
, unsigned j
);
388 extern const struct util_format_description
389 util_format_description_table
[];
392 const struct util_format_description
*
393 util_format_description(enum pipe_format format
);
397 * Format query functions.
400 static INLINE
const char *
401 util_format_name(enum pipe_format format
)
403 const struct util_format_description
*desc
= util_format_description(format
);
407 return "PIPE_FORMAT_???";
413 static INLINE
const char *
414 util_format_short_name(enum pipe_format format
)
416 const struct util_format_description
*desc
= util_format_description(format
);
423 return desc
->short_name
;
427 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
429 static INLINE boolean
430 util_format_is_plain(enum pipe_format format
)
432 const struct util_format_description
*desc
= util_format_description(format
);
438 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
441 static INLINE boolean
442 util_format_is_compressed(enum pipe_format format
)
444 const struct util_format_description
*desc
= util_format_description(format
);
451 switch (desc
->layout
) {
452 case UTIL_FORMAT_LAYOUT_S3TC
:
453 case UTIL_FORMAT_LAYOUT_RGTC
:
454 case UTIL_FORMAT_LAYOUT_ETC
:
455 /* XXX add other formats in the future */
462 static INLINE boolean
463 util_format_is_s3tc(enum pipe_format format
)
465 const struct util_format_description
*desc
= util_format_description(format
);
472 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
475 static INLINE boolean
476 util_format_is_srgb(enum pipe_format format
)
478 const struct util_format_description
*desc
= util_format_description(format
);
479 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
482 static INLINE boolean
483 util_format_has_depth(const struct util_format_description
*desc
)
485 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
486 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
489 static INLINE boolean
490 util_format_has_stencil(const struct util_format_description
*desc
)
492 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
493 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
496 static INLINE boolean
497 util_format_is_depth_or_stencil(enum pipe_format format
)
499 const struct util_format_description
*desc
= util_format_description(format
);
506 return util_format_has_depth(desc
) ||
507 util_format_has_stencil(desc
);
510 static INLINE boolean
511 util_format_is_depth_and_stencil(enum pipe_format format
)
513 const struct util_format_description
*desc
= util_format_description(format
);
520 return util_format_has_depth(desc
) &&
521 util_format_has_stencil(desc
);
525 * Return whether this is an RGBA, Z, S, or combined ZS format.
526 * Useful for initializing pipe_blit_info::mask.
528 static INLINE
unsigned
529 util_format_get_mask(enum pipe_format format
)
531 const struct util_format_description
*desc
=
532 util_format_description(format
);
537 if (util_format_has_depth(desc
)) {
538 if (util_format_has_stencil(desc
)) {
544 if (util_format_has_stencil(desc
)) {
547 return PIPE_MASK_RGBA
;
553 * Give the RGBA colormask of the channels that can be represented in this
556 * That is, the channels whose values are preserved.
558 static INLINE
unsigned
559 util_format_colormask(const struct util_format_description
*desc
)
564 switch (desc
->colorspace
) {
565 case UTIL_FORMAT_COLORSPACE_RGB
:
566 case UTIL_FORMAT_COLORSPACE_SRGB
:
567 case UTIL_FORMAT_COLORSPACE_YUV
:
569 for (chan
= 0; chan
< 4; ++chan
) {
570 if (desc
->swizzle
[chan
] < 4) {
571 colormask
|= (1 << chan
);
575 case UTIL_FORMAT_COLORSPACE_ZS
:
585 * Checks if color mask covers every channel for the specified format
587 * @param desc a format description to check colormask with
588 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
590 static INLINE boolean
591 util_format_colormask_full(const struct util_format_description
*desc
, unsigned colormask
)
593 return (~colormask
& util_format_colormask(desc
)) == 0;
598 util_format_is_float(enum pipe_format format
);
602 util_format_has_alpha(enum pipe_format format
);
606 util_format_is_luminance(enum pipe_format format
);
610 util_format_is_luminance_alpha(enum pipe_format format
);
614 util_format_is_intensity(enum pipe_format format
);
617 util_format_is_pure_integer(enum pipe_format format
);
620 util_format_is_pure_sint(enum pipe_format format
);
623 util_format_is_pure_uint(enum pipe_format format
);
626 * Check if the src format can be blitted to the destination format with
627 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
631 util_is_format_compatible(const struct util_format_description
*src_desc
,
632 const struct util_format_description
*dst_desc
);
635 * Whether the format is supported by Gallium for the given bindings.
636 * This covers S3TC textures and floating-point render targets.
639 util_format_is_supported(enum pipe_format format
, unsigned bind
);
642 * Whether this format is a rgab8 variant.
644 * That is, any format that matches the
646 * PIPE_FORMAT_?8?8?8?8_UNORM
648 static INLINE boolean
649 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
653 if(desc
->block
.width
!= 1 ||
654 desc
->block
.height
!= 1 ||
655 desc
->block
.bits
!= 32)
658 for(chan
= 0; chan
< 4; ++chan
) {
659 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
660 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
662 if(desc
->channel
[chan
].size
!= 8)
671 * Return total bits needed for the pixel format per block.
674 util_format_get_blocksizebits(enum pipe_format format
)
676 const struct util_format_description
*desc
= util_format_description(format
);
683 return desc
->block
.bits
;
687 * Return bytes per block (not pixel) for the given format.
690 util_format_get_blocksize(enum pipe_format format
)
692 uint bits
= util_format_get_blocksizebits(format
);
694 assert(bits
% 8 == 0);
700 util_format_get_blockwidth(enum pipe_format format
)
702 const struct util_format_description
*desc
= util_format_description(format
);
709 return desc
->block
.width
;
713 util_format_get_blockheight(enum pipe_format format
)
715 const struct util_format_description
*desc
= util_format_description(format
);
722 return desc
->block
.height
;
725 static INLINE
unsigned
726 util_format_get_nblocksx(enum pipe_format format
,
729 unsigned blockwidth
= util_format_get_blockwidth(format
);
730 return (x
+ blockwidth
- 1) / blockwidth
;
733 static INLINE
unsigned
734 util_format_get_nblocksy(enum pipe_format format
,
737 unsigned blockheight
= util_format_get_blockheight(format
);
738 return (y
+ blockheight
- 1) / blockheight
;
741 static INLINE
unsigned
742 util_format_get_nblocks(enum pipe_format format
,
746 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
750 util_format_get_stride(enum pipe_format format
,
753 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
757 util_format_get_2d_size(enum pipe_format format
,
761 return util_format_get_nblocksy(format
, height
) * stride
;
765 util_format_get_component_bits(enum pipe_format format
,
766 enum util_format_colorspace colorspace
,
769 const struct util_format_description
*desc
= util_format_description(format
);
770 enum util_format_colorspace desc_colorspace
;
777 assert(component
< 4);
779 /* Treat RGB and SRGB as equivalent. */
780 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
781 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
783 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
784 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
786 desc_colorspace
= desc
->colorspace
;
789 if (desc_colorspace
!= colorspace
) {
793 switch (desc
->swizzle
[component
]) {
794 case UTIL_FORMAT_SWIZZLE_X
:
795 return desc
->channel
[0].size
;
796 case UTIL_FORMAT_SWIZZLE_Y
:
797 return desc
->channel
[1].size
;
798 case UTIL_FORMAT_SWIZZLE_Z
:
799 return desc
->channel
[2].size
;
800 case UTIL_FORMAT_SWIZZLE_W
:
801 return desc
->channel
[3].size
;
808 * Given a linear RGB colorspace format, return the corresponding SRGB
809 * format, or PIPE_FORMAT_NONE if none.
811 static INLINE
enum pipe_format
812 util_format_srgb(enum pipe_format format
)
815 case PIPE_FORMAT_L8_UNORM
:
816 return PIPE_FORMAT_L8_SRGB
;
817 case PIPE_FORMAT_L8A8_UNORM
:
818 return PIPE_FORMAT_L8A8_SRGB
;
819 case PIPE_FORMAT_R8G8B8_UNORM
:
820 return PIPE_FORMAT_R8G8B8_SRGB
;
821 case PIPE_FORMAT_A8B8G8R8_UNORM
:
822 return PIPE_FORMAT_A8B8G8R8_SRGB
;
823 case PIPE_FORMAT_X8B8G8R8_UNORM
:
824 return PIPE_FORMAT_X8B8G8R8_SRGB
;
825 case PIPE_FORMAT_B8G8R8A8_UNORM
:
826 return PIPE_FORMAT_B8G8R8A8_SRGB
;
827 case PIPE_FORMAT_B8G8R8X8_UNORM
:
828 return PIPE_FORMAT_B8G8R8X8_SRGB
;
829 case PIPE_FORMAT_A8R8G8B8_UNORM
:
830 return PIPE_FORMAT_A8R8G8B8_SRGB
;
831 case PIPE_FORMAT_X8R8G8B8_UNORM
:
832 return PIPE_FORMAT_X8R8G8B8_SRGB
;
833 case PIPE_FORMAT_R8G8B8A8_UNORM
:
834 return PIPE_FORMAT_R8G8B8A8_SRGB
;
835 case PIPE_FORMAT_R8G8B8X8_UNORM
:
836 return PIPE_FORMAT_R8G8B8X8_SRGB
;
837 case PIPE_FORMAT_DXT1_RGB
:
838 return PIPE_FORMAT_DXT1_SRGB
;
839 case PIPE_FORMAT_DXT1_RGBA
:
840 return PIPE_FORMAT_DXT1_SRGBA
;
841 case PIPE_FORMAT_DXT3_RGBA
:
842 return PIPE_FORMAT_DXT3_SRGBA
;
843 case PIPE_FORMAT_DXT5_RGBA
:
844 return PIPE_FORMAT_DXT5_SRGBA
;
846 return PIPE_FORMAT_NONE
;
851 * Given an sRGB format, return the corresponding linear colorspace format.
852 * For non sRGB formats, return the format unchanged.
854 static INLINE
enum pipe_format
855 util_format_linear(enum pipe_format format
)
858 case PIPE_FORMAT_L8_SRGB
:
859 return PIPE_FORMAT_L8_UNORM
;
860 case PIPE_FORMAT_L8A8_SRGB
:
861 return PIPE_FORMAT_L8A8_UNORM
;
862 case PIPE_FORMAT_R8G8B8_SRGB
:
863 return PIPE_FORMAT_R8G8B8_UNORM
;
864 case PIPE_FORMAT_A8B8G8R8_SRGB
:
865 return PIPE_FORMAT_A8B8G8R8_UNORM
;
866 case PIPE_FORMAT_X8B8G8R8_SRGB
:
867 return PIPE_FORMAT_X8B8G8R8_UNORM
;
868 case PIPE_FORMAT_B8G8R8A8_SRGB
:
869 return PIPE_FORMAT_B8G8R8A8_UNORM
;
870 case PIPE_FORMAT_B8G8R8X8_SRGB
:
871 return PIPE_FORMAT_B8G8R8X8_UNORM
;
872 case PIPE_FORMAT_A8R8G8B8_SRGB
:
873 return PIPE_FORMAT_A8R8G8B8_UNORM
;
874 case PIPE_FORMAT_X8R8G8B8_SRGB
:
875 return PIPE_FORMAT_X8R8G8B8_UNORM
;
876 case PIPE_FORMAT_R8G8B8A8_SRGB
:
877 return PIPE_FORMAT_R8G8B8A8_UNORM
;
878 case PIPE_FORMAT_R8G8B8X8_SRGB
:
879 return PIPE_FORMAT_R8G8B8X8_UNORM
;
880 case PIPE_FORMAT_DXT1_SRGB
:
881 return PIPE_FORMAT_DXT1_RGB
;
882 case PIPE_FORMAT_DXT1_SRGBA
:
883 return PIPE_FORMAT_DXT1_RGBA
;
884 case PIPE_FORMAT_DXT3_SRGBA
:
885 return PIPE_FORMAT_DXT3_RGBA
;
886 case PIPE_FORMAT_DXT5_SRGBA
:
887 return PIPE_FORMAT_DXT5_RGBA
;
894 * Given a depth-stencil format, return the corresponding stencil-only format.
895 * For stencil-only formats, return the format unchanged.
897 static INLINE
enum pipe_format
898 util_format_stencil_only(enum pipe_format format
)
901 /* mask out the depth component */
902 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
903 return PIPE_FORMAT_X24S8_UINT
;
904 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
905 return PIPE_FORMAT_S8X24_UINT
;
906 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
907 return PIPE_FORMAT_X32_S8X24_UINT
;
909 /* stencil only formats */
910 case PIPE_FORMAT_X24S8_UINT
:
911 case PIPE_FORMAT_S8X24_UINT
:
912 case PIPE_FORMAT_X32_S8X24_UINT
:
913 case PIPE_FORMAT_S8_UINT
:
918 return PIPE_FORMAT_NONE
;
923 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
924 * This is identity for non-intensity formats.
926 static INLINE
enum pipe_format
927 util_format_intensity_to_red(enum pipe_format format
)
930 case PIPE_FORMAT_I8_UNORM
:
931 return PIPE_FORMAT_R8_UNORM
;
932 case PIPE_FORMAT_I8_SNORM
:
933 return PIPE_FORMAT_R8_SNORM
;
934 case PIPE_FORMAT_I16_UNORM
:
935 return PIPE_FORMAT_R16_UNORM
;
936 case PIPE_FORMAT_I16_SNORM
:
937 return PIPE_FORMAT_R16_SNORM
;
938 case PIPE_FORMAT_I16_FLOAT
:
939 return PIPE_FORMAT_R16_FLOAT
;
940 case PIPE_FORMAT_I32_FLOAT
:
941 return PIPE_FORMAT_R32_FLOAT
;
942 case PIPE_FORMAT_I8_UINT
:
943 return PIPE_FORMAT_R8_UINT
;
944 case PIPE_FORMAT_I8_SINT
:
945 return PIPE_FORMAT_R8_SINT
;
946 case PIPE_FORMAT_I16_UINT
:
947 return PIPE_FORMAT_R16_UINT
;
948 case PIPE_FORMAT_I16_SINT
:
949 return PIPE_FORMAT_R16_SINT
;
950 case PIPE_FORMAT_I32_UINT
:
951 return PIPE_FORMAT_R32_UINT
;
952 case PIPE_FORMAT_I32_SINT
:
953 return PIPE_FORMAT_R32_SINT
;
955 assert(!util_format_is_intensity(format
));
961 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
962 * This is identity for non-luminance formats.
964 static INLINE
enum pipe_format
965 util_format_luminance_to_red(enum pipe_format format
)
968 case PIPE_FORMAT_L8_UNORM
:
969 return PIPE_FORMAT_R8_UNORM
;
970 case PIPE_FORMAT_L8_SNORM
:
971 return PIPE_FORMAT_R8_SNORM
;
972 case PIPE_FORMAT_L16_UNORM
:
973 return PIPE_FORMAT_R16_UNORM
;
974 case PIPE_FORMAT_L16_SNORM
:
975 return PIPE_FORMAT_R16_SNORM
;
976 case PIPE_FORMAT_L16_FLOAT
:
977 return PIPE_FORMAT_R16_FLOAT
;
978 case PIPE_FORMAT_L32_FLOAT
:
979 return PIPE_FORMAT_R32_FLOAT
;
980 case PIPE_FORMAT_L8_UINT
:
981 return PIPE_FORMAT_R8_UINT
;
982 case PIPE_FORMAT_L8_SINT
:
983 return PIPE_FORMAT_R8_SINT
;
984 case PIPE_FORMAT_L16_UINT
:
985 return PIPE_FORMAT_R16_UINT
;
986 case PIPE_FORMAT_L16_SINT
:
987 return PIPE_FORMAT_R16_SINT
;
988 case PIPE_FORMAT_L32_UINT
:
989 return PIPE_FORMAT_R32_UINT
;
990 case PIPE_FORMAT_L32_SINT
:
991 return PIPE_FORMAT_R32_SINT
;
993 case PIPE_FORMAT_LATC1_UNORM
:
994 return PIPE_FORMAT_RGTC1_UNORM
;
995 case PIPE_FORMAT_LATC1_SNORM
:
996 return PIPE_FORMAT_RGTC1_SNORM
;
998 case PIPE_FORMAT_L4A4_UNORM
:
999 /* XXX A4R4 is defined as x00y in u_format.csv */
1000 return PIPE_FORMAT_A4R4_UNORM
;
1002 case PIPE_FORMAT_L8A8_UNORM
:
1003 return PIPE_FORMAT_R8A8_UNORM
;
1004 case PIPE_FORMAT_L8A8_SNORM
:
1005 return PIPE_FORMAT_R8A8_SNORM
;
1006 case PIPE_FORMAT_L16A16_UNORM
:
1007 return PIPE_FORMAT_R16A16_UNORM
;
1008 case PIPE_FORMAT_L16A16_SNORM
:
1009 return PIPE_FORMAT_R16A16_SNORM
;
1010 case PIPE_FORMAT_L16A16_FLOAT
:
1011 return PIPE_FORMAT_R16A16_FLOAT
;
1012 case PIPE_FORMAT_L32A32_FLOAT
:
1013 return PIPE_FORMAT_R32A32_FLOAT
;
1014 case PIPE_FORMAT_L8A8_UINT
:
1015 return PIPE_FORMAT_R8A8_UINT
;
1016 case PIPE_FORMAT_L8A8_SINT
:
1017 return PIPE_FORMAT_R8A8_SINT
;
1018 case PIPE_FORMAT_L16A16_UINT
:
1019 return PIPE_FORMAT_R16A16_UINT
;
1020 case PIPE_FORMAT_L16A16_SINT
:
1021 return PIPE_FORMAT_R16A16_SINT
;
1022 case PIPE_FORMAT_L32A32_UINT
:
1023 return PIPE_FORMAT_R32A32_UINT
;
1024 case PIPE_FORMAT_L32A32_SINT
:
1025 return PIPE_FORMAT_R32A32_SINT
;
1027 /* We don't have compressed red-alpha variants for these. */
1028 case PIPE_FORMAT_LATC2_UNORM
:
1029 case PIPE_FORMAT_LATC2_SNORM
:
1030 return PIPE_FORMAT_NONE
;
1033 assert(!util_format_is_luminance(format
) &&
1034 !util_format_is_luminance_alpha(format
));
1040 * Return the number of components stored.
1041 * Formats with block size != 1x1 will always have 1 component (the block).
1043 static INLINE
unsigned
1044 util_format_get_nr_components(enum pipe_format format
)
1046 const struct util_format_description
*desc
= util_format_description(format
);
1047 return desc
->nr_channels
;
1051 * Return the index of the first non-void channel
1052 * -1 if no non-void channels
1055 util_format_get_first_non_void_channel(enum pipe_format format
)
1057 const struct util_format_description
*desc
= util_format_description(format
);
1060 for (i
= 0; i
< 4; i
++)
1061 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1071 * Format access functions.
1075 util_format_read_4f(enum pipe_format format
,
1076 float *dst
, unsigned dst_stride
,
1077 const void *src
, unsigned src_stride
,
1078 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1081 util_format_write_4f(enum pipe_format format
,
1082 const float *src
, unsigned src_stride
,
1083 void *dst
, unsigned dst_stride
,
1084 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1087 util_format_read_4ub(enum pipe_format format
,
1088 uint8_t *dst
, unsigned dst_stride
,
1089 const void *src
, unsigned src_stride
,
1090 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1093 util_format_write_4ub(enum pipe_format format
,
1094 const uint8_t *src
, unsigned src_stride
,
1095 void *dst
, unsigned dst_stride
,
1096 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1099 util_format_read_4ui(enum pipe_format format
,
1100 unsigned *dst
, unsigned dst_stride
,
1101 const void *src
, unsigned src_stride
,
1102 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1105 util_format_write_4ui(enum pipe_format format
,
1106 const unsigned int *src
, unsigned src_stride
,
1107 void *dst
, unsigned dst_stride
,
1108 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1111 util_format_read_4i(enum pipe_format format
,
1112 int *dst
, unsigned dst_stride
,
1113 const void *src
, unsigned src_stride
,
1114 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1117 util_format_write_4i(enum pipe_format format
,
1118 const int *src
, unsigned src_stride
,
1119 void *dst
, unsigned dst_stride
,
1120 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
1123 * Generic format conversion;
1127 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
1130 util_format_translate(enum pipe_format dst_format
,
1131 void *dst
, unsigned dst_stride
,
1132 unsigned dst_x
, unsigned dst_y
,
1133 enum pipe_format src_format
,
1134 const void *src
, unsigned src_stride
,
1135 unsigned src_x
, unsigned src_y
,
1136 unsigned width
, unsigned height
);
1139 * Swizzle operations.
1142 /* Compose two sets of swizzles.
1143 * If V is a 4D vector and the function parameters represent functions that
1144 * swizzle vector components, this holds:
1145 * swz2(swz1(V)) = dst(V)
1147 void util_format_compose_swizzles(const unsigned char swz1
[4],
1148 const unsigned char swz2
[4],
1149 unsigned char dst
[4]);
1151 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1152 * to \param src and store the result in \param dst.
1153 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1155 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
1156 const union pipe_color_union
*src
,
1157 const unsigned char swz
[4],
1158 const boolean is_integer
);
1160 void util_format_swizzle_4f(float *dst
, const float *src
,
1161 const unsigned char swz
[4]);
1163 void util_format_unswizzle_4f(float *dst
, const float *src
,
1164 const unsigned char swz
[4]);
1170 #endif /* ! U_FORMAT_H */