1 /**************************************************************************
2  *
3  * Copyright 2008 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  * @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/format/u_format.h"
41 #include "util/u_math.h"
42 
43 
44 /**
45  * Helper union for packing pixel values.
46  * Will often contain values in formats which are too complex to be described
47  * in simple terms, hence might just effectively contain a number of bytes.
48  * Must be big enough to hold data for all formats (currently 256 bits).
49  */
50 union util_color {
51    ubyte ub;
52    ushort us;
53    uint ui[4];
54    ushort h[4]; /* half float */
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
util_pack_color_ub(ubyte r,ubyte g,ubyte b,ubyte a,enum pipe_format format,union util_color * uc)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_ABGR8888_UNORM:
68       {
69          uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | a;
70       }
71       return;
72    case PIPE_FORMAT_XBGR8888_UNORM:
73       {
74          uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | 0xff;
75       }
76       return;
77    case PIPE_FORMAT_BGRA8888_UNORM:
78       {
79          uc->ui[0] = (a << 24) | (r << 16) | (g << 8) | b;
80       }
81       return;
82    case PIPE_FORMAT_BGRX8888_UNORM:
83       {
84          uc->ui[0] = (0xff << 24) | (r << 16) | (g << 8) | b;
85       }
86       return;
87    case PIPE_FORMAT_ARGB8888_UNORM:
88       {
89          uc->ui[0] = (b << 24) | (g << 16) | (r << 8) | a;
90       }
91       return;
92    case PIPE_FORMAT_XRGB8888_UNORM:
93       {
94          uc->ui[0] = (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
util_unpack_color_ub(enum pipe_format format,union util_color * uc,ubyte * r,ubyte * g,ubyte * b,ubyte * a)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_ABGR8888_UNORM:
169       {
170          uint p = uc->ui[0];
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_XBGR8888_UNORM:
178       {
179          uint p = uc->ui[0];
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_BGRA8888_UNORM:
187       {
188          uint p = uc->ui[0];
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_BGRX8888_UNORM:
196       {
197          uint p = uc->ui[0];
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_ARGB8888_UNORM:
205       {
206          uint p = uc->ui[0];
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_XRGB8888_UNORM:
214       {
215          uint p = uc->ui[0];
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
util_pack_color(const float rgba[4],enum pipe_format format,union util_color * uc)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_ABGR8888_UNORM:
353       {
354          uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | a;
355       }
356       return;
357    case PIPE_FORMAT_XBGR8888_UNORM:
358       {
359          uc->ui[0] = (r << 24) | (g << 16) | (b << 8) | 0xff;
360       }
361       return;
362    case PIPE_FORMAT_BGRA8888_UNORM:
363       {
364          uc->ui[0] = (a << 24) | (r << 16) | (g << 8) | b;
365       }
366       return;
367    case PIPE_FORMAT_BGRX8888_UNORM:
368       {
369          uc->ui[0] = (0xffu << 24) | (r << 16) | (g << 8) | b;
370       }
371       return;
372    case PIPE_FORMAT_ARGB8888_UNORM:
373       {
374          uc->ui[0] = (b << 24) | (g << 16) | (r << 8) | a;
375       }
376       return;
377    case PIPE_FORMAT_XRGB8888_UNORM:
378       {
379          uc->ui[0] = (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_pack_rgba(format, uc, rgba, 1);
433    }
434 }
435 
436 static inline void
util_pack_color_union(enum pipe_format format,union util_color * dst,const union pipe_color_union * src)437 util_pack_color_union(enum pipe_format format,
438                       union util_color *dst,
439                       const union pipe_color_union *src)
440 {
441    util_format_pack_rgba(format, dst, &src->ui, 1);
442 }
443 
444 /* Integer versions of util_pack_z and util_pack_z_stencil - useful for
445  * constructing clear masks.
446  */
447 static inline uint32_t
util_pack_mask_z(enum pipe_format format,uint32_t z)448 util_pack_mask_z(enum pipe_format format, uint32_t z)
449 {
450    switch (format) {
451    case PIPE_FORMAT_Z16_UNORM:
452       return z & 0xffff;
453    case PIPE_FORMAT_Z32_UNORM:
454    case PIPE_FORMAT_Z32_FLOAT:
455       return z;
456    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
457    case PIPE_FORMAT_Z24X8_UNORM:
458       return z & 0xffffff;
459    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
460    case PIPE_FORMAT_X8Z24_UNORM:
461       return (z & 0xffffff) << 8;
462    case PIPE_FORMAT_S8_UINT:
463       return 0;
464    default:
465       debug_printf("gallium: unhandled format in util_pack_mask_z(): %s\n",
466                    util_format_name(format));
467       assert(0);
468       return 0;
469    }
470 }
471 
472 
473 static inline uint64_t
util_pack64_mask_z(enum pipe_format format,uint32_t z)474 util_pack64_mask_z(enum pipe_format format, uint32_t z)
475 {
476    switch (format) {
477    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
478       return z;
479    default:
480       return util_pack_mask_z(format, z);
481    }
482 }
483 
484 
485 static inline uint32_t
util_pack_mask_z_stencil(enum pipe_format format,uint32_t z,uint8_t s)486 util_pack_mask_z_stencil(enum pipe_format format, uint32_t z, uint8_t s)
487 {
488    uint32_t packed = util_pack_mask_z(format, z);
489 
490    switch (format) {
491    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
492       packed |= (uint32_t)s << 24;
493       break;
494    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
495       packed |= s;
496       break;
497    case PIPE_FORMAT_S8_UINT:
498       packed |= s;
499       break;
500    default:
501       break;
502    }
503 
504    return packed;
505 }
506 
507 
508 static inline uint64_t
util_pack64_mask_z_stencil(enum pipe_format format,uint32_t z,uint8_t s)509 util_pack64_mask_z_stencil(enum pipe_format format, uint32_t z, uint8_t s)
510 {
511    uint64_t packed;
512 
513    switch (format) {
514    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
515       packed = util_pack64_mask_z(format, z);
516       packed |= (uint64_t)s << 32ull;
517       return packed;
518    default:
519       return util_pack_mask_z_stencil(format, z, s);
520    }
521 }
522 
523 
524 /**
525  * Note: it's assumed that z is in [0,1]
526  */
527 static inline uint32_t
util_pack_z(enum pipe_format format,double z)528 util_pack_z(enum pipe_format format, double z)
529 {
530    union fi fui;
531 
532    if (z == 0.0)
533       return 0;
534 
535    switch (format) {
536    case PIPE_FORMAT_Z16_UNORM:
537       if (z == 1.0)
538          return 0xffff;
539       return (uint32_t) lrint(z * 0xffff);
540    case PIPE_FORMAT_Z32_UNORM:
541       /* special-case to avoid overflow */
542       if (z == 1.0)
543          return 0xffffffff;
544       return (uint32_t) llrint(z * 0xffffffff);
545    case PIPE_FORMAT_Z32_FLOAT:
546       fui.f = (float)z;
547       return fui.ui;
548    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
549    case PIPE_FORMAT_Z24X8_UNORM:
550       if (z == 1.0)
551          return 0xffffff;
552       return (uint32_t) lrint(z * 0xffffff);
553    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
554    case PIPE_FORMAT_X8Z24_UNORM:
555       if (z == 1.0)
556          return 0xffffff00;
557       return ((uint32_t) lrint(z * 0xffffff)) << 8;
558    case PIPE_FORMAT_S8_UINT:
559       /* this case can get it via util_pack_z_stencil() */
560       return 0;
561    default:
562       debug_printf("gallium: unhandled format in util_pack_z(): %s\n",
563                    util_format_name(format));
564       assert(0);
565       return 0;
566    }
567 }
568 
569 
570 static inline uint64_t
util_pack64_z(enum pipe_format format,double z)571 util_pack64_z(enum pipe_format format, double z)
572 {
573    union fi fui;
574 
575    if (z == 0)
576       return 0;
577 
578    switch (format) {
579    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
580       fui.f = (float)z;
581       return fui.ui;
582    default:
583       return util_pack_z(format, z);
584    }
585 }
586 
587 
588 /**
589  * Pack Z and/or stencil values into a 32-bit value described by format.
590  * Note: it's assumed that z is in [0,1] and s in [0,255]
591  */
592 static inline uint32_t
util_pack_z_stencil(enum pipe_format format,double z,uint8_t s)593 util_pack_z_stencil(enum pipe_format format, double z, uint8_t s)
594 {
595    uint32_t packed = util_pack_z(format, z);
596 
597    switch (format) {
598    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
599       packed |= (uint32_t)s << 24;
600       break;
601    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
602       packed |= s;
603       break;
604    case PIPE_FORMAT_S8_UINT:
605       packed |= s;
606       break;
607    default:
608       break;
609    }
610 
611    return packed;
612 }
613 
614 
615 static inline uint64_t
util_pack64_z_stencil(enum pipe_format format,double z,uint8_t s)616 util_pack64_z_stencil(enum pipe_format format, double z, uint8_t s)
617 {
618    uint64_t packed;
619 
620    switch (format) {
621    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
622       packed = util_pack64_z(format, z);
623       packed |= (uint64_t)s << 32ull;
624       break;
625    default:
626       return util_pack_z_stencil(format, z, s);
627    }
628 
629    return packed;
630 }
631 
632 
633 /**
634  * Pack 4 ubytes into a 4-byte word
635  */
636 static inline unsigned
pack_ub4(ubyte b0,ubyte b1,ubyte b2,ubyte b3)637 pack_ub4(ubyte b0, ubyte b1, ubyte b2, ubyte b3)
638 {
639    return ((((unsigned int)b0) << 0) |
640 	   (((unsigned int)b1) << 8) |
641 	   (((unsigned int)b2) << 16) |
642 	   (((unsigned int)b3) << 24));
643 }
644 
645 
646 /**
647  * Pack/convert 4 floats into one 4-byte word.
648  */
649 static inline unsigned
pack_ui32_float4(float a,float b,float c,float d)650 pack_ui32_float4(float a, float b, float c, float d)
651 {
652    return pack_ub4( float_to_ubyte(a),
653 		    float_to_ubyte(b),
654 		    float_to_ubyte(c),
655 		    float_to_ubyte(d) );
656 }
657 
658 
659 
660 #endif /* U_PACK_COLOR_H */
661