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_format.h"
41 #include "util/u_math.h"
53 * Pack ubyte R,G,B,A into dest pixel.
56 util_pack_color_ub(ubyte r
, ubyte g
, ubyte b
, ubyte a
,
57 enum pipe_format format
, union util_color
*uc
)
60 case PIPE_FORMAT_R8G8B8A8_UNORM
:
62 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
65 case PIPE_FORMAT_R8G8B8X8_UNORM
:
67 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
70 case PIPE_FORMAT_A8R8G8B8_UNORM
:
72 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
75 case PIPE_FORMAT_X8R8G8B8_UNORM
:
77 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
80 case PIPE_FORMAT_B8G8R8A8_UNORM
:
82 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
85 case PIPE_FORMAT_B8G8R8X8_UNORM
:
87 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
90 case PIPE_FORMAT_R5G6B5_UNORM
:
92 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
95 case PIPE_FORMAT_A1R5G5B5_UNORM
:
97 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
100 case PIPE_FORMAT_A4R4G4B4_UNORM
:
102 uc
->us
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
105 case PIPE_FORMAT_A8_UNORM
:
110 case PIPE_FORMAT_L8_UNORM
:
111 case PIPE_FORMAT_I8_UNORM
:
116 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
118 uc
->f
[0] = (float)r
/ 255.0f
;
119 uc
->f
[1] = (float)g
/ 255.0f
;
120 uc
->f
[2] = (float)b
/ 255.0f
;
121 uc
->f
[3] = (float)a
/ 255.0f
;
124 case PIPE_FORMAT_R32G32B32_FLOAT
:
126 uc
->f
[0] = (float)r
/ 255.0f
;
127 uc
->f
[1] = (float)g
/ 255.0f
;
128 uc
->f
[2] = (float)b
/ 255.0f
;
132 /* Handle other cases with a generic function.
142 util_format_write_4ub(format
, src
, 0, uc
, 0, 0, 0, 1, 1);
149 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
152 util_unpack_color_ub(enum pipe_format format
, union util_color
*uc
,
153 ubyte
*r
, ubyte
*g
, ubyte
*b
, ubyte
*a
)
156 case PIPE_FORMAT_R8G8B8A8_UNORM
:
159 *r
= (ubyte
) ((p
>> 24) & 0xff);
160 *g
= (ubyte
) ((p
>> 16) & 0xff);
161 *b
= (ubyte
) ((p
>> 8) & 0xff);
162 *a
= (ubyte
) ((p
>> 0) & 0xff);
165 case PIPE_FORMAT_R8G8B8X8_UNORM
:
168 *r
= (ubyte
) ((p
>> 24) & 0xff);
169 *g
= (ubyte
) ((p
>> 16) & 0xff);
170 *b
= (ubyte
) ((p
>> 8) & 0xff);
174 case PIPE_FORMAT_A8R8G8B8_UNORM
:
177 *r
= (ubyte
) ((p
>> 16) & 0xff);
178 *g
= (ubyte
) ((p
>> 8) & 0xff);
179 *b
= (ubyte
) ((p
>> 0) & 0xff);
180 *a
= (ubyte
) ((p
>> 24) & 0xff);
183 case PIPE_FORMAT_X8R8G8B8_UNORM
:
186 *r
= (ubyte
) ((p
>> 16) & 0xff);
187 *g
= (ubyte
) ((p
>> 8) & 0xff);
188 *b
= (ubyte
) ((p
>> 0) & 0xff);
192 case PIPE_FORMAT_B8G8R8A8_UNORM
:
195 *r
= (ubyte
) ((p
>> 8) & 0xff);
196 *g
= (ubyte
) ((p
>> 16) & 0xff);
197 *b
= (ubyte
) ((p
>> 24) & 0xff);
198 *a
= (ubyte
) ((p
>> 0) & 0xff);
201 case PIPE_FORMAT_B8G8R8X8_UNORM
:
204 *r
= (ubyte
) ((p
>> 8) & 0xff);
205 *g
= (ubyte
) ((p
>> 16) & 0xff);
206 *b
= (ubyte
) ((p
>> 24) & 0xff);
210 case PIPE_FORMAT_R5G6B5_UNORM
:
213 *r
= (ubyte
) (((p
>> 8) & 0xf8) | ((p
>> 13) & 0x7));
214 *g
= (ubyte
) (((p
>> 3) & 0xfc) | ((p
>> 9) & 0x3));
215 *b
= (ubyte
) (((p
<< 3) & 0xf8) | ((p
>> 2) & 0x7));
219 case PIPE_FORMAT_A1R5G5B5_UNORM
:
222 *r
= (ubyte
) (((p
>> 7) & 0xf8) | ((p
>> 12) & 0x7));
223 *g
= (ubyte
) (((p
>> 2) & 0xf8) | ((p
>> 7) & 0x7));
224 *b
= (ubyte
) (((p
<< 3) & 0xf8) | ((p
>> 2) & 0x7));
225 *a
= (ubyte
) (0xff * (p
>> 15));
228 case PIPE_FORMAT_A4R4G4B4_UNORM
:
231 *r
= (ubyte
) (((p
>> 4) & 0xf0) | ((p
>> 8) & 0xf));
232 *g
= (ubyte
) (((p
>> 0) & 0xf0) | ((p
>> 4) & 0xf));
233 *b
= (ubyte
) (((p
<< 4) & 0xf0) | ((p
>> 0) & 0xf));
234 *a
= (ubyte
) (((p
>> 8) & 0xf0) | ((p
>> 12) & 0xf));
237 case PIPE_FORMAT_A8_UNORM
:
240 *r
= *g
= *b
= (ubyte
) 0xff;
244 case PIPE_FORMAT_L8_UNORM
:
251 case PIPE_FORMAT_I8_UNORM
:
254 *r
= *g
= *b
= *a
= p
;
257 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
259 const float *p
= &uc
->f
[0];
260 *r
= float_to_ubyte(p
[0]);
261 *g
= float_to_ubyte(p
[1]);
262 *b
= float_to_ubyte(p
[2]);
263 *a
= float_to_ubyte(p
[3]);
266 case PIPE_FORMAT_R32G32B32_FLOAT
:
268 const float *p
= &uc
->f
[0];
269 *r
= float_to_ubyte(p
[0]);
270 *g
= float_to_ubyte(p
[1]);
271 *b
= float_to_ubyte(p
[2]);
276 case PIPE_FORMAT_R32G32_FLOAT
:
278 const float *p
= &uc
->f
[0];
279 *r
= float_to_ubyte(p
[0]);
280 *g
= float_to_ubyte(p
[1]);
281 *b
= *a
= (ubyte
) 0xff;
285 case PIPE_FORMAT_R32_FLOAT
:
287 const float *p
= &uc
->f
[0];
288 *r
= float_to_ubyte(p
[0]);
289 *g
= *b
= *a
= (ubyte
) 0xff;
293 /* Handle other cases with a generic function.
299 util_format_read_4ub(format
, dst
, 0, uc
, 0, 0, 0, 1, 1);
310 * Note rgba outside [0,1] will be clamped for int pixel formats.
313 util_pack_color(const float rgba
[4], enum pipe_format format
, union util_color
*uc
)
320 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) <= 8) {
321 /* format uses 8-bit components or less */
322 r
= float_to_ubyte(rgba
[0]);
323 g
= float_to_ubyte(rgba
[1]);
324 b
= float_to_ubyte(rgba
[2]);
325 a
= float_to_ubyte(rgba
[3]);
329 case PIPE_FORMAT_R8G8B8A8_UNORM
:
331 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
334 case PIPE_FORMAT_R8G8B8X8_UNORM
:
336 uc
->ui
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
339 case PIPE_FORMAT_A8R8G8B8_UNORM
:
341 uc
->ui
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
344 case PIPE_FORMAT_X8R8G8B8_UNORM
:
346 uc
->ui
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
349 case PIPE_FORMAT_B8G8R8A8_UNORM
:
351 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
354 case PIPE_FORMAT_B8G8R8X8_UNORM
:
356 uc
->ui
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
359 case PIPE_FORMAT_R5G6B5_UNORM
:
361 uc
->us
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
364 case PIPE_FORMAT_A1R5G5B5_UNORM
:
366 uc
->us
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
369 case PIPE_FORMAT_A4R4G4B4_UNORM
:
371 uc
->ub
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
374 case PIPE_FORMAT_A8_UNORM
:
379 case PIPE_FORMAT_L8_UNORM
:
380 case PIPE_FORMAT_I8_UNORM
:
385 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
393 case PIPE_FORMAT_R32G32B32_FLOAT
:
401 /* Handle other cases with a generic function.
404 util_format_write_4f(format
, rgba
, 0, uc
, 0, 0, 0, 1, 1);
410 * Note: it's assumed that z is in [0,1]
413 util_pack_z(enum pipe_format format
, double z
)
419 case PIPE_FORMAT_Z16_UNORM
:
422 return (uint
) (z
* 0xffff);
423 case PIPE_FORMAT_Z32_UNORM
:
424 /* special-case to avoid overflow */
427 return (uint
) (z
* 0xffffffff);
428 case PIPE_FORMAT_Z32_FLOAT
:
430 case PIPE_FORMAT_S8Z24_UNORM
:
431 case PIPE_FORMAT_X8Z24_UNORM
:
434 return (uint
) (z
* 0xffffff);
435 case PIPE_FORMAT_Z24S8_UNORM
:
436 case PIPE_FORMAT_Z24X8_UNORM
:
439 return ((uint
) (z
* 0xffffff)) << 8;
440 case PIPE_FORMAT_S8_UNORM
:
441 /* this case can get it via util_pack_z_stencil() */
444 debug_print_format("gallium: unhandled format in util_pack_z()", format
);
452 * Pack Z and/or stencil values into a 32-bit value described by format.
453 * Note: it's assumed that z is in [0,1] and s in [0,255]
456 util_pack_z_stencil(enum pipe_format format
, double z
, uint s
)
458 unsigned packed
= util_pack_z(format
, z
);
461 case PIPE_FORMAT_S8Z24_UNORM
:
464 case PIPE_FORMAT_Z24S8_UNORM
:
467 case PIPE_FORMAT_S8_UNORM
:
479 * Pack 4 ubytes into a 4-byte word
481 static INLINE
unsigned
482 pack_ub4(ubyte b0
, ubyte b1
, ubyte b2
, ubyte b3
)
484 return ((((unsigned int)b0
) << 0) |
485 (((unsigned int)b1
) << 8) |
486 (((unsigned int)b2
) << 16) |
487 (((unsigned int)b3
) << 24));
492 * Pack/convert 4 floats into one 4-byte word.
494 static INLINE
unsigned
495 pack_ui32_float4(float a
, float b
, float c
, float d
)
497 return pack_ub4( float_to_ubyte(a
),
505 #endif /* U_PACK_COLOR_H */