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