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
35
36static void clear_active_eval1( struct vbo_exec_context *exec, GLuint attr )
37{
38   assert(attr < ARRAY_SIZE(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 < ARRAY_SIZE(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 < ARRAY_SIZE(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 < ARRAY_SIZE(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   exec->eval.recalculate_maps = GL_FALSE;
121}
122
123
124
125void vbo_exec_do_EvalCoord1f(struct vbo_exec_context *exec, GLfloat u)
126{
127   GLuint attr;
128
129   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
130      struct gl_1d_map *map = exec->eval.map1[attr].map;
131      if (map) {
132	 GLfloat uu = (u - map->u1) * map->du;
133	 fi_type data[4];
134
135	 ASSIGN_4V(data, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
136		   FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
137
138	 _math_horner_bezier_curve(map->Points, &data[0].f, uu,
139				   exec->eval.map1[attr].sz,
140				   map->Order);
141
142	 COPY_SZ_4V( exec->vtx.attrptr[attr],
143		     exec->vtx.attrsz[attr],
144		     data );
145      }
146   }
147
148   /** Vertex -- EvalCoord1f is a noop if this map not enabled:
149    **/
150   if (exec->eval.map1[0].map) {
151      struct gl_1d_map *map = exec->eval.map1[0].map;
152      GLfloat uu = (u - map->u1) * map->du;
153      GLfloat vertex[4];
154
155      ASSIGN_4V(vertex, 0, 0, 0, 1);
156
157      _math_horner_bezier_curve(map->Points, vertex, uu,
158				exec->eval.map1[0].sz,
159				map->Order);
160
161      if (exec->eval.map1[0].sz == 4)
162	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
163      else
164	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
165   }
166}
167
168
169
170void vbo_exec_do_EvalCoord2f( struct vbo_exec_context *exec,
171			      GLfloat u, GLfloat v )
172{
173   GLuint attr;
174
175   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
176      struct gl_2d_map *map = exec->eval.map2[attr].map;
177      if (map) {
178	 GLfloat uu = (u - map->u1) * map->du;
179	 GLfloat vv = (v - map->v1) * map->dv;
180	 fi_type data[4];
181
182	 ASSIGN_4V(data, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
183		   FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
184
185	 _math_horner_bezier_surf(map->Points,
186				  &data[0].f,
187				  uu, vv,
188				  exec->eval.map2[attr].sz,
189				  map->Uorder, map->Vorder);
190
191	 COPY_SZ_4V( exec->vtx.attrptr[attr],
192		     exec->vtx.attrsz[attr],
193		     data );
194      }
195   }
196
197   /** Vertex -- EvalCoord2f is a noop if this map not enabled:
198    **/
199   if (exec->eval.map2[0].map) {
200      struct gl_2d_map *map = exec->eval.map2[0].map;
201      GLfloat uu = (u - map->u1) * map->du;
202      GLfloat vv = (v - map->v1) * map->dv;
203      GLfloat vertex[4];
204
205      ASSIGN_4V(vertex, 0, 0, 0, 1);
206
207      if (exec->ctx->Eval.AutoNormal) {
208	 fi_type normal[4];
209         GLfloat du[4], dv[4];
210
211         _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
212				 exec->eval.map2[0].sz,
213				 map->Uorder, map->Vorder);
214
215	 if (exec->eval.map2[0].sz == 4) {
216	    du[0] = du[0]*vertex[3] - du[3]*vertex[0];
217	    du[1] = du[1]*vertex[3] - du[3]*vertex[1];
218	    du[2] = du[2]*vertex[3] - du[3]*vertex[2];
219
220	    dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
221	    dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
222	    dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
223	 }
224
225
226         CROSS3(&normal[0].f, du, dv);
227         NORMALIZE_3FV(&normal[0].f);
228	 normal[3] = FLOAT_AS_UNION(1.0);
229
230 	 COPY_SZ_4V( exec->vtx.attrptr[VBO_ATTRIB_NORMAL],
231		     exec->vtx.attrsz[VBO_ATTRIB_NORMAL],
232		     normal );
233
234      }
235      else {
236         _math_horner_bezier_surf(map->Points, vertex, uu, vv,
237				  exec->eval.map2[0].sz,
238				  map->Uorder, map->Vorder);
239      }
240
241      if (exec->vtx.attrsz[0] == 4)
242	 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
243      else
244	 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
245   }
246}
247
248
249