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