Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / auxiliary / util / u_format.h
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 Vmware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
31
32
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
36
37 union pipe_color_union;
38
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /**
46 * Describe how to pack/unpack pixels into/from the prescribed format.
47 *
48 * XXX: This could be renamed to something like util_format_pack, or broke down
49 * in flags inside util_format_block that said exactly what we want.
50 */
51 enum util_format_layout {
52 /**
53 * Formats with util_format_block::width == util_format_block::height == 1
54 * that can be described as an ordinary data structure.
55 */
56 UTIL_FORMAT_LAYOUT_PLAIN = 0,
57
58 /**
59 * Formats with sub-sampled channels.
60 *
61 * This is for formats like YVYU where there is less than one sample per
62 * pixel.
63 */
64 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
65
66 /**
67 * S3 Texture Compression formats.
68 */
69 UTIL_FORMAT_LAYOUT_S3TC = 4,
70
71 /**
72 * Red-Green Texture Compression formats.
73 */
74 UTIL_FORMAT_LAYOUT_RGTC = 5,
75
76 /**
77 * Ericsson Texture Compression
78 */
79 UTIL_FORMAT_LAYOUT_ETC = 6,
80
81 /**
82 * BC6/7 Texture Compression
83 */
84 UTIL_FORMAT_LAYOUT_BPTC = 7,
85
86 /**
87 * Everything else that doesn't fit in any of the above layouts.
88 */
89 UTIL_FORMAT_LAYOUT_OTHER = 8
90 };
91
92
93 struct util_format_block
94 {
95 /** Block width in pixels */
96 unsigned width;
97
98 /** Block height in pixels */
99 unsigned height;
100
101 /** Block size in bits */
102 unsigned bits;
103 };
104
105
106 enum util_format_type {
107 UTIL_FORMAT_TYPE_VOID = 0,
108 UTIL_FORMAT_TYPE_UNSIGNED = 1,
109 UTIL_FORMAT_TYPE_SIGNED = 2,
110 UTIL_FORMAT_TYPE_FIXED = 3,
111 UTIL_FORMAT_TYPE_FLOAT = 4
112 };
113
114
115 enum util_format_swizzle {
116 UTIL_FORMAT_SWIZZLE_X = 0,
117 UTIL_FORMAT_SWIZZLE_Y = 1,
118 UTIL_FORMAT_SWIZZLE_Z = 2,
119 UTIL_FORMAT_SWIZZLE_W = 3,
120 UTIL_FORMAT_SWIZZLE_0 = 4,
121 UTIL_FORMAT_SWIZZLE_1 = 5,
122 UTIL_FORMAT_SWIZZLE_NONE = 6,
123 UTIL_FORMAT_SWIZZLE_MAX = 7 /**< Number of enums counter (must be last) */
124 };
125
126
127 enum util_format_colorspace {
128 UTIL_FORMAT_COLORSPACE_RGB = 0,
129 UTIL_FORMAT_COLORSPACE_SRGB = 1,
130 UTIL_FORMAT_COLORSPACE_YUV = 2,
131 UTIL_FORMAT_COLORSPACE_ZS = 3
132 };
133
134
135 struct util_format_channel_description
136 {
137 unsigned type:5; /**< UTIL_FORMAT_TYPE_x */
138 unsigned normalized:1;
139 unsigned pure_integer:1;
140 unsigned size:9; /**< bits per channel */
141 unsigned shift:16; /** number of bits from lsb */
142 };
143
144
145 struct util_format_description
146 {
147 enum pipe_format format;
148
149 const char *name;
150
151 /**
152 * Short name, striped of the prefix, lower case.
153 */
154 const char *short_name;
155
156 /**
157 * Pixel block dimensions.
158 */
159 struct util_format_block block;
160
161 enum util_format_layout layout;
162
163 /**
164 * The number of channels.
165 */
166 unsigned nr_channels:3;
167
168 /**
169 * Whether all channels have the same number of (whole) bytes and type.
170 */
171 unsigned is_array:1;
172
173 /**
174 * Whether the pixel format can be described as a bitfield structure.
175 *
176 * In particular:
177 * - pixel depth must be 8, 16, or 32 bits;
178 * - all channels must be unsigned, signed, or void
179 */
180 unsigned is_bitmask:1;
181
182 /**
183 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
184 */
185 unsigned is_mixed:1;
186
187 /**
188 * Input channel description, in the order XYZW.
189 *
190 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
191 *
192 * If each channel is accessed as an individual N-byte value, X is always
193 * at the lowest address in memory, Y is always next, and so on. For all
194 * currently-defined formats, the N-byte value has native endianness.
195 *
196 * If instead a group of channels is accessed as a single N-byte value,
197 * the order of the channels within that value depends on endianness.
198 * For big-endian targets, X is the most significant subvalue,
199 * otherwise it is the least significant one.
200 *
201 * For example, if X is 8 bits and Y is 24 bits, the memory order is:
202 *
203 * 0 1 2 3
204 * little-endian: X Yl Ym Yu (l = lower, m = middle, u = upper)
205 * big-endian: X Yu Ym Yl
206 *
207 * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
208 *
209 * 0 1
210 * msb lsb msb lsb
211 * little-endian: YYYXXXXX WZZZZZYY
212 * big-endian: XXXXXYYY YYZZZZZW
213 */
214 struct util_format_channel_description channel[4];
215
216 /**
217 * Output channel swizzle.
218 *
219 * The order is either:
220 * - RGBA
221 * - YUV(A)
222 * - ZS
223 * depending on the colorspace.
224 */
225 unsigned char swizzle[4];
226
227 /**
228 * Colorspace transformation.
229 */
230 enum util_format_colorspace colorspace;
231
232 /**
233 * Unpack pixel blocks to R8G8B8A8_UNORM.
234 * Note: strides are in bytes.
235 *
236 * Only defined for non-depth-stencil formats.
237 */
238 void
239 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
240 const uint8_t *src, unsigned src_stride,
241 unsigned width, unsigned height);
242
243 /**
244 * Pack pixel blocks from R8G8B8A8_UNORM.
245 * Note: strides are in bytes.
246 *
247 * Only defined for non-depth-stencil formats.
248 */
249 void
250 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
251 const uint8_t *src, unsigned src_stride,
252 unsigned width, unsigned height);
253
254 /**
255 * Fetch a single pixel (i, j) from a block.
256 *
257 * XXX: Only defined for a very few select formats.
258 */
259 void
260 (*fetch_rgba_8unorm)(uint8_t *dst,
261 const uint8_t *src,
262 unsigned i, unsigned j);
263
264 /**
265 * Unpack pixel blocks to R32G32B32A32_FLOAT.
266 * Note: strides are in bytes.
267 *
268 * Only defined for non-depth-stencil formats.
269 */
270 void
271 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
272 const uint8_t *src, unsigned src_stride,
273 unsigned width, unsigned height);
274
275 /**
276 * Pack pixel blocks from R32G32B32A32_FLOAT.
277 * Note: strides are in bytes.
278 *
279 * Only defined for non-depth-stencil formats.
280 */
281 void
282 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
283 const float *src, unsigned src_stride,
284 unsigned width, unsigned height);
285
286 /**
287 * Fetch a single pixel (i, j) from a block.
288 *
289 * Only defined for non-depth-stencil and non-integer formats.
290 */
291 void
292 (*fetch_rgba_float)(float *dst,
293 const uint8_t *src,
294 unsigned i, unsigned j);
295
296 /**
297 * Unpack pixels to Z32_UNORM.
298 * Note: strides are in bytes.
299 *
300 * Only defined for depth formats.
301 */
302 void
303 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
304 const uint8_t *src, unsigned src_stride,
305 unsigned width, unsigned height);
306
307 /**
308 * Pack pixels from Z32_FLOAT.
309 * Note: strides are in bytes.
310 *
311 * Only defined for depth formats.
312 */
313 void
314 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
315 const uint32_t *src, unsigned src_stride,
316 unsigned width, unsigned height);
317
318 /**
319 * Unpack pixels to Z32_FLOAT.
320 * Note: strides are in bytes.
321 *
322 * Only defined for depth formats.
323 */
324 void
325 (*unpack_z_float)(float *dst, unsigned dst_stride,
326 const uint8_t *src, unsigned src_stride,
327 unsigned width, unsigned height);
328
329 /**
330 * Pack pixels from Z32_FLOAT.
331 * Note: strides are in bytes.
332 *
333 * Only defined for depth formats.
334 */
335 void
336 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
337 const float *src, unsigned src_stride,
338 unsigned width, unsigned height);
339
340 /**
341 * Unpack pixels to S8_UINT.
342 * Note: strides are in bytes.
343 *
344 * Only defined for stencil formats.
345 */
346 void
347 (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
348 const uint8_t *src, unsigned src_stride,
349 unsigned width, unsigned height);
350
351 /**
352 * Pack pixels from S8_UINT.
353 * Note: strides are in bytes.
354 *
355 * Only defined for stencil formats.
356 */
357 void
358 (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
359 const uint8_t *src, unsigned src_stride,
360 unsigned width, unsigned height);
361
362 /**
363 * Unpack pixel blocks to R32G32B32A32_UINT.
364 * Note: strides are in bytes.
365 *
366 * Only defined for INT formats.
367 */
368 void
369 (*unpack_rgba_uint)(uint32_t *dst, unsigned dst_stride,
370 const uint8_t *src, unsigned src_stride,
371 unsigned width, unsigned height);
372
373 void
374 (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
375 const uint32_t *src, unsigned src_stride,
376 unsigned width, unsigned height);
377
378 /**
379 * Unpack pixel blocks to R32G32B32A32_SINT.
380 * Note: strides are in bytes.
381 *
382 * Only defined for INT formats.
383 */
384 void
385 (*unpack_rgba_sint)(int32_t *dst, unsigned dst_stride,
386 const uint8_t *src, unsigned src_stride,
387 unsigned width, unsigned height);
388
389 void
390 (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
391 const int32_t *src, unsigned src_stride,
392 unsigned width, unsigned height);
393
394 /**
395 * Fetch a single pixel (i, j) from a block.
396 *
397 * Only defined for unsigned (pure) integer formats.
398 */
399 void
400 (*fetch_rgba_uint)(uint32_t *dst,
401 const uint8_t *src,
402 unsigned i, unsigned j);
403
404 /**
405 * Fetch a single pixel (i, j) from a block.
406 *
407 * Only defined for signed (pure) integer formats.
408 */
409 void
410 (*fetch_rgba_sint)(int32_t *dst,
411 const uint8_t *src,
412 unsigned i, unsigned j);
413 };
414
415
416 extern const struct util_format_description
417 util_format_description_table[];
418
419
420 const struct util_format_description *
421 util_format_description(enum pipe_format format);
422
423
424 /*
425 * Format query functions.
426 */
427
428 static inline const char *
429 util_format_name(enum pipe_format format)
430 {
431 const struct util_format_description *desc = util_format_description(format);
432
433 assert(desc);
434 if (!desc) {
435 return "PIPE_FORMAT_???";
436 }
437
438 return desc->name;
439 }
440
441 static inline const char *
442 util_format_short_name(enum pipe_format format)
443 {
444 const struct util_format_description *desc = util_format_description(format);
445
446 assert(desc);
447 if (!desc) {
448 return "???";
449 }
450
451 return desc->short_name;
452 }
453
454 /**
455 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
456 */
457 static inline boolean
458 util_format_is_plain(enum pipe_format format)
459 {
460 const struct util_format_description *desc = util_format_description(format);
461
462 if (!format) {
463 return FALSE;
464 }
465
466 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
467 }
468
469 static inline boolean
470 util_format_is_compressed(enum pipe_format format)
471 {
472 const struct util_format_description *desc = util_format_description(format);
473
474 assert(desc);
475 if (!desc) {
476 return FALSE;
477 }
478
479 switch (desc->layout) {
480 case UTIL_FORMAT_LAYOUT_S3TC:
481 case UTIL_FORMAT_LAYOUT_RGTC:
482 case UTIL_FORMAT_LAYOUT_ETC:
483 case UTIL_FORMAT_LAYOUT_BPTC:
484 /* XXX add other formats in the future */
485 return TRUE;
486 default:
487 return FALSE;
488 }
489 }
490
491 static inline boolean
492 util_format_is_s3tc(enum pipe_format format)
493 {
494 const struct util_format_description *desc = util_format_description(format);
495
496 assert(desc);
497 if (!desc) {
498 return FALSE;
499 }
500
501 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
502 }
503
504 static inline boolean
505 util_format_is_srgb(enum pipe_format format)
506 {
507 const struct util_format_description *desc = util_format_description(format);
508 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
509 }
510
511 static inline boolean
512 util_format_has_depth(const struct util_format_description *desc)
513 {
514 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
515 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
516 }
517
518 static inline boolean
519 util_format_has_stencil(const struct util_format_description *desc)
520 {
521 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
522 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
523 }
524
525 static inline boolean
526 util_format_is_depth_or_stencil(enum pipe_format format)
527 {
528 const struct util_format_description *desc = util_format_description(format);
529
530 assert(desc);
531 if (!desc) {
532 return FALSE;
533 }
534
535 return util_format_has_depth(desc) ||
536 util_format_has_stencil(desc);
537 }
538
539 static inline boolean
540 util_format_is_depth_and_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
554 /**
555 * Calculates the depth format type based upon the incoming format description.
556 */
557 static inline unsigned
558 util_get_depth_format_type(const struct util_format_description *desc)
559 {
560 unsigned depth_channel = desc->swizzle[0];
561 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
562 depth_channel != UTIL_FORMAT_SWIZZLE_NONE) {
563 return desc->channel[depth_channel].type;
564 } else {
565 return UTIL_FORMAT_TYPE_VOID;
566 }
567 }
568
569
570 /**
571 * Calculates the MRD for the depth format. MRD is used in depth bias
572 * for UNORM and unbound depth buffers. When the depth buffer is floating
573 * point, the depth bias calculation does not use the MRD. However, the
574 * default MRD will be 1.0 / ((1 << 24) - 1).
575 */
576 double
577 util_get_depth_format_mrd(const struct util_format_description *desc);
578
579
580 /**
581 * Return whether this is an RGBA, Z, S, or combined ZS format.
582 * Useful for initializing pipe_blit_info::mask.
583 */
584 static inline unsigned
585 util_format_get_mask(enum pipe_format format)
586 {
587 const struct util_format_description *desc =
588 util_format_description(format);
589
590 if (!desc)
591 return 0;
592
593 if (util_format_has_depth(desc)) {
594 if (util_format_has_stencil(desc)) {
595 return PIPE_MASK_ZS;
596 } else {
597 return PIPE_MASK_Z;
598 }
599 } else {
600 if (util_format_has_stencil(desc)) {
601 return PIPE_MASK_S;
602 } else {
603 return PIPE_MASK_RGBA;
604 }
605 }
606 }
607
608 /**
609 * Give the RGBA colormask of the channels that can be represented in this
610 * format.
611 *
612 * That is, the channels whose values are preserved.
613 */
614 static inline unsigned
615 util_format_colormask(const struct util_format_description *desc)
616 {
617 unsigned colormask;
618 unsigned chan;
619
620 switch (desc->colorspace) {
621 case UTIL_FORMAT_COLORSPACE_RGB:
622 case UTIL_FORMAT_COLORSPACE_SRGB:
623 case UTIL_FORMAT_COLORSPACE_YUV:
624 colormask = 0;
625 for (chan = 0; chan < 4; ++chan) {
626 if (desc->swizzle[chan] < 4) {
627 colormask |= (1 << chan);
628 }
629 }
630 return colormask;
631 case UTIL_FORMAT_COLORSPACE_ZS:
632 return 0;
633 default:
634 assert(0);
635 return 0;
636 }
637 }
638
639
640 /**
641 * Checks if color mask covers every channel for the specified format
642 *
643 * @param desc a format description to check colormask with
644 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
645 */
646 static inline boolean
647 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
648 {
649 return (~colormask & util_format_colormask(desc)) == 0;
650 }
651
652
653 boolean
654 util_format_is_float(enum pipe_format format);
655
656
657 boolean
658 util_format_has_alpha(enum pipe_format format);
659
660
661 boolean
662 util_format_is_luminance(enum pipe_format format);
663
664 boolean
665 util_format_is_alpha(enum pipe_format format);
666
667 boolean
668 util_format_is_luminance_alpha(enum pipe_format format);
669
670
671 boolean
672 util_format_is_intensity(enum pipe_format format);
673
674 boolean
675 util_format_is_subsampled_422(enum pipe_format format);
676
677 boolean
678 util_format_is_pure_integer(enum pipe_format format);
679
680 boolean
681 util_format_is_pure_sint(enum pipe_format format);
682
683 boolean
684 util_format_is_pure_uint(enum pipe_format format);
685
686 boolean
687 util_format_is_snorm(enum pipe_format format);
688
689 boolean
690 util_format_is_snorm8(enum pipe_format format);
691
692 /**
693 * Check if the src format can be blitted to the destination format with
694 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
695 * the reverse.
696 */
697 boolean
698 util_is_format_compatible(const struct util_format_description *src_desc,
699 const struct util_format_description *dst_desc);
700
701 /**
702 * Whether the format is supported by Gallium for the given bindings.
703 * This covers S3TC textures and floating-point render targets.
704 */
705 boolean
706 util_format_is_supported(enum pipe_format format, unsigned bind);
707
708 /**
709 * Whether this format is a rgab8 variant.
710 *
711 * That is, any format that matches the
712 *
713 * PIPE_FORMAT_?8?8?8?8_UNORM
714 */
715 static inline boolean
716 util_format_is_rgba8_variant(const struct util_format_description *desc)
717 {
718 unsigned chan;
719
720 if(desc->block.width != 1 ||
721 desc->block.height != 1 ||
722 desc->block.bits != 32)
723 return FALSE;
724
725 for(chan = 0; chan < 4; ++chan) {
726 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
727 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
728 return FALSE;
729 if(desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED &&
730 !desc->channel[chan].normalized)
731 return FALSE;
732 if(desc->channel[chan].size != 8)
733 return FALSE;
734 }
735
736 return TRUE;
737 }
738
739
740 /**
741 * Return total bits needed for the pixel format per block.
742 */
743 static inline uint
744 util_format_get_blocksizebits(enum pipe_format format)
745 {
746 const struct util_format_description *desc = util_format_description(format);
747
748 assert(desc);
749 if (!desc) {
750 return 0;
751 }
752
753 return desc->block.bits;
754 }
755
756 /**
757 * Return bytes per block (not pixel) for the given format.
758 */
759 static inline uint
760 util_format_get_blocksize(enum pipe_format format)
761 {
762 uint bits = util_format_get_blocksizebits(format);
763 uint bytes = bits / 8;
764
765 assert(bits % 8 == 0);
766 assert(bytes > 0);
767 if (bytes == 0) {
768 bytes = 1;
769 }
770
771 return bytes;
772 }
773
774 static inline uint
775 util_format_get_blockwidth(enum pipe_format format)
776 {
777 const struct util_format_description *desc = util_format_description(format);
778
779 assert(desc);
780 if (!desc) {
781 return 1;
782 }
783
784 return desc->block.width;
785 }
786
787 static inline uint
788 util_format_get_blockheight(enum pipe_format format)
789 {
790 const struct util_format_description *desc = util_format_description(format);
791
792 assert(desc);
793 if (!desc) {
794 return 1;
795 }
796
797 return desc->block.height;
798 }
799
800 static inline unsigned
801 util_format_get_nblocksx(enum pipe_format format,
802 unsigned x)
803 {
804 unsigned blockwidth = util_format_get_blockwidth(format);
805 return (x + blockwidth - 1) / blockwidth;
806 }
807
808 static inline unsigned
809 util_format_get_nblocksy(enum pipe_format format,
810 unsigned y)
811 {
812 unsigned blockheight = util_format_get_blockheight(format);
813 return (y + blockheight - 1) / blockheight;
814 }
815
816 static inline unsigned
817 util_format_get_nblocks(enum pipe_format format,
818 unsigned width,
819 unsigned height)
820 {
821 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
822 }
823
824 static inline size_t
825 util_format_get_stride(enum pipe_format format,
826 unsigned width)
827 {
828 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
829 }
830
831 static inline size_t
832 util_format_get_2d_size(enum pipe_format format,
833 size_t stride,
834 unsigned height)
835 {
836 return util_format_get_nblocksy(format, height) * stride;
837 }
838
839 static inline uint
840 util_format_get_component_bits(enum pipe_format format,
841 enum util_format_colorspace colorspace,
842 uint component)
843 {
844 const struct util_format_description *desc = util_format_description(format);
845 enum util_format_colorspace desc_colorspace;
846
847 assert(format);
848 if (!format) {
849 return 0;
850 }
851
852 assert(component < 4);
853
854 /* Treat RGB and SRGB as equivalent. */
855 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
856 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
857 }
858 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
859 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
860 } else {
861 desc_colorspace = desc->colorspace;
862 }
863
864 if (desc_colorspace != colorspace) {
865 return 0;
866 }
867
868 switch (desc->swizzle[component]) {
869 case UTIL_FORMAT_SWIZZLE_X:
870 return desc->channel[0].size;
871 case UTIL_FORMAT_SWIZZLE_Y:
872 return desc->channel[1].size;
873 case UTIL_FORMAT_SWIZZLE_Z:
874 return desc->channel[2].size;
875 case UTIL_FORMAT_SWIZZLE_W:
876 return desc->channel[3].size;
877 default:
878 return 0;
879 }
880 }
881
882 /**
883 * Given a linear RGB colorspace format, return the corresponding SRGB
884 * format, or PIPE_FORMAT_NONE if none.
885 */
886 static inline enum pipe_format
887 util_format_srgb(enum pipe_format format)
888 {
889 if (util_format_is_srgb(format))
890 return format;
891
892 switch (format) {
893 case PIPE_FORMAT_L8_UNORM:
894 return PIPE_FORMAT_L8_SRGB;
895 case PIPE_FORMAT_L8A8_UNORM:
896 return PIPE_FORMAT_L8A8_SRGB;
897 case PIPE_FORMAT_R8G8B8_UNORM:
898 return PIPE_FORMAT_R8G8B8_SRGB;
899 case PIPE_FORMAT_A8B8G8R8_UNORM:
900 return PIPE_FORMAT_A8B8G8R8_SRGB;
901 case PIPE_FORMAT_X8B8G8R8_UNORM:
902 return PIPE_FORMAT_X8B8G8R8_SRGB;
903 case PIPE_FORMAT_B8G8R8A8_UNORM:
904 return PIPE_FORMAT_B8G8R8A8_SRGB;
905 case PIPE_FORMAT_B8G8R8X8_UNORM:
906 return PIPE_FORMAT_B8G8R8X8_SRGB;
907 case PIPE_FORMAT_A8R8G8B8_UNORM:
908 return PIPE_FORMAT_A8R8G8B8_SRGB;
909 case PIPE_FORMAT_X8R8G8B8_UNORM:
910 return PIPE_FORMAT_X8R8G8B8_SRGB;
911 case PIPE_FORMAT_R8G8B8A8_UNORM:
912 return PIPE_FORMAT_R8G8B8A8_SRGB;
913 case PIPE_FORMAT_R8G8B8X8_UNORM:
914 return PIPE_FORMAT_R8G8B8X8_SRGB;
915 case PIPE_FORMAT_DXT1_RGB:
916 return PIPE_FORMAT_DXT1_SRGB;
917 case PIPE_FORMAT_DXT1_RGBA:
918 return PIPE_FORMAT_DXT1_SRGBA;
919 case PIPE_FORMAT_DXT3_RGBA:
920 return PIPE_FORMAT_DXT3_SRGBA;
921 case PIPE_FORMAT_DXT5_RGBA:
922 return PIPE_FORMAT_DXT5_SRGBA;
923 case PIPE_FORMAT_B5G6R5_UNORM:
924 return PIPE_FORMAT_B5G6R5_SRGB;
925 case PIPE_FORMAT_BPTC_RGBA_UNORM:
926 return PIPE_FORMAT_BPTC_SRGBA;
927 default:
928 return PIPE_FORMAT_NONE;
929 }
930 }
931
932 /**
933 * Given an sRGB format, return the corresponding linear colorspace format.
934 * For non sRGB formats, return the format unchanged.
935 */
936 static inline enum pipe_format
937 util_format_linear(enum pipe_format format)
938 {
939 switch (format) {
940 case PIPE_FORMAT_L8_SRGB:
941 return PIPE_FORMAT_L8_UNORM;
942 case PIPE_FORMAT_L8A8_SRGB:
943 return PIPE_FORMAT_L8A8_UNORM;
944 case PIPE_FORMAT_R8G8B8_SRGB:
945 return PIPE_FORMAT_R8G8B8_UNORM;
946 case PIPE_FORMAT_A8B8G8R8_SRGB:
947 return PIPE_FORMAT_A8B8G8R8_UNORM;
948 case PIPE_FORMAT_X8B8G8R8_SRGB:
949 return PIPE_FORMAT_X8B8G8R8_UNORM;
950 case PIPE_FORMAT_B8G8R8A8_SRGB:
951 return PIPE_FORMAT_B8G8R8A8_UNORM;
952 case PIPE_FORMAT_B8G8R8X8_SRGB:
953 return PIPE_FORMAT_B8G8R8X8_UNORM;
954 case PIPE_FORMAT_A8R8G8B8_SRGB:
955 return PIPE_FORMAT_A8R8G8B8_UNORM;
956 case PIPE_FORMAT_X8R8G8B8_SRGB:
957 return PIPE_FORMAT_X8R8G8B8_UNORM;
958 case PIPE_FORMAT_R8G8B8A8_SRGB:
959 return PIPE_FORMAT_R8G8B8A8_UNORM;
960 case PIPE_FORMAT_R8G8B8X8_SRGB:
961 return PIPE_FORMAT_R8G8B8X8_UNORM;
962 case PIPE_FORMAT_DXT1_SRGB:
963 return PIPE_FORMAT_DXT1_RGB;
964 case PIPE_FORMAT_DXT1_SRGBA:
965 return PIPE_FORMAT_DXT1_RGBA;
966 case PIPE_FORMAT_DXT3_SRGBA:
967 return PIPE_FORMAT_DXT3_RGBA;
968 case PIPE_FORMAT_DXT5_SRGBA:
969 return PIPE_FORMAT_DXT5_RGBA;
970 case PIPE_FORMAT_B5G6R5_SRGB:
971 return PIPE_FORMAT_B5G6R5_UNORM;
972 case PIPE_FORMAT_BPTC_SRGBA:
973 return PIPE_FORMAT_BPTC_RGBA_UNORM;
974 default:
975 return format;
976 }
977 }
978
979 /**
980 * Given a depth-stencil format, return the corresponding stencil-only format.
981 * For stencil-only formats, return the format unchanged.
982 */
983 static inline enum pipe_format
984 util_format_stencil_only(enum pipe_format format)
985 {
986 switch (format) {
987 /* mask out the depth component */
988 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
989 return PIPE_FORMAT_X24S8_UINT;
990 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
991 return PIPE_FORMAT_S8X24_UINT;
992 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
993 return PIPE_FORMAT_X32_S8X24_UINT;
994
995 /* stencil only formats */
996 case PIPE_FORMAT_X24S8_UINT:
997 case PIPE_FORMAT_S8X24_UINT:
998 case PIPE_FORMAT_X32_S8X24_UINT:
999 case PIPE_FORMAT_S8_UINT:
1000 return format;
1001
1002 default:
1003 assert(0);
1004 return PIPE_FORMAT_NONE;
1005 }
1006 }
1007
1008 /**
1009 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1010 * This is identity for non-intensity formats.
1011 */
1012 static inline enum pipe_format
1013 util_format_intensity_to_red(enum pipe_format format)
1014 {
1015 switch (format) {
1016 case PIPE_FORMAT_I8_UNORM:
1017 return PIPE_FORMAT_R8_UNORM;
1018 case PIPE_FORMAT_I8_SNORM:
1019 return PIPE_FORMAT_R8_SNORM;
1020 case PIPE_FORMAT_I16_UNORM:
1021 return PIPE_FORMAT_R16_UNORM;
1022 case PIPE_FORMAT_I16_SNORM:
1023 return PIPE_FORMAT_R16_SNORM;
1024 case PIPE_FORMAT_I16_FLOAT:
1025 return PIPE_FORMAT_R16_FLOAT;
1026 case PIPE_FORMAT_I32_FLOAT:
1027 return PIPE_FORMAT_R32_FLOAT;
1028 case PIPE_FORMAT_I8_UINT:
1029 return PIPE_FORMAT_R8_UINT;
1030 case PIPE_FORMAT_I8_SINT:
1031 return PIPE_FORMAT_R8_SINT;
1032 case PIPE_FORMAT_I16_UINT:
1033 return PIPE_FORMAT_R16_UINT;
1034 case PIPE_FORMAT_I16_SINT:
1035 return PIPE_FORMAT_R16_SINT;
1036 case PIPE_FORMAT_I32_UINT:
1037 return PIPE_FORMAT_R32_UINT;
1038 case PIPE_FORMAT_I32_SINT:
1039 return PIPE_FORMAT_R32_SINT;
1040 default:
1041 assert(!util_format_is_intensity(format));
1042 return format;
1043 }
1044 }
1045
1046 /**
1047 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1048 * This is identity for non-luminance formats.
1049 */
1050 static inline enum pipe_format
1051 util_format_luminance_to_red(enum pipe_format format)
1052 {
1053 switch (format) {
1054 case PIPE_FORMAT_L8_UNORM:
1055 return PIPE_FORMAT_R8_UNORM;
1056 case PIPE_FORMAT_L8_SNORM:
1057 return PIPE_FORMAT_R8_SNORM;
1058 case PIPE_FORMAT_L16_UNORM:
1059 return PIPE_FORMAT_R16_UNORM;
1060 case PIPE_FORMAT_L16_SNORM:
1061 return PIPE_FORMAT_R16_SNORM;
1062 case PIPE_FORMAT_L16_FLOAT:
1063 return PIPE_FORMAT_R16_FLOAT;
1064 case PIPE_FORMAT_L32_FLOAT:
1065 return PIPE_FORMAT_R32_FLOAT;
1066 case PIPE_FORMAT_L8_UINT:
1067 return PIPE_FORMAT_R8_UINT;
1068 case PIPE_FORMAT_L8_SINT:
1069 return PIPE_FORMAT_R8_SINT;
1070 case PIPE_FORMAT_L16_UINT:
1071 return PIPE_FORMAT_R16_UINT;
1072 case PIPE_FORMAT_L16_SINT:
1073 return PIPE_FORMAT_R16_SINT;
1074 case PIPE_FORMAT_L32_UINT:
1075 return PIPE_FORMAT_R32_UINT;
1076 case PIPE_FORMAT_L32_SINT:
1077 return PIPE_FORMAT_R32_SINT;
1078
1079 case PIPE_FORMAT_LATC1_UNORM:
1080 return PIPE_FORMAT_RGTC1_UNORM;
1081 case PIPE_FORMAT_LATC1_SNORM:
1082 return PIPE_FORMAT_RGTC1_SNORM;
1083
1084 case PIPE_FORMAT_L4A4_UNORM:
1085 return PIPE_FORMAT_R4A4_UNORM;
1086
1087 case PIPE_FORMAT_L8A8_UNORM:
1088 return PIPE_FORMAT_R8A8_UNORM;
1089 case PIPE_FORMAT_L8A8_SNORM:
1090 return PIPE_FORMAT_R8A8_SNORM;
1091 case PIPE_FORMAT_L16A16_UNORM:
1092 return PIPE_FORMAT_R16A16_UNORM;
1093 case PIPE_FORMAT_L16A16_SNORM:
1094 return PIPE_FORMAT_R16A16_SNORM;
1095 case PIPE_FORMAT_L16A16_FLOAT:
1096 return PIPE_FORMAT_R16A16_FLOAT;
1097 case PIPE_FORMAT_L32A32_FLOAT:
1098 return PIPE_FORMAT_R32A32_FLOAT;
1099 case PIPE_FORMAT_L8A8_UINT:
1100 return PIPE_FORMAT_R8A8_UINT;
1101 case PIPE_FORMAT_L8A8_SINT:
1102 return PIPE_FORMAT_R8A8_SINT;
1103 case PIPE_FORMAT_L16A16_UINT:
1104 return PIPE_FORMAT_R16A16_UINT;
1105 case PIPE_FORMAT_L16A16_SINT:
1106 return PIPE_FORMAT_R16A16_SINT;
1107 case PIPE_FORMAT_L32A32_UINT:
1108 return PIPE_FORMAT_R32A32_UINT;
1109 case PIPE_FORMAT_L32A32_SINT:
1110 return PIPE_FORMAT_R32A32_SINT;
1111
1112 /* We don't have compressed red-alpha variants for these. */
1113 case PIPE_FORMAT_LATC2_UNORM:
1114 case PIPE_FORMAT_LATC2_SNORM:
1115 return PIPE_FORMAT_NONE;
1116
1117 default:
1118 assert(!util_format_is_luminance(format) &&
1119 !util_format_is_luminance_alpha(format));
1120 return format;
1121 }
1122 }
1123
1124 /**
1125 * Return the number of components stored.
1126 * Formats with block size != 1x1 will always have 1 component (the block).
1127 */
1128 static inline unsigned
1129 util_format_get_nr_components(enum pipe_format format)
1130 {
1131 const struct util_format_description *desc = util_format_description(format);
1132 return desc->nr_channels;
1133 }
1134
1135 /**
1136 * Return the index of the first non-void channel
1137 * -1 if no non-void channels
1138 */
1139 static inline int
1140 util_format_get_first_non_void_channel(enum pipe_format format)
1141 {
1142 const struct util_format_description *desc = util_format_description(format);
1143 int i;
1144
1145 for (i = 0; i < 4; i++)
1146 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1147 break;
1148
1149 if (i == 4)
1150 return -1;
1151
1152 return i;
1153 }
1154
1155 /*
1156 * Format access functions.
1157 */
1158
1159 void
1160 util_format_read_4f(enum pipe_format format,
1161 float *dst, unsigned dst_stride,
1162 const void *src, unsigned src_stride,
1163 unsigned x, unsigned y, unsigned w, unsigned h);
1164
1165 void
1166 util_format_write_4f(enum pipe_format format,
1167 const float *src, unsigned src_stride,
1168 void *dst, unsigned dst_stride,
1169 unsigned x, unsigned y, unsigned w, unsigned h);
1170
1171 void
1172 util_format_read_4ub(enum pipe_format format,
1173 uint8_t *dst, unsigned dst_stride,
1174 const void *src, unsigned src_stride,
1175 unsigned x, unsigned y, unsigned w, unsigned h);
1176
1177 void
1178 util_format_write_4ub(enum pipe_format format,
1179 const uint8_t *src, unsigned src_stride,
1180 void *dst, unsigned dst_stride,
1181 unsigned x, unsigned y, unsigned w, unsigned h);
1182
1183 void
1184 util_format_read_4ui(enum pipe_format format,
1185 unsigned *dst, unsigned dst_stride,
1186 const void *src, unsigned src_stride,
1187 unsigned x, unsigned y, unsigned w, unsigned h);
1188
1189 void
1190 util_format_write_4ui(enum pipe_format format,
1191 const unsigned int *src, unsigned src_stride,
1192 void *dst, unsigned dst_stride,
1193 unsigned x, unsigned y, unsigned w, unsigned h);
1194
1195 void
1196 util_format_read_4i(enum pipe_format format,
1197 int *dst, unsigned dst_stride,
1198 const void *src, unsigned src_stride,
1199 unsigned x, unsigned y, unsigned w, unsigned h);
1200
1201 void
1202 util_format_write_4i(enum pipe_format format,
1203 const int *src, unsigned src_stride,
1204 void *dst, unsigned dst_stride,
1205 unsigned x, unsigned y, unsigned w, unsigned h);
1206
1207 /*
1208 * Generic format conversion;
1209 */
1210
1211 boolean
1212 util_format_fits_8unorm(const struct util_format_description *format_desc);
1213
1214 boolean
1215 util_format_translate(enum pipe_format dst_format,
1216 void *dst, unsigned dst_stride,
1217 unsigned dst_x, unsigned dst_y,
1218 enum pipe_format src_format,
1219 const void *src, unsigned src_stride,
1220 unsigned src_x, unsigned src_y,
1221 unsigned width, unsigned height);
1222
1223 /*
1224 * Swizzle operations.
1225 */
1226
1227 /* Compose two sets of swizzles.
1228 * If V is a 4D vector and the function parameters represent functions that
1229 * swizzle vector components, this holds:
1230 * swz2(swz1(V)) = dst(V)
1231 */
1232 void util_format_compose_swizzles(const unsigned char swz1[4],
1233 const unsigned char swz2[4],
1234 unsigned char dst[4]);
1235
1236 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1237 * to \param src and store the result in \param dst.
1238 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1239 */
1240 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1241 const union pipe_color_union *src,
1242 const unsigned char swz[4],
1243 const boolean is_integer);
1244
1245 void util_format_swizzle_4f(float *dst, const float *src,
1246 const unsigned char swz[4]);
1247
1248 void util_format_unswizzle_4f(float *dst, const float *src,
1249 const unsigned char swz[4]);
1250
1251 #ifdef __cplusplus
1252 } // extern "C" {
1253 #endif
1254
1255 #endif /* ! U_FORMAT_H */