1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Functions to produce packed colors/Z from floats.
34 #ifndef U_PACK_COLOR_H
35 #define U_PACK_COLOR_H
38 #include "pipe/p_compiler.h"
39 #include "pipe/p_format.h"
40 #include "util/u_debug_gallium.h"
41 #include "util/u_format.h"
42 #include "util/u_math.h"
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).
55 ushort h
[4]; /* half float */
61 * Pack ubyte R,G,B,A into dest pixel.
64 util_pack_color_ub(ubyte r
, ubyte g
, ubyte b
, ubyte a
,
65 enum pipe_format format
, union util_color
*uc
)
68 case PIPE_FORMAT_ABGR8888_UNORM
:
70 uc
->ui
[0] = (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
73 case PIPE_FORMAT_XBGR8888_UNORM
:
75 uc
->ui
[0] = (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
78 case PIPE_FORMAT_BGRA8888_UNORM
:
80 uc
->ui
[0] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
83 case PIPE_FORMAT_BGRX8888_UNORM
:
85 uc
->ui
[0] = (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
88 case PIPE_FORMAT_ARGB8888_UNORM
:
90 uc
->ui
[0] = (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
93 case PIPE_FORMAT_XRGB8888_UNORM
:
95 uc
->ui
[0] = (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
98 case PIPE_FORMAT_B5G6R5_UNORM
:
100 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
103 case PIPE_FORMAT_B5G5R5X1_UNORM
:
105 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
108 case PIPE_FORMAT_B5G5R5A1_UNORM
:
110 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
113 case PIPE_FORMAT_B4G4R4A4_UNORM
:
115 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
118 case PIPE_FORMAT_A8_UNORM
:
123 case PIPE_FORMAT_L8_UNORM
:
124 case PIPE_FORMAT_I8_UNORM
:
129 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
131 uc
->f
[0] = (float)r
/ 255.0f
;
132 uc
->f
[1] = (float)g
/ 255.0f
;
133 uc
->f
[2] = (float)b
/ 255.0f
;
134 uc
->f
[3] = (float)a
/ 255.0f
;
137 case PIPE_FORMAT_R32G32B32_FLOAT
:
139 uc
->f
[0] = (float)r
/ 255.0f
;
140 uc
->f
[1] = (float)g
/ 255.0f
;
141 uc
->f
[2] = (float)b
/ 255.0f
;
145 /* Handle other cases with a generic function.
155 util_format_write_4ub(format
, src
, 0, uc
, 0, 0, 0, 1, 1);
162 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
165 util_unpack_color_ub(enum pipe_format format
, union util_color
*uc
,
166 ubyte
*r
, ubyte
*g
, ubyte
*b
, ubyte
*a
)
169 case PIPE_FORMAT_ABGR8888_UNORM
:
172 *r
= (ubyte
) ((p
>> 24) & 0xff);
173 *g
= (ubyte
) ((p
>> 16) & 0xff);
174 *b
= (ubyte
) ((p
>> 8) & 0xff);
175 *a
= (ubyte
) ((p
>> 0) & 0xff);
178 case PIPE_FORMAT_XBGR8888_UNORM
:
181 *r
= (ubyte
) ((p
>> 24) & 0xff);
182 *g
= (ubyte
) ((p
>> 16) & 0xff);
183 *b
= (ubyte
) ((p
>> 8) & 0xff);
187 case PIPE_FORMAT_BGRA8888_UNORM
:
190 *r
= (ubyte
) ((p
>> 16) & 0xff);
191 *g
= (ubyte
) ((p
>> 8) & 0xff);
192 *b
= (ubyte
) ((p
>> 0) & 0xff);
193 *a
= (ubyte
) ((p
>> 24) & 0xff);
196 case PIPE_FORMAT_BGRX8888_UNORM
:
199 *r
= (ubyte
) ((p
>> 16) & 0xff);
200 *g
= (ubyte
) ((p
>> 8) & 0xff);
201 *b
= (ubyte
) ((p
>> 0) & 0xff);
205 case PIPE_FORMAT_ARGB8888_UNORM
:
208 *r
= (ubyte
) ((p
>> 8) & 0xff);
209 *g
= (ubyte
) ((p
>> 16) & 0xff);
210 *b
= (ubyte
) ((p
>> 24) & 0xff);
211 *a
= (ubyte
) ((p
>> 0) & 0xff);
214 case PIPE_FORMAT_XRGB8888_UNORM
:
217 *r
= (ubyte
) ((p
>> 8) & 0xff);
218 *g
= (ubyte
) ((p
>> 16) & 0xff);
219 *b
= (ubyte
) ((p
>> 24) & 0xff);
223 case PIPE_FORMAT_B5G6R5_UNORM
:
226 *r
= (ubyte
) (((p
>> 8) & 0xf8) | ((p
>> 13) & 0x7));
227 *g
= (ubyte
) (((p
>> 3) & 0xfc) | ((p
>> 9) & 0x3));
228 *b
= (ubyte
) (((p
<< 3) & 0xf8) | ((p
>> 2) & 0x7));
232 case PIPE_FORMAT_B5G5R5X1_UNORM
:
235 *r
= (ubyte
) (((p
>> 7) & 0xf8) | ((p
>> 12) & 0x7));
236 *g
= (ubyte
) (((p
>> 2) & 0xf8) | ((p
>> 7) & 0x7));
237 *b
= (ubyte
) (((p
<< 3) & 0xf8) | ((p
>> 2) & 0x7));
241 case PIPE_FORMAT_B5G5R5A1_UNORM
:
244 *r
= (ubyte
) (((p
>> 7) & 0xf8) | ((p
>> 12) & 0x7));
245 *g
= (ubyte
) (((p
>> 2) & 0xf8) | ((p
>> 7) & 0x7));
246 *b
= (ubyte
) (((p
<< 3) & 0xf8) | ((p
>> 2) & 0x7));
247 *a
= (ubyte
) (0xff * (p
>> 15));
250 case PIPE_FORMAT_B4G4R4A4_UNORM
:
253 *r
= (ubyte
) (((p
>> 4) & 0xf0) | ((p
>> 8) & 0xf));
254 *g
= (ubyte
) (((p
>> 0) & 0xf0) | ((p
>> 4) & 0xf));
255 *b
= (ubyte
) (((p
<< 4) & 0xf0) | ((p
>> 0) & 0xf));
256 *a
= (ubyte
) (((p
>> 8) & 0xf0) | ((p
>> 12) & 0xf));
259 case PIPE_FORMAT_A8_UNORM
:
262 *r
= *g
= *b
= (ubyte
) 0xff;
266 case PIPE_FORMAT_L8_UNORM
:
273 case PIPE_FORMAT_I8_UNORM
:
276 *r
= *g
= *b
= *a
= p
;
279 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
281 const float *p
= &uc
->f
[0];
282 *r
= float_to_ubyte(p
[0]);
283 *g
= float_to_ubyte(p
[1]);
284 *b
= float_to_ubyte(p
[2]);
285 *a
= float_to_ubyte(p
[3]);
288 case PIPE_FORMAT_R32G32B32_FLOAT
:
290 const float *p
= &uc
->f
[0];
291 *r
= float_to_ubyte(p
[0]);
292 *g
= float_to_ubyte(p
[1]);
293 *b
= float_to_ubyte(p
[2]);
298 case PIPE_FORMAT_R32G32_FLOAT
:
300 const float *p
= &uc
->f
[0];
301 *r
= float_to_ubyte(p
[0]);
302 *g
= float_to_ubyte(p
[1]);
303 *b
= *a
= (ubyte
) 0xff;
307 case PIPE_FORMAT_R32_FLOAT
:
309 const float *p
= &uc
->f
[0];
310 *r
= float_to_ubyte(p
[0]);
311 *g
= *b
= *a
= (ubyte
) 0xff;
315 /* Handle other cases with a generic function.
321 util_format_read_4ub(format
, dst
, 0, uc
, 0, 0, 0, 1, 1);
332 * Note rgba outside [0,1] will be clamped for int pixel formats.
333 * This will not work (and might not really be useful with float input)
334 * for pure integer formats (which lack the pack_rgba_float function).
337 util_pack_color(const float rgba
[4], enum pipe_format format
, union util_color
*uc
)
344 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) <= 8) {
345 /* format uses 8-bit components or less */
346 r
= float_to_ubyte(rgba
[0]);
347 g
= float_to_ubyte(rgba
[1]);
348 b
= float_to_ubyte(rgba
[2]);
349 a
= float_to_ubyte(rgba
[3]);
353 case PIPE_FORMAT_ABGR8888_UNORM
:
355 uc
->ui
[0] = (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
358 case PIPE_FORMAT_XBGR8888_UNORM
:
360 uc
->ui
[0] = (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
363 case PIPE_FORMAT_BGRA8888_UNORM
:
365 uc
->ui
[0] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
368 case PIPE_FORMAT_BGRX8888_UNORM
:
370 uc
->ui
[0] = (0xffu
<< 24) | (r
<< 16) | (g
<< 8) | b
;
373 case PIPE_FORMAT_ARGB8888_UNORM
:
375 uc
->ui
[0] = (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
378 case PIPE_FORMAT_XRGB8888_UNORM
:
380 uc
->ui
[0] = (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
383 case PIPE_FORMAT_B5G6R5_UNORM
:
385 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
388 case PIPE_FORMAT_B5G5R5X1_UNORM
:
390 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
393 case PIPE_FORMAT_B5G5R5A1_UNORM
:
395 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
398 case PIPE_FORMAT_B4G4R4A4_UNORM
:
400 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
403 case PIPE_FORMAT_A8_UNORM
:
408 case PIPE_FORMAT_L8_UNORM
:
409 case PIPE_FORMAT_I8_UNORM
:
414 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
422 case PIPE_FORMAT_R32G32B32_FLOAT
:
430 /* Handle other cases with a generic function.
433 util_format_write_4f(format
, rgba
, 0, uc
, 0, 0, 0, 1, 1);
437 /* Integer versions of util_pack_z and util_pack_z_stencil - useful for
438 * constructing clear masks.
440 static inline uint32_t
441 util_pack_mask_z(enum pipe_format format
, uint32_t z
)
444 case PIPE_FORMAT_Z16_UNORM
:
446 case PIPE_FORMAT_Z32_UNORM
:
447 case PIPE_FORMAT_Z32_FLOAT
:
449 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
450 case PIPE_FORMAT_Z24X8_UNORM
:
452 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
453 case PIPE_FORMAT_X8Z24_UNORM
:
454 return (z
& 0xffffff) << 8;
455 case PIPE_FORMAT_S8_UINT
:
458 debug_print_format("gallium: unhandled format in util_pack_mask_z()", format
);
465 static inline uint64_t
466 util_pack64_mask_z(enum pipe_format format
, uint32_t z
)
469 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
472 return util_pack_mask_z(format
, z
);
477 static inline uint32_t
478 util_pack_mask_z_stencil(enum pipe_format format
, uint32_t z
, uint8_t s
)
480 uint32_t packed
= util_pack_mask_z(format
, z
);
483 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
484 packed
|= (uint32_t)s
<< 24;
486 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
489 case PIPE_FORMAT_S8_UINT
:
500 static inline uint64_t
501 util_pack64_mask_z_stencil(enum pipe_format format
, uint32_t z
, uint8_t s
)
506 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
507 packed
= util_pack64_mask_z(format
, z
);
508 packed
|= (uint64_t)s
<< 32ull;
511 return util_pack_mask_z_stencil(format
, z
, s
);
517 * Note: it's assumed that z is in [0,1]
519 static inline uint32_t
520 util_pack_z(enum pipe_format format
, double z
)
528 case PIPE_FORMAT_Z16_UNORM
:
531 return (uint32_t) lrint(z
* 0xffff);
532 case PIPE_FORMAT_Z32_UNORM
:
533 /* special-case to avoid overflow */
536 return (uint32_t) llrint(z
* 0xffffffff);
537 case PIPE_FORMAT_Z32_FLOAT
:
540 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
541 case PIPE_FORMAT_Z24X8_UNORM
:
544 return (uint32_t) lrint(z
* 0xffffff);
545 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
546 case PIPE_FORMAT_X8Z24_UNORM
:
549 return ((uint32_t) lrint(z
* 0xffffff)) << 8;
550 case PIPE_FORMAT_S8_UINT
:
551 /* this case can get it via util_pack_z_stencil() */
554 debug_print_format("gallium: unhandled format in util_pack_z()", format
);
561 static inline uint64_t
562 util_pack64_z(enum pipe_format format
, double z
)
570 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
574 return util_pack_z(format
, z
);
580 * Pack Z and/or stencil values into a 32-bit value described by format.
581 * Note: it's assumed that z is in [0,1] and s in [0,255]
583 static inline uint32_t
584 util_pack_z_stencil(enum pipe_format format
, double z
, uint8_t s
)
586 uint32_t packed
= util_pack_z(format
, z
);
589 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
590 packed
|= (uint32_t)s
<< 24;
592 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
595 case PIPE_FORMAT_S8_UINT
:
606 static inline uint64_t
607 util_pack64_z_stencil(enum pipe_format format
, double z
, uint8_t s
)
612 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
613 packed
= util_pack64_z(format
, z
);
614 packed
|= (uint64_t)s
<< 32ull;
617 return util_pack_z_stencil(format
, z
, s
);
625 * Pack 4 ubytes into a 4-byte word
627 static inline unsigned
628 pack_ub4(ubyte b0
, ubyte b1
, ubyte b2
, ubyte b3
)
630 return ((((unsigned int)b0
) << 0) |
631 (((unsigned int)b1
) << 8) |
632 (((unsigned int)b2
) << 16) |
633 (((unsigned int)b3
) << 24));
638 * Pack/convert 4 floats into one 4-byte word.
640 static inline unsigned
641 pack_ui32_float4(float a
, float b
, float c
, float d
)
643 return pack_ub4( float_to_ubyte(a
),
651 #endif /* U_PACK_COLOR_H */