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"
54 * Pack ubyte R,G,B,A into dest pixel.
57 util_pack_color_ub(ubyte r
, ubyte g
, ubyte b
, ubyte a
,
58 enum pipe_format format
, union util_color
*uc
)
61 case PIPE_FORMAT_A8B8G8R8_UNORM
:
63 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
66 case PIPE_FORMAT_X8B8G8R8_UNORM
:
68 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
71 case PIPE_FORMAT_B8G8R8A8_UNORM
:
73 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
76 case PIPE_FORMAT_B8G8R8X8_UNORM
:
78 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
81 case PIPE_FORMAT_A8R8G8B8_UNORM
:
83 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
86 case PIPE_FORMAT_X8R8G8B8_UNORM
:
88 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
91 case PIPE_FORMAT_B5G6R5_UNORM
:
93 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
96 case PIPE_FORMAT_B5G5R5X1_UNORM
:
98 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
101 case PIPE_FORMAT_B5G5R5A1_UNORM
:
103 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
106 case PIPE_FORMAT_B4G4R4A4_UNORM
:
108 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
111 case PIPE_FORMAT_A8_UNORM
:
116 case PIPE_FORMAT_L8_UNORM
:
117 case PIPE_FORMAT_I8_UNORM
:
122 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
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
;
130 case PIPE_FORMAT_R32G32B32_FLOAT
:
132 uc
->f
[0] = (float)r
/ 255.0f
;
133 uc
->f
[1] = (float)g
/ 255.0f
;
134 uc
->f
[2] = (float)b
/ 255.0f
;
138 /* Handle other cases with a generic function.
148 util_format_write_4ub(format
, src
, 0, uc
, 0, 0, 0, 1, 1);
155 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
158 util_unpack_color_ub(enum pipe_format format
, union util_color
*uc
,
159 ubyte
*r
, ubyte
*g
, ubyte
*b
, ubyte
*a
)
162 case PIPE_FORMAT_A8B8G8R8_UNORM
:
165 *r
= (ubyte
) ((p
>> 24) & 0xff);
166 *g
= (ubyte
) ((p
>> 16) & 0xff);
167 *b
= (ubyte
) ((p
>> 8) & 0xff);
168 *a
= (ubyte
) ((p
>> 0) & 0xff);
171 case PIPE_FORMAT_X8B8G8R8_UNORM
:
174 *r
= (ubyte
) ((p
>> 24) & 0xff);
175 *g
= (ubyte
) ((p
>> 16) & 0xff);
176 *b
= (ubyte
) ((p
>> 8) & 0xff);
180 case PIPE_FORMAT_B8G8R8A8_UNORM
:
183 *r
= (ubyte
) ((p
>> 16) & 0xff);
184 *g
= (ubyte
) ((p
>> 8) & 0xff);
185 *b
= (ubyte
) ((p
>> 0) & 0xff);
186 *a
= (ubyte
) ((p
>> 24) & 0xff);
189 case PIPE_FORMAT_B8G8R8X8_UNORM
:
192 *r
= (ubyte
) ((p
>> 16) & 0xff);
193 *g
= (ubyte
) ((p
>> 8) & 0xff);
194 *b
= (ubyte
) ((p
>> 0) & 0xff);
198 case PIPE_FORMAT_A8R8G8B8_UNORM
:
201 *r
= (ubyte
) ((p
>> 8) & 0xff);
202 *g
= (ubyte
) ((p
>> 16) & 0xff);
203 *b
= (ubyte
) ((p
>> 24) & 0xff);
204 *a
= (ubyte
) ((p
>> 0) & 0xff);
207 case PIPE_FORMAT_X8R8G8B8_UNORM
:
210 *r
= (ubyte
) ((p
>> 8) & 0xff);
211 *g
= (ubyte
) ((p
>> 16) & 0xff);
212 *b
= (ubyte
) ((p
>> 24) & 0xff);
216 case PIPE_FORMAT_B5G6R5_UNORM
:
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));
225 case PIPE_FORMAT_B5G5R5X1_UNORM
:
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));
234 case PIPE_FORMAT_B5G5R5A1_UNORM
:
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));
243 case PIPE_FORMAT_B4G4R4A4_UNORM
:
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));
252 case PIPE_FORMAT_A8_UNORM
:
255 *r
= *g
= *b
= (ubyte
) 0xff;
259 case PIPE_FORMAT_L8_UNORM
:
266 case PIPE_FORMAT_I8_UNORM
:
269 *r
= *g
= *b
= *a
= p
;
272 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
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]);
281 case PIPE_FORMAT_R32G32B32_FLOAT
:
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]);
291 case PIPE_FORMAT_R32G32_FLOAT
:
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;
300 case PIPE_FORMAT_R32_FLOAT
:
302 const float *p
= &uc
->f
[0];
303 *r
= float_to_ubyte(p
[0]);
304 *g
= *b
= *a
= (ubyte
) 0xff;
308 /* Handle other cases with a generic function.
314 util_format_read_4ub(format
, dst
, 0, uc
, 0, 0, 0, 1, 1);
325 * Note rgba outside [0,1] will be clamped for int pixel formats.
328 util_pack_color(const float rgba
[4], enum pipe_format format
, union util_color
*uc
)
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]);
344 case PIPE_FORMAT_A8B8G8R8_UNORM
:
346 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
349 case PIPE_FORMAT_X8B8G8R8_UNORM
:
351 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
354 case PIPE_FORMAT_B8G8R8A8_UNORM
:
356 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
359 case PIPE_FORMAT_B8G8R8X8_UNORM
:
361 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
364 case PIPE_FORMAT_A8R8G8B8_UNORM
:
366 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
369 case PIPE_FORMAT_X8R8G8B8_UNORM
:
371 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
374 case PIPE_FORMAT_B5G6R5_UNORM
:
376 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
379 case PIPE_FORMAT_B5G5R5X1_UNORM
:
381 uc
->us
= ((0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
384 case PIPE_FORMAT_B5G5R5A1_UNORM
:
386 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
389 case PIPE_FORMAT_B4G4R4A4_UNORM
:
391 uc
->ub
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
394 case PIPE_FORMAT_A8_UNORM
:
399 case PIPE_FORMAT_L8_UNORM
:
400 case PIPE_FORMAT_I8_UNORM
:
405 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
413 case PIPE_FORMAT_R32G32B32_FLOAT
:
421 /* Handle other cases with a generic function.
424 util_format_write_4f(format
, rgba
, 0, uc
, 0, 0, 0, 1, 1);
430 * Note: it's assumed that z is in [0,1]
433 util_pack_z(enum pipe_format format
, double z
)
439 case PIPE_FORMAT_Z16_UNORM
:
442 return (uint
) (z
* 0xffff);
443 case PIPE_FORMAT_Z32_UNORM
:
444 /* special-case to avoid overflow */
447 return (uint
) (z
* 0xffffffff);
448 case PIPE_FORMAT_Z32_FLOAT
:
450 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
451 case PIPE_FORMAT_Z24X8_UNORM
:
454 return (uint
) (z
* 0xffffff);
455 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
456 case PIPE_FORMAT_X8Z24_UNORM
:
459 return ((uint
) (z
* 0xffffff)) << 8;
460 case PIPE_FORMAT_S8_USCALED
:
461 /* this case can get it via util_pack_z_stencil() */
464 debug_print_format("gallium: unhandled format in util_pack_z()", format
);
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]
476 util_pack_z_stencil(enum pipe_format format
, double z
, uint s
)
478 unsigned packed
= util_pack_z(format
, z
);
481 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
484 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
487 case PIPE_FORMAT_S8_USCALED
:
499 * Pack 4 ubytes into a 4-byte word
501 static INLINE
unsigned
502 pack_ub4(ubyte b0
, ubyte b1
, ubyte b2
, ubyte b3
)
504 return ((((unsigned int)b0
) << 0) |
505 (((unsigned int)b1
) << 8) |
506 (((unsigned int)b2
) << 16) |
507 (((unsigned int)b3
) << 24));
512 * Pack/convert 4 floats into one 4-byte word.
514 static INLINE
unsigned
515 pack_ui32_float4(float a
, float b
, float c
, float d
)
517 return pack_ub4( float_to_ubyte(a
),
525 #endif /* U_PACK_COLOR_H */