gallium/u_format: add a new helper for initializing pipe_blit_info::mask
[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 * Everything else that doesn't fit in any of the above layouts.
83 */
84 UTIL_FORMAT_LAYOUT_OTHER = 7
85 };
86
87
88 struct util_format_block
89 {
90 /** Block width in pixels */
91 unsigned width;
92
93 /** Block height in pixels */
94 unsigned height;
95
96 /** Block size in bits */
97 unsigned bits;
98 };
99
100
101 enum util_format_type {
102 UTIL_FORMAT_TYPE_VOID = 0,
103 UTIL_FORMAT_TYPE_UNSIGNED = 1,
104 UTIL_FORMAT_TYPE_SIGNED = 2,
105 UTIL_FORMAT_TYPE_FIXED = 3,
106 UTIL_FORMAT_TYPE_FLOAT = 4
107 };
108
109
110 enum util_format_swizzle {
111 UTIL_FORMAT_SWIZZLE_X = 0,
112 UTIL_FORMAT_SWIZZLE_Y = 1,
113 UTIL_FORMAT_SWIZZLE_Z = 2,
114 UTIL_FORMAT_SWIZZLE_W = 3,
115 UTIL_FORMAT_SWIZZLE_0 = 4,
116 UTIL_FORMAT_SWIZZLE_1 = 5,
117 UTIL_FORMAT_SWIZZLE_NONE = 6,
118 UTIL_FORMAT_SWIZZLE_MAX = 7 /**< Number of enums counter (must be last) */
119 };
120
121
122 enum util_format_colorspace {
123 UTIL_FORMAT_COLORSPACE_RGB = 0,
124 UTIL_FORMAT_COLORSPACE_SRGB = 1,
125 UTIL_FORMAT_COLORSPACE_YUV = 2,
126 UTIL_FORMAT_COLORSPACE_ZS = 3
127 };
128
129
130 struct util_format_channel_description
131 {
132 unsigned type:5; /**< UTIL_FORMAT_TYPE_x */
133 unsigned normalized:1;
134 unsigned pure_integer:1;
135 unsigned size:9; /**< bits per channel */
136 };
137
138
139 struct util_format_description
140 {
141 enum pipe_format format;
142
143 const char *name;
144
145 /**
146 * Short name, striped of the prefix, lower case.
147 */
148 const char *short_name;
149
150 /**
151 * Pixel block dimensions.
152 */
153 struct util_format_block block;
154
155 enum util_format_layout layout;
156
157 /**
158 * The number of channels.
159 */
160 unsigned nr_channels:3;
161
162 /**
163 * Whether all channels have the same number of (whole) bytes and type.
164 */
165 unsigned is_array:1;
166
167 /**
168 * Whether the pixel format can be described as a bitfield structure.
169 *
170 * In particular:
171 * - pixel depth must be 8, 16, or 32 bits;
172 * - all channels must be unsigned, signed, or void
173 */
174 unsigned is_bitmask:1;
175
176 /**
177 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
178 */
179 unsigned is_mixed:1;
180
181 /**
182 * Input channel description.
183 *
184 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
185 */
186 struct util_format_channel_description channel[4];
187
188 /**
189 * Output channel swizzle.
190 *
191 * The order is either:
192 * - RGBA
193 * - YUV(A)
194 * - ZS
195 * depending on the colorspace.
196 */
197 unsigned char swizzle[4];
198
199 /**
200 * Colorspace transformation.
201 */
202 enum util_format_colorspace colorspace;
203
204 /**
205 * Unpack pixel blocks to R8G8B8A8_UNORM.
206 * Note: strides are in bytes.
207 *
208 * Only defined for non-depth-stencil formats.
209 */
210 void
211 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
212 const uint8_t *src, unsigned src_stride,
213 unsigned width, unsigned height);
214
215 /**
216 * Pack pixel blocks from R8G8B8A8_UNORM.
217 * Note: strides are in bytes.
218 *
219 * Only defined for non-depth-stencil formats.
220 */
221 void
222 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
223 const uint8_t *src, unsigned src_stride,
224 unsigned width, unsigned height);
225
226 /**
227 * Fetch a single pixel (i, j) from a block.
228 *
229 * XXX: Only defined for a very few select formats.
230 */
231 void
232 (*fetch_rgba_8unorm)(uint8_t *dst,
233 const uint8_t *src,
234 unsigned i, unsigned j);
235
236 /**
237 * Unpack pixel blocks to R32G32B32A32_FLOAT.
238 * Note: strides are in bytes.
239 *
240 * Only defined for non-depth-stencil formats.
241 */
242 void
243 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
244 const uint8_t *src, unsigned src_stride,
245 unsigned width, unsigned height);
246
247 /**
248 * Pack pixel blocks from R32G32B32A32_FLOAT.
249 * Note: strides are in bytes.
250 *
251 * Only defined for non-depth-stencil formats.
252 */
253 void
254 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
255 const float *src, unsigned src_stride,
256 unsigned width, unsigned height);
257
258 /**
259 * Fetch a single pixel (i, j) from a block.
260 *
261 * Only defined for non-depth-stencil and non-integer formats.
262 */
263 void
264 (*fetch_rgba_float)(float *dst,
265 const uint8_t *src,
266 unsigned i, unsigned j);
267
268 /**
269 * Unpack pixels to Z32_UNORM.
270 * Note: strides are in bytes.
271 *
272 * Only defined for depth formats.
273 */
274 void
275 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
276 const uint8_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_32unorm)(uint8_t *dst, unsigned dst_stride,
287 const uint32_t *src, unsigned src_stride,
288 unsigned width, unsigned height);
289
290 /**
291 * Unpack pixels to Z32_FLOAT.
292 * Note: strides are in bytes.
293 *
294 * Only defined for depth formats.
295 */
296 void
297 (*unpack_z_float)(float *dst, unsigned dst_stride,
298 const uint8_t *src, unsigned src_stride,
299 unsigned width, unsigned height);
300
301 /**
302 * Pack pixels from Z32_FLOAT.
303 * Note: strides are in bytes.
304 *
305 * Only defined for depth formats.
306 */
307 void
308 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
309 const float *src, unsigned src_stride,
310 unsigned width, unsigned height);
311
312 /**
313 * Unpack pixels to S8_UINT.
314 * Note: strides are in bytes.
315 *
316 * Only defined for stencil formats.
317 */
318 void
319 (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
320 const uint8_t *src, unsigned src_stride,
321 unsigned width, unsigned height);
322
323 /**
324 * Pack pixels from S8_UINT.
325 * Note: strides are in bytes.
326 *
327 * Only defined for stencil formats.
328 */
329 void
330 (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
331 const uint8_t *src, unsigned src_stride,
332 unsigned width, unsigned height);
333
334 /**
335 * Unpack pixel blocks to R32G32B32A32_UINT.
336 * Note: strides are in bytes.
337 *
338 * Only defined for INT formats.
339 */
340 void
341 (*unpack_rgba_uint)(unsigned *dst, unsigned dst_stride,
342 const uint8_t *src, unsigned src_stride,
343 unsigned width, unsigned height);
344
345 void
346 (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
347 const unsigned *src, unsigned src_stride,
348 unsigned width, unsigned height);
349
350 /**
351 * Unpack pixel blocks to R32G32B32A32_SINT.
352 * Note: strides are in bytes.
353 *
354 * Only defined for INT formats.
355 */
356 void
357 (*unpack_rgba_sint)(signed *dst, unsigned dst_stride,
358 const uint8_t *src, unsigned src_stride,
359 unsigned width, unsigned height);
360
361 void
362 (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
363 const int *src, unsigned src_stride,
364 unsigned width, unsigned height);
365
366 /**
367 * Fetch a single pixel (i, j) from a block.
368 *
369 * Only defined for unsigned (pure) integer formats.
370 */
371 void
372 (*fetch_rgba_uint)(uint32_t *dst,
373 const uint8_t *src,
374 unsigned i, unsigned j);
375
376 /**
377 * Fetch a single pixel (i, j) from a block.
378 *
379 * Only defined for signed (pure) integer formats.
380 */
381 void
382 (*fetch_rgba_sint)(int32_t *dst,
383 const uint8_t *src,
384 unsigned i, unsigned j);
385 };
386
387
388 extern const struct util_format_description
389 util_format_description_table[];
390
391
392 const struct util_format_description *
393 util_format_description(enum pipe_format format);
394
395
396 /*
397 * Format query functions.
398 */
399
400 static INLINE const char *
401 util_format_name(enum pipe_format format)
402 {
403 const struct util_format_description *desc = util_format_description(format);
404
405 assert(desc);
406 if (!desc) {
407 return "PIPE_FORMAT_???";
408 }
409
410 return desc->name;
411 }
412
413 static INLINE const char *
414 util_format_short_name(enum pipe_format format)
415 {
416 const struct util_format_description *desc = util_format_description(format);
417
418 assert(desc);
419 if (!desc) {
420 return "???";
421 }
422
423 return desc->short_name;
424 }
425
426 /**
427 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
428 */
429 static INLINE boolean
430 util_format_is_plain(enum pipe_format format)
431 {
432 const struct util_format_description *desc = util_format_description(format);
433
434 if (!format) {
435 return FALSE;
436 }
437
438 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
439 }
440
441 static INLINE boolean
442 util_format_is_compressed(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 FALSE;
449 }
450
451 switch (desc->layout) {
452 case UTIL_FORMAT_LAYOUT_S3TC:
453 case UTIL_FORMAT_LAYOUT_RGTC:
454 case UTIL_FORMAT_LAYOUT_ETC:
455 /* XXX add other formats in the future */
456 return TRUE;
457 default:
458 return FALSE;
459 }
460 }
461
462 static INLINE boolean
463 util_format_is_s3tc(enum pipe_format format)
464 {
465 const struct util_format_description *desc = util_format_description(format);
466
467 assert(desc);
468 if (!desc) {
469 return FALSE;
470 }
471
472 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
473 }
474
475 static INLINE boolean
476 util_format_is_srgb(enum pipe_format format)
477 {
478 const struct util_format_description *desc = util_format_description(format);
479 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
480 }
481
482 static INLINE boolean
483 util_format_has_depth(const struct util_format_description *desc)
484 {
485 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
486 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
487 }
488
489 static INLINE boolean
490 util_format_has_stencil(const struct util_format_description *desc)
491 {
492 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
493 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
494 }
495
496 static INLINE boolean
497 util_format_is_depth_or_stencil(enum pipe_format format)
498 {
499 const struct util_format_description *desc = util_format_description(format);
500
501 assert(desc);
502 if (!desc) {
503 return FALSE;
504 }
505
506 return util_format_has_depth(desc) ||
507 util_format_has_stencil(desc);
508 }
509
510 static INLINE boolean
511 util_format_is_depth_and_stencil(enum pipe_format format)
512 {
513 const struct util_format_description *desc = util_format_description(format);
514
515 assert(desc);
516 if (!desc) {
517 return FALSE;
518 }
519
520 return util_format_has_depth(desc) &&
521 util_format_has_stencil(desc);
522 }
523
524 /**
525 * Return whether this is an RGBA, Z, S, or combined ZS format.
526 * Useful for initializing pipe_blit_info::mask.
527 */
528 static INLINE unsigned
529 util_format_get_mask(enum pipe_format format)
530 {
531 const struct util_format_description *desc =
532 util_format_description(format);
533
534 if (!desc)
535 return 0;
536
537 if (util_format_has_depth(desc)) {
538 if (util_format_has_stencil(desc)) {
539 return PIPE_MASK_ZS;
540 } else {
541 return PIPE_MASK_Z;
542 }
543 } else {
544 if (util_format_has_stencil(desc)) {
545 return PIPE_MASK_S;
546 } else {
547 return PIPE_MASK_RGBA;
548 }
549 }
550 }
551
552 /**
553 * Give the RGBA colormask of the channels that can be represented in this
554 * format.
555 *
556 * That is, the channels whose values are preserved.
557 */
558 static INLINE unsigned
559 util_format_colormask(const struct util_format_description *desc)
560 {
561 unsigned colormask;
562 unsigned chan;
563
564 switch (desc->colorspace) {
565 case UTIL_FORMAT_COLORSPACE_RGB:
566 case UTIL_FORMAT_COLORSPACE_SRGB:
567 case UTIL_FORMAT_COLORSPACE_YUV:
568 colormask = 0;
569 for (chan = 0; chan < 4; ++chan) {
570 if (desc->swizzle[chan] < 4) {
571 colormask |= (1 << chan);
572 }
573 }
574 return colormask;
575 case UTIL_FORMAT_COLORSPACE_ZS:
576 return 0;
577 default:
578 assert(0);
579 return 0;
580 }
581 }
582
583
584 /**
585 * Checks if color mask covers every channel for the specified format
586 *
587 * @param desc a format description to check colormask with
588 * @param colormask a bit mask for channels, matches format of PIPE_MASK_RGBA
589 */
590 static INLINE boolean
591 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
592 {
593 return (~colormask & util_format_colormask(desc)) == 0;
594 }
595
596
597 boolean
598 util_format_is_float(enum pipe_format format);
599
600
601 boolean
602 util_format_has_alpha(enum pipe_format format);
603
604
605 boolean
606 util_format_is_luminance(enum pipe_format format);
607
608
609 boolean
610 util_format_is_luminance_alpha(enum pipe_format format);
611
612
613 boolean
614 util_format_is_intensity(enum pipe_format format);
615
616 boolean
617 util_format_is_pure_integer(enum pipe_format format);
618
619 boolean
620 util_format_is_pure_sint(enum pipe_format format);
621
622 boolean
623 util_format_is_pure_uint(enum pipe_format format);
624
625 /**
626 * Check if the src format can be blitted to the destination format with
627 * a simple memcpy. For example, blitting from RGBA to RGBx is OK, but not
628 * the reverse.
629 */
630 boolean
631 util_is_format_compatible(const struct util_format_description *src_desc,
632 const struct util_format_description *dst_desc);
633
634 /**
635 * Whether the format is supported by Gallium for the given bindings.
636 * This covers S3TC textures and floating-point render targets.
637 */
638 boolean
639 util_format_is_supported(enum pipe_format format, unsigned bind);
640
641 /**
642 * Whether this format is a rgab8 variant.
643 *
644 * That is, any format that matches the
645 *
646 * PIPE_FORMAT_?8?8?8?8_UNORM
647 */
648 static INLINE boolean
649 util_format_is_rgba8_variant(const struct util_format_description *desc)
650 {
651 unsigned chan;
652
653 if(desc->block.width != 1 ||
654 desc->block.height != 1 ||
655 desc->block.bits != 32)
656 return FALSE;
657
658 for(chan = 0; chan < 4; ++chan) {
659 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
660 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
661 return FALSE;
662 if(desc->channel[chan].size != 8)
663 return FALSE;
664 }
665
666 return TRUE;
667 }
668
669
670 /**
671 * Return total bits needed for the pixel format per block.
672 */
673 static INLINE uint
674 util_format_get_blocksizebits(enum pipe_format format)
675 {
676 const struct util_format_description *desc = util_format_description(format);
677
678 assert(desc);
679 if (!desc) {
680 return 0;
681 }
682
683 return desc->block.bits;
684 }
685
686 /**
687 * Return bytes per block (not pixel) for the given format.
688 */
689 static INLINE uint
690 util_format_get_blocksize(enum pipe_format format)
691 {
692 uint bits = util_format_get_blocksizebits(format);
693
694 assert(bits % 8 == 0);
695
696 return bits / 8;
697 }
698
699 static INLINE uint
700 util_format_get_blockwidth(enum pipe_format format)
701 {
702 const struct util_format_description *desc = util_format_description(format);
703
704 assert(desc);
705 if (!desc) {
706 return 1;
707 }
708
709 return desc->block.width;
710 }
711
712 static INLINE uint
713 util_format_get_blockheight(enum pipe_format format)
714 {
715 const struct util_format_description *desc = util_format_description(format);
716
717 assert(desc);
718 if (!desc) {
719 return 1;
720 }
721
722 return desc->block.height;
723 }
724
725 static INLINE unsigned
726 util_format_get_nblocksx(enum pipe_format format,
727 unsigned x)
728 {
729 unsigned blockwidth = util_format_get_blockwidth(format);
730 return (x + blockwidth - 1) / blockwidth;
731 }
732
733 static INLINE unsigned
734 util_format_get_nblocksy(enum pipe_format format,
735 unsigned y)
736 {
737 unsigned blockheight = util_format_get_blockheight(format);
738 return (y + blockheight - 1) / blockheight;
739 }
740
741 static INLINE unsigned
742 util_format_get_nblocks(enum pipe_format format,
743 unsigned width,
744 unsigned height)
745 {
746 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
747 }
748
749 static INLINE size_t
750 util_format_get_stride(enum pipe_format format,
751 unsigned width)
752 {
753 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
754 }
755
756 static INLINE size_t
757 util_format_get_2d_size(enum pipe_format format,
758 size_t stride,
759 unsigned height)
760 {
761 return util_format_get_nblocksy(format, height) * stride;
762 }
763
764 static INLINE uint
765 util_format_get_component_bits(enum pipe_format format,
766 enum util_format_colorspace colorspace,
767 uint component)
768 {
769 const struct util_format_description *desc = util_format_description(format);
770 enum util_format_colorspace desc_colorspace;
771
772 assert(format);
773 if (!format) {
774 return 0;
775 }
776
777 assert(component < 4);
778
779 /* Treat RGB and SRGB as equivalent. */
780 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
781 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
782 }
783 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
784 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
785 } else {
786 desc_colorspace = desc->colorspace;
787 }
788
789 if (desc_colorspace != colorspace) {
790 return 0;
791 }
792
793 switch (desc->swizzle[component]) {
794 case UTIL_FORMAT_SWIZZLE_X:
795 return desc->channel[0].size;
796 case UTIL_FORMAT_SWIZZLE_Y:
797 return desc->channel[1].size;
798 case UTIL_FORMAT_SWIZZLE_Z:
799 return desc->channel[2].size;
800 case UTIL_FORMAT_SWIZZLE_W:
801 return desc->channel[3].size;
802 default:
803 return 0;
804 }
805 }
806
807 /**
808 * Given a linear RGB colorspace format, return the corresponding SRGB
809 * format, or PIPE_FORMAT_NONE if none.
810 */
811 static INLINE enum pipe_format
812 util_format_srgb(enum pipe_format format)
813 {
814 switch (format) {
815 case PIPE_FORMAT_L8_UNORM:
816 return PIPE_FORMAT_L8_SRGB;
817 case PIPE_FORMAT_L8A8_UNORM:
818 return PIPE_FORMAT_L8A8_SRGB;
819 case PIPE_FORMAT_R8G8B8_UNORM:
820 return PIPE_FORMAT_R8G8B8_SRGB;
821 case PIPE_FORMAT_A8B8G8R8_UNORM:
822 return PIPE_FORMAT_A8B8G8R8_SRGB;
823 case PIPE_FORMAT_X8B8G8R8_UNORM:
824 return PIPE_FORMAT_X8B8G8R8_SRGB;
825 case PIPE_FORMAT_B8G8R8A8_UNORM:
826 return PIPE_FORMAT_B8G8R8A8_SRGB;
827 case PIPE_FORMAT_B8G8R8X8_UNORM:
828 return PIPE_FORMAT_B8G8R8X8_SRGB;
829 case PIPE_FORMAT_A8R8G8B8_UNORM:
830 return PIPE_FORMAT_A8R8G8B8_SRGB;
831 case PIPE_FORMAT_X8R8G8B8_UNORM:
832 return PIPE_FORMAT_X8R8G8B8_SRGB;
833 case PIPE_FORMAT_R8G8B8A8_UNORM:
834 return PIPE_FORMAT_R8G8B8A8_SRGB;
835 case PIPE_FORMAT_R8G8B8X8_UNORM:
836 return PIPE_FORMAT_R8G8B8X8_SRGB;
837 case PIPE_FORMAT_DXT1_RGB:
838 return PIPE_FORMAT_DXT1_SRGB;
839 case PIPE_FORMAT_DXT1_RGBA:
840 return PIPE_FORMAT_DXT1_SRGBA;
841 case PIPE_FORMAT_DXT3_RGBA:
842 return PIPE_FORMAT_DXT3_SRGBA;
843 case PIPE_FORMAT_DXT5_RGBA:
844 return PIPE_FORMAT_DXT5_SRGBA;
845 default:
846 return PIPE_FORMAT_NONE;
847 }
848 }
849
850 /**
851 * Given an sRGB format, return the corresponding linear colorspace format.
852 * For non sRGB formats, return the format unchanged.
853 */
854 static INLINE enum pipe_format
855 util_format_linear(enum pipe_format format)
856 {
857 switch (format) {
858 case PIPE_FORMAT_L8_SRGB:
859 return PIPE_FORMAT_L8_UNORM;
860 case PIPE_FORMAT_L8A8_SRGB:
861 return PIPE_FORMAT_L8A8_UNORM;
862 case PIPE_FORMAT_R8G8B8_SRGB:
863 return PIPE_FORMAT_R8G8B8_UNORM;
864 case PIPE_FORMAT_A8B8G8R8_SRGB:
865 return PIPE_FORMAT_A8B8G8R8_UNORM;
866 case PIPE_FORMAT_X8B8G8R8_SRGB:
867 return PIPE_FORMAT_X8B8G8R8_UNORM;
868 case PIPE_FORMAT_B8G8R8A8_SRGB:
869 return PIPE_FORMAT_B8G8R8A8_UNORM;
870 case PIPE_FORMAT_B8G8R8X8_SRGB:
871 return PIPE_FORMAT_B8G8R8X8_UNORM;
872 case PIPE_FORMAT_A8R8G8B8_SRGB:
873 return PIPE_FORMAT_A8R8G8B8_UNORM;
874 case PIPE_FORMAT_X8R8G8B8_SRGB:
875 return PIPE_FORMAT_X8R8G8B8_UNORM;
876 case PIPE_FORMAT_R8G8B8A8_SRGB:
877 return PIPE_FORMAT_R8G8B8A8_UNORM;
878 case PIPE_FORMAT_R8G8B8X8_SRGB:
879 return PIPE_FORMAT_R8G8B8X8_UNORM;
880 case PIPE_FORMAT_DXT1_SRGB:
881 return PIPE_FORMAT_DXT1_RGB;
882 case PIPE_FORMAT_DXT1_SRGBA:
883 return PIPE_FORMAT_DXT1_RGBA;
884 case PIPE_FORMAT_DXT3_SRGBA:
885 return PIPE_FORMAT_DXT3_RGBA;
886 case PIPE_FORMAT_DXT5_SRGBA:
887 return PIPE_FORMAT_DXT5_RGBA;
888 default:
889 return format;
890 }
891 }
892
893 /**
894 * Given a depth-stencil format, return the corresponding stencil-only format.
895 * For stencil-only formats, return the format unchanged.
896 */
897 static INLINE enum pipe_format
898 util_format_stencil_only(enum pipe_format format)
899 {
900 switch (format) {
901 /* mask out the depth component */
902 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
903 return PIPE_FORMAT_X24S8_UINT;
904 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
905 return PIPE_FORMAT_S8X24_UINT;
906 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
907 return PIPE_FORMAT_X32_S8X24_UINT;
908
909 /* stencil only formats */
910 case PIPE_FORMAT_X24S8_UINT:
911 case PIPE_FORMAT_S8X24_UINT:
912 case PIPE_FORMAT_X32_S8X24_UINT:
913 case PIPE_FORMAT_S8_UINT:
914 return format;
915
916 default:
917 assert(0);
918 return PIPE_FORMAT_NONE;
919 }
920 }
921
922 /**
923 * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
924 * This is identity for non-intensity formats.
925 */
926 static INLINE enum pipe_format
927 util_format_intensity_to_red(enum pipe_format format)
928 {
929 switch (format) {
930 case PIPE_FORMAT_I8_UNORM:
931 return PIPE_FORMAT_R8_UNORM;
932 case PIPE_FORMAT_I8_SNORM:
933 return PIPE_FORMAT_R8_SNORM;
934 case PIPE_FORMAT_I16_UNORM:
935 return PIPE_FORMAT_R16_UNORM;
936 case PIPE_FORMAT_I16_SNORM:
937 return PIPE_FORMAT_R16_SNORM;
938 case PIPE_FORMAT_I16_FLOAT:
939 return PIPE_FORMAT_R16_FLOAT;
940 case PIPE_FORMAT_I32_FLOAT:
941 return PIPE_FORMAT_R32_FLOAT;
942 case PIPE_FORMAT_I8_UINT:
943 return PIPE_FORMAT_R8_UINT;
944 case PIPE_FORMAT_I8_SINT:
945 return PIPE_FORMAT_R8_SINT;
946 case PIPE_FORMAT_I16_UINT:
947 return PIPE_FORMAT_R16_UINT;
948 case PIPE_FORMAT_I16_SINT:
949 return PIPE_FORMAT_R16_SINT;
950 case PIPE_FORMAT_I32_UINT:
951 return PIPE_FORMAT_R32_UINT;
952 case PIPE_FORMAT_I32_SINT:
953 return PIPE_FORMAT_R32_SINT;
954 default:
955 assert(!util_format_is_intensity(format));
956 return format;
957 }
958 }
959
960 /**
961 * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
962 * This is identity for non-luminance formats.
963 */
964 static INLINE enum pipe_format
965 util_format_luminance_to_red(enum pipe_format format)
966 {
967 switch (format) {
968 case PIPE_FORMAT_L8_UNORM:
969 return PIPE_FORMAT_R8_UNORM;
970 case PIPE_FORMAT_L8_SNORM:
971 return PIPE_FORMAT_R8_SNORM;
972 case PIPE_FORMAT_L16_UNORM:
973 return PIPE_FORMAT_R16_UNORM;
974 case PIPE_FORMAT_L16_SNORM:
975 return PIPE_FORMAT_R16_SNORM;
976 case PIPE_FORMAT_L16_FLOAT:
977 return PIPE_FORMAT_R16_FLOAT;
978 case PIPE_FORMAT_L32_FLOAT:
979 return PIPE_FORMAT_R32_FLOAT;
980 case PIPE_FORMAT_L8_UINT:
981 return PIPE_FORMAT_R8_UINT;
982 case PIPE_FORMAT_L8_SINT:
983 return PIPE_FORMAT_R8_SINT;
984 case PIPE_FORMAT_L16_UINT:
985 return PIPE_FORMAT_R16_UINT;
986 case PIPE_FORMAT_L16_SINT:
987 return PIPE_FORMAT_R16_SINT;
988 case PIPE_FORMAT_L32_UINT:
989 return PIPE_FORMAT_R32_UINT;
990 case PIPE_FORMAT_L32_SINT:
991 return PIPE_FORMAT_R32_SINT;
992
993 case PIPE_FORMAT_LATC1_UNORM:
994 return PIPE_FORMAT_RGTC1_UNORM;
995 case PIPE_FORMAT_LATC1_SNORM:
996 return PIPE_FORMAT_RGTC1_SNORM;
997
998 case PIPE_FORMAT_L4A4_UNORM:
999 /* XXX A4R4 is defined as x00y in u_format.csv */
1000 return PIPE_FORMAT_A4R4_UNORM;
1001
1002 case PIPE_FORMAT_L8A8_UNORM:
1003 return PIPE_FORMAT_R8A8_UNORM;
1004 case PIPE_FORMAT_L8A8_SNORM:
1005 return PIPE_FORMAT_R8A8_SNORM;
1006 case PIPE_FORMAT_L16A16_UNORM:
1007 return PIPE_FORMAT_R16A16_UNORM;
1008 case PIPE_FORMAT_L16A16_SNORM:
1009 return PIPE_FORMAT_R16A16_SNORM;
1010 case PIPE_FORMAT_L16A16_FLOAT:
1011 return PIPE_FORMAT_R16A16_FLOAT;
1012 case PIPE_FORMAT_L32A32_FLOAT:
1013 return PIPE_FORMAT_R32A32_FLOAT;
1014 case PIPE_FORMAT_L8A8_UINT:
1015 return PIPE_FORMAT_R8A8_UINT;
1016 case PIPE_FORMAT_L8A8_SINT:
1017 return PIPE_FORMAT_R8A8_SINT;
1018 case PIPE_FORMAT_L16A16_UINT:
1019 return PIPE_FORMAT_R16A16_UINT;
1020 case PIPE_FORMAT_L16A16_SINT:
1021 return PIPE_FORMAT_R16A16_SINT;
1022 case PIPE_FORMAT_L32A32_UINT:
1023 return PIPE_FORMAT_R32A32_UINT;
1024 case PIPE_FORMAT_L32A32_SINT:
1025 return PIPE_FORMAT_R32A32_SINT;
1026
1027 /* We don't have compressed red-alpha variants for these. */
1028 case PIPE_FORMAT_LATC2_UNORM:
1029 case PIPE_FORMAT_LATC2_SNORM:
1030 return PIPE_FORMAT_NONE;
1031
1032 default:
1033 assert(!util_format_is_luminance(format) &&
1034 !util_format_is_luminance_alpha(format));
1035 return format;
1036 }
1037 }
1038
1039 /**
1040 * Return the number of components stored.
1041 * Formats with block size != 1x1 will always have 1 component (the block).
1042 */
1043 static INLINE unsigned
1044 util_format_get_nr_components(enum pipe_format format)
1045 {
1046 const struct util_format_description *desc = util_format_description(format);
1047 return desc->nr_channels;
1048 }
1049
1050 /**
1051 * Return the index of the first non-void channel
1052 * -1 if no non-void channels
1053 */
1054 static INLINE int
1055 util_format_get_first_non_void_channel(enum pipe_format format)
1056 {
1057 const struct util_format_description *desc = util_format_description(format);
1058 int i;
1059
1060 for (i = 0; i < 4; i++)
1061 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1062 break;
1063
1064 if (i == 4)
1065 return -1;
1066
1067 return i;
1068 }
1069
1070 /*
1071 * Format access functions.
1072 */
1073
1074 void
1075 util_format_read_4f(enum pipe_format format,
1076 float *dst, unsigned dst_stride,
1077 const void *src, unsigned src_stride,
1078 unsigned x, unsigned y, unsigned w, unsigned h);
1079
1080 void
1081 util_format_write_4f(enum pipe_format format,
1082 const float *src, unsigned src_stride,
1083 void *dst, unsigned dst_stride,
1084 unsigned x, unsigned y, unsigned w, unsigned h);
1085
1086 void
1087 util_format_read_4ub(enum pipe_format format,
1088 uint8_t *dst, unsigned dst_stride,
1089 const void *src, unsigned src_stride,
1090 unsigned x, unsigned y, unsigned w, unsigned h);
1091
1092 void
1093 util_format_write_4ub(enum pipe_format format,
1094 const uint8_t *src, unsigned src_stride,
1095 void *dst, unsigned dst_stride,
1096 unsigned x, unsigned y, unsigned w, unsigned h);
1097
1098 void
1099 util_format_read_4ui(enum pipe_format format,
1100 unsigned *dst, unsigned dst_stride,
1101 const void *src, unsigned src_stride,
1102 unsigned x, unsigned y, unsigned w, unsigned h);
1103
1104 void
1105 util_format_write_4ui(enum pipe_format format,
1106 const unsigned int *src, unsigned src_stride,
1107 void *dst, unsigned dst_stride,
1108 unsigned x, unsigned y, unsigned w, unsigned h);
1109
1110 void
1111 util_format_read_4i(enum pipe_format format,
1112 int *dst, unsigned dst_stride,
1113 const void *src, unsigned src_stride,
1114 unsigned x, unsigned y, unsigned w, unsigned h);
1115
1116 void
1117 util_format_write_4i(enum pipe_format format,
1118 const int *src, unsigned src_stride,
1119 void *dst, unsigned dst_stride,
1120 unsigned x, unsigned y, unsigned w, unsigned h);
1121
1122 /*
1123 * Generic format conversion;
1124 */
1125
1126 boolean
1127 util_format_fits_8unorm(const struct util_format_description *format_desc);
1128
1129 void
1130 util_format_translate(enum pipe_format dst_format,
1131 void *dst, unsigned dst_stride,
1132 unsigned dst_x, unsigned dst_y,
1133 enum pipe_format src_format,
1134 const void *src, unsigned src_stride,
1135 unsigned src_x, unsigned src_y,
1136 unsigned width, unsigned height);
1137
1138 /*
1139 * Swizzle operations.
1140 */
1141
1142 /* Compose two sets of swizzles.
1143 * If V is a 4D vector and the function parameters represent functions that
1144 * swizzle vector components, this holds:
1145 * swz2(swz1(V)) = dst(V)
1146 */
1147 void util_format_compose_swizzles(const unsigned char swz1[4],
1148 const unsigned char swz2[4],
1149 unsigned char dst[4]);
1150
1151 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1152 * to \param src and store the result in \param dst.
1153 * \param is_integer determines the value written for PIPE_SWIZZLE_ONE.
1154 */
1155 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1156 const union pipe_color_union *src,
1157 const unsigned char swz[4],
1158 const boolean is_integer);
1159
1160 void util_format_swizzle_4f(float *dst, const float *src,
1161 const unsigned char swz[4]);
1162
1163 void util_format_unswizzle_4f(float *dst, const float *src,
1164 const unsigned char swz[4]);
1165
1166 #ifdef __cplusplus
1167 } // extern "C" {
1168 #endif
1169
1170 #endif /* ! U_FORMAT_H */