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