gallium: add ASTC formats
[mesa.git] / src / gallium / auxiliary / util / u_format.h
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 Vmware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
31
32
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
36
37 union pipe_color_union;
38
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /**
46 * Describe how to pack/unpack pixels into/from the prescribed format.
47 *
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.
50 */
51 enum util_format_layout {
52 /**
53 * Formats with util_format_block::width == util_format_block::height == 1
54 * that can be described as an ordinary data structure.
55 */
56 UTIL_FORMAT_LAYOUT_PLAIN = 0,
57
58 /**
59 * Formats with sub-sampled channels.
60 *
61 * This is for formats like YVYU where there is less than one sample per
62 * pixel.
63 */
64 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
65
66 /**
67 * S3 Texture Compression formats.
68 */
69 UTIL_FORMAT_LAYOUT_S3TC = 4,
70
71 /**
72 * Red-Green Texture Compression formats.
73 */
74 UTIL_FORMAT_LAYOUT_RGTC = 5,
75
76 /**
77 * Ericsson Texture Compression
78 */
79 UTIL_FORMAT_LAYOUT_ETC = 6,
80
81 /**
82 * BC6/7 Texture Compression
83 */
84 UTIL_FORMAT_LAYOUT_BPTC = 7,
85
86 /**
87 * ASTC
88 */
89 UTIL_FORMAT_LAYOUT_ASTC = 8,
90
91 /**
92 * Everything else that doesn't fit in any of the above layouts.
93 */
94 UTIL_FORMAT_LAYOUT_OTHER = 9
95 };
96
97
98 struct util_format_block
99 {
100 /** Block width in pixels */
101 unsigned width;
102
103 /** Block height in pixels */
104 unsigned height;
105
106 /** Block size in bits */
107 unsigned bits;
108 };
109
110
111 enum util_format_type {
112 UTIL_FORMAT_TYPE_VOID = 0,
113 UTIL_FORMAT_TYPE_UNSIGNED = 1,
114 UTIL_FORMAT_TYPE_SIGNED = 2,
115 UTIL_FORMAT_TYPE_FIXED = 3,
116 UTIL_FORMAT_TYPE_FLOAT = 4
117 };
118
119
120 enum util_format_swizzle {
121 UTIL_FORMAT_SWIZZLE_X = 0,
122 UTIL_FORMAT_SWIZZLE_Y = 1,
123 UTIL_FORMAT_SWIZZLE_Z = 2,
124 UTIL_FORMAT_SWIZZLE_W = 3,
125 UTIL_FORMAT_SWIZZLE_0 = 4,
126 UTIL_FORMAT_SWIZZLE_1 = 5,
127 UTIL_FORMAT_SWIZZLE_NONE = 6,
128 UTIL_FORMAT_SWIZZLE_MAX = 7 /**< Number of enums counter (must be last) */
129 };
130
131
132 enum util_format_colorspace {
133 UTIL_FORMAT_COLORSPACE_RGB = 0,
134 UTIL_FORMAT_COLORSPACE_SRGB = 1,
135 UTIL_FORMAT_COLORSPACE_YUV = 2,
136 UTIL_FORMAT_COLORSPACE_ZS = 3
137 };
138
139
140 struct util_format_channel_description
141 {
142 unsigned type:5; /**< UTIL_FORMAT_TYPE_x */
143 unsigned normalized:1;
144 unsigned pure_integer:1;
145 unsigned size:9; /**< bits per channel */
146 unsigned shift:16; /** number of bits from lsb */
147 };
148
149
150 struct util_format_description
151 {
152 enum pipe_format format;
153
154 const char *name;
155
156 /**
157 * Short name, striped of the prefix, lower case.
158 */
159 const char *short_name;
160
161 /**
162 * Pixel block dimensions.
163 */
164 struct util_format_block block;
165
166 enum util_format_layout layout;
167
168 /**
169 * The number of channels.
170 */
171 unsigned nr_channels:3;
172
173 /**
174 * Whether all channels have the same number of (whole) bytes and type.
175 */
176 unsigned is_array:1;
177
178 /**
179 * Whether the pixel format can be described as a bitfield structure.
180 *
181 * In particular:
182 * - pixel depth must be 8, 16, or 32 bits;
183 * - all channels must be unsigned, signed, or void
184 */
185 unsigned is_bitmask:1;
186
187 /**
188 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
189 */
190 unsigned is_mixed:1;
191
192 /**
193 * Input channel description, in the order XYZW.
194 *
195 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
196 *
197 * If each channel is accessed as an individual N-byte value, X is always
198 * at the lowest address in memory, Y is always next, and so on. For all
199 * currently-defined formats, the N-byte value has native endianness.
200 *
201 * If instead a group of channels is accessed as a single N-byte value,
202 * the order of the channels within that value depends on endianness.
203 * For big-endian targets, X is the most significant subvalue,
204 * otherwise it is the least significant one.
205 *
206 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
207 *
208 * 0 1 2 3
209 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
210 * big-endian: X Yu Ym Yl
211 *
212 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
213 *
214 * 0 1
215 * msb lsb msb lsb
216 * little-endian: YYYXXXXX WZZZZZYY
217 * big-endian: XXXXXYYY YYZZZZZW
218 */
219 struct util_format_channel_description channel[4];
220
221 /**
222 * Output channel swizzle.
223 *
224 * The order is either:
225 * - RGBA
226 * - YUV(A)
227 * - ZS
228 * depending on the colorspace.
229 */
230 unsigned char swizzle[4];
231
232 /**
233 * Colorspace transformation.
234 */
235 enum util_format_colorspace colorspace;
236
237 /**
238 * Unpack pixel blocks to R8G8B8A8_UNORM.
239 * Note: strides are in bytes.
240 *
241 * Only defined for non-depth-stencil formats.
242 */
243 void
244 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
245 const uint8_t *src, unsigned src_stride,
246 unsigned width, unsigned height);
247
248 /**
249 * Pack pixel blocks from R8G8B8A8_UNORM.
250 * Note: strides are in bytes.
251 *
252 * Only defined for non-depth-stencil formats.
253 */
254 void
255 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
256 const uint8_t *src, unsigned src_stride,
257 unsigned width, unsigned height);
258
259 /**
260 * Fetch a single pixel (i, j) from a block.
261 *
262 * XXX: Only defined for a very few select formats.
263 */
264 void
265 (*fetch_rgba_8unorm)(uint8_t *dst,
266 const uint8_t *src,
267 unsigned i, unsigned j);
268
269 /**
270 * Unpack pixel blocks to R32G32B32A32_FLOAT.
271 * Note: strides are in bytes.
272 *
273 * Only defined for non-depth-stencil formats.
274 */
275 void
276 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
277 const uint8_t *src, unsigned src_stride,
278 unsigned width, unsigned height);
279
280 /**
281 * Pack pixel blocks from R32G32B32A32_FLOAT.
282 * Note: strides are in bytes.
283 *
284 * Only defined for non-depth-stencil formats.
285 */
286 void
287 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
288 const float *src, unsigned src_stride,
289 unsigned width, unsigned height);
290
291 /**
292 * Fetch a single pixel (i, j) from a block.
293 *
294 * Only defined for non-depth-stencil and non-integer formats.
295 */
296 void
297 (*fetch_rgba_float)(float *dst,
298 const uint8_t *src,
299 unsigned i, unsigned j);
300
301 /**
302 * Unpack pixels to Z32_UNORM.
303 * Note: strides are in bytes.
304 *
305 * Only defined for depth formats.
306 */
307 void
308 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
309 const uint8_t *src, unsigned src_stride,
310 unsigned width, unsigned height);
311
312 /**
313 * Pack pixels from Z32_FLOAT.
314 * Note: strides are in bytes.
315 *
316 * Only defined for depth formats.
317 */
318 void
319 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
320 const uint32_t *src, unsigned src_stride,
321 unsigned width, unsigned height);
322
323 /**
324 * Unpack pixels to Z32_FLOAT.
325 * Note: strides are in bytes.
326 *
327 * Only defined for depth formats.
328 */
329 void
330 (*unpack_z_float)(float *dst, unsigned dst_stride,
331 const uint8_t *src, unsigned src_stride,
332 unsigned width, unsigned height);
333
334 /**
335 * Pack pixels from Z32_FLOAT.
336 * Note: strides are in bytes.
337 *
338 * Only defined for depth formats.
339 */
340 void
341 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
342 const float *src, unsigned src_stride,
343 unsigned width, unsigned height);
344
345 /**
346 * Unpack pixels to S8_UINT.
347 * Note: strides are in bytes.
348 *
349 * Only defined for stencil formats.
350 */
351 void
352 (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
353 const uint8_t *src, unsigned src_stride,
354 unsigned width, unsigned height);
355
356 /**
357 * Pack pixels from S8_UINT.
358 * Note: strides are in bytes.
359 *
360 * Only defined for stencil formats.
361 */
362 void
363 (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
364 const uint8_t *src, unsigned src_stride,
365 unsigned width, unsigned height);
366
367 /**
368 * Unpack pixel blocks to R32G32B32A32_UINT.
369 * Note: strides are in bytes.
370 *
371 * Only defined for INT formats.
372 */
373 void
374 (*unpack_rgba_uint)(uint32_t *dst, unsigned dst_stride,
375 const uint8_t *src, unsigned src_stride,
376 unsigned width, unsigned height);
377
378 void
379 (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
380 const uint32_t *src, unsigned src_stride,
381 unsigned width, unsigned height);
382
383 /**
384 * Unpack pixel blocks to R32G32B32A32_SINT.
385 * Note: strides are in bytes.
386 *
387 * Only defined for INT formats.
388 */
389 void
390 (*unpack_rgba_sint)(int32_t *dst, unsigned dst_stride,
391 const uint8_t *src, unsigned src_stride,
392 unsigned width, unsigned height);
393
394 void
395 (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
396 const int32_t *src, unsigned src_stride,
397 unsigned width, unsigned height);
398
399 /**
400 * Fetch a single pixel (i, j) from a block.
401 *
402 * Only defined for unsigned (pure) integer formats.
403 */
404 void
405 (*fetch_rgba_uint)(uint32_t *dst,
406 const uint8_t *src,
407 unsigned i, unsigned j);
408
409 /**
410 * Fetch a single pixel (i, j) from a block.
411 *
412 * Only defined for signed (pure) integer formats.
413 */
414 void
415 (*fetch_rgba_sint)(int32_t *dst,
416 const uint8_t *src,
417 unsigned i, unsigned j);
418 };
419
420
421 extern const struct util_format_description
422 util_format_description_table[];
423
424
425 const struct util_format_description *
426 util_format_description(enum pipe_format format);
427
428
429 /*
430 * Format query functions.
431 */
432
433 static inline const char *
434 util_format_name(enum pipe_format format)
435 {
436 const struct util_format_description *desc = util_format_description(format);
437
438 assert(desc);
439 if (!desc) {
440 return "PIPE_FORMAT_???";
441 }
442
443 return desc->name;
444 }
445
446 static inline const char *
447 util_format_short_name(enum pipe_format format)
448 {
449 const struct util_format_description *desc = util_format_description(format);
450
451 assert(desc);
452 if (!desc) {
453 return "???";
454 }
455
456 return desc->short_name;
457 }
458
459 /**
460 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
461 */
462 static inline boolean
463 util_format_is_plain(enum pipe_format format)
464 {
465 const struct util_format_description *desc = util_format_description(format);
466
467 if (!format) {
468 return FALSE;
469 }
470
471 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
472 }
473
474 static inline boolean
475 util_format_is_compressed(enum pipe_format format)
476 {
477 const struct util_format_description *desc = util_format_description(format);
478
479 assert(desc);
480 if (!desc) {
481 return FALSE;
482 }
483
484 switch (desc->layout) {
485 case UTIL_FORMAT_LAYOUT_S3TC:
486 case UTIL_FORMAT_LAYOUT_RGTC:
487 case UTIL_FORMAT_LAYOUT_ETC:
488 case UTIL_FORMAT_LAYOUT_BPTC:
489 case UTIL_FORMAT_LAYOUT_ASTC:
490 /* XXX add other formats in the future */
491 return TRUE;
492 default:
493 return FALSE;
494 }
495 }
496
497 static inline boolean
498 util_format_is_s3tc(enum pipe_format format)
499 {
500 const struct util_format_description *desc = util_format_description(format);
501
502 assert(desc);
503 if (!desc) {
504 return FALSE;
505 }
506
507 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
508 }
509
510 static inline boolean
511 util_format_is_srgb(enum pipe_format format)
512 {
513 const struct util_format_description *desc = util_format_description(format);
514 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
515 }
516
517 static inline boolean
518 util_format_has_depth(const struct util_format_description *desc)
519 {
520 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
521 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
522 }
523
524 static inline boolean
525 util_format_has_stencil(const struct util_format_description *desc)
526 {
527 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
528 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
529 }
530
531 static inline boolean
532 util_format_is_depth_or_stencil(enum pipe_format format)
533 {
534 const struct util_format_description *desc = util_format_description(format);
535
536 assert(desc);
537 if (!desc) {
538 return FALSE;
539 }
540
541 return util_format_has_depth(desc) ||
542 util_format_has_stencil(desc);
543 }
544
545 static inline boolean
546 util_format_is_depth_and_stencil(enum pipe_format format)
547 {
548 const struct util_format_description *desc = util_format_description(format);
549
550 assert(desc);
551 if (!desc) {
552 return FALSE;
553 }
554
555 return util_format_has_depth(desc) &&
556 util_format_has_stencil(desc);
557 }
558
559
560 /**
561 * Calculates the depth format type based upon the incoming format description.
562 */
563 static inline unsigned
564 util_get_depth_format_type(const struct util_format_description *desc)
565 {
566 unsigned depth_channel = desc->swizzle[0];
567 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
568 depth_channel != UTIL_FORMAT_SWIZZLE_NONE) {
569 return desc->channel[depth_channel].type;
570 } else {
571 return UTIL_FORMAT_TYPE_VOID;
572 }
573 }
574
575
576 /**
577 * Calculates the MRD for the depth format. MRD is used in depth bias
578 * for UNORM and unbound depth buffers. When the depth buffer is floating
579 * point, the depth bias calculation does not use the MRD. However, the
580 * default MRD will be 1.0 / ((1 << 24) - 1).
581 */
582 double
583 util_get_depth_format_mrd(const struct util_format_description *desc);
584
585
586 /**
587 * Return whether this is an RGBA, Z, S, or combined ZS format.
588 * Useful for initializing pipe_blit_info::mask.
589 */
590 static inline unsigned
591 util_format_get_mask(enum pipe_format format)
592 {
593 const struct util_format_description *desc =
594 util_format_description(format);
595
596 if (!desc)
597 return 0;
598
599 if (util_format_has_depth(desc)) {
600 if (util_format_has_stencil(desc)) {
601 return PIPE_MASK_ZS;
602 } else {
603 return PIPE_MASK_Z;
604 }
605 } else {
606 if (util_format_has_stencil(desc)) {
607 return PIPE_MASK_S;
608 } else {
609 return PIPE_MASK_RGBA;
610 }
611 }
612 }
613
614 /**
615 * Give the RGBA colormask of the channels that can be represented in this
616 * format.
617 *
618 * That is, the channels whose values are preserved.
619 */
620 static inline unsigned
621 util_format_colormask(const struct util_format_description *desc)
622 {
623 unsigned colormask;
624 unsigned chan;
625
626 switch (desc->colorspace) {
627 case UTIL_FORMAT_COLORSPACE_RGB:
628 case UTIL_FORMAT_COLORSPACE_SRGB:
629 case UTIL_FORMAT_COLORSPACE_YUV:
630 colormask = 0;
631 for (chan = 0; chan < 4; ++chan) {
632 if (desc->swizzle[chan] < 4) {
633 colormask |= (1 << chan);
634 }
635 }
636 return colormask;
637 case UTIL_FORMAT_COLORSPACE_ZS:
638 return 0;
639 default:
640 assert(0);
641 return 0;
642 }
643 }
644
645
646 /**
647 * Checks if color mask covers every channel for the specified format
648 *
649 * @param desc a format description to check colormask with
650 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
651 */
652 static inline boolean
653 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
654 {
655 return (~colormask & util_format_colormask(desc)) == 0;
656 }
657
658
659 boolean
660 util_format_is_float(enum pipe_format format);
661
662
663 boolean
664 util_format_has_alpha(enum pipe_format format);
665
666
667 boolean
668 util_format_is_luminance(enum pipe_format format);
669
670 boolean
671 util_format_is_alpha(enum pipe_format format);
672
673 boolean
674 util_format_is_luminance_alpha(enum pipe_format format);
675
676
677 boolean
678 util_format_is_intensity(enum pipe_format format);
679
680 boolean
681 util_format_is_subsampled_422(enum pipe_format format);
682
683 boolean
684 util_format_is_pure_integer(enum pipe_format format);
685
686 boolean
687 util_format_is_pure_sint(enum pipe_format format);
688
689 boolean
690 util_format_is_pure_uint(enum pipe_format format);
691
692 boolean
693 util_format_is_snorm(enum pipe_format format);
694
695 boolean
696 util_format_is_snorm8(enum pipe_format format);
697
698 /**
699 * Check if the src format can be blitted to the destination format with
700 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
701 * the reverse.
702 */
703 boolean
704 util_is_format_compatible(const struct util_format_description *src_desc,
705 const struct util_format_description *dst_desc);
706
707 /**
708 * Whether the format is supported by Gallium for the given bindings.
709 * This covers S3TC textures and floating-point render targets.
710 */
711 boolean
712 util_format_is_supported(enum pipe_format format, unsigned bind);
713
714 /**
715 * Whether this format is a rgab8 variant.
716 *
717 * That is, any format that matches the
718 *
719 * PIPE_FORMAT_?8?8?8?8_UNORM
720 */
721 static inline boolean
722 util_format_is_rgba8_variant(const struct util_format_description *desc)
723 {
724 unsigned chan;
725
726 if(desc->block.width != 1 ||
727 desc->block.height != 1 ||
728 desc->block.bits != 32)
729 return FALSE;
730
731 for(chan = 0; chan < 4; ++chan) {
732 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
733 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
734 return FALSE;
735 if(desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED &&
736 !desc->channel[chan].normalized)
737 return FALSE;
738 if(desc->channel[chan].size != 8)
739 return FALSE;
740 }
741
742 return TRUE;
743 }
744
745
746 /**
747 * Return total bits needed for the pixel format per block.
748 */
749 static inline uint
750 util_format_get_blocksizebits(enum pipe_format format)
751 {
752 const struct util_format_description *desc = util_format_description(format);
753
754 assert(desc);
755 if (!desc) {
756 return 0;
757 }
758
759 return desc->block.bits;
760 }
761
762 /**
763 * Return bytes per block (not pixel) for the given format.
764 */
765 static inline uint
766 util_format_get_blocksize(enum pipe_format format)
767 {
768 uint bits = util_format_get_blocksizebits(format);
769 uint bytes = bits / 8;
770
771 assert(bits % 8 == 0);
772 assert(bytes > 0);
773 if (bytes == 0) {
774 bytes = 1;
775 }
776
777 return bytes;
778 }
779
780 static inline uint
781 util_format_get_blockwidth(enum pipe_format format)
782 {
783 const struct util_format_description *desc = util_format_description(format);
784
785 assert(desc);
786 if (!desc) {
787 return 1;
788 }
789
790 return desc->block.width;
791 }
792
793 static inline uint
794 util_format_get_blockheight(enum pipe_format format)
795 {
796 const struct util_format_description *desc = util_format_description(format);
797
798 assert(desc);
799 if (!desc) {
800 return 1;
801 }
802
803 return desc->block.height;
804 }
805
806 static inline unsigned
807 util_format_get_nblocksx(enum pipe_format format,
808 unsigned x)
809 {
810 unsigned blockwidth = util_format_get_blockwidth(format);
811 return (x + blockwidth - 1) / blockwidth;
812 }
813
814 static inline unsigned
815 util_format_get_nblocksy(enum pipe_format format,
816 unsigned y)
817 {
818 unsigned blockheight = util_format_get_blockheight(format);
819 return (y + blockheight - 1) / blockheight;
820 }
821
822 static inline unsigned
823 util_format_get_nblocks(enum pipe_format format,
824 unsigned width,
825 unsigned height)
826 {
827 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
828 }
829
830 static inline size_t
831 util_format_get_stride(enum pipe_format format,
832 unsigned width)
833 {
834 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
835 }
836
837 static inline size_t
838 util_format_get_2d_size(enum pipe_format format,
839 size_t stride,
840 unsigned height)
841 {
842 return util_format_get_nblocksy(format, height) * stride;
843 }
844
845 static inline uint
846 util_format_get_component_bits(enum pipe_format format,
847 enum util_format_colorspace colorspace,
848 uint component)
849 {
850 const struct util_format_description *desc = util_format_description(format);
851 enum util_format_colorspace desc_colorspace;
852
853 assert(format);
854 if (!format) {
855 return 0;
856 }
857
858 assert(component < 4);
859
860 /* Treat RGB and SRGB as equivalent. */
861 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
862 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
863 }
864 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
865 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
866 } else {
867 desc_colorspace = desc->colorspace;
868 }
869
870 if (desc_colorspace != colorspace) {
871 return 0;
872 }
873
874 switch (desc->swizzle[component]) {
875 case UTIL_FORMAT_SWIZZLE_X:
876 return desc->channel[0].size;
877 case UTIL_FORMAT_SWIZZLE_Y:
878 return desc->channel[1].size;
879 case UTIL_FORMAT_SWIZZLE_Z:
880 return desc->channel[2].size;
881 case UTIL_FORMAT_SWIZZLE_W:
882 return desc->channel[3].size;
883 default:
884 return 0;
885 }
886 }
887
888 /**
889 * Given a linear RGB colorspace format, return the corresponding SRGB
890 * format, or PIPE_FORMAT_NONE if none.
891 */
892 static inline enum pipe_format
893 util_format_srgb(enum pipe_format format)
894 {
895 if (util_format_is_srgb(format))
896 return format;
897
898 switch (format) {
899 case PIPE_FORMAT_L8_UNORM:
900 return PIPE_FORMAT_L8_SRGB;
901 case PIPE_FORMAT_L8A8_UNORM:
902 return PIPE_FORMAT_L8A8_SRGB;
903 case PIPE_FORMAT_R8G8B8_UNORM:
904 return PIPE_FORMAT_R8G8B8_SRGB;
905 case PIPE_FORMAT_A8B8G8R8_UNORM:
906 return PIPE_FORMAT_A8B8G8R8_SRGB;
907 case PIPE_FORMAT_X8B8G8R8_UNORM:
908 return PIPE_FORMAT_X8B8G8R8_SRGB;
909 case PIPE_FORMAT_B8G8R8A8_UNORM:
910 return PIPE_FORMAT_B8G8R8A8_SRGB;
911 case PIPE_FORMAT_B8G8R8X8_UNORM:
912 return PIPE_FORMAT_B8G8R8X8_SRGB;
913 case PIPE_FORMAT_A8R8G8B8_UNORM:
914 return PIPE_FORMAT_A8R8G8B8_SRGB;
915 case PIPE_FORMAT_X8R8G8B8_UNORM:
916 return PIPE_FORMAT_X8R8G8B8_SRGB;
917 case PIPE_FORMAT_R8G8B8A8_UNORM:
918 return PIPE_FORMAT_R8G8B8A8_SRGB;
919 case PIPE_FORMAT_R8G8B8X8_UNORM:
920 return PIPE_FORMAT_R8G8B8X8_SRGB;
921 case PIPE_FORMAT_DXT1_RGB:
922 return PIPE_FORMAT_DXT1_SRGB;
923 case PIPE_FORMAT_DXT1_RGBA:
924 return PIPE_FORMAT_DXT1_SRGBA;
925 case PIPE_FORMAT_DXT3_RGBA:
926 return PIPE_FORMAT_DXT3_SRGBA;
927 case PIPE_FORMAT_DXT5_RGBA:
928 return PIPE_FORMAT_DXT5_SRGBA;
929 case PIPE_FORMAT_B5G6R5_UNORM:
930 return PIPE_FORMAT_B5G6R5_SRGB;
931 case PIPE_FORMAT_BPTC_RGBA_UNORM:
932 return PIPE_FORMAT_BPTC_SRGBA;
933 case PIPE_FORMAT_ASTC_4x4:
934 return PIPE_FORMAT_ASTC_4x4_SRGB;
935 case PIPE_FORMAT_ASTC_5x4:
936 return PIPE_FORMAT_ASTC_5x4_SRGB;
937 case PIPE_FORMAT_ASTC_5x5:
938 return PIPE_FORMAT_ASTC_5x5_SRGB;
939 case PIPE_FORMAT_ASTC_6x5:
940 return PIPE_FORMAT_ASTC_6x5_SRGB;
941 case PIPE_FORMAT_ASTC_6x6:
942 return PIPE_FORMAT_ASTC_6x6_SRGB;
943 case PIPE_FORMAT_ASTC_8x5:
944 return PIPE_FORMAT_ASTC_8x5_SRGB;
945 case PIPE_FORMAT_ASTC_8x6:
946 return PIPE_FORMAT_ASTC_8x6_SRGB;
947 case PIPE_FORMAT_ASTC_8x8:
948 return PIPE_FORMAT_ASTC_8x8_SRGB;
949 case PIPE_FORMAT_ASTC_10x5:
950 return PIPE_FORMAT_ASTC_10x5_SRGB;
951 case PIPE_FORMAT_ASTC_10x6:
952 return PIPE_FORMAT_ASTC_10x6_SRGB;
953 case PIPE_FORMAT_ASTC_10x8:
954 return PIPE_FORMAT_ASTC_10x8_SRGB;
955 case PIPE_FORMAT_ASTC_10x10:
956 return PIPE_FORMAT_ASTC_10x10_SRGB;
957 case PIPE_FORMAT_ASTC_12x10:
958 return PIPE_FORMAT_ASTC_12x10_SRGB;
959 case PIPE_FORMAT_ASTC_12x12:
960 return PIPE_FORMAT_ASTC_12x12_SRGB;
961
962 default:
963 return PIPE_FORMAT_NONE;
964 }
965 }
966
967 /**
968 * Given an sRGB format, return the corresponding linear colorspace format.
969 * For non sRGB formats, return the format unchanged.
970 */
971 static inline enum pipe_format
972 util_format_linear(enum pipe_format format)
973 {
974 switch (format) {
975 case PIPE_FORMAT_L8_SRGB:
976 return PIPE_FORMAT_L8_UNORM;
977 case PIPE_FORMAT_L8A8_SRGB:
978 return PIPE_FORMAT_L8A8_UNORM;
979 case PIPE_FORMAT_R8G8B8_SRGB:
980 return PIPE_FORMAT_R8G8B8_UNORM;
981 case PIPE_FORMAT_A8B8G8R8_SRGB:
982 return PIPE_FORMAT_A8B8G8R8_UNORM;
983 case PIPE_FORMAT_X8B8G8R8_SRGB:
984 return PIPE_FORMAT_X8B8G8R8_UNORM;
985 case PIPE_FORMAT_B8G8R8A8_SRGB:
986 return PIPE_FORMAT_B8G8R8A8_UNORM;
987 case PIPE_FORMAT_B8G8R8X8_SRGB:
988 return PIPE_FORMAT_B8G8R8X8_UNORM;
989 case PIPE_FORMAT_A8R8G8B8_SRGB:
990 return PIPE_FORMAT_A8R8G8B8_UNORM;
991 case PIPE_FORMAT_X8R8G8B8_SRGB:
992 return PIPE_FORMAT_X8R8G8B8_UNORM;
993 case PIPE_FORMAT_R8G8B8A8_SRGB:
994 return PIPE_FORMAT_R8G8B8A8_UNORM;
995 case PIPE_FORMAT_R8G8B8X8_SRGB:
996 return PIPE_FORMAT_R8G8B8X8_UNORM;
997 case PIPE_FORMAT_DXT1_SRGB:
998 return PIPE_FORMAT_DXT1_RGB;
999 case PIPE_FORMAT_DXT1_SRGBA:
1000 return PIPE_FORMAT_DXT1_RGBA;
1001 case PIPE_FORMAT_DXT3_SRGBA:
1002 return PIPE_FORMAT_DXT3_RGBA;
1003 case PIPE_FORMAT_DXT5_SRGBA:
1004 return PIPE_FORMAT_DXT5_RGBA;
1005 case PIPE_FORMAT_B5G6R5_SRGB:
1006 return PIPE_FORMAT_B5G6R5_UNORM;
1007 case PIPE_FORMAT_BPTC_SRGBA:
1008 return PIPE_FORMAT_BPTC_RGBA_UNORM;
1009 case PIPE_FORMAT_ASTC_4x4_SRGB:
1010 return PIPE_FORMAT_ASTC_4x4;
1011 case PIPE_FORMAT_ASTC_5x4_SRGB:
1012 return PIPE_FORMAT_ASTC_5x4;
1013 case PIPE_FORMAT_ASTC_5x5_SRGB:
1014 return PIPE_FORMAT_ASTC_5x5;
1015 case PIPE_FORMAT_ASTC_6x5_SRGB:
1016 return PIPE_FORMAT_ASTC_6x5;
1017 case PIPE_FORMAT_ASTC_6x6_SRGB:
1018 return PIPE_FORMAT_ASTC_6x6;
1019 case PIPE_FORMAT_ASTC_8x5_SRGB:
1020 return PIPE_FORMAT_ASTC_8x5;
1021 case PIPE_FORMAT_ASTC_8x6_SRGB:
1022 return PIPE_FORMAT_ASTC_8x6;
1023 case PIPE_FORMAT_ASTC_8x8_SRGB:
1024 return PIPE_FORMAT_ASTC_8x8;
1025 case PIPE_FORMAT_ASTC_10x5_SRGB:
1026 return PIPE_FORMAT_ASTC_10x5;
1027 case PIPE_FORMAT_ASTC_10x6_SRGB:
1028 return PIPE_FORMAT_ASTC_10x6;
1029 case PIPE_FORMAT_ASTC_10x8_SRGB:
1030 return PIPE_FORMAT_ASTC_10x8;
1031 case PIPE_FORMAT_ASTC_10x10_SRGB:
1032 return PIPE_FORMAT_ASTC_10x10;
1033 case PIPE_FORMAT_ASTC_12x10_SRGB:
1034 return PIPE_FORMAT_ASTC_12x10;
1035 case PIPE_FORMAT_ASTC_12x12_SRGB:
1036 return PIPE_FORMAT_ASTC_12x12;
1037 default:
1038 return format;
1039 }
1040 }
1041
1042 /**
1043 * Given a depth-stencil format, return the corresponding stencil-only format.
1044 * For stencil-only formats, return the format unchanged.
1045 */
1046 static inline enum pipe_format
1047 util_format_stencil_only(enum pipe_format format)
1048 {
1049 switch (format) {
1050 /* mask out the depth component */
1051 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1052 return PIPE_FORMAT_X24S8_UINT;
1053 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1054 return PIPE_FORMAT_S8X24_UINT;
1055 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1056 return PIPE_FORMAT_X32_S8X24_UINT;
1057
1058 /* stencil only formats */
1059 case PIPE_FORMAT_X24S8_UINT:
1060 case PIPE_FORMAT_S8X24_UINT:
1061 case PIPE_FORMAT_X32_S8X24_UINT:
1062 case PIPE_FORMAT_S8_UINT:
1063 return format;
1064
1065 default:
1066 assert(0);
1067 return PIPE_FORMAT_NONE;
1068 }
1069 }
1070
1071 /**
1072 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1073 * This is identity for non-intensity formats.
1074 */
1075 static inline enum pipe_format
1076 util_format_intensity_to_red(enum pipe_format format)
1077 {
1078 switch (format) {
1079 case PIPE_FORMAT_I8_UNORM:
1080 return PIPE_FORMAT_R8_UNORM;
1081 case PIPE_FORMAT_I8_SNORM:
1082 return PIPE_FORMAT_R8_SNORM;
1083 case PIPE_FORMAT_I16_UNORM:
1084 return PIPE_FORMAT_R16_UNORM;
1085 case PIPE_FORMAT_I16_SNORM:
1086 return PIPE_FORMAT_R16_SNORM;
1087 case PIPE_FORMAT_I16_FLOAT:
1088 return PIPE_FORMAT_R16_FLOAT;
1089 case PIPE_FORMAT_I32_FLOAT:
1090 return PIPE_FORMAT_R32_FLOAT;
1091 case PIPE_FORMAT_I8_UINT:
1092 return PIPE_FORMAT_R8_UINT;
1093 case PIPE_FORMAT_I8_SINT:
1094 return PIPE_FORMAT_R8_SINT;
1095 case PIPE_FORMAT_I16_UINT:
1096 return PIPE_FORMAT_R16_UINT;
1097 case PIPE_FORMAT_I16_SINT:
1098 return PIPE_FORMAT_R16_SINT;
1099 case PIPE_FORMAT_I32_UINT:
1100 return PIPE_FORMAT_R32_UINT;
1101 case PIPE_FORMAT_I32_SINT:
1102 return PIPE_FORMAT_R32_SINT;
1103 default:
1104 assert(!util_format_is_intensity(format));
1105 return format;
1106 }
1107 }
1108
1109 /**
1110 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1111 * This is identity for non-luminance formats.
1112 */
1113 static inline enum pipe_format
1114 util_format_luminance_to_red(enum pipe_format format)
1115 {
1116 switch (format) {
1117 case PIPE_FORMAT_L8_UNORM:
1118 return PIPE_FORMAT_R8_UNORM;
1119 case PIPE_FORMAT_L8_SNORM:
1120 return PIPE_FORMAT_R8_SNORM;
1121 case PIPE_FORMAT_L16_UNORM:
1122 return PIPE_FORMAT_R16_UNORM;
1123 case PIPE_FORMAT_L16_SNORM:
1124 return PIPE_FORMAT_R16_SNORM;
1125 case PIPE_FORMAT_L16_FLOAT:
1126 return PIPE_FORMAT_R16_FLOAT;
1127 case PIPE_FORMAT_L32_FLOAT:
1128 return PIPE_FORMAT_R32_FLOAT;
1129 case PIPE_FORMAT_L8_UINT:
1130 return PIPE_FORMAT_R8_UINT;
1131 case PIPE_FORMAT_L8_SINT:
1132 return PIPE_FORMAT_R8_SINT;
1133 case PIPE_FORMAT_L16_UINT:
1134 return PIPE_FORMAT_R16_UINT;
1135 case PIPE_FORMAT_L16_SINT:
1136 return PIPE_FORMAT_R16_SINT;
1137 case PIPE_FORMAT_L32_UINT:
1138 return PIPE_FORMAT_R32_UINT;
1139 case PIPE_FORMAT_L32_SINT:
1140 return PIPE_FORMAT_R32_SINT;
1141
1142 case PIPE_FORMAT_LATC1_UNORM:
1143 return PIPE_FORMAT_RGTC1_UNORM;
1144 case PIPE_FORMAT_LATC1_SNORM:
1145 return PIPE_FORMAT_RGTC1_SNORM;
1146
1147 case PIPE_FORMAT_L4A4_UNORM:
1148 return PIPE_FORMAT_R4A4_UNORM;
1149
1150 case PIPE_FORMAT_L8A8_UNORM:
1151 return PIPE_FORMAT_R8A8_UNORM;
1152 case PIPE_FORMAT_L8A8_SNORM:
1153 return PIPE_FORMAT_R8A8_SNORM;
1154 case PIPE_FORMAT_L16A16_UNORM:
1155 return PIPE_FORMAT_R16A16_UNORM;
1156 case PIPE_FORMAT_L16A16_SNORM:
1157 return PIPE_FORMAT_R16A16_SNORM;
1158 case PIPE_FORMAT_L16A16_FLOAT:
1159 return PIPE_FORMAT_R16A16_FLOAT;
1160 case PIPE_FORMAT_L32A32_FLOAT:
1161 return PIPE_FORMAT_R32A32_FLOAT;
1162 case PIPE_FORMAT_L8A8_UINT:
1163 return PIPE_FORMAT_R8A8_UINT;
1164 case PIPE_FORMAT_L8A8_SINT:
1165 return PIPE_FORMAT_R8A8_SINT;
1166 case PIPE_FORMAT_L16A16_UINT:
1167 return PIPE_FORMAT_R16A16_UINT;
1168 case PIPE_FORMAT_L16A16_SINT:
1169 return PIPE_FORMAT_R16A16_SINT;
1170 case PIPE_FORMAT_L32A32_UINT:
1171 return PIPE_FORMAT_R32A32_UINT;
1172 case PIPE_FORMAT_L32A32_SINT:
1173 return PIPE_FORMAT_R32A32_SINT;
1174
1175 /* We don't have compressed red-alpha variants for these. */
1176 case PIPE_FORMAT_LATC2_UNORM:
1177 case PIPE_FORMAT_LATC2_SNORM:
1178 return PIPE_FORMAT_NONE;
1179
1180 default:
1181 assert(!util_format_is_luminance(format) &&
1182 !util_format_is_luminance_alpha(format));
1183 return format;
1184 }
1185 }
1186
1187 /**
1188 * Return the number of components stored.
1189 * Formats with block size != 1x1 will always have 1 component (the block).
1190 */
1191 static inline unsigned
1192 util_format_get_nr_components(enum pipe_format format)
1193 {
1194 const struct util_format_description *desc = util_format_description(format);
1195 return desc->nr_channels;
1196 }
1197
1198 /**
1199 * Return the index of the first non-void channel
1200 * -1 if no non-void channels
1201 */
1202 static inline int
1203 util_format_get_first_non_void_channel(enum pipe_format format)
1204 {
1205 const struct util_format_description *desc = util_format_description(format);
1206 int i;
1207
1208 for (i = 0; i < 4; i++)
1209 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1210 break;
1211
1212 if (i == 4)
1213 return -1;
1214
1215 return i;
1216 }
1217
1218 /*
1219 * Format access functions.
1220 */
1221
1222 void
1223 util_format_read_4f(enum pipe_format format,
1224 float *dst, unsigned dst_stride,
1225 const void *src, unsigned src_stride,
1226 unsigned x, unsigned y, unsigned w, unsigned h);
1227
1228 void
1229 util_format_write_4f(enum pipe_format format,
1230 const float *src, unsigned src_stride,
1231 void *dst, unsigned dst_stride,
1232 unsigned x, unsigned y, unsigned w, unsigned h);
1233
1234 void
1235 util_format_read_4ub(enum pipe_format format,
1236 uint8_t *dst, unsigned dst_stride,
1237 const void *src, unsigned src_stride,
1238 unsigned x, unsigned y, unsigned w, unsigned h);
1239
1240 void
1241 util_format_write_4ub(enum pipe_format format,
1242 const uint8_t *src, unsigned src_stride,
1243 void *dst, unsigned dst_stride,
1244 unsigned x, unsigned y, unsigned w, unsigned h);
1245
1246 void
1247 util_format_read_4ui(enum pipe_format format,
1248 unsigned *dst, unsigned dst_stride,
1249 const void *src, unsigned src_stride,
1250 unsigned x, unsigned y, unsigned w, unsigned h);
1251
1252 void
1253 util_format_write_4ui(enum pipe_format format,
1254 const unsigned int *src, unsigned src_stride,
1255 void *dst, unsigned dst_stride,
1256 unsigned x, unsigned y, unsigned w, unsigned h);
1257
1258 void
1259 util_format_read_4i(enum pipe_format format,
1260 int *dst, unsigned dst_stride,
1261 const void *src, unsigned src_stride,
1262 unsigned x, unsigned y, unsigned w, unsigned h);
1263
1264 void
1265 util_format_write_4i(enum pipe_format format,
1266 const int *src, unsigned src_stride,
1267 void *dst, unsigned dst_stride,
1268 unsigned x, unsigned y, unsigned w, unsigned h);
1269
1270 /*
1271 * Generic format conversion;
1272 */
1273
1274 boolean
1275 util_format_fits_8unorm(const struct util_format_description *format_desc);
1276
1277 boolean
1278 util_format_translate(enum pipe_format dst_format,
1279 void *dst, unsigned dst_stride,
1280 unsigned dst_x, unsigned dst_y,
1281 enum pipe_format src_format,
1282 const void *src, unsigned src_stride,
1283 unsigned src_x, unsigned src_y,
1284 unsigned width, unsigned height);
1285
1286 /*
1287 * Swizzle operations.
1288 */
1289
1290 /* Compose two sets of swizzles.
1291 * If V is a 4D vector and the function parameters represent functions that
1292 * swizzle vector components, this holds:
1293 * swz2(swz1(V)) = dst(V)
1294 */
1295 void util_format_compose_swizzles(const unsigned char swz1[4],
1296 const unsigned char swz2[4],
1297 unsigned char dst[4]);
1298
1299 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1300 * to \param src and store the result in \param dst.
1301 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1302 */
1303 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1304 const union pipe_color_union *src,
1305 const unsigned char swz[4],
1306 const boolean is_integer);
1307
1308 void util_format_swizzle_4f(float *dst, const float *src,
1309 const unsigned char swz[4]);
1310
1311 void util_format_unswizzle_4f(float *dst, const float *src,
1312 const unsigned char swz[4]);
1313
1314 #ifdef __cplusplus
1315 } // extern "C" {
1316 #endif
1317
1318 #endif /* ! U_FORMAT_H */