1 /* $Id: t_imm_eval.c,v 1.3 2001/01/03 15:59:30 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include "math/m_eval.h"
37 #include "t_context.h"
38 #include "t_imm_eval.h"
39 #include "t_imm_exec.h"
40 #include "t_imm_fixup.h"
43 static void eval_points1( GLfloat outcoord
[][4],
46 GLfloat du
, GLfloat u1
)
49 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
50 if (flags
[i
] & VERT_EVAL_ANY
) {
51 outcoord
[i
][0] = coord
[i
][0];
52 outcoord
[i
][1] = coord
[i
][1];
53 if (flags
[i
] & VERT_EVAL_P1
)
54 outcoord
[i
][0] = coord
[i
][0] * du
+ u1
;
58 static void eval_points2( GLfloat outcoord
[][4],
61 GLfloat du
, GLfloat u1
,
62 GLfloat dv
, GLfloat v1
)
65 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++) {
66 if (flags
[i
] & VERT_EVAL_ANY
) {
67 outcoord
[i
][0] = coord
[i
][0];
68 outcoord
[i
][1] = coord
[i
][1];
69 if (flags
[i
] & VERT_EVAL_P2
) {
70 outcoord
[i
][0] = coord
[i
][0] * du
+ u1
;
71 outcoord
[i
][1] = coord
[i
][1] * dv
+ v1
;
77 static const GLubyte dirty_flags
[5] = {
86 static void eval1_4f( GLvector4f
*dest
,
90 struct gl_1d_map
*map
)
92 const GLfloat u1
= map
->u1
;
93 const GLfloat du
= map
->du
;
94 GLfloat (*to
)[4] = dest
->data
;
97 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
98 if (flags
[i
] & (VERT_EVAL_C1
|VERT_EVAL_P1
)) {
99 GLfloat u
= (coord
[i
][0] - u1
) * du
;
100 ASSIGN_4V(to
[i
], 0,0,0,1);
101 _math_horner_bezier_curve(map
->Points
, to
[i
], u
,
102 dimension
, map
->Order
);
105 dest
->size
= MAX2(dest
->size
, dimension
);
106 dest
->flags
|= dirty_flags
[dimension
];
110 static void eval1_1ui( GLvector1ui
*dest
,
113 struct gl_1d_map
*map
)
115 const GLfloat u1
= map
->u1
;
116 const GLfloat du
= map
->du
;
117 GLuint
*to
= dest
->data
;
120 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
121 if (flags
[i
] & (VERT_EVAL_C1
|VERT_EVAL_P1
)) {
122 GLfloat u
= (coord
[i
][0] - u1
) * du
;
124 _math_horner_bezier_curve(map
->Points
, &tmp
, u
, 1, map
->Order
);
125 to
[i
] = (GLuint
) (GLint
) tmp
;
130 static void eval1_norm( GLvector3f
*dest
,
133 struct gl_1d_map
*map
)
135 const GLfloat u1
= map
->u1
;
136 const GLfloat du
= map
->du
;
137 GLfloat (*to
)[3] = dest
->data
;
140 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
141 if (flags
[i
] & (VERT_EVAL_C1
|VERT_EVAL_P1
)) {
142 GLfloat u
= (coord
[i
][0] - u1
) * du
;
143 _math_horner_bezier_curve(map
->Points
, to
[i
], u
, 3, map
->Order
);
147 static void eval1_color( GLvector4ub
*dest
,
150 struct gl_1d_map
*map
)
152 const GLfloat u1
= map
->u1
;
153 const GLfloat du
= map
->du
;
154 GLubyte (*to
)[4] = dest
->data
;
157 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++) {
158 if (flags
[i
] & (VERT_EVAL_C1
|VERT_EVAL_P1
)) {
159 GLfloat u
= (coord
[i
][0] - u1
) * du
;
161 _math_horner_bezier_curve(map
->Points
, fcolor
, u
, 4, map
->Order
);
162 UNCLAMPED_FLOAT_TO_RGBA_CHAN(to
[i
], fcolor
);
170 static void eval2_obj_norm( GLvector4f
*obj_ptr
,
171 GLvector3f
*norm_ptr
,
175 struct gl_2d_map
*map
)
177 const GLfloat u1
= map
->u1
;
178 const GLfloat du
= map
->du
;
179 const GLfloat v1
= map
->v1
;
180 const GLfloat dv
= map
->dv
;
181 GLfloat (*obj
)[4] = obj_ptr
->data
;
182 GLfloat (*normal
)[3] = norm_ptr
->data
;
185 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
186 if (flags
[i
] & (VERT_EVAL_C2
|VERT_EVAL_P2
)) {
187 GLfloat u
= (coord
[i
][0] - u1
) * du
;
188 GLfloat v
= (coord
[i
][1] - v1
) * dv
;
189 GLfloat du
[4], dv
[4];
191 ASSIGN_4V(obj
[i
], 0,0,0,1);
192 _math_de_casteljau_surf(map
->Points
, obj
[i
], du
, dv
, u
, v
, dimension
,
193 map
->Uorder
, map
->Vorder
);
195 CROSS3(normal
[i
], du
, dv
);
196 NORMALIZE_3FV(normal
[i
]);
199 obj_ptr
->size
= MAX2(obj_ptr
->size
, dimension
);
200 obj_ptr
->flags
|= dirty_flags
[dimension
];
204 static void eval2_4f( GLvector4f
*dest
,
208 struct gl_2d_map
*map
)
210 const GLfloat u1
= map
->u1
;
211 const GLfloat du
= map
->du
;
212 const GLfloat v1
= map
->v1
;
213 const GLfloat dv
= map
->dv
;
214 GLfloat (*to
)[4] = dest
->data
;
217 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
218 if (flags
[i
] & (VERT_EVAL_C2
|VERT_EVAL_P2
)) {
219 GLfloat u
= (coord
[i
][0] - u1
) * du
;
220 GLfloat v
= (coord
[i
][1] - v1
) * dv
;
221 _math_horner_bezier_surf(map
->Points
, to
[i
], u
, v
, dimension
,
222 map
->Uorder
, map
->Vorder
);
225 dest
->size
= MAX2(dest
->size
, dimension
);
226 dest
->flags
|= dirty_flags
[dimension
];
230 static void eval2_norm( GLvector3f
*dest
,
233 struct gl_2d_map
*map
)
235 const GLfloat u1
= map
->u1
;
236 const GLfloat du
= map
->du
;
237 const GLfloat v1
= map
->v1
;
238 const GLfloat dv
= map
->dv
;
239 GLfloat (*to
)[3] = dest
->data
;
242 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
243 if (flags
[i
] & (VERT_EVAL_C2
|VERT_EVAL_P2
)) {
244 GLfloat u
= (coord
[i
][0] - u1
) * du
;
245 GLfloat v
= (coord
[i
][1] - v1
) * dv
;
246 _math_horner_bezier_surf(map
->Points
, to
[i
], u
, v
, 3,
247 map
->Uorder
, map
->Vorder
);
253 static void eval2_1ui( GLvector1ui
*dest
,
256 struct gl_2d_map
*map
)
258 const GLfloat u1
= map
->u1
;
259 const GLfloat du
= map
->du
;
260 const GLfloat v1
= map
->v1
;
261 const GLfloat dv
= map
->dv
;
262 GLuint
*to
= dest
->data
;
265 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++)
266 if (flags
[i
] & (VERT_EVAL_C2
|VERT_EVAL_P2
)) {
267 GLfloat u
= (coord
[i
][0] - u1
) * du
;
268 GLfloat v
= (coord
[i
][1] - v1
) * dv
;
270 _math_horner_bezier_surf(map
->Points
, &tmp
, u
, v
, 1,
271 map
->Uorder
, map
->Vorder
);
273 to
[i
] = (GLuint
) (GLint
) tmp
;
279 static void eval2_color( GLvector4ub
*dest
,
282 struct gl_2d_map
*map
)
284 const GLfloat u1
= map
->u1
;
285 const GLfloat du
= map
->du
;
286 const GLfloat v1
= map
->v1
;
287 const GLfloat dv
= map
->dv
;
288 GLubyte (*to
)[4] = dest
->data
;
291 for (i
= 0 ; !(flags
[i
] & VERT_END_VB
) ; i
++) {
292 if (flags
[i
] & (VERT_EVAL_C2
|VERT_EVAL_P2
)) {
293 GLfloat u
= (coord
[i
][0] - u1
) * du
;
294 GLfloat v
= (coord
[i
][1] - v1
) * dv
;
296 _math_horner_bezier_surf(map
->Points
, fcolor
, u
, v
, 4,
297 map
->Uorder
, map
->Vorder
);
298 UNCLAMPED_FLOAT_TO_RGBA_CHAN(to
[i
], fcolor
);
305 static void copy_4f( GLfloat to
[][4], GLfloat from
[][4], GLuint count
)
307 MEMCPY( to
, from
, count
* sizeof(to
[0]));
310 static void copy_3f( GLfloat to
[][3], GLfloat from
[][3], GLuint count
)
312 MEMCPY( to
, from
, (count
) * sizeof(to
[0]));
315 static void copy_4ub( GLubyte to
[][4], GLubyte from
[][4], GLuint count
)
317 MEMCPY( to
, from
, (count
) * sizeof(to
[0]));
320 static void copy_1ui( GLuint to
[], GLuint from
[], GLuint count
)
322 MEMCPY( to
, from
, (count
) * sizeof(to
[0]));
327 /* Translate eval enabled flags to VERT_* flags.
329 static void update_eval( GLcontext
*ctx
)
331 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
332 GLuint eval1
= 0, eval2
= 0;
334 if (ctx
->Eval
.Map1Index
)
337 if (ctx
->Eval
.Map2Index
)
340 if (ctx
->Eval
.Map1Color4
)
343 if (ctx
->Eval
.Map2Color4
)
346 if (ctx
->Eval
.Map1Normal
)
349 if (ctx
->Eval
.Map2Normal
)
352 if (ctx
->Eval
.Map1TextureCoord4
||
353 ctx
->Eval
.Map1TextureCoord3
||
354 ctx
->Eval
.Map1TextureCoord2
||
355 ctx
->Eval
.Map1TextureCoord1
)
358 if (ctx
->Eval
.Map2TextureCoord4
||
359 ctx
->Eval
.Map2TextureCoord3
||
360 ctx
->Eval
.Map2TextureCoord2
||
361 ctx
->Eval
.Map2TextureCoord1
)
364 if (ctx
->Eval
.Map1Vertex4
)
365 eval1
|= VERT_OBJ_234
;
367 if (ctx
->Eval
.Map1Vertex3
)
368 eval1
|= VERT_OBJ_23
;
370 if (ctx
->Eval
.Map2Vertex4
) {
371 if (ctx
->Eval
.AutoNormal
)
372 eval2
|= VERT_OBJ_234
| VERT_NORM
;
374 eval2
|= VERT_OBJ_234
;
376 else if (ctx
->Eval
.Map2Vertex3
) {
377 if (ctx
->Eval
.AutoNormal
)
378 eval2
|= VERT_OBJ_23
| VERT_NORM
;
380 eval2
|= VERT_OBJ_23
;
383 tnl
->eval
.EvalMap1Flags
= eval1
;
384 tnl
->eval
.EvalMap2Flags
= eval2
;
385 tnl
->eval
.EvalNewState
= 0;
389 /* This looks a lot like a pipeline stage, but for various reasons is
390 * better handled outside the pipeline, and considered the final stage
391 * of fixing up an immediate struct for execution.
393 * Really want to cache the results of this function in display lists,
394 * at least for EvalMesh commands.
396 void _tnl_eval_vb( GLcontext
*ctx
,
401 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
402 struct vertex_arrays
*tmp
= &tnl
->imm_inputs
;
403 struct tnl_eval_store
*store
= &tnl
->eval
;
404 GLuint
*flags
= tnl
->vb
.Flag
;
405 GLuint count
= tnl
->vb
.Count
;
406 GLuint any_eval1
= orflag
& (VERT_EVAL_C1
|VERT_EVAL_P1
);
407 GLuint any_eval2
= orflag
& (VERT_EVAL_C2
|VERT_EVAL_P2
);
408 GLuint all_eval
= andflag
& VERT_EVAL_ANY
; /* may have false negatives */
410 GLuint purge_flags
= 0;
412 if (tnl
->eval
.EvalNewState
& _NEW_EVAL
)
415 /* Handle the degenerate cases.
417 if (any_eval1
&& !ctx
->Eval
.Map1Vertex4
&& !ctx
->Eval
.Map1Vertex3
)
418 purge_flags
= (VERT_EVAL_P1
|VERT_EVAL_C1
);
420 if (any_eval2
&& !ctx
->Eval
.Map2Vertex4
&& !ctx
->Eval
.Map2Vertex3
)
421 purge_flags
|= (VERT_EVAL_P1
|VERT_EVAL_C1
);
424 req
|= tnl
->pipeline
.inputs
& tnl
->eval
.EvalMap1Flags
;
427 req
|= tnl
->pipeline
.inputs
& tnl
->eval
.EvalMap2Flags
;
430 /* Translate points into coords. Use store->Coord to hold the
433 if (any_eval1
&& (orflag
& VERT_EVAL_P1
))
435 eval_points1( store
->Coord
, coord
, flags
,
436 ctx
->Eval
.MapGrid1du
,
437 ctx
->Eval
.MapGrid1u1
);
439 coord
= store
->Coord
;
442 if (any_eval2
&& (orflag
& VERT_EVAL_P2
))
444 eval_points2( store
->Coord
, coord
, flags
,
445 ctx
->Eval
.MapGrid2du
,
446 ctx
->Eval
.MapGrid2u1
,
447 ctx
->Eval
.MapGrid2dv
,
448 ctx
->Eval
.MapGrid2v1
);
450 coord
= store
->Coord
;
454 /* Perform the evaluations on active data elements.
456 if (req
& VERT_INDEX
)
459 copy_1ui( store
->Index
, tmp
->Index
.data
, count
);
461 tmp
->Index
.data
= store
->Index
;
462 tmp
->Index
.start
= store
->Index
;
464 if (ctx
->Eval
.Map1Index
&& any_eval1
)
465 eval1_1ui( &tmp
->Index
, coord
, flags
, &ctx
->EvalMap
.Map1Index
);
467 if (ctx
->Eval
.Map2Index
&& any_eval2
)
468 eval2_1ui( &tmp
->Index
, coord
, flags
, &ctx
->EvalMap
.Map2Index
);
475 copy_4ub( store
->Color
, tmp
->Color
.data
, count
);
477 tmp
->Color
.data
= store
->Color
;
478 tmp
->Color
.start
= (GLubyte
*)store
->Color
;
480 if (ctx
->Eval
.Map1Color4
&& any_eval1
)
481 eval1_color( &tmp
->Color
, coord
, flags
, &ctx
->EvalMap
.Map1Color4
);
483 if (ctx
->Eval
.Map2Color4
&& any_eval2
)
484 eval2_color( &tmp
->Color
, coord
, flags
, &ctx
->EvalMap
.Map2Color4
);
488 if (req
& VERT_TEX(0))
491 copy_4f( store
->TexCoord
, tmp
->TexCoord
[0].data
, count
);
493 tmp
->TexCoord
[0].size
= 0;
495 tmp
->TexCoord
[0].data
= store
->TexCoord
;
496 tmp
->TexCoord
[0].start
= (GLfloat
*)store
->TexCoord
;
499 if (ctx
->Eval
.Map1TextureCoord4
) {
500 eval1_4f( &tmp
->TexCoord
[0], coord
, flags
, 4,
501 &ctx
->EvalMap
.Map1Texture4
);
503 else if (ctx
->Eval
.Map1TextureCoord3
) {
504 eval1_4f( &tmp
->TexCoord
[0], coord
, flags
, 3,
505 &ctx
->EvalMap
.Map1Texture3
);
507 else if (ctx
->Eval
.Map1TextureCoord2
) {
508 eval1_4f( &tmp
->TexCoord
[0], coord
, flags
, 2,
509 &ctx
->EvalMap
.Map1Texture2
);
511 else if (ctx
->Eval
.Map1TextureCoord1
) {
512 eval1_4f( &tmp
->TexCoord
[0], coord
, flags
, 1,
513 &ctx
->EvalMap
.Map1Texture1
);
518 if (ctx
->Eval
.Map2TextureCoord4
) {
519 eval2_4f( &tmp
->TexCoord
[0], coord
, flags
, 4,
520 &ctx
->EvalMap
.Map2Texture4
);
522 else if (ctx
->Eval
.Map2TextureCoord3
) {
523 eval2_4f( &tmp
->TexCoord
[0], coord
, flags
, 3,
524 &ctx
->EvalMap
.Map2Texture3
);
526 else if (ctx
->Eval
.Map2TextureCoord2
) {
527 eval2_4f( &tmp
->TexCoord
[0], coord
, flags
, 2,
528 &ctx
->EvalMap
.Map2Texture2
);
530 else if (ctx
->Eval
.Map2TextureCoord1
) {
531 eval2_4f( &tmp
->TexCoord
[0], coord
, flags
, 1,
532 &ctx
->EvalMap
.Map2Texture1
);
541 copy_3f( store
->Normal
, tmp
->Normal
.data
, count
);
543 tmp
->Normal
.data
= store
->Normal
;
544 tmp
->Normal
.start
= (GLfloat
*)store
->Normal
;
546 if (ctx
->Eval
.Map1Normal
&& any_eval1
)
547 eval1_norm( &tmp
->Normal
, coord
, flags
,
548 &ctx
->EvalMap
.Map1Normal
);
550 if (ctx
->Eval
.Map2Normal
&& any_eval2
)
551 eval2_norm( &tmp
->Normal
, coord
, flags
,
552 &ctx
->EvalMap
.Map2Normal
);
557 /* In the AutoNormal case, the copy and assignment of tmp->NormalPtr
563 copy_4f( store
->Obj
, tmp
->Obj
.data
, count
);
567 tmp
->Obj
.data
= store
->Obj
;
568 tmp
->Obj
.start
= (GLfloat
*)store
->Obj
;
571 if (ctx
->Eval
.Map1Vertex4
) {
572 eval1_4f( &tmp
->Obj
, coord
, flags
, 4,
573 &ctx
->EvalMap
.Map1Vertex4
);
575 else if (ctx
->Eval
.Map1Vertex3
) {
576 eval1_4f( &tmp
->Obj
, coord
, flags
, 3,
577 &ctx
->EvalMap
.Map1Vertex3
);
582 if (ctx
->Eval
.Map2Vertex4
)
584 if (ctx
->Eval
.AutoNormal
&& (req
& VERT_NORM
))
585 eval2_obj_norm( &tmp
->Obj
, &tmp
->Normal
, coord
, flags
, 4,
586 &ctx
->EvalMap
.Map2Vertex4
);
588 eval2_4f( &tmp
->Obj
, coord
, flags
, 4,
589 &ctx
->EvalMap
.Map2Vertex4
);
591 else if (ctx
->Eval
.Map2Vertex3
)
593 if (ctx
->Eval
.AutoNormal
&& (req
& VERT_NORM
))
594 eval2_obj_norm( &tmp
->Obj
, &tmp
->Normal
, coord
, flags
, 3,
595 &ctx
->EvalMap
.Map2Vertex3
);
597 eval2_4f( &tmp
->Obj
, coord
, flags
, 3,
598 &ctx
->EvalMap
.Map2Vertex3
);
606 copy_1ui( store
->Flag
, flags
, count
);
607 tnl
->vb
.Flag
= store
->Flag
;
609 /* This is overkill, but correct as fixup will have copied the
610 * values to all vertices in the VB - we may be falsely stating
611 * that some repeated values are new, but doing so is fairly
614 for (i
= 0 ; i
< count
; i
++)
615 store
->Flag
[i
] |= req
;