Merge branch '7.8' into master
[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 * XXX: This could actually b
61 */
62 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
63
64 /**
65 * An unspecified compression algorithm.
66 */
67 UTIL_FORMAT_LAYOUT_COMPRESSED = 4
68 };
69
70
71 struct util_format_block
72 {
73 /** Block width in pixels */
74 unsigned width;
75
76 /** Block height in pixels */
77 unsigned height;
78
79 /** Block size in bits */
80 unsigned bits;
81 };
82
83
84 enum util_format_type {
85 UTIL_FORMAT_TYPE_VOID = 0,
86 UTIL_FORMAT_TYPE_UNSIGNED = 1,
87 UTIL_FORMAT_TYPE_SIGNED = 2,
88 UTIL_FORMAT_TYPE_FIXED = 3,
89 UTIL_FORMAT_TYPE_FLOAT = 4
90 };
91
92
93 enum util_format_swizzle {
94 UTIL_FORMAT_SWIZZLE_X = 0,
95 UTIL_FORMAT_SWIZZLE_Y = 1,
96 UTIL_FORMAT_SWIZZLE_Z = 2,
97 UTIL_FORMAT_SWIZZLE_W = 3,
98 UTIL_FORMAT_SWIZZLE_0 = 4,
99 UTIL_FORMAT_SWIZZLE_1 = 5,
100 UTIL_FORMAT_SWIZZLE_NONE = 6
101 };
102
103
104 enum util_format_colorspace {
105 UTIL_FORMAT_COLORSPACE_RGB = 0,
106 UTIL_FORMAT_COLORSPACE_SRGB = 1,
107 UTIL_FORMAT_COLORSPACE_YUV = 2,
108 UTIL_FORMAT_COLORSPACE_ZS = 3
109 };
110
111
112 struct util_format_channel_description
113 {
114 unsigned type:6;
115 unsigned normalized:1;
116 unsigned size:9;
117 };
118
119
120 struct util_format_description
121 {
122 enum pipe_format format;
123
124 const char *name;
125
126 /**
127 * Short name, striped of the prefix, lower case.
128 */
129 const char *short_name;
130
131 /**
132 * Pixel block dimensions.
133 */
134 struct util_format_block block;
135
136 enum util_format_layout layout;
137
138 /**
139 * The number of channels.
140 */
141 unsigned nr_channels:3;
142
143 /**
144 * Whether all channels have the same number of (whole) bytes.
145 */
146 unsigned is_array:1;
147
148 /**
149 * Whether the pixel format can be described as a bitfield structure.
150 *
151 * In particular:
152 * - pixel depth must be 8, 16, or 32 bits;
153 * - all channels must be unsigned, signed, or void
154 */
155 unsigned is_bitmask:1;
156
157 /**
158 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
159 */
160 unsigned is_mixed:1;
161
162 /**
163 * Input channel description.
164 *
165 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
166 */
167 struct util_format_channel_description channel[4];
168
169 /**
170 * Output channel swizzle.
171 *
172 * The order is either:
173 * - RGBA
174 * - YUV(A)
175 * - ZS
176 * depending on the colorspace.
177 */
178 unsigned char swizzle[4];
179
180 /**
181 * Colorspace transformation.
182 */
183 enum util_format_colorspace colorspace;
184 };
185
186
187 extern const struct util_format_description
188 util_format_description_table[];
189
190
191 const struct util_format_description *
192 util_format_description(enum pipe_format format);
193
194
195 /*
196 * Format query functions.
197 */
198
199 static INLINE const char *
200 util_format_name(enum pipe_format format)
201 {
202 const struct util_format_description *desc = util_format_description(format);
203
204 assert(format);
205 if (!format) {
206 return "???";
207 }
208
209 return desc->name;
210 }
211
212 static INLINE boolean
213 util_format_is_compressed(enum pipe_format format)
214 {
215 const struct util_format_description *desc = util_format_description(format);
216
217 assert(format);
218 if (!format) {
219 return FALSE;
220 }
221
222 return desc->layout == UTIL_FORMAT_LAYOUT_COMPRESSED ? TRUE : FALSE;
223 }
224
225 static INLINE boolean
226 util_format_is_depth_or_stencil(enum pipe_format format)
227 {
228 const struct util_format_description *desc = util_format_description(format);
229
230 assert(format);
231 if (!format) {
232 return FALSE;
233 }
234
235 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
236 }
237
238 static INLINE boolean
239 util_format_is_depth_and_stencil(enum pipe_format format)
240 {
241 const struct util_format_description *desc = util_format_description(format);
242
243 assert(format);
244 if (!format) {
245 return FALSE;
246 }
247
248 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
249 return FALSE;
250 }
251
252 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
253 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
254 }
255
256
257 /**
258 * Return total bits needed for the pixel format per block.
259 */
260 static INLINE uint
261 util_format_get_blocksizebits(enum pipe_format format)
262 {
263 const struct util_format_description *desc = util_format_description(format);
264
265 assert(format);
266 if (!format) {
267 return 0;
268 }
269
270 return desc->block.bits;
271 }
272
273 /**
274 * Return bytes per block (not pixel) for the given format.
275 */
276 static INLINE uint
277 util_format_get_blocksize(enum pipe_format format)
278 {
279 uint bits = util_format_get_blocksizebits(format);
280
281 assert(bits % 8 == 0);
282
283 return bits / 8;
284 }
285
286 static INLINE uint
287 util_format_get_blockwidth(enum pipe_format format)
288 {
289 const struct util_format_description *desc = util_format_description(format);
290
291 assert(format);
292 if (!format) {
293 return 1;
294 }
295
296 return desc->block.width;
297 }
298
299 static INLINE uint
300 util_format_get_blockheight(enum pipe_format format)
301 {
302 const struct util_format_description *desc = util_format_description(format);
303
304 assert(format);
305 if (!format) {
306 return 1;
307 }
308
309 return desc->block.height;
310 }
311
312 static INLINE unsigned
313 util_format_get_nblocksx(enum pipe_format format,
314 unsigned x)
315 {
316 unsigned blockwidth = util_format_get_blockwidth(format);
317 return (x + blockwidth - 1) / blockwidth;
318 }
319
320 static INLINE unsigned
321 util_format_get_nblocksy(enum pipe_format format,
322 unsigned y)
323 {
324 unsigned blockheight = util_format_get_blockheight(format);
325 return (y + blockheight - 1) / blockheight;
326 }
327
328 static INLINE unsigned
329 util_format_get_nblocks(enum pipe_format format,
330 unsigned width,
331 unsigned height)
332 {
333 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
334 }
335
336 static INLINE size_t
337 util_format_get_stride(enum pipe_format format,
338 unsigned width)
339 {
340 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
341 }
342
343 static INLINE size_t
344 util_format_get_2d_size(enum pipe_format format,
345 size_t stride,
346 unsigned height)
347 {
348 return util_format_get_nblocksy(format, height) * stride;
349 }
350
351 static INLINE uint
352 util_format_get_component_bits(enum pipe_format format,
353 enum util_format_colorspace colorspace,
354 uint component)
355 {
356 const struct util_format_description *desc = util_format_description(format);
357 enum util_format_colorspace desc_colorspace;
358
359 assert(format);
360 if (!format) {
361 return 0;
362 }
363
364 assert(component < 4);
365
366 /* Treat RGB and SRGB as equivalent. */
367 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
368 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
369 }
370 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
371 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
372 } else {
373 desc_colorspace = desc->colorspace;
374 }
375
376 if (desc_colorspace != colorspace) {
377 return 0;
378 }
379
380 switch (desc->swizzle[component]) {
381 case UTIL_FORMAT_SWIZZLE_X:
382 return desc->channel[0].size;
383 case UTIL_FORMAT_SWIZZLE_Y:
384 return desc->channel[1].size;
385 case UTIL_FORMAT_SWIZZLE_Z:
386 return desc->channel[2].size;
387 case UTIL_FORMAT_SWIZZLE_W:
388 return desc->channel[3].size;
389 default:
390 return 0;
391 }
392 }
393
394 static INLINE boolean
395 util_format_has_alpha(enum pipe_format format)
396 {
397 const struct util_format_description *desc = util_format_description(format);
398
399 assert(format);
400 if (!format) {
401 return FALSE;
402 }
403
404 switch (desc->colorspace) {
405 case UTIL_FORMAT_COLORSPACE_RGB:
406 case UTIL_FORMAT_COLORSPACE_SRGB:
407 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
408 case UTIL_FORMAT_COLORSPACE_YUV:
409 return FALSE;
410 case UTIL_FORMAT_COLORSPACE_ZS:
411 return FALSE;
412 default:
413 assert(0);
414 return FALSE;
415 }
416 }
417
418 /**
419 * Return the number of components stored.
420 * Formats with block size != 1x1 will always have 1 component (the block).
421 */
422 static INLINE unsigned
423 util_format_get_nr_components(enum pipe_format format)
424 {
425 const struct util_format_description *desc = util_format_description(format);
426 return desc->nr_channels;
427 }
428
429 /*
430 * Format access functions.
431 */
432
433 void
434 util_format_read_4f(enum pipe_format format,
435 float *dst, unsigned dst_stride,
436 const void *src, unsigned src_stride,
437 unsigned x, unsigned y, unsigned w, unsigned h);
438
439 void
440 util_format_write_4f(enum pipe_format format,
441 const float *src, unsigned src_stride,
442 void *dst, unsigned dst_stride,
443 unsigned x, unsigned y, unsigned w, unsigned h);
444
445 void
446 util_format_read_4ub(enum pipe_format format,
447 uint8_t *dst, unsigned dst_stride,
448 const void *src, unsigned src_stride,
449 unsigned x, unsigned y, unsigned w, unsigned h);
450
451 void
452 util_format_write_4ub(enum pipe_format format,
453 const uint8_t *src, unsigned src_stride,
454 void *dst, unsigned dst_stride,
455 unsigned x, unsigned y, unsigned w, unsigned h);
456
457 #ifdef __cplusplus
458 } // extern "C" {
459 #endif
460
461 #endif /* ! U_FORMAT_H */