gallium/util: add util_format_init that inits s3tc and util_half
[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 #include "util/u_inline_init.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 /**
43 * Describe how to pack/unpack pixels into/from the prescribed format.
44 *
45 * XXX: This could be renamed to something like util_format_pack, or broke down
46 * in flags inside util_format_block that said exactly what we want.
47 */
48 enum util_format_layout {
49 /**
50 * Formats with util_format_block::width == util_format_block::height == 1
51 * that can be described as an ordinary data structure.
52 */
53 UTIL_FORMAT_LAYOUT_PLAIN = 0,
54
55 /**
56 * Formats with sub-sampled channels.
57 *
58 * This is for formats like YV12 where there is less than one sample per
59 * pixel.
60 */
61 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
62
63 /**
64 * S3 Texture Compression formats.
65 */
66 UTIL_FORMAT_LAYOUT_S3TC = 4,
67
68 /**
69 * Red-Green Texture Compression formats.
70 */
71 UTIL_FORMAT_LAYOUT_RGTC = 5,
72
73 /**
74 * Everything else that doesn't fit in any of the above layouts.
75 */
76 UTIL_FORMAT_LAYOUT_OTHER = 6
77 };
78
79
80 struct util_format_block
81 {
82 /** Block width in pixels */
83 unsigned width;
84
85 /** Block height in pixels */
86 unsigned height;
87
88 /** Block size in bits */
89 unsigned bits;
90 };
91
92
93 enum util_format_type {
94 UTIL_FORMAT_TYPE_VOID = 0,
95 UTIL_FORMAT_TYPE_UNSIGNED = 1,
96 UTIL_FORMAT_TYPE_SIGNED = 2,
97 UTIL_FORMAT_TYPE_FIXED = 3,
98 UTIL_FORMAT_TYPE_FLOAT = 4
99 };
100
101
102 enum util_format_swizzle {
103 UTIL_FORMAT_SWIZZLE_X = 0,
104 UTIL_FORMAT_SWIZZLE_Y = 1,
105 UTIL_FORMAT_SWIZZLE_Z = 2,
106 UTIL_FORMAT_SWIZZLE_W = 3,
107 UTIL_FORMAT_SWIZZLE_0 = 4,
108 UTIL_FORMAT_SWIZZLE_1 = 5,
109 UTIL_FORMAT_SWIZZLE_NONE = 6
110 };
111
112
113 enum util_format_colorspace {
114 UTIL_FORMAT_COLORSPACE_RGB = 0,
115 UTIL_FORMAT_COLORSPACE_SRGB = 1,
116 UTIL_FORMAT_COLORSPACE_YUV = 2,
117 UTIL_FORMAT_COLORSPACE_ZS = 3
118 };
119
120
121 struct util_format_channel_description
122 {
123 unsigned type:6;
124 unsigned normalized:1;
125 unsigned size:9;
126 };
127
128
129 struct util_format_description
130 {
131 enum pipe_format format;
132
133 const char *name;
134
135 /**
136 * Short name, striped of the prefix, lower case.
137 */
138 const char *short_name;
139
140 /**
141 * Pixel block dimensions.
142 */
143 struct util_format_block block;
144
145 enum util_format_layout layout;
146
147 /**
148 * The number of channels.
149 */
150 unsigned nr_channels:3;
151
152 /**
153 * Whether all channels have the same number of (whole) bytes.
154 */
155 unsigned is_array:1;
156
157 /**
158 * Whether the pixel format can be described as a bitfield structure.
159 *
160 * In particular:
161 * - pixel depth must be 8, 16, or 32 bits;
162 * - all channels must be unsigned, signed, or void
163 */
164 unsigned is_bitmask:1;
165
166 /**
167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
168 */
169 unsigned is_mixed:1;
170
171 /**
172 * Input channel description.
173 *
174 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
175 */
176 struct util_format_channel_description channel[4];
177
178 /**
179 * Output channel swizzle.
180 *
181 * The order is either:
182 * - RGBA
183 * - YUV(A)
184 * - ZS
185 * depending on the colorspace.
186 */
187 unsigned char swizzle[4];
188
189 /**
190 * Colorspace transformation.
191 */
192 enum util_format_colorspace colorspace;
193
194 /**
195 * Unpack pixel blocks to R8G8B8A8_UNORM.
196 */
197 void
198 (*unpack_8unorm)(uint8_t *dst, unsigned dst_stride,
199 const uint8_t *src, unsigned src_stride,
200 unsigned width, unsigned height);
201
202 /**
203 * Pack pixel blocks from R8G8B8A8_UNORM.
204 */
205 void
206 (*pack_8unorm)(uint8_t *dst, unsigned dst_stride,
207 const uint8_t *src, unsigned src_stride,
208 unsigned width, unsigned height);
209
210 /**
211 * Unpack pixel blocks to R32G32B32A32_FLOAT.
212 */
213 void
214 (*unpack_float)(float *dst, unsigned dst_stride,
215 const uint8_t *src, unsigned src_stride,
216 unsigned width, unsigned height);
217
218 /**
219 * Pack pixel blocks from R32G32B32A32_FLOAT.
220 */
221 void
222 (*pack_float)(uint8_t *dst, unsigned dst_stride,
223 const float *src, unsigned src_stride,
224 unsigned width, unsigned height);
225
226 /**
227 * Fetch a single pixel (i, j) from a block.
228 */
229 void
230 (*fetch_float)(float *dst,
231 const uint8_t *src,
232 unsigned i, unsigned j);
233 };
234
235
236 extern const struct util_format_description
237 util_format_description_table[];
238
239
240 const struct util_format_description *
241 util_format_description(enum pipe_format format);
242
243
244 /*
245 * Format query functions.
246 */
247
248 static INLINE const char *
249 util_format_name(enum pipe_format format)
250 {
251 const struct util_format_description *desc = util_format_description(format);
252
253 assert(desc);
254 if (!desc) {
255 return "???";
256 }
257
258 return desc->name;
259 }
260
261 static INLINE boolean
262 util_format_is_s3tc(enum pipe_format format)
263 {
264 const struct util_format_description *desc = util_format_description(format);
265
266 assert(desc);
267 if (!desc) {
268 return FALSE;
269 }
270
271 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
272 }
273
274 static INLINE boolean
275 util_format_is_depth_or_stencil(enum pipe_format format)
276 {
277 const struct util_format_description *desc = util_format_description(format);
278
279 assert(desc);
280 if (!desc) {
281 return FALSE;
282 }
283
284 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
285 }
286
287 static INLINE boolean
288 util_format_is_depth_and_stencil(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 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
298 return FALSE;
299 }
300
301 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
302 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
303 }
304
305 /**
306 * Whether this format is a rgab8 variant.
307 *
308 * That is, any format that matches the
309 *
310 * PIPE_FORMAT_?8?8?8?8_UNORM
311 */
312 static INLINE boolean
313 util_format_is_rgba8_variant(const struct util_format_description *desc)
314 {
315 unsigned chan;
316
317 if(desc->block.width != 1 ||
318 desc->block.height != 1 ||
319 desc->block.bits != 32)
320 return FALSE;
321
322 for(chan = 0; chan < 4; ++chan) {
323 if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
324 desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
325 return FALSE;
326 if(desc->channel[chan].size != 8)
327 return FALSE;
328 }
329
330 return TRUE;
331 }
332
333
334 /**
335 * Return total bits needed for the pixel format per block.
336 */
337 static INLINE uint
338 util_format_get_blocksizebits(enum pipe_format format)
339 {
340 const struct util_format_description *desc = util_format_description(format);
341
342 assert(desc);
343 if (!desc) {
344 return 0;
345 }
346
347 return desc->block.bits;
348 }
349
350 /**
351 * Return bytes per block (not pixel) for the given format.
352 */
353 static INLINE uint
354 util_format_get_blocksize(enum pipe_format format)
355 {
356 uint bits = util_format_get_blocksizebits(format);
357
358 assert(bits % 8 == 0);
359
360 return bits / 8;
361 }
362
363 static INLINE uint
364 util_format_get_blockwidth(enum pipe_format format)
365 {
366 const struct util_format_description *desc = util_format_description(format);
367
368 assert(desc);
369 if (!desc) {
370 return 1;
371 }
372
373 return desc->block.width;
374 }
375
376 static INLINE uint
377 util_format_get_blockheight(enum pipe_format format)
378 {
379 const struct util_format_description *desc = util_format_description(format);
380
381 assert(desc);
382 if (!desc) {
383 return 1;
384 }
385
386 return desc->block.height;
387 }
388
389 static INLINE unsigned
390 util_format_get_nblocksx(enum pipe_format format,
391 unsigned x)
392 {
393 unsigned blockwidth = util_format_get_blockwidth(format);
394 return (x + blockwidth - 1) / blockwidth;
395 }
396
397 static INLINE unsigned
398 util_format_get_nblocksy(enum pipe_format format,
399 unsigned y)
400 {
401 unsigned blockheight = util_format_get_blockheight(format);
402 return (y + blockheight - 1) / blockheight;
403 }
404
405 static INLINE unsigned
406 util_format_get_nblocks(enum pipe_format format,
407 unsigned width,
408 unsigned height)
409 {
410 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
411 }
412
413 static INLINE size_t
414 util_format_get_stride(enum pipe_format format,
415 unsigned width)
416 {
417 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
418 }
419
420 static INLINE size_t
421 util_format_get_2d_size(enum pipe_format format,
422 size_t stride,
423 unsigned height)
424 {
425 return util_format_get_nblocksy(format, height) * stride;
426 }
427
428 static INLINE uint
429 util_format_get_component_bits(enum pipe_format format,
430 enum util_format_colorspace colorspace,
431 uint component)
432 {
433 const struct util_format_description *desc = util_format_description(format);
434 enum util_format_colorspace desc_colorspace;
435
436 assert(format);
437 if (!format) {
438 return 0;
439 }
440
441 assert(component < 4);
442
443 /* Treat RGB and SRGB as equivalent. */
444 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
445 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
446 }
447 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
448 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
449 } else {
450 desc_colorspace = desc->colorspace;
451 }
452
453 if (desc_colorspace != colorspace) {
454 return 0;
455 }
456
457 switch (desc->swizzle[component]) {
458 case UTIL_FORMAT_SWIZZLE_X:
459 return desc->channel[0].size;
460 case UTIL_FORMAT_SWIZZLE_Y:
461 return desc->channel[1].size;
462 case UTIL_FORMAT_SWIZZLE_Z:
463 return desc->channel[2].size;
464 case UTIL_FORMAT_SWIZZLE_W:
465 return desc->channel[3].size;
466 default:
467 return 0;
468 }
469 }
470
471 static INLINE boolean
472 util_format_has_alpha(enum pipe_format format)
473 {
474 const struct util_format_description *desc = util_format_description(format);
475
476 assert(format);
477 if (!format) {
478 return FALSE;
479 }
480
481 switch (desc->colorspace) {
482 case UTIL_FORMAT_COLORSPACE_RGB:
483 case UTIL_FORMAT_COLORSPACE_SRGB:
484 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
485 case UTIL_FORMAT_COLORSPACE_YUV:
486 return FALSE;
487 case UTIL_FORMAT_COLORSPACE_ZS:
488 return FALSE;
489 default:
490 assert(0);
491 return FALSE;
492 }
493 }
494
495 /**
496 * Return the number of components stored.
497 * Formats with block size != 1x1 will always have 1 component (the block).
498 */
499 static INLINE unsigned
500 util_format_get_nr_components(enum pipe_format format)
501 {
502 const struct util_format_description *desc = util_format_description(format);
503 return desc->nr_channels;
504 }
505
506 /*
507 * Format access functions.
508 */
509
510 void
511 util_format_read_4f(enum pipe_format format,
512 float *dst, unsigned dst_stride,
513 const void *src, unsigned src_stride,
514 unsigned x, unsigned y, unsigned w, unsigned h);
515
516 void
517 util_format_write_4f(enum pipe_format format,
518 const float *src, unsigned src_stride,
519 void *dst, unsigned dst_stride,
520 unsigned x, unsigned y, unsigned w, unsigned h);
521
522 void
523 util_format_read_4ub(enum pipe_format format,
524 uint8_t *dst, unsigned dst_stride,
525 const void *src, unsigned src_stride,
526 unsigned x, unsigned y, unsigned w, unsigned h);
527
528 void
529 util_format_write_4ub(enum pipe_format format,
530 const uint8_t *src, unsigned src_stride,
531 void *dst, unsigned dst_stride,
532 unsigned x, unsigned y, unsigned w, unsigned h);
533
534 UTIL_INLINE_INIT(util_format);
535
536 #ifdef __cplusplus
537 } // extern "C" {
538 #endif
539
540 #endif /* ! U_FORMAT_H */