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_depth_or_stencil(enum pipe_format format
)
416 const struct util_format_description
*desc
= util_format_description(format
);
423 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
426 static INLINE boolean
427 util_format_is_depth_and_stencil(enum pipe_format format
)
429 const struct util_format_description
*desc
= util_format_description(format
);
436 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
440 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
441 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
446 * Give the RGBA colormask of the channels that can be represented in this
449 * That is, the channels whose values are preserved.
451 static INLINE
unsigned
452 util_format_colormask(const struct util_format_description
*desc
)
457 switch (desc
->colorspace
) {
458 case UTIL_FORMAT_COLORSPACE_RGB
:
459 case UTIL_FORMAT_COLORSPACE_SRGB
:
460 case UTIL_FORMAT_COLORSPACE_YUV
:
462 for (chan
= 0; chan
< 4; ++chan
) {
463 if (desc
->swizzle
[chan
] < 4) {
464 colormask
|= (1 << chan
);
468 case UTIL_FORMAT_COLORSPACE_ZS
:
478 util_format_is_float(enum pipe_format format
);
482 util_format_is_rgb_no_alpha(enum pipe_format format
);
486 util_format_is_luminance(enum pipe_format format
);
490 util_format_is_luminance_alpha(enum pipe_format format
);
494 util_format_is_intensity(enum pipe_format format
);
498 * Whether the src format can be blitted to destation format with a simple
502 util_is_format_compatible(const struct util_format_description
*src_desc
,
503 const struct util_format_description
*dst_desc
);
506 * Whether the format is supported by Gallium for the given bindings.
507 * This covers S3TC textures and floating-point render targets.
510 util_format_is_supported(enum pipe_format format
, unsigned bind
);
513 * Whether this format is a rgab8 variant.
515 * That is, any format that matches the
517 * PIPE_FORMAT_?8?8?8?8_UNORM
519 static INLINE boolean
520 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
524 if(desc
->block
.width
!= 1 ||
525 desc
->block
.height
!= 1 ||
526 desc
->block
.bits
!= 32)
529 for(chan
= 0; chan
< 4; ++chan
) {
530 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
531 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
533 if(desc
->channel
[chan
].size
!= 8)
542 * Return total bits needed for the pixel format per block.
545 util_format_get_blocksizebits(enum pipe_format format
)
547 const struct util_format_description
*desc
= util_format_description(format
);
554 return desc
->block
.bits
;
558 * Return bytes per block (not pixel) for the given format.
561 util_format_get_blocksize(enum pipe_format format
)
563 uint bits
= util_format_get_blocksizebits(format
);
565 assert(bits
% 8 == 0);
571 util_format_get_blockwidth(enum pipe_format format
)
573 const struct util_format_description
*desc
= util_format_description(format
);
580 return desc
->block
.width
;
584 util_format_get_blockheight(enum pipe_format format
)
586 const struct util_format_description
*desc
= util_format_description(format
);
593 return desc
->block
.height
;
596 static INLINE
unsigned
597 util_format_get_nblocksx(enum pipe_format format
,
600 unsigned blockwidth
= util_format_get_blockwidth(format
);
601 return (x
+ blockwidth
- 1) / blockwidth
;
604 static INLINE
unsigned
605 util_format_get_nblocksy(enum pipe_format format
,
608 unsigned blockheight
= util_format_get_blockheight(format
);
609 return (y
+ blockheight
- 1) / blockheight
;
612 static INLINE
unsigned
613 util_format_get_nblocks(enum pipe_format format
,
617 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
621 util_format_get_stride(enum pipe_format format
,
624 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
628 util_format_get_2d_size(enum pipe_format format
,
632 return util_format_get_nblocksy(format
, height
) * stride
;
636 util_format_get_component_bits(enum pipe_format format
,
637 enum util_format_colorspace colorspace
,
640 const struct util_format_description
*desc
= util_format_description(format
);
641 enum util_format_colorspace desc_colorspace
;
648 assert(component
< 4);
650 /* Treat RGB and SRGB as equivalent. */
651 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
652 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
654 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
655 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
657 desc_colorspace
= desc
->colorspace
;
660 if (desc_colorspace
!= colorspace
) {
664 switch (desc
->swizzle
[component
]) {
665 case UTIL_FORMAT_SWIZZLE_X
:
666 return desc
->channel
[0].size
;
667 case UTIL_FORMAT_SWIZZLE_Y
:
668 return desc
->channel
[1].size
;
669 case UTIL_FORMAT_SWIZZLE_Z
:
670 return desc
->channel
[2].size
;
671 case UTIL_FORMAT_SWIZZLE_W
:
672 return desc
->channel
[3].size
;
678 static INLINE boolean
679 util_format_has_alpha(enum pipe_format format
)
681 const struct util_format_description
*desc
= util_format_description(format
);
688 switch (desc
->colorspace
) {
689 case UTIL_FORMAT_COLORSPACE_RGB
:
690 case UTIL_FORMAT_COLORSPACE_SRGB
:
691 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
692 case UTIL_FORMAT_COLORSPACE_YUV
:
694 case UTIL_FORMAT_COLORSPACE_ZS
:
703 * Given a linear RGB colorspace format, return the corresponding SRGB
704 * format, or PIPE_FORMAT_NONE if none.
706 static INLINE
enum pipe_format
707 util_format_srgb(enum pipe_format format
)
710 case PIPE_FORMAT_L8_UNORM
:
711 return PIPE_FORMAT_L8_SRGB
;
712 case PIPE_FORMAT_L8A8_UNORM
:
713 return PIPE_FORMAT_L8A8_SRGB
;
714 case PIPE_FORMAT_R8G8B8_UNORM
:
715 return PIPE_FORMAT_R8G8B8_SRGB
;
716 case PIPE_FORMAT_A8B8G8R8_UNORM
:
717 return PIPE_FORMAT_A8B8G8R8_SRGB
;
718 case PIPE_FORMAT_X8B8G8R8_UNORM
:
719 return PIPE_FORMAT_X8B8G8R8_SRGB
;
720 case PIPE_FORMAT_B8G8R8A8_UNORM
:
721 return PIPE_FORMAT_B8G8R8A8_SRGB
;
722 case PIPE_FORMAT_B8G8R8X8_UNORM
:
723 return PIPE_FORMAT_B8G8R8X8_SRGB
;
724 case PIPE_FORMAT_A8R8G8B8_UNORM
:
725 return PIPE_FORMAT_A8R8G8B8_SRGB
;
726 case PIPE_FORMAT_X8R8G8B8_UNORM
:
727 return PIPE_FORMAT_X8R8G8B8_SRGB
;
728 case PIPE_FORMAT_DXT1_RGB
:
729 return PIPE_FORMAT_DXT1_SRGB
;
730 case PIPE_FORMAT_DXT1_RGBA
:
731 return PIPE_FORMAT_DXT1_SRGBA
;
732 case PIPE_FORMAT_DXT3_RGBA
:
733 return PIPE_FORMAT_DXT3_SRGBA
;
734 case PIPE_FORMAT_DXT5_RGBA
:
735 return PIPE_FORMAT_DXT5_SRGBA
;
737 return PIPE_FORMAT_NONE
;
742 * Given an sRGB format, return the corresponding linear colorspace format.
743 * For non sRGB formats, return the format unchanged.
745 static INLINE
enum pipe_format
746 util_format_linear(enum pipe_format format
)
749 case PIPE_FORMAT_L8_SRGB
:
750 return PIPE_FORMAT_L8_UNORM
;
751 case PIPE_FORMAT_L8A8_SRGB
:
752 return PIPE_FORMAT_L8A8_UNORM
;
753 case PIPE_FORMAT_R8G8B8_SRGB
:
754 return PIPE_FORMAT_R8G8B8_UNORM
;
755 case PIPE_FORMAT_A8B8G8R8_SRGB
:
756 return PIPE_FORMAT_A8B8G8R8_UNORM
;
757 case PIPE_FORMAT_X8B8G8R8_SRGB
:
758 return PIPE_FORMAT_X8B8G8R8_UNORM
;
759 case PIPE_FORMAT_B8G8R8A8_SRGB
:
760 return PIPE_FORMAT_B8G8R8A8_UNORM
;
761 case PIPE_FORMAT_B8G8R8X8_SRGB
:
762 return PIPE_FORMAT_B8G8R8X8_UNORM
;
763 case PIPE_FORMAT_A8R8G8B8_SRGB
:
764 return PIPE_FORMAT_A8R8G8B8_UNORM
;
765 case PIPE_FORMAT_X8R8G8B8_SRGB
:
766 return PIPE_FORMAT_X8R8G8B8_UNORM
;
767 case PIPE_FORMAT_DXT1_SRGB
:
768 return PIPE_FORMAT_DXT1_RGB
;
769 case PIPE_FORMAT_DXT1_SRGBA
:
770 return PIPE_FORMAT_DXT1_RGBA
;
771 case PIPE_FORMAT_DXT3_SRGBA
:
772 return PIPE_FORMAT_DXT3_RGBA
;
773 case PIPE_FORMAT_DXT5_SRGBA
:
774 return PIPE_FORMAT_DXT5_RGBA
;
781 * Return the number of components stored.
782 * Formats with block size != 1x1 will always have 1 component (the block).
784 static INLINE
unsigned
785 util_format_get_nr_components(enum pipe_format format
)
787 const struct util_format_description
*desc
= util_format_description(format
);
788 return desc
->nr_channels
;
792 * Format access functions.
796 util_format_read_4f(enum pipe_format format
,
797 float *dst
, unsigned dst_stride
,
798 const void *src
, unsigned src_stride
,
799 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
802 util_format_write_4f(enum pipe_format format
,
803 const float *src
, unsigned src_stride
,
804 void *dst
, unsigned dst_stride
,
805 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
808 util_format_read_4ub(enum pipe_format format
,
809 uint8_t *dst
, unsigned dst_stride
,
810 const void *src
, unsigned src_stride
,
811 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
814 util_format_write_4ub(enum pipe_format format
,
815 const uint8_t *src
, unsigned src_stride
,
816 void *dst
, unsigned dst_stride
,
817 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
820 * Generic format conversion;
824 util_format_fits_8unorm(const struct util_format_description
*format_desc
);
827 util_format_translate(enum pipe_format dst_format
,
828 void *dst
, unsigned dst_stride
,
829 unsigned dst_x
, unsigned dst_y
,
830 enum pipe_format src_format
,
831 const void *src
, unsigned src_stride
,
832 unsigned src_x
, unsigned src_y
,
833 unsigned width
, unsigned height
);
836 * Swizzle operations.
839 /* Compose two sets of swizzles.
840 * If V is a 4D vector and the function parameters represent functions that
841 * swizzle vector components, this holds:
842 * swz2(swz1(V)) = dst(V)
844 void util_format_compose_swizzles(const unsigned char swz1
[4],
845 const unsigned char swz2
[4],
846 unsigned char dst
[4]);
848 void util_format_swizzle_4f(float *dst
, const float *src
,
849 const unsigned char swz
[4]);
851 void util_format_unswizzle_4f(float *dst
, const float *src
,
852 const unsigned char swz
[4]);
858 #endif /* ! U_FORMAT_H */