Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / util / u_pack_color.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 * @file
30 * Functions to produce packed colors/Z from floats.
31 */
32
33
34 #ifndef U_PACK_COLOR_H
35 #define U_PACK_COLOR_H
36
37
38 #include "pipe/p_compiler.h"
39 #include "pipe/p_format.h"
40 #include "util/u_debug.h"
41 #include "util/u_format.h"
42 #include "util/u_math.h"
43
44
45
46 union util_color {
47 ubyte ub;
48 ushort us;
49 uint ui;
50 float f[4];
51 };
52
53 /**
54 * Pack ubyte R,G,B,A into dest pixel.
55 */
56 static INLINE void
57 util_pack_color_ub(ubyte r, ubyte g, ubyte b, ubyte a,
58 enum pipe_format format, union util_color *uc)
59 {
60 switch (format) {
61 case PIPE_FORMAT_A8B8G8R8_UNORM:
62 {
63 uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
64 }
65 return;
66 case PIPE_FORMAT_X8B8G8R8_UNORM:
67 {
68 uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
69 }
70 return;
71 case PIPE_FORMAT_B8G8R8A8_UNORM:
72 {
73 uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
74 }
75 return;
76 case PIPE_FORMAT_B8G8R8X8_UNORM:
77 {
78 uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
79 }
80 return;
81 case PIPE_FORMAT_A8R8G8B8_UNORM:
82 {
83 uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
84 }
85 return;
86 case PIPE_FORMAT_X8R8G8B8_UNORM:
87 {
88 uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
89 }
90 return;
91 case PIPE_FORMAT_B5G6R5_UNORM:
92 {
93 uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
94 }
95 return;
96 case PIPE_FORMAT_B5G5R5X1_UNORM:
97 {
98 uc->us = ((0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
99 }
100 return;
101 case PIPE_FORMAT_B5G5R5A1_UNORM:
102 {
103 uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
104 }
105 return;
106 case PIPE_FORMAT_B4G4R4A4_UNORM:
107 {
108 uc->us = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
109 }
110 return;
111 case PIPE_FORMAT_A8_UNORM:
112 {
113 uc->ub = a;
114 }
115 return;
116 case PIPE_FORMAT_L8_UNORM:
117 case PIPE_FORMAT_I8_UNORM:
118 {
119 uc->ub = a;
120 }
121 return;
122 case PIPE_FORMAT_R32G32B32A32_FLOAT:
123 {
124 uc->f[0] = (float)r / 255.0f;
125 uc->f[1] = (float)g / 255.0f;
126 uc->f[2] = (float)b / 255.0f;
127 uc->f[3] = (float)a / 255.0f;
128 }
129 return;
130 case PIPE_FORMAT_R32G32B32_FLOAT:
131 {
132 uc->f[0] = (float)r / 255.0f;
133 uc->f[1] = (float)g / 255.0f;
134 uc->f[2] = (float)b / 255.0f;
135 }
136 return;
137
138 /* Handle other cases with a generic function.
139 */
140 default:
141 {
142 ubyte src[4];
143
144 src[0] = r;
145 src[1] = g;
146 src[2] = b;
147 src[3] = a;
148 util_format_write_4ub(format, src, 0, uc, 0, 0, 0, 1, 1);
149 }
150 }
151 }
152
153
154 /**
155 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
156 */
157 static INLINE void
158 util_unpack_color_ub(enum pipe_format format, union util_color *uc,
159 ubyte *r, ubyte *g, ubyte *b, ubyte *a)
160 {
161 switch (format) {
162 case PIPE_FORMAT_A8B8G8R8_UNORM:
163 {
164 uint p = uc->ui;
165 *r = (ubyte) ((p >> 24) & 0xff);
166 *g = (ubyte) ((p >> 16) & 0xff);
167 *b = (ubyte) ((p >> 8) & 0xff);
168 *a = (ubyte) ((p >> 0) & 0xff);
169 }
170 return;
171 case PIPE_FORMAT_X8B8G8R8_UNORM:
172 {
173 uint p = uc->ui;
174 *r = (ubyte) ((p >> 24) & 0xff);
175 *g = (ubyte) ((p >> 16) & 0xff);
176 *b = (ubyte) ((p >> 8) & 0xff);
177 *a = (ubyte) 0xff;
178 }
179 return;
180 case PIPE_FORMAT_B8G8R8A8_UNORM:
181 {
182 uint p = uc->ui;
183 *r = (ubyte) ((p >> 16) & 0xff);
184 *g = (ubyte) ((p >> 8) & 0xff);
185 *b = (ubyte) ((p >> 0) & 0xff);
186 *a = (ubyte) ((p >> 24) & 0xff);
187 }
188 return;
189 case PIPE_FORMAT_B8G8R8X8_UNORM:
190 {
191 uint p = uc->ui;
192 *r = (ubyte) ((p >> 16) & 0xff);
193 *g = (ubyte) ((p >> 8) & 0xff);
194 *b = (ubyte) ((p >> 0) & 0xff);
195 *a = (ubyte) 0xff;
196 }
197 return;
198 case PIPE_FORMAT_A8R8G8B8_UNORM:
199 {
200 uint p = uc->ui;
201 *r = (ubyte) ((p >> 8) & 0xff);
202 *g = (ubyte) ((p >> 16) & 0xff);
203 *b = (ubyte) ((p >> 24) & 0xff);
204 *a = (ubyte) ((p >> 0) & 0xff);
205 }
206 return;
207 case PIPE_FORMAT_X8R8G8B8_UNORM:
208 {
209 uint p = uc->ui;
210 *r = (ubyte) ((p >> 8) & 0xff);
211 *g = (ubyte) ((p >> 16) & 0xff);
212 *b = (ubyte) ((p >> 24) & 0xff);
213 *a = (ubyte) 0xff;
214 }
215 return;
216 case PIPE_FORMAT_B5G6R5_UNORM:
217 {
218 ushort p = uc->us;
219 *r = (ubyte) (((p >> 8) & 0xf8) | ((p >> 13) & 0x7));
220 *g = (ubyte) (((p >> 3) & 0xfc) | ((p >> 9) & 0x3));
221 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
222 *a = (ubyte) 0xff;
223 }
224 return;
225 case PIPE_FORMAT_B5G5R5X1_UNORM:
226 {
227 ushort p = uc->us;
228 *r = (ubyte) (((p >> 7) & 0xf8) | ((p >> 12) & 0x7));
229 *g = (ubyte) (((p >> 2) & 0xf8) | ((p >> 7) & 0x7));
230 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
231 *a = (ubyte) 0xff;
232 }
233 return;
234 case PIPE_FORMAT_B5G5R5A1_UNORM:
235 {
236 ushort p = uc->us;
237 *r = (ubyte) (((p >> 7) & 0xf8) | ((p >> 12) & 0x7));
238 *g = (ubyte) (((p >> 2) & 0xf8) | ((p >> 7) & 0x7));
239 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
240 *a = (ubyte) (0xff * (p >> 15));
241 }
242 return;
243 case PIPE_FORMAT_B4G4R4A4_UNORM:
244 {
245 ushort p = uc->us;
246 *r = (ubyte) (((p >> 4) & 0xf0) | ((p >> 8) & 0xf));
247 *g = (ubyte) (((p >> 0) & 0xf0) | ((p >> 4) & 0xf));
248 *b = (ubyte) (((p << 4) & 0xf0) | ((p >> 0) & 0xf));
249 *a = (ubyte) (((p >> 8) & 0xf0) | ((p >> 12) & 0xf));
250 }
251 return;
252 case PIPE_FORMAT_A8_UNORM:
253 {
254 ubyte p = uc->ub;
255 *r = *g = *b = (ubyte) 0xff;
256 *a = p;
257 }
258 return;
259 case PIPE_FORMAT_L8_UNORM:
260 {
261 ubyte p = uc->ub;
262 *r = *g = *b = p;
263 *a = (ubyte) 0xff;
264 }
265 return;
266 case PIPE_FORMAT_I8_UNORM:
267 {
268 ubyte p = uc->ub;
269 *r = *g = *b = *a = p;
270 }
271 return;
272 case PIPE_FORMAT_R32G32B32A32_FLOAT:
273 {
274 const float *p = &uc->f[0];
275 *r = float_to_ubyte(p[0]);
276 *g = float_to_ubyte(p[1]);
277 *b = float_to_ubyte(p[2]);
278 *a = float_to_ubyte(p[3]);
279 }
280 return;
281 case PIPE_FORMAT_R32G32B32_FLOAT:
282 {
283 const float *p = &uc->f[0];
284 *r = float_to_ubyte(p[0]);
285 *g = float_to_ubyte(p[1]);
286 *b = float_to_ubyte(p[2]);
287 *a = (ubyte) 0xff;
288 }
289 return;
290
291 case PIPE_FORMAT_R32G32_FLOAT:
292 {
293 const float *p = &uc->f[0];
294 *r = float_to_ubyte(p[0]);
295 *g = float_to_ubyte(p[1]);
296 *b = *a = (ubyte) 0xff;
297 }
298 return;
299
300 case PIPE_FORMAT_R32_FLOAT:
301 {
302 const float *p = &uc->f[0];
303 *r = float_to_ubyte(p[0]);
304 *g = *b = *a = (ubyte) 0xff;
305 }
306 return;
307
308 /* Handle other cases with a generic function.
309 */
310 default:
311 {
312 ubyte dst[4];
313
314 util_format_read_4ub(format, dst, 0, uc, 0, 0, 0, 1, 1);
315 *r = dst[0];
316 *g = dst[1];
317 *b = dst[2];
318 *a = dst[3];
319 }
320 }
321 }
322
323
324 /**
325 * Note rgba outside [0,1] will be clamped for int pixel formats.
326 */
327 static INLINE void
328 util_pack_color(const float rgba[4], enum pipe_format format, union util_color *uc)
329 {
330 ubyte r = 0;
331 ubyte g = 0;
332 ubyte b = 0;
333 ubyte a = 0;
334
335 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) <= 8) {
336 /* format uses 8-bit components or less */
337 r = float_to_ubyte(rgba[0]);
338 g = float_to_ubyte(rgba[1]);
339 b = float_to_ubyte(rgba[2]);
340 a = float_to_ubyte(rgba[3]);
341 }
342
343 switch (format) {
344 case PIPE_FORMAT_A8B8G8R8_UNORM:
345 {
346 uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
347 }
348 return;
349 case PIPE_FORMAT_X8B8G8R8_UNORM:
350 {
351 uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
352 }
353 return;
354 case PIPE_FORMAT_B8G8R8A8_UNORM:
355 {
356 uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
357 }
358 return;
359 case PIPE_FORMAT_B8G8R8X8_UNORM:
360 {
361 uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
362 }
363 return;
364 case PIPE_FORMAT_A8R8G8B8_UNORM:
365 {
366 uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
367 }
368 return;
369 case PIPE_FORMAT_X8R8G8B8_UNORM:
370 {
371 uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
372 }
373 return;
374 case PIPE_FORMAT_B5G6R5_UNORM:
375 {
376 uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
377 }
378 return;
379 case PIPE_FORMAT_B5G5R5X1_UNORM:
380 {
381 uc->us = ((0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
382 }
383 return;
384 case PIPE_FORMAT_B5G5R5A1_UNORM:
385 {
386 uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
387 }
388 return;
389 case PIPE_FORMAT_B4G4R4A4_UNORM:
390 {
391 uc->ub = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
392 }
393 return;
394 case PIPE_FORMAT_A8_UNORM:
395 {
396 uc->ub = a;
397 }
398 return;
399 case PIPE_FORMAT_L8_UNORM:
400 case PIPE_FORMAT_I8_UNORM:
401 {
402 uc->ub = r;
403 }
404 return;
405 case PIPE_FORMAT_R32G32B32A32_FLOAT:
406 {
407 uc->f[0] = rgba[0];
408 uc->f[1] = rgba[1];
409 uc->f[2] = rgba[2];
410 uc->f[3] = rgba[3];
411 }
412 return;
413 case PIPE_FORMAT_R32G32B32_FLOAT:
414 {
415 uc->f[0] = rgba[0];
416 uc->f[1] = rgba[1];
417 uc->f[2] = rgba[2];
418 }
419 return;
420
421 /* Handle other cases with a generic function.
422 */
423 default:
424 util_format_write_4f(format, rgba, 0, uc, 0, 0, 0, 1, 1);
425 }
426 }
427
428
429 /**
430 * Note: it's assumed that z is in [0,1]
431 */
432 static INLINE uint
433 util_pack_z(enum pipe_format format, double z)
434 {
435 if (z == 0.0)
436 return 0;
437
438 switch (format) {
439 case PIPE_FORMAT_Z16_UNORM:
440 if (z == 1.0)
441 return 0xffff;
442 return (uint) (z * 0xffff);
443 case PIPE_FORMAT_Z32_UNORM:
444 /* special-case to avoid overflow */
445 if (z == 1.0)
446 return 0xffffffff;
447 return (uint) (z * 0xffffffff);
448 case PIPE_FORMAT_Z32_FLOAT:
449 return (uint)z;
450 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
451 case PIPE_FORMAT_Z24X8_UNORM:
452 if (z == 1.0)
453 return 0xffffff;
454 return (uint) (z * 0xffffff);
455 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
456 case PIPE_FORMAT_X8Z24_UNORM:
457 if (z == 1.0)
458 return 0xffffff00;
459 return ((uint) (z * 0xffffff)) << 8;
460 case PIPE_FORMAT_S8_USCALED:
461 /* this case can get it via util_pack_z_stencil() */
462 return 0;
463 default:
464 debug_print_format("gallium: unhandled format in util_pack_z()", format);
465 assert(0);
466 return 0;
467 }
468 }
469
470
471 /**
472 * Pack Z and/or stencil values into a 32-bit value described by format.
473 * Note: it's assumed that z is in [0,1] and s in [0,255]
474 */
475 static INLINE uint
476 util_pack_z_stencil(enum pipe_format format, double z, uint s)
477 {
478 unsigned packed = util_pack_z(format, z);
479
480 switch (format) {
481 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
482 packed |= s << 24;
483 break;
484 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
485 packed |= s;
486 break;
487 case PIPE_FORMAT_S8_USCALED:
488 packed |= s;
489 break;
490 default:
491 break;
492 }
493
494 return packed;
495 }
496
497
498 /**
499 * Pack 4 ubytes into a 4-byte word
500 */
501 static INLINE unsigned
502 pack_ub4(ubyte b0, ubyte b1, ubyte b2, ubyte b3)
503 {
504 return ((((unsigned int)b0) << 0) |
505 (((unsigned int)b1) << 8) |
506 (((unsigned int)b2) << 16) |
507 (((unsigned int)b3) << 24));
508 }
509
510
511 /**
512 * Pack/convert 4 floats into one 4-byte word.
513 */
514 static INLINE unsigned
515 pack_ui32_float4(float a, float b, float c, float d)
516 {
517 return pack_ub4( float_to_ubyte(a),
518 float_to_ubyte(b),
519 float_to_ubyte(c),
520 float_to_ubyte(d) );
521 }
522
523
524
525 #endif /* U_PACK_COLOR_H */