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 YV12 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 * Everything else that doesn't fit in any of the above layouts.
75 UTIL_FORMAT_LAYOUT_OTHER
= 6
79 struct util_format_block
81 /** Block width in pixels */
84 /** Block height in pixels */
87 /** Block size in bits */
92 enum util_format_type
{
93 UTIL_FORMAT_TYPE_VOID
= 0,
94 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
95 UTIL_FORMAT_TYPE_SIGNED
= 2,
96 UTIL_FORMAT_TYPE_FIXED
= 3,
97 UTIL_FORMAT_TYPE_FLOAT
= 4
101 enum util_format_swizzle
{
102 UTIL_FORMAT_SWIZZLE_X
= 0,
103 UTIL_FORMAT_SWIZZLE_Y
= 1,
104 UTIL_FORMAT_SWIZZLE_Z
= 2,
105 UTIL_FORMAT_SWIZZLE_W
= 3,
106 UTIL_FORMAT_SWIZZLE_0
= 4,
107 UTIL_FORMAT_SWIZZLE_1
= 5,
108 UTIL_FORMAT_SWIZZLE_NONE
= 6,
109 UTIL_FORMAT_SWIZZLE_MAX
= 7 /**< Number of enums counter (must be last) */
113 enum util_format_colorspace
{
114 UTIL_FORMAT_COLORSPACE_RGB
= 0,
115 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
116 UTIL_FORMAT_COLORSPACE_YUV
= 2,
117 UTIL_FORMAT_COLORSPACE_ZS
= 3
121 struct util_format_channel_description
123 unsigned type
:6; /**< UTIL_FORMAT_TYPE_x */
124 unsigned normalized
:1;
125 unsigned size
:9; /**< bits per channel */
129 struct util_format_description
131 enum pipe_format format
;
136 * Short name, striped of the prefix, lower case.
138 const char *short_name
;
141 * Pixel block dimensions.
143 struct util_format_block block
;
145 enum util_format_layout layout
;
148 * The number of channels.
150 unsigned nr_channels
:3;
153 * Whether all channels have the same number of (whole) bytes.
158 * Whether the pixel format can be described as a bitfield structure.
161 * - pixel depth must be 8, 16, or 32 bits;
162 * - all channels must be unsigned, signed, or void
164 unsigned is_bitmask
:1;
167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
172 * Input channel description.
174 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
176 struct util_format_channel_description channel
[4];
179 * Output channel swizzle.
181 * The order is either:
185 * depending on the colorspace.
187 unsigned char swizzle
[4];
190 * Colorspace transformation.
192 enum util_format_colorspace colorspace
;
195 * Unpack pixel blocks to R8G8B8A8_UNORM.
196 * Note: strides are in bytes.
198 * Only defined for non-depth-stencil formats.
201 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
202 const uint8_t *src
, unsigned src_stride
,
203 unsigned width
, unsigned height
);
206 * Pack pixel blocks from R8G8B8A8_UNORM.
207 * Note: strides are in bytes.
209 * Only defined for non-depth-stencil formats.
212 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
213 const uint8_t *src
, unsigned src_stride
,
214 unsigned width
, unsigned height
);
217 * Fetch a single pixel (i, j) from a block.
219 * XXX: Only defined for a very few select formats.
222 (*fetch_rgba_8unorm
)(uint8_t *dst
,
224 unsigned i
, unsigned j
);
227 * Unpack pixel blocks to R32G32B32A32_FLOAT.
228 * Note: strides are in bytes.
230 * Only defined for non-depth-stencil formats.
233 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
234 const uint8_t *src
, unsigned src_stride
,
235 unsigned width
, unsigned height
);
238 * Pack pixel blocks from R32G32B32A32_FLOAT.
239 * Note: strides are in bytes.
241 * Only defined for non-depth-stencil formats.
244 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
245 const float *src
, unsigned src_stride
,
246 unsigned width
, unsigned height
);
249 * Fetch a single pixel (i, j) from a block.
251 * Only defined for non-depth-stencil formats.
254 (*fetch_rgba_float
)(float *dst
,
256 unsigned i
, unsigned j
);
259 * Unpack pixels to Z32_UNORM.
260 * Note: strides are in bytes.
262 * Only defined for depth formats.
265 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
266 const uint8_t *src
, unsigned src_stride
,
267 unsigned width
, unsigned height
);
270 * Pack pixels from Z32_FLOAT.
271 * Note: strides are in bytes.
273 * Only defined for depth formats.
276 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
277 const uint32_t *src
, unsigned src_stride
,
278 unsigned width
, unsigned height
);
281 * Unpack pixels to Z32_FLOAT.
282 * Note: strides are in bytes.
284 * Only defined for depth formats.
287 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
288 const uint8_t *src
, unsigned src_stride
,
289 unsigned width
, unsigned height
);
292 * Pack pixels from Z32_FLOAT.
293 * Note: strides are in bytes.
295 * Only defined for depth formats.
298 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
299 const float *src
, unsigned src_stride
,
300 unsigned width
, unsigned height
);
303 * Unpack pixels to S8_USCALED.
304 * Note: strides are in bytes.
306 * Only defined for stencil formats.
309 (*unpack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
310 const uint8_t *src
, unsigned src_stride
,
311 unsigned width
, unsigned height
);
314 * Pack pixels from S8_USCALED.
315 * Note: strides are in bytes.
317 * Only defined for stencil formats.
320 (*pack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
321 const uint8_t *src
, unsigned src_stride
,
322 unsigned width
, unsigned height
);
327 extern const struct util_format_description
328 util_format_description_table
[];
331 const struct util_format_description
*
332 util_format_description(enum pipe_format format
);
336 * Format query functions.
339 static INLINE
const char *
340 util_format_name(enum pipe_format format
)
342 const struct util_format_description
*desc
= util_format_description(format
);
346 return "PIPE_FORMAT_???";
352 static INLINE
const char *
353 util_format_short_name(enum pipe_format format
)
355 const struct util_format_description
*desc
= util_format_description(format
);
362 return desc
->short_name
;
366 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
368 static INLINE boolean
369 util_format_is_plain(enum pipe_format format
)
371 const struct util_format_description
*desc
= util_format_description(format
);
377 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
? TRUE
: FALSE
;
380 static INLINE boolean
381 util_format_is_compressed(enum pipe_format format
)
383 const struct util_format_description
*desc
= util_format_description(format
);
390 switch (desc
->layout
) {
391 case UTIL_FORMAT_LAYOUT_S3TC
:
392 case UTIL_FORMAT_LAYOUT_RGTC
:
393 /* XXX add other formats in the future */
400 static INLINE boolean
401 util_format_is_s3tc(enum pipe_format format
)
403 const struct util_format_description
*desc
= util_format_description(format
);
410 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
413 static INLINE boolean
414 util_format_is_srgb(enum pipe_format format
)
416 const struct util_format_description
*desc
= util_format_description(format
);
417 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
;
420 static INLINE boolean
421 util_format_has_depth(const struct util_format_description
*desc
)
423 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
424 desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
;
427 static INLINE boolean
428 util_format_has_stencil(const struct util_format_description
*desc
)
430 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
&&
431 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
;
434 static INLINE boolean
435 util_format_is_depth_or_stencil(enum pipe_format format
)
437 const struct util_format_description
*desc
= util_format_description(format
);
444 return util_format_has_depth(desc
) ||
445 util_format_has_stencil(desc
);
448 static INLINE boolean
449 util_format_is_depth_and_stencil(enum pipe_format format
)
451 const struct util_format_description
*desc
= util_format_description(format
);
458 return util_format_has_depth(desc
) &&
459 util_format_has_stencil(desc
);
464 * Give the RGBA colormask of the channels that can be represented in this
467 * That is, the channels whose values are preserved.
469 static INLINE
unsigned
470 util_format_colormask(const struct util_format_description
*desc
)
475 switch (desc
->colorspace
) {
476 case UTIL_FORMAT_COLORSPACE_RGB
:
477 case UTIL_FORMAT_COLORSPACE_SRGB
:
478 case UTIL_FORMAT_COLORSPACE_YUV
:
480 for (chan
= 0; chan
< 4; ++chan
) {
481 if (desc
->swizzle
[chan
] < 4) {
482 colormask
|= (1 << chan
);
486 case UTIL_FORMAT_COLORSPACE_ZS
:
496 util_format_is_float(enum pipe_format format
);
500 util_format_is_rgb_no_alpha(enum pipe_format format
);
504 util_format_is_luminance(enum pipe_format format
);
508 util_format_is_luminance_alpha(enum pipe_format format
);
512 util_format_is_intensity(enum pipe_format format
);
516 * Whether the src format can be blitted to destation format with a simple
520 util_is_format_compatible(const struct util_format_description
*src_desc
,
521 const struct util_format_description
*dst_desc
);
524 * Whether the format is supported by Gallium for the given bindings.
525 * This covers S3TC textures and floating-point render targets.
528 util_format_is_supported(enum pipe_format format
, unsigned bind
);
531 * Whether this format is a rgab8 variant.
533 * That is, any format that matches the
535 * PIPE_FORMAT_?8?8?8?8_UNORM
537 static INLINE boolean
538 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
542 if(desc
->block
.width
!= 1 ||
543 desc
->block
.height
!= 1 ||
544 desc
->block
.bits
!= 32)
547 for(chan
= 0; chan
< 4; ++chan
) {
548 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
549 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
551 if(desc
->channel
[chan
].size
!= 8)
560 * Return total bits needed for the pixel format per block.
563 util_format_get_blocksizebits(enum pipe_format format
)
565 const struct util_format_description
*desc
= util_format_description(format
);
572 return desc
->block
.bits
;
576 * Return bytes per block (not pixel) for the given format.
579 util_format_get_blocksize(enum pipe_format format
)
581 uint bits
= util_format_get_blocksizebits(format
);
583 assert(bits
% 8 == 0);
589 util_format_get_blockwidth(enum pipe_format format
)
591 const struct util_format_description
*desc
= util_format_description(format
);
598 return desc
->block
.width
;
602 util_format_get_blockheight(enum pipe_format format
)
604 const struct util_format_description
*desc
= util_format_description(format
);
611 return desc
->block
.height
;
614 static INLINE
unsigned
615 util_format_get_nblocksx(enum pipe_format format
,
618 unsigned blockwidth
= util_format_get_blockwidth(format
);
619 return (x
+ blockwidth
- 1) / blockwidth
;
622 static INLINE
unsigned
623 util_format_get_nblocksy(enum pipe_format format
,
626 unsigned blockheight
= util_format_get_blockheight(format
);
627 return (y
+ blockheight
- 1) / blockheight
;
630 static INLINE
unsigned
631 util_format_get_nblocks(enum pipe_format format
,
635 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
639 util_format_get_stride(enum pipe_format format
,
642 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
646 util_format_get_2d_size(enum pipe_format format
,
650 return util_format_get_nblocksy(format
, height
) * stride
;
654 util_format_get_component_bits(enum pipe_format format
,
655 enum util_format_colorspace colorspace
,
658 const struct util_format_description
*desc
= util_format_description(format
);
659 enum util_format_colorspace desc_colorspace
;
666 assert(component
< 4);
668 /* Treat RGB and SRGB as equivalent. */
669 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
670 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
672 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
673 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
675 desc_colorspace
= desc
->colorspace
;
678 if (desc_colorspace
!= colorspace
) {
682 switch (desc
->swizzle
[component
]) {
683 case UTIL_FORMAT_SWIZZLE_X
:
684 return desc
->channel
[0].size
;
685 case UTIL_FORMAT_SWIZZLE_Y
:
686 return desc
->channel
[1].size
;
687 case UTIL_FORMAT_SWIZZLE_Z
:
688 return desc
->channel
[2].size
;
689 case UTIL_FORMAT_SWIZZLE_W
:
690 return desc
->channel
[3].size
;
696 static INLINE boolean
697 util_format_has_alpha(enum pipe_format format
)
699 const struct util_format_description
*desc
= util_format_description(format
);
706 switch (desc
->colorspace
) {
707 case UTIL_FORMAT_COLORSPACE_RGB
:
708 case UTIL_FORMAT_COLORSPACE_SRGB
:
709 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
710 case UTIL_FORMAT_COLORSPACE_YUV
:
712 case UTIL_FORMAT_COLORSPACE_ZS
:
721 * Given a linear RGB colorspace format, return the corresponding SRGB
722 * format, or PIPE_FORMAT_NONE if none.
724 static INLINE
enum pipe_format
725 util_format_srgb(enum pipe_format format
)
728 case PIPE_FORMAT_L8_UNORM
:
729 return PIPE_FORMAT_L8_SRGB
;
730 case PIPE_FORMAT_L8A8_UNORM
:
731 return PIPE_FORMAT_L8A8_SRGB
;
732 case PIPE_FORMAT_R8G8B8_UNORM
:
733 return PIPE_FORMAT_R8G8B8_SRGB
;
734 case PIPE_FORMAT_A8B8G8R8_UNORM
:
735 return PIPE_FORMAT_A8B8G8R8_SRGB
;
736 case PIPE_FORMAT_X8B8G8R8_UNORM
:
737 return PIPE_FORMAT_X8B8G8R8_SRGB
;
738 case PIPE_FORMAT_B8G8R8A8_UNORM
:
739 return PIPE_FORMAT_B8G8R8A8_SRGB
;
740 case PIPE_FORMAT_B8G8R8X8_UNORM
:
741 return PIPE_FORMAT_B8G8R8X8_SRGB
;
742 case PIPE_FORMAT_A8R8G8B8_UNORM
:
743 return PIPE_FORMAT_A8R8G8B8_SRGB
;
744 case PIPE_FORMAT_X8R8G8B8_UNORM
:
745 return PIPE_FORMAT_X8R8G8B8_SRGB
;
746 case PIPE_FORMAT_DXT1_RGB
:
747 return PIPE_FORMAT_DXT1_SRGB
;
748 case PIPE_FORMAT_DXT1_RGBA
:
749 return PIPE_FORMAT_DXT1_SRGBA
;
750 case PIPE_FORMAT_DXT3_RGBA
:
751 return PIPE_FORMAT_DXT3_SRGBA
;
752 case PIPE_FORMAT_DXT5_RGBA
:
753 return PIPE_FORMAT_DXT5_SRGBA
;
755 return PIPE_FORMAT_NONE
;
760 * Given an sRGB format, return the corresponding linear colorspace format.
761 * For non sRGB formats, return the format unchanged.
763 static INLINE
enum pipe_format
764 util_format_linear(enum pipe_format format
)
767 case PIPE_FORMAT_L8_SRGB
:
768 return PIPE_FORMAT_L8_UNORM
;
769 case PIPE_FORMAT_L8A8_SRGB
:
770 return PIPE_FORMAT_L8A8_UNORM
;
771 case PIPE_FORMAT_R8G8B8_SRGB
:
772 return PIPE_FORMAT_R8G8B8_UNORM
;
773 case PIPE_FORMAT_A8B8G8R8_SRGB
:
774 return PIPE_FORMAT_A8B8G8R8_UNORM
;
775 case PIPE_FORMAT_X8B8G8R8_SRGB
:
776 return PIPE_FORMAT_X8B8G8R8_UNORM
;
777 case PIPE_FORMAT_B8G8R8A8_SRGB
:
778 return PIPE_FORMAT_B8G8R8A8_UNORM
;
779 case PIPE_FORMAT_B8G8R8X8_SRGB
:
780 return PIPE_FORMAT_B8G8R8X8_UNORM
;
781 case PIPE_FORMAT_A8R8G8B8_SRGB
:
782 return PIPE_FORMAT_A8R8G8B8_UNORM
;
783 case PIPE_FORMAT_X8R8G8B8_SRGB
:
784 return PIPE_FORMAT_X8R8G8B8_UNORM
;
785 case PIPE_FORMAT_DXT1_SRGB
:
786 return PIPE_FORMAT_DXT1_RGB
;
787 case PIPE_FORMAT_DXT1_SRGBA
:
788 return PIPE_FORMAT_DXT1_RGBA
;
789 case PIPE_FORMAT_DXT3_SRGBA
:
790 return PIPE_FORMAT_DXT3_RGBA
;
791 case PIPE_FORMAT_DXT5_SRGBA
:
792 return PIPE_FORMAT_DXT5_RGBA
;
799 * Return the number of components stored.
800 * Formats with block size != 1x1 will always have 1 component (the block).
802 static INLINE
unsigned
803 util_format_get_nr_components(enum pipe_format format
)
805 const struct util_format_description
*desc
= util_format_description(format
);
806 return desc
->nr_channels
;
810 * Return the index of the first non-void channel
811 * -1 if no non-void channels
814 util_format_get_first_non_void_channel(enum pipe_format format
)
816 const struct util_format_description
*desc
= util_format_description(format
);
819 for (i
= 0; i
< 4; i
++)
820 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
830 * Format access functions.
834 util_format_read_4f(enum pipe_format format
,
835 float *dst
, unsigned dst_stride
,
836 const void *src
, unsigned src_stride
,
837 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
840 util_format_write_4f(enum pipe_format format
,
841 const float *src
, unsigned src_stride
,
842 void *dst
, unsigned dst_stride
,
843 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
846 util_format_read_4ub(enum pipe_format format
,
847 uint8_t *dst
, unsigned dst_stride
,
848 const void *src
, unsigned src_stride
,
849 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
852 util_format_write_4ub(enum pipe_format format
,
853 const uint8_t *src
, unsigned src_stride
,
854 void *dst
, unsigned dst_stride
,
855 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
858 * Generic format conversion;
862 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
865 util_format_translate(enum pipe_format dst_format
,
866 void *dst
, unsigned dst_stride
,
867 unsigned dst_x
, unsigned dst_y
,
868 enum pipe_format src_format
,
869 const void *src
, unsigned src_stride
,
870 unsigned src_x
, unsigned src_y
,
871 unsigned width
, unsigned height
);
874 * Swizzle operations.
877 /* Compose two sets of swizzles.
878 * If V is a 4D vector and the function parameters represent functions that
879 * swizzle vector components, this holds:
880 * swz2(swz1(V)) = dst(V)
882 void util_format_compose_swizzles(const unsigned char swz1
[4],
883 const unsigned char swz2
[4],
884 unsigned char dst
[4]);
886 void util_format_swizzle_4f(float *dst
, const float *src
,
887 const unsigned char swz
[4]);
889 void util_format_unswizzle_4f(float *dst
, const float *src
,
890 const unsigned char swz
[4]);
896 #endif /* ! U_FORMAT_H */