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
112 enum util_format_colorspace {
113 UTIL_FORMAT_COLORSPACE_RGB = 0,
114 UTIL_FORMAT_COLORSPACE_SRGB = 1,
115 UTIL_FORMAT_COLORSPACE_YUV = 2,
116 UTIL_FORMAT_COLORSPACE_ZS = 3
120 struct util_format_channel_description
123 unsigned normalized:1;
128 struct util_format_description
130 enum pipe_format format;
135 * Short name, striped of the prefix, lower case.
137 const char *short_name;
140 * Pixel block dimensions.
142 struct util_format_block block;
144 enum util_format_layout layout;
147 * The number of channels.
149 unsigned nr_channels:3;
152 * Whether all channels have the same number of (whole) bytes.
157 * Whether the pixel format can be described as a bitfield structure.
160 * - pixel depth must be 8, 16, or 32 bits;
161 * - all channels must be unsigned, signed, or void
163 unsigned is_bitmask:1;
166 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
171 * Input channel description.
173 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
175 struct util_format_channel_description channel[4];
178 * Output channel swizzle.
180 * The order is either:
184 * depending on the colorspace.
186 unsigned char swizzle[4];
189 * Colorspace transformation.
191 enum util_format_colorspace colorspace;
194 * Unpack pixel blocks to R8G8B8A8_UNORM.
195 * Note: strides are in bytes.
197 * Only defined for non-depth-stencil formats.
200 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
201 const uint8_t *src, unsigned src_stride,
202 unsigned width, unsigned height);
205 * Pack pixel blocks from R8G8B8A8_UNORM.
206 * Note: strides are in bytes.
208 * Only defined for non-depth-stencil formats.
211 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
212 const uint8_t *src, unsigned src_stride,
213 unsigned width, unsigned height);
216 * Fetch a single pixel (i, j) from a block.
218 * XXX: Only defined for a very few select formats.
221 (*fetch_rgba_8unorm)(uint8_t *dst,
223 unsigned i, unsigned j);
226 * Unpack pixel blocks to R32G32B32A32_FLOAT.
227 * Note: strides are in bytes.
229 * Only defined for non-depth-stencil formats.
232 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
233 const uint8_t *src, unsigned src_stride,
234 unsigned width, unsigned height);
237 * Pack pixel blocks from R32G32B32A32_FLOAT.
238 * Note: strides are in bytes.
240 * Only defined for non-depth-stencil formats.
243 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
244 const float *src, unsigned src_stride,
245 unsigned width, unsigned height);
248 * Fetch a single pixel (i, j) from a block.
250 * Only defined for non-depth-stencil formats.
253 (*fetch_rgba_float)(float *dst,
255 unsigned i, unsigned j);
258 * Unpack pixels to Z32_UNORM.
259 * Note: strides are in bytes.
261 * Only defined for depth formats.
264 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
265 const uint8_t *src, unsigned src_stride,
266 unsigned width, unsigned height);
269 * Pack pixels from Z32_FLOAT.
270 * Note: strides are in bytes.
272 * Only defined for depth formats.
275 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
276 const uint32_t *src, unsigned src_stride,
277 unsigned width, unsigned height);
280 * Unpack pixels to Z32_FLOAT.
281 * Note: strides are in bytes.
283 * Only defined for depth formats.
286 (*unpack_z_float)(float *dst, unsigned dst_stride,
287 const uint8_t *src, unsigned src_stride,
288 unsigned width, unsigned height);
291 * Pack pixels from Z32_FLOAT.
292 * Note: strides are in bytes.
294 * Only defined for depth formats.
297 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
298 const float *src, unsigned src_stride,
299 unsigned width, unsigned height);
302 * Unpack pixels to S8_USCALED.
303 * Note: strides are in bytes.
305 * Only defined for stencil formats.
308 (*unpack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
309 const uint8_t *src, unsigned src_stride,
310 unsigned width, unsigned height);
313 * Pack pixels from S8_USCALED.
314 * Note: strides are in bytes.
316 * Only defined for stencil formats.
319 (*pack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
320 const uint8_t *src, unsigned src_stride,
321 unsigned width, unsigned height);
326 extern const struct util_format_description
327 util_format_description_table[];
330 const struct util_format_description *
331 util_format_description(enum pipe_format format);
335 * Format query functions.
338 static INLINE const char *
339 util_format_name(enum pipe_format format)
341 const struct util_format_description *desc = util_format_description(format);
345 return "PIPE_FORMAT_???";
351 static INLINE const char *
352 util_format_short_name(enum pipe_format format)
354 const struct util_format_description *desc = util_format_description(format);
361 return desc->short_name;
365 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
367 static INLINE boolean
368 util_format_is_plain(enum pipe_format format)
370 const struct util_format_description *desc = util_format_description(format);
376 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
379 static INLINE boolean
380 util_format_is_compressed(enum pipe_format format)
382 const struct util_format_description *desc = util_format_description(format);
389 switch (desc->layout) {
390 case UTIL_FORMAT_LAYOUT_S3TC:
391 case UTIL_FORMAT_LAYOUT_RGTC:
392 /* XXX add other formats in the future */
399 static INLINE boolean
400 util_format_is_s3tc(enum pipe_format format)
402 const struct util_format_description *desc = util_format_description(format);
409 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
412 static INLINE boolean
413 util_format_is_depth_or_stencil(enum pipe_format format)
415 const struct util_format_description *desc = util_format_description(format);
422 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
425 static INLINE boolean
426 util_format_is_depth_and_stencil(enum pipe_format format)
428 const struct util_format_description *desc = util_format_description(format);
435 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
439 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
440 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
445 * Give the RGBA colormask of the channels that can be represented in this
448 * That is, the channels whose values are preserved.
450 static INLINE unsigned
451 util_format_colormask(const struct util_format_description *desc)
456 switch (desc->colorspace) {
457 case UTIL_FORMAT_COLORSPACE_RGB:
458 case UTIL_FORMAT_COLORSPACE_SRGB:
459 case UTIL_FORMAT_COLORSPACE_YUV:
461 for (chan = 0; chan < 4; ++chan) {
462 if (desc->swizzle[chan] < 4) {
463 colormask |= (1 << chan);
467 case UTIL_FORMAT_COLORSPACE_ZS:
477 util_format_is_float(enum pipe_format format);
481 * Whether the src format can be blitted to destation format with a simple
485 util_is_format_compatible(const struct util_format_description *src_desc,
486 const struct util_format_description *dst_desc);
489 * Whether the format is supported by Gallium for the given bindings.
490 * This covers S3TC textures and floating-point render targets.
493 util_format_is_supported(enum pipe_format format, unsigned bind);
496 * Whether this format is a rgab8 variant.
498 * That is, any format that matches the
500 * PIPE_FORMAT_?8?8?8?8_UNORM
502 static INLINE boolean
503 util_format_is_rgba8_variant(const struct util_format_description *desc)
507 if(desc->block.width != 1 ||
508 desc->block.height != 1 ||
509 desc->block.bits != 32)
512 for(chan = 0; chan < 4; ++chan) {
513 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
514 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
516 if(desc->channel[chan].size != 8)
525 * Return total bits needed for the pixel format per block.
528 util_format_get_blocksizebits(enum pipe_format format)
530 const struct util_format_description *desc = util_format_description(format);
537 return desc->block.bits;
541 * Return bytes per block (not pixel) for the given format.
544 util_format_get_blocksize(enum pipe_format format)
546 uint bits = util_format_get_blocksizebits(format);
548 assert(bits % 8 == 0);
554 util_format_get_blockwidth(enum pipe_format format)
556 const struct util_format_description *desc = util_format_description(format);
563 return desc->block.width;
567 util_format_get_blockheight(enum pipe_format format)
569 const struct util_format_description *desc = util_format_description(format);
576 return desc->block.height;
579 static INLINE unsigned
580 util_format_get_nblocksx(enum pipe_format format,
583 unsigned blockwidth = util_format_get_blockwidth(format);
584 return (x + blockwidth - 1) / blockwidth;
587 static INLINE unsigned
588 util_format_get_nblocksy(enum pipe_format format,
591 unsigned blockheight = util_format_get_blockheight(format);
592 return (y + blockheight - 1) / blockheight;
595 static INLINE unsigned
596 util_format_get_nblocks(enum pipe_format format,
600 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
604 util_format_get_stride(enum pipe_format format,
607 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
611 util_format_get_2d_size(enum pipe_format format,
615 return util_format_get_nblocksy(format, height) * stride;
619 util_format_get_component_bits(enum pipe_format format,
620 enum util_format_colorspace colorspace,
623 const struct util_format_description *desc = util_format_description(format);
624 enum util_format_colorspace desc_colorspace;
631 assert(component < 4);
633 /* Treat RGB and SRGB as equivalent. */
634 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
635 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
637 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
638 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
640 desc_colorspace = desc->colorspace;
643 if (desc_colorspace != colorspace) {
647 switch (desc->swizzle[component]) {
648 case UTIL_FORMAT_SWIZZLE_X:
649 return desc->channel[0].size;
650 case UTIL_FORMAT_SWIZZLE_Y:
651 return desc->channel[1].size;
652 case UTIL_FORMAT_SWIZZLE_Z:
653 return desc->channel[2].size;
654 case UTIL_FORMAT_SWIZZLE_W:
655 return desc->channel[3].size;
661 static INLINE boolean
662 util_format_has_alpha(enum pipe_format format)
664 const struct util_format_description *desc = util_format_description(format);
671 switch (desc->colorspace) {
672 case UTIL_FORMAT_COLORSPACE_RGB:
673 case UTIL_FORMAT_COLORSPACE_SRGB:
674 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
675 case UTIL_FORMAT_COLORSPACE_YUV:
677 case UTIL_FORMAT_COLORSPACE_ZS:
686 * Given a linear RGB colorspace format, return the corresponding SRGB
687 * format, or PIPE_FORMAT_NONE if none.
689 static INLINE enum pipe_format
690 util_format_srgb(enum pipe_format format)
693 case PIPE_FORMAT_L8_UNORM:
694 return PIPE_FORMAT_L8_SRGB;
695 case PIPE_FORMAT_L8A8_UNORM:
696 return PIPE_FORMAT_L8A8_SRGB;
697 case PIPE_FORMAT_R8G8B8_UNORM:
698 return PIPE_FORMAT_R8G8B8_SRGB;
699 case PIPE_FORMAT_A8B8G8R8_UNORM:
700 return PIPE_FORMAT_A8B8G8R8_SRGB;
701 case PIPE_FORMAT_X8B8G8R8_UNORM:
702 return PIPE_FORMAT_X8B8G8R8_SRGB;
703 case PIPE_FORMAT_B8G8R8A8_UNORM:
704 return PIPE_FORMAT_B8G8R8A8_SRGB;
705 case PIPE_FORMAT_B8G8R8X8_UNORM:
706 return PIPE_FORMAT_B8G8R8X8_SRGB;
707 case PIPE_FORMAT_A8R8G8B8_UNORM:
708 return PIPE_FORMAT_A8R8G8B8_SRGB;
709 case PIPE_FORMAT_X8R8G8B8_UNORM:
710 return PIPE_FORMAT_X8R8G8B8_SRGB;
711 case PIPE_FORMAT_DXT1_RGB:
712 return PIPE_FORMAT_DXT1_SRGB;
713 case PIPE_FORMAT_DXT1_RGBA:
714 return PIPE_FORMAT_DXT1_SRGBA;
715 case PIPE_FORMAT_DXT3_RGBA:
716 return PIPE_FORMAT_DXT3_SRGBA;
717 case PIPE_FORMAT_DXT5_RGBA:
718 return PIPE_FORMAT_DXT5_SRGBA;
720 return PIPE_FORMAT_NONE;
725 * Given an sRGB format, return the corresponding linear colorspace format.
726 * For non sRGB formats, return the format unchanged.
728 static INLINE enum pipe_format
729 util_format_linear(enum pipe_format format)
732 case PIPE_FORMAT_L8_SRGB:
733 return PIPE_FORMAT_L8_UNORM;
734 case PIPE_FORMAT_L8A8_SRGB:
735 return PIPE_FORMAT_L8A8_UNORM;
736 case PIPE_FORMAT_R8G8B8_SRGB:
737 return PIPE_FORMAT_R8G8B8_UNORM;
738 case PIPE_FORMAT_A8B8G8R8_SRGB:
739 return PIPE_FORMAT_A8B8G8R8_UNORM;
740 case PIPE_FORMAT_X8B8G8R8_SRGB:
741 return PIPE_FORMAT_X8B8G8R8_UNORM;
742 case PIPE_FORMAT_B8G8R8A8_SRGB:
743 return PIPE_FORMAT_B8G8R8A8_UNORM;
744 case PIPE_FORMAT_B8G8R8X8_SRGB:
745 return PIPE_FORMAT_B8G8R8X8_UNORM;
746 case PIPE_FORMAT_A8R8G8B8_SRGB:
747 return PIPE_FORMAT_A8R8G8B8_UNORM;
748 case PIPE_FORMAT_X8R8G8B8_SRGB:
749 return PIPE_FORMAT_X8R8G8B8_UNORM;
750 case PIPE_FORMAT_DXT1_SRGB:
751 return PIPE_FORMAT_DXT1_RGB;
752 case PIPE_FORMAT_DXT1_SRGBA:
753 return PIPE_FORMAT_DXT1_RGBA;
754 case PIPE_FORMAT_DXT3_SRGBA:
755 return PIPE_FORMAT_DXT3_RGBA;
756 case PIPE_FORMAT_DXT5_SRGBA:
757 return PIPE_FORMAT_DXT5_RGBA;
764 * Return the number of components stored.
765 * Formats with block size != 1x1 will always have 1 component (the block).
767 static INLINE unsigned
768 util_format_get_nr_components(enum pipe_format format)
770 const struct util_format_description *desc = util_format_description(format);
771 return desc->nr_channels;
775 * Format access functions.
779 util_format_read_4f(enum pipe_format format,
780 float *dst, unsigned dst_stride,
781 const void *src, unsigned src_stride,
782 unsigned x, unsigned y, unsigned w, unsigned h);
785 util_format_write_4f(enum pipe_format format,
786 const float *src, unsigned src_stride,
787 void *dst, unsigned dst_stride,
788 unsigned x, unsigned y, unsigned w, unsigned h);
791 util_format_read_4ub(enum pipe_format format,
792 uint8_t *dst, unsigned dst_stride,
793 const void *src, unsigned src_stride,
794 unsigned x, unsigned y, unsigned w, unsigned h);
797 util_format_write_4ub(enum pipe_format format,
798 const uint8_t *src, unsigned src_stride,
799 void *dst, unsigned dst_stride,
800 unsigned x, unsigned y, unsigned w, unsigned h);
803 * Generic format conversion;
807 util_format_fits_8unorm(const struct util_format_description *format_desc);
810 util_format_translate(enum pipe_format dst_format,
811 void *dst, unsigned dst_stride,
812 unsigned dst_x, unsigned dst_y,
813 enum pipe_format src_format,
814 const void *src, unsigned src_stride,
815 unsigned src_x, unsigned src_y,
816 unsigned width, unsigned height);
822 #endif /* ! U_FORMAT_H */