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