Merge branch 'gallium-msaa'
[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 * Unpack pixel blocks to R32G32B32A32_FLOAT.
217 * Note: strides are in bytes.
218 *
219 * Only defined for non-depth-stencil formats.
220 */
221 void
222 (*unpack_rgba_float)(float *dst, unsigned dst_stride,
223 const uint8_t *src, unsigned src_stride,
224 unsigned width, unsigned height);
225
226 /**
227 * Pack pixel blocks from R32G32B32A32_FLOAT.
228 * Note: strides are in bytes.
229 *
230 * Only defined for non-depth-stencil formats.
231 */
232 void
233 (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
234 const float *src, unsigned src_stride,
235 unsigned width, unsigned height);
236
237 /**
238 * Fetch a single pixel (i, j) from a block.
239 *
240 * Only defined for non-depth-stencil formats.
241 */
242 void
243 (*fetch_rgba_float)(float *dst,
244 const uint8_t *src,
245 unsigned i, unsigned j);
246
247 /**
248 * Unpack pixels to Z32_UNORM.
249 * Note: strides are in bytes.
250 *
251 * Only defined for depth formats.
252 */
253 void
254 (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
255 const uint8_t *src, unsigned src_stride,
256 unsigned width, unsigned height);
257
258 /**
259 * Pack pixels from Z32_FLOAT.
260 * Note: strides are in bytes.
261 *
262 * Only defined for depth formats.
263 */
264 void
265 (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
266 const uint32_t *src, unsigned src_stride,
267 unsigned width, unsigned height);
268
269 /**
270 * Unpack pixels to Z32_FLOAT.
271 * Note: strides are in bytes.
272 *
273 * Only defined for depth formats.
274 */
275 void
276 (*unpack_z_float)(float *dst, unsigned dst_stride,
277 const uint8_t *src, unsigned src_stride,
278 unsigned width, unsigned height);
279
280 /**
281 * Pack pixels from Z32_FLOAT.
282 * Note: strides are in bytes.
283 *
284 * Only defined for depth formats.
285 */
286 void
287 (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
288 const float *src, unsigned src_stride,
289 unsigned width, unsigned height);
290
291 /**
292 * Unpack pixels to S8_USCALED.
293 * Note: strides are in bytes.
294 *
295 * Only defined for stencil formats.
296 */
297 void
298 (*unpack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
299 const uint8_t *src, unsigned src_stride,
300 unsigned width, unsigned height);
301
302 /**
303 * Pack pixels from S8_USCALED.
304 * Note: strides are in bytes.
305 *
306 * Only defined for stencil formats.
307 */
308 void
309 (*pack_s_8uscaled)(uint8_t *dst, unsigned dst_stride,
310 const uint8_t *src, unsigned src_stride,
311 unsigned width, unsigned height);
312
313 };
314
315
316 extern const struct util_format_description
317 util_format_description_table[];
318
319
320 const struct util_format_description *
321 util_format_description(enum pipe_format format);
322
323
324 /*
325 * Format query functions.
326 */
327
328 static INLINE const char *
329 util_format_name(enum pipe_format format)
330 {
331 const struct util_format_description *desc = util_format_description(format);
332
333 assert(desc);
334 if (!desc) {
335 return "PIPE_FORMAT_???";
336 }
337
338 return desc->name;
339 }
340
341 static INLINE const char *
342 util_format_short_name(enum pipe_format format)
343 {
344 const struct util_format_description *desc = util_format_description(format);
345
346 assert(desc);
347 if (!desc) {
348 return "???";
349 }
350
351 return desc->short_name;
352 }
353
354 /**
355 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
356 */
357 static INLINE boolean
358 util_format_is_plain(enum pipe_format format)
359 {
360 const struct util_format_description *desc = util_format_description(format);
361
362 if (!format) {
363 return FALSE;
364 }
365
366 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
367 }
368
369 static INLINE boolean
370 util_format_is_compressed(enum pipe_format format)
371 {
372 const struct util_format_description *desc = util_format_description(format);
373
374 assert(desc);
375 if (!desc) {
376 return FALSE;
377 }
378
379 switch (desc->layout) {
380 case UTIL_FORMAT_LAYOUT_S3TC:
381 case UTIL_FORMAT_LAYOUT_RGTC:
382 /* XXX add other formats in the future */
383 return TRUE;
384 default:
385 return FALSE;
386 }
387 }
388
389 static INLINE boolean
390 util_format_is_s3tc(enum pipe_format format)
391 {
392 const struct util_format_description *desc = util_format_description(format);
393
394 assert(desc);
395 if (!desc) {
396 return FALSE;
397 }
398
399 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
400 }
401
402 static INLINE boolean
403 util_format_is_depth_or_stencil(enum pipe_format format)
404 {
405 const struct util_format_description *desc = util_format_description(format);
406
407 assert(desc);
408 if (!desc) {
409 return FALSE;
410 }
411
412 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
413 }
414
415 static INLINE boolean
416 util_format_is_depth_and_stencil(enum pipe_format format)
417 {
418 const struct util_format_description *desc = util_format_description(format);
419
420 assert(desc);
421 if (!desc) {
422 return FALSE;
423 }
424
425 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
426 return FALSE;
427 }
428
429 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
430 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
431 }
432
433 /**
434 * Whether this format is a rgab8 variant.
435 *
436 * That is, any format that matches the
437 *
438 * PIPE_FORMAT_?8?8?8?8_UNORM
439 */
440 static INLINE boolean
441 util_format_is_rgba8_variant(const struct util_format_description *desc)
442 {
443 unsigned chan;
444
445 if(desc->block.width != 1 ||
446 desc->block.height != 1 ||
447 desc->block.bits != 32)
448 return FALSE;
449
450 for(chan = 0; chan < 4; ++chan) {
451 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
452 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
453 return FALSE;
454 if(desc->channel[chan].size != 8)
455 return FALSE;
456 }
457
458 return TRUE;
459 }
460
461
462 /**
463 * Return total bits needed for the pixel format per block.
464 */
465 static INLINE uint
466 util_format_get_blocksizebits(enum pipe_format format)
467 {
468 const struct util_format_description *desc = util_format_description(format);
469
470 assert(desc);
471 if (!desc) {
472 return 0;
473 }
474
475 return desc->block.bits;
476 }
477
478 /**
479 * Return bytes per block (not pixel) for the given format.
480 */
481 static INLINE uint
482 util_format_get_blocksize(enum pipe_format format)
483 {
484 uint bits = util_format_get_blocksizebits(format);
485
486 assert(bits % 8 == 0);
487
488 return bits / 8;
489 }
490
491 static INLINE uint
492 util_format_get_blockwidth(enum pipe_format format)
493 {
494 const struct util_format_description *desc = util_format_description(format);
495
496 assert(desc);
497 if (!desc) {
498 return 1;
499 }
500
501 return desc->block.width;
502 }
503
504 static INLINE uint
505 util_format_get_blockheight(enum pipe_format format)
506 {
507 const struct util_format_description *desc = util_format_description(format);
508
509 assert(desc);
510 if (!desc) {
511 return 1;
512 }
513
514 return desc->block.height;
515 }
516
517 static INLINE unsigned
518 util_format_get_nblocksx(enum pipe_format format,
519 unsigned x)
520 {
521 unsigned blockwidth = util_format_get_blockwidth(format);
522 return (x + blockwidth - 1) / blockwidth;
523 }
524
525 static INLINE unsigned
526 util_format_get_nblocksy(enum pipe_format format,
527 unsigned y)
528 {
529 unsigned blockheight = util_format_get_blockheight(format);
530 return (y + blockheight - 1) / blockheight;
531 }
532
533 static INLINE unsigned
534 util_format_get_nblocks(enum pipe_format format,
535 unsigned width,
536 unsigned height)
537 {
538 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
539 }
540
541 static INLINE size_t
542 util_format_get_stride(enum pipe_format format,
543 unsigned width)
544 {
545 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
546 }
547
548 static INLINE size_t
549 util_format_get_2d_size(enum pipe_format format,
550 size_t stride,
551 unsigned height)
552 {
553 return util_format_get_nblocksy(format, height) * stride;
554 }
555
556 static INLINE uint
557 util_format_get_component_bits(enum pipe_format format,
558 enum util_format_colorspace colorspace,
559 uint component)
560 {
561 const struct util_format_description *desc = util_format_description(format);
562 enum util_format_colorspace desc_colorspace;
563
564 assert(format);
565 if (!format) {
566 return 0;
567 }
568
569 assert(component < 4);
570
571 /* Treat RGB and SRGB as equivalent. */
572 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
573 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
574 }
575 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
576 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
577 } else {
578 desc_colorspace = desc->colorspace;
579 }
580
581 if (desc_colorspace != colorspace) {
582 return 0;
583 }
584
585 switch (desc->swizzle[component]) {
586 case UTIL_FORMAT_SWIZZLE_X:
587 return desc->channel[0].size;
588 case UTIL_FORMAT_SWIZZLE_Y:
589 return desc->channel[1].size;
590 case UTIL_FORMAT_SWIZZLE_Z:
591 return desc->channel[2].size;
592 case UTIL_FORMAT_SWIZZLE_W:
593 return desc->channel[3].size;
594 default:
595 return 0;
596 }
597 }
598
599 static INLINE boolean
600 util_format_has_alpha(enum pipe_format format)
601 {
602 const struct util_format_description *desc = util_format_description(format);
603
604 assert(format);
605 if (!format) {
606 return FALSE;
607 }
608
609 switch (desc->colorspace) {
610 case UTIL_FORMAT_COLORSPACE_RGB:
611 case UTIL_FORMAT_COLORSPACE_SRGB:
612 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
613 case UTIL_FORMAT_COLORSPACE_YUV:
614 return FALSE;
615 case UTIL_FORMAT_COLORSPACE_ZS:
616 return FALSE;
617 default:
618 assert(0);
619 return FALSE;
620 }
621 }
622
623 /**
624 * Return the number of components stored.
625 * Formats with block size != 1x1 will always have 1 component (the block).
626 */
627 static INLINE unsigned
628 util_format_get_nr_components(enum pipe_format format)
629 {
630 const struct util_format_description *desc = util_format_description(format);
631 return desc->nr_channels;
632 }
633
634 /*
635 * Format access functions.
636 */
637
638 void
639 util_format_read_4f(enum pipe_format format,
640 float *dst, unsigned dst_stride,
641 const void *src, unsigned src_stride,
642 unsigned x, unsigned y, unsigned w, unsigned h);
643
644 void
645 util_format_write_4f(enum pipe_format format,
646 const float *src, unsigned src_stride,
647 void *dst, unsigned dst_stride,
648 unsigned x, unsigned y, unsigned w, unsigned h);
649
650 void
651 util_format_read_4ub(enum pipe_format format,
652 uint8_t *dst, unsigned dst_stride,
653 const void *src, unsigned src_stride,
654 unsigned x, unsigned y, unsigned w, unsigned h);
655
656 void
657 util_format_write_4ub(enum pipe_format format,
658 const uint8_t *src, unsigned src_stride,
659 void *dst, unsigned dst_stride,
660 unsigned x, unsigned y, unsigned w, unsigned h);
661
662 /*
663 * Generic format conversion;
664 */
665
666 void
667 util_format_translate(enum pipe_format dst_format,
668 void *dst, unsigned dst_stride,
669 unsigned dst_x, unsigned dst_y,
670 enum pipe_format src_format,
671 const void *src, unsigned src_stride,
672 unsigned src_x, unsigned src_y,
673 unsigned width, unsigned height);
674
675 #ifdef __cplusplus
676 } // extern "C" {
677 #endif
678
679 #endif /* ! U_FORMAT_H */