util: Split the pack/unpack functions out of the format desc.
[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 struct util_format_pack_description {
246 /**
247 * Pack pixel blocks from R8G8B8A8_UNORM.
248 * Note: strides are in bytes.
249 *
250 * Only defined for non-depth-stencil formats.
251 */
252 void
253 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
254 const uint8_t *src, unsigned src_stride,
255 unsigned width, unsigned height);
256
257 /**
258 * Pack pixel blocks from R32G32B32A32_FLOAT.
259 * Note: strides are in bytes.
260 *
261 * Only defined for non-depth-stencil formats.
262 */
263 void
264 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
265 const float *src, unsigned src_stride,
266 unsigned width, unsigned height);
267
268 /**
269 * Pack pixels from Z32_FLOAT.
270 * Note: strides are in bytes.
271 *
272 * Only defined for depth formats.
273 */
274 void
275 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
276 const uint32_t *src, unsigned src_stride,
277 unsigned width, unsigned height);
278
279 /**
280 * Pack pixels from Z32_FLOAT.
281 * Note: strides are in bytes.
282 *
283 * Only defined for depth formats.
284 */
285 void
286 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
287 const float *src, unsigned src_stride,
288 unsigned width, unsigned height);
289
290 /**
291 * Pack pixels from S8_UINT.
292 * Note: strides are in bytes.
293 *
294 * Only defined for stencil formats.
295 */
296 void
297 (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
298 const uint8_t *src, unsigned src_stride,
299 unsigned width, unsigned height);
300
301 void
302 (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
303 const uint32_t *src, unsigned src_stride,
304 unsigned width, unsigned height);
305
306 void
307 (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
308 const int32_t *src, unsigned src_stride,
309 unsigned width, unsigned height);
310 };
311
312
313 struct util_format_unpack_description {
314 /**
315 * Unpack pixel blocks to R8G8B8A8_UNORM.
316 * Note: strides are in bytes.
317 *
318 * Only defined for non-depth-stencil formats.
319 */
320 void
321 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
322 const uint8_t *src, unsigned src_stride,
323 unsigned width, unsigned height);
324
325 /**
326 * Fetch a single pixel (i, j) from a block.
327 *
328 * XXX: Only defined for a very few select formats.
329 */
330 void
331 (*fetch_rgba_8unorm)(uint8_t *dst,
332 const uint8_t *src,
333 unsigned i, unsigned j);
334
335 /**
336 * Unpack pixel blocks to R32G32B32A32_UINT/_INT_FLOAT based on whether the
337 * type is pure uint, int, or other.
338 *
339 * Note: strides are in bytes.
340 *
341 * Only defined for non-depth-stencil formats.
342 */
343 void
344 (*unpack_rgba)(void *dst, unsigned dst_stride,
345 const uint8_t *src, unsigned src_stride,
346 unsigned width, unsigned height);
347
348 /**
349 * Fetch a single pixel (i, j) from a block.
350 *
351 * Only defined for non-depth-stencil and non-integer formats.
352 */
353 void
354 (*fetch_rgba_float)(float *dst,
355 const uint8_t *src,
356 unsigned i, unsigned j);
357
358 /**
359 * Unpack pixels to Z32_UNORM.
360 * Note: strides are in bytes.
361 *
362 * Only defined for depth formats.
363 */
364 void
365 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
366 const uint8_t *src, unsigned src_stride,
367 unsigned width, unsigned height);
368
369 /**
370 * Unpack pixels to Z32_FLOAT.
371 * Note: strides are in bytes.
372 *
373 * Only defined for depth formats.
374 */
375 void
376 (*unpack_z_float)(float *dst, unsigned dst_stride,
377 const uint8_t *src, unsigned src_stride,
378 unsigned width, unsigned height);
379
380 /**
381 * Unpack pixels to S8_UINT.
382 * Note: strides are in bytes.
383 *
384 * Only defined for stencil formats.
385 */
386 void
387 (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
388 const uint8_t *src, unsigned src_stride,
389 unsigned width, unsigned height);
390
391 /**
392 * Fetch a single pixel (i, j) from a block.
393 *
394 * Only defined for unsigned (pure) integer formats.
395 */
396 void
397 (*fetch_rgba_uint)(uint32_t *dst,
398 const uint8_t *src,
399 unsigned i, unsigned j);
400
401 /**
402 * Fetch a single pixel (i, j) from a block.
403 *
404 * Only defined for signed (pure) integer formats.
405 */
406 void
407 (*fetch_rgba_sint)(int32_t *dst,
408 const uint8_t *src,
409 unsigned i, unsigned j);
410 };
411
412 const struct util_format_description *
413 util_format_description(enum pipe_format format) ATTRIBUTE_CONST;
414
415 const struct util_format_pack_description *
416 util_format_pack_description(enum pipe_format format);
417
418 const struct util_format_unpack_description *
419 util_format_unpack_description(enum pipe_format format);
420
421
422 /*
423 * Format query functions.
424 */
425
426 static inline const char *
427 util_format_name(enum pipe_format format)
428 {
429 const struct util_format_description *desc = util_format_description(format);
430
431 assert(desc);
432 if (!desc) {
433 return "PIPE_FORMAT_???";
434 }
435
436 return desc->name;
437 }
438
439 static inline const char *
440 util_format_short_name(enum pipe_format format)
441 {
442 const struct util_format_description *desc = util_format_description(format);
443
444 assert(desc);
445 if (!desc) {
446 return "???";
447 }
448
449 return desc->short_name;
450 }
451
452 /**
453 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
454 */
455 static inline boolean
456 util_format_is_plain(enum pipe_format format)
457 {
458 const struct util_format_description *desc = util_format_description(format);
459
460 if (!format) {
461 return FALSE;
462 }
463
464 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
465 }
466
467 static inline boolean
468 util_format_is_compressed(enum pipe_format format)
469 {
470 const struct util_format_description *desc = util_format_description(format);
471
472 assert(desc);
473 if (!desc) {
474 return FALSE;
475 }
476
477 switch (desc->layout) {
478 case UTIL_FORMAT_LAYOUT_S3TC:
479 case UTIL_FORMAT_LAYOUT_RGTC:
480 case UTIL_FORMAT_LAYOUT_ETC:
481 case UTIL_FORMAT_LAYOUT_BPTC:
482 case UTIL_FORMAT_LAYOUT_ASTC:
483 case UTIL_FORMAT_LAYOUT_ATC:
484 case UTIL_FORMAT_LAYOUT_FXT1:
485 /* XXX add other formats in the future */
486 return TRUE;
487 default:
488 return FALSE;
489 }
490 }
491
492 static inline boolean
493 util_format_is_s3tc(enum pipe_format format)
494 {
495 const struct util_format_description *desc = util_format_description(format);
496
497 assert(desc);
498 if (!desc) {
499 return FALSE;
500 }
501
502 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
503 }
504
505 static inline boolean
506 util_format_is_etc(enum pipe_format format)
507 {
508 const struct util_format_description *desc = util_format_description(format);
509
510 assert(desc);
511 if (!desc) {
512 return FALSE;
513 }
514
515 return desc->layout == UTIL_FORMAT_LAYOUT_ETC ? TRUE : FALSE;
516 }
517
518 static inline boolean
519 util_format_is_srgb(enum pipe_format format)
520 {
521 const struct util_format_description *desc = util_format_description(format);
522 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
523 }
524
525 static inline boolean
526 util_format_has_depth(const struct util_format_description *desc)
527 {
528 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
529 desc->swizzle[0] != PIPE_SWIZZLE_NONE;
530 }
531
532 static inline boolean
533 util_format_has_stencil(const struct util_format_description *desc)
534 {
535 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
536 desc->swizzle[1] != PIPE_SWIZZLE_NONE;
537 }
538
539 static inline boolean
540 util_format_is_depth_or_stencil(enum pipe_format format)
541 {
542 const struct util_format_description *desc = util_format_description(format);
543
544 assert(desc);
545 if (!desc) {
546 return FALSE;
547 }
548
549 return util_format_has_depth(desc) ||
550 util_format_has_stencil(desc);
551 }
552
553 static inline boolean
554 util_format_is_depth_and_stencil(enum pipe_format format)
555 {
556 const struct util_format_description *desc = util_format_description(format);
557
558 assert(desc);
559 if (!desc) {
560 return FALSE;
561 }
562
563 return util_format_has_depth(desc) &&
564 util_format_has_stencil(desc);
565 }
566
567 /**
568 * For depth-stencil formats, return the equivalent depth-only format.
569 */
570 static inline enum pipe_format
571 util_format_get_depth_only(enum pipe_format format)
572 {
573 switch (format) {
574 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
575 return PIPE_FORMAT_Z24X8_UNORM;
576
577 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
578 return PIPE_FORMAT_X8Z24_UNORM;
579
580 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
581 return PIPE_FORMAT_Z32_FLOAT;
582
583 default:
584 return format;
585 }
586 }
587
588 static inline boolean
589 util_format_is_yuv(enum pipe_format format)
590 {
591 const struct util_format_description *desc = util_format_description(format);
592
593 assert(desc);
594 if (!desc) {
595 return FALSE;
596 }
597
598 return desc->colorspace == UTIL_FORMAT_COLORSPACE_YUV;
599 }
600
601 /**
602 * Calculates the depth format type based upon the incoming format description.
603 */
604 static inline unsigned
605 util_get_depth_format_type(const struct util_format_description *desc)
606 {
607 unsigned depth_channel = desc->swizzle[0];
608 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
609 depth_channel != PIPE_SWIZZLE_NONE) {
610 return desc->channel[depth_channel].type;
611 } else {
612 return UTIL_FORMAT_TYPE_VOID;
613 }
614 }
615
616
617 /**
618 * Calculates the MRD for the depth format. MRD is used in depth bias
619 * for UNORM and unbound depth buffers. When the depth buffer is floating
620 * point, the depth bias calculation does not use the MRD. However, the
621 * default MRD will be 1.0 / ((1 << 24) - 1).
622 */
623 double
624 util_get_depth_format_mrd(const struct util_format_description *desc);
625
626
627 /**
628 * Return whether this is an RGBA, Z, S, or combined ZS format.
629 * Useful for initializing pipe_blit_info::mask.
630 */
631 static inline unsigned
632 util_format_get_mask(enum pipe_format format)
633 {
634 const struct util_format_description *desc =
635 util_format_description(format);
636
637 if (!desc)
638 return 0;
639
640 if (util_format_has_depth(desc)) {
641 if (util_format_has_stencil(desc)) {
642 return PIPE_MASK_ZS;
643 } else {
644 return PIPE_MASK_Z;
645 }
646 } else {
647 if (util_format_has_stencil(desc)) {
648 return PIPE_MASK_S;
649 } else {
650 return PIPE_MASK_RGBA;
651 }
652 }
653 }
654
655 /**
656 * Give the RGBA colormask of the channels that can be represented in this
657 * format.
658 *
659 * That is, the channels whose values are preserved.
660 */
661 static inline unsigned
662 util_format_colormask(const struct util_format_description *desc)
663 {
664 unsigned colormask;
665 unsigned chan;
666
667 switch (desc->colorspace) {
668 case UTIL_FORMAT_COLORSPACE_RGB:
669 case UTIL_FORMAT_COLORSPACE_SRGB:
670 case UTIL_FORMAT_COLORSPACE_YUV:
671 colormask = 0;
672 for (chan = 0; chan < 4; ++chan) {
673 if (desc->swizzle[chan] < 4) {
674 colormask |= (1 << chan);
675 }
676 }
677 return colormask;
678 case UTIL_FORMAT_COLORSPACE_ZS:
679 return 0;
680 default:
681 assert(0);
682 return 0;
683 }
684 }
685
686
687 /**
688 * Checks if color mask covers every channel for the specified format
689 *
690 * @param desc a format description to check colormask with
691 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
692 */
693 static inline boolean
694 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
695 {
696 return (~colormask & util_format_colormask(desc)) == 0;
697 }
698
699
700 boolean
701 util_format_is_float(enum pipe_format format);
702
703
704 boolean
705 util_format_has_alpha(enum pipe_format format);
706
707
708 boolean
709 util_format_is_luminance(enum pipe_format format);
710
711 boolean
712 util_format_is_alpha(enum pipe_format format);
713
714 boolean
715 util_format_is_luminance_alpha(enum pipe_format format);
716
717
718 boolean
719 util_format_is_intensity(enum pipe_format format);
720
721 boolean
722 util_format_is_subsampled_422(enum pipe_format format);
723
724 boolean
725 util_format_is_pure_integer(enum pipe_format format);
726
727 boolean
728 util_format_is_pure_sint(enum pipe_format format);
729
730 boolean
731 util_format_is_pure_uint(enum pipe_format format);
732
733 boolean
734 util_format_is_snorm(enum pipe_format format);
735
736 boolean
737 util_format_is_unorm(enum pipe_format format);
738
739 boolean
740 util_format_is_snorm8(enum pipe_format format);
741
742 /**
743 * Check if the src format can be blitted to the destination format with
744 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
745 * the reverse.
746 */
747 boolean
748 util_is_format_compatible(const struct util_format_description *src_desc,
749 const struct util_format_description *dst_desc);
750
751 /**
752 * Whether this format is a rgab8 variant.
753 *
754 * That is, any format that matches the
755 *
756 * PIPE_FORMAT_?8?8?8?8_UNORM
757 */
758 static inline boolean
759 util_format_is_rgba8_variant(const struct util_format_description *desc)
760 {
761 unsigned chan;
762
763 if(desc->block.width != 1 ||
764 desc->block.height != 1 ||
765 desc->block.bits != 32)
766 return FALSE;
767
768 for(chan = 0; chan < 4; ++chan) {
769 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
770 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
771 return FALSE;
772 if(desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED &&
773 !desc->channel[chan].normalized)
774 return FALSE;
775 if(desc->channel[chan].size != 8)
776 return FALSE;
777 }
778
779 return TRUE;
780 }
781
782 /**
783 * Return total bits needed for the pixel format per block.
784 */
785 static inline uint
786 util_format_get_blocksizebits(enum pipe_format format)
787 {
788 const struct util_format_description *desc = util_format_description(format);
789
790 assert(desc);
791 if (!desc) {
792 return 0;
793 }
794
795 return desc->block.bits;
796 }
797
798 /**
799 * Return bytes per block (not pixel) for the given format.
800 */
801 static inline uint
802 util_format_get_blocksize(enum pipe_format format)
803 {
804 uint bits = util_format_get_blocksizebits(format);
805 uint bytes = bits / 8;
806
807 assert(bits % 8 == 0);
808 assert(bytes > 0);
809 if (bytes == 0) {
810 bytes = 1;
811 }
812
813 return bytes;
814 }
815
816 static inline uint
817 util_format_get_blockwidth(enum pipe_format format)
818 {
819 const struct util_format_description *desc = util_format_description(format);
820
821 assert(desc);
822 if (!desc) {
823 return 1;
824 }
825
826 return desc->block.width;
827 }
828
829 static inline uint
830 util_format_get_blockheight(enum pipe_format format)
831 {
832 const struct util_format_description *desc = util_format_description(format);
833
834 assert(desc);
835 if (!desc) {
836 return 1;
837 }
838
839 return desc->block.height;
840 }
841
842 static inline uint
843 util_format_get_blockdepth(enum pipe_format format)
844 {
845 const struct util_format_description *desc = util_format_description(format);
846
847 assert(desc);
848 if (!desc) {
849 return 1;
850 }
851
852 return desc->block.depth;
853 }
854
855 static inline unsigned
856 util_format_get_nblocksx(enum pipe_format format,
857 unsigned x)
858 {
859 unsigned blockwidth = util_format_get_blockwidth(format);
860 return (x + blockwidth - 1) / blockwidth;
861 }
862
863 static inline unsigned
864 util_format_get_nblocksy(enum pipe_format format,
865 unsigned y)
866 {
867 unsigned blockheight = util_format_get_blockheight(format);
868 return (y + blockheight - 1) / blockheight;
869 }
870
871 static inline unsigned
872 util_format_get_nblocksz(enum pipe_format format,
873 unsigned z)
874 {
875 unsigned blockdepth = util_format_get_blockdepth(format);
876 return (z + blockdepth - 1) / blockdepth;
877 }
878
879 static inline unsigned
880 util_format_get_nblocks(enum pipe_format format,
881 unsigned width,
882 unsigned height)
883 {
884 assert(util_format_get_blockdepth(format) == 1);
885 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
886 }
887
888 static inline size_t
889 util_format_get_stride(enum pipe_format format,
890 unsigned width)
891 {
892 return (size_t)util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
893 }
894
895 static inline size_t
896 util_format_get_2d_size(enum pipe_format format,
897 size_t stride,
898 unsigned height)
899 {
900 return util_format_get_nblocksy(format, height) * stride;
901 }
902
903 static inline uint
904 util_format_get_component_bits(enum pipe_format format,
905 enum util_format_colorspace colorspace,
906 uint component)
907 {
908 const struct util_format_description *desc = util_format_description(format);
909 enum util_format_colorspace desc_colorspace;
910
911 assert(format);
912 if (!format) {
913 return 0;
914 }
915
916 assert(component < 4);
917
918 /* Treat RGB and SRGB as equivalent. */
919 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
920 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
921 }
922 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
923 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
924 } else {
925 desc_colorspace = desc->colorspace;
926 }
927
928 if (desc_colorspace != colorspace) {
929 return 0;
930 }
931
932 switch (desc->swizzle[component]) {
933 case PIPE_SWIZZLE_X:
934 return desc->channel[0].size;
935 case PIPE_SWIZZLE_Y:
936 return desc->channel[1].size;
937 case PIPE_SWIZZLE_Z:
938 return desc->channel[2].size;
939 case PIPE_SWIZZLE_W:
940 return desc->channel[3].size;
941 default:
942 return 0;
943 }
944 }
945
946 /**
947 * Given a linear RGB colorspace format, return the corresponding SRGB
948 * format, or PIPE_FORMAT_NONE if none.
949 */
950 static inline enum pipe_format
951 util_format_srgb(enum pipe_format format)
952 {
953 if (util_format_is_srgb(format))
954 return format;
955
956 switch (format) {
957 case PIPE_FORMAT_L8_UNORM:
958 return PIPE_FORMAT_L8_SRGB;
959 case PIPE_FORMAT_R8_UNORM:
960 return PIPE_FORMAT_R8_SRGB;
961 case PIPE_FORMAT_L8A8_UNORM:
962 return PIPE_FORMAT_L8A8_SRGB;
963 case PIPE_FORMAT_R8G8_UNORM:
964 return PIPE_FORMAT_R8G8_SRGB;
965 case PIPE_FORMAT_R8G8B8_UNORM:
966 return PIPE_FORMAT_R8G8B8_SRGB;
967 case PIPE_FORMAT_B8G8R8_UNORM:
968 return PIPE_FORMAT_B8G8R8_SRGB;
969 case PIPE_FORMAT_A8B8G8R8_UNORM:
970 return PIPE_FORMAT_A8B8G8R8_SRGB;
971 case PIPE_FORMAT_X8B8G8R8_UNORM:
972 return PIPE_FORMAT_X8B8G8R8_SRGB;
973 case PIPE_FORMAT_B8G8R8A8_UNORM:
974 return PIPE_FORMAT_B8G8R8A8_SRGB;
975 case PIPE_FORMAT_B8G8R8X8_UNORM:
976 return PIPE_FORMAT_B8G8R8X8_SRGB;
977 case PIPE_FORMAT_A8R8G8B8_UNORM:
978 return PIPE_FORMAT_A8R8G8B8_SRGB;
979 case PIPE_FORMAT_X8R8G8B8_UNORM:
980 return PIPE_FORMAT_X8R8G8B8_SRGB;
981 case PIPE_FORMAT_R8G8B8A8_UNORM:
982 return PIPE_FORMAT_R8G8B8A8_SRGB;
983 case PIPE_FORMAT_R8G8B8X8_UNORM:
984 return PIPE_FORMAT_R8G8B8X8_SRGB;
985 case PIPE_FORMAT_DXT1_RGB:
986 return PIPE_FORMAT_DXT1_SRGB;
987 case PIPE_FORMAT_DXT1_RGBA:
988 return PIPE_FORMAT_DXT1_SRGBA;
989 case PIPE_FORMAT_DXT3_RGBA:
990 return PIPE_FORMAT_DXT3_SRGBA;
991 case PIPE_FORMAT_DXT5_RGBA:
992 return PIPE_FORMAT_DXT5_SRGBA;
993 case PIPE_FORMAT_B5G6R5_UNORM:
994 return PIPE_FORMAT_B5G6R5_SRGB;
995 case PIPE_FORMAT_BPTC_RGBA_UNORM:
996 return PIPE_FORMAT_BPTC_SRGBA;
997 case PIPE_FORMAT_ETC2_RGB8:
998 return PIPE_FORMAT_ETC2_SRGB8;
999 case PIPE_FORMAT_ETC2_RGB8A1:
1000 return PIPE_FORMAT_ETC2_SRGB8A1;
1001 case PIPE_FORMAT_ETC2_RGBA8:
1002 return PIPE_FORMAT_ETC2_SRGBA8;
1003 case PIPE_FORMAT_ASTC_4x4:
1004 return PIPE_FORMAT_ASTC_4x4_SRGB;
1005 case PIPE_FORMAT_ASTC_5x4:
1006 return PIPE_FORMAT_ASTC_5x4_SRGB;
1007 case PIPE_FORMAT_ASTC_5x5:
1008 return PIPE_FORMAT_ASTC_5x5_SRGB;
1009 case PIPE_FORMAT_ASTC_6x5:
1010 return PIPE_FORMAT_ASTC_6x5_SRGB;
1011 case PIPE_FORMAT_ASTC_6x6:
1012 return PIPE_FORMAT_ASTC_6x6_SRGB;
1013 case PIPE_FORMAT_ASTC_8x5:
1014 return PIPE_FORMAT_ASTC_8x5_SRGB;
1015 case PIPE_FORMAT_ASTC_8x6:
1016 return PIPE_FORMAT_ASTC_8x6_SRGB;
1017 case PIPE_FORMAT_ASTC_8x8:
1018 return PIPE_FORMAT_ASTC_8x8_SRGB;
1019 case PIPE_FORMAT_ASTC_10x5:
1020 return PIPE_FORMAT_ASTC_10x5_SRGB;
1021 case PIPE_FORMAT_ASTC_10x6:
1022 return PIPE_FORMAT_ASTC_10x6_SRGB;
1023 case PIPE_FORMAT_ASTC_10x8:
1024 return PIPE_FORMAT_ASTC_10x8_SRGB;
1025 case PIPE_FORMAT_ASTC_10x10:
1026 return PIPE_FORMAT_ASTC_10x10_SRGB;
1027 case PIPE_FORMAT_ASTC_12x10:
1028 return PIPE_FORMAT_ASTC_12x10_SRGB;
1029 case PIPE_FORMAT_ASTC_12x12:
1030 return PIPE_FORMAT_ASTC_12x12_SRGB;
1031 case PIPE_FORMAT_ASTC_3x3x3:
1032 return PIPE_FORMAT_ASTC_3x3x3_SRGB;
1033 case PIPE_FORMAT_ASTC_4x3x3:
1034 return PIPE_FORMAT_ASTC_4x3x3_SRGB;
1035 case PIPE_FORMAT_ASTC_4x4x3:
1036 return PIPE_FORMAT_ASTC_4x4x3_SRGB;
1037 case PIPE_FORMAT_ASTC_4x4x4:
1038 return PIPE_FORMAT_ASTC_4x4x4_SRGB;
1039 case PIPE_FORMAT_ASTC_5x4x4:
1040 return PIPE_FORMAT_ASTC_5x4x4_SRGB;
1041 case PIPE_FORMAT_ASTC_5x5x4:
1042 return PIPE_FORMAT_ASTC_5x5x4_SRGB;
1043 case PIPE_FORMAT_ASTC_5x5x5:
1044 return PIPE_FORMAT_ASTC_5x5x5_SRGB;
1045 case PIPE_FORMAT_ASTC_6x5x5:
1046 return PIPE_FORMAT_ASTC_6x5x5_SRGB;
1047 case PIPE_FORMAT_ASTC_6x6x5:
1048 return PIPE_FORMAT_ASTC_6x6x5_SRGB;
1049 case PIPE_FORMAT_ASTC_6x6x6:
1050 return PIPE_FORMAT_ASTC_6x6x6_SRGB;
1051
1052 default:
1053 return PIPE_FORMAT_NONE;
1054 }
1055 }
1056
1057 /**
1058 * Given an sRGB format, return the corresponding linear colorspace format.
1059 * For non sRGB formats, return the format unchanged.
1060 */
1061 static inline enum pipe_format
1062 util_format_linear(enum pipe_format format)
1063 {
1064 switch (format) {
1065 case PIPE_FORMAT_L8_SRGB:
1066 return PIPE_FORMAT_L8_UNORM;
1067 case PIPE_FORMAT_R8_SRGB:
1068 return PIPE_FORMAT_R8_UNORM;
1069 case PIPE_FORMAT_L8A8_SRGB:
1070 return PIPE_FORMAT_L8A8_UNORM;
1071 case PIPE_FORMAT_R8G8_SRGB:
1072 return PIPE_FORMAT_R8G8_UNORM;
1073 case PIPE_FORMAT_R8G8B8_SRGB:
1074 return PIPE_FORMAT_R8G8B8_UNORM;
1075 case PIPE_FORMAT_B8G8R8_SRGB:
1076 return PIPE_FORMAT_B8G8R8_UNORM;
1077 case PIPE_FORMAT_A8B8G8R8_SRGB:
1078 return PIPE_FORMAT_A8B8G8R8_UNORM;
1079 case PIPE_FORMAT_X8B8G8R8_SRGB:
1080 return PIPE_FORMAT_X8B8G8R8_UNORM;
1081 case PIPE_FORMAT_B8G8R8A8_SRGB:
1082 return PIPE_FORMAT_B8G8R8A8_UNORM;
1083 case PIPE_FORMAT_B8G8R8X8_SRGB:
1084 return PIPE_FORMAT_B8G8R8X8_UNORM;
1085 case PIPE_FORMAT_A8R8G8B8_SRGB:
1086 return PIPE_FORMAT_A8R8G8B8_UNORM;
1087 case PIPE_FORMAT_X8R8G8B8_SRGB:
1088 return PIPE_FORMAT_X8R8G8B8_UNORM;
1089 case PIPE_FORMAT_R8G8B8A8_SRGB:
1090 return PIPE_FORMAT_R8G8B8A8_UNORM;
1091 case PIPE_FORMAT_R8G8B8X8_SRGB:
1092 return PIPE_FORMAT_R8G8B8X8_UNORM;
1093 case PIPE_FORMAT_DXT1_SRGB:
1094 return PIPE_FORMAT_DXT1_RGB;
1095 case PIPE_FORMAT_DXT1_SRGBA:
1096 return PIPE_FORMAT_DXT1_RGBA;
1097 case PIPE_FORMAT_DXT3_SRGBA:
1098 return PIPE_FORMAT_DXT3_RGBA;
1099 case PIPE_FORMAT_DXT5_SRGBA:
1100 return PIPE_FORMAT_DXT5_RGBA;
1101 case PIPE_FORMAT_B5G6R5_SRGB:
1102 return PIPE_FORMAT_B5G6R5_UNORM;
1103 case PIPE_FORMAT_BPTC_SRGBA:
1104 return PIPE_FORMAT_BPTC_RGBA_UNORM;
1105 case PIPE_FORMAT_ETC2_SRGB8:
1106 return PIPE_FORMAT_ETC2_RGB8;
1107 case PIPE_FORMAT_ETC2_SRGB8A1:
1108 return PIPE_FORMAT_ETC2_RGB8A1;
1109 case PIPE_FORMAT_ETC2_SRGBA8:
1110 return PIPE_FORMAT_ETC2_RGBA8;
1111 case PIPE_FORMAT_ASTC_4x4_SRGB:
1112 return PIPE_FORMAT_ASTC_4x4;
1113 case PIPE_FORMAT_ASTC_5x4_SRGB:
1114 return PIPE_FORMAT_ASTC_5x4;
1115 case PIPE_FORMAT_ASTC_5x5_SRGB:
1116 return PIPE_FORMAT_ASTC_5x5;
1117 case PIPE_FORMAT_ASTC_6x5_SRGB:
1118 return PIPE_FORMAT_ASTC_6x5;
1119 case PIPE_FORMAT_ASTC_6x6_SRGB:
1120 return PIPE_FORMAT_ASTC_6x6;
1121 case PIPE_FORMAT_ASTC_8x5_SRGB:
1122 return PIPE_FORMAT_ASTC_8x5;
1123 case PIPE_FORMAT_ASTC_8x6_SRGB:
1124 return PIPE_FORMAT_ASTC_8x6;
1125 case PIPE_FORMAT_ASTC_8x8_SRGB:
1126 return PIPE_FORMAT_ASTC_8x8;
1127 case PIPE_FORMAT_ASTC_10x5_SRGB:
1128 return PIPE_FORMAT_ASTC_10x5;
1129 case PIPE_FORMAT_ASTC_10x6_SRGB:
1130 return PIPE_FORMAT_ASTC_10x6;
1131 case PIPE_FORMAT_ASTC_10x8_SRGB:
1132 return PIPE_FORMAT_ASTC_10x8;
1133 case PIPE_FORMAT_ASTC_10x10_SRGB:
1134 return PIPE_FORMAT_ASTC_10x10;
1135 case PIPE_FORMAT_ASTC_12x10_SRGB:
1136 return PIPE_FORMAT_ASTC_12x10;
1137 case PIPE_FORMAT_ASTC_12x12_SRGB:
1138 return PIPE_FORMAT_ASTC_12x12;
1139 case PIPE_FORMAT_ASTC_3x3x3_SRGB:
1140 return PIPE_FORMAT_ASTC_3x3x3;
1141 case PIPE_FORMAT_ASTC_4x3x3_SRGB:
1142 return PIPE_FORMAT_ASTC_4x3x3;
1143 case PIPE_FORMAT_ASTC_4x4x3_SRGB:
1144 return PIPE_FORMAT_ASTC_4x4x3;
1145 case PIPE_FORMAT_ASTC_4x4x4_SRGB:
1146 return PIPE_FORMAT_ASTC_4x4x4;
1147 case PIPE_FORMAT_ASTC_5x4x4_SRGB:
1148 return PIPE_FORMAT_ASTC_5x4x4;
1149 case PIPE_FORMAT_ASTC_5x5x4_SRGB:
1150 return PIPE_FORMAT_ASTC_5x5x4;
1151 case PIPE_FORMAT_ASTC_5x5x5_SRGB:
1152 return PIPE_FORMAT_ASTC_5x5x5;
1153 case PIPE_FORMAT_ASTC_6x5x5_SRGB:
1154 return PIPE_FORMAT_ASTC_6x5x5;
1155 case PIPE_FORMAT_ASTC_6x6x5_SRGB:
1156 return PIPE_FORMAT_ASTC_6x6x5;
1157 case PIPE_FORMAT_ASTC_6x6x6_SRGB:
1158 return PIPE_FORMAT_ASTC_6x6x6;
1159 default:
1160 return format;
1161 }
1162 }
1163
1164 /**
1165 * Given a depth-stencil format, return the corresponding stencil-only format.
1166 * For stencil-only formats, return the format unchanged.
1167 */
1168 static inline enum pipe_format
1169 util_format_stencil_only(enum pipe_format format)
1170 {
1171 switch (format) {
1172 /* mask out the depth component */
1173 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1174 return PIPE_FORMAT_X24S8_UINT;
1175 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1176 return PIPE_FORMAT_S8X24_UINT;
1177 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1178 return PIPE_FORMAT_X32_S8X24_UINT;
1179
1180 /* stencil only formats */
1181 case PIPE_FORMAT_X24S8_UINT:
1182 case PIPE_FORMAT_S8X24_UINT:
1183 case PIPE_FORMAT_X32_S8X24_UINT:
1184 case PIPE_FORMAT_S8_UINT:
1185 return format;
1186
1187 default:
1188 assert(0);
1189 return PIPE_FORMAT_NONE;
1190 }
1191 }
1192
1193 /**
1194 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1195 * This is identity for non-intensity formats.
1196 */
1197 static inline enum pipe_format
1198 util_format_intensity_to_red(enum pipe_format format)
1199 {
1200 switch (format) {
1201 case PIPE_FORMAT_I8_UNORM:
1202 return PIPE_FORMAT_R8_UNORM;
1203 case PIPE_FORMAT_I8_SNORM:
1204 return PIPE_FORMAT_R8_SNORM;
1205 case PIPE_FORMAT_I16_UNORM:
1206 return PIPE_FORMAT_R16_UNORM;
1207 case PIPE_FORMAT_I16_SNORM:
1208 return PIPE_FORMAT_R16_SNORM;
1209 case PIPE_FORMAT_I16_FLOAT:
1210 return PIPE_FORMAT_R16_FLOAT;
1211 case PIPE_FORMAT_I32_FLOAT:
1212 return PIPE_FORMAT_R32_FLOAT;
1213 case PIPE_FORMAT_I8_UINT:
1214 return PIPE_FORMAT_R8_UINT;
1215 case PIPE_FORMAT_I8_SINT:
1216 return PIPE_FORMAT_R8_SINT;
1217 case PIPE_FORMAT_I16_UINT:
1218 return PIPE_FORMAT_R16_UINT;
1219 case PIPE_FORMAT_I16_SINT:
1220 return PIPE_FORMAT_R16_SINT;
1221 case PIPE_FORMAT_I32_UINT:
1222 return PIPE_FORMAT_R32_UINT;
1223 case PIPE_FORMAT_I32_SINT:
1224 return PIPE_FORMAT_R32_SINT;
1225 default:
1226 assert(!util_format_is_intensity(format));
1227 return format;
1228 }
1229 }
1230
1231 /**
1232 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1233 * This is identity for non-luminance formats.
1234 */
1235 static inline enum pipe_format
1236 util_format_luminance_to_red(enum pipe_format format)
1237 {
1238 switch (format) {
1239 case PIPE_FORMAT_L8_UNORM:
1240 return PIPE_FORMAT_R8_UNORM;
1241 case PIPE_FORMAT_L8_SNORM:
1242 return PIPE_FORMAT_R8_SNORM;
1243 case PIPE_FORMAT_L16_UNORM:
1244 return PIPE_FORMAT_R16_UNORM;
1245 case PIPE_FORMAT_L16_SNORM:
1246 return PIPE_FORMAT_R16_SNORM;
1247 case PIPE_FORMAT_L16_FLOAT:
1248 return PIPE_FORMAT_R16_FLOAT;
1249 case PIPE_FORMAT_L32_FLOAT:
1250 return PIPE_FORMAT_R32_FLOAT;
1251 case PIPE_FORMAT_L8_UINT:
1252 return PIPE_FORMAT_R8_UINT;
1253 case PIPE_FORMAT_L8_SINT:
1254 return PIPE_FORMAT_R8_SINT;
1255 case PIPE_FORMAT_L16_UINT:
1256 return PIPE_FORMAT_R16_UINT;
1257 case PIPE_FORMAT_L16_SINT:
1258 return PIPE_FORMAT_R16_SINT;
1259 case PIPE_FORMAT_L32_UINT:
1260 return PIPE_FORMAT_R32_UINT;
1261 case PIPE_FORMAT_L32_SINT:
1262 return PIPE_FORMAT_R32_SINT;
1263
1264 case PIPE_FORMAT_LATC1_UNORM:
1265 return PIPE_FORMAT_RGTC1_UNORM;
1266 case PIPE_FORMAT_LATC1_SNORM:
1267 return PIPE_FORMAT_RGTC1_SNORM;
1268
1269 case PIPE_FORMAT_L4A4_UNORM:
1270 return PIPE_FORMAT_R4A4_UNORM;
1271
1272 case PIPE_FORMAT_L8A8_UNORM:
1273 return PIPE_FORMAT_R8A8_UNORM;
1274 case PIPE_FORMAT_L8A8_SNORM:
1275 return PIPE_FORMAT_R8A8_SNORM;
1276 case PIPE_FORMAT_L16A16_UNORM:
1277 return PIPE_FORMAT_R16A16_UNORM;
1278 case PIPE_FORMAT_L16A16_SNORM:
1279 return PIPE_FORMAT_R16A16_SNORM;
1280 case PIPE_FORMAT_L16A16_FLOAT:
1281 return PIPE_FORMAT_R16A16_FLOAT;
1282 case PIPE_FORMAT_L32A32_FLOAT:
1283 return PIPE_FORMAT_R32A32_FLOAT;
1284 case PIPE_FORMAT_L8A8_UINT:
1285 return PIPE_FORMAT_R8A8_UINT;
1286 case PIPE_FORMAT_L8A8_SINT:
1287 return PIPE_FORMAT_R8A8_SINT;
1288 case PIPE_FORMAT_L16A16_UINT:
1289 return PIPE_FORMAT_R16A16_UINT;
1290 case PIPE_FORMAT_L16A16_SINT:
1291 return PIPE_FORMAT_R16A16_SINT;
1292 case PIPE_FORMAT_L32A32_UINT:
1293 return PIPE_FORMAT_R32A32_UINT;
1294 case PIPE_FORMAT_L32A32_SINT:
1295 return PIPE_FORMAT_R32A32_SINT;
1296
1297 /* We don't have compressed red-alpha variants for these. */
1298 case PIPE_FORMAT_LATC2_UNORM:
1299 case PIPE_FORMAT_LATC2_SNORM:
1300 return PIPE_FORMAT_NONE;
1301
1302 default:
1303 assert(!util_format_is_luminance(format) &&
1304 !util_format_is_luminance_alpha(format));
1305 return format;
1306 }
1307 }
1308
1309 static inline unsigned
1310 util_format_get_num_planes(enum pipe_format format)
1311 {
1312 switch (util_format_description(format)->layout) {
1313 case UTIL_FORMAT_LAYOUT_PLANAR3:
1314 return 3;
1315 case UTIL_FORMAT_LAYOUT_PLANAR2:
1316 return 2;
1317 default:
1318 return 1;
1319 }
1320 }
1321
1322 static inline enum pipe_format
1323 util_format_get_plane_format(enum pipe_format format, unsigned plane)
1324 {
1325 switch (format) {
1326 case PIPE_FORMAT_YV12:
1327 case PIPE_FORMAT_YV16:
1328 case PIPE_FORMAT_IYUV:
1329 case PIPE_FORMAT_Y8_U8_V8_422_UNORM:
1330 case PIPE_FORMAT_Y8_U8_V8_444_UNORM:
1331 return PIPE_FORMAT_R8_UNORM;
1332 case PIPE_FORMAT_NV12:
1333 case PIPE_FORMAT_Y8_U8V8_422_UNORM:
1334 return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_RG88_UNORM;
1335 case PIPE_FORMAT_NV21:
1336 return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_GR88_UNORM;
1337 case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1338 case PIPE_FORMAT_Y16_U16_V16_422_UNORM:
1339 case PIPE_FORMAT_Y16_U16_V16_444_UNORM:
1340 return PIPE_FORMAT_R16_UNORM;
1341 case PIPE_FORMAT_P010:
1342 case PIPE_FORMAT_P016:
1343 case PIPE_FORMAT_Y16_U16V16_422_UNORM:
1344 return !plane ? PIPE_FORMAT_R16_UNORM : PIPE_FORMAT_R16G16_UNORM;
1345 default:
1346 return format;
1347 }
1348 }
1349
1350 static inline unsigned
1351 util_format_get_plane_width(enum pipe_format format, unsigned plane,
1352 unsigned width)
1353 {
1354 switch (format) {
1355 case PIPE_FORMAT_YV12:
1356 case PIPE_FORMAT_YV16:
1357 case PIPE_FORMAT_IYUV:
1358 case PIPE_FORMAT_NV12:
1359 case PIPE_FORMAT_NV21:
1360 case PIPE_FORMAT_P010:
1361 case PIPE_FORMAT_P016:
1362 case PIPE_FORMAT_Y8_U8_V8_422_UNORM:
1363 case PIPE_FORMAT_Y8_U8V8_422_UNORM:
1364 case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1365 case PIPE_FORMAT_Y16_U16_V16_422_UNORM:
1366 case PIPE_FORMAT_Y16_U16V16_422_UNORM:
1367 return !plane ? width : (width + 1) / 2;
1368 default:
1369 return width;
1370 }
1371 }
1372
1373 static inline unsigned
1374 util_format_get_plane_height(enum pipe_format format, unsigned plane,
1375 unsigned height)
1376 {
1377 switch (format) {
1378 case PIPE_FORMAT_YV12:
1379 case PIPE_FORMAT_IYUV:
1380 case PIPE_FORMAT_NV12:
1381 case PIPE_FORMAT_NV21:
1382 case PIPE_FORMAT_P010:
1383 case PIPE_FORMAT_P016:
1384 case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1385 return !plane ? height : (height + 1) / 2;
1386 case PIPE_FORMAT_YV16:
1387 default:
1388 return height;
1389 }
1390 }
1391
1392 /**
1393 * Return the number of components stored.
1394 * Formats with block size != 1x1 will always have 1 component (the block).
1395 */
1396 static inline unsigned
1397 util_format_get_nr_components(enum pipe_format format)
1398 {
1399 const struct util_format_description *desc = util_format_description(format);
1400 return desc->nr_channels;
1401 }
1402
1403 /**
1404 * Return the index of the first non-void channel
1405 * -1 if no non-void channels
1406 */
1407 static inline int
1408 util_format_get_first_non_void_channel(enum pipe_format format)
1409 {
1410 const struct util_format_description *desc = util_format_description(format);
1411 int i;
1412
1413 for (i = 0; i < 4; i++)
1414 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1415 break;
1416
1417 if (i == 4)
1418 return -1;
1419
1420 return i;
1421 }
1422
1423 /**
1424 * Whether this format is any 8-bit UNORM variant. Looser than
1425 * util_is_rgba8_variant (also includes alpha textures, for instance).
1426 */
1427
1428 static inline bool
1429 util_format_is_unorm8(const struct util_format_description *desc)
1430 {
1431 int c = util_format_get_first_non_void_channel(desc->format);
1432
1433 if (c == -1)
1434 return false;
1435
1436 return desc->is_unorm && desc->is_array && desc->channel[c].size == 8;
1437 }
1438
1439 static inline void
1440 util_format_unpack_z_float(enum pipe_format format, float *dst,
1441 const void *src, unsigned w)
1442 {
1443 const struct util_format_unpack_description *desc =
1444 util_format_unpack_description(format);
1445
1446 desc->unpack_z_float(dst, 0, (const uint8_t *)src, 0, w, 1);
1447 }
1448
1449 static inline void
1450 util_format_unpack_z_32unorm(enum pipe_format format, uint32_t *dst,
1451 const void *src, unsigned w)
1452 {
1453 const struct util_format_unpack_description *desc =
1454 util_format_unpack_description(format);
1455
1456 desc->unpack_z_32unorm(dst, 0, (const uint8_t *)src, 0, w, 1);
1457 }
1458
1459 static inline void
1460 util_format_unpack_s_8uint(enum pipe_format format, uint8_t *dst,
1461 const void *src, unsigned w)
1462 {
1463 const struct util_format_unpack_description *desc =
1464 util_format_unpack_description(format);
1465
1466 desc->unpack_s_8uint(dst, 0, (const uint8_t *)src, 0, w, 1);
1467 }
1468
1469 /**
1470 * Unpacks a row of color data to 32-bit RGBA, either integers for pure
1471 * integer formats (sign-extended for signed data), or 32-bit floats.
1472 */
1473 static inline void
1474 util_format_unpack_rgba(enum pipe_format format, void *dst,
1475 const void *src, unsigned w)
1476 {
1477 const struct util_format_unpack_description *desc =
1478 util_format_unpack_description(format);
1479
1480 desc->unpack_rgba(dst, 0, (const uint8_t *)src, 0, w, 1);
1481 }
1482
1483 static inline void
1484 util_format_pack_z_float(enum pipe_format format, void *dst,
1485 const float *src, unsigned w)
1486 {
1487 const struct util_format_pack_description *desc =
1488 util_format_pack_description(format);
1489
1490 desc->pack_z_float((uint8_t *)dst, 0, src, 0, w, 1);
1491 }
1492
1493 static inline void
1494 util_format_pack_z_32unorm(enum pipe_format format, void *dst,
1495 const uint32_t *src, unsigned w)
1496 {
1497 const struct util_format_pack_description *desc =
1498 util_format_pack_description(format);
1499
1500 desc->pack_z_32unorm((uint8_t *)dst, 0, src, 0, w, 1);
1501 }
1502
1503 static inline void
1504 util_format_pack_s_8uint(enum pipe_format format, void *dst,
1505 const uint8_t *src, unsigned w)
1506 {
1507 const struct util_format_pack_description *desc =
1508 util_format_pack_description(format);
1509
1510 desc->pack_s_8uint((uint8_t *)dst, 0, src, 0, w, 1);
1511 }
1512
1513 /**
1514 * Packs a row of color data from 32-bit RGBA, either integers for pure
1515 * integer formats, or 32-bit floats. Values are clamped to the packed
1516 * representation's range.
1517 */
1518 static inline void
1519 util_format_pack_rgba(enum pipe_format format, void *dst,
1520 const void *src, unsigned w)
1521 {
1522 const struct util_format_pack_description *desc =
1523 util_format_pack_description(format);
1524
1525 if (util_format_is_pure_uint(format))
1526 desc->pack_rgba_uint((uint8_t *)dst, 0, (const uint32_t *)src, 0, w, 1);
1527 else if (util_format_is_pure_sint(format))
1528 desc->pack_rgba_sint((uint8_t *)dst, 0, (const int32_t *)src, 0, w, 1);
1529 else
1530 desc->pack_rgba_float((uint8_t *)dst, 0, (const float *)src, 0, w, 1);
1531 }
1532
1533 /*
1534 * Format access functions for subrectangles
1535 */
1536
1537 void
1538 util_format_read_4(enum pipe_format format,
1539 void *dst, unsigned dst_stride,
1540 const void *src, unsigned src_stride,
1541 unsigned x, unsigned y, unsigned w, unsigned h);
1542
1543 void
1544 util_format_write_4(enum pipe_format format,
1545 const void *src, unsigned src_stride,
1546 void *dst, unsigned dst_stride,
1547 unsigned x, unsigned y, unsigned w, unsigned h);
1548
1549 void
1550 util_format_read_4ub(enum pipe_format format,
1551 uint8_t *dst, unsigned dst_stride,
1552 const void *src, unsigned src_stride,
1553 unsigned x, unsigned y, unsigned w, unsigned h);
1554
1555 void
1556 util_format_write_4ub(enum pipe_format format,
1557 const uint8_t *src, unsigned src_stride,
1558 void *dst, unsigned dst_stride,
1559 unsigned x, unsigned y, unsigned w, unsigned h);
1560
1561 /*
1562 * Generic format conversion;
1563 */
1564
1565 boolean
1566 util_format_fits_8unorm(const struct util_format_description *format_desc);
1567
1568 boolean
1569 util_format_translate(enum pipe_format dst_format,
1570 void *dst, unsigned dst_stride,
1571 unsigned dst_x, unsigned dst_y,
1572 enum pipe_format src_format,
1573 const void *src, unsigned src_stride,
1574 unsigned src_x, unsigned src_y,
1575 unsigned width, unsigned height);
1576
1577 boolean
1578 util_format_translate_3d(enum pipe_format dst_format,
1579 void *dst, unsigned dst_stride,
1580 unsigned dst_slice_stride,
1581 unsigned dst_x, unsigned dst_y,
1582 unsigned dst_z,
1583 enum pipe_format src_format,
1584 const void *src, unsigned src_stride,
1585 unsigned src_slice_stride,
1586 unsigned src_x, unsigned src_y,
1587 unsigned src_z, unsigned width,
1588 unsigned height, unsigned depth);
1589
1590 /*
1591 * Swizzle operations.
1592 */
1593
1594 /* Compose two sets of swizzles.
1595 * If V is a 4D vector and the function parameters represent functions that
1596 * swizzle vector components, this holds:
1597 * swz2(swz1(V)) = dst(V)
1598 */
1599 void util_format_compose_swizzles(const unsigned char swz1[4],
1600 const unsigned char swz2[4],
1601 unsigned char dst[4]);
1602
1603 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1604 * to \param src and store the result in \param dst.
1605 * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1606 */
1607 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1608 const union pipe_color_union *src,
1609 const unsigned char swz[4],
1610 const boolean is_integer);
1611
1612 void pipe_swizzle_4f(float *dst, const float *src,
1613 const unsigned char swz[4]);
1614
1615 void util_format_unswizzle_4f(float *dst, const float *src,
1616 const unsigned char swz[4]);
1617
1618 enum pipe_format
1619 util_format_snorm8_to_sint8(enum pipe_format format);
1620
1621
1622 extern void
1623 util_copy_rect(ubyte * dst, enum pipe_format format,
1624 unsigned dst_stride, unsigned dst_x, unsigned dst_y,
1625 unsigned width, unsigned height, const ubyte * src,
1626 int src_stride, unsigned src_x, unsigned src_y);
1627
1628 #ifdef __cplusplus
1629 } // extern "C" {
1630 #endif
1631
1632 #endif /* ! U_FORMAT_H */