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