gallium: add PIPE_FORMAT_ETC1_RGB8
[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 YV12 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.
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 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
364 extern const struct util_format_description
365 util_format_description_table[];
366
367
368 const struct util_format_description *
369 util_format_description(enum pipe_format format);
370
371
372 /*
373 * Format query functions.
374 */
375
376 static INLINE const char *
377 util_format_name(enum pipe_format format)
378 {
379 const struct util_format_description *desc = util_format_description(format);
380
381 assert(desc);
382 if (!desc) {
383 return "PIPE_FORMAT_???";
384 }
385
386 return desc->name;
387 }
388
389 static INLINE const char *
390 util_format_short_name(enum pipe_format format)
391 {
392 const struct util_format_description *desc = util_format_description(format);
393
394 assert(desc);
395 if (!desc) {
396 return "???";
397 }
398
399 return desc->short_name;
400 }
401
402 /**
403 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
404 */
405 static INLINE boolean
406 util_format_is_plain(enum pipe_format format)
407 {
408 const struct util_format_description *desc = util_format_description(format);
409
410 if (!format) {
411 return FALSE;
412 }
413
414 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
415 }
416
417 static INLINE boolean
418 util_format_is_compressed(enum pipe_format format)
419 {
420 const struct util_format_description *desc = util_format_description(format);
421
422 assert(desc);
423 if (!desc) {
424 return FALSE;
425 }
426
427 switch (desc->layout) {
428 case UTIL_FORMAT_LAYOUT_S3TC:
429 case UTIL_FORMAT_LAYOUT_RGTC:
430 /* XXX add other formats in the future */
431 return TRUE;
432 default:
433 return FALSE;
434 }
435 }
436
437 static INLINE boolean
438 util_format_is_s3tc(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 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
448 }
449
450 static INLINE boolean
451 util_format_is_srgb(enum pipe_format format)
452 {
453 const struct util_format_description *desc = util_format_description(format);
454 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
455 }
456
457 static INLINE boolean
458 util_format_has_depth(const struct util_format_description *desc)
459 {
460 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
461 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
462 }
463
464 static INLINE boolean
465 util_format_has_stencil(const struct util_format_description *desc)
466 {
467 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
468 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
469 }
470
471 static INLINE boolean
472 util_format_is_depth_or_stencil(enum pipe_format format)
473 {
474 const struct util_format_description *desc = util_format_description(format);
475
476 assert(desc);
477 if (!desc) {
478 return FALSE;
479 }
480
481 return util_format_has_depth(desc) ||
482 util_format_has_stencil(desc);
483 }
484
485 static INLINE boolean
486 util_format_is_depth_and_stencil(enum pipe_format format)
487 {
488 const struct util_format_description *desc = util_format_description(format);
489
490 assert(desc);
491 if (!desc) {
492 return FALSE;
493 }
494
495 return util_format_has_depth(desc) &&
496 util_format_has_stencil(desc);
497 }
498
499
500 /**
501 * Give the RGBA colormask of the channels that can be represented in this
502 * format.
503 *
504 * That is, the channels whose values are preserved.
505 */
506 static INLINE unsigned
507 util_format_colormask(const struct util_format_description *desc)
508 {
509 unsigned colormask;
510 unsigned chan;
511
512 switch (desc->colorspace) {
513 case UTIL_FORMAT_COLORSPACE_RGB:
514 case UTIL_FORMAT_COLORSPACE_SRGB:
515 case UTIL_FORMAT_COLORSPACE_YUV:
516 colormask = 0;
517 for (chan = 0; chan < 4; ++chan) {
518 if (desc->swizzle[chan] < 4) {
519 colormask |= (1 << chan);
520 }
521 }
522 return colormask;
523 case UTIL_FORMAT_COLORSPACE_ZS:
524 return 0;
525 default:
526 assert(0);
527 return 0;
528 }
529 }
530
531
532 boolean
533 util_format_is_float(enum pipe_format format);
534
535
536 boolean
537 util_format_is_rgb_no_alpha(enum pipe_format format);
538
539
540 boolean
541 util_format_is_luminance(enum pipe_format format);
542
543
544 boolean
545 util_format_is_luminance_alpha(enum pipe_format format);
546
547
548 boolean
549 util_format_is_intensity(enum pipe_format format);
550
551 boolean
552 util_format_is_pure_integer(enum pipe_format format);
553
554 boolean
555 util_format_is_pure_sint(enum pipe_format format);
556
557 boolean
558 util_format_is_pure_uint(enum pipe_format format);
559
560 /**
561 * Whether the src format can be blitted to destation format with a simple
562 * memcpy.
563 */
564 boolean
565 util_is_format_compatible(const struct util_format_description *src_desc,
566 const struct util_format_description *dst_desc);
567
568 /**
569 * Whether the format is supported by Gallium for the given bindings.
570 * This covers S3TC textures and floating-point render targets.
571 */
572 boolean
573 util_format_is_supported(enum pipe_format format, unsigned bind);
574
575 /**
576 * Whether this format is a rgab8 variant.
577 *
578 * That is, any format that matches the
579 *
580 * PIPE_FORMAT_?8?8?8?8_UNORM
581 */
582 static INLINE boolean
583 util_format_is_rgba8_variant(const struct util_format_description *desc)
584 {
585 unsigned chan;
586
587 if(desc->block.width != 1 ||
588 desc->block.height != 1 ||
589 desc->block.bits != 32)
590 return FALSE;
591
592 for(chan = 0; chan < 4; ++chan) {
593 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
594 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
595 return FALSE;
596 if(desc->channel[chan].size != 8)
597 return FALSE;
598 }
599
600 return TRUE;
601 }
602
603
604 /**
605 * Return total bits needed for the pixel format per block.
606 */
607 static INLINE uint
608 util_format_get_blocksizebits(enum pipe_format format)
609 {
610 const struct util_format_description *desc = util_format_description(format);
611
612 assert(desc);
613 if (!desc) {
614 return 0;
615 }
616
617 return desc->block.bits;
618 }
619
620 /**
621 * Return bytes per block (not pixel) for the given format.
622 */
623 static INLINE uint
624 util_format_get_blocksize(enum pipe_format format)
625 {
626 uint bits = util_format_get_blocksizebits(format);
627
628 assert(bits % 8 == 0);
629
630 return bits / 8;
631 }
632
633 static INLINE uint
634 util_format_get_blockwidth(enum pipe_format format)
635 {
636 const struct util_format_description *desc = util_format_description(format);
637
638 assert(desc);
639 if (!desc) {
640 return 1;
641 }
642
643 return desc->block.width;
644 }
645
646 static INLINE uint
647 util_format_get_blockheight(enum pipe_format format)
648 {
649 const struct util_format_description *desc = util_format_description(format);
650
651 assert(desc);
652 if (!desc) {
653 return 1;
654 }
655
656 return desc->block.height;
657 }
658
659 static INLINE unsigned
660 util_format_get_nblocksx(enum pipe_format format,
661 unsigned x)
662 {
663 unsigned blockwidth = util_format_get_blockwidth(format);
664 return (x + blockwidth - 1) / blockwidth;
665 }
666
667 static INLINE unsigned
668 util_format_get_nblocksy(enum pipe_format format,
669 unsigned y)
670 {
671 unsigned blockheight = util_format_get_blockheight(format);
672 return (y + blockheight - 1) / blockheight;
673 }
674
675 static INLINE unsigned
676 util_format_get_nblocks(enum pipe_format format,
677 unsigned width,
678 unsigned height)
679 {
680 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
681 }
682
683 static INLINE size_t
684 util_format_get_stride(enum pipe_format format,
685 unsigned width)
686 {
687 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
688 }
689
690 static INLINE size_t
691 util_format_get_2d_size(enum pipe_format format,
692 size_t stride,
693 unsigned height)
694 {
695 return util_format_get_nblocksy(format, height) * stride;
696 }
697
698 static INLINE uint
699 util_format_get_component_bits(enum pipe_format format,
700 enum util_format_colorspace colorspace,
701 uint component)
702 {
703 const struct util_format_description *desc = util_format_description(format);
704 enum util_format_colorspace desc_colorspace;
705
706 assert(format);
707 if (!format) {
708 return 0;
709 }
710
711 assert(component < 4);
712
713 /* Treat RGB and SRGB as equivalent. */
714 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
715 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
716 }
717 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
718 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
719 } else {
720 desc_colorspace = desc->colorspace;
721 }
722
723 if (desc_colorspace != colorspace) {
724 return 0;
725 }
726
727 switch (desc->swizzle[component]) {
728 case UTIL_FORMAT_SWIZZLE_X:
729 return desc->channel[0].size;
730 case UTIL_FORMAT_SWIZZLE_Y:
731 return desc->channel[1].size;
732 case UTIL_FORMAT_SWIZZLE_Z:
733 return desc->channel[2].size;
734 case UTIL_FORMAT_SWIZZLE_W:
735 return desc->channel[3].size;
736 default:
737 return 0;
738 }
739 }
740
741 static INLINE boolean
742 util_format_has_alpha(enum pipe_format format)
743 {
744 const struct util_format_description *desc = util_format_description(format);
745
746 assert(format);
747 if (!format) {
748 return FALSE;
749 }
750
751 switch (desc->colorspace) {
752 case UTIL_FORMAT_COLORSPACE_RGB:
753 case UTIL_FORMAT_COLORSPACE_SRGB:
754 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
755 case UTIL_FORMAT_COLORSPACE_YUV:
756 return FALSE;
757 case UTIL_FORMAT_COLORSPACE_ZS:
758 return FALSE;
759 default:
760 assert(0);
761 return FALSE;
762 }
763 }
764
765 /**
766 * Given a linear RGB colorspace format, return the corresponding SRGB
767 * format, or PIPE_FORMAT_NONE if none.
768 */
769 static INLINE enum pipe_format
770 util_format_srgb(enum pipe_format format)
771 {
772 switch (format) {
773 case PIPE_FORMAT_L8_UNORM:
774 return PIPE_FORMAT_L8_SRGB;
775 case PIPE_FORMAT_L8A8_UNORM:
776 return PIPE_FORMAT_L8A8_SRGB;
777 case PIPE_FORMAT_R8G8B8_UNORM:
778 return PIPE_FORMAT_R8G8B8_SRGB;
779 case PIPE_FORMAT_A8B8G8R8_UNORM:
780 return PIPE_FORMAT_A8B8G8R8_SRGB;
781 case PIPE_FORMAT_X8B8G8R8_UNORM:
782 return PIPE_FORMAT_X8B8G8R8_SRGB;
783 case PIPE_FORMAT_B8G8R8A8_UNORM:
784 return PIPE_FORMAT_B8G8R8A8_SRGB;
785 case PIPE_FORMAT_B8G8R8X8_UNORM:
786 return PIPE_FORMAT_B8G8R8X8_SRGB;
787 case PIPE_FORMAT_A8R8G8B8_UNORM:
788 return PIPE_FORMAT_A8R8G8B8_SRGB;
789 case PIPE_FORMAT_X8R8G8B8_UNORM:
790 return PIPE_FORMAT_X8R8G8B8_SRGB;
791 case PIPE_FORMAT_DXT1_RGB:
792 return PIPE_FORMAT_DXT1_SRGB;
793 case PIPE_FORMAT_DXT1_RGBA:
794 return PIPE_FORMAT_DXT1_SRGBA;
795 case PIPE_FORMAT_DXT3_RGBA:
796 return PIPE_FORMAT_DXT3_SRGBA;
797 case PIPE_FORMAT_DXT5_RGBA:
798 return PIPE_FORMAT_DXT5_SRGBA;
799 default:
800 return PIPE_FORMAT_NONE;
801 }
802 }
803
804 /**
805 * Given an sRGB format, return the corresponding linear colorspace format.
806 * For non sRGB formats, return the format unchanged.
807 */
808 static INLINE enum pipe_format
809 util_format_linear(enum pipe_format format)
810 {
811 switch (format) {
812 case PIPE_FORMAT_L8_SRGB:
813 return PIPE_FORMAT_L8_UNORM;
814 case PIPE_FORMAT_L8A8_SRGB:
815 return PIPE_FORMAT_L8A8_UNORM;
816 case PIPE_FORMAT_R8G8B8_SRGB:
817 return PIPE_FORMAT_R8G8B8_UNORM;
818 case PIPE_FORMAT_A8B8G8R8_SRGB:
819 return PIPE_FORMAT_A8B8G8R8_UNORM;
820 case PIPE_FORMAT_X8B8G8R8_SRGB:
821 return PIPE_FORMAT_X8B8G8R8_UNORM;
822 case PIPE_FORMAT_B8G8R8A8_SRGB:
823 return PIPE_FORMAT_B8G8R8A8_UNORM;
824 case PIPE_FORMAT_B8G8R8X8_SRGB:
825 return PIPE_FORMAT_B8G8R8X8_UNORM;
826 case PIPE_FORMAT_A8R8G8B8_SRGB:
827 return PIPE_FORMAT_A8R8G8B8_UNORM;
828 case PIPE_FORMAT_X8R8G8B8_SRGB:
829 return PIPE_FORMAT_X8R8G8B8_UNORM;
830 case PIPE_FORMAT_DXT1_SRGB:
831 return PIPE_FORMAT_DXT1_RGB;
832 case PIPE_FORMAT_DXT1_SRGBA:
833 return PIPE_FORMAT_DXT1_RGBA;
834 case PIPE_FORMAT_DXT3_SRGBA:
835 return PIPE_FORMAT_DXT3_RGBA;
836 case PIPE_FORMAT_DXT5_SRGBA:
837 return PIPE_FORMAT_DXT5_RGBA;
838 default:
839 return format;
840 }
841 }
842
843 /**
844 * Return the number of components stored.
845 * Formats with block size != 1x1 will always have 1 component (the block).
846 */
847 static INLINE unsigned
848 util_format_get_nr_components(enum pipe_format format)
849 {
850 const struct util_format_description *desc = util_format_description(format);
851 return desc->nr_channels;
852 }
853
854 /**
855 * Return the index of the first non-void channel
856 * -1 if no non-void channels
857 */
858 static INLINE int
859 util_format_get_first_non_void_channel(enum pipe_format format)
860 {
861 const struct util_format_description *desc = util_format_description(format);
862 int i;
863
864 for (i = 0; i < 4; i++)
865 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
866 break;
867
868 if (i == 4)
869 return -1;
870
871 return i;
872 }
873
874 /*
875 * Format access functions.
876 */
877
878 void
879 util_format_read_4f(enum pipe_format format,
880 float *dst, unsigned dst_stride,
881 const void *src, unsigned src_stride,
882 unsigned x, unsigned y, unsigned w, unsigned h);
883
884 void
885 util_format_write_4f(enum pipe_format format,
886 const float *src, unsigned src_stride,
887 void *dst, unsigned dst_stride,
888 unsigned x, unsigned y, unsigned w, unsigned h);
889
890 void
891 util_format_read_4ub(enum pipe_format format,
892 uint8_t *dst, unsigned dst_stride,
893 const void *src, unsigned src_stride,
894 unsigned x, unsigned y, unsigned w, unsigned h);
895
896 void
897 util_format_write_4ub(enum pipe_format format,
898 const uint8_t *src, unsigned src_stride,
899 void *dst, unsigned dst_stride,
900 unsigned x, unsigned y, unsigned w, unsigned h);
901
902 void
903 util_format_read_4ui(enum pipe_format format,
904 unsigned *dst, unsigned dst_stride,
905 const void *src, unsigned src_stride,
906 unsigned x, unsigned y, unsigned w, unsigned h);
907
908 void
909 util_format_write_4ui(enum pipe_format format,
910 const unsigned int *src, unsigned src_stride,
911 void *dst, unsigned dst_stride,
912 unsigned x, unsigned y, unsigned w, unsigned h);
913
914 void
915 util_format_read_4i(enum pipe_format format,
916 int *dst, unsigned dst_stride,
917 const void *src, unsigned src_stride,
918 unsigned x, unsigned y, unsigned w, unsigned h);
919
920 void
921 util_format_write_4i(enum pipe_format format,
922 const int *src, unsigned src_stride,
923 void *dst, unsigned dst_stride,
924 unsigned x, unsigned y, unsigned w, unsigned h);
925
926 /*
927 * Generic format conversion;
928 */
929
930 boolean
931 util_format_fits_8unorm(const struct util_format_description *format_desc);
932
933 void
934 util_format_translate(enum pipe_format dst_format,
935 void *dst, unsigned dst_stride,
936 unsigned dst_x, unsigned dst_y,
937 enum pipe_format src_format,
938 const void *src, unsigned src_stride,
939 unsigned src_x, unsigned src_y,
940 unsigned width, unsigned height);
941
942 /*
943 * Swizzle operations.
944 */
945
946 /* Compose two sets of swizzles.
947 * If V is a 4D vector and the function parameters represent functions that
948 * swizzle vector components, this holds:
949 * swz2(swz1(V)) = dst(V)
950 */
951 void util_format_compose_swizzles(const unsigned char swz1[4],
952 const unsigned char swz2[4],
953 unsigned char dst[4]);
954
955 void util_format_swizzle_4f(float *dst, const float *src,
956 const unsigned char swz[4]);
957
958 void util_format_unswizzle_4f(float *dst, const float *src,
959 const unsigned char swz[4]);
960
961 #ifdef __cplusplus
962 } // extern "C" {
963 #endif
964
965 #endif /* ! U_FORMAT_H */