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