17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
47117f1b4Smrg * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg *
247117f1b4Smrg * Authors:
25af69d88dSmrg *    Keith Whitwell <keithw@vmware.com>
267117f1b4Smrg */
277117f1b4Smrg
28c1f859d4Smrg#include "main/glheader.h"
29c1f859d4Smrg#include "main/context.h"
30c1f859d4Smrg#include "main/macros.h"
317117f1b4Smrg#include "math/m_eval.h"
32cdc920a0Smrg#include "main/dispatch.h"
337117f1b4Smrg#include "vbo_exec.h"
347ec681f3Smrg#include "vbo_private.h"
357117f1b4Smrg
367117f1b4Smrg
377117f1b4Smrgstatic void clear_active_eval1( struct vbo_exec_context *exec, GLuint attr )
387117f1b4Smrg{
3901e04c3fSmrg   assert(attr < ARRAY_SIZE(exec->eval.map1));
407117f1b4Smrg   exec->eval.map1[attr].map = NULL;
417117f1b4Smrg}
427117f1b4Smrg
437117f1b4Smrgstatic void clear_active_eval2( struct vbo_exec_context *exec, GLuint attr )
447117f1b4Smrg{
4501e04c3fSmrg   assert(attr < ARRAY_SIZE(exec->eval.map2));
467117f1b4Smrg   exec->eval.map2[attr].map = NULL;
477117f1b4Smrg}
487117f1b4Smrg
497117f1b4Smrgstatic void set_active_eval1( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
507117f1b4Smrg			      struct gl_1d_map *map )
517117f1b4Smrg{
5201e04c3fSmrg   assert(attr < ARRAY_SIZE(exec->eval.map1));
537117f1b4Smrg   if (!exec->eval.map1[attr].map) {
547117f1b4Smrg      exec->eval.map1[attr].map = map;
557117f1b4Smrg      exec->eval.map1[attr].sz = dim;
567117f1b4Smrg   }
577117f1b4Smrg}
587117f1b4Smrg
597117f1b4Smrgstatic void set_active_eval2( struct vbo_exec_context *exec, GLuint attr, GLuint dim,
607117f1b4Smrg			      struct gl_2d_map *map )
617117f1b4Smrg{
6201e04c3fSmrg   assert(attr < ARRAY_SIZE(exec->eval.map2));
637117f1b4Smrg   if (!exec->eval.map2[attr].map) {
647117f1b4Smrg      exec->eval.map2[attr].map = map;
657117f1b4Smrg      exec->eval.map2[attr].sz = dim;
667117f1b4Smrg   }
677117f1b4Smrg}
687117f1b4Smrg
697117f1b4Smrgvoid vbo_exec_eval_update( struct vbo_exec_context *exec )
707117f1b4Smrg{
717ec681f3Smrg   struct gl_context *ctx = gl_context_from_vbo_exec(exec);
727117f1b4Smrg   GLuint attr;
737117f1b4Smrg
747117f1b4Smrg   /* Vertex program maps have priority over conventional attribs */
757117f1b4Smrg
767117f1b4Smrg   for (attr = 0; attr < VBO_ATTRIB_FIRST_MATERIAL; attr++) {
777117f1b4Smrg      clear_active_eval1( exec, attr );
787117f1b4Smrg      clear_active_eval2( exec, attr );
797117f1b4Smrg   }
807117f1b4Smrg
817117f1b4Smrg   if (ctx->Eval.Map1Color4)
827117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
837117f1b4Smrg
847117f1b4Smrg   if (ctx->Eval.Map2Color4)
857117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );
867117f1b4Smrg
877117f1b4Smrg   if (ctx->Eval.Map1TextureCoord4)
887117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
897117f1b4Smrg   else if (ctx->Eval.Map1TextureCoord3)
907117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
917117f1b4Smrg   else if (ctx->Eval.Map1TextureCoord2)
927117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
937117f1b4Smrg   else if (ctx->Eval.Map1TextureCoord1)
947117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );
957117f1b4Smrg
967117f1b4Smrg   if (ctx->Eval.Map2TextureCoord4)
977117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
987117f1b4Smrg   else if (ctx->Eval.Map2TextureCoord3)
997117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
1007117f1b4Smrg   else if (ctx->Eval.Map2TextureCoord2)
1017117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
1027117f1b4Smrg   else if (ctx->Eval.Map2TextureCoord1)
1037117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );
1047117f1b4Smrg
1057117f1b4Smrg   if (ctx->Eval.Map1Normal)
1067117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );
1077117f1b4Smrg
1087117f1b4Smrg   if (ctx->Eval.Map2Normal)
1097117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );
1107117f1b4Smrg
1117117f1b4Smrg   if (ctx->Eval.Map1Vertex4)
1127117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
1137117f1b4Smrg   else if (ctx->Eval.Map1Vertex3)
1147117f1b4Smrg      set_active_eval1( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );
1157117f1b4Smrg
1167117f1b4Smrg   if (ctx->Eval.Map2Vertex4)
1177117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
1187117f1b4Smrg   else if (ctx->Eval.Map2Vertex3)
1197117f1b4Smrg      set_active_eval2( exec, VBO_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );
1207117f1b4Smrg
121af69d88dSmrg   exec->eval.recalculate_maps = GL_FALSE;
1227117f1b4Smrg}
1237117f1b4Smrg
1247117f1b4Smrg
1257117f1b4Smrg
1267117f1b4Smrgvoid vbo_exec_do_EvalCoord1f(struct vbo_exec_context *exec, GLfloat u)
1277117f1b4Smrg{
1287ec681f3Smrg   struct gl_context *ctx = gl_context_from_vbo_exec(exec);
1297117f1b4Smrg   GLuint attr;
1307117f1b4Smrg
1317117f1b4Smrg   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
1327117f1b4Smrg      struct gl_1d_map *map = exec->eval.map1[attr].map;
1337117f1b4Smrg      if (map) {
1347117f1b4Smrg	 GLfloat uu = (u - map->u1) * map->du;
13501e04c3fSmrg	 fi_type data[4];
1367117f1b4Smrg
13701e04c3fSmrg	 ASSIGN_4V(data, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
13801e04c3fSmrg		   FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
1397117f1b4Smrg
14001e04c3fSmrg	 _math_horner_bezier_curve(map->Points, &data[0].f, uu,
1417117f1b4Smrg				   exec->eval.map1[attr].sz,
1427117f1b4Smrg				   map->Order);
1437117f1b4Smrg
1447117f1b4Smrg	 COPY_SZ_4V( exec->vtx.attrptr[attr],
1457ec681f3Smrg		     exec->vtx.attr[attr].size,
1467117f1b4Smrg		     data );
1477117f1b4Smrg      }
1487117f1b4Smrg   }
1497117f1b4Smrg
1507117f1b4Smrg   /** Vertex -- EvalCoord1f is a noop if this map not enabled:
1517117f1b4Smrg    **/
1527117f1b4Smrg   if (exec->eval.map1[0].map) {
1537117f1b4Smrg      struct gl_1d_map *map = exec->eval.map1[0].map;
1547117f1b4Smrg      GLfloat uu = (u - map->u1) * map->du;
1557117f1b4Smrg      GLfloat vertex[4];
1567117f1b4Smrg
1577117f1b4Smrg      ASSIGN_4V(vertex, 0, 0, 0, 1);
1587117f1b4Smrg
1597117f1b4Smrg      _math_horner_bezier_curve(map->Points, vertex, uu,
1607117f1b4Smrg				exec->eval.map1[0].sz,
1617117f1b4Smrg				map->Order);
1627117f1b4Smrg
1637117f1b4Smrg      if (exec->eval.map1[0].sz == 4)
1647ec681f3Smrg	 CALL_Vertex4fv(ctx->CurrentServerDispatch, ( vertex ));
1657117f1b4Smrg      else
1667ec681f3Smrg	 CALL_Vertex3fv(ctx->CurrentServerDispatch, ( vertex ));
1677117f1b4Smrg   }
1687117f1b4Smrg}
1697117f1b4Smrg
1707117f1b4Smrg
1717117f1b4Smrg
1727117f1b4Smrgvoid vbo_exec_do_EvalCoord2f( struct vbo_exec_context *exec,
1737117f1b4Smrg			      GLfloat u, GLfloat v )
1747117f1b4Smrg{
1757ec681f3Smrg   struct gl_context *ctx = gl_context_from_vbo_exec(exec);
1767117f1b4Smrg   GLuint attr;
1777117f1b4Smrg
1787117f1b4Smrg   for (attr = 1; attr <= VBO_ATTRIB_TEX7; attr++) {
1797117f1b4Smrg      struct gl_2d_map *map = exec->eval.map2[attr].map;
1807117f1b4Smrg      if (map) {
1817117f1b4Smrg	 GLfloat uu = (u - map->u1) * map->du;
1827117f1b4Smrg	 GLfloat vv = (v - map->v1) * map->dv;
18301e04c3fSmrg	 fi_type data[4];
1847117f1b4Smrg
18501e04c3fSmrg	 ASSIGN_4V(data, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
18601e04c3fSmrg		   FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
1877117f1b4Smrg
1887117f1b4Smrg	 _math_horner_bezier_surf(map->Points,
18901e04c3fSmrg				  &data[0].f,
1907117f1b4Smrg				  uu, vv,
1917117f1b4Smrg				  exec->eval.map2[attr].sz,
1927117f1b4Smrg				  map->Uorder, map->Vorder);
1937117f1b4Smrg
1947117f1b4Smrg	 COPY_SZ_4V( exec->vtx.attrptr[attr],
1957ec681f3Smrg		     exec->vtx.attr[attr].size,
1967117f1b4Smrg		     data );
1977117f1b4Smrg      }
1987117f1b4Smrg   }
1997117f1b4Smrg
2007117f1b4Smrg   /** Vertex -- EvalCoord2f is a noop if this map not enabled:
2017117f1b4Smrg    **/
2027117f1b4Smrg   if (exec->eval.map2[0].map) {
2037117f1b4Smrg      struct gl_2d_map *map = exec->eval.map2[0].map;
2047117f1b4Smrg      GLfloat uu = (u - map->u1) * map->du;
2057117f1b4Smrg      GLfloat vv = (v - map->v1) * map->dv;
2067117f1b4Smrg      GLfloat vertex[4];
2077117f1b4Smrg
2087117f1b4Smrg      ASSIGN_4V(vertex, 0, 0, 0, 1);
2097117f1b4Smrg
2107ec681f3Smrg      if (ctx->Eval.AutoNormal) {
21101e04c3fSmrg	 fi_type normal[4];
2127117f1b4Smrg         GLfloat du[4], dv[4];
2137117f1b4Smrg
2147117f1b4Smrg         _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
2157117f1b4Smrg				 exec->eval.map2[0].sz,
2167117f1b4Smrg				 map->Uorder, map->Vorder);
2177117f1b4Smrg
2187117f1b4Smrg	 if (exec->eval.map2[0].sz == 4) {
2197117f1b4Smrg	    du[0] = du[0]*vertex[3] - du[3]*vertex[0];
2207117f1b4Smrg	    du[1] = du[1]*vertex[3] - du[3]*vertex[1];
2217117f1b4Smrg	    du[2] = du[2]*vertex[3] - du[3]*vertex[2];
2227117f1b4Smrg
2237117f1b4Smrg	    dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
2247117f1b4Smrg	    dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
2257117f1b4Smrg	    dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
2267117f1b4Smrg	 }
2277117f1b4Smrg
2287117f1b4Smrg
22901e04c3fSmrg         CROSS3(&normal[0].f, du, dv);
23001e04c3fSmrg         NORMALIZE_3FV(&normal[0].f);
23101e04c3fSmrg	 normal[3] = FLOAT_AS_UNION(1.0);
2327117f1b4Smrg
23301e04c3fSmrg 	 COPY_SZ_4V( exec->vtx.attrptr[VBO_ATTRIB_NORMAL],
2347ec681f3Smrg		     exec->vtx.attr[VBO_ATTRIB_NORMAL].size,
2357117f1b4Smrg		     normal );
2367117f1b4Smrg
2377117f1b4Smrg      }
2387117f1b4Smrg      else {
2397117f1b4Smrg         _math_horner_bezier_surf(map->Points, vertex, uu, vv,
2407117f1b4Smrg				  exec->eval.map2[0].sz,
2417117f1b4Smrg				  map->Uorder, map->Vorder);
2427117f1b4Smrg      }
2437117f1b4Smrg
2447ec681f3Smrg      if (exec->vtx.attr[VBO_ATTRIB_POS].size == 4)
2457ec681f3Smrg	 CALL_Vertex4fv(ctx->CurrentServerDispatch, ( vertex ));
2467117f1b4Smrg      else
2477ec681f3Smrg	 CALL_Vertex3fv(ctx->CurrentServerDispatch, ( vertex ));
2487117f1b4Smrg   }
2497117f1b4Smrg}
2507117f1b4Smrg
2517117f1b4Smrg
252