1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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.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).
60 * Pack ubyte R,G,B,A into dest pixel.
63 util_pack_color_ub(ubyte r
, ubyte g
, ubyte b
, ubyte a
,
64 enum pipe_format format
, union util_color
*uc
)
67 case PIPE_FORMAT_A8B8G8R8_UNORM
:
69 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
72 case PIPE_FORMAT_X8B8G8R8_UNORM
:
74 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
77 case PIPE_FORMAT_B8G8R8A8_UNORM
:
79 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
82 case PIPE_FORMAT_B8G8R8X8_UNORM
:
84 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
87 case PIPE_FORMAT_A8R8G8B8_UNORM
:
89 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
92 case PIPE_FORMAT_X8R8G8B8_UNORM
:
94 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
97 case PIPE_FORMAT_B5G6R5_UNORM
:
99 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
102 case PIPE_FORMAT_B5G5R5X1_UNORM
:
104 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
107 case PIPE_FORMAT_B5G5R5A1_UNORM
:
109 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
112 case PIPE_FORMAT_B4G4R4A4_UNORM
:
114 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
117 case PIPE_FORMAT_A8_UNORM
:
122 case PIPE_FORMAT_L8_UNORM
:
123 case PIPE_FORMAT_I8_UNORM
:
128 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
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
;
136 case PIPE_FORMAT_R32G32B32_FLOAT
:
138 uc
->f
[0] = (float)r
/ 255.0f
;
139 uc
->f
[1] = (float)g
/ 255.0f
;
140 uc
->f
[2] = (float)b
/ 255.0f
;
144 /* Handle other cases with a generic function.
154 util_format_write_4ub(format
, src
, 0, uc
, 0, 0, 0, 1, 1);
161 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
164 util_unpack_color_ub(enum pipe_format format
, union util_color
*uc
,
165 ubyte
*r
, ubyte
*g
, ubyte
*b
, ubyte
*a
)
168 case PIPE_FORMAT_A8B8G8R8_UNORM
:
171 *r
= (ubyte
) ((p
>> 24) & 0xff);
172 *g
= (ubyte
) ((p
>> 16) & 0xff);
173 *b
= (ubyte
) ((p
>> 8) & 0xff);
174 *a
= (ubyte
) ((p
>> 0) & 0xff);
177 case PIPE_FORMAT_X8B8G8R8_UNORM
:
180 *r
= (ubyte
) ((p
>> 24) & 0xff);
181 *g
= (ubyte
) ((p
>> 16) & 0xff);
182 *b
= (ubyte
) ((p
>> 8) & 0xff);
186 case PIPE_FORMAT_B8G8R8A8_UNORM
:
189 *r
= (ubyte
) ((p
>> 16) & 0xff);
190 *g
= (ubyte
) ((p
>> 8) & 0xff);
191 *b
= (ubyte
) ((p
>> 0) & 0xff);
192 *a
= (ubyte
) ((p
>> 24) & 0xff);
195 case PIPE_FORMAT_B8G8R8X8_UNORM
:
198 *r
= (ubyte
) ((p
>> 16) & 0xff);
199 *g
= (ubyte
) ((p
>> 8) & 0xff);
200 *b
= (ubyte
) ((p
>> 0) & 0xff);
204 case PIPE_FORMAT_A8R8G8B8_UNORM
:
207 *r
= (ubyte
) ((p
>> 8) & 0xff);
208 *g
= (ubyte
) ((p
>> 16) & 0xff);
209 *b
= (ubyte
) ((p
>> 24) & 0xff);
210 *a
= (ubyte
) ((p
>> 0) & 0xff);
213 case PIPE_FORMAT_X8R8G8B8_UNORM
:
216 *r
= (ubyte
) ((p
>> 8) & 0xff);
217 *g
= (ubyte
) ((p
>> 16) & 0xff);
218 *b
= (ubyte
) ((p
>> 24) & 0xff);
222 case PIPE_FORMAT_B5G6R5_UNORM
:
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));
231 case PIPE_FORMAT_B5G5R5X1_UNORM
:
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));
240 case PIPE_FORMAT_B5G5R5A1_UNORM
:
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));
249 case PIPE_FORMAT_B4G4R4A4_UNORM
:
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));
258 case PIPE_FORMAT_A8_UNORM
:
261 *r
= *g
= *b
= (ubyte
) 0xff;
265 case PIPE_FORMAT_L8_UNORM
:
272 case PIPE_FORMAT_I8_UNORM
:
275 *r
= *g
= *b
= *a
= p
;
278 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
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]);
287 case PIPE_FORMAT_R32G32B32_FLOAT
:
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]);
297 case PIPE_FORMAT_R32G32_FLOAT
:
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;
306 case PIPE_FORMAT_R32_FLOAT
:
308 const float *p
= &uc
->f
[0];
309 *r
= float_to_ubyte(p
[0]);
310 *g
= *b
= *a
= (ubyte
) 0xff;
314 /* Handle other cases with a generic function.
320 util_format_read_4ub(format
, dst
, 0, uc
, 0, 0, 0, 1, 1);
331 * Note rgba outside [0,1] will be clamped for int pixel formats.
334 util_pack_color(const float rgba
[4], enum pipe_format format
, union util_color
*uc
)
341 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) <= 8) {
342 /* format uses 8-bit components or less */
343 r
= float_to_ubyte(rgba
[0]);
344 g
= float_to_ubyte(rgba
[1]);
345 b
= float_to_ubyte(rgba
[2]);
346 a
= float_to_ubyte(rgba
[3]);
350 case PIPE_FORMAT_A8B8G8R8_UNORM
:
352 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
355 case PIPE_FORMAT_X8B8G8R8_UNORM
:
357 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
360 case PIPE_FORMAT_B8G8R8A8_UNORM
:
362 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
365 case PIPE_FORMAT_B8G8R8X8_UNORM
:
367 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
370 case PIPE_FORMAT_A8R8G8B8_UNORM
:
372 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
375 case PIPE_FORMAT_X8R8G8B8_UNORM
:
377 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
380 case PIPE_FORMAT_B5G6R5_UNORM
:
382 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
385 case PIPE_FORMAT_B5G5R5X1_UNORM
:
387 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
390 case PIPE_FORMAT_B5G5R5A1_UNORM
:
392 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
395 case PIPE_FORMAT_B4G4R4A4_UNORM
:
397 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
400 case PIPE_FORMAT_A8_UNORM
:
405 case PIPE_FORMAT_L8_UNORM
:
406 case PIPE_FORMAT_I8_UNORM
:
411 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
419 case PIPE_FORMAT_R32G32B32_FLOAT
:
427 /* Handle other cases with a generic function.
430 util_format_write_4f(format
, rgba
, 0, uc
, 0, 0, 0, 1, 1);
434 /* Integer versions of util_pack_z and util_pack_z_stencil - useful for
435 * constructing clear masks.
437 static INLINE
uint32_t
438 util_pack_mask_z(enum pipe_format format
, uint32_t z
)
441 case PIPE_FORMAT_Z16_UNORM
:
443 case PIPE_FORMAT_Z32_UNORM
:
444 case PIPE_FORMAT_Z32_FLOAT
:
446 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
447 case PIPE_FORMAT_Z24X8_UNORM
:
449 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
450 case PIPE_FORMAT_X8Z24_UNORM
:
451 return (z
& 0xffffff) << 8;
452 case PIPE_FORMAT_S8_USCALED
:
455 debug_print_format("gallium: unhandled format in util_pack_mask_z()", format
);
462 static INLINE
uint64_t
463 util_pack64_mask_z(enum pipe_format format
, uint32_t z
)
466 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
469 return util_pack_mask_z(format
, z
);
474 static INLINE
uint32_t
475 util_pack_mask_z_stencil(enum pipe_format format
, uint32_t z
, uint8_t s
)
477 uint32_t packed
= util_pack_mask_z(format
, z
);
480 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
481 packed
|= (uint32_t)s
<< 24;
483 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
486 case PIPE_FORMAT_S8_USCALED
:
497 static INLINE
uint64_t
498 util_pack64_mask_z_stencil(enum pipe_format format
, uint32_t z
, uint8_t s
)
503 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
504 packed
= util_pack64_mask_z(format
, z
);
505 packed
|= (uint64_t)s
<< 32ull;
508 return util_pack_mask_z_stencil(format
, z
, s
);
514 * Note: it's assumed that z is in [0,1]
516 static INLINE
uint32_t
517 util_pack_z(enum pipe_format format
, double z
)
525 case PIPE_FORMAT_Z16_UNORM
:
528 return (uint32_t) (z
* 0xffff);
529 case PIPE_FORMAT_Z32_UNORM
:
530 /* special-case to avoid overflow */
533 return (uint32_t) (z
* 0xffffffff);
534 case PIPE_FORMAT_Z32_FLOAT
:
537 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
538 case PIPE_FORMAT_Z24X8_UNORM
:
541 return (uint32_t) (z
* 0xffffff);
542 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
543 case PIPE_FORMAT_X8Z24_UNORM
:
546 return ((uint32_t) (z
* 0xffffff)) << 8;
547 case PIPE_FORMAT_S8_USCALED
:
548 /* this case can get it via util_pack_z_stencil() */
551 debug_print_format("gallium: unhandled format in util_pack_z()", format
);
558 static INLINE
uint64_t
559 util_pack64_z(enum pipe_format format
, double z
)
567 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
571 return util_pack_z(format
, z
);
577 * Pack Z and/or stencil values into a 32-bit value described by format.
578 * Note: it's assumed that z is in [0,1] and s in [0,255]
580 static INLINE
uint32_t
581 util_pack_z_stencil(enum pipe_format format
, double z
, uint8_t s
)
583 uint32_t packed
= util_pack_z(format
, z
);
586 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
587 packed
|= (uint32_t)s
<< 24;
589 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
592 case PIPE_FORMAT_S8_USCALED
:
603 static INLINE
uint64_t
604 util_pack64_z_stencil(enum pipe_format format
, double z
, uint8_t s
)
609 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
610 packed
= util_pack64_z(format
, z
);
611 packed
|= (uint64_t)s
<< 32ull;
614 return util_pack_z_stencil(format
, z
, s
);
622 * Pack 4 ubytes into a 4-byte word
624 static INLINE
unsigned
625 pack_ub4(ubyte b0
, ubyte b1
, ubyte b2
, ubyte b3
)
627 return ((((unsigned int)b0
) << 0) |
628 (((unsigned int)b1
) << 8) |
629 (((unsigned int)b2
) << 16) |
630 (((unsigned int)b3
) << 24));
635 * Pack/convert 4 floats into one 4-byte word.
637 static INLINE
unsigned
638 pack_ui32_float4(float a
, float b
, float c
, float d
)
640 return pack_ub4( float_to_ubyte(a
),
648 #endif /* U_PACK_COLOR_H */