Merge commit '381d5e209815235911c4aab516037c868c8f695f'
[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 struct util_format_channel_description channel[4];
142 unsigned char swizzle[4];
143 enum util_format_colorspace colorspace;
144 };
145
146
147 extern const struct util_format_description
148 util_format_description_table[];
149
150
151 const struct util_format_description *
152 util_format_description(enum pipe_format format);
153
154
155 /*
156 * Format query functions.
157 */
158
159 static INLINE const char *
160 util_format_name(enum pipe_format format)
161 {
162 const struct util_format_description *desc = util_format_description(format);
163
164 assert(format);
165 if (!format) {
166 return "???";
167 }
168
169 return desc->name;
170 }
171
172 static INLINE boolean
173 util_format_is_compressed(enum pipe_format format)
174 {
175 const struct util_format_description *desc = util_format_description(format);
176
177 assert(format);
178 if (!format) {
179 return FALSE;
180 }
181
182 return desc->layout == UTIL_FORMAT_LAYOUT_DXT ? TRUE : FALSE;
183 }
184
185 static INLINE boolean
186 util_format_is_depth_or_stencil(enum pipe_format format)
187 {
188 const struct util_format_description *desc = util_format_description(format);
189
190 assert(format);
191 if (!format) {
192 return FALSE;
193 }
194
195 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
196 }
197
198 static INLINE boolean
199 util_format_is_depth_and_stencil(enum pipe_format format)
200 {
201 const struct util_format_description *desc = util_format_description(format);
202
203 assert(format);
204 if (!format) {
205 return FALSE;
206 }
207
208 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
209 return FALSE;
210 }
211
212 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
213 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
214 }
215
216
217 /**
218 * Return total bits needed for the pixel format per block.
219 */
220 static INLINE uint
221 util_format_get_blocksizebits(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 0;
228 }
229
230 return desc->block.bits;
231 }
232
233 /**
234 * Return bytes per block (not pixel) for the given format.
235 */
236 static INLINE uint
237 util_format_get_blocksize(enum pipe_format format)
238 {
239 uint bits = util_format_get_blocksizebits(format);
240
241 assert(bits % 8 == 0);
242
243 return bits / 8;
244 }
245
246 static INLINE uint
247 util_format_get_blockwidth(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 1;
254 }
255
256 switch (desc->layout) {
257 case UTIL_FORMAT_LAYOUT_YUV:
258 return 2;
259 case UTIL_FORMAT_LAYOUT_DXT:
260 return 4;
261 default:
262 return 1;
263 }
264 }
265
266 static INLINE uint
267 util_format_get_blockheight(enum pipe_format format)
268 {
269 const struct util_format_description *desc = util_format_description(format);
270
271 assert(format);
272 if (!format) {
273 return 1;
274 }
275
276 switch (desc->layout) {
277 case UTIL_FORMAT_LAYOUT_DXT:
278 return 4;
279 default:
280 return 1;
281 }
282 }
283
284 static INLINE unsigned
285 util_format_get_nblocksx(enum pipe_format format,
286 unsigned x)
287 {
288 unsigned blockwidth = util_format_get_blockwidth(format);
289 return (x + blockwidth - 1) / blockwidth;
290 }
291
292 static INLINE unsigned
293 util_format_get_nblocksy(enum pipe_format format,
294 unsigned y)
295 {
296 unsigned blockheight = util_format_get_blockheight(format);
297 return (y + blockheight - 1) / blockheight;
298 }
299
300 static INLINE unsigned
301 util_format_get_nblocks(enum pipe_format format,
302 unsigned width,
303 unsigned height)
304 {
305 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
306 }
307
308 static INLINE size_t
309 util_format_get_stride(enum pipe_format format,
310 unsigned width)
311 {
312 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
313 }
314
315 static INLINE size_t
316 util_format_get_2d_size(enum pipe_format format,
317 size_t stride,
318 unsigned height)
319 {
320 return util_format_get_nblocksy(format, height) * stride;
321 }
322
323 static INLINE uint
324 util_format_get_component_bits(enum pipe_format format,
325 enum util_format_colorspace colorspace,
326 uint component)
327 {
328 const struct util_format_description *desc = util_format_description(format);
329 enum util_format_colorspace desc_colorspace;
330
331 assert(format);
332 if (!format) {
333 return 0;
334 }
335
336 assert(component < 4);
337
338 /* Treat RGB and SRGB as equivalent. */
339 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
340 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
341 }
342 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
343 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
344 } else {
345 desc_colorspace = desc->colorspace;
346 }
347
348 if (desc_colorspace != colorspace) {
349 return 0;
350 }
351
352 switch (desc->swizzle[component]) {
353 case UTIL_FORMAT_SWIZZLE_X:
354 return desc->channel[0].size;
355 case UTIL_FORMAT_SWIZZLE_Y:
356 return desc->channel[1].size;
357 case UTIL_FORMAT_SWIZZLE_Z:
358 return desc->channel[2].size;
359 case UTIL_FORMAT_SWIZZLE_W:
360 return desc->channel[3].size;
361 default:
362 return 0;
363 }
364 }
365
366 static INLINE boolean
367 util_format_has_alpha(enum pipe_format format)
368 {
369 const struct util_format_description *desc = util_format_description(format);
370
371 assert(format);
372 if (!format) {
373 return FALSE;
374 }
375
376 switch (desc->layout) {
377 case UTIL_FORMAT_LAYOUT_SCALAR:
378 case UTIL_FORMAT_LAYOUT_ARITH:
379 case UTIL_FORMAT_LAYOUT_ARRAY:
380 /* FIXME: pf_get_component_bits( PIPE_FORMAT_A8L8_UNORM, PIPE_FORMAT_COMP_A ) should not return 0 right? */
381 if (format == PIPE_FORMAT_A8_UNORM ||
382 format == PIPE_FORMAT_A8L8_UNORM ||
383 format == PIPE_FORMAT_A8L8_SRGB) {
384 return TRUE;
385 }
386 return util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3) != 0;
387 case UTIL_FORMAT_LAYOUT_YUV:
388 return FALSE;
389 case UTIL_FORMAT_LAYOUT_DXT:
390 switch (format) {
391 case PIPE_FORMAT_DXT1_RGBA:
392 case PIPE_FORMAT_DXT3_RGBA:
393 case PIPE_FORMAT_DXT5_RGBA:
394 case PIPE_FORMAT_DXT1_SRGBA:
395 case PIPE_FORMAT_DXT3_SRGBA:
396 case PIPE_FORMAT_DXT5_SRGBA:
397 return TRUE;
398 default:
399 return FALSE;
400 }
401 default:
402 assert(0);
403 return FALSE;
404 }
405 }
406
407
408 /*
409 * Format access functions.
410 */
411
412 void
413 util_format_read_4f(enum pipe_format format,
414 float *dst, unsigned dst_stride,
415 const void *src, unsigned src_stride,
416 unsigned x, unsigned y, unsigned w, unsigned h);
417
418 void
419 util_format_write_4f(enum pipe_format format,
420 const float *src, unsigned src_stride,
421 void *dst, unsigned dst_stride,
422 unsigned x, unsigned y, unsigned w, unsigned h);
423
424 void
425 util_format_read_4ub(enum pipe_format format,
426 uint8_t *dst, unsigned dst_stride,
427 const void *src, unsigned src_stride,
428 unsigned x, unsigned y, unsigned w, unsigned h);
429
430 void
431 util_format_write_4ub(enum pipe_format format,
432 const uint8_t *src, unsigned src_stride,
433 void *dst, unsigned dst_stride,
434 unsigned x, unsigned y, unsigned w, unsigned h);
435
436 #ifdef __cplusplus
437 } // extern "C" {
438 #endif
439
440 #endif /* ! U_FORMAT_H */