1b6f0b882893a316bbcdf01fbc1196a41a1decf4
[mesa.git] / src / util / format / 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 struct pipe_screen;
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45
46 /**
47 * Describe how to pack/unpack pixels into/from the prescribed format.
48 *
49 * XXX: This could be renamed to something like util_format_pack, or broke down
50 * in flags inside util_format_block that said exactly what we want.
51 */
52 enum util_format_layout {
53 /**
54 * Formats with util_format_block::width == util_format_block::height == 1
55 * that can be described as an ordinary data structure.
56 */
57 UTIL_FORMAT_LAYOUT_PLAIN,
58
59 /**
60 * Formats with sub-sampled channels.
61 *
62 * This is for formats like YVYU where there is less than one sample per
63 * pixel.
64 */
65 UTIL_FORMAT_LAYOUT_SUBSAMPLED,
66
67 /**
68 * S3 Texture Compression formats.
69 */
70 UTIL_FORMAT_LAYOUT_S3TC,
71
72 /**
73 * Red-Green Texture Compression formats.
74 */
75 UTIL_FORMAT_LAYOUT_RGTC,
76
77 /**
78 * Ericsson Texture Compression
79 */
80 UTIL_FORMAT_LAYOUT_ETC,
81
82 /**
83 * BC6/7 Texture Compression
84 */
85 UTIL_FORMAT_LAYOUT_BPTC,
86
87 UTIL_FORMAT_LAYOUT_ASTC,
88
89 UTIL_FORMAT_LAYOUT_ATC,
90
91 /** Formats with 2 or more planes. */
92 UTIL_FORMAT_LAYOUT_PLANAR2,
93 UTIL_FORMAT_LAYOUT_PLANAR3,
94
95 UTIL_FORMAT_LAYOUT_FXT1 = 10,
96
97 /**
98 * Everything else that doesn't fit in any of the above layouts.
99 */
100 UTIL_FORMAT_LAYOUT_OTHER,
101 };
102
103
104 struct util_format_block
105 {
106 /** Block width in pixels */
107 unsigned width;
108
109 /** Block height in pixels */
110 unsigned height;
111
112 /** Block depth in pixels */
113 unsigned depth;
114
115 /** Block size in bits */
116 unsigned bits;
117 };
118
119
120 enum util_format_type {
121 UTIL_FORMAT_TYPE_VOID = 0,
122 UTIL_FORMAT_TYPE_UNSIGNED = 1,
123 UTIL_FORMAT_TYPE_SIGNED = 2,
124 UTIL_FORMAT_TYPE_FIXED = 3,
125 UTIL_FORMAT_TYPE_FLOAT = 4
126 };
127
128
129 enum util_format_colorspace {
130 UTIL_FORMAT_COLORSPACE_RGB = 0,
131 UTIL_FORMAT_COLORSPACE_SRGB = 1,
132 UTIL_FORMAT_COLORSPACE_YUV = 2,
133 UTIL_FORMAT_COLORSPACE_ZS = 3
134 };
135
136
137 struct util_format_channel_description
138 {
139 unsigned type:5; /**< UTIL_FORMAT_TYPE_x */
140 unsigned normalized:1;
141 unsigned pure_integer:1;
142 unsigned size:9; /**< bits per channel */
143 unsigned shift:16; /** number of bits from lsb */
144 };
145
146
147 struct util_format_description
148 {
149 enum pipe_format format;
150
151 const char *name;
152
153 /**
154 * Short name, striped of the prefix, lower case.
155 */
156 const char *short_name;
157
158 /**
159 * Pixel block dimensions.
160 */
161 struct util_format_block block;
162
163 enum util_format_layout layout;
164
165 /**
166 * The number of channels.
167 */
168 unsigned nr_channels:3;
169
170 /**
171 * Whether all channels have the same number of (whole) bytes and type.
172 */
173 unsigned is_array:1;
174
175 /**
176 * Whether the pixel format can be described as a bitfield structure.
177 *
178 * In particular:
179 * - pixel depth must be 8, 16, or 32 bits;
180 * - all channels must be unsigned, signed, or void
181 */
182 unsigned is_bitmask:1;
183
184 /**
185 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
186 */
187 unsigned is_mixed:1;
188
189 /**
190 * Whether the format contains UNORM channels
191 */
192 unsigned is_unorm:1;
193
194 /**
195 * Whether the format contains SNORM channels
196 */
197 unsigned is_snorm:1;
198
199 /**
200 * Input channel description, in the order XYZW.
201 *
202 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
203 *
204 * If each channel is accessed as an individual N-byte value, X is always
205 * at the lowest address in memory, Y is always next, and so on. For all
206 * currently-defined formats, the N-byte value has native endianness.
207 *
208 * If instead a group of channels is accessed as a single N-byte value,
209 * the order of the channels within that value depends on endianness.
210 * For big-endian targets, X is the most significant subvalue,
211 * otherwise it is the least significant one.
212 *
213 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
214 *
215 * 0 1 2 3
216 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
217 * big-endian: X Yu Ym Yl
218 *
219 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
220 *
221 * 0 1
222 * msb lsb msb lsb
223 * little-endian: YYYXXXXX WZZZZZYY
224 * big-endian: XXXXXYYY YYZZZZZW
225 */
226 struct util_format_channel_description channel[4];
227
228 /**
229 * Output channel swizzle.
230 *
231 * The order is either:
232 * - RGBA
233 * - YUV(A)
234 * - ZS
235 * depending on the colorspace.
236 */
237 unsigned char swizzle[4];
238
239 /**
240 * Colorspace transformation.
241 */
242 enum util_format_colorspace colorspace;
243
244 /**
245 * Unpack pixel blocks to R8G8B8A8_UNORM.
246 * Note: strides are in bytes.
247 *
248 * Only defined for non-depth-stencil formats.
249 */
250 void
251 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
252 const uint8_t *src, unsigned src_stride,
253 unsigned width, unsigned height);
254
255 /**
256 * Pack pixel blocks from R8G8B8A8_UNORM.
257 * Note: strides are in bytes.
258 *
259 * Only defined for non-depth-stencil formats.
260 */
261 void
262 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
263 const uint8_t *src, unsigned src_stride,
264 unsigned width, unsigned height);
265
266 /**
267 * Fetch a single pixel (i, j) from a block.
268 *
269 * XXX: Only defined for a very few select formats.
270 */
271 void
272 (*fetch_rgba_8unorm)(uint8_t *dst,
273 const uint8_t *src,
274 unsigned i, unsigned j);
275
276 /**
277 * Unpack pixel blocks to R32G32B32A32_FLOAT.
278 * Note: strides are in bytes.
279 *
280 * Only defined for non-depth-stencil formats.
281 */
282 void
283 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
284 const uint8_t *src, unsigned src_stride,
285 unsigned width, unsigned height);
286
287 /**
288 * Pack pixel blocks from R32G32B32A32_FLOAT.
289 * Note: strides are in bytes.
290 *
291 * Only defined for non-depth-stencil formats.
292 */
293 void
294 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
295 const float *src, unsigned src_stride,
296 unsigned width, unsigned height);
297
298 /**
299 * Fetch a single pixel (i, j) from a block.
300 *
301 * Only defined for non-depth-stencil and non-integer formats.
302 */
303 void
304 (*fetch_rgba_float)(float *dst,
305 const uint8_t *src,
306 unsigned i, unsigned j);
307
308 /**
309 * Unpack pixels to Z32_UNORM.
310 * Note: strides are in bytes.
311 *
312 * Only defined for depth formats.
313 */
314 void
315 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
316 const uint8_t *src, unsigned src_stride,
317 unsigned width, unsigned height);
318
319 /**
320 * Pack pixels from Z32_FLOAT.
321 * Note: strides are in bytes.
322 *
323 * Only defined for depth formats.
324 */
325 void
326 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
327 const uint32_t *src, unsigned src_stride,
328 unsigned width, unsigned height);
329
330 /**
331 * Unpack pixels to Z32_FLOAT.
332 * Note: strides are in bytes.
333 *
334 * Only defined for depth formats.
335 */
336 void
337 (*unpack_z_float)(float *dst, unsigned dst_stride,
338 const uint8_t *src, unsigned src_stride,
339 unsigned width, unsigned height);
340
341 /**
342 * Pack pixels from Z32_FLOAT.
343 * Note: strides are in bytes.
344 *
345 * Only defined for depth formats.
346 */
347 void
348 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
349 const float *src, unsigned src_stride,
350 unsigned width, unsigned height);
351
352 /**
353 * Unpack pixels to S8_UINT.
354 * Note: strides are in bytes.
355 *
356 * Only defined for stencil formats.
357 */
358 void
359 (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
360 const uint8_t *src, unsigned src_stride,
361 unsigned width, unsigned height);
362
363 /**
364 * Pack pixels from S8_UINT.
365 * Note: strides are in bytes.
366 *
367 * Only defined for stencil formats.
368 */
369 void
370 (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
371 const uint8_t *src, unsigned src_stride,
372 unsigned width, unsigned height);
373
374 /**
375 * Unpack pixel blocks to R32G32B32A32_UINT.
376 * Note: strides are in bytes.
377 *
378 * Only defined for INT formats.
379 */
380 void
381 (*unpack_rgba_uint)(uint32_t *dst, unsigned dst_stride,
382 const uint8_t *src, unsigned src_stride,
383 unsigned width, unsigned height);
384
385 void
386 (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
387 const uint32_t *src, unsigned src_stride,
388 unsigned width, unsigned height);
389
390 /**
391 * Unpack pixel blocks to R32G32B32A32_SINT.
392 * Note: strides are in bytes.
393 *
394 * Only defined for INT formats.
395 */
396 void
397 (*unpack_rgba_sint)(int32_t *dst, unsigned dst_stride,
398 const uint8_t *src, unsigned src_stride,
399 unsigned width, unsigned height);
400
401 void
402 (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
403 const int32_t *src, unsigned src_stride,
404 unsigned width, unsigned height);
405
406 /**
407 * Fetch a single pixel (i, j) from a block.
408 *
409 * Only defined for unsigned (pure) integer formats.
410 */
411 void
412 (*fetch_rgba_uint)(uint32_t *dst,
413 const uint8_t *src,
414 unsigned i, unsigned j);
415
416 /**
417 * Fetch a single pixel (i, j) from a block.
418 *
419 * Only defined for signed (pure) integer formats.
420 */
421 void
422 (*fetch_rgba_sint)(int32_t *dst,
423 const uint8_t *src,
424 unsigned i, unsigned j);
425 };
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 case UTIL_FORMAT_LAYOUT_FXT1:
495 /* XXX add other formats in the future */
496 return TRUE;
497 default:
498 return FALSE;
499 }
500 }
501
502 static inline boolean
503 util_format_is_s3tc(enum pipe_format format)
504 {
505 const struct util_format_description *desc = util_format_description(format);
506
507 assert(desc);
508 if (!desc) {
509 return FALSE;
510 }
511
512 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
513 }
514
515 static inline boolean
516 util_format_is_etc(enum pipe_format format)
517 {
518 const struct util_format_description *desc = util_format_description(format);
519
520 assert(desc);
521 if (!desc) {
522 return FALSE;
523 }
524
525 return desc->layout == UTIL_FORMAT_LAYOUT_ETC ? TRUE : FALSE;
526 }
527
528 static inline boolean
529 util_format_is_srgb(enum pipe_format format)
530 {
531 const struct util_format_description *desc = util_format_description(format);
532 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
533 }
534
535 static inline boolean
536 util_format_has_depth(const struct util_format_description *desc)
537 {
538 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
539 desc->swizzle[0] != PIPE_SWIZZLE_NONE;
540 }
541
542 static inline boolean
543 util_format_has_stencil(const struct util_format_description *desc)
544 {
545 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
546 desc->swizzle[1] != PIPE_SWIZZLE_NONE;
547 }
548
549 static inline boolean
550 util_format_is_depth_or_stencil(enum pipe_format format)
551 {
552 const struct util_format_description *desc = util_format_description(format);
553
554 assert(desc);
555 if (!desc) {
556 return FALSE;
557 }
558
559 return util_format_has_depth(desc) ||
560 util_format_has_stencil(desc);
561 }
562
563 static inline boolean
564 util_format_is_depth_and_stencil(enum pipe_format format)
565 {
566 const struct util_format_description *desc = util_format_description(format);
567
568 assert(desc);
569 if (!desc) {
570 return FALSE;
571 }
572
573 return util_format_has_depth(desc) &&
574 util_format_has_stencil(desc);
575 }
576
577 /**
578 * For depth-stencil formats, return the equivalent depth-only format.
579 */
580 static inline enum pipe_format
581 util_format_get_depth_only(enum pipe_format format)
582 {
583 switch (format) {
584 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
585 return PIPE_FORMAT_Z24X8_UNORM;
586
587 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
588 return PIPE_FORMAT_X8Z24_UNORM;
589
590 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
591 return PIPE_FORMAT_Z32_FLOAT;
592
593 default:
594 return format;
595 }
596 }
597
598 static inline boolean
599 util_format_is_yuv(enum pipe_format format)
600 {
601 const struct util_format_description *desc = util_format_description(format);
602
603 assert(desc);
604 if (!desc) {
605 return FALSE;
606 }
607
608 return desc->colorspace == UTIL_FORMAT_COLORSPACE_YUV;
609 }
610
611 /**
612 * Calculates the depth format type based upon the incoming format description.
613 */
614 static inline unsigned
615 util_get_depth_format_type(const struct util_format_description *desc)
616 {
617 unsigned depth_channel = desc->swizzle[0];
618 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
619 depth_channel != PIPE_SWIZZLE_NONE) {
620 return desc->channel[depth_channel].type;
621 } else {
622 return UTIL_FORMAT_TYPE_VOID;
623 }
624 }
625
626
627 /**
628 * Calculates the MRD for the depth format. MRD is used in depth bias
629 * for UNORM and unbound depth buffers. When the depth buffer is floating
630 * point, the depth bias calculation does not use the MRD. However, the
631 * default MRD will be 1.0 / ((1 << 24) - 1).
632 */
633 double
634 util_get_depth_format_mrd(const struct util_format_description *desc);
635
636
637 /**
638 * Return whether this is an RGBA, Z, S, or combined ZS format.
639 * Useful for initializing pipe_blit_info::mask.
640 */
641 static inline unsigned
642 util_format_get_mask(enum pipe_format format)
643 {
644 const struct util_format_description *desc =
645 util_format_description(format);
646
647 if (!desc)
648 return 0;
649
650 if (util_format_has_depth(desc)) {
651 if (util_format_has_stencil(desc)) {
652 return PIPE_MASK_ZS;
653 } else {
654 return PIPE_MASK_Z;
655 }
656 } else {
657 if (util_format_has_stencil(desc)) {
658 return PIPE_MASK_S;
659 } else {
660 return PIPE_MASK_RGBA;
661 }
662 }
663 }
664
665 /**
666 * Give the RGBA colormask of the channels that can be represented in this
667 * format.
668 *
669 * That is, the channels whose values are preserved.
670 */
671 static inline unsigned
672 util_format_colormask(const struct util_format_description *desc)
673 {
674 unsigned colormask;
675 unsigned chan;
676
677 switch (desc->colorspace) {
678 case UTIL_FORMAT_COLORSPACE_RGB:
679 case UTIL_FORMAT_COLORSPACE_SRGB:
680 case UTIL_FORMAT_COLORSPACE_YUV:
681 colormask = 0;
682 for (chan = 0; chan < 4; ++chan) {
683 if (desc->swizzle[chan] < 4) {
684 colormask |= (1 << chan);
685 }
686 }
687 return colormask;
688 case UTIL_FORMAT_COLORSPACE_ZS:
689 return 0;
690 default:
691 assert(0);
692 return 0;
693 }
694 }
695
696
697 /**
698 * Checks if color mask covers every channel for the specified format
699 *
700 * @param desc a format description to check colormask with
701 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
702 */
703 static inline boolean
704 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
705 {
706 return (~colormask & util_format_colormask(desc)) == 0;
707 }
708
709
710 boolean
711 util_format_is_float(enum pipe_format format);
712
713
714 boolean
715 util_format_has_alpha(enum pipe_format format);
716
717
718 boolean
719 util_format_is_luminance(enum pipe_format format);
720
721 boolean
722 util_format_is_alpha(enum pipe_format format);
723
724 boolean
725 util_format_is_luminance_alpha(enum pipe_format format);
726
727
728 boolean
729 util_format_is_intensity(enum pipe_format format);
730
731 boolean
732 util_format_is_subsampled_422(enum pipe_format format);
733
734 boolean
735 util_format_is_pure_integer(enum pipe_format format);
736
737 boolean
738 util_format_is_pure_sint(enum pipe_format format);
739
740 boolean
741 util_format_is_pure_uint(enum pipe_format format);
742
743 boolean
744 util_format_is_snorm(enum pipe_format format);
745
746 boolean
747 util_format_is_unorm(enum pipe_format format);
748
749 boolean
750 util_format_is_snorm8(enum pipe_format format);
751
752 /**
753 * Check if the src format can be blitted to the destination format with
754 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
755 * the reverse.
756 */
757 boolean
758 util_is_format_compatible(const struct util_format_description *src_desc,
759 const struct util_format_description *dst_desc);
760
761 /**
762 * Whether this format is a rgab8 variant.
763 *
764 * That is, any format that matches the
765 *
766 * PIPE_FORMAT_?8?8?8?8_UNORM
767 */
768 static inline boolean
769 util_format_is_rgba8_variant(const struct util_format_description *desc)
770 {
771 unsigned chan;
772
773 if(desc->block.width != 1 ||
774 desc->block.height != 1 ||
775 desc->block.bits != 32)
776 return FALSE;
777
778 for(chan = 0; chan < 4; ++chan) {
779 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
780 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
781 return FALSE;
782 if(desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED &&
783 !desc->channel[chan].normalized)
784 return FALSE;
785 if(desc->channel[chan].size != 8)
786 return FALSE;
787 }
788
789 return TRUE;
790 }
791
792 /**
793 * Return total bits needed for the pixel format per block.
794 */
795 static inline uint
796 util_format_get_blocksizebits(enum pipe_format format)
797 {
798 const struct util_format_description *desc = util_format_description(format);
799
800 assert(desc);
801 if (!desc) {
802 return 0;
803 }
804
805 return desc->block.bits;
806 }
807
808 /**
809 * Return bytes per block (not pixel) for the given format.
810 */
811 static inline uint
812 util_format_get_blocksize(enum pipe_format format)
813 {
814 uint bits = util_format_get_blocksizebits(format);
815 uint bytes = bits / 8;
816
817 assert(bits % 8 == 0);
818 assert(bytes > 0);
819 if (bytes == 0) {
820 bytes = 1;
821 }
822
823 return bytes;
824 }
825
826 static inline uint
827 util_format_get_blockwidth(enum pipe_format format)
828 {
829 const struct util_format_description *desc = util_format_description(format);
830
831 assert(desc);
832 if (!desc) {
833 return 1;
834 }
835
836 return desc->block.width;
837 }
838
839 static inline uint
840 util_format_get_blockheight(enum pipe_format format)
841 {
842 const struct util_format_description *desc = util_format_description(format);
843
844 assert(desc);
845 if (!desc) {
846 return 1;
847 }
848
849 return desc->block.height;
850 }
851
852 static inline uint
853 util_format_get_blockdepth(enum pipe_format format)
854 {
855 const struct util_format_description *desc = util_format_description(format);
856
857 assert(desc);
858 if (!desc) {
859 return 1;
860 }
861
862 return desc->block.depth;
863 }
864
865 static inline unsigned
866 util_format_get_nblocksx(enum pipe_format format,
867 unsigned x)
868 {
869 unsigned blockwidth = util_format_get_blockwidth(format);
870 return (x + blockwidth - 1) / blockwidth;
871 }
872
873 static inline unsigned
874 util_format_get_nblocksy(enum pipe_format format,
875 unsigned y)
876 {
877 unsigned blockheight = util_format_get_blockheight(format);
878 return (y + blockheight - 1) / blockheight;
879 }
880
881 static inline unsigned
882 util_format_get_nblocksz(enum pipe_format format,
883 unsigned z)
884 {
885 unsigned blockdepth = util_format_get_blockdepth(format);
886 return (z + blockdepth - 1) / blockdepth;
887 }
888
889 static inline unsigned
890 util_format_get_nblocks(enum pipe_format format,
891 unsigned width,
892 unsigned height)
893 {
894 assert(util_format_get_blockdepth(format) == 1);
895 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
896 }
897
898 static inline size_t
899 util_format_get_stride(enum pipe_format format,
900 unsigned width)
901 {
902 return (size_t)util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
903 }
904
905 static inline size_t
906 util_format_get_2d_size(enum pipe_format format,
907 size_t stride,
908 unsigned height)
909 {
910 return util_format_get_nblocksy(format, height) * stride;
911 }
912
913 static inline uint
914 util_format_get_component_bits(enum pipe_format format,
915 enum util_format_colorspace colorspace,
916 uint component)
917 {
918 const struct util_format_description *desc = util_format_description(format);
919 enum util_format_colorspace desc_colorspace;
920
921 assert(format);
922 if (!format) {
923 return 0;
924 }
925
926 assert(component < 4);
927
928 /* Treat RGB and SRGB as equivalent. */
929 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
930 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
931 }
932 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
933 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
934 } else {
935 desc_colorspace = desc->colorspace;
936 }
937
938 if (desc_colorspace != colorspace) {
939 return 0;
940 }
941
942 switch (desc->swizzle[component]) {
943 case PIPE_SWIZZLE_X:
944 return desc->channel[0].size;
945 case PIPE_SWIZZLE_Y:
946 return desc->channel[1].size;
947 case PIPE_SWIZZLE_Z:
948 return desc->channel[2].size;
949 case PIPE_SWIZZLE_W:
950 return desc->channel[3].size;
951 default:
952 return 0;
953 }
954 }
955
956 /**
957 * Given a linear RGB colorspace format, return the corresponding SRGB
958 * format, or PIPE_FORMAT_NONE if none.
959 */
960 static inline enum pipe_format
961 util_format_srgb(enum pipe_format format)
962 {
963 if (util_format_is_srgb(format))
964 return format;
965
966 switch (format) {
967 case PIPE_FORMAT_L8_UNORM:
968 return PIPE_FORMAT_L8_SRGB;
969 case PIPE_FORMAT_R8_UNORM:
970 return PIPE_FORMAT_R8_SRGB;
971 case PIPE_FORMAT_L8A8_UNORM:
972 return PIPE_FORMAT_L8A8_SRGB;
973 case PIPE_FORMAT_R8G8_UNORM:
974 return PIPE_FORMAT_R8G8_SRGB;
975 case PIPE_FORMAT_R8G8B8_UNORM:
976 return PIPE_FORMAT_R8G8B8_SRGB;
977 case PIPE_FORMAT_B8G8R8_UNORM:
978 return PIPE_FORMAT_B8G8R8_SRGB;
979 case PIPE_FORMAT_A8B8G8R8_UNORM:
980 return PIPE_FORMAT_A8B8G8R8_SRGB;
981 case PIPE_FORMAT_X8B8G8R8_UNORM:
982 return PIPE_FORMAT_X8B8G8R8_SRGB;
983 case PIPE_FORMAT_B8G8R8A8_UNORM:
984 return PIPE_FORMAT_B8G8R8A8_SRGB;
985 case PIPE_FORMAT_B8G8R8X8_UNORM:
986 return PIPE_FORMAT_B8G8R8X8_SRGB;
987 case PIPE_FORMAT_A8R8G8B8_UNORM:
988 return PIPE_FORMAT_A8R8G8B8_SRGB;
989 case PIPE_FORMAT_X8R8G8B8_UNORM:
990 return PIPE_FORMAT_X8R8G8B8_SRGB;
991 case PIPE_FORMAT_R8G8B8A8_UNORM:
992 return PIPE_FORMAT_R8G8B8A8_SRGB;
993 case PIPE_FORMAT_R8G8B8X8_UNORM:
994 return PIPE_FORMAT_R8G8B8X8_SRGB;
995 case PIPE_FORMAT_DXT1_RGB:
996 return PIPE_FORMAT_DXT1_SRGB;
997 case PIPE_FORMAT_DXT1_RGBA:
998 return PIPE_FORMAT_DXT1_SRGBA;
999 case PIPE_FORMAT_DXT3_RGBA:
1000 return PIPE_FORMAT_DXT3_SRGBA;
1001 case PIPE_FORMAT_DXT5_RGBA:
1002 return PIPE_FORMAT_DXT5_SRGBA;
1003 case PIPE_FORMAT_B5G6R5_UNORM:
1004 return PIPE_FORMAT_B5G6R5_SRGB;
1005 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1006 return PIPE_FORMAT_BPTC_SRGBA;
1007 case PIPE_FORMAT_ETC2_RGB8:
1008 return PIPE_FORMAT_ETC2_SRGB8;
1009 case PIPE_FORMAT_ETC2_RGB8A1:
1010 return PIPE_FORMAT_ETC2_SRGB8A1;
1011 case PIPE_FORMAT_ETC2_RGBA8:
1012 return PIPE_FORMAT_ETC2_SRGBA8;
1013 case PIPE_FORMAT_ASTC_4x4:
1014 return PIPE_FORMAT_ASTC_4x4_SRGB;
1015 case PIPE_FORMAT_ASTC_5x4:
1016 return PIPE_FORMAT_ASTC_5x4_SRGB;
1017 case PIPE_FORMAT_ASTC_5x5:
1018 return PIPE_FORMAT_ASTC_5x5_SRGB;
1019 case PIPE_FORMAT_ASTC_6x5:
1020 return PIPE_FORMAT_ASTC_6x5_SRGB;
1021 case PIPE_FORMAT_ASTC_6x6:
1022 return PIPE_FORMAT_ASTC_6x6_SRGB;
1023 case PIPE_FORMAT_ASTC_8x5:
1024 return PIPE_FORMAT_ASTC_8x5_SRGB;
1025 case PIPE_FORMAT_ASTC_8x6:
1026 return PIPE_FORMAT_ASTC_8x6_SRGB;
1027 case PIPE_FORMAT_ASTC_8x8:
1028 return PIPE_FORMAT_ASTC_8x8_SRGB;
1029 case PIPE_FORMAT_ASTC_10x5:
1030 return PIPE_FORMAT_ASTC_10x5_SRGB;
1031 case PIPE_FORMAT_ASTC_10x6:
1032 return PIPE_FORMAT_ASTC_10x6_SRGB;
1033 case PIPE_FORMAT_ASTC_10x8:
1034 return PIPE_FORMAT_ASTC_10x8_SRGB;
1035 case PIPE_FORMAT_ASTC_10x10:
1036 return PIPE_FORMAT_ASTC_10x10_SRGB;
1037 case PIPE_FORMAT_ASTC_12x10:
1038 return PIPE_FORMAT_ASTC_12x10_SRGB;
1039 case PIPE_FORMAT_ASTC_12x12:
1040 return PIPE_FORMAT_ASTC_12x12_SRGB;
1041 case PIPE_FORMAT_ASTC_3x3x3:
1042 return PIPE_FORMAT_ASTC_3x3x3_SRGB;
1043 case PIPE_FORMAT_ASTC_4x3x3:
1044 return PIPE_FORMAT_ASTC_4x3x3_SRGB;
1045 case PIPE_FORMAT_ASTC_4x4x3:
1046 return PIPE_FORMAT_ASTC_4x4x3_SRGB;
1047 case PIPE_FORMAT_ASTC_4x4x4:
1048 return PIPE_FORMAT_ASTC_4x4x4_SRGB;
1049 case PIPE_FORMAT_ASTC_5x4x4:
1050 return PIPE_FORMAT_ASTC_5x4x4_SRGB;
1051 case PIPE_FORMAT_ASTC_5x5x4:
1052 return PIPE_FORMAT_ASTC_5x5x4_SRGB;
1053 case PIPE_FORMAT_ASTC_5x5x5:
1054 return PIPE_FORMAT_ASTC_5x5x5_SRGB;
1055 case PIPE_FORMAT_ASTC_6x5x5:
1056 return PIPE_FORMAT_ASTC_6x5x5_SRGB;
1057 case PIPE_FORMAT_ASTC_6x6x5:
1058 return PIPE_FORMAT_ASTC_6x6x5_SRGB;
1059 case PIPE_FORMAT_ASTC_6x6x6:
1060 return PIPE_FORMAT_ASTC_6x6x6_SRGB;
1061
1062 default:
1063 return PIPE_FORMAT_NONE;
1064 }
1065 }
1066
1067 /**
1068 * Given an sRGB format, return the corresponding linear colorspace format.
1069 * For non sRGB formats, return the format unchanged.
1070 */
1071 static inline enum pipe_format
1072 util_format_linear(enum pipe_format format)
1073 {
1074 switch (format) {
1075 case PIPE_FORMAT_L8_SRGB:
1076 return PIPE_FORMAT_L8_UNORM;
1077 case PIPE_FORMAT_R8_SRGB:
1078 return PIPE_FORMAT_R8_UNORM;
1079 case PIPE_FORMAT_L8A8_SRGB:
1080 return PIPE_FORMAT_L8A8_UNORM;
1081 case PIPE_FORMAT_R8G8_SRGB:
1082 return PIPE_FORMAT_R8G8_UNORM;
1083 case PIPE_FORMAT_R8G8B8_SRGB:
1084 return PIPE_FORMAT_R8G8B8_UNORM;
1085 case PIPE_FORMAT_B8G8R8_SRGB:
1086 return PIPE_FORMAT_B8G8R8_UNORM;
1087 case PIPE_FORMAT_A8B8G8R8_SRGB:
1088 return PIPE_FORMAT_A8B8G8R8_UNORM;
1089 case PIPE_FORMAT_X8B8G8R8_SRGB:
1090 return PIPE_FORMAT_X8B8G8R8_UNORM;
1091 case PIPE_FORMAT_B8G8R8A8_SRGB:
1092 return PIPE_FORMAT_B8G8R8A8_UNORM;
1093 case PIPE_FORMAT_B8G8R8X8_SRGB:
1094 return PIPE_FORMAT_B8G8R8X8_UNORM;
1095 case PIPE_FORMAT_A8R8G8B8_SRGB:
1096 return PIPE_FORMAT_A8R8G8B8_UNORM;
1097 case PIPE_FORMAT_X8R8G8B8_SRGB:
1098 return PIPE_FORMAT_X8R8G8B8_UNORM;
1099 case PIPE_FORMAT_R8G8B8A8_SRGB:
1100 return PIPE_FORMAT_R8G8B8A8_UNORM;
1101 case PIPE_FORMAT_R8G8B8X8_SRGB:
1102 return PIPE_FORMAT_R8G8B8X8_UNORM;
1103 case PIPE_FORMAT_DXT1_SRGB:
1104 return PIPE_FORMAT_DXT1_RGB;
1105 case PIPE_FORMAT_DXT1_SRGBA:
1106 return PIPE_FORMAT_DXT1_RGBA;
1107 case PIPE_FORMAT_DXT3_SRGBA:
1108 return PIPE_FORMAT_DXT3_RGBA;
1109 case PIPE_FORMAT_DXT5_SRGBA:
1110 return PIPE_FORMAT_DXT5_RGBA;
1111 case PIPE_FORMAT_B5G6R5_SRGB:
1112 return PIPE_FORMAT_B5G6R5_UNORM;
1113 case PIPE_FORMAT_BPTC_SRGBA:
1114 return PIPE_FORMAT_BPTC_RGBA_UNORM;
1115 case PIPE_FORMAT_ETC2_SRGB8:
1116 return PIPE_FORMAT_ETC2_RGB8;
1117 case PIPE_FORMAT_ETC2_SRGB8A1:
1118 return PIPE_FORMAT_ETC2_RGB8A1;
1119 case PIPE_FORMAT_ETC2_SRGBA8:
1120 return PIPE_FORMAT_ETC2_RGBA8;
1121 case PIPE_FORMAT_ASTC_4x4_SRGB:
1122 return PIPE_FORMAT_ASTC_4x4;
1123 case PIPE_FORMAT_ASTC_5x4_SRGB:
1124 return PIPE_FORMAT_ASTC_5x4;
1125 case PIPE_FORMAT_ASTC_5x5_SRGB:
1126 return PIPE_FORMAT_ASTC_5x5;
1127 case PIPE_FORMAT_ASTC_6x5_SRGB:
1128 return PIPE_FORMAT_ASTC_6x5;
1129 case PIPE_FORMAT_ASTC_6x6_SRGB:
1130 return PIPE_FORMAT_ASTC_6x6;
1131 case PIPE_FORMAT_ASTC_8x5_SRGB:
1132 return PIPE_FORMAT_ASTC_8x5;
1133 case PIPE_FORMAT_ASTC_8x6_SRGB:
1134 return PIPE_FORMAT_ASTC_8x6;
1135 case PIPE_FORMAT_ASTC_8x8_SRGB:
1136 return PIPE_FORMAT_ASTC_8x8;
1137 case PIPE_FORMAT_ASTC_10x5_SRGB:
1138 return PIPE_FORMAT_ASTC_10x5;
1139 case PIPE_FORMAT_ASTC_10x6_SRGB:
1140 return PIPE_FORMAT_ASTC_10x6;
1141 case PIPE_FORMAT_ASTC_10x8_SRGB:
1142 return PIPE_FORMAT_ASTC_10x8;
1143 case PIPE_FORMAT_ASTC_10x10_SRGB:
1144 return PIPE_FORMAT_ASTC_10x10;
1145 case PIPE_FORMAT_ASTC_12x10_SRGB:
1146 return PIPE_FORMAT_ASTC_12x10;
1147 case PIPE_FORMAT_ASTC_12x12_SRGB:
1148 return PIPE_FORMAT_ASTC_12x12;
1149 case PIPE_FORMAT_ASTC_3x3x3_SRGB:
1150 return PIPE_FORMAT_ASTC_3x3x3;
1151 case PIPE_FORMAT_ASTC_4x3x3_SRGB:
1152 return PIPE_FORMAT_ASTC_4x3x3;
1153 case PIPE_FORMAT_ASTC_4x4x3_SRGB:
1154 return PIPE_FORMAT_ASTC_4x4x3;
1155 case PIPE_FORMAT_ASTC_4x4x4_SRGB:
1156 return PIPE_FORMAT_ASTC_4x4x4;
1157 case PIPE_FORMAT_ASTC_5x4x4_SRGB:
1158 return PIPE_FORMAT_ASTC_5x4x4;
1159 case PIPE_FORMAT_ASTC_5x5x4_SRGB:
1160 return PIPE_FORMAT_ASTC_5x5x4;
1161 case PIPE_FORMAT_ASTC_5x5x5_SRGB:
1162 return PIPE_FORMAT_ASTC_5x5x5;
1163 case PIPE_FORMAT_ASTC_6x5x5_SRGB:
1164 return PIPE_FORMAT_ASTC_6x5x5;
1165 case PIPE_FORMAT_ASTC_6x6x5_SRGB:
1166 return PIPE_FORMAT_ASTC_6x6x5;
1167 case PIPE_FORMAT_ASTC_6x6x6_SRGB:
1168 return PIPE_FORMAT_ASTC_6x6x6;
1169 default:
1170 return format;
1171 }
1172 }
1173
1174 /**
1175 * Given a depth-stencil format, return the corresponding stencil-only format.
1176 * For stencil-only formats, return the format unchanged.
1177 */
1178 static inline enum pipe_format
1179 util_format_stencil_only(enum pipe_format format)
1180 {
1181 switch (format) {
1182 /* mask out the depth component */
1183 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1184 return PIPE_FORMAT_X24S8_UINT;
1185 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1186 return PIPE_FORMAT_S8X24_UINT;
1187 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1188 return PIPE_FORMAT_X32_S8X24_UINT;
1189
1190 /* stencil only formats */
1191 case PIPE_FORMAT_X24S8_UINT:
1192 case PIPE_FORMAT_S8X24_UINT:
1193 case PIPE_FORMAT_X32_S8X24_UINT:
1194 case PIPE_FORMAT_S8_UINT:
1195 return format;
1196
1197 default:
1198 assert(0);
1199 return PIPE_FORMAT_NONE;
1200 }
1201 }
1202
1203 /**
1204 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1205 * This is identity for non-intensity formats.
1206 */
1207 static inline enum pipe_format
1208 util_format_intensity_to_red(enum pipe_format format)
1209 {
1210 switch (format) {
1211 case PIPE_FORMAT_I8_UNORM:
1212 return PIPE_FORMAT_R8_UNORM;
1213 case PIPE_FORMAT_I8_SNORM:
1214 return PIPE_FORMAT_R8_SNORM;
1215 case PIPE_FORMAT_I16_UNORM:
1216 return PIPE_FORMAT_R16_UNORM;
1217 case PIPE_FORMAT_I16_SNORM:
1218 return PIPE_FORMAT_R16_SNORM;
1219 case PIPE_FORMAT_I16_FLOAT:
1220 return PIPE_FORMAT_R16_FLOAT;
1221 case PIPE_FORMAT_I32_FLOAT:
1222 return PIPE_FORMAT_R32_FLOAT;
1223 case PIPE_FORMAT_I8_UINT:
1224 return PIPE_FORMAT_R8_UINT;
1225 case PIPE_FORMAT_I8_SINT:
1226 return PIPE_FORMAT_R8_SINT;
1227 case PIPE_FORMAT_I16_UINT:
1228 return PIPE_FORMAT_R16_UINT;
1229 case PIPE_FORMAT_I16_SINT:
1230 return PIPE_FORMAT_R16_SINT;
1231 case PIPE_FORMAT_I32_UINT:
1232 return PIPE_FORMAT_R32_UINT;
1233 case PIPE_FORMAT_I32_SINT:
1234 return PIPE_FORMAT_R32_SINT;
1235 default:
1236 assert(!util_format_is_intensity(format));
1237 return format;
1238 }
1239 }
1240
1241 /**
1242 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1243 * This is identity for non-luminance formats.
1244 */
1245 static inline enum pipe_format
1246 util_format_luminance_to_red(enum pipe_format format)
1247 {
1248 switch (format) {
1249 case PIPE_FORMAT_L8_UNORM:
1250 return PIPE_FORMAT_R8_UNORM;
1251 case PIPE_FORMAT_L8_SNORM:
1252 return PIPE_FORMAT_R8_SNORM;
1253 case PIPE_FORMAT_L16_UNORM:
1254 return PIPE_FORMAT_R16_UNORM;
1255 case PIPE_FORMAT_L16_SNORM:
1256 return PIPE_FORMAT_R16_SNORM;
1257 case PIPE_FORMAT_L16_FLOAT:
1258 return PIPE_FORMAT_R16_FLOAT;
1259 case PIPE_FORMAT_L32_FLOAT:
1260 return PIPE_FORMAT_R32_FLOAT;
1261 case PIPE_FORMAT_L8_UINT:
1262 return PIPE_FORMAT_R8_UINT;
1263 case PIPE_FORMAT_L8_SINT:
1264 return PIPE_FORMAT_R8_SINT;
1265 case PIPE_FORMAT_L16_UINT:
1266 return PIPE_FORMAT_R16_UINT;
1267 case PIPE_FORMAT_L16_SINT:
1268 return PIPE_FORMAT_R16_SINT;
1269 case PIPE_FORMAT_L32_UINT:
1270 return PIPE_FORMAT_R32_UINT;
1271 case PIPE_FORMAT_L32_SINT:
1272 return PIPE_FORMAT_R32_SINT;
1273
1274 case PIPE_FORMAT_LATC1_UNORM:
1275 return PIPE_FORMAT_RGTC1_UNORM;
1276 case PIPE_FORMAT_LATC1_SNORM:
1277 return PIPE_FORMAT_RGTC1_SNORM;
1278
1279 case PIPE_FORMAT_L4A4_UNORM:
1280 return PIPE_FORMAT_R4A4_UNORM;
1281
1282 case PIPE_FORMAT_L8A8_UNORM:
1283 return PIPE_FORMAT_R8A8_UNORM;
1284 case PIPE_FORMAT_L8A8_SNORM:
1285 return PIPE_FORMAT_R8A8_SNORM;
1286 case PIPE_FORMAT_L16A16_UNORM:
1287 return PIPE_FORMAT_R16A16_UNORM;
1288 case PIPE_FORMAT_L16A16_SNORM:
1289 return PIPE_FORMAT_R16A16_SNORM;
1290 case PIPE_FORMAT_L16A16_FLOAT:
1291 return PIPE_FORMAT_R16A16_FLOAT;
1292 case PIPE_FORMAT_L32A32_FLOAT:
1293 return PIPE_FORMAT_R32A32_FLOAT;
1294 case PIPE_FORMAT_L8A8_UINT:
1295 return PIPE_FORMAT_R8A8_UINT;
1296 case PIPE_FORMAT_L8A8_SINT:
1297 return PIPE_FORMAT_R8A8_SINT;
1298 case PIPE_FORMAT_L16A16_UINT:
1299 return PIPE_FORMAT_R16A16_UINT;
1300 case PIPE_FORMAT_L16A16_SINT:
1301 return PIPE_FORMAT_R16A16_SINT;
1302 case PIPE_FORMAT_L32A32_UINT:
1303 return PIPE_FORMAT_R32A32_UINT;
1304 case PIPE_FORMAT_L32A32_SINT:
1305 return PIPE_FORMAT_R32A32_SINT;
1306
1307 /* We don't have compressed red-alpha variants for these. */
1308 case PIPE_FORMAT_LATC2_UNORM:
1309 case PIPE_FORMAT_LATC2_SNORM:
1310 return PIPE_FORMAT_NONE;
1311
1312 default:
1313 assert(!util_format_is_luminance(format) &&
1314 !util_format_is_luminance_alpha(format));
1315 return format;
1316 }
1317 }
1318
1319 static inline unsigned
1320 util_format_get_num_planes(enum pipe_format format)
1321 {
1322 switch (util_format_description(format)->layout) {
1323 case UTIL_FORMAT_LAYOUT_PLANAR3:
1324 return 3;
1325 case UTIL_FORMAT_LAYOUT_PLANAR2:
1326 return 2;
1327 default:
1328 return 1;
1329 }
1330 }
1331
1332 static inline enum pipe_format
1333 util_format_get_plane_format(enum pipe_format format, unsigned plane)
1334 {
1335 switch (format) {
1336 case PIPE_FORMAT_YV12:
1337 case PIPE_FORMAT_YV16:
1338 case PIPE_FORMAT_IYUV:
1339 return PIPE_FORMAT_R8_UNORM;
1340 case PIPE_FORMAT_NV12:
1341 return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_RG88_UNORM;
1342 case PIPE_FORMAT_NV21:
1343 return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_GR88_UNORM;
1344 case PIPE_FORMAT_P010:
1345 case PIPE_FORMAT_P016:
1346 return !plane ? PIPE_FORMAT_R16_UNORM : PIPE_FORMAT_R16G16_UNORM;
1347 default:
1348 return format;
1349 }
1350 }
1351
1352 static inline unsigned
1353 util_format_get_plane_width(enum pipe_format format, unsigned plane,
1354 unsigned width)
1355 {
1356 switch (format) {
1357 case PIPE_FORMAT_YV12:
1358 case PIPE_FORMAT_YV16:
1359 case PIPE_FORMAT_IYUV:
1360 case PIPE_FORMAT_NV12:
1361 case PIPE_FORMAT_NV21:
1362 case PIPE_FORMAT_P010:
1363 case PIPE_FORMAT_P016:
1364 return !plane ? width : (width + 1) / 2;
1365 default:
1366 return width;
1367 }
1368 }
1369
1370 static inline unsigned
1371 util_format_get_plane_height(enum pipe_format format, unsigned plane,
1372 unsigned height)
1373 {
1374 switch (format) {
1375 case PIPE_FORMAT_YV12:
1376 case PIPE_FORMAT_IYUV:
1377 case PIPE_FORMAT_NV12:
1378 case PIPE_FORMAT_NV21:
1379 case PIPE_FORMAT_P010:
1380 case PIPE_FORMAT_P016:
1381 return !plane ? height : (height + 1) / 2;
1382 case PIPE_FORMAT_YV16:
1383 default:
1384 return height;
1385 }
1386 }
1387
1388 bool util_format_planar_is_supported(struct pipe_screen *screen,
1389 enum pipe_format format,
1390 enum pipe_texture_target target,
1391 unsigned sample_count,
1392 unsigned storage_sample_count,
1393 unsigned bind);
1394
1395 /**
1396 * Return the number of components stored.
1397 * Formats with block size != 1x1 will always have 1 component (the block).
1398 */
1399 static inline unsigned
1400 util_format_get_nr_components(enum pipe_format format)
1401 {
1402 const struct util_format_description *desc = util_format_description(format);
1403 return desc->nr_channels;
1404 }
1405
1406 /**
1407 * Return the index of the first non-void channel
1408 * -1 if no non-void channels
1409 */
1410 static inline int
1411 util_format_get_first_non_void_channel(enum pipe_format format)
1412 {
1413 const struct util_format_description *desc = util_format_description(format);
1414 int i;
1415
1416 for (i = 0; i < 4; i++)
1417 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1418 break;
1419
1420 if (i == 4)
1421 return -1;
1422
1423 return i;
1424 }
1425
1426 /**
1427 * Whether this format is any 8-bit UNORM variant. Looser than
1428 * util_is_rgba8_variant (also includes alpha textures, for instance).
1429 */
1430
1431 static inline bool
1432 util_format_is_unorm8(const struct util_format_description *desc)
1433 {
1434 int c = util_format_get_first_non_void_channel(desc->format);
1435
1436 if (c == -1)
1437 return false;
1438
1439 return desc->is_unorm && desc->is_array && desc->channel[c].size == 8;
1440 }
1441
1442 static inline void
1443 util_format_unpack_z_float(enum pipe_format format, float *dst,
1444 const void *src, unsigned w)
1445 {
1446 const struct util_format_description *desc = util_format_description(format);
1447
1448 desc->unpack_z_float(dst, 0, (const uint8_t *)src, 0, w, 1);
1449 }
1450
1451 static inline void
1452 util_format_unpack_z_32unorm(enum pipe_format format, uint32_t *dst,
1453 const void *src, unsigned w)
1454 {
1455 const struct util_format_description *desc = util_format_description(format);
1456
1457 desc->unpack_z_32unorm(dst, 0, (const uint8_t *)src, 0, w, 1);
1458 }
1459
1460 static inline void
1461 util_format_unpack_s_8uint(enum pipe_format format, uint8_t *dst,
1462 const void *src, unsigned w)
1463 {
1464 const struct util_format_description *desc = util_format_description(format);
1465
1466 desc->unpack_s_8uint(dst, 0, (const uint8_t *)src, 0, w, 1);
1467 }
1468
1469 static inline void
1470 util_format_unpack_rgba_float(enum pipe_format format, float *dst,
1471 const void *src, unsigned w)
1472 {
1473 const struct util_format_description *desc = util_format_description(format);
1474
1475 desc->unpack_rgba_float(dst, 0, (const uint8_t *)src, 0, w, 1);
1476 }
1477
1478 /**
1479 * Unpacks a row of color data to 32-bit RGBA, either integers for pure
1480 * integer formats (sign-extended for signed data), or 32-bit floats.
1481 */
1482 static inline void
1483 util_format_unpack_rgba(enum pipe_format format, void *dst,
1484 const void *src, unsigned w)
1485 {
1486 const struct util_format_description *desc = util_format_description(format);
1487
1488 if (util_format_is_pure_uint(format))
1489 desc->unpack_rgba_uint((uint32_t *)dst, 0, (const uint8_t *)src, 0, w, 1);
1490 else if (util_format_is_pure_sint(format))
1491 desc->unpack_rgba_sint((int32_t *)dst, 0, (const uint8_t *)src, 0, w, 1);
1492 else
1493 desc->unpack_rgba_float((float *)dst, 0, (const uint8_t *)src, 0, w, 1);
1494 }
1495
1496 static inline void
1497 util_format_pack_z_float(enum pipe_format format, void *dst,
1498 const float *src, unsigned w)
1499 {
1500 const struct util_format_description *desc = util_format_description(format);
1501
1502 desc->pack_z_float((uint8_t *)dst, 0, src, 0, w, 1);
1503 }
1504
1505 static inline void
1506 util_format_pack_z_32unorm(enum pipe_format format, void *dst,
1507 const uint32_t *src, unsigned w)
1508 {
1509 const struct util_format_description *desc = util_format_description(format);
1510
1511 desc->pack_z_32unorm((uint8_t *)dst, 0, src, 0, w, 1);
1512 }
1513
1514 static inline void
1515 util_format_pack_s_8uint(enum pipe_format format, void *dst,
1516 const uint8_t *src, unsigned w)
1517 {
1518 const struct util_format_description *desc = util_format_description(format);
1519
1520 desc->pack_s_8uint((uint8_t *)dst, 0, src, 0, w, 1);
1521 }
1522
1523 /**
1524 * Packs a row of color data from 32-bit RGBA, either integers for pure
1525 * integer formats, or 32-bit floats. Values are clamped to the packed
1526 * representation's range.
1527 */
1528 static inline void
1529 util_format_pack_rgba(enum pipe_format format, void *dst,
1530 const void *src, unsigned w)
1531 {
1532 const struct util_format_description *desc = util_format_description(format);
1533
1534 if (util_format_is_pure_uint(format))
1535 desc->pack_rgba_uint((uint8_t *)dst, 0, (const uint32_t *)src, 0, w, 1);
1536 else if (util_format_is_pure_sint(format))
1537 desc->pack_rgba_sint((uint8_t *)dst, 0, (const int32_t *)src, 0, w, 1);
1538 else
1539 desc->pack_rgba_float((uint8_t *)dst, 0, (const float *)src, 0, w, 1);
1540 }
1541
1542 /*
1543 * Format access functions.
1544 */
1545
1546 void
1547 util_format_read_4f(enum pipe_format format,
1548 float *dst, unsigned dst_stride,
1549 const void *src, unsigned src_stride,
1550 unsigned x, unsigned y, unsigned w, unsigned h);
1551
1552 void
1553 util_format_write_4f(enum pipe_format format,
1554 const float *src, unsigned src_stride,
1555 void *dst, unsigned dst_stride,
1556 unsigned x, unsigned y, unsigned w, unsigned h);
1557
1558 void
1559 util_format_read_4ub(enum pipe_format format,
1560 uint8_t *dst, unsigned dst_stride,
1561 const void *src, unsigned src_stride,
1562 unsigned x, unsigned y, unsigned w, unsigned h);
1563
1564 void
1565 util_format_write_4ub(enum pipe_format format,
1566 const uint8_t *src, unsigned src_stride,
1567 void *dst, unsigned dst_stride,
1568 unsigned x, unsigned y, unsigned w, unsigned h);
1569
1570 void
1571 util_format_read_4ui(enum pipe_format format,
1572 unsigned *dst, unsigned dst_stride,
1573 const void *src, unsigned src_stride,
1574 unsigned x, unsigned y, unsigned w, unsigned h);
1575
1576 void
1577 util_format_write_4ui(enum pipe_format format,
1578 const unsigned int *src, unsigned src_stride,
1579 void *dst, unsigned dst_stride,
1580 unsigned x, unsigned y, unsigned w, unsigned h);
1581
1582 void
1583 util_format_read_4i(enum pipe_format format,
1584 int *dst, unsigned dst_stride,
1585 const void *src, unsigned src_stride,
1586 unsigned x, unsigned y, unsigned w, unsigned h);
1587
1588 void
1589 util_format_write_4i(enum pipe_format format,
1590 const int *src, unsigned src_stride,
1591 void *dst, unsigned dst_stride,
1592 unsigned x, unsigned y, unsigned w, unsigned h);
1593
1594 /*
1595 * Generic format conversion;
1596 */
1597
1598 boolean
1599 util_format_fits_8unorm(const struct util_format_description *format_desc);
1600
1601 boolean
1602 util_format_translate(enum pipe_format dst_format,
1603 void *dst, unsigned dst_stride,
1604 unsigned dst_x, unsigned dst_y,
1605 enum pipe_format src_format,
1606 const void *src, unsigned src_stride,
1607 unsigned src_x, unsigned src_y,
1608 unsigned width, unsigned height);
1609
1610 boolean
1611 util_format_translate_3d(enum pipe_format dst_format,
1612 void *dst, unsigned dst_stride,
1613 unsigned dst_slice_stride,
1614 unsigned dst_x, unsigned dst_y,
1615 unsigned dst_z,
1616 enum pipe_format src_format,
1617 const void *src, unsigned src_stride,
1618 unsigned src_slice_stride,
1619 unsigned src_x, unsigned src_y,
1620 unsigned src_z, unsigned width,
1621 unsigned height, unsigned depth);
1622
1623 /*
1624 * Swizzle operations.
1625 */
1626
1627 /* Compose two sets of swizzles.
1628 * If V is a 4D vector and the function parameters represent functions that
1629 * swizzle vector components, this holds:
1630 * swz2(swz1(V)) = dst(V)
1631 */
1632 void util_format_compose_swizzles(const unsigned char swz1[4],
1633 const unsigned char swz2[4],
1634 unsigned char dst[4]);
1635
1636 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1637 * to \param src and store the result in \param dst.
1638 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1639 */
1640 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1641 const union pipe_color_union *src,
1642 const unsigned char swz[4],
1643 const boolean is_integer);
1644
1645 void pipe_swizzle_4f(float *dst, const float *src,
1646 const unsigned char swz[4]);
1647
1648 void util_format_unswizzle_4f(float *dst, const float *src,
1649 const unsigned char swz[4]);
1650
1651 enum pipe_format
1652 util_format_snorm8_to_sint8(enum pipe_format format);
1653
1654
1655 extern void
1656 util_copy_rect(ubyte * dst, enum pipe_format format,
1657 unsigned dst_stride, unsigned dst_x, unsigned dst_y,
1658 unsigned width, unsigned height, const ubyte * src,
1659 int src_stride, unsigned src_x, unsigned src_y);
1660
1661 #ifdef __cplusplus
1662 } // extern "C" {
1663 #endif
1664
1665 #endif /* ! U_FORMAT_H */