nouveau: Add notifier support functions
[mesa.git] / src / mesa / tnl / t_vtx_eval.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 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 "glheader.h"
29 #include "api_eval.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "math/m_eval.h"
33 #include "t_vtx_api.h"
34 #include "dispatch.h"
35
36
37 static void clear_active_eval1( TNLcontext *tnl, GLuint attr )
38 {
39 ASSERT(attr < _TNL_NUM_EVAL);
40 tnl->vtx.eval.map1[attr].map = NULL;
41 }
42
43 static void clear_active_eval2( TNLcontext *tnl, GLuint attr )
44 {
45 ASSERT(attr < _TNL_NUM_EVAL);
46 tnl->vtx.eval.map2[attr].map = NULL;
47 }
48
49 static void set_active_eval1( TNLcontext *tnl, GLuint attr, GLuint dim,
50 struct gl_1d_map *map )
51 {
52 ASSERT(attr < _TNL_NUM_EVAL);
53 if (!tnl->vtx.eval.map1[attr].map) {
54 tnl->vtx.eval.map1[attr].map = map;
55 tnl->vtx.eval.map1[attr].sz = dim;
56 }
57 }
58
59 static void set_active_eval2( TNLcontext *tnl, GLuint attr, GLuint dim,
60 struct gl_2d_map *map )
61 {
62 ASSERT(attr < _TNL_NUM_EVAL);
63 if (!tnl->vtx.eval.map2[attr].map) {
64 tnl->vtx.eval.map2[attr].map = map;
65 tnl->vtx.eval.map2[attr].sz = dim;
66 }
67 }
68
69 void _tnl_update_eval( GLcontext *ctx )
70 {
71 TNLcontext *tnl = TNL_CONTEXT(ctx);
72 GLuint attr;
73
74 /* Vertex program maps have priority over conventional attribs */
75
76 for (attr = 0; attr < _TNL_NUM_EVAL; attr++) {
77 clear_active_eval1( tnl, attr );
78 clear_active_eval2( tnl, attr );
79 }
80
81 if (ctx->Eval.Map1Color4)
82 set_active_eval1( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map1Color4 );
83
84 if (ctx->Eval.Map2Color4)
85 set_active_eval2( tnl, VERT_ATTRIB_COLOR0, 4, &ctx->EvalMap.Map2Color4 );
86
87 if (ctx->Eval.Map1TextureCoord4)
88 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map1Texture4 );
89 else if (ctx->Eval.Map1TextureCoord3)
90 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map1Texture3 );
91 else if (ctx->Eval.Map1TextureCoord2)
92 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map1Texture2 );
93 else if (ctx->Eval.Map1TextureCoord1)
94 set_active_eval1( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map1Texture1 );
95
96 if (ctx->Eval.Map2TextureCoord4)
97 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 4, &ctx->EvalMap.Map2Texture4 );
98 else if (ctx->Eval.Map2TextureCoord3)
99 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 3, &ctx->EvalMap.Map2Texture3 );
100 else if (ctx->Eval.Map2TextureCoord2)
101 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 2, &ctx->EvalMap.Map2Texture2 );
102 else if (ctx->Eval.Map2TextureCoord1)
103 set_active_eval2( tnl, VERT_ATTRIB_TEX0, 1, &ctx->EvalMap.Map2Texture1 );
104
105 if (ctx->Eval.Map1Normal)
106 set_active_eval1( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map1Normal );
107
108 if (ctx->Eval.Map2Normal)
109 set_active_eval2( tnl, VERT_ATTRIB_NORMAL, 3, &ctx->EvalMap.Map2Normal );
110
111 if (ctx->Eval.Map1Vertex4)
112 set_active_eval1( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map1Vertex4 );
113 else if (ctx->Eval.Map1Vertex3)
114 set_active_eval1( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map1Vertex3 );
115
116 if (ctx->Eval.Map2Vertex4)
117 set_active_eval2( tnl, VERT_ATTRIB_POS, 4, &ctx->EvalMap.Map2Vertex4 );
118 else if (ctx->Eval.Map2Vertex3)
119 set_active_eval2( tnl, VERT_ATTRIB_POS, 3, &ctx->EvalMap.Map2Vertex3 );
120
121 /* Evaluators with generic attributes is only supported for NV vertex
122 * programs, not ARB vertex programs. 16 evaluator maps are supported.
123 * We do this after the conventional attributes since the spec says that
124 * these generic maps have higher priority.
125 */
126 if (ctx->VertexProgram._Enabled &&
127 ctx->VertexProgram._Current &&
128 ctx->VertexProgram._Current->IsNVProgram) {
129 for (attr = 0; attr < 16; attr++) {
130 if (ctx->Eval.Map1Attrib[attr])
131 set_active_eval1( tnl, attr, 4, &ctx->EvalMap.Map1Attrib[attr] );
132
133 if (ctx->Eval.Map2Attrib[attr])
134 set_active_eval2( tnl, attr, 4, &ctx->EvalMap.Map2Attrib[attr] );
135 }
136 }
137
138 tnl->vtx.eval.new_state = 0;
139 }
140
141
142
143 void _tnl_do_EvalCoord1f(GLcontext* ctx, GLfloat u)
144 {
145 TNLcontext *tnl = TNL_CONTEXT(ctx);
146 GLuint attr;
147
148 for (attr = 1; attr < _TNL_NUM_EVAL; attr++) {
149 struct gl_1d_map *map = tnl->vtx.eval.map1[attr].map;
150 if (map) {
151 GLfloat uu = (u - map->u1) * map->du;
152 GLfloat data[4];
153
154 ASSIGN_4V(data, 0, 0, 0, 1);
155
156 _math_horner_bezier_curve(map->Points, data, uu,
157 tnl->vtx.eval.map1[attr].sz,
158 map->Order);
159
160 COPY_SZ_4V( tnl->vtx.attrptr[attr],
161 tnl->vtx.attrsz[attr],
162 data );
163 }
164 }
165
166 /** Vertex -- EvalCoord1f is a noop if this map not enabled:
167 **/
168 if (tnl->vtx.eval.map1[0].map) {
169 struct gl_1d_map *map = tnl->vtx.eval.map1[0].map;
170 GLfloat uu = (u - map->u1) * map->du;
171 GLfloat vertex[4];
172
173 ASSIGN_4V(vertex, 0, 0, 0, 1);
174
175 _math_horner_bezier_curve(map->Points, vertex, uu,
176 tnl->vtx.eval.map1[0].sz,
177 map->Order);
178
179 if (tnl->vtx.eval.map1[0].sz == 4)
180 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
181 else
182 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
183 }
184 }
185
186
187
188 void _tnl_do_EvalCoord2f( GLcontext* ctx, GLfloat u, GLfloat v )
189 {
190 TNLcontext *tnl = TNL_CONTEXT(ctx);
191 GLuint attr;
192
193 for (attr = 1; attr < _TNL_NUM_EVAL; attr++) {
194 struct gl_2d_map *map = tnl->vtx.eval.map2[attr].map;
195 if (map) {
196 GLfloat uu = (u - map->u1) * map->du;
197 GLfloat vv = (v - map->v1) * map->dv;
198 GLfloat data[4];
199
200 ASSIGN_4V(data, 0, 0, 0, 1);
201
202 _math_horner_bezier_surf(map->Points,
203 data,
204 uu, vv,
205 tnl->vtx.eval.map2[attr].sz,
206 map->Uorder, map->Vorder);
207
208 COPY_SZ_4V( tnl->vtx.attrptr[attr],
209 tnl->vtx.attrsz[attr],
210 data );
211 }
212 }
213
214 /** Vertex -- EvalCoord2f is a noop if this map not enabled:
215 **/
216 if (tnl->vtx.eval.map2[0].map) {
217 struct gl_2d_map *map = tnl->vtx.eval.map2[0].map;
218 GLfloat uu = (u - map->u1) * map->du;
219 GLfloat vv = (v - map->v1) * map->dv;
220 GLfloat vertex[4];
221
222 ASSIGN_4V(vertex, 0, 0, 0, 1);
223
224 if (ctx->Eval.AutoNormal) {
225 GLfloat normal[4];
226 GLfloat du[4], dv[4];
227
228 _math_de_casteljau_surf(map->Points, vertex, du, dv, uu, vv,
229 tnl->vtx.eval.map2[0].sz,
230 map->Uorder, map->Vorder);
231
232 if (tnl->vtx.eval.map2[0].sz == 4) {
233 du[0] = du[0]*vertex[3] - du[3]*vertex[0];
234 du[1] = du[1]*vertex[3] - du[3]*vertex[1];
235 du[2] = du[2]*vertex[3] - du[3]*vertex[2];
236
237 dv[0] = dv[0]*vertex[3] - dv[3]*vertex[0];
238 dv[1] = dv[1]*vertex[3] - dv[3]*vertex[1];
239 dv[2] = dv[2]*vertex[3] - dv[3]*vertex[2];
240 }
241
242
243 CROSS3(normal, du, dv);
244 NORMALIZE_3FV(normal);
245 normal[3] = 1.0;
246
247 COPY_SZ_4V( tnl->vtx.attrptr[_TNL_ATTRIB_NORMAL],
248 tnl->vtx.attrsz[_TNL_ATTRIB_NORMAL],
249 normal );
250
251 }
252 else {
253 _math_horner_bezier_surf(map->Points, vertex, uu, vv,
254 tnl->vtx.eval.map2[0].sz,
255 map->Uorder, map->Vorder);
256 }
257
258 if (tnl->vtx.attrsz[0] == 4)
259 CALL_Vertex4fv(GET_DISPATCH(), ( vertex ));
260 else
261 CALL_Vertex3fv(GET_DISPATCH(), ( vertex ));
262 }
263 }
264
265