revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / util / u_format.h
blobbb3ed72e93256500ccac6fa98a3e62f7e6fb9e16
1 /**************************************************************************
3 * Copyright 2009-2010 Vmware, Inc.
4 * All Rights Reserved.
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
16 * of the Software.
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 **************************************************************************/
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
33 #include "pipe/p_format.h"
34 #include "util/u_debug.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
41 /**
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 {
48 /**
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,
54 /**
55 * Formats with sub-sampled channels.
57 * This is for formats like YV12 where there is less than one sample per
58 * pixel.
60 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
62 /**
63 * S3 Texture Compression formats.
65 UTIL_FORMAT_LAYOUT_S3TC = 4,
67 /**
68 * Red-Green Texture Compression formats.
70 UTIL_FORMAT_LAYOUT_RGTC = 5,
72 /**
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 */
82 unsigned width;
84 /** Block height in pixels */
85 unsigned height;
87 /** Block size in bits */
88 unsigned 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
122 unsigned type:6;
123 unsigned normalized:1;
124 unsigned size:9;
128 struct util_format_description
130 enum pipe_format format;
132 const char *name;
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.
154 unsigned is_array:1;
157 * Whether the pixel format can be described as a bitfield structure.
159 * In particular:
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).
168 unsigned is_mixed:1;
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:
181 * - RGBA
182 * - YUV(A)
183 * - ZS
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.
199 void
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.
210 void
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.
220 void
221 (*fetch_rgba_8unorm)(uint8_t *dst,
222 const uint8_t *src,
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.
231 void
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.
242 void
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.
252 void
253 (*fetch_rgba_float)(float *dst,
254 const uint8_t *src,
255 unsigned i, unsigned j);
258 * Unpack pixels to Z32_UNORM.
259 * Note: strides are in bytes.
261 * Only defined for depth formats.
263 void
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.
274 void
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.
285 void
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.
296 void
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.
307 void
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.
318 void
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);
343 assert(desc);
344 if (!desc) {
345 return "PIPE_FORMAT_???";
348 return desc->name;
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);
356 assert(desc);
357 if (!desc) {
358 return "???";
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);
372 if (!format) {
373 return FALSE;
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);
384 assert(desc);
385 if (!desc) {
386 return FALSE;
389 switch (desc->layout) {
390 case UTIL_FORMAT_LAYOUT_S3TC:
391 case UTIL_FORMAT_LAYOUT_RGTC:
392 /* XXX add other formats in the future */
393 return TRUE;
394 default:
395 return FALSE;
399 static INLINE boolean
400 util_format_is_s3tc(enum pipe_format format)
402 const struct util_format_description *desc = util_format_description(format);
404 assert(desc);
405 if (!desc) {
406 return FALSE;
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);
417 assert(desc);
418 if (!desc) {
419 return FALSE;
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);
430 assert(desc);
431 if (!desc) {
432 return FALSE;
435 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
436 return FALSE;
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
446 * format.
448 * That is, the channels whose values are preserved.
450 static INLINE unsigned
451 util_format_colormask(const struct util_format_description *desc)
453 unsigned colormask;
454 unsigned chan;
456 switch (desc->colorspace) {
457 case UTIL_FORMAT_COLORSPACE_RGB:
458 case UTIL_FORMAT_COLORSPACE_SRGB:
459 case UTIL_FORMAT_COLORSPACE_YUV:
460 colormask = 0;
461 for (chan = 0; chan < 4; ++chan) {
462 if (desc->swizzle[chan] < 4) {
463 colormask |= (1 << chan);
466 return colormask;
467 case UTIL_FORMAT_COLORSPACE_ZS:
468 return 0;
469 default:
470 assert(0);
471 return 0;
476 boolean
477 util_format_is_float(enum pipe_format format);
481 * Whether the src format can be blitted to destation format with a simple
482 * memcpy.
484 boolean
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.
492 boolean
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)
505 unsigned chan;
507 if(desc->block.width != 1 ||
508 desc->block.height != 1 ||
509 desc->block.bits != 32)
510 return FALSE;
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)
515 return FALSE;
516 if(desc->channel[chan].size != 8)
517 return FALSE;
520 return TRUE;
525 * Return total bits needed for the pixel format per block.
527 static INLINE uint
528 util_format_get_blocksizebits(enum pipe_format format)
530 const struct util_format_description *desc = util_format_description(format);
532 assert(desc);
533 if (!desc) {
534 return 0;
537 return desc->block.bits;
541 * Return bytes per block (not pixel) for the given format.
543 static INLINE uint
544 util_format_get_blocksize(enum pipe_format format)
546 uint bits = util_format_get_blocksizebits(format);
548 assert(bits % 8 == 0);
550 return bits / 8;
553 static INLINE uint
554 util_format_get_blockwidth(enum pipe_format format)
556 const struct util_format_description *desc = util_format_description(format);
558 assert(desc);
559 if (!desc) {
560 return 1;
563 return desc->block.width;
566 static INLINE uint
567 util_format_get_blockheight(enum pipe_format format)
569 const struct util_format_description *desc = util_format_description(format);
571 assert(desc);
572 if (!desc) {
573 return 1;
576 return desc->block.height;
579 static INLINE unsigned
580 util_format_get_nblocksx(enum pipe_format format,
581 unsigned x)
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,
589 unsigned y)
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,
597 unsigned width,
598 unsigned height)
600 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
603 static INLINE size_t
604 util_format_get_stride(enum pipe_format format,
605 unsigned width)
607 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
610 static INLINE size_t
611 util_format_get_2d_size(enum pipe_format format,
612 size_t stride,
613 unsigned height)
615 return util_format_get_nblocksy(format, height) * stride;
618 static INLINE uint
619 util_format_get_component_bits(enum pipe_format format,
620 enum util_format_colorspace colorspace,
621 uint component)
623 const struct util_format_description *desc = util_format_description(format);
624 enum util_format_colorspace desc_colorspace;
626 assert(format);
627 if (!format) {
628 return 0;
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;
639 } else {
640 desc_colorspace = desc->colorspace;
643 if (desc_colorspace != colorspace) {
644 return 0;
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;
656 default:
657 return 0;
661 static INLINE boolean
662 util_format_has_alpha(enum pipe_format format)
664 const struct util_format_description *desc = util_format_description(format);
666 assert(format);
667 if (!format) {
668 return FALSE;
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:
676 return FALSE;
677 case UTIL_FORMAT_COLORSPACE_ZS:
678 return FALSE;
679 default:
680 assert(0);
681 return FALSE;
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)
692 switch (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;
719 default:
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)
731 switch (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;
758 default:
759 return format;
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.
778 void
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);
784 void
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);
790 void
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);
796 void
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;
806 boolean
807 util_format_fits_8unorm(const struct util_format_description *format_desc);
809 void
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);
818 #ifdef __cplusplus
819 } // extern "C" {
820 #endif
822 #endif /* ! U_FORMAT_H */