u_format: add inline helper to find first non void channel
[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 * Everything else that doesn't fit in any of the above layouts.
74 */
75 UTIL_FORMAT_LAYOUT_OTHER = 6
76 };
77
78
79 struct util_format_block
80 {
81 /** Block width in pixels */
82 unsigned width;
83
84 /** Block height in pixels */
85 unsigned height;
86
87 /** Block size in bits */
88 unsigned bits;
89 };
90
91
92 enum util_format_type {
93 UTIL_FORMAT_TYPE_VOID = 0,
94 UTIL_FORMAT_TYPE_UNSIGNED = 1,
95 UTIL_FORMAT_TYPE_SIGNED = 2,
96 UTIL_FORMAT_TYPE_FIXED = 3,
97 UTIL_FORMAT_TYPE_FLOAT = 4
98 };
99
100
101 enum util_format_swizzle {
102 UTIL_FORMAT_SWIZZLE_X = 0,
103 UTIL_FORMAT_SWIZZLE_Y = 1,
104 UTIL_FORMAT_SWIZZLE_Z = 2,
105 UTIL_FORMAT_SWIZZLE_W = 3,
106 UTIL_FORMAT_SWIZZLE_0 = 4,
107 UTIL_FORMAT_SWIZZLE_1 = 5,
108 UTIL_FORMAT_SWIZZLE_NONE = 6,
109 UTIL_FORMAT_SWIZZLE_MAX = 7 /**< Number of enums counter (must be last) */
110 };
111
112
113 enum util_format_colorspace {
114 UTIL_FORMAT_COLORSPACE_RGB = 0,
115 UTIL_FORMAT_COLORSPACE_SRGB = 1,
116 UTIL_FORMAT_COLORSPACE_YUV = 2,
117 UTIL_FORMAT_COLORSPACE_ZS = 3
118 };
119
120
121 struct util_format_channel_description
122 {
123 unsigned type:6; /**< UTIL_FORMAT_TYPE_x */
124 unsigned normalized:1;
125 unsigned size:9; /**< bits per channel */
126 };
127
128
129 struct util_format_description
130 {
131 enum pipe_format format;
132
133 const char *name;
134
135 /**
136 * Short name, striped of the prefix, lower case.
137 */
138 const char *short_name;
139
140 /**
141 * Pixel block dimensions.
142 */
143 struct util_format_block block;
144
145 enum util_format_layout layout;
146
147 /**
148 * The number of channels.
149 */
150 unsigned nr_channels:3;
151
152 /**
153 * Whether all channels have the same number of (whole) bytes.
154 */
155 unsigned is_array:1;
156
157 /**
158 * Whether the pixel format can be described as a bitfield structure.
159 *
160 * In particular:
161 * - pixel depth must be 8, 16, or 32 bits;
162 * - all channels must be unsigned, signed, or void
163 */
164 unsigned is_bitmask:1;
165
166 /**
167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
168 */
169 unsigned is_mixed:1;
170
171 /**
172 * Input channel description.
173 *
174 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
175 */
176 struct util_format_channel_description channel[4];
177
178 /**
179 * Output channel swizzle.
180 *
181 * The order is either:
182 * - RGBA
183 * - YUV(A)
184 * - ZS
185 * depending on the colorspace.
186 */
187 unsigned char swizzle[4];
188
189 /**
190 * Colorspace transformation.
191 */
192 enum util_format_colorspace colorspace;
193
194 /**
195 * Unpack pixel blocks to R8G8B8A8_UNORM.
196 * Note: strides are in bytes.
197 *
198 * Only defined for non-depth-stencil formats.
199 */
200 void
201 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
202 const uint8_t *src, unsigned src_stride,
203 unsigned width, unsigned height);
204
205 /**
206 * Pack pixel blocks from R8G8B8A8_UNORM.
207 * Note: strides are in bytes.
208 *
209 * Only defined for non-depth-stencil formats.
210 */
211 void
212 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
213 const uint8_t *src, unsigned src_stride,
214 unsigned width, unsigned height);
215
216 /**
217 * Fetch a single pixel (i, j) from a block.
218 *
219 * XXX: Only defined for a very few select formats.
220 */
221 void
222 (*fetch_rgba_8unorm)(uint8_t *dst,
223 const uint8_t *src,
224 unsigned i, unsigned j);
225
226 /**
227 * Unpack pixel blocks to R32G32B32A32_FLOAT.
228 * Note: strides are in bytes.
229 *
230 * Only defined for non-depth-stencil formats.
231 */
232 void
233 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
234 const uint8_t *src, unsigned src_stride,
235 unsigned width, unsigned height);
236
237 /**
238 * Pack pixel blocks from R32G32B32A32_FLOAT.
239 * Note: strides are in bytes.
240 *
241 * Only defined for non-depth-stencil formats.
242 */
243 void
244 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
245 const float *src, unsigned src_stride,
246 unsigned width, unsigned height);
247
248 /**
249 * Fetch a single pixel (i, j) from a block.
250 *
251 * Only defined for non-depth-stencil formats.
252 */
253 void
254 (*fetch_rgba_float)(float *dst,
255 const uint8_t *src,
256 unsigned i, unsigned j);
257
258 /**
259 * Unpack pixels to Z32_UNORM.
260 * Note: strides are in bytes.
261 *
262 * Only defined for depth formats.
263 */
264 void
265 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
266 const uint8_t *src, unsigned src_stride,
267 unsigned width, unsigned height);
268
269 /**
270 * Pack pixels from Z32_FLOAT.
271 * Note: strides are in bytes.
272 *
273 * Only defined for depth formats.
274 */
275 void
276 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
277 const uint32_t *src, unsigned src_stride,
278 unsigned width, unsigned height);
279
280 /**
281 * Unpack pixels to Z32_FLOAT.
282 * Note: strides are in bytes.
283 *
284 * Only defined for depth formats.
285 */
286 void
287 (*unpack_z_float)(float *dst, unsigned dst_stride,
288 const uint8_t *src, unsigned src_stride,
289 unsigned width, unsigned height);
290
291 /**
292 * Pack pixels from Z32_FLOAT.
293 * Note: strides are in bytes.
294 *
295 * Only defined for depth formats.
296 */
297 void
298 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
299 const float *src, unsigned src_stride,
300 unsigned width, unsigned height);
301
302 /**
303 * Unpack pixels to S8_USCALED.
304 * Note: strides are in bytes.
305 *
306 * Only defined for stencil formats.
307 */
308 void
309 (*unpack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
310 const uint8_t *src, unsigned src_stride,
311 unsigned width, unsigned height);
312
313 /**
314 * Pack pixels from S8_USCALED.
315 * Note: strides are in bytes.
316 *
317 * Only defined for stencil formats.
318 */
319 void
320 (*pack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
321 const uint8_t *src, unsigned src_stride,
322 unsigned width, unsigned height);
323
324 };
325
326
327 extern const struct util_format_description
328 util_format_description_table[];
329
330
331 const struct util_format_description *
332 util_format_description(enum pipe_format format);
333
334
335 /*
336 * Format query functions.
337 */
338
339 static INLINE const char *
340 util_format_name(enum pipe_format format)
341 {
342 const struct util_format_description *desc = util_format_description(format);
343
344 assert(desc);
345 if (!desc) {
346 return "PIPE_FORMAT_???";
347 }
348
349 return desc->name;
350 }
351
352 static INLINE const char *
353 util_format_short_name(enum pipe_format format)
354 {
355 const struct util_format_description *desc = util_format_description(format);
356
357 assert(desc);
358 if (!desc) {
359 return "???";
360 }
361
362 return desc->short_name;
363 }
364
365 /**
366 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
367 */
368 static INLINE boolean
369 util_format_is_plain(enum pipe_format format)
370 {
371 const struct util_format_description *desc = util_format_description(format);
372
373 if (!format) {
374 return FALSE;
375 }
376
377 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
378 }
379
380 static INLINE boolean
381 util_format_is_compressed(enum pipe_format format)
382 {
383 const struct util_format_description *desc = util_format_description(format);
384
385 assert(desc);
386 if (!desc) {
387 return FALSE;
388 }
389
390 switch (desc->layout) {
391 case UTIL_FORMAT_LAYOUT_S3TC:
392 case UTIL_FORMAT_LAYOUT_RGTC:
393 /* XXX add other formats in the future */
394 return TRUE;
395 default:
396 return FALSE;
397 }
398 }
399
400 static INLINE boolean
401 util_format_is_s3tc(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 FALSE;
408 }
409
410 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
411 }
412
413 static INLINE boolean
414 util_format_is_srgb(enum pipe_format format)
415 {
416 const struct util_format_description *desc = util_format_description(format);
417 return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
418 }
419
420 static INLINE boolean
421 util_format_has_depth(const struct util_format_description *desc)
422 {
423 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
424 desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
425 }
426
427 static INLINE boolean
428 util_format_has_stencil(const struct util_format_description *desc)
429 {
430 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
431 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
432 }
433
434 static INLINE boolean
435 util_format_is_depth_or_stencil(enum pipe_format format)
436 {
437 const struct util_format_description *desc = util_format_description(format);
438
439 assert(desc);
440 if (!desc) {
441 return FALSE;
442 }
443
444 return util_format_has_depth(desc) ||
445 util_format_has_stencil(desc);
446 }
447
448 static INLINE boolean
449 util_format_is_depth_and_stencil(enum pipe_format format)
450 {
451 const struct util_format_description *desc = util_format_description(format);
452
453 assert(desc);
454 if (!desc) {
455 return FALSE;
456 }
457
458 return util_format_has_depth(desc) &&
459 util_format_has_stencil(desc);
460 }
461
462
463 /**
464 * Give the RGBA colormask of the channels that can be represented in this
465 * format.
466 *
467 * That is, the channels whose values are preserved.
468 */
469 static INLINE unsigned
470 util_format_colormask(const struct util_format_description *desc)
471 {
472 unsigned colormask;
473 unsigned chan;
474
475 switch (desc->colorspace) {
476 case UTIL_FORMAT_COLORSPACE_RGB:
477 case UTIL_FORMAT_COLORSPACE_SRGB:
478 case UTIL_FORMAT_COLORSPACE_YUV:
479 colormask = 0;
480 for (chan = 0; chan < 4; ++chan) {
481 if (desc->swizzle[chan] < 4) {
482 colormask |= (1 << chan);
483 }
484 }
485 return colormask;
486 case UTIL_FORMAT_COLORSPACE_ZS:
487 return 0;
488 default:
489 assert(0);
490 return 0;
491 }
492 }
493
494
495 boolean
496 util_format_is_float(enum pipe_format format);
497
498
499 boolean
500 util_format_is_rgb_no_alpha(enum pipe_format format);
501
502
503 boolean
504 util_format_is_luminance(enum pipe_format format);
505
506
507 boolean
508 util_format_is_luminance_alpha(enum pipe_format format);
509
510
511 boolean
512 util_format_is_intensity(enum pipe_format format);
513
514
515 /**
516 * Whether the src format can be blitted to destation format with a simple
517 * memcpy.
518 */
519 boolean
520 util_is_format_compatible(const struct util_format_description *src_desc,
521 const struct util_format_description *dst_desc);
522
523 /**
524 * Whether the format is supported by Gallium for the given bindings.
525 * This covers S3TC textures and floating-point render targets.
526 */
527 boolean
528 util_format_is_supported(enum pipe_format format, unsigned bind);
529
530 /**
531 * Whether this format is a rgab8 variant.
532 *
533 * That is, any format that matches the
534 *
535 * PIPE_FORMAT_?8?8?8?8_UNORM
536 */
537 static INLINE boolean
538 util_format_is_rgba8_variant(const struct util_format_description *desc)
539 {
540 unsigned chan;
541
542 if(desc->block.width != 1 ||
543 desc->block.height != 1 ||
544 desc->block.bits != 32)
545 return FALSE;
546
547 for(chan = 0; chan < 4; ++chan) {
548 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
549 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
550 return FALSE;
551 if(desc->channel[chan].size != 8)
552 return FALSE;
553 }
554
555 return TRUE;
556 }
557
558
559 /**
560 * Return total bits needed for the pixel format per block.
561 */
562 static INLINE uint
563 util_format_get_blocksizebits(enum pipe_format format)
564 {
565 const struct util_format_description *desc = util_format_description(format);
566
567 assert(desc);
568 if (!desc) {
569 return 0;
570 }
571
572 return desc->block.bits;
573 }
574
575 /**
576 * Return bytes per block (not pixel) for the given format.
577 */
578 static INLINE uint
579 util_format_get_blocksize(enum pipe_format format)
580 {
581 uint bits = util_format_get_blocksizebits(format);
582
583 assert(bits % 8 == 0);
584
585 return bits / 8;
586 }
587
588 static INLINE uint
589 util_format_get_blockwidth(enum pipe_format format)
590 {
591 const struct util_format_description *desc = util_format_description(format);
592
593 assert(desc);
594 if (!desc) {
595 return 1;
596 }
597
598 return desc->block.width;
599 }
600
601 static INLINE uint
602 util_format_get_blockheight(enum pipe_format format)
603 {
604 const struct util_format_description *desc = util_format_description(format);
605
606 assert(desc);
607 if (!desc) {
608 return 1;
609 }
610
611 return desc->block.height;
612 }
613
614 static INLINE unsigned
615 util_format_get_nblocksx(enum pipe_format format,
616 unsigned x)
617 {
618 unsigned blockwidth = util_format_get_blockwidth(format);
619 return (x + blockwidth - 1) / blockwidth;
620 }
621
622 static INLINE unsigned
623 util_format_get_nblocksy(enum pipe_format format,
624 unsigned y)
625 {
626 unsigned blockheight = util_format_get_blockheight(format);
627 return (y + blockheight - 1) / blockheight;
628 }
629
630 static INLINE unsigned
631 util_format_get_nblocks(enum pipe_format format,
632 unsigned width,
633 unsigned height)
634 {
635 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
636 }
637
638 static INLINE size_t
639 util_format_get_stride(enum pipe_format format,
640 unsigned width)
641 {
642 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
643 }
644
645 static INLINE size_t
646 util_format_get_2d_size(enum pipe_format format,
647 size_t stride,
648 unsigned height)
649 {
650 return util_format_get_nblocksy(format, height) * stride;
651 }
652
653 static INLINE uint
654 util_format_get_component_bits(enum pipe_format format,
655 enum util_format_colorspace colorspace,
656 uint component)
657 {
658 const struct util_format_description *desc = util_format_description(format);
659 enum util_format_colorspace desc_colorspace;
660
661 assert(format);
662 if (!format) {
663 return 0;
664 }
665
666 assert(component < 4);
667
668 /* Treat RGB and SRGB as equivalent. */
669 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
670 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
671 }
672 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
673 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
674 } else {
675 desc_colorspace = desc->colorspace;
676 }
677
678 if (desc_colorspace != colorspace) {
679 return 0;
680 }
681
682 switch (desc->swizzle[component]) {
683 case UTIL_FORMAT_SWIZZLE_X:
684 return desc->channel[0].size;
685 case UTIL_FORMAT_SWIZZLE_Y:
686 return desc->channel[1].size;
687 case UTIL_FORMAT_SWIZZLE_Z:
688 return desc->channel[2].size;
689 case UTIL_FORMAT_SWIZZLE_W:
690 return desc->channel[3].size;
691 default:
692 return 0;
693 }
694 }
695
696 static INLINE boolean
697 util_format_has_alpha(enum pipe_format format)
698 {
699 const struct util_format_description *desc = util_format_description(format);
700
701 assert(format);
702 if (!format) {
703 return FALSE;
704 }
705
706 switch (desc->colorspace) {
707 case UTIL_FORMAT_COLORSPACE_RGB:
708 case UTIL_FORMAT_COLORSPACE_SRGB:
709 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
710 case UTIL_FORMAT_COLORSPACE_YUV:
711 return FALSE;
712 case UTIL_FORMAT_COLORSPACE_ZS:
713 return FALSE;
714 default:
715 assert(0);
716 return FALSE;
717 }
718 }
719
720 /**
721 * Given a linear RGB colorspace format, return the corresponding SRGB
722 * format, or PIPE_FORMAT_NONE if none.
723 */
724 static INLINE enum pipe_format
725 util_format_srgb(enum pipe_format format)
726 {
727 switch (format) {
728 case PIPE_FORMAT_L8_UNORM:
729 return PIPE_FORMAT_L8_SRGB;
730 case PIPE_FORMAT_L8A8_UNORM:
731 return PIPE_FORMAT_L8A8_SRGB;
732 case PIPE_FORMAT_R8G8B8_UNORM:
733 return PIPE_FORMAT_R8G8B8_SRGB;
734 case PIPE_FORMAT_A8B8G8R8_UNORM:
735 return PIPE_FORMAT_A8B8G8R8_SRGB;
736 case PIPE_FORMAT_X8B8G8R8_UNORM:
737 return PIPE_FORMAT_X8B8G8R8_SRGB;
738 case PIPE_FORMAT_B8G8R8A8_UNORM:
739 return PIPE_FORMAT_B8G8R8A8_SRGB;
740 case PIPE_FORMAT_B8G8R8X8_UNORM:
741 return PIPE_FORMAT_B8G8R8X8_SRGB;
742 case PIPE_FORMAT_A8R8G8B8_UNORM:
743 return PIPE_FORMAT_A8R8G8B8_SRGB;
744 case PIPE_FORMAT_X8R8G8B8_UNORM:
745 return PIPE_FORMAT_X8R8G8B8_SRGB;
746 case PIPE_FORMAT_DXT1_RGB:
747 return PIPE_FORMAT_DXT1_SRGB;
748 case PIPE_FORMAT_DXT1_RGBA:
749 return PIPE_FORMAT_DXT1_SRGBA;
750 case PIPE_FORMAT_DXT3_RGBA:
751 return PIPE_FORMAT_DXT3_SRGBA;
752 case PIPE_FORMAT_DXT5_RGBA:
753 return PIPE_FORMAT_DXT5_SRGBA;
754 default:
755 return PIPE_FORMAT_NONE;
756 }
757 }
758
759 /**
760 * Given an sRGB format, return the corresponding linear colorspace format.
761 * For non sRGB formats, return the format unchanged.
762 */
763 static INLINE enum pipe_format
764 util_format_linear(enum pipe_format format)
765 {
766 switch (format) {
767 case PIPE_FORMAT_L8_SRGB:
768 return PIPE_FORMAT_L8_UNORM;
769 case PIPE_FORMAT_L8A8_SRGB:
770 return PIPE_FORMAT_L8A8_UNORM;
771 case PIPE_FORMAT_R8G8B8_SRGB:
772 return PIPE_FORMAT_R8G8B8_UNORM;
773 case PIPE_FORMAT_A8B8G8R8_SRGB:
774 return PIPE_FORMAT_A8B8G8R8_UNORM;
775 case PIPE_FORMAT_X8B8G8R8_SRGB:
776 return PIPE_FORMAT_X8B8G8R8_UNORM;
777 case PIPE_FORMAT_B8G8R8A8_SRGB:
778 return PIPE_FORMAT_B8G8R8A8_UNORM;
779 case PIPE_FORMAT_B8G8R8X8_SRGB:
780 return PIPE_FORMAT_B8G8R8X8_UNORM;
781 case PIPE_FORMAT_A8R8G8B8_SRGB:
782 return PIPE_FORMAT_A8R8G8B8_UNORM;
783 case PIPE_FORMAT_X8R8G8B8_SRGB:
784 return PIPE_FORMAT_X8R8G8B8_UNORM;
785 case PIPE_FORMAT_DXT1_SRGB:
786 return PIPE_FORMAT_DXT1_RGB;
787 case PIPE_FORMAT_DXT1_SRGBA:
788 return PIPE_FORMAT_DXT1_RGBA;
789 case PIPE_FORMAT_DXT3_SRGBA:
790 return PIPE_FORMAT_DXT3_RGBA;
791 case PIPE_FORMAT_DXT5_SRGBA:
792 return PIPE_FORMAT_DXT5_RGBA;
793 default:
794 return format;
795 }
796 }
797
798 /**
799 * Return the number of components stored.
800 * Formats with block size != 1x1 will always have 1 component (the block).
801 */
802 static INLINE unsigned
803 util_format_get_nr_components(enum pipe_format format)
804 {
805 const struct util_format_description *desc = util_format_description(format);
806 return desc->nr_channels;
807 }
808
809 /**
810 * Return the index of the first non-void channel
811 * -1 if no non-void channels
812 */
813 static INLINE int
814 util_format_get_first_non_void_channel(enum pipe_format format)
815 {
816 const struct util_format_description *desc = util_format_description(format);
817 int i;
818
819 for (i = 0; i < 4; i++)
820 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
821 break;
822
823 if (i == 4)
824 return -1;
825
826 return i;
827 }
828
829 /*
830 * Format access functions.
831 */
832
833 void
834 util_format_read_4f(enum pipe_format format,
835 float *dst, unsigned dst_stride,
836 const void *src, unsigned src_stride,
837 unsigned x, unsigned y, unsigned w, unsigned h);
838
839 void
840 util_format_write_4f(enum pipe_format format,
841 const float *src, unsigned src_stride,
842 void *dst, unsigned dst_stride,
843 unsigned x, unsigned y, unsigned w, unsigned h);
844
845 void
846 util_format_read_4ub(enum pipe_format format,
847 uint8_t *dst, unsigned dst_stride,
848 const void *src, unsigned src_stride,
849 unsigned x, unsigned y, unsigned w, unsigned h);
850
851 void
852 util_format_write_4ub(enum pipe_format format,
853 const uint8_t *src, unsigned src_stride,
854 void *dst, unsigned dst_stride,
855 unsigned x, unsigned y, unsigned w, unsigned h);
856
857 /*
858 * Generic format conversion;
859 */
860
861 boolean
862 util_format_fits_8unorm(const struct util_format_description *format_desc);
863
864 void
865 util_format_translate(enum pipe_format dst_format,
866 void *dst, unsigned dst_stride,
867 unsigned dst_x, unsigned dst_y,
868 enum pipe_format src_format,
869 const void *src, unsigned src_stride,
870 unsigned src_x, unsigned src_y,
871 unsigned width, unsigned height);
872
873 /*
874 * Swizzle operations.
875 */
876
877 /* Compose two sets of swizzles.
878 * If V is a 4D vector and the function parameters represent functions that
879 * swizzle vector components, this holds:
880 * swz2(swz1(V)) = dst(V)
881 */
882 void util_format_compose_swizzles(const unsigned char swz1[4],
883 const unsigned char swz2[4],
884 unsigned char dst[4]);
885
886 void util_format_swizzle_4f(float *dst, const float *src,
887 const unsigned char swz[4]);
888
889 void util_format_unswizzle_4f(float *dst, const float *src,
890 const unsigned char swz[4]);
891
892 #ifdef __cplusplus
893 } // extern "C" {
894 #endif
895
896 #endif /* ! U_FORMAT_H */