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 * Note rgba outside [0,1] will be clamped for int pixel formats.
46 util_pack_color(const float rgba
[4], enum pipe_format format
, void *dest
)
50 if (pf_size_x(format
) <= 8) {
51 /* format uses 8-bit components or less */
52 UNCLAMPED_FLOAT_TO_UBYTE(r
, rgba
[0]);
53 UNCLAMPED_FLOAT_TO_UBYTE(g
, rgba
[1]);
54 UNCLAMPED_FLOAT_TO_UBYTE(b
, rgba
[2]);
55 UNCLAMPED_FLOAT_TO_UBYTE(a
, rgba
[3]);
59 case PIPE_FORMAT_R8G8B8A8_UNORM
:
61 uint
*d
= (uint
*) dest
;
62 *d
= (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
65 case PIPE_FORMAT_A8R8G8B8_UNORM
:
67 uint
*d
= (uint
*) dest
;
68 *d
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
71 case PIPE_FORMAT_B8G8R8A8_UNORM
:
73 uint
*d
= (uint
*) dest
;
74 *d
= (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
77 case PIPE_FORMAT_R5G6B5_UNORM
:
79 ushort
*d
= (ushort
*) dest
;
80 *d
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
83 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
85 float *d
= (float *) dest
;
92 case PIPE_FORMAT_R32G32B32_FLOAT
:
94 float *d
= (float *) dest
;
100 /* XXX lots more cases to add */
102 debug_printf("gallium: unhandled format in util_pack_color()");
108 * Note: it's assumed that z is in [0,1]
111 util_pack_z(enum pipe_format format
, double z
)
114 case PIPE_FORMAT_Z16_UNORM
:
115 return (uint
) (z
* 0xffff);
116 case PIPE_FORMAT_Z32_UNORM
:
117 /* special-case to avoid overflow */
121 return (uint
) (z
* 0xffffffff);
122 case PIPE_FORMAT_S8Z24_UNORM
:
123 return (uint
) (z
* 0xffffff);
124 case PIPE_FORMAT_Z24S8_UNORM
:
125 return ((uint
) (z
* 0xffffff)) << 8;
127 debug_printf("gallium: unhandled fomrat in util_pack_z()");
133 #endif /* U_PACK_COLOR_H */