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