Checkpoint of new vbo-building code. Currently builds regular arrays
[mesa.git] / src / mesa / vbo / vbo_exec_eval.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "api_eval.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "math/m_eval.h"
33 #include "vbo_exec.h"
34 #include "dispatch.h"
35
36
37 static void clear_active_eval1( struct vbo_exec_context *exec, GLuint attr )
38 {
39 exec->eval.map1[attr].map = NULL;
40 }
41
42 static void clear_active_eval2( struct vbo_exec_context *exec, GLuint attr )
43 {
44 exec->eval.map2[attr].map = NULL;
45 }
46
47 static void set_active_eval1( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
48 struct gl_1d_map *map )
49 {
50 if (!exec->eval.map1[attr].map) {
51 exec->eval.map1[attr].map = map;
52 exec->eval.map1[attr].sz = dim;
53 }
54 }
55
56 static void set_active_eval2( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
57 struct gl_2d_map *map )
58 {
59 if (!exec->eval.map2[attr].map) {
60 exec->eval.map2[attr].map = map;
61 exec->eval.map2[attr].sz = dim;
62 }
63 }
64
65 void vbo_exec_eval_update( struct vbo_exec_context *exec )
66 {
67 GLcontext *ctx = exec->ctx;
68 GLuint attr;
69
70 /* Vertex program maps have priority over conventional attribs */
71
72 for (attr = 0; attr < VBO_ATTRIB_FIRST_MATERIAL; attr++) {
73 clear_active_eval1( exec, attr );
74 clear_active_eval2( exec, attr );
75 }
76
77 /* _NEW_PROGRAM */
78 if (ctx->VertexProgram._Enabled) {
79 for (attr = 0; attr < VBO_ATTRIB_FIRST_MATERIAL; attr++) {
80 /* _NEW_EVAL */
81 if (ctx->Eval.Map1Attrib[attr])
82 set_active_eval1( exec, attr, 4, &ctx->EvalMap.Map1Attrib[attr] );
83
84 if (ctx->Eval.Map2Attrib[attr])
85 set_active_eval2( exec, attr, 4, &ctx->EvalMap.Map2Attrib[attr] );
86 }
87 }
88
89 if (ctx->Eval.Map1Color4)
90 set_active_eval1( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
91
92 if (ctx->Eval.Map2Color4)
93 set_active_eval2( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );
94
95 if (ctx->Eval.Map1TextureCoord4)
96 set_active_eval1( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
97 else if (ctx->Eval.Map1TextureCoord3)
98 set_active_eval1( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
99 else if (ctx->Eval.Map1TextureCoord2)
100 set_active_eval1( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
101 else if (ctx->Eval.Map1TextureCoord1)
102 set_active_eval1( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );
103
104 if (ctx->Eval.Map2TextureCoord4)
105 set_active_eval2( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
106 else if (ctx->Eval.Map2TextureCoord3)
107 set_active_eval2( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
108 else if (ctx->Eval.Map2TextureCoord2)
109 set_active_eval2( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
110 else if (ctx->Eval.Map2TextureCoord1)
111 set_active_eval2( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );
112
113 if (ctx->Eval.Map1Normal)
114 set_active_eval1( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );
115
116 if (ctx->Eval.Map2Normal)
117 set_active_eval2( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );
118
119 if (ctx->Eval.Map1Vertex4)
120 set_active_eval1( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
121 else if (ctx->Eval.Map1Vertex3)
122 set_active_eval1( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );
123
124 if (ctx->Eval.Map2Vertex4)
125 set_active_eval2( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
126 else if (ctx->Eval.Map2Vertex3)
127 set_active_eval2( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );
128
129 exec->eval.recalculate_maps = 0;
130 }
131
132
133
134 void vbo_exec_do_EvalCoord1f(struct vbo_exec_context *exec, GLfloat u)
135 {
136 GLuint attr;
137
138 for (attr = 1; attr <= VBO_ATTRIB_INDEX; attr++) {
139 struct gl_1d_map *map = exec->eval.map1[attr].map;
140 if (map) {
141 GLfloat uu = (u - map->u1) * map->du;
142 GLfloat data[4];
143
144 ASSIGN_4V(data, 0, 0, 0, 1);
145
146 _math_horner_bezier_curve(map->Points, data, uu,
147 exec->eval.map1[attr].sz,
148 map->Order);
149
150 COPY_SZ_4V( exec->vtx.attrptr[attr],
151 exec->vtx.attrsz[attr],
152 data );
153 }
154 }
155
156 /** Vertex -- EvalCoord1f is a noop if this map not enabled:
157 **/
158 if (exec->eval.map1[0].map) {
159 struct gl_1d_map *map = exec->eval.map1[0].map;
160 GLfloat uu = (u - map->u1) * map->du;
161 GLfloat vertex[4];
162
163 ASSIGN_4V(vertex, 0, 0, 0, 1);
164
165 _math_horner_bezier_curve(map->Points, vertex, uu,
166 exec->eval.map1[0].sz,
167 map->Order);
168
169 if (exec->eval.map1[0].sz == 4)
170 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
171 else
172 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
173 }
174 }
175
176
177
178 void vbo_exec_do_EvalCoord2f( struct vbo_exec_context *exec,
179 GLfloat u, GLfloat v )
180 {
181 GLuint attr;
182
183 for (attr = 1; attr <= VBO_ATTRIB_INDEX; attr++) {
184 struct gl_2d_map *map = exec->eval.map2[attr].map;
185 if (map) {
186 GLfloat uu = (u - map->u1) * map->du;
187 GLfloat vv = (v - map->v1) * map->dv;
188 GLfloat data[4];
189
190 ASSIGN_4V(data, 0, 0, 0, 1);
191
192 _math_horner_bezier_surf(map->Points,
193 data,
194 uu, vv,
195 exec->eval.map2[attr].sz,
196 map->Uorder, map->Vorder);
197
198 COPY_SZ_4V( exec->vtx.attrptr[attr],
199 exec->vtx.attrsz[attr],
200 data );
201 }
202 }
203
204 /** Vertex -- EvalCoord2f is a noop if this map not enabled:
205 **/
206 if (exec->eval.map2[0].map) {
207 struct gl_2d_map *map = exec->eval.map2[0].map;
208 GLfloat uu = (u - map->u1) * map->du;
209 GLfloat vv = (v - map->v1) * map->dv;
210 GLfloat vertex[4];
211
212 ASSIGN_4V(vertex, 0, 0, 0, 1);
213
214 if (exec->ctx->Eval.AutoNormal) {
215 GLfloat normal[4];
216 GLfloat du[4], dv[4];
217
218 _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
219 exec->eval.map2[0].sz,
220 map->Uorder, map->Vorder);
221
222 if (exec->eval.map2[0].sz == 4) {
223 du[0] = du[0]*vertex[3] - du[3]*vertex[0];
224 du[1] = du[1]*vertex[3] - du[3]*vertex[1];
225 du[2] = du[2]*vertex[3] - du[3]*vertex[2];
226
227 dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
228 dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
229 dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
230 }
231
232
233 CROSS3(normal, du, dv);
234 NORMALIZE_3FV(normal);
235 normal[3] = 1.0;
236
237 COPY_SZ_4V( exec->vtx.attrptr[VBO_ATTRIB_NORMAL],
238 exec->vtx.attrsz[VBO_ATTRIB_NORMAL],
239 normal );
240
241 }
242 else {
243 _math_horner_bezier_surf(map->Points, vertex, uu, vv,
244 exec->eval.map2[0].sz,
245 map->Uorder, map->Vorder);
246 }
247
248 if (exec->vtx.attrsz[0] == 4)
249 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
250 else
251 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
252 }
253 }
254
255