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