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