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 "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
35
36static void clear_active_eval1( struct vbo_exec_context *exec, GLuint attr )
37{
38   assert(attr < Elements(exec->eval.map1));
39   exec->eval.map1[attr].map = NULL;
40}
41
42static void clear_active_eval2( struct vbo_exec_context *exec, GLuint attr )
43{
44   assert(attr < Elements(exec->eval.map2));
45   exec->eval.map2[attr].map = NULL;
46}
47
48static void set_active_eval1( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
49			      struct gl_1d_map *map )
50{
51   assert(attr < Elements(exec->eval.map1));
52   if (!exec->eval.map1[attr].map) {
53      exec->eval.map1[attr].map = map;
54      exec->eval.map1[attr].sz = dim;
55   }
56}
57
58static void set_active_eval2( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
59			      struct gl_2d_map *map )
60{
61   assert(attr < Elements(exec->eval.map2));
62   if (!exec->eval.map2[attr].map) {
63      exec->eval.map2[attr].map = map;
64      exec->eval.map2[attr].sz = dim;
65   }
66}
67
68void vbo_exec_eval_update( struct vbo_exec_context *exec )
69{
70   struct gl_context *ctx = exec->ctx;
71   GLuint attr;
72
73   /* Vertex program maps have priority over conventional attribs */
74
75   for (attr = 0; attr < VBO_ATTRIB_FIRST_MATERIAL; attr++) {
76      clear_active_eval1( exec, attr );
77      clear_active_eval2( exec, attr );
78   }
79
80   if (ctx->Eval.Map1Color4)
81      set_active_eval1( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
82
83   if (ctx->Eval.Map2Color4)
84      set_active_eval2( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );
85
86   if (ctx->Eval.Map1TextureCoord4)
87      set_active_eval1( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
88   else if (ctx->Eval.Map1TextureCoord3)
89      set_active_eval1( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
90   else if (ctx->Eval.Map1TextureCoord2)
91      set_active_eval1( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
92   else if (ctx->Eval.Map1TextureCoord1)
93      set_active_eval1( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );
94
95   if (ctx->Eval.Map2TextureCoord4)
96      set_active_eval2( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
97   else if (ctx->Eval.Map2TextureCoord3)
98      set_active_eval2( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
99   else if (ctx->Eval.Map2TextureCoord2)
100      set_active_eval2( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
101   else if (ctx->Eval.Map2TextureCoord1)
102      set_active_eval2( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );
103
104   if (ctx->Eval.Map1Normal)
105      set_active_eval1( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );
106
107   if (ctx->Eval.Map2Normal)
108      set_active_eval2( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );
109
110   if (ctx->Eval.Map1Vertex4)
111      set_active_eval1( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
112   else if (ctx->Eval.Map1Vertex3)
113      set_active_eval1( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );
114
115   if (ctx->Eval.Map2Vertex4)
116      set_active_eval2( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
117   else if (ctx->Eval.Map2Vertex3)
118      set_active_eval2( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );
119
120   /* _NEW_PROGRAM */
121   if (ctx->VertexProgram._Enabled) {
122      /* These are the 16 evaluators which GL_NV_vertex_program defines.
123       * They alias and override the conventional vertex attributs.
124       */
125      for (attr = 0; attr < 16; attr++) {
126         /* _NEW_EVAL */
127         assert(attr < Elements(ctx->Eval.Map1Attrib));
128         if (ctx->Eval.Map1Attrib[attr])
129            set_active_eval1( exec, attr, 4, &ctx->EvalMap.Map1Attrib[attr] );
130
131         assert(attr < Elements(ctx->Eval.Map2Attrib));
132         if (ctx->Eval.Map2Attrib[attr])
133            set_active_eval2( exec, attr, 4, &ctx->EvalMap.Map2Attrib[attr] );
134      }
135   }
136
137   exec->eval.recalculate_maps = 0;
138}
139
140
141
142void vbo_exec_do_EvalCoord1f(struct vbo_exec_context *exec, GLfloat u)
143{
144   GLuint attr;
145
146   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
147      struct gl_1d_map *map = exec->eval.map1[attr].map;
148      if (map) {
149	 GLfloat uu = (u - map->u1) * map->du;
150	 GLfloat data[4];
151
152	 ASSIGN_4V(data, 0, 0, 0, 1);
153
154	 _math_horner_bezier_curve(map->Points, data, uu,
155				   exec->eval.map1[attr].sz,
156				   map->Order);
157
158	 COPY_SZ_4V( exec->vtx.attrptr[attr],
159		     exec->vtx.attrsz[attr],
160		     data );
161      }
162   }
163
164   /** Vertex -- EvalCoord1f is a noop if this map not enabled:
165    **/
166   if (exec->eval.map1[0].map) {
167      struct gl_1d_map *map = exec->eval.map1[0].map;
168      GLfloat uu = (u - map->u1) * map->du;
169      GLfloat vertex[4];
170
171      ASSIGN_4V(vertex, 0, 0, 0, 1);
172
173      _math_horner_bezier_curve(map->Points, vertex, uu,
174				exec->eval.map1[0].sz,
175				map->Order);
176
177      if (exec->eval.map1[0].sz == 4)
178	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
179      else
180	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
181   }
182}
183
184
185
186void vbo_exec_do_EvalCoord2f( struct vbo_exec_context *exec,
187			      GLfloat u, GLfloat v )
188{
189   GLuint attr;
190
191   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
192      struct gl_2d_map *map = exec->eval.map2[attr].map;
193      if (map) {
194	 GLfloat uu = (u - map->u1) * map->du;
195	 GLfloat vv = (v - map->v1) * map->dv;
196	 GLfloat data[4];
197
198	 ASSIGN_4V(data, 0, 0, 0, 1);
199
200	 _math_horner_bezier_surf(map->Points,
201				  data,
202				  uu, vv,
203				  exec->eval.map2[attr].sz,
204				  map->Uorder, map->Vorder);
205
206	 COPY_SZ_4V( exec->vtx.attrptr[attr],
207		     exec->vtx.attrsz[attr],
208		     data );
209      }
210   }
211
212   /** Vertex -- EvalCoord2f is a noop if this map not enabled:
213    **/
214   if (exec->eval.map2[0].map) {
215      struct gl_2d_map *map = exec->eval.map2[0].map;
216      GLfloat uu = (u - map->u1) * map->du;
217      GLfloat vv = (v - map->v1) * map->dv;
218      GLfloat vertex[4];
219
220      ASSIGN_4V(vertex, 0, 0, 0, 1);
221
222      if (exec->ctx->Eval.AutoNormal) {
223	 GLfloat normal[4];
224         GLfloat du[4], dv[4];
225
226         _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
227				 exec->eval.map2[0].sz,
228				 map->Uorder, map->Vorder);
229
230	 if (exec->eval.map2[0].sz == 4) {
231	    du[0] = du[0]*vertex[3] - du[3]*vertex[0];
232	    du[1] = du[1]*vertex[3] - du[3]*vertex[1];
233	    du[2] = du[2]*vertex[3] - du[3]*vertex[2];
234
235	    dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
236	    dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
237	    dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
238	 }
239
240
241         CROSS3(normal, du, dv);
242         NORMALIZE_3FV(normal);
243	 normal[3] = 1.0;
244
245	 COPY_SZ_4V( exec->vtx.attrptr[VBO_ATTRIB_NORMAL],
246		     exec->vtx.attrsz[VBO_ATTRIB_NORMAL],
247		     normal );
248
249      }
250      else {
251         _math_horner_bezier_surf(map->Points, vertex, uu, vv,
252				  exec->eval.map2[0].sz,
253				  map->Uorder, map->Vorder);
254      }
255
256      if (exec->vtx.attrsz[0] == 4)
257	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
258      else
259	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
260   }
261}
262
263
264