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