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