make sure the HW is clean before shooting the pipeline
[mesa.git] / src / mesa / drivers / glide / fxtris.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.0
4 *
5 * Copyright (C) 1999-2001 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
25 /* Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 * Daniel Borca <dborca@users.sourceforge.net>
28 */
29
30 #include "glheader.h"
31
32 #ifdef FX
33
34 #include "imports.h"
35 #include "mtypes.h"
36 #include "macros.h"
37 #include "colormac.h"
38 #include "nvfragprog.h"
39
40 #include "swrast/swrast.h"
41 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/t_context.h"
43 #include "tnl/t_pipeline.h"
44
45 #include "fxdrv.h"
46
47
48 GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass);
49
50
51 /*
52 * Subpixel offsets to adjust Mesa's (true) window coordinates to
53 * Glide coordinates. We need these to ensure precise rasterization.
54 * Otherwise, we'll fail a bunch of conformance tests.
55 */
56 #define TRI_X_OFFSET ( 0.0F)
57 #define TRI_Y_OFFSET ( 0.0F)
58 #define LINE_X_OFFSET ( 0.0F)
59 #define LINE_Y_OFFSET ( 0.125F)
60 #define PNT_X_OFFSET ( 0.375F)
61 #define PNT_Y_OFFSET ( 0.375F)
62
63 static void fxRasterPrimitive( GLcontext *ctx, GLenum prim );
64 static void fxRenderPrimitive( GLcontext *ctx, GLenum prim );
65
66 /***********************************************************************
67 * Macros for t_dd_tritmp.h to draw basic primitives *
68 ***********************************************************************/
69
70 #define TRI( a, b, c ) \
71 do { \
72 if (DO_FALLBACK) \
73 fxMesa->draw_tri( fxMesa, a, b, c ); \
74 else \
75 grDrawTriangle( a, b, c ); \
76 } while (0) \
77
78 #define QUAD( a, b, c, d ) \
79 do { \
80 if (DO_FALLBACK) { \
81 fxMesa->draw_tri( fxMesa, a, b, d ); \
82 fxMesa->draw_tri( fxMesa, b, c, d ); \
83 } else { \
84 GrVertex *_v_[4]; \
85 _v_[0] = d; \
86 _v_[1] = a; \
87 _v_[2] = b; \
88 _v_[3] = c; \
89 grDrawVertexArray(GR_TRIANGLE_FAN, 4, _v_);\
90 /*grDrawTriangle( a, b, d );*/ \
91 /*grDrawTriangle( b, c, d );*/ \
92 } \
93 } while (0)
94
95 #define LINE( v0, v1 ) \
96 do { \
97 if (DO_FALLBACK) \
98 fxMesa->draw_line( fxMesa, v0, v1 ); \
99 else { \
100 v0->x += LINE_X_OFFSET - TRI_X_OFFSET; \
101 v0->y += LINE_Y_OFFSET - TRI_Y_OFFSET; \
102 v1->x += LINE_X_OFFSET - TRI_X_OFFSET; \
103 v1->y += LINE_Y_OFFSET - TRI_Y_OFFSET; \
104 grDrawLine( v0, v1 ); \
105 v0->x -= LINE_X_OFFSET - TRI_X_OFFSET; \
106 v0->y -= LINE_Y_OFFSET - TRI_Y_OFFSET; \
107 v1->x -= LINE_X_OFFSET - TRI_X_OFFSET; \
108 v1->y -= LINE_Y_OFFSET - TRI_Y_OFFSET; \
109 } \
110 } while (0)
111
112 #define POINT( v0 ) \
113 do { \
114 if (DO_FALLBACK) \
115 fxMesa->draw_point( fxMesa, v0 ); \
116 else { \
117 v0->x += PNT_X_OFFSET - TRI_X_OFFSET; \
118 v0->y += PNT_Y_OFFSET - TRI_Y_OFFSET; \
119 grDrawPoint( v0 ); \
120 v0->x -= PNT_X_OFFSET - TRI_X_OFFSET; \
121 v0->y -= PNT_Y_OFFSET - TRI_Y_OFFSET; \
122 } \
123 } while (0)
124
125
126 /***********************************************************************
127 * Fallback to swrast for basic primitives *
128 ***********************************************************************/
129
130 /* Build an SWvertex from a hardware vertex.
131 *
132 * This code is hit only when a mix of accelerated and unaccelerated
133 * primitives are being drawn, and only for the unaccelerated
134 * primitives.
135 */
136 static void
137 fx_translate_vertex( GLcontext *ctx, const GrVertex *src, SWvertex *dst)
138 {
139 fxMesaContext fxMesa = FX_CONTEXT(ctx);
140 GLuint ts0 = fxMesa->tmu_source[0];
141 GLuint ts1 = fxMesa->tmu_source[1];
142 GLfloat w = 1.0 / src->oow;
143
144 dst->win[0] = src->x;
145 dst->win[1] = src->y;
146 dst->win[2] = src->ooz;
147 dst->win[3] = src->oow;
148
149 dst->color[0] = src->pargb[2];
150 dst->color[1] = src->pargb[1];
151 dst->color[2] = src->pargb[0];
152 dst->color[3] = src->pargb[3];
153
154 dst->specular[0] = src->pspec[2];
155 dst->specular[1] = src->pspec[1];
156 dst->specular[2] = src->pspec[0];
157
158 dst->texcoord[ts0][0] = fxMesa->inv_s0scale * src->tmuvtx[0].sow * w;
159 dst->texcoord[ts0][1] = fxMesa->inv_t0scale * src->tmuvtx[0].tow * w;
160
161 if (fxMesa->stw_hint_state & GR_STWHINT_W_DIFF_TMU0)
162 dst->texcoord[ts0][3] = src->tmuvtx[0].oow * w;
163 else
164 dst->texcoord[ts0][3] = 1.0;
165
166 if (fxMesa->SetupIndex & SETUP_TMU1) {
167 dst->texcoord[ts1][0] = fxMesa->inv_s1scale * src->tmuvtx[1].sow * w;
168 dst->texcoord[ts1][1] = fxMesa->inv_t1scale * src->tmuvtx[1].tow * w;
169
170 if (fxMesa->stw_hint_state & GR_STWHINT_W_DIFF_TMU1)
171 dst->texcoord[ts1][3] = src->tmuvtx[1].oow * w;
172 else
173 dst->texcoord[ts1][3] = 1.0;
174 }
175
176 dst->pointSize = src->psize;
177 }
178
179
180 static void
181 fx_fallback_tri( fxMesaContext fxMesa,
182 GrVertex *v0,
183 GrVertex *v1,
184 GrVertex *v2 )
185 {
186 GLcontext *ctx = fxMesa->glCtx;
187 SWvertex v[3];
188
189 fx_translate_vertex( ctx, v0, &v[0] );
190 fx_translate_vertex( ctx, v1, &v[1] );
191 fx_translate_vertex( ctx, v2, &v[2] );
192 _swrast_Triangle( ctx, &v[0], &v[1], &v[2] );
193 }
194
195
196 static void
197 fx_fallback_line( fxMesaContext fxMesa,
198 GrVertex *v0,
199 GrVertex *v1 )
200 {
201 GLcontext *ctx = fxMesa->glCtx;
202 SWvertex v[2];
203 fx_translate_vertex( ctx, v0, &v[0] );
204 fx_translate_vertex( ctx, v1, &v[1] );
205 _swrast_Line( ctx, &v[0], &v[1] );
206 }
207
208
209 static void
210 fx_fallback_point( fxMesaContext fxMesa,
211 GrVertex *v0 )
212 {
213 GLcontext *ctx = fxMesa->glCtx;
214 SWvertex v[1];
215 fx_translate_vertex( ctx, v0, &v[0] );
216 _swrast_Point( ctx, &v[0] );
217 }
218
219 /***********************************************************************
220 * Functions to draw basic primitives *
221 ***********************************************************************/
222
223 static void fx_print_vertex( GLcontext *ctx, const GrVertex *v )
224 {
225 fprintf(stderr, "fx_print_vertex:\n");
226
227 fprintf(stderr, "\tvertex at %p\n", (void *) v);
228
229 fprintf(stderr, "\tx %f y %f z %f oow %f\n", v->x, v->y, v->ooz, v->oow);
230 fprintf(stderr, "\tr %d g %d b %d a %d\n", v->pargb[2], v->pargb[1], v->pargb[0], v->pargb[3]);
231
232 fprintf(stderr, "\n");
233 }
234
235 #define DO_FALLBACK 0
236
237 /* Need to do clip loop at each triangle when mixing swrast and hw
238 * rendering. These functions are only used when mixed-mode rendering
239 * is occurring.
240 */
241 static void fx_draw_triangle( fxMesaContext fxMesa,
242 GrVertex *v0,
243 GrVertex *v1,
244 GrVertex *v2 )
245 {
246 BEGIN_CLIP_LOOP();
247 TRI( v0, v1, v2 );
248 END_CLIP_LOOP();
249 }
250
251 static void fx_draw_line( fxMesaContext fxMesa,
252 GrVertex *v0,
253 GrVertex *v1 )
254 {
255 /* No support for wide lines (avoid wide/aa line fallback).
256 */
257 BEGIN_CLIP_LOOP();
258 LINE(v0, v1);
259 END_CLIP_LOOP();
260 }
261
262 static void fx_draw_point( fxMesaContext fxMesa,
263 GrVertex *v0 )
264 {
265 /* No support for wide points.
266 */
267 BEGIN_CLIP_LOOP();
268 POINT( v0 );
269 END_CLIP_LOOP();
270 }
271
272 #ifndef M_2PI
273 #define M_2PI 6.28318530717958647692528676655901
274 #endif
275 #define __GL_COSF cos
276 #define __GL_SINF sin
277 static void fx_draw_point_sprite ( fxMesaContext fxMesa,
278 GrVertex *v0, GLfloat psize )
279 {
280 const GLcontext *ctx = fxMesa->glCtx;
281
282 GLfloat radius;
283 GrVertex _v_[4];
284 GLuint ts0 = fxMesa->tmu_source[0];
285 GLuint ts1 = fxMesa->tmu_source[1];
286 GLfloat w = v0->oow;
287 GLfloat u0scale = fxMesa->s0scale * w;
288 GLfloat v0scale = fxMesa->t0scale * w;
289 GLfloat u1scale = fxMesa->s1scale * w;
290 GLfloat v1scale = fxMesa->t1scale * w;
291
292 radius = psize / 2.;
293 _v_[0] = *v0;
294 _v_[1] = *v0;
295 _v_[2] = *v0;
296 _v_[3] = *v0;
297 /* CLIP_LOOP ?!? */
298 /* point coverage? */
299 /* we don't care about culling here (see fxSetupCull) */
300
301 _v_[0].x -= radius;
302 _v_[0].y += radius;
303 _v_[1].x += radius;
304 _v_[1].y += radius;
305 _v_[2].x += radius;
306 _v_[2].y -= radius;
307 _v_[3].x -= radius;
308 _v_[3].y -= radius;
309
310 if (ctx->Point.CoordReplace[ts0]) {
311 _v_[0].tmuvtx[0].sow = 0;
312 _v_[0].tmuvtx[0].tow = 0;
313 _v_[1].tmuvtx[0].sow = u0scale;
314 _v_[1].tmuvtx[0].tow = 0;
315 _v_[2].tmuvtx[0].sow = u0scale;
316 _v_[2].tmuvtx[0].tow = v0scale;
317 _v_[3].tmuvtx[0].sow = 0;
318 _v_[3].tmuvtx[0].tow = v0scale;
319 }
320 if (ctx->Point.CoordReplace[ts1]) {
321 _v_[0].tmuvtx[1].sow = 0;
322 _v_[0].tmuvtx[1].tow = 0;
323 _v_[1].tmuvtx[1].sow = u1scale;
324 _v_[1].tmuvtx[1].tow = 0;
325 _v_[2].tmuvtx[1].sow = u1scale;
326 _v_[2].tmuvtx[1].tow = v1scale;
327 _v_[3].tmuvtx[1].sow = 0;
328 _v_[3].tmuvtx[1].tow = v1scale;
329 }
330
331 grDrawVertexArrayContiguous(GR_TRIANGLE_FAN, 4, _v_, sizeof(GrVertex));
332 }
333
334 static void fx_draw_point_wide ( fxMesaContext fxMesa,
335 GrVertex *v0 )
336 {
337 GLint i, n;
338 GLfloat ang, radius, oon;
339 GrVertex vtxB, vtxC;
340 GrVertex *_v_[3];
341
342 const GLcontext *ctx = fxMesa->glCtx;
343 const GLfloat psize = (ctx->_TriangleCaps & DD_POINT_ATTEN) ? v0->psize : ctx->Point.Size;
344
345 if (ctx->Point.PointSprite) {
346 fx_draw_point_sprite(fxMesa, v0, psize);
347 return;
348 }
349
350 _v_[0] = v0;
351 _v_[1] = &vtxB;
352 _v_[2] = &vtxC;
353
354 radius = psize / 2.;
355 n = IROUND(psize * 2); /* radius x 4 */
356 if (n < 4) n = 4;
357 oon = 1.0 / (GLfloat)n;
358
359 /* CLIP_LOOP ?!? */
360 /* point coverage? */
361 /* we don't care about culling here (see fxSetupCull) */
362
363 vtxB = *v0;
364 vtxC = *v0;
365
366 vtxB.x += radius;
367 ang = M_2PI * oon;
368 vtxC.x += radius * __GL_COSF(ang);
369 vtxC.y += radius * __GL_SINF(ang);
370 grDrawVertexArray(GR_TRIANGLE_FAN, 3, _v_);
371 for (i = 2; i <= n; i++) {
372 ang = M_2PI * i * oon;
373 vtxC.x = v0->x + radius * __GL_COSF(ang);
374 vtxC.y = v0->y + radius * __GL_SINF(ang);
375 grDrawVertexArray(GR_TRIANGLE_FAN_CONTINUE, 1, &_v_[2]);
376 }
377 }
378
379 static void fx_render_pw_verts( GLcontext *ctx,
380 GLuint start,
381 GLuint count,
382 GLuint flags )
383 {
384 fxMesaContext fxMesa = FX_CONTEXT(ctx);
385 GrVertex *fxVB = fxMesa->verts;
386 (void) flags;
387
388 fxRenderPrimitive( ctx, GL_POINTS );
389
390 for ( ; start < count ; start++)
391 fx_draw_point_wide(fxMesa, fxVB + start);
392 }
393
394 static void fx_render_pw_elts ( GLcontext *ctx,
395 GLuint start,
396 GLuint count,
397 GLuint flags )
398 {
399 fxMesaContext fxMesa = FX_CONTEXT(ctx);
400 GrVertex *fxVB = fxMesa->verts;
401 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts;
402 (void) flags;
403
404 fxRenderPrimitive( ctx, GL_POINTS );
405
406 for ( ; start < count ; start++)
407 fx_draw_point_wide(fxMesa, fxVB + elt[start]);
408 }
409
410 static void fx_draw_point_wide_aa ( fxMesaContext fxMesa,
411 GrVertex *v0 )
412 {
413 GLint i, n;
414 GLfloat ang, radius, oon;
415 GrVertex vtxB, vtxC;
416
417 const GLcontext *ctx = fxMesa->glCtx;
418 const GLfloat psize = (ctx->_TriangleCaps & DD_POINT_ATTEN) ? v0->psize : ctx->Point.Size;
419
420 if (ctx->Point.PointSprite) {
421 fx_draw_point_sprite(fxMesa, v0, psize);
422 return;
423 }
424
425 radius = psize / 2.;
426 n = IROUND(psize * 2); /* radius x 4 */
427 if (n < 4) n = 4;
428 oon = 1.0 / (GLfloat)n;
429
430 /* CLIP_LOOP ?!? */
431 /* point coverage? */
432 /* we don't care about culling here (see fxSetupCull) */
433
434 vtxB = *v0;
435 vtxC = *v0;
436
437 vtxB.x += radius;
438 for (i = 1; i <= n; i++) {
439 ang = M_2PI * i * oon;
440 vtxC.x = v0->x + radius * __GL_COSF(ang);
441 vtxC.y = v0->y + radius * __GL_SINF(ang);
442 grAADrawTriangle( v0, &vtxB, &vtxC, FXFALSE, FXTRUE, FXFALSE);
443 /*grDrawTriangle( v0, &vtxB, &vtxC);*/
444 vtxB.x = vtxC.x;
445 vtxB.y = vtxC.y;
446 }
447 }
448 #undef __GLCOSF
449 #undef __GLSINF
450 #undef M_2PI
451
452 #undef DO_FALLBACK
453
454
455 #define FX_UNFILLED_BIT 0x1
456 #define FX_OFFSET_BIT 0x2
457 #define FX_TWOSIDE_BIT 0x4
458 #define FX_FLAT_BIT 0x8
459 #define FX_FALLBACK_BIT 0x10
460 #define FX_MAX_TRIFUNC 0x20
461
462 static struct {
463 tnl_points_func points;
464 tnl_line_func line;
465 tnl_triangle_func triangle;
466 tnl_quad_func quad;
467 } rast_tab[FX_MAX_TRIFUNC];
468
469 #define DO_FALLBACK (IND & FX_FALLBACK_BIT)
470 #define DO_OFFSET (IND & FX_OFFSET_BIT)
471 #define DO_UNFILLED (IND & FX_UNFILLED_BIT)
472 #define DO_TWOSIDE (IND & FX_TWOSIDE_BIT)
473 #define DO_FLAT (IND & FX_FLAT_BIT)
474 #define DO_TRI 1
475 #define DO_QUAD 1
476 #define DO_LINE 1
477 #define DO_POINTS 1
478 #define DO_FULL_QUAD 1
479
480 #define HAVE_RGBA 1
481 #define HAVE_SPEC 1
482 #define HAVE_HW_FLATSHADE 0
483 #define HAVE_BACK_COLORS 0
484 #define VERTEX GrVertex
485 #define TAB rast_tab
486
487 #define DEPTH_SCALE 1.0
488 #define UNFILLED_TRI unfilled_tri
489 #define UNFILLED_QUAD unfilled_quad
490 #define VERT_X(_v) _v->x
491 #define VERT_Y(_v) _v->y
492 #define VERT_Z(_v) _v->ooz
493 #define GET_VERTEX(e) (fxMesa->verts + e)
494
495 #ifdef USE_IEEE
496 #define AREA_IS_CCW( a ) (((fi_type *)&(a))->i < 0)
497 #else
498 #define AREA_IS_CCW( a ) (a < 0)
499 #endif
500
501
502 #define VERT_SET_RGBA( dst, f ) \
503 do { \
504 UNCLAMPED_FLOAT_TO_UBYTE(dst->pargb[2], f[0]);\
505 UNCLAMPED_FLOAT_TO_UBYTE(dst->pargb[1], f[1]);\
506 UNCLAMPED_FLOAT_TO_UBYTE(dst->pargb[0], f[2]);\
507 UNCLAMPED_FLOAT_TO_UBYTE(dst->pargb[3], f[3]);\
508 } while (0)
509
510 #define VERT_COPY_RGBA( v0, v1 ) \
511 *(GLuint *)&v0->pargb = *(GLuint *)&v1->pargb
512
513 #define VERT_SAVE_RGBA( idx ) \
514 *(GLuint *)&color[idx] = *(GLuint *)&v[idx]->pargb
515
516 #define VERT_RESTORE_RGBA( idx ) \
517 *(GLuint *)&v[idx]->pargb = *(GLuint *)&color[idx]
518
519
520 #define VERT_SET_SPEC( dst, f ) \
521 do { \
522 UNCLAMPED_FLOAT_TO_UBYTE(dst->pspec[2], f[0]);\
523 UNCLAMPED_FLOAT_TO_UBYTE(dst->pspec[1], f[1]);\
524 UNCLAMPED_FLOAT_TO_UBYTE(dst->pspec[0], f[2]);\
525 } while (0)
526
527 #define VERT_COPY_SPEC( v0, v1 ) \
528 *(GLuint *)&v0->pspec = *(GLuint *)&v1->pspec
529
530 #define VERT_SAVE_SPEC( idx ) \
531 *(GLuint *)&spec[idx] = *(GLuint *)&v[idx]->pspec
532
533 #define VERT_RESTORE_SPEC( idx ) \
534 *(GLuint *)&v[idx]->pspec = *(GLuint *)&spec[idx]
535
536
537 #define LOCAL_VARS(n) \
538 fxMesaContext fxMesa = FX_CONTEXT(ctx); \
539 GLubyte color[n][4], spec[n][4]; \
540 (void) color; (void) spec;
541
542
543
544 /***********************************************************************
545 * Functions to draw basic unfilled primitives *
546 ***********************************************************************/
547
548 #define RASTERIZE(x) if (fxMesa->raster_primitive != x) \
549 fxRasterPrimitive( ctx, x )
550 #define RENDER_PRIMITIVE fxMesa->render_primitive
551 #define IND FX_FALLBACK_BIT
552 #define TAG(x) x
553 #include "tnl_dd/t_dd_unfilled.h"
554 #undef IND
555
556 /***********************************************************************
557 * Functions to draw GL primitives *
558 ***********************************************************************/
559
560 #define IND (0)
561 #define TAG(x) x
562 #include "tnl_dd/t_dd_tritmp.h"
563
564 #define IND (FX_OFFSET_BIT)
565 #define TAG(x) x##_offset
566 #include "tnl_dd/t_dd_tritmp.h"
567
568 #define IND (FX_TWOSIDE_BIT)
569 #define TAG(x) x##_twoside
570 #include "tnl_dd/t_dd_tritmp.h"
571
572 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT)
573 #define TAG(x) x##_twoside_offset
574 #include "tnl_dd/t_dd_tritmp.h"
575
576 #define IND (FX_UNFILLED_BIT)
577 #define TAG(x) x##_unfilled
578 #include "tnl_dd/t_dd_tritmp.h"
579
580 #define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT)
581 #define TAG(x) x##_offset_unfilled
582 #include "tnl_dd/t_dd_tritmp.h"
583
584 #define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT)
585 #define TAG(x) x##_twoside_unfilled
586 #include "tnl_dd/t_dd_tritmp.h"
587
588 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT)
589 #define TAG(x) x##_twoside_offset_unfilled
590 #include "tnl_dd/t_dd_tritmp.h"
591
592 #define IND (FX_FALLBACK_BIT)
593 #define TAG(x) x##_fallback
594 #include "tnl_dd/t_dd_tritmp.h"
595
596 #define IND (FX_OFFSET_BIT|FX_FALLBACK_BIT)
597 #define TAG(x) x##_offset_fallback
598 #include "tnl_dd/t_dd_tritmp.h"
599
600 #define IND (FX_TWOSIDE_BIT|FX_FALLBACK_BIT)
601 #define TAG(x) x##_twoside_fallback
602 #include "tnl_dd/t_dd_tritmp.h"
603
604 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FALLBACK_BIT)
605 #define TAG(x) x##_twoside_offset_fallback
606 #include "tnl_dd/t_dd_tritmp.h"
607
608 #define IND (FX_UNFILLED_BIT|FX_FALLBACK_BIT)
609 #define TAG(x) x##_unfilled_fallback
610 #include "tnl_dd/t_dd_tritmp.h"
611
612 #define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT)
613 #define TAG(x) x##_offset_unfilled_fallback
614 #include "tnl_dd/t_dd_tritmp.h"
615
616 #define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT)
617 #define TAG(x) x##_twoside_unfilled_fallback
618 #include "tnl_dd/t_dd_tritmp.h"
619
620 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT| \
621 FX_FALLBACK_BIT)
622 #define TAG(x) x##_twoside_offset_unfilled_fallback
623 #include "tnl_dd/t_dd_tritmp.h"
624
625
626 /* Fx doesn't support provoking-vertex flat-shading?
627 */
628 #define IND (FX_FLAT_BIT)
629 #define TAG(x) x##_flat
630 #include "tnl_dd/t_dd_tritmp.h"
631
632 #define IND (FX_OFFSET_BIT|FX_FLAT_BIT)
633 #define TAG(x) x##_offset_flat
634 #include "tnl_dd/t_dd_tritmp.h"
635
636 #define IND (FX_TWOSIDE_BIT|FX_FLAT_BIT)
637 #define TAG(x) x##_twoside_flat
638 #include "tnl_dd/t_dd_tritmp.h"
639
640 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FLAT_BIT)
641 #define TAG(x) x##_twoside_offset_flat
642 #include "tnl_dd/t_dd_tritmp.h"
643
644 #define IND (FX_UNFILLED_BIT|FX_FLAT_BIT)
645 #define TAG(x) x##_unfilled_flat
646 #include "tnl_dd/t_dd_tritmp.h"
647
648 #define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT)
649 #define TAG(x) x##_offset_unfilled_flat
650 #include "tnl_dd/t_dd_tritmp.h"
651
652 #define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT)
653 #define TAG(x) x##_twoside_unfilled_flat
654 #include "tnl_dd/t_dd_tritmp.h"
655
656 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT)
657 #define TAG(x) x##_twoside_offset_unfilled_flat
658 #include "tnl_dd/t_dd_tritmp.h"
659
660 #define IND (FX_FALLBACK_BIT|FX_FLAT_BIT)
661 #define TAG(x) x##_fallback_flat
662 #include "tnl_dd/t_dd_tritmp.h"
663
664 #define IND (FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
665 #define TAG(x) x##_offset_fallback_flat
666 #include "tnl_dd/t_dd_tritmp.h"
667
668 #define IND (FX_TWOSIDE_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
669 #define TAG(x) x##_twoside_fallback_flat
670 #include "tnl_dd/t_dd_tritmp.h"
671
672 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
673 #define TAG(x) x##_twoside_offset_fallback_flat
674 #include "tnl_dd/t_dd_tritmp.h"
675
676 #define IND (FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
677 #define TAG(x) x##_unfilled_fallback_flat
678 #include "tnl_dd/t_dd_tritmp.h"
679
680 #define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
681 #define TAG(x) x##_offset_unfilled_fallback_flat
682 #include "tnl_dd/t_dd_tritmp.h"
683
684 #define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT)
685 #define TAG(x) x##_twoside_unfilled_fallback_flat
686 #include "tnl_dd/t_dd_tritmp.h"
687
688 #define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT| \
689 FX_FALLBACK_BIT|FX_FLAT_BIT)
690 #define TAG(x) x##_twoside_offset_unfilled_fallback_flat
691 #include "tnl_dd/t_dd_tritmp.h"
692
693
694 static void init_rast_tab( void )
695 {
696 init();
697 init_offset();
698 init_twoside();
699 init_twoside_offset();
700 init_unfilled();
701 init_offset_unfilled();
702 init_twoside_unfilled();
703 init_twoside_offset_unfilled();
704 init_fallback();
705 init_offset_fallback();
706 init_twoside_fallback();
707 init_twoside_offset_fallback();
708 init_unfilled_fallback();
709 init_offset_unfilled_fallback();
710 init_twoside_unfilled_fallback();
711 init_twoside_offset_unfilled_fallback();
712
713 init_flat();
714 init_offset_flat();
715 init_twoside_flat();
716 init_twoside_offset_flat();
717 init_unfilled_flat();
718 init_offset_unfilled_flat();
719 init_twoside_unfilled_flat();
720 init_twoside_offset_unfilled_flat();
721 init_fallback_flat();
722 init_offset_fallback_flat();
723 init_twoside_fallback_flat();
724 init_twoside_offset_fallback_flat();
725 init_unfilled_fallback_flat();
726 init_offset_unfilled_fallback_flat();
727 init_twoside_unfilled_fallback_flat();
728 init_twoside_offset_unfilled_fallback_flat();
729 }
730
731
732 /**********************************************************************/
733 /* Render whole begin/end objects */
734 /**********************************************************************/
735
736
737 /* Accelerate vertex buffer rendering when renderindex == 0 and
738 * there is no clipping.
739 */
740 #define INIT(x) fxRenderPrimitive( ctx, x )
741
742 static void fx_render_vb_points( GLcontext *ctx,
743 GLuint start,
744 GLuint count,
745 GLuint flags )
746 {
747 fxMesaContext fxMesa = FX_CONTEXT(ctx);
748 GrVertex *fxVB = fxMesa->verts;
749 GLint i;
750 (void) flags;
751
752 if (TDFX_DEBUG & VERBOSE_VARRAY) {
753 fprintf(stderr, "fx_render_vb_points\n");
754 }
755
756 INIT(GL_POINTS);
757
758 /* Adjust point coords */
759 for (i = start; i < count; i++) {
760 fxVB[i].x += PNT_X_OFFSET - TRI_X_OFFSET;
761 fxVB[i].y += PNT_Y_OFFSET - TRI_Y_OFFSET;
762 }
763
764 grDrawVertexArrayContiguous( GR_POINTS, count-start,
765 fxVB + start, sizeof(GrVertex));
766 /* restore point coords */
767 for (i = start; i < count; i++) {
768 fxVB[i].x -= PNT_X_OFFSET - TRI_X_OFFSET;
769 fxVB[i].y -= PNT_Y_OFFSET - TRI_Y_OFFSET;
770 }
771 }
772
773 static void fx_render_vb_line_strip( GLcontext *ctx,
774 GLuint start,
775 GLuint count,
776 GLuint flags )
777 {
778 fxMesaContext fxMesa = FX_CONTEXT(ctx);
779 GrVertex *fxVB = fxMesa->verts;
780 GLint i;
781 (void) flags;
782
783 if (TDFX_DEBUG & VERBOSE_VARRAY) {
784 fprintf(stderr, "fx_render_vb_line_strip\n");
785 }
786
787 INIT(GL_LINE_STRIP);
788
789 /* adjust line coords */
790 for (i = start; i < count; i++) {
791 fxVB[i].x += LINE_X_OFFSET - TRI_X_OFFSET;
792 fxVB[i].y += LINE_Y_OFFSET - TRI_Y_OFFSET;
793 }
794
795 grDrawVertexArrayContiguous( GR_LINE_STRIP, count-start,
796 fxVB + start, sizeof(GrVertex));
797
798 /* restore line coords */
799 for (i = start; i < count; i++) {
800 fxVB[i].x -= LINE_X_OFFSET - TRI_X_OFFSET;
801 fxVB[i].y -= LINE_Y_OFFSET - TRI_Y_OFFSET;
802 }
803 }
804
805 static void fx_render_vb_line_loop( GLcontext *ctx,
806 GLuint start,
807 GLuint count,
808 GLuint flags )
809 {
810 fxMesaContext fxMesa = FX_CONTEXT(ctx);
811 GrVertex *fxVB = fxMesa->verts;
812 GLint i;
813 GLint j = start;
814 (void) flags;
815
816 if (TDFX_DEBUG & VERBOSE_VARRAY) {
817 fprintf(stderr, "fx_render_vb_line_loop\n");
818 }
819
820 INIT(GL_LINE_LOOP);
821
822 if (!(flags & PRIM_BEGIN)) {
823 j++;
824 }
825
826 /* adjust line coords */
827 for (i = start; i < count; i++) {
828 fxVB[i].x += LINE_X_OFFSET - TRI_X_OFFSET;
829 fxVB[i].y += LINE_Y_OFFSET - TRI_Y_OFFSET;
830 }
831
832 grDrawVertexArrayContiguous( GR_LINE_STRIP, count-j,
833 fxVB + j, sizeof(GrVertex));
834
835 if (flags & PRIM_END)
836 grDrawLine( fxVB + (count - 1),
837 fxVB + start );
838
839 /* restore line coords */
840 for (i = start; i < count; i++) {
841 fxVB[i].x -= LINE_X_OFFSET - TRI_X_OFFSET;
842 fxVB[i].y -= LINE_Y_OFFSET - TRI_Y_OFFSET;
843 }
844 }
845
846 static void fx_render_vb_lines( GLcontext *ctx,
847 GLuint start,
848 GLuint count,
849 GLuint flags )
850 {
851 fxMesaContext fxMesa = FX_CONTEXT(ctx);
852 GrVertex *fxVB = fxMesa->verts;
853 GLint i;
854 (void) flags;
855
856 if (TDFX_DEBUG & VERBOSE_VARRAY) {
857 fprintf(stderr, "fx_render_vb_lines\n");
858 }
859
860 INIT(GL_LINES);
861
862 /* adjust line coords */
863 for (i = start; i < count; i++) {
864 fxVB[i].x += LINE_X_OFFSET - TRI_X_OFFSET;
865 fxVB[i].y += LINE_Y_OFFSET - TRI_Y_OFFSET;
866 }
867
868 grDrawVertexArrayContiguous( GR_LINES, count-start,
869 fxVB + start, sizeof(GrVertex));
870
871 /* restore line coords */
872 for (i = start; i < count; i++) {
873 fxVB[i].x -= LINE_X_OFFSET - TRI_X_OFFSET;
874 fxVB[i].y -= LINE_Y_OFFSET - TRI_Y_OFFSET;
875 }
876 }
877
878 static void fx_render_vb_triangles( GLcontext *ctx,
879 GLuint start,
880 GLuint count,
881 GLuint flags )
882 {
883 fxMesaContext fxMesa = FX_CONTEXT(ctx);
884 GrVertex *fxVB = fxMesa->verts;
885 GLuint j;
886 (void) flags;
887
888 if (TDFX_DEBUG & VERBOSE_VARRAY) {
889 fprintf(stderr, "fx_render_vb_triangles\n");
890 }
891
892 INIT(GL_TRIANGLES);
893
894 for (j=start+2; j<count; j+=3) {
895 grDrawTriangle(fxVB + (j-2), fxVB + (j-1), fxVB + j);
896 }
897 }
898
899
900 static void fx_render_vb_tri_strip( GLcontext *ctx,
901 GLuint start,
902 GLuint count,
903 GLuint flags )
904 {
905 fxMesaContext fxMesa = FX_CONTEXT(ctx);
906 GrVertex *fxVB = fxMesa->verts;
907 int mode;
908 (void) flags;
909
910 if (TDFX_DEBUG & VERBOSE_VARRAY) {
911 fprintf(stderr, "fx_render_vb_tri_strip\n");
912 }
913
914 INIT(GL_TRIANGLE_STRIP);
915
916 /* [dBorca] WTF?!?
917 if (flags & PRIM_PARITY)
918 mode = GR_TRIANGLE_STRIP_CONTINUE;
919 else*/
920 mode = GR_TRIANGLE_STRIP;
921
922 grDrawVertexArrayContiguous( mode, count-start,
923 fxVB + start, sizeof(GrVertex));
924 }
925
926
927 static void fx_render_vb_tri_fan( GLcontext *ctx,
928 GLuint start,
929 GLuint count,
930 GLuint flags )
931 {
932 fxMesaContext fxMesa = FX_CONTEXT(ctx);
933 GrVertex *fxVB = fxMesa->verts;
934 (void) flags;
935
936 if (TDFX_DEBUG & VERBOSE_VARRAY) {
937 fprintf(stderr, "fx_render_vb_tri_fan\n");
938 }
939
940 INIT(GL_TRIANGLE_FAN);
941
942 grDrawVertexArrayContiguous( GR_TRIANGLE_FAN, count-start,
943 fxVB + start, sizeof(GrVertex) );
944 }
945
946 static void fx_render_vb_quads( GLcontext *ctx,
947 GLuint start,
948 GLuint count,
949 GLuint flags )
950 {
951 fxMesaContext fxMesa = FX_CONTEXT(ctx);
952 GrVertex *fxVB = fxMesa->verts;
953 GLuint i;
954 (void) flags;
955
956 if (TDFX_DEBUG & VERBOSE_VARRAY) {
957 fprintf(stderr, "fx_render_vb_quads\n");
958 }
959
960 INIT(GL_QUADS);
961
962 for (i = start + 3 ; i < count ; i += 4 ) {
963 #define VERT(x) (fxVB + (x))
964 GrVertex *_v_[4];
965 _v_[0] = VERT(i);
966 _v_[1] = VERT(i-3);
967 _v_[2] = VERT(i-2);
968 _v_[3] = VERT(i-1);
969 grDrawVertexArray(GR_TRIANGLE_FAN, 4, _v_);
970 /*grDrawTriangle( VERT(i-3), VERT(i-2), VERT(i) );*/
971 /*grDrawTriangle( VERT(i-2), VERT(i-1), VERT(i) );*/
972 #undef VERT
973 }
974 }
975
976 static void fx_render_vb_quad_strip( GLcontext *ctx,
977 GLuint start,
978 GLuint count,
979 GLuint flags )
980 {
981 fxMesaContext fxMesa = FX_CONTEXT(ctx);
982 GrVertex *fxVB = fxMesa->verts;
983 (void) flags;
984
985 if (TDFX_DEBUG & VERBOSE_VARRAY) {
986 fprintf(stderr, "fx_render_vb_quad_strip\n");
987 }
988
989 INIT(GL_QUAD_STRIP);
990
991 count -= (count-start)&1;
992
993 grDrawVertexArrayContiguous( GR_TRIANGLE_STRIP,
994 count-start, fxVB + start, sizeof(GrVertex));
995 }
996
997 static void fx_render_vb_poly( GLcontext *ctx,
998 GLuint start,
999 GLuint count,
1000 GLuint flags )
1001 {
1002 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1003 GrVertex *fxVB = fxMesa->verts;
1004 (void) flags;
1005
1006 if (TDFX_DEBUG & VERBOSE_VARRAY) {
1007 fprintf(stderr, "fx_render_vb_poly\n");
1008 }
1009
1010 INIT(GL_POLYGON);
1011
1012 grDrawVertexArrayContiguous( GR_POLYGON, count-start,
1013 fxVB + start, sizeof(GrVertex));
1014 }
1015
1016 static void fx_render_vb_noop( GLcontext *ctx,
1017 GLuint start,
1018 GLuint count,
1019 GLuint flags )
1020 {
1021 (void) (ctx && start && count && flags);
1022 }
1023
1024 static void (*fx_render_tab_verts[GL_POLYGON+2])(GLcontext *,
1025 GLuint,
1026 GLuint,
1027 GLuint) =
1028 {
1029 fx_render_vb_points,
1030 fx_render_vb_lines,
1031 fx_render_vb_line_loop,
1032 fx_render_vb_line_strip,
1033 fx_render_vb_triangles,
1034 fx_render_vb_tri_strip,
1035 fx_render_vb_tri_fan,
1036 fx_render_vb_quads,
1037 fx_render_vb_quad_strip,
1038 fx_render_vb_poly,
1039 fx_render_vb_noop,
1040 };
1041 #undef INIT
1042
1043
1044 /**********************************************************************/
1045 /* Render whole (indexed) begin/end objects */
1046 /**********************************************************************/
1047
1048
1049 #define VERT(x) (vertptr + x)
1050
1051 #define RENDER_POINTS( start, count ) \
1052 for ( ; start < count ; start++) \
1053 grDrawPoint( VERT(ELT(start)) );
1054
1055 #define RENDER_LINE( v0, v1 ) \
1056 grDrawLine( VERT(v0), VERT(v1) )
1057
1058 #define RENDER_TRI( v0, v1, v2 ) \
1059 grDrawTriangle( VERT(v0), VERT(v1), VERT(v2) )
1060
1061 #define RENDER_QUAD( v0, v1, v2, v3 ) \
1062 do { \
1063 GrVertex *_v_[4]; \
1064 _v_[0] = VERT(v3);\
1065 _v_[1] = VERT(v0);\
1066 _v_[2] = VERT(v1);\
1067 _v_[3] = VERT(v2);\
1068 grDrawVertexArray(GR_TRIANGLE_FAN, 4, _v_);\
1069 /*grDrawTriangle( VERT(v0), VERT(v1), VERT(v3) );*/\
1070 /*grDrawTriangle( VERT(v1), VERT(v2), VERT(v3) );*/\
1071 } while (0)
1072
1073 #define INIT(x) fxRenderPrimitive( ctx, x )
1074
1075 #undef LOCAL_VARS
1076 #define LOCAL_VARS \
1077 fxMesaContext fxMesa = FX_CONTEXT(ctx); \
1078 GrVertex *vertptr = fxMesa->verts; \
1079 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
1080 (void) elt;
1081
1082 #define RESET_STIPPLE
1083 #define RESET_OCCLUSION
1084 #define PRESERVE_VB_DEFS
1085
1086 /* Elts, no clipping.
1087 */
1088 #undef ELT
1089 #undef TAG
1090 #define TAG(x) fx_##x##_elts
1091 #define ELT(x) elt[x]
1092 #include "tnl_dd/t_dd_rendertmp.h"
1093
1094 /* Verts, no clipping.
1095 */
1096 #undef ELT
1097 #undef TAG
1098 #define TAG(x) fx_##x##_verts
1099 #define ELT(x) x
1100 /*#include "tnl_dd/t_dd_rendertmp.h"*/ /* we have fx_render_vb_* now */
1101
1102
1103
1104 /**********************************************************************/
1105 /* Render clipped primitives */
1106 /**********************************************************************/
1107
1108
1109
1110 static void fxRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
1111 GLuint n )
1112 {
1113 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1114 TNLcontext *tnl = TNL_CONTEXT(ctx);
1115 struct vertex_buffer *VB = &tnl->vb;
1116 GLuint prim = fxMesa->render_primitive;
1117
1118 /* Render the new vertices as an unclipped polygon.
1119 */
1120 {
1121 GLuint *tmp = VB->Elts;
1122 VB->Elts = (GLuint *)elts;
1123 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n,
1124 PRIM_BEGIN|PRIM_END );
1125 VB->Elts = tmp;
1126 }
1127
1128 /* Restore the render primitive
1129 */
1130 if (prim != GL_POLYGON)
1131 tnl->Driver.Render.PrimitiveNotify( ctx, prim );
1132 }
1133
1134
1135 static void fxFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
1136 GLuint n )
1137 {
1138 int i;
1139 fxMesaContext fxMesa = FX_CONTEXT( ctx );
1140 GrVertex *vertptr = fxMesa->verts;
1141 if (n == 3) {
1142 grDrawTriangle( VERT(elts[0]), VERT(elts[1]), VERT(elts[2]) );
1143 } else if (n <= 32) {
1144 GrVertex *newvptr[32];
1145 for (i = 0 ; i < n ; i++) {
1146 newvptr[i] = VERT(elts[i]);
1147 }
1148 grDrawVertexArray(GR_TRIANGLE_FAN, n, newvptr);
1149 } else {
1150 const GrVertex *start = VERT(elts[0]);
1151 for (i = 2 ; i < n ; i++) {
1152 grDrawTriangle( start, VERT(elts[i-1]), VERT(elts[i]) );
1153 }
1154 }
1155 }
1156
1157 /**********************************************************************/
1158 /* Choose render functions */
1159 /**********************************************************************/
1160
1161
1162 #define POINT_FALLBACK (DD_POINT_SMOOTH)
1163 #define LINE_FALLBACK (DD_LINE_STIPPLE)
1164 #define TRI_FALLBACK (DD_TRI_SMOOTH | DD_TRI_STIPPLE)
1165 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK | LINE_FALLBACK | TRI_FALLBACK)
1166 #define ANY_RASTER_FLAGS (DD_FLATSHADE | DD_TRI_LIGHT_TWOSIDE | DD_TRI_OFFSET \
1167 | DD_TRI_UNFILLED)
1168
1169
1170
1171 void fxDDChooseRenderState(GLcontext *ctx)
1172 {
1173 TNLcontext *tnl = TNL_CONTEXT(ctx);
1174 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1175 GLuint flags = ctx->_TriangleCaps;
1176 GLuint index = 0;
1177
1178 if (flags & (ANY_FALLBACK_FLAGS|ANY_RASTER_FLAGS)) {
1179 if (flags & ANY_RASTER_FLAGS) {
1180 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= FX_TWOSIDE_BIT;
1181 if (flags & DD_TRI_OFFSET) index |= FX_OFFSET_BIT;
1182 if (flags & DD_TRI_UNFILLED) index |= FX_UNFILLED_BIT;
1183 if (flags & DD_FLATSHADE) index |= FX_FLAT_BIT;
1184 }
1185
1186 fxMesa->draw_point = fx_draw_point;
1187 fxMesa->draw_line = fx_draw_line;
1188 fxMesa->draw_tri = fx_draw_triangle;
1189
1190 /* Hook in fallbacks for specific primitives. */
1191 if (flags & (POINT_FALLBACK|
1192 LINE_FALLBACK|
1193 TRI_FALLBACK))
1194 {
1195 if (fxMesa->verbose) {
1196 fprintf(stderr, "Voodoo ! fallback (%x), raster (%x)\n",
1197 flags & ANY_FALLBACK_FLAGS, flags & ANY_RASTER_FLAGS);
1198 }
1199
1200 if (flags & POINT_FALLBACK)
1201 fxMesa->draw_point = fx_fallback_point;
1202
1203 if (flags & LINE_FALLBACK)
1204 fxMesa->draw_line = fx_fallback_line;
1205
1206 if (flags & TRI_FALLBACK)
1207 fxMesa->draw_tri = fx_fallback_tri;
1208
1209 index |= FX_FALLBACK_BIT;
1210 }
1211 }
1212
1213 tnl->Driver.Render.Points = rast_tab[index].points;
1214 tnl->Driver.Render.Line = rast_tab[index].line;
1215 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
1216 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
1217 tnl->Driver.Render.Quad = rast_tab[index].quad;
1218
1219 if (index == 0) {
1220 tnl->Driver.Render.PrimTabVerts = fx_render_tab_verts;
1221 tnl->Driver.Render.PrimTabElts = fx_render_tab_elts;
1222 tnl->Driver.Render.ClippedPolygon = fxFastRenderClippedPoly;
1223 } else {
1224 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
1225 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
1226 tnl->Driver.Render.ClippedPolygon = fxRenderClippedPoly;
1227 }
1228
1229 fxMesa->render_index = index;
1230
1231 /* [dBorca] Hack alert: more a trick than a real plug-in!!! */
1232 if (flags & (DD_POINT_SIZE | DD_POINT_ATTEN)) {
1233 /* We need to set the point primitive to go through "rast_tab",
1234 * to make sure "POINT" calls "fxMesa->draw_point" instead of
1235 * "grDrawPoint". We can achieve this by using FX_FALLBACK_BIT
1236 * (not really a total rasterization fallback, so we don't alter
1237 * "fxMesa->render_index"). If we get here with DD_POINT_SMOOTH,
1238 * we're done, cos we've already set _tnl_render_tab_{verts|elts}
1239 * above. Otherwise, the T&L engine can optimize point rendering
1240 * by using fx_render_tab_{verts|elts} hence the extra work.
1241 */
1242 if (flags & DD_POINT_SMOOTH) {
1243 fxMesa->draw_point = fx_draw_point_wide_aa;
1244 } else {
1245 fxMesa->draw_point = fx_draw_point_wide;
1246 fx_render_tab_verts[0] = fx_render_pw_verts;
1247 fx_render_tab_elts[0] = fx_render_pw_elts;
1248 }
1249 tnl->Driver.Render.Points = rast_tab[index|FX_FALLBACK_BIT].points;
1250 } else {
1251 fx_render_tab_verts[0] = fx_render_vb_points;
1252 fx_render_tab_elts[0] = fx_render_points_elts;
1253 }
1254 }
1255
1256
1257 /**********************************************************************/
1258 /* Runtime render state and callbacks */
1259 /**********************************************************************/
1260
1261 static void fxRunPipeline( GLcontext *ctx )
1262 {
1263 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1264 GLuint new_gl_state = fxMesa->new_gl_state;
1265
1266 if (TDFX_DEBUG & VERBOSE_PIPELINE) {
1267 fprintf(stderr, "fxRunPipeline()\n");
1268 }
1269
1270 #if 0
1271 /* Recalculate fog table on projection matrix changes. This used to
1272 * be triggered by the NearFar callback.
1273 */
1274 if (new_gl_state & _NEW_PROJECTION)
1275 fxMesa->new_state |= FX_NEW_FOG;
1276 #endif
1277
1278 if (new_gl_state & _FX_NEW_IS_IN_HARDWARE)
1279 fxCheckIsInHardware(ctx);
1280
1281 if (fxMesa->new_state)
1282 fxSetupFXUnits(ctx);
1283
1284 if (!fxMesa->fallback) {
1285 if (new_gl_state & _FX_NEW_RENDERSTATE)
1286 fxDDChooseRenderState(ctx);
1287
1288 if (new_gl_state & _FX_NEW_SETUP_FUNCTION)
1289 fxChooseVertexState(ctx);
1290 }
1291
1292 if (new_gl_state & _NEW_TEXTURE) {
1293 struct gl_texture_unit *t0 = &ctx->Texture.Unit[fxMesa->tmu_source[0]];
1294 struct gl_texture_unit *t1 = &ctx->Texture.Unit[fxMesa->tmu_source[1]];
1295
1296 if (t0->_Current && FX_TEXTURE_DATA(t0)) {
1297 fxMesa->s0scale = FX_TEXTURE_DATA(t0)->sScale;
1298 fxMesa->t0scale = FX_TEXTURE_DATA(t0)->tScale;
1299 fxMesa->inv_s0scale = 1.0 / fxMesa->s0scale;
1300 fxMesa->inv_t0scale = 1.0 / fxMesa->t0scale;
1301 }
1302
1303 if (t1->_Current && FX_TEXTURE_DATA(t1)) {
1304 fxMesa->s1scale = FX_TEXTURE_DATA(t1)->sScale;
1305 fxMesa->t1scale = FX_TEXTURE_DATA(t1)->tScale;
1306 fxMesa->inv_s1scale = 1.0 / fxMesa->s1scale;
1307 fxMesa->inv_t1scale = 1.0 / fxMesa->t1scale;
1308 }
1309 }
1310
1311 fxMesa->new_gl_state = 0;
1312
1313 _tnl_run_pipeline( ctx );
1314 }
1315
1316
1317 static GLenum reduced_prim[GL_POLYGON+1] = {
1318 GL_POINTS,
1319 GL_LINES,
1320 GL_LINES,
1321 GL_LINES,
1322 GL_TRIANGLES,
1323 GL_TRIANGLES,
1324 GL_TRIANGLES,
1325 GL_TRIANGLES,
1326 GL_TRIANGLES,
1327 GL_TRIANGLES
1328 };
1329
1330
1331
1332 /* Always called between RenderStart and RenderFinish --> We already
1333 * hold the lock.
1334 */
1335 static void fxRasterPrimitive( GLcontext *ctx, GLenum prim )
1336 {
1337 fxMesaContext fxMesa = FX_CONTEXT( ctx );
1338
1339 fxMesa->raster_primitive = prim;
1340
1341 fxSetupCull(ctx);
1342 }
1343
1344
1345
1346 /* Determine the rasterized primitive when not drawing unfilled
1347 * polygons.
1348 */
1349 static void fxRenderPrimitive( GLcontext *ctx, GLenum prim )
1350 {
1351 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1352 GLuint rprim = reduced_prim[prim];
1353
1354 fxMesa->render_primitive = prim;
1355
1356 if (rprim == GL_TRIANGLES && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
1357 return;
1358
1359 if (fxMesa->raster_primitive != rprim) {
1360 fxRasterPrimitive( ctx, rprim );
1361 }
1362 }
1363
1364 static void fxRenderFinish( GLcontext *ctx )
1365 {
1366 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1367
1368 if (fxMesa->render_index & FX_FALLBACK_BIT)
1369 _swrast_flush( ctx );
1370 }
1371
1372
1373
1374 /**********************************************************************/
1375 /* Manage total rasterization fallbacks */
1376 /**********************************************************************/
1377
1378 static char *fallbackStrings[] = {
1379 "3D/Rect/Cube Texture map",
1380 "glDrawBuffer(GL_FRONT_AND_BACK)",
1381 "Separate specular color",
1382 "glEnable/Disable(GL_STENCIL_TEST)",
1383 "glRenderMode(selection or feedback)",
1384 "glLogicOp()",
1385 "Texture env mode",
1386 "Texture border",
1387 "glColorMask",
1388 "blend mode",
1389 "line stipple"
1390 };
1391
1392
1393 static char *getFallbackString(GLuint bit)
1394 {
1395 int i = 0;
1396 while (bit > 1) {
1397 i++;
1398 bit >>= 1;
1399 }
1400 return fallbackStrings[i];
1401 }
1402
1403
1404 void fxCheckIsInHardware( GLcontext *ctx )
1405 {
1406 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1407 TNLcontext *tnl = TNL_CONTEXT(ctx);
1408 GLuint oldfallback = fxMesa->fallback;
1409 GLuint newfallback = fxMesa->fallback = fx_check_IsInHardware( ctx );
1410
1411 if (newfallback) {
1412 if (oldfallback == 0) {
1413 if (fxMesa->verbose) {
1414 fprintf(stderr, "Voodoo ! enter SW 0x%08x %s\n", newfallback, getFallbackString(newfallback));
1415 }
1416 _swsetup_Wakeup( ctx );
1417 }
1418 }
1419 else {
1420 if (oldfallback) {
1421 _swrast_flush( ctx );
1422 tnl->Driver.Render.Start = fxCheckTexSizes;
1423 tnl->Driver.Render.Finish = fxRenderFinish;
1424 tnl->Driver.Render.PrimitiveNotify = fxRenderPrimitive;
1425 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
1426 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine;
1427 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
1428 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
1429 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1430 tnl->Driver.Render.BuildVertices = fxBuildVertices;
1431 fxChooseVertexState(ctx);
1432 fxDDChooseRenderState(ctx);
1433 if (fxMesa->verbose) {
1434 fprintf(stderr, "Voodoo ! leave SW 0x%08x %s\n", oldfallback, getFallbackString(oldfallback));
1435 }
1436 }
1437 tnl->Driver.Render.Multipass = (HAVE_SPEC && NEED_SECONDARY_COLOR(ctx)) ? fxMultipass_ColorSum : NULL;
1438 }
1439 }
1440
1441 void fxDDInitTriFuncs( GLcontext *ctx )
1442 {
1443 TNLcontext *tnl = TNL_CONTEXT(ctx);
1444 static int firsttime = 1;
1445
1446 if (firsttime) {
1447 init_rast_tab();
1448 firsttime = 0;
1449 }
1450
1451 tnl->Driver.RunPipeline = fxRunPipeline;
1452 tnl->Driver.Render.Start = fxCheckTexSizes;
1453 tnl->Driver.Render.Finish = fxRenderFinish;
1454 tnl->Driver.Render.PrimitiveNotify = fxRenderPrimitive;
1455 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
1456 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine;
1457 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
1458 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
1459 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1460 tnl->Driver.Render.BuildVertices = fxBuildVertices;
1461 tnl->Driver.Render.Multipass = NULL;
1462
1463 (void) fx_print_vertex;
1464 }
1465
1466
1467 /* [dBorca] Hack alert:
1468 * doesn't work with blending.
1469 * need to take care of stencil.
1470 */
1471 GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass)
1472 {
1473 fxMesaContext fxMesa = FX_CONTEXT(ctx);
1474
1475 static int t0 = 0;
1476 static int t1 = 0;
1477
1478 switch (pass) {
1479 case 1: /* first pass: the TEXTURED triangles are drawn */
1480 /* save per-pass data */
1481 fxMesa->restoreUnitsState = fxMesa->unitsState;
1482 /* turn off texturing */
1483 t0 = ctx->Texture.Unit[0]._ReallyEnabled;
1484 t1 = ctx->Texture.Unit[1]._ReallyEnabled;
1485 ctx->Texture.Unit[0]._ReallyEnabled = 0;
1486 ctx->Texture.Unit[1]._ReallyEnabled = 0;
1487 /* SUM the colors */
1488 fxDDBlendEquationSeparate(ctx, GL_FUNC_ADD, GL_FUNC_ADD);
1489 fxDDBlendFuncSeparate(ctx, GL_ONE, GL_ONE, GL_ZERO, GL_ONE);
1490 fxDDEnable(ctx, GL_BLEND, GL_TRUE);
1491 /* make sure we draw only where we want to */
1492 if (ctx->Depth.Mask) {
1493 switch (ctx->Depth.Func) {
1494 case GL_NEVER:
1495 case GL_ALWAYS:
1496 break;
1497 default:
1498 fxDDDepthFunc( ctx, GL_EQUAL );
1499 break;
1500 }
1501 fxDDDepthMask( ctx, GL_FALSE );
1502 }
1503 /* switch to secondary colors */
1504 grVertexLayout(GR_PARAM_PARGB, GR_VERTEX_PSPEC_OFFSET << 2, GR_PARAM_ENABLE);
1505 /* don't advertise new state */
1506 fxMesa->new_state = 0;
1507 break;
1508 case 2: /* 2nd pass (last): the secondary color is summed over texture */
1509 /* restore original state */
1510 fxMesa->unitsState = fxMesa->restoreUnitsState;
1511 /* restore texturing */
1512 ctx->Texture.Unit[0]._ReallyEnabled = t0;
1513 ctx->Texture.Unit[1]._ReallyEnabled = t1;
1514 /* revert to primary colors */
1515 grVertexLayout(GR_PARAM_PARGB, GR_VERTEX_PARGB_OFFSET << 2, GR_PARAM_ENABLE);
1516 break;
1517 default:
1518 assert(0); /* NOTREACHED */
1519 }
1520
1521 /* update HW state */
1522 fxSetupBlend(ctx);
1523 fxSetupDepthTest(ctx);
1524 fxSetupTexture(ctx);
1525
1526 return (pass == 1);
1527 }
1528
1529
1530 #else
1531
1532
1533 /*
1534 * Need this to provide at least one external definition.
1535 */
1536
1537 extern int gl_fx_dummy_function_tris(void);
1538 int
1539 gl_fx_dummy_function_tris(void)
1540 {
1541 return 0;
1542 }
1543
1544 #endif /* FX */