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"
43 * Pack ubyte R,G,B,A into dest pixel.
46 util_pack_color_ub(ubyte r
, ubyte g
, ubyte b
, ubyte a
,
47 enum pipe_format format
, void *dest
)
50 case PIPE_FORMAT_R8G8B8A8_UNORM
:
52 uint
*d
= (uint
*) dest
;
53 *d
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
56 case PIPE_FORMAT_R8G8B8X8_UNORM
:
58 uint
*d
= (uint
*) dest
;
59 *d
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
62 case PIPE_FORMAT_A8R8G8B8_UNORM
:
64 uint
*d
= (uint
*) dest
;
65 *d
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
68 case PIPE_FORMAT_X8R8G8B8_UNORM
:
70 uint
*d
= (uint
*) dest
;
71 *d
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
74 case PIPE_FORMAT_B8G8R8A8_UNORM
:
76 uint
*d
= (uint
*) dest
;
77 *d
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
80 case PIPE_FORMAT_B8G8R8X8_UNORM
:
82 uint
*d
= (uint
*) dest
;
83 *d
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
86 case PIPE_FORMAT_R5G6B5_UNORM
:
88 ushort
*d
= (ushort
*) dest
;
89 *d
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
92 case PIPE_FORMAT_A1R5G5B5_UNORM
:
94 ushort
*d
= (ushort
*) dest
;
95 *d
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
98 case PIPE_FORMAT_A4R4G4B4_UNORM
:
100 ushort
*d
= (ushort
*) dest
;
101 *d
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
104 case PIPE_FORMAT_A8_UNORM
:
106 ubyte
*d
= (ubyte
*) dest
;
110 case PIPE_FORMAT_L8_UNORM
:
111 case PIPE_FORMAT_I8_UNORM
:
113 ubyte
*d
= (ubyte
*) dest
;
117 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
119 float *d
= (float *) dest
;
120 d
[0] = (float)r
/ 255.0f
;
121 d
[1] = (float)g
/ 255.0f
;
122 d
[2] = (float)b
/ 255.0f
;
123 d
[3] = (float)a
/ 255.0f
;
126 case PIPE_FORMAT_R32G32B32_FLOAT
:
128 float *d
= (float *) dest
;
129 d
[0] = (float)r
/ 255.0f
;
130 d
[1] = (float)g
/ 255.0f
;
131 d
[2] = (float)b
/ 255.0f
;
135 /* XXX lots more cases to add */
137 debug_print_format("gallium: unhandled format in util_pack_color_ub()", format
);
144 * Note rgba outside [0,1] will be clamped for int pixel formats.
147 util_pack_color(const float rgba
[4], enum pipe_format format
, void *dest
)
151 if (pf_size_x(format
) <= 8) {
152 /* format uses 8-bit components or less */
153 UNCLAMPED_FLOAT_TO_UBYTE(r
, rgba
[0]);
154 UNCLAMPED_FLOAT_TO_UBYTE(g
, rgba
[1]);
155 UNCLAMPED_FLOAT_TO_UBYTE(b
, rgba
[2]);
156 UNCLAMPED_FLOAT_TO_UBYTE(a
, rgba
[3]);
160 case PIPE_FORMAT_R8G8B8A8_UNORM
:
162 uint
*d
= (uint
*) dest
;
163 *d
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
166 case PIPE_FORMAT_R8G8B8X8_UNORM
:
168 uint
*d
= (uint
*) dest
;
169 *d
= (r
<< 24) | (g
<< 16) | (b
<< 8) | 0xff;
172 case PIPE_FORMAT_A8R8G8B8_UNORM
:
174 uint
*d
= (uint
*) dest
;
175 *d
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
178 case PIPE_FORMAT_X8R8G8B8_UNORM
:
180 uint
*d
= (uint
*) dest
;
181 *d
= (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
184 case PIPE_FORMAT_B8G8R8A8_UNORM
:
186 uint
*d
= (uint
*) dest
;
187 *d
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
190 case PIPE_FORMAT_B8G8R8X8_UNORM
:
192 uint
*d
= (uint
*) dest
;
193 *d
= (b
<< 24) | (g
<< 16) | (r
<< 8) | 0xff;
196 case PIPE_FORMAT_R5G6B5_UNORM
:
198 ushort
*d
= (ushort
*) dest
;
199 *d
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
202 case PIPE_FORMAT_A1R5G5B5_UNORM
:
204 ushort
*d
= (ushort
*) dest
;
205 *d
= ((a
& 0x80) << 8) | ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | (b
>> 3);
208 case PIPE_FORMAT_A4R4G4B4_UNORM
:
210 ushort
*d
= (ushort
*) dest
;
211 *d
= ((a
& 0xf0) << 8) | ((r
& 0xf0) << 4) | ((g
& 0xf0) << 0) | (b
>> 4);
214 case PIPE_FORMAT_A8_UNORM
:
216 ubyte
*d
= (ubyte
*) dest
;
220 case PIPE_FORMAT_L8_UNORM
:
221 case PIPE_FORMAT_I8_UNORM
:
223 ubyte
*d
= (ubyte
*) dest
;
227 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
229 float *d
= (float *) dest
;
236 case PIPE_FORMAT_R32G32B32_FLOAT
:
238 float *d
= (float *) dest
;
244 /* XXX lots more cases to add */
246 debug_print_format("gallium: unhandled format in util_pack_color()", format
);
253 * Note: it's assumed that z is in [0,1]
256 util_pack_z(enum pipe_format format
, double z
)
259 case PIPE_FORMAT_Z16_UNORM
:
260 return (uint
) (z
* 0xffff);
261 case PIPE_FORMAT_Z32_UNORM
:
262 /* special-case to avoid overflow */
266 return (uint
) (z
* 0xffffffff);
267 case PIPE_FORMAT_S8Z24_UNORM
:
268 case PIPE_FORMAT_X8Z24_UNORM
:
269 return (uint
) (z
* 0xffffff);
270 case PIPE_FORMAT_Z24S8_UNORM
:
271 case PIPE_FORMAT_Z24X8_UNORM
:
272 return ((uint
) (z
* 0xffffff)) << 8;
274 debug_print_format("gallium: unhandled format in util_pack_z()", format
);
281 #endif /* U_PACK_COLOR_H */