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