llvmpipe: fix clearing integer color buffers
[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 * Helper union for packing pixel values.
47 * Will often contain values in formats which are too complex to be described
48 * in simple terms, hence might just effectively contain a number of bytes.
49 * Must be big enough to hold data for all formats (currently 256 bits).
50 */
51 union util_color {
52 ubyte ub;
53 ushort us;
54 uint ui;
55 float f[4];
56 double d[4];
57 };
58
59 /**
60 * Pack ubyte R,G,B,A into dest pixel.
61 */
62 static INLINE void
63 util_pack_color_ub(ubyte r, ubyte g, ubyte b, ubyte a,
64 enum pipe_format format, union util_color *uc)
65 {
66 switch (format) {
67 case PIPE_FORMAT_A8B8G8R8_UNORM:
68 {
69 uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
70 }
71 return;
72 case PIPE_FORMAT_X8B8G8R8_UNORM:
73 {
74 uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
75 }
76 return;
77 case PIPE_FORMAT_B8G8R8A8_UNORM:
78 {
79 uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
80 }
81 return;
82 case PIPE_FORMAT_B8G8R8X8_UNORM:
83 {
84 uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
85 }
86 return;
87 case PIPE_FORMAT_A8R8G8B8_UNORM:
88 {
89 uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
90 }
91 return;
92 case PIPE_FORMAT_X8R8G8B8_UNORM:
93 {
94 uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
95 }
96 return;
97 case PIPE_FORMAT_B5G6R5_UNORM:
98 {
99 uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
100 }
101 return;
102 case PIPE_FORMAT_B5G5R5X1_UNORM:
103 {
104 uc->us = ((0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
105 }
106 return;
107 case PIPE_FORMAT_B5G5R5A1_UNORM:
108 {
109 uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
110 }
111 return;
112 case PIPE_FORMAT_B4G4R4A4_UNORM:
113 {
114 uc->us = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
115 }
116 return;
117 case PIPE_FORMAT_A8_UNORM:
118 {
119 uc->ub = a;
120 }
121 return;
122 case PIPE_FORMAT_L8_UNORM:
123 case PIPE_FORMAT_I8_UNORM:
124 {
125 uc->ub = r;
126 }
127 return;
128 case PIPE_FORMAT_R32G32B32A32_FLOAT:
129 {
130 uc->f[0] = (float)r / 255.0f;
131 uc->f[1] = (float)g / 255.0f;
132 uc->f[2] = (float)b / 255.0f;
133 uc->f[3] = (float)a / 255.0f;
134 }
135 return;
136 case PIPE_FORMAT_R32G32B32_FLOAT:
137 {
138 uc->f[0] = (float)r / 255.0f;
139 uc->f[1] = (float)g / 255.0f;
140 uc->f[2] = (float)b / 255.0f;
141 }
142 return;
143
144 /* Handle other cases with a generic function.
145 */
146 default:
147 {
148 ubyte src[4];
149
150 src[0] = r;
151 src[1] = g;
152 src[2] = b;
153 src[3] = a;
154 util_format_write_4ub(format, src, 0, uc, 0, 0, 0, 1, 1);
155 }
156 }
157 }
158
159
160 /**
161 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
162 */
163 static INLINE void
164 util_unpack_color_ub(enum pipe_format format, union util_color *uc,
165 ubyte *r, ubyte *g, ubyte *b, ubyte *a)
166 {
167 switch (format) {
168 case PIPE_FORMAT_A8B8G8R8_UNORM:
169 {
170 uint p = uc->ui;
171 *r = (ubyte) ((p >> 24) & 0xff);
172 *g = (ubyte) ((p >> 16) & 0xff);
173 *b = (ubyte) ((p >> 8) & 0xff);
174 *a = (ubyte) ((p >> 0) & 0xff);
175 }
176 return;
177 case PIPE_FORMAT_X8B8G8R8_UNORM:
178 {
179 uint p = uc->ui;
180 *r = (ubyte) ((p >> 24) & 0xff);
181 *g = (ubyte) ((p >> 16) & 0xff);
182 *b = (ubyte) ((p >> 8) & 0xff);
183 *a = (ubyte) 0xff;
184 }
185 return;
186 case PIPE_FORMAT_B8G8R8A8_UNORM:
187 {
188 uint p = uc->ui;
189 *r = (ubyte) ((p >> 16) & 0xff);
190 *g = (ubyte) ((p >> 8) & 0xff);
191 *b = (ubyte) ((p >> 0) & 0xff);
192 *a = (ubyte) ((p >> 24) & 0xff);
193 }
194 return;
195 case PIPE_FORMAT_B8G8R8X8_UNORM:
196 {
197 uint p = uc->ui;
198 *r = (ubyte) ((p >> 16) & 0xff);
199 *g = (ubyte) ((p >> 8) & 0xff);
200 *b = (ubyte) ((p >> 0) & 0xff);
201 *a = (ubyte) 0xff;
202 }
203 return;
204 case PIPE_FORMAT_A8R8G8B8_UNORM:
205 {
206 uint p = uc->ui;
207 *r = (ubyte) ((p >> 8) & 0xff);
208 *g = (ubyte) ((p >> 16) & 0xff);
209 *b = (ubyte) ((p >> 24) & 0xff);
210 *a = (ubyte) ((p >> 0) & 0xff);
211 }
212 return;
213 case PIPE_FORMAT_X8R8G8B8_UNORM:
214 {
215 uint p = uc->ui;
216 *r = (ubyte) ((p >> 8) & 0xff);
217 *g = (ubyte) ((p >> 16) & 0xff);
218 *b = (ubyte) ((p >> 24) & 0xff);
219 *a = (ubyte) 0xff;
220 }
221 return;
222 case PIPE_FORMAT_B5G6R5_UNORM:
223 {
224 ushort p = uc->us;
225 *r = (ubyte) (((p >> 8) & 0xf8) | ((p >> 13) & 0x7));
226 *g = (ubyte) (((p >> 3) & 0xfc) | ((p >> 9) & 0x3));
227 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
228 *a = (ubyte) 0xff;
229 }
230 return;
231 case PIPE_FORMAT_B5G5R5X1_UNORM:
232 {
233 ushort p = uc->us;
234 *r = (ubyte) (((p >> 7) & 0xf8) | ((p >> 12) & 0x7));
235 *g = (ubyte) (((p >> 2) & 0xf8) | ((p >> 7) & 0x7));
236 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
237 *a = (ubyte) 0xff;
238 }
239 return;
240 case PIPE_FORMAT_B5G5R5A1_UNORM:
241 {
242 ushort p = uc->us;
243 *r = (ubyte) (((p >> 7) & 0xf8) | ((p >> 12) & 0x7));
244 *g = (ubyte) (((p >> 2) & 0xf8) | ((p >> 7) & 0x7));
245 *b = (ubyte) (((p << 3) & 0xf8) | ((p >> 2) & 0x7));
246 *a = (ubyte) (0xff * (p >> 15));
247 }
248 return;
249 case PIPE_FORMAT_B4G4R4A4_UNORM:
250 {
251 ushort p = uc->us;
252 *r = (ubyte) (((p >> 4) & 0xf0) | ((p >> 8) & 0xf));
253 *g = (ubyte) (((p >> 0) & 0xf0) | ((p >> 4) & 0xf));
254 *b = (ubyte) (((p << 4) & 0xf0) | ((p >> 0) & 0xf));
255 *a = (ubyte) (((p >> 8) & 0xf0) | ((p >> 12) & 0xf));
256 }
257 return;
258 case PIPE_FORMAT_A8_UNORM:
259 {
260 ubyte p = uc->ub;
261 *r = *g = *b = (ubyte) 0xff;
262 *a = p;
263 }
264 return;
265 case PIPE_FORMAT_L8_UNORM:
266 {
267 ubyte p = uc->ub;
268 *r = *g = *b = p;
269 *a = (ubyte) 0xff;
270 }
271 return;
272 case PIPE_FORMAT_I8_UNORM:
273 {
274 ubyte p = uc->ub;
275 *r = *g = *b = *a = p;
276 }
277 return;
278 case PIPE_FORMAT_R32G32B32A32_FLOAT:
279 {
280 const float *p = &uc->f[0];
281 *r = float_to_ubyte(p[0]);
282 *g = float_to_ubyte(p[1]);
283 *b = float_to_ubyte(p[2]);
284 *a = float_to_ubyte(p[3]);
285 }
286 return;
287 case PIPE_FORMAT_R32G32B32_FLOAT:
288 {
289 const float *p = &uc->f[0];
290 *r = float_to_ubyte(p[0]);
291 *g = float_to_ubyte(p[1]);
292 *b = float_to_ubyte(p[2]);
293 *a = (ubyte) 0xff;
294 }
295 return;
296
297 case PIPE_FORMAT_R32G32_FLOAT:
298 {
299 const float *p = &uc->f[0];
300 *r = float_to_ubyte(p[0]);
301 *g = float_to_ubyte(p[1]);
302 *b = *a = (ubyte) 0xff;
303 }
304 return;
305
306 case PIPE_FORMAT_R32_FLOAT:
307 {
308 const float *p = &uc->f[0];
309 *r = float_to_ubyte(p[0]);
310 *g = *b = *a = (ubyte) 0xff;
311 }
312 return;
313
314 /* Handle other cases with a generic function.
315 */
316 default:
317 {
318 ubyte dst[4];
319
320 util_format_read_4ub(format, dst, 0, uc, 0, 0, 0, 1, 1);
321 *r = dst[0];
322 *g = dst[1];
323 *b = dst[2];
324 *a = dst[3];
325 }
326 }
327 }
328
329
330 /**
331 * Note rgba outside [0,1] will be clamped for int pixel formats.
332 * This will not work (and might not really be useful with float input)
333 * for pure integer formats (which lack the pack_rgba_float function).
334 */
335 static INLINE void
336 util_pack_color(const float rgba[4], enum pipe_format format, union util_color *uc)
337 {
338 ubyte r = 0;
339 ubyte g = 0;
340 ubyte b = 0;
341 ubyte a = 0;
342
343 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) <= 8) {
344 /* format uses 8-bit components or less */
345 r = float_to_ubyte(rgba[0]);
346 g = float_to_ubyte(rgba[1]);
347 b = float_to_ubyte(rgba[2]);
348 a = float_to_ubyte(rgba[3]);
349 }
350
351 switch (format) {
352 case PIPE_FORMAT_A8B8G8R8_UNORM:
353 {
354 uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
355 }
356 return;
357 case PIPE_FORMAT_X8B8G8R8_UNORM:
358 {
359 uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
360 }
361 return;
362 case PIPE_FORMAT_B8G8R8A8_UNORM:
363 {
364 uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
365 }
366 return;
367 case PIPE_FORMAT_B8G8R8X8_UNORM:
368 {
369 uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
370 }
371 return;
372 case PIPE_FORMAT_A8R8G8B8_UNORM:
373 {
374 uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
375 }
376 return;
377 case PIPE_FORMAT_X8R8G8B8_UNORM:
378 {
379 uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
380 }
381 return;
382 case PIPE_FORMAT_B5G6R5_UNORM:
383 {
384 uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
385 }
386 return;
387 case PIPE_FORMAT_B5G5R5X1_UNORM:
388 {
389 uc->us = ((0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
390 }
391 return;
392 case PIPE_FORMAT_B5G5R5A1_UNORM:
393 {
394 uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
395 }
396 return;
397 case PIPE_FORMAT_B4G4R4A4_UNORM:
398 {
399 uc->us = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
400 }
401 return;
402 case PIPE_FORMAT_A8_UNORM:
403 {
404 uc->ub = a;
405 }
406 return;
407 case PIPE_FORMAT_L8_UNORM:
408 case PIPE_FORMAT_I8_UNORM:
409 {
410 uc->ub = r;
411 }
412 return;
413 case PIPE_FORMAT_R32G32B32A32_FLOAT:
414 {
415 uc->f[0] = rgba[0];
416 uc->f[1] = rgba[1];
417 uc->f[2] = rgba[2];
418 uc->f[3] = rgba[3];
419 }
420 return;
421 case PIPE_FORMAT_R32G32B32_FLOAT:
422 {
423 uc->f[0] = rgba[0];
424 uc->f[1] = rgba[1];
425 uc->f[2] = rgba[2];
426 }
427 return;
428
429 /* Handle other cases with a generic function.
430 */
431 default:
432 util_format_write_4f(format, rgba, 0, uc, 0, 0, 0, 1, 1);
433 }
434 }
435
436 /* Integer versions of util_pack_z and util_pack_z_stencil - useful for
437 * constructing clear masks.
438 */
439 static INLINE uint32_t
440 util_pack_mask_z(enum pipe_format format, uint32_t z)
441 {
442 switch (format) {
443 case PIPE_FORMAT_Z16_UNORM:
444 return z & 0xffff;
445 case PIPE_FORMAT_Z32_UNORM:
446 case PIPE_FORMAT_Z32_FLOAT:
447 return z;
448 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
449 case PIPE_FORMAT_Z24X8_UNORM:
450 return z & 0xffffff;
451 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
452 case PIPE_FORMAT_X8Z24_UNORM:
453 return (z & 0xffffff) << 8;
454 case PIPE_FORMAT_S8_UINT:
455 return 0;
456 default:
457 debug_print_format("gallium: unhandled format in util_pack_mask_z()", format);
458 assert(0);
459 return 0;
460 }
461 }
462
463
464 static INLINE uint64_t
465 util_pack64_mask_z(enum pipe_format format, uint32_t z)
466 {
467 switch (format) {
468 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
469 return z;
470 default:
471 return util_pack_mask_z(format, z);
472 }
473 }
474
475
476 static INLINE uint32_t
477 util_pack_mask_z_stencil(enum pipe_format format, uint32_t z, uint8_t s)
478 {
479 uint32_t packed = util_pack_mask_z(format, z);
480
481 switch (format) {
482 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
483 packed |= (uint32_t)s << 24;
484 break;
485 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
486 packed |= s;
487 break;
488 case PIPE_FORMAT_S8_UINT:
489 packed |= s;
490 break;
491 default:
492 break;
493 }
494
495 return packed;
496 }
497
498
499 static INLINE uint64_t
500 util_pack64_mask_z_stencil(enum pipe_format format, uint32_t z, uint8_t s)
501 {
502 uint64_t packed;
503
504 switch (format) {
505 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
506 packed = util_pack64_mask_z(format, z);
507 packed |= (uint64_t)s << 32ull;
508 return packed;
509 default:
510 return util_pack_mask_z_stencil(format, z, s);
511 }
512 }
513
514
515 /**
516 * Note: it's assumed that z is in [0,1]
517 */
518 static INLINE uint32_t
519 util_pack_z(enum pipe_format format, double z)
520 {
521 union fi fui;
522
523 if (z == 0.0)
524 return 0;
525
526 switch (format) {
527 case PIPE_FORMAT_Z16_UNORM:
528 if (z == 1.0)
529 return 0xffff;
530 return (uint32_t) (z * 0xffff);
531 case PIPE_FORMAT_Z32_UNORM:
532 /* special-case to avoid overflow */
533 if (z == 1.0)
534 return 0xffffffff;
535 return (uint32_t) (z * 0xffffffff);
536 case PIPE_FORMAT_Z32_FLOAT:
537 fui.f = (float)z;
538 return fui.ui;
539 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
540 case PIPE_FORMAT_Z24X8_UNORM:
541 if (z == 1.0)
542 return 0xffffff;
543 return (uint32_t) (z * 0xffffff);
544 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
545 case PIPE_FORMAT_X8Z24_UNORM:
546 if (z == 1.0)
547 return 0xffffff00;
548 return ((uint32_t) (z * 0xffffff)) << 8;
549 case PIPE_FORMAT_S8_UINT:
550 /* this case can get it via util_pack_z_stencil() */
551 return 0;
552 default:
553 debug_print_format("gallium: unhandled format in util_pack_z()", format);
554 assert(0);
555 return 0;
556 }
557 }
558
559
560 static INLINE uint64_t
561 util_pack64_z(enum pipe_format format, double z)
562 {
563 union fi fui;
564
565 if (z == 0)
566 return 0;
567
568 switch (format) {
569 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
570 fui.f = (float)z;
571 return fui.ui;
572 default:
573 return util_pack_z(format, z);
574 }
575 }
576
577
578 /**
579 * Pack Z and/or stencil values into a 32-bit value described by format.
580 * Note: it's assumed that z is in [0,1] and s in [0,255]
581 */
582 static INLINE uint32_t
583 util_pack_z_stencil(enum pipe_format format, double z, uint8_t s)
584 {
585 uint32_t packed = util_pack_z(format, z);
586
587 switch (format) {
588 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
589 packed |= (uint32_t)s << 24;
590 break;
591 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
592 packed |= s;
593 break;
594 case PIPE_FORMAT_S8_UINT:
595 packed |= s;
596 break;
597 default:
598 break;
599 }
600
601 return packed;
602 }
603
604
605 static INLINE uint64_t
606 util_pack64_z_stencil(enum pipe_format format, double z, uint8_t s)
607 {
608 uint64_t packed;
609
610 switch (format) {
611 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
612 packed = util_pack64_z(format, z);
613 packed |= (uint64_t)s << 32ull;
614 break;
615 default:
616 return util_pack_z_stencil(format, z, s);
617 }
618
619 return packed;
620 }
621
622
623 /**
624 * Pack 4 ubytes into a 4-byte word
625 */
626 static INLINE unsigned
627 pack_ub4(ubyte b0, ubyte b1, ubyte b2, ubyte b3)
628 {
629 return ((((unsigned int)b0) << 0) |
630 (((unsigned int)b1) << 8) |
631 (((unsigned int)b2) << 16) |
632 (((unsigned int)b3) << 24));
633 }
634
635
636 /**
637 * Pack/convert 4 floats into one 4-byte word.
638 */
639 static INLINE unsigned
640 pack_ui32_float4(float a, float b, float c, float d)
641 {
642 return pack_ub4( float_to_ubyte(a),
643 float_to_ubyte(b),
644 float_to_ubyte(c),
645 float_to_ubyte(d) );
646 }
647
648
649
650 #endif /* U_PACK_COLOR_H */