gallium: added util_draw_vertex_buffer()
[mesa.git] / src / gallium / auxiliary / util / u_pack_color.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Functions to produce packed colors/Z from floats.
31 */
32
33
34 #ifndef U_PACK_COLOR_H
35 #define U_PACK_COLOR_H
36
37
38 #include "pipe/p_compiler.h"
39 #include "pipe/p_format.h"
40
41
42 /**
43 * Note rgba outside [0,1] will be clamped for int pixel formats.
44 */
45 static INLINE void
46 util_pack_color(const float rgba[4], enum pipe_format format, void *dest)
47 {
48 ubyte r, g, b, a;
49
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]);
56 }
57
58 switch (format) {
59 case PIPE_FORMAT_R8G8B8A8_UNORM:
60 {
61 uint *d = (uint *) dest;
62 *d = (r << 24) | (g << 16) | (b << 8) | a;
63 }
64 return;
65 case PIPE_FORMAT_A8R8G8B8_UNORM:
66 {
67 uint *d = (uint *) dest;
68 *d = (a << 24) | (r << 16) | (g << 8) | b;
69 }
70 return;
71 case PIPE_FORMAT_B8G8R8A8_UNORM:
72 {
73 uint *d = (uint *) dest;
74 *d = (b << 24) | (g << 16) | (r << 8) | a;
75 }
76 return;
77 case PIPE_FORMAT_R5G6B5_UNORM:
78 {
79 ushort *d = (ushort *) dest;
80 *d = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
81 }
82 return;
83 case PIPE_FORMAT_R32G32B32A32_FLOAT:
84 {
85 float *d = (float *) dest;
86 d[0] = rgba[0];
87 d[1] = rgba[1];
88 d[2] = rgba[2];
89 d[3] = rgba[3];
90 }
91 return;
92 case PIPE_FORMAT_R32G32B32_FLOAT:
93 {
94 float *d = (float *) dest;
95 d[0] = rgba[0];
96 d[1] = rgba[1];
97 d[2] = rgba[2];
98 }
99 return;
100 /* XXX lots more cases to add */
101 default:
102 debug_printf("gallium: unhandled format in util_pack_color()");
103 }
104 }
105
106
107 /**
108 * Note: it's assumed that z is in [0,1]
109 */
110 static INLINE uint
111 util_pack_z(enum pipe_format format, double z)
112 {
113 switch (format) {
114 case PIPE_FORMAT_Z16_UNORM:
115 return (uint) (z * 0xffff);
116 case PIPE_FORMAT_Z32_UNORM:
117 /* special-case to avoid overflow */
118 if (z == 1.0)
119 return 0xffffffff;
120 else
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;
126 default:
127 debug_printf("gallium: unhandled fomrat in util_pack_z()");
128 return 0;
129 }
130 }
131
132
133 #endif /* U_PACK_COLOR_H */