Merge remote branch 'origin/master' into nv50-compiler
[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 * Whether this format is a rgab8 variant.
445 *
446 * That is, any format that matches the
447 *
448 * PIPE_FORMAT_?8?8?8?8_UNORM
449 */
450 static INLINE boolean
451 util_format_is_rgba8_variant(const struct util_format_description *desc)
452 {
453 unsigned chan;
454
455 if(desc->block.width != 1 ||
456 desc->block.height != 1 ||
457 desc->block.bits != 32)
458 return FALSE;
459
460 for(chan = 0; chan < 4; ++chan) {
461 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
462 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
463 return FALSE;
464 if(desc->channel[chan].size != 8)
465 return FALSE;
466 }
467
468 return TRUE;
469 }
470
471
472 /**
473 * Return total bits needed for the pixel format per block.
474 */
475 static INLINE uint
476 util_format_get_blocksizebits(enum pipe_format format)
477 {
478 const struct util_format_description *desc = util_format_description(format);
479
480 assert(desc);
481 if (!desc) {
482 return 0;
483 }
484
485 return desc->block.bits;
486 }
487
488 /**
489 * Return bytes per block (not pixel) for the given format.
490 */
491 static INLINE uint
492 util_format_get_blocksize(enum pipe_format format)
493 {
494 uint bits = util_format_get_blocksizebits(format);
495
496 assert(bits % 8 == 0);
497
498 return bits / 8;
499 }
500
501 static INLINE uint
502 util_format_get_blockwidth(enum pipe_format format)
503 {
504 const struct util_format_description *desc = util_format_description(format);
505
506 assert(desc);
507 if (!desc) {
508 return 1;
509 }
510
511 return desc->block.width;
512 }
513
514 static INLINE uint
515 util_format_get_blockheight(enum pipe_format format)
516 {
517 const struct util_format_description *desc = util_format_description(format);
518
519 assert(desc);
520 if (!desc) {
521 return 1;
522 }
523
524 return desc->block.height;
525 }
526
527 static INLINE unsigned
528 util_format_get_nblocksx(enum pipe_format format,
529 unsigned x)
530 {
531 unsigned blockwidth = util_format_get_blockwidth(format);
532 return (x + blockwidth - 1) / blockwidth;
533 }
534
535 static INLINE unsigned
536 util_format_get_nblocksy(enum pipe_format format,
537 unsigned y)
538 {
539 unsigned blockheight = util_format_get_blockheight(format);
540 return (y + blockheight - 1) / blockheight;
541 }
542
543 static INLINE unsigned
544 util_format_get_nblocks(enum pipe_format format,
545 unsigned width,
546 unsigned height)
547 {
548 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
549 }
550
551 static INLINE size_t
552 util_format_get_stride(enum pipe_format format,
553 unsigned width)
554 {
555 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
556 }
557
558 static INLINE size_t
559 util_format_get_2d_size(enum pipe_format format,
560 size_t stride,
561 unsigned height)
562 {
563 return util_format_get_nblocksy(format, height) * stride;
564 }
565
566 static INLINE uint
567 util_format_get_component_bits(enum pipe_format format,
568 enum util_format_colorspace colorspace,
569 uint component)
570 {
571 const struct util_format_description *desc = util_format_description(format);
572 enum util_format_colorspace desc_colorspace;
573
574 assert(format);
575 if (!format) {
576 return 0;
577 }
578
579 assert(component < 4);
580
581 /* Treat RGB and SRGB as equivalent. */
582 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
583 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
584 }
585 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
586 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
587 } else {
588 desc_colorspace = desc->colorspace;
589 }
590
591 if (desc_colorspace != colorspace) {
592 return 0;
593 }
594
595 switch (desc->swizzle[component]) {
596 case UTIL_FORMAT_SWIZZLE_X:
597 return desc->channel[0].size;
598 case UTIL_FORMAT_SWIZZLE_Y:
599 return desc->channel[1].size;
600 case UTIL_FORMAT_SWIZZLE_Z:
601 return desc->channel[2].size;
602 case UTIL_FORMAT_SWIZZLE_W:
603 return desc->channel[3].size;
604 default:
605 return 0;
606 }
607 }
608
609 static INLINE boolean
610 util_format_has_alpha(enum pipe_format format)
611 {
612 const struct util_format_description *desc = util_format_description(format);
613
614 assert(format);
615 if (!format) {
616 return FALSE;
617 }
618
619 switch (desc->colorspace) {
620 case UTIL_FORMAT_COLORSPACE_RGB:
621 case UTIL_FORMAT_COLORSPACE_SRGB:
622 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
623 case UTIL_FORMAT_COLORSPACE_YUV:
624 return FALSE;
625 case UTIL_FORMAT_COLORSPACE_ZS:
626 return FALSE;
627 default:
628 assert(0);
629 return FALSE;
630 }
631 }
632
633 /**
634 * Return the matching SRGB format, or PIPE_FORMAT_NONE if none.
635 */
636 static INLINE enum pipe_format
637 util_format_srgb(enum pipe_format format)
638 {
639 switch (format) {
640 case PIPE_FORMAT_L8_UNORM:
641 return PIPE_FORMAT_L8_SRGB;
642 case PIPE_FORMAT_L8A8_UNORM:
643 return PIPE_FORMAT_L8A8_SRGB;
644 case PIPE_FORMAT_R8G8B8_UNORM:
645 return PIPE_FORMAT_R8G8B8_SRGB;
646 case PIPE_FORMAT_A8B8G8R8_UNORM:
647 return PIPE_FORMAT_A8B8G8R8_SRGB;
648 case PIPE_FORMAT_X8B8G8R8_UNORM:
649 return PIPE_FORMAT_X8B8G8R8_SRGB;
650 case PIPE_FORMAT_B8G8R8A8_UNORM:
651 return PIPE_FORMAT_B8G8R8A8_SRGB;
652 case PIPE_FORMAT_B8G8R8X8_UNORM:
653 return PIPE_FORMAT_B8G8R8X8_SRGB;
654 case PIPE_FORMAT_A8R8G8B8_UNORM:
655 return PIPE_FORMAT_A8R8G8B8_SRGB;
656 case PIPE_FORMAT_X8R8G8B8_UNORM:
657 return PIPE_FORMAT_X8R8G8B8_SRGB;
658 case PIPE_FORMAT_DXT1_RGB:
659 return PIPE_FORMAT_DXT1_SRGB;
660 case PIPE_FORMAT_DXT1_RGBA:
661 return PIPE_FORMAT_DXT1_SRGBA;
662 case PIPE_FORMAT_DXT3_RGBA:
663 return PIPE_FORMAT_DXT3_SRGBA;
664 case PIPE_FORMAT_DXT5_RGBA:
665 return PIPE_FORMAT_DXT5_SRGBA;
666 default:
667 return PIPE_FORMAT_NONE;
668 }
669 }
670
671 /**
672 * Return the number of components stored.
673 * Formats with block size != 1x1 will always have 1 component (the block).
674 */
675 static INLINE unsigned
676 util_format_get_nr_components(enum pipe_format format)
677 {
678 const struct util_format_description *desc = util_format_description(format);
679 return desc->nr_channels;
680 }
681
682 /*
683 * Format access functions.
684 */
685
686 void
687 util_format_read_4f(enum pipe_format format,
688 float *dst, unsigned dst_stride,
689 const void *src, unsigned src_stride,
690 unsigned x, unsigned y, unsigned w, unsigned h);
691
692 void
693 util_format_write_4f(enum pipe_format format,
694 const float *src, unsigned src_stride,
695 void *dst, unsigned dst_stride,
696 unsigned x, unsigned y, unsigned w, unsigned h);
697
698 void
699 util_format_read_4ub(enum pipe_format format,
700 uint8_t *dst, unsigned dst_stride,
701 const void *src, unsigned src_stride,
702 unsigned x, unsigned y, unsigned w, unsigned h);
703
704 void
705 util_format_write_4ub(enum pipe_format format,
706 const uint8_t *src, unsigned src_stride,
707 void *dst, unsigned dst_stride,
708 unsigned x, unsigned y, unsigned w, unsigned h);
709
710 /*
711 * Generic format conversion;
712 */
713
714 boolean
715 util_format_fits_8unorm(const struct util_format_description *format_desc);
716
717 void
718 util_format_translate(enum pipe_format dst_format,
719 void *dst, unsigned dst_stride,
720 unsigned dst_x, unsigned dst_y,
721 enum pipe_format src_format,
722 const void *src, unsigned src_stride,
723 unsigned src_x, unsigned src_y,
724 unsigned width, unsigned height);
725
726 #ifdef __cplusplus
727 } // extern "C" {
728 #endif
729
730 #endif /* ! U_FORMAT_H */