util: Utility function to determine the channels that can be written in a color format.
[mesa.git] / src / gallium / auxiliary / util / u_format.h
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 Vmware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
31
32
33 #include "pipe/p_format.h"
34 #include "util/u_debug.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 /**
42 * Describe how to pack/unpack pixels into/from the prescribed format.
43 *
44 * XXX: This could be renamed to something like util_format_pack, or broke down
45 * in flags inside util_format_block that said exactly what we want.
46 */
47 enum util_format_layout {
48 /**
49 * Formats with util_format_block::width == util_format_block::height == 1
50 * that can be described as an ordinary data structure.
51 */
52 UTIL_FORMAT_LAYOUT_PLAIN = 0,
53
54 /**
55 * Formats with sub-sampled channels.
56 *
57 * This is for formats like 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 };
110
111
112 enum util_format_colorspace {
113 UTIL_FORMAT_COLORSPACE_RGB = 0,
114 UTIL_FORMAT_COLORSPACE_SRGB = 1,
115 UTIL_FORMAT_COLORSPACE_YUV = 2,
116 UTIL_FORMAT_COLORSPACE_ZS = 3
117 };
118
119
120 struct util_format_channel_description
121 {
122 unsigned type:6;
123 unsigned normalized:1;
124 unsigned size:9;
125 };
126
127
128 struct util_format_description
129 {
130 enum pipe_format format;
131
132 const char *name;
133
134 /**
135 * Short name, striped of the prefix, lower case.
136 */
137 const char *short_name;
138
139 /**
140 * Pixel block dimensions.
141 */
142 struct util_format_block block;
143
144 enum util_format_layout layout;
145
146 /**
147 * The number of channels.
148 */
149 unsigned nr_channels:3;
150
151 /**
152 * Whether all channels have the same number of (whole) bytes.
153 */
154 unsigned is_array:1;
155
156 /**
157 * Whether the pixel format can be described as a bitfield structure.
158 *
159 * In particular:
160 * - pixel depth must be 8, 16, or 32 bits;
161 * - all channels must be unsigned, signed, or void
162 */
163 unsigned is_bitmask:1;
164
165 /**
166 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
167 */
168 unsigned is_mixed:1;
169
170 /**
171 * Input channel description.
172 *
173 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
174 */
175 struct util_format_channel_description channel[4];
176
177 /**
178 * Output channel swizzle.
179 *
180 * The order is either:
181 * - RGBA
182 * - YUV(A)
183 * - ZS
184 * depending on the colorspace.
185 */
186 unsigned char swizzle[4];
187
188 /**
189 * Colorspace transformation.
190 */
191 enum util_format_colorspace colorspace;
192
193 /**
194 * Unpack pixel blocks to R8G8B8A8_UNORM.
195 * Note: strides are in bytes.
196 *
197 * Only defined for non-depth-stencil formats.
198 */
199 void
200 (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
201 const uint8_t *src, unsigned src_stride,
202 unsigned width, unsigned height);
203
204 /**
205 * Pack pixel blocks from R8G8B8A8_UNORM.
206 * Note: strides are in bytes.
207 *
208 * Only defined for non-depth-stencil formats.
209 */
210 void
211 (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
212 const uint8_t *src, unsigned src_stride,
213 unsigned width, unsigned height);
214
215 /**
216 * Fetch a single pixel (i, j) from a block.
217 *
218 * XXX: Only defined for a very few select formats.
219 */
220 void
221 (*fetch_rgba_8unorm)(uint8_t *dst,
222 const uint8_t *src,
223 unsigned i, unsigned j);
224
225 /**
226 * Unpack pixel blocks to R32G32B32A32_FLOAT.
227 * Note: strides are in bytes.
228 *
229 * Only defined for non-depth-stencil formats.
230 */
231 void
232 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
233 const uint8_t *src, unsigned src_stride,
234 unsigned width, unsigned height);
235
236 /**
237 * Pack pixel blocks from R32G32B32A32_FLOAT.
238 * Note: strides are in bytes.
239 *
240 * Only defined for non-depth-stencil formats.
241 */
242 void
243 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
244 const float *src, unsigned src_stride,
245 unsigned width, unsigned height);
246
247 /**
248 * Fetch a single pixel (i, j) from a block.
249 *
250 * Only defined for non-depth-stencil formats.
251 */
252 void
253 (*fetch_rgba_float)(float *dst,
254 const uint8_t *src,
255 unsigned i, unsigned j);
256
257 /**
258 * Unpack pixels to Z32_UNORM.
259 * Note: strides are in bytes.
260 *
261 * Only defined for depth formats.
262 */
263 void
264 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
265 const uint8_t *src, unsigned src_stride,
266 unsigned width, unsigned height);
267
268 /**
269 * Pack pixels from Z32_FLOAT.
270 * Note: strides are in bytes.
271 *
272 * Only defined for depth formats.
273 */
274 void
275 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
276 const uint32_t *src, unsigned src_stride,
277 unsigned width, unsigned height);
278
279 /**
280 * Unpack pixels to Z32_FLOAT.
281 * Note: strides are in bytes.
282 *
283 * Only defined for depth formats.
284 */
285 void
286 (*unpack_z_float)(float *dst, unsigned dst_stride,
287 const uint8_t *src, unsigned src_stride,
288 unsigned width, unsigned height);
289
290 /**
291 * Pack pixels from Z32_FLOAT.
292 * Note: strides are in bytes.
293 *
294 * Only defined for depth formats.
295 */
296 void
297 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
298 const float *src, unsigned src_stride,
299 unsigned width, unsigned height);
300
301 /**
302 * Unpack pixels to S8_USCALED.
303 * Note: strides are in bytes.
304 *
305 * Only defined for stencil formats.
306 */
307 void
308 (*unpack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
309 const uint8_t *src, unsigned src_stride,
310 unsigned width, unsigned height);
311
312 /**
313 * Pack pixels from S8_USCALED.
314 * Note: strides are in bytes.
315 *
316 * Only defined for stencil formats.
317 */
318 void
319 (*pack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
320 const uint8_t *src, unsigned src_stride,
321 unsigned width, unsigned height);
322
323 };
324
325
326 extern const struct util_format_description
327 util_format_description_table[];
328
329
330 const struct util_format_description *
331 util_format_description(enum pipe_format format);
332
333
334 /*
335 * Format query functions.
336 */
337
338 static INLINE const char *
339 util_format_name(enum pipe_format format)
340 {
341 const struct util_format_description *desc = util_format_description(format);
342
343 assert(desc);
344 if (!desc) {
345 return "PIPE_FORMAT_???";
346 }
347
348 return desc->name;
349 }
350
351 static INLINE const char *
352 util_format_short_name(enum pipe_format format)
353 {
354 const struct util_format_description *desc = util_format_description(format);
355
356 assert(desc);
357 if (!desc) {
358 return "???";
359 }
360
361 return desc->short_name;
362 }
363
364 /**
365 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
366 */
367 static INLINE boolean
368 util_format_is_plain(enum pipe_format format)
369 {
370 const struct util_format_description *desc = util_format_description(format);
371
372 if (!format) {
373 return FALSE;
374 }
375
376 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
377 }
378
379 static INLINE boolean
380 util_format_is_compressed(enum pipe_format format)
381 {
382 const struct util_format_description *desc = util_format_description(format);
383
384 assert(desc);
385 if (!desc) {
386 return FALSE;
387 }
388
389 switch (desc->layout) {
390 case UTIL_FORMAT_LAYOUT_S3TC:
391 case UTIL_FORMAT_LAYOUT_RGTC:
392 /* XXX add other formats in the future */
393 return TRUE;
394 default:
395 return FALSE;
396 }
397 }
398
399 static INLINE boolean
400 util_format_is_s3tc(enum pipe_format format)
401 {
402 const struct util_format_description *desc = util_format_description(format);
403
404 assert(desc);
405 if (!desc) {
406 return FALSE;
407 }
408
409 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
410 }
411
412 static INLINE boolean
413 util_format_is_depth_or_stencil(enum pipe_format format)
414 {
415 const struct util_format_description *desc = util_format_description(format);
416
417 assert(desc);
418 if (!desc) {
419 return FALSE;
420 }
421
422 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
423 }
424
425 static INLINE boolean
426 util_format_is_depth_and_stencil(enum pipe_format format)
427 {
428 const struct util_format_description *desc = util_format_description(format);
429
430 assert(desc);
431 if (!desc) {
432 return FALSE;
433 }
434
435 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
436 return FALSE;
437 }
438
439 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
440 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
441 }
442
443
444 /**
445 * Give the RGBA colormask of the channels that can be represented in this
446 * format.
447 *
448 * That is, the channels whose values are preserved.
449 */
450 static INLINE unsigned
451 util_format_colormask(const struct util_format_description *desc)
452 {
453 unsigned colormask;
454 unsigned chan;
455
456 switch (desc->colorspace) {
457 case UTIL_FORMAT_COLORSPACE_RGB:
458 case UTIL_FORMAT_COLORSPACE_SRGB:
459 case UTIL_FORMAT_COLORSPACE_YUV:
460 colormask = 0;
461 for (chan = 0; chan < 4; ++chan) {
462 if (desc->swizzle[chan] < 4) {
463 colormask |= (1 << chan);
464 }
465 }
466 return colormask;
467 case UTIL_FORMAT_COLORSPACE_ZS:
468 return 0;
469 default:
470 assert(0);
471 return 0;
472 }
473 }
474
475
476 /**
477 * Whether this format is a rgab8 variant.
478 *
479 * That is, any format that matches the
480 *
481 * PIPE_FORMAT_?8?8?8?8_UNORM
482 */
483 static INLINE boolean
484 util_format_is_rgba8_variant(const struct util_format_description *desc)
485 {
486 unsigned chan;
487
488 if(desc->block.width != 1 ||
489 desc->block.height != 1 ||
490 desc->block.bits != 32)
491 return FALSE;
492
493 for(chan = 0; chan < 4; ++chan) {
494 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
495 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
496 return FALSE;
497 if(desc->channel[chan].size != 8)
498 return FALSE;
499 }
500
501 return TRUE;
502 }
503
504
505 /**
506 * Return total bits needed for the pixel format per block.
507 */
508 static INLINE uint
509 util_format_get_blocksizebits(enum pipe_format format)
510 {
511 const struct util_format_description *desc = util_format_description(format);
512
513 assert(desc);
514 if (!desc) {
515 return 0;
516 }
517
518 return desc->block.bits;
519 }
520
521 /**
522 * Return bytes per block (not pixel) for the given format.
523 */
524 static INLINE uint
525 util_format_get_blocksize(enum pipe_format format)
526 {
527 uint bits = util_format_get_blocksizebits(format);
528
529 assert(bits % 8 == 0);
530
531 return bits / 8;
532 }
533
534 static INLINE uint
535 util_format_get_blockwidth(enum pipe_format format)
536 {
537 const struct util_format_description *desc = util_format_description(format);
538
539 assert(desc);
540 if (!desc) {
541 return 1;
542 }
543
544 return desc->block.width;
545 }
546
547 static INLINE uint
548 util_format_get_blockheight(enum pipe_format format)
549 {
550 const struct util_format_description *desc = util_format_description(format);
551
552 assert(desc);
553 if (!desc) {
554 return 1;
555 }
556
557 return desc->block.height;
558 }
559
560 static INLINE unsigned
561 util_format_get_nblocksx(enum pipe_format format,
562 unsigned x)
563 {
564 unsigned blockwidth = util_format_get_blockwidth(format);
565 return (x + blockwidth - 1) / blockwidth;
566 }
567
568 static INLINE unsigned
569 util_format_get_nblocksy(enum pipe_format format,
570 unsigned y)
571 {
572 unsigned blockheight = util_format_get_blockheight(format);
573 return (y + blockheight - 1) / blockheight;
574 }
575
576 static INLINE unsigned
577 util_format_get_nblocks(enum pipe_format format,
578 unsigned width,
579 unsigned height)
580 {
581 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
582 }
583
584 static INLINE size_t
585 util_format_get_stride(enum pipe_format format,
586 unsigned width)
587 {
588 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
589 }
590
591 static INLINE size_t
592 util_format_get_2d_size(enum pipe_format format,
593 size_t stride,
594 unsigned height)
595 {
596 return util_format_get_nblocksy(format, height) * stride;
597 }
598
599 static INLINE uint
600 util_format_get_component_bits(enum pipe_format format,
601 enum util_format_colorspace colorspace,
602 uint component)
603 {
604 const struct util_format_description *desc = util_format_description(format);
605 enum util_format_colorspace desc_colorspace;
606
607 assert(format);
608 if (!format) {
609 return 0;
610 }
611
612 assert(component < 4);
613
614 /* Treat RGB and SRGB as equivalent. */
615 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
616 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
617 }
618 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
619 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
620 } else {
621 desc_colorspace = desc->colorspace;
622 }
623
624 if (desc_colorspace != colorspace) {
625 return 0;
626 }
627
628 switch (desc->swizzle[component]) {
629 case UTIL_FORMAT_SWIZZLE_X:
630 return desc->channel[0].size;
631 case UTIL_FORMAT_SWIZZLE_Y:
632 return desc->channel[1].size;
633 case UTIL_FORMAT_SWIZZLE_Z:
634 return desc->channel[2].size;
635 case UTIL_FORMAT_SWIZZLE_W:
636 return desc->channel[3].size;
637 default:
638 return 0;
639 }
640 }
641
642 static INLINE boolean
643 util_format_has_alpha(enum pipe_format format)
644 {
645 const struct util_format_description *desc = util_format_description(format);
646
647 assert(format);
648 if (!format) {
649 return FALSE;
650 }
651
652 switch (desc->colorspace) {
653 case UTIL_FORMAT_COLORSPACE_RGB:
654 case UTIL_FORMAT_COLORSPACE_SRGB:
655 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
656 case UTIL_FORMAT_COLORSPACE_YUV:
657 return FALSE;
658 case UTIL_FORMAT_COLORSPACE_ZS:
659 return FALSE;
660 default:
661 assert(0);
662 return FALSE;
663 }
664 }
665
666 /**
667 * Return the matching SRGB format, or PIPE_FORMAT_NONE if none.
668 */
669 static INLINE enum pipe_format
670 util_format_srgb(enum pipe_format format)
671 {
672 switch (format) {
673 case PIPE_FORMAT_L8_UNORM:
674 return PIPE_FORMAT_L8_SRGB;
675 case PIPE_FORMAT_L8A8_UNORM:
676 return PIPE_FORMAT_L8A8_SRGB;
677 case PIPE_FORMAT_R8G8B8_UNORM:
678 return PIPE_FORMAT_R8G8B8_SRGB;
679 case PIPE_FORMAT_A8B8G8R8_UNORM:
680 return PIPE_FORMAT_A8B8G8R8_SRGB;
681 case PIPE_FORMAT_X8B8G8R8_UNORM:
682 return PIPE_FORMAT_X8B8G8R8_SRGB;
683 case PIPE_FORMAT_B8G8R8A8_UNORM:
684 return PIPE_FORMAT_B8G8R8A8_SRGB;
685 case PIPE_FORMAT_B8G8R8X8_UNORM:
686 return PIPE_FORMAT_B8G8R8X8_SRGB;
687 case PIPE_FORMAT_A8R8G8B8_UNORM:
688 return PIPE_FORMAT_A8R8G8B8_SRGB;
689 case PIPE_FORMAT_X8R8G8B8_UNORM:
690 return PIPE_FORMAT_X8R8G8B8_SRGB;
691 case PIPE_FORMAT_DXT1_RGB:
692 return PIPE_FORMAT_DXT1_SRGB;
693 case PIPE_FORMAT_DXT1_RGBA:
694 return PIPE_FORMAT_DXT1_SRGBA;
695 case PIPE_FORMAT_DXT3_RGBA:
696 return PIPE_FORMAT_DXT3_SRGBA;
697 case PIPE_FORMAT_DXT5_RGBA:
698 return PIPE_FORMAT_DXT5_SRGBA;
699 default:
700 return PIPE_FORMAT_NONE;
701 }
702 }
703
704 /**
705 * Return the number of components stored.
706 * Formats with block size != 1x1 will always have 1 component (the block).
707 */
708 static INLINE unsigned
709 util_format_get_nr_components(enum pipe_format format)
710 {
711 const struct util_format_description *desc = util_format_description(format);
712 return desc->nr_channels;
713 }
714
715 /*
716 * Format access functions.
717 */
718
719 void
720 util_format_read_4f(enum pipe_format format,
721 float *dst, unsigned dst_stride,
722 const void *src, unsigned src_stride,
723 unsigned x, unsigned y, unsigned w, unsigned h);
724
725 void
726 util_format_write_4f(enum pipe_format format,
727 const float *src, unsigned src_stride,
728 void *dst, unsigned dst_stride,
729 unsigned x, unsigned y, unsigned w, unsigned h);
730
731 void
732 util_format_read_4ub(enum pipe_format format,
733 uint8_t *dst, unsigned dst_stride,
734 const void *src, unsigned src_stride,
735 unsigned x, unsigned y, unsigned w, unsigned h);
736
737 void
738 util_format_write_4ub(enum pipe_format format,
739 const uint8_t *src, unsigned src_stride,
740 void *dst, unsigned dst_stride,
741 unsigned x, unsigned y, unsigned w, unsigned h);
742
743 /*
744 * Generic format conversion;
745 */
746
747 boolean
748 util_format_fits_8unorm(const struct util_format_description *format_desc);
749
750 void
751 util_format_translate(enum pipe_format dst_format,
752 void *dst, unsigned dst_stride,
753 unsigned dst_x, unsigned dst_y,
754 enum pipe_format src_format,
755 const void *src, unsigned src_stride,
756 unsigned src_x, unsigned src_y,
757 unsigned width, unsigned height);
758
759 #ifdef __cplusplus
760 } // extern "C" {
761 #endif
762
763 #endif /* ! U_FORMAT_H */