7054a07b2dbdfa98a794d27d188fb9fc703c8781
[mesa.git] / src / gallium / auxiliary / util / u_format.h
1 /**************************************************************************
2 *
3 * Copyright 2009 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 best pack/unpack pixels into/from the prescribed format.
43 *
44 * These are used for automatic code generation of pixel packing and unpacking
45 * routines (in compile time, e.g., u_format_access.py, or in runtime, like
46 * llvmpipe does).
47 *
48 * Thumb rule is: if you're not code generating pixel packing/unpacking then
49 * these are irrelevant for you.
50 *
51 * Note that this can be deduced from other values in util_format_description
52 * structure. This is by design, to make code generation of pixel
53 * packing/unpacking/sampling routines simple and efficient.
54 *
55 * XXX: This should be renamed to something like util_format_pack.
56 */
57 enum util_format_layout {
58 /**
59 * Single scalar component.
60 */
61 UTIL_FORMAT_LAYOUT_SCALAR = 0,
62
63 /**
64 * One or more components of mixed integer formats, arithmetically encoded
65 * in a word up to 32bits.
66 */
67 UTIL_FORMAT_LAYOUT_ARITH = 1,
68
69 /**
70 * One or more components, no mixed formats, each with equal power of two
71 * number of bytes.
72 */
73 UTIL_FORMAT_LAYOUT_ARRAY = 2,
74
75 /**
76 * XXX: Not used yet. These might go away and be replaced by a single entry,
77 * for formats where multiple pixels have to be
78 * read in order to determine a single pixel value (i.e., block.width > 1
79 * || block.height > 1)
80 */
81 UTIL_FORMAT_LAYOUT_YUV = 3,
82 UTIL_FORMAT_LAYOUT_DXT = 4
83 };
84
85
86 struct util_format_block
87 {
88 /** Block width in pixels */
89 unsigned width;
90
91 /** Block height in pixels */
92 unsigned height;
93
94 /** Block size in bits */
95 unsigned bits;
96 };
97
98
99 enum util_format_type {
100 UTIL_FORMAT_TYPE_VOID = 0,
101 UTIL_FORMAT_TYPE_UNSIGNED = 1,
102 UTIL_FORMAT_TYPE_SIGNED = 2,
103 UTIL_FORMAT_TYPE_FIXED = 3,
104 UTIL_FORMAT_TYPE_FLOAT = 4
105 };
106
107
108 enum util_format_swizzle {
109 UTIL_FORMAT_SWIZZLE_X = 0,
110 UTIL_FORMAT_SWIZZLE_Y = 1,
111 UTIL_FORMAT_SWIZZLE_Z = 2,
112 UTIL_FORMAT_SWIZZLE_W = 3,
113 UTIL_FORMAT_SWIZZLE_0 = 4,
114 UTIL_FORMAT_SWIZZLE_1 = 5,
115 UTIL_FORMAT_SWIZZLE_NONE = 6
116 };
117
118
119 enum util_format_colorspace {
120 UTIL_FORMAT_COLORSPACE_RGB = 0,
121 UTIL_FORMAT_COLORSPACE_SRGB = 1,
122 UTIL_FORMAT_COLORSPACE_YUV = 2,
123 UTIL_FORMAT_COLORSPACE_ZS = 3
124 };
125
126
127 struct util_format_channel_description
128 {
129 unsigned type:6;
130 unsigned normalized:1;
131 unsigned size:9;
132 };
133
134
135 struct util_format_description
136 {
137 enum pipe_format format;
138 const char *name;
139 struct util_format_block block;
140 enum util_format_layout layout;
141
142 /**
143 * The number of channels.
144 */
145 unsigned nr_channels:3;
146
147 /**
148 * Whether all channels have the same number of whole bytes.
149 */
150 unsigned is_array:1;
151
152 /**
153 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
154 */
155 unsigned is_mixed:1;
156
157 struct util_format_channel_description channel[4];
158
159 unsigned char swizzle[4];
160
161 enum util_format_colorspace colorspace;
162 };
163
164
165 extern const struct util_format_description
166 util_format_description_table[];
167
168
169 const struct util_format_description *
170 util_format_description(enum pipe_format format);
171
172
173 /*
174 * Format query functions.
175 */
176
177 static INLINE const char *
178 util_format_name(enum pipe_format format)
179 {
180 const struct util_format_description *desc = util_format_description(format);
181
182 assert(format);
183 if (!format) {
184 return "???";
185 }
186
187 return desc->name;
188 }
189
190 static INLINE boolean
191 util_format_is_compressed(enum pipe_format format)
192 {
193 const struct util_format_description *desc = util_format_description(format);
194
195 assert(format);
196 if (!format) {
197 return FALSE;
198 }
199
200 return desc->layout == UTIL_FORMAT_LAYOUT_DXT ? TRUE : FALSE;
201 }
202
203 static INLINE boolean
204 util_format_is_depth_or_stencil(enum pipe_format format)
205 {
206 const struct util_format_description *desc = util_format_description(format);
207
208 assert(format);
209 if (!format) {
210 return FALSE;
211 }
212
213 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
214 }
215
216 static INLINE boolean
217 util_format_is_depth_and_stencil(enum pipe_format format)
218 {
219 const struct util_format_description *desc = util_format_description(format);
220
221 assert(format);
222 if (!format) {
223 return FALSE;
224 }
225
226 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
227 return FALSE;
228 }
229
230 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
231 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
232 }
233
234
235 /**
236 * Return total bits needed for the pixel format per block.
237 */
238 static INLINE uint
239 util_format_get_blocksizebits(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 0;
246 }
247
248 return desc->block.bits;
249 }
250
251 /**
252 * Return bytes per block (not pixel) for the given format.
253 */
254 static INLINE uint
255 util_format_get_blocksize(enum pipe_format format)
256 {
257 uint bits = util_format_get_blocksizebits(format);
258
259 assert(bits % 8 == 0);
260
261 return bits / 8;
262 }
263
264 static INLINE uint
265 util_format_get_blockwidth(enum pipe_format format)
266 {
267 const struct util_format_description *desc = util_format_description(format);
268
269 assert(format);
270 if (!format) {
271 return 1;
272 }
273
274 switch (desc->layout) {
275 case UTIL_FORMAT_LAYOUT_YUV:
276 return 2;
277 case UTIL_FORMAT_LAYOUT_DXT:
278 return 4;
279 default:
280 return 1;
281 }
282 }
283
284 static INLINE uint
285 util_format_get_blockheight(enum pipe_format format)
286 {
287 const struct util_format_description *desc = util_format_description(format);
288
289 assert(format);
290 if (!format) {
291 return 1;
292 }
293
294 switch (desc->layout) {
295 case UTIL_FORMAT_LAYOUT_DXT:
296 return 4;
297 default:
298 return 1;
299 }
300 }
301
302 static INLINE unsigned
303 util_format_get_nblocksx(enum pipe_format format,
304 unsigned x)
305 {
306 unsigned blockwidth = util_format_get_blockwidth(format);
307 return (x + blockwidth - 1) / blockwidth;
308 }
309
310 static INLINE unsigned
311 util_format_get_nblocksy(enum pipe_format format,
312 unsigned y)
313 {
314 unsigned blockheight = util_format_get_blockheight(format);
315 return (y + blockheight - 1) / blockheight;
316 }
317
318 static INLINE unsigned
319 util_format_get_nblocks(enum pipe_format format,
320 unsigned width,
321 unsigned height)
322 {
323 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
324 }
325
326 static INLINE size_t
327 util_format_get_stride(enum pipe_format format,
328 unsigned width)
329 {
330 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
331 }
332
333 static INLINE size_t
334 util_format_get_2d_size(enum pipe_format format,
335 size_t stride,
336 unsigned height)
337 {
338 return util_format_get_nblocksy(format, height) * stride;
339 }
340
341 static INLINE uint
342 util_format_get_component_bits(enum pipe_format format,
343 enum util_format_colorspace colorspace,
344 uint component)
345 {
346 const struct util_format_description *desc = util_format_description(format);
347 enum util_format_colorspace desc_colorspace;
348
349 assert(format);
350 if (!format) {
351 return 0;
352 }
353
354 assert(component < 4);
355
356 /* Treat RGB and SRGB as equivalent. */
357 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
358 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
359 }
360 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
361 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
362 } else {
363 desc_colorspace = desc->colorspace;
364 }
365
366 if (desc_colorspace != colorspace) {
367 return 0;
368 }
369
370 switch (desc->swizzle[component]) {
371 case UTIL_FORMAT_SWIZZLE_X:
372 return desc->channel[0].size;
373 case UTIL_FORMAT_SWIZZLE_Y:
374 return desc->channel[1].size;
375 case UTIL_FORMAT_SWIZZLE_Z:
376 return desc->channel[2].size;
377 case UTIL_FORMAT_SWIZZLE_W:
378 return desc->channel[3].size;
379 default:
380 return 0;
381 }
382 }
383
384 static INLINE boolean
385 util_format_has_alpha(enum pipe_format format)
386 {
387 const struct util_format_description *desc = util_format_description(format);
388
389 assert(format);
390 if (!format) {
391 return FALSE;
392 }
393
394 switch (desc->layout) {
395 case UTIL_FORMAT_LAYOUT_SCALAR:
396 case UTIL_FORMAT_LAYOUT_ARITH:
397 case UTIL_FORMAT_LAYOUT_ARRAY:
398 /* FIXME: pf_get_component_bits( PIPE_FORMAT_A8L8_UNORM, PIPE_FORMAT_COMP_A ) should not return 0 right? */
399 if (format == PIPE_FORMAT_A8_UNORM ||
400 format == PIPE_FORMAT_A8L8_UNORM ||
401 format == PIPE_FORMAT_A8L8_SRGB) {
402 return TRUE;
403 }
404 return util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3) != 0;
405 case UTIL_FORMAT_LAYOUT_YUV:
406 return FALSE;
407 case UTIL_FORMAT_LAYOUT_DXT:
408 switch (format) {
409 case PIPE_FORMAT_DXT1_RGBA:
410 case PIPE_FORMAT_DXT3_RGBA:
411 case PIPE_FORMAT_DXT5_RGBA:
412 case PIPE_FORMAT_DXT1_SRGBA:
413 case PIPE_FORMAT_DXT3_SRGBA:
414 case PIPE_FORMAT_DXT5_SRGBA:
415 return TRUE;
416 default:
417 return FALSE;
418 }
419 default:
420 assert(0);
421 return FALSE;
422 }
423 }
424
425
426 /*
427 * Format access functions.
428 */
429
430 void
431 util_format_read_4f(enum pipe_format format,
432 float *dst, unsigned dst_stride,
433 const void *src, unsigned src_stride,
434 unsigned x, unsigned y, unsigned w, unsigned h);
435
436 void
437 util_format_write_4f(enum pipe_format format,
438 const float *src, unsigned src_stride,
439 void *dst, unsigned dst_stride,
440 unsigned x, unsigned y, unsigned w, unsigned h);
441
442 void
443 util_format_read_4ub(enum pipe_format format,
444 uint8_t *dst, unsigned dst_stride,
445 const void *src, unsigned src_stride,
446 unsigned x, unsigned y, unsigned w, unsigned h);
447
448 void
449 util_format_write_4ub(enum pipe_format format,
450 const uint8_t *src, unsigned src_stride,
451 void *dst, unsigned dst_stride,
452 unsigned x, unsigned y, unsigned w, unsigned h);
453
454 #ifdef __cplusplus
455 } // extern "C" {
456 #endif
457
458 #endif /* ! U_FORMAT_H */