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