Merge branch '7.8'
[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
195 extern const struct util_format_description
196 util_format_description_table[];
197
198
199 const struct util_format_description *
200 util_format_description(enum pipe_format format);
201
202
203 /*
204 * Format query functions.
205 */
206
207 static INLINE const char *
208 util_format_name(enum pipe_format format)
209 {
210 const struct util_format_description *desc = util_format_description(format);
211
212 assert(format);
213 if (!format) {
214 return "???";
215 }
216
217 return desc->name;
218 }
219
220 static INLINE boolean
221 util_format_is_s3tc(enum pipe_format format)
222 {
223 const struct util_format_description *desc = util_format_description(format);
224
225 assert(format);
226 if (!format) {
227 return FALSE;
228 }
229
230 return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
231 }
232
233 static INLINE boolean
234 util_format_is_depth_or_stencil(enum pipe_format format)
235 {
236 const struct util_format_description *desc = util_format_description(format);
237
238 assert(format);
239 if (!format) {
240 return FALSE;
241 }
242
243 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
244 }
245
246 static INLINE boolean
247 util_format_is_depth_and_stencil(enum pipe_format format)
248 {
249 const struct util_format_description *desc = util_format_description(format);
250
251 assert(format);
252 if (!format) {
253 return FALSE;
254 }
255
256 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
257 return FALSE;
258 }
259
260 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
261 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
262 }
263
264
265 /**
266 * Return total bits needed for the pixel format per block.
267 */
268 static INLINE uint
269 util_format_get_blocksizebits(enum pipe_format format)
270 {
271 const struct util_format_description *desc = util_format_description(format);
272
273 assert(format);
274 if (!format) {
275 return 0;
276 }
277
278 return desc->block.bits;
279 }
280
281 /**
282 * Return bytes per block (not pixel) for the given format.
283 */
284 static INLINE uint
285 util_format_get_blocksize(enum pipe_format format)
286 {
287 uint bits = util_format_get_blocksizebits(format);
288
289 assert(bits % 8 == 0);
290
291 return bits / 8;
292 }
293
294 static INLINE uint
295 util_format_get_blockwidth(enum pipe_format format)
296 {
297 const struct util_format_description *desc = util_format_description(format);
298
299 assert(format);
300 if (!format) {
301 return 1;
302 }
303
304 return desc->block.width;
305 }
306
307 static INLINE uint
308 util_format_get_blockheight(enum pipe_format format)
309 {
310 const struct util_format_description *desc = util_format_description(format);
311
312 assert(format);
313 if (!format) {
314 return 1;
315 }
316
317 return desc->block.height;
318 }
319
320 static INLINE unsigned
321 util_format_get_nblocksx(enum pipe_format format,
322 unsigned x)
323 {
324 unsigned blockwidth = util_format_get_blockwidth(format);
325 return (x + blockwidth - 1) / blockwidth;
326 }
327
328 static INLINE unsigned
329 util_format_get_nblocksy(enum pipe_format format,
330 unsigned y)
331 {
332 unsigned blockheight = util_format_get_blockheight(format);
333 return (y + blockheight - 1) / blockheight;
334 }
335
336 static INLINE unsigned
337 util_format_get_nblocks(enum pipe_format format,
338 unsigned width,
339 unsigned height)
340 {
341 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
342 }
343
344 static INLINE size_t
345 util_format_get_stride(enum pipe_format format,
346 unsigned width)
347 {
348 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
349 }
350
351 static INLINE size_t
352 util_format_get_2d_size(enum pipe_format format,
353 size_t stride,
354 unsigned height)
355 {
356 return util_format_get_nblocksy(format, height) * stride;
357 }
358
359 static INLINE uint
360 util_format_get_component_bits(enum pipe_format format,
361 enum util_format_colorspace colorspace,
362 uint component)
363 {
364 const struct util_format_description *desc = util_format_description(format);
365 enum util_format_colorspace desc_colorspace;
366
367 assert(format);
368 if (!format) {
369 return 0;
370 }
371
372 assert(component < 4);
373
374 /* Treat RGB and SRGB as equivalent. */
375 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
376 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
377 }
378 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
379 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
380 } else {
381 desc_colorspace = desc->colorspace;
382 }
383
384 if (desc_colorspace != colorspace) {
385 return 0;
386 }
387
388 switch (desc->swizzle[component]) {
389 case UTIL_FORMAT_SWIZZLE_X:
390 return desc->channel[0].size;
391 case UTIL_FORMAT_SWIZZLE_Y:
392 return desc->channel[1].size;
393 case UTIL_FORMAT_SWIZZLE_Z:
394 return desc->channel[2].size;
395 case UTIL_FORMAT_SWIZZLE_W:
396 return desc->channel[3].size;
397 default:
398 return 0;
399 }
400 }
401
402 static INLINE boolean
403 util_format_has_alpha(enum pipe_format format)
404 {
405 const struct util_format_description *desc = util_format_description(format);
406
407 assert(format);
408 if (!format) {
409 return FALSE;
410 }
411
412 switch (desc->colorspace) {
413 case UTIL_FORMAT_COLORSPACE_RGB:
414 case UTIL_FORMAT_COLORSPACE_SRGB:
415 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
416 case UTIL_FORMAT_COLORSPACE_YUV:
417 return FALSE;
418 case UTIL_FORMAT_COLORSPACE_ZS:
419 return FALSE;
420 default:
421 assert(0);
422 return FALSE;
423 }
424 }
425
426 /**
427 * Return the number of components stored.
428 * Formats with block size != 1x1 will always have 1 component (the block).
429 */
430 static INLINE unsigned
431 util_format_get_nr_components(enum pipe_format format)
432 {
433 const struct util_format_description *desc = util_format_description(format);
434 return desc->nr_channels;
435 }
436
437 /*
438 * Format access functions.
439 */
440
441 void
442 util_format_read_4f(enum pipe_format format,
443 float *dst, unsigned dst_stride,
444 const void *src, unsigned src_stride,
445 unsigned x, unsigned y, unsigned w, unsigned h);
446
447 void
448 util_format_write_4f(enum pipe_format format,
449 const float *src, unsigned src_stride,
450 void *dst, unsigned dst_stride,
451 unsigned x, unsigned y, unsigned w, unsigned h);
452
453 void
454 util_format_read_4ub(enum pipe_format format,
455 uint8_t *dst, unsigned dst_stride,
456 const void *src, unsigned src_stride,
457 unsigned x, unsigned y, unsigned w, unsigned h);
458
459 void
460 util_format_write_4ub(enum pipe_format format,
461 const uint8_t *src, unsigned src_stride,
462 void *dst, unsigned dst_stride,
463 unsigned x, unsigned y, unsigned w, unsigned h);
464
465 #ifdef __cplusplus
466 } // extern "C" {
467 #endif
468
469 #endif /* ! U_FORMAT_H */