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