summaryrefslogtreecommitdiff
path: root/src/mesa/tnl_dd/t_dd_tritmp.h
blob: 2c36d845ab8875adab65a4cbb79d07f38b1f1a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 * Mesa 3-D graphics library
 * Version:  6.3
 *
 * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Keith Whitwell <keith@tungstengraphics.com>
 */


/* Template for building functions to plug into the driver interface
 * of t_vb_render.c:
 *     ctx->Driver.QuadFunc
 *     ctx->Driver.TriangleFunc
 *     ctx->Driver.LineFunc
 *     ctx->Driver.PointsFunc
 *
 * DO_TWOSIDE:   Plug back-color values from the VB into backfacing triangles,
 *               and restore vertices afterwards.
 * DO_OFFSET:    Calculate offset for triangles and adjust vertices.  Restore
 *               vertices after rendering.
 * DO_FLAT:      For hardware without native flatshading, copy provoking colors
 *               into the other vertices.  Restore after rendering.
 * DO_UNFILLED:  Decompose triangles to lines and points where appropriate.
 * DO_TWOSTENCIL:Gross hack for two-sided stencil.
 *
 * HAVE_SPEC: Vertices have secondary rgba values.
 *
 * VERT_X(v): Alias for vertex x value.
 * VERT_Y(v): Alias for vertex y value.
 * VERT_Z(v): Alias for vertex z value.
 * DEPTH_SCALE: Scale for constant offset.
 * REVERSE_DEPTH: Viewport depth range reversed.
 *
 * VERTEX: Hardware vertex type.
 * GET_VERTEX(n): Retreive vertex with index n.
 * AREA_IS_CCW(a): Return true if triangle with signed area a is ccw.
 *
 * VERT_SET_RGBA: Assign vertex rgba from VB color.
 * VERT_COPY_RGBA: Copy vertex rgba another vertex.
 * VERT_SAVE_RGBA: Save vertex rgba to a local variable.
 * VERT_RESTORE_RGBA: Restore vertex rgba from a local variable.
 *   --> Similar for SPEC.
 *
 * LOCAL_VARS(n): (At least) define local vars for save/restore rgba.
 *
 */

#if HAVE_BACK_COLORS
#define VERT_SET_RGBA( v, c )
#endif

#if !HAVE_SPEC
#define VERT_SET_SPEC( v, c ) (void) c
#define VERT_COPY_SPEC( v0, v1 )
#define VERT_SAVE_SPEC( idx )
#define VERT_RESTORE_SPEC( idx )
#if HAVE_BACK_COLORS
#define VERT_COPY_SPEC1( v )
#endif
#else
#if HAVE_BACK_COLORS
#define VERT_SET_SPEC( v, c )
#endif
#endif

#if !HAVE_BACK_COLORS
#define VERT_COPY_SPEC1( v )
#define VERT_COPY_RGBA1( v )
#endif

#ifndef INSANE_VERTICES
#define VERT_SET_Z(v,val) VERT_Z(v) = val
#define VERT_Z_ADD(v,val) VERT_Z(v) += val
#endif

#ifndef REVERSE_DEPTH
#define REVERSE_DEPTH 0
#endif

/* disable twostencil for un-aware drivers */
#ifndef HAVE_STENCIL_TWOSIDE
#define HAVE_STENCIL_TWOSIDE 0
#endif
#ifndef DO_TWOSTENCIL
#define DO_TWOSTENCIL 0
#endif
#ifndef SETUP_STENCIL
#define SETUP_STENCIL(f)
#endif
#ifndef UNSET_STENCIL
#define UNSET_STENCIL(f)
#endif

#if DO_TRI
static void TAG(triangle)( GLcontext *ctx, GLuint e0, GLuint e1, GLuint e2 )
{
   struct vertex_buffer *VB = &TNL_CONTEXT( ctx )->vb;
   VERTEX *v[3];
   GLfloat offset = 0;
   GLfloat z[3] = { 0 };
   GLenum mode = GL_FILL;
   GLuint facing = 0;
   LOCAL_VARS(3);

/*     fprintf(stderr, "%s\n", __FUNCTION__); */

   v[0] = (VERTEX *)GET_VERTEX(e0);
   v[1] = (VERTEX *)GET_VERTEX(e1);
   v[2] = (VERTEX *)GET_VERTEX(e2);

   if (DO_TWOSIDE || DO_OFFSET || DO_UNFILLED || DO_TWOSTENCIL)
   {
      GLfloat ex = VERT_X(v[0]) - VERT_X(v[2]);
      GLfloat ey = VERT_Y(v[0]) - VERT_Y(v[2]);
      GLfloat fx = VERT_X(v[1]) - VERT_X(v[2]);
      GLfloat fy = VERT_Y(v[1]) - VERT_Y(v[2]);
      GLfloat cc = ex*fy - ey*fx;

      if (DO_TWOSIDE || DO_UNFILLED || DO_TWOSTENCIL)
      {
	 facing = AREA_IS_CCW( cc ) ^ ctx->Polygon._FrontBit;

	 if (DO_UNFILLED) {
	    if (facing) {
	       mode = ctx->Polygon.BackMode;
	       if (ctx->Polygon.CullFlag &&
		   ctx->Polygon.CullFaceMode != GL_FRONT) {
		  return;
	       }
	    } else {
	       mode = ctx->Polygon.FrontMode;
	       if (ctx->Polygon.CullFlag &&
		   ctx->Polygon.CullFaceMode != GL_BACK) {
		  return;
	       }
	    }
	 }

	 if (DO_TWOSIDE && facing == 1) {
	    if (HAVE_BACK_COLORS) {
	       if (!DO_FLAT) {
		  VERT_SAVE_RGBA( 0 );
		  VERT_SAVE_RGBA( 1 );
		  VERT_COPY_RGBA1( v[0] );
		  VERT_COPY_RGBA1( v[1] );
	       }
	       VERT_SAVE_RGBA( 2 );
	       VERT_COPY_RGBA1( v[2] );
	       if (HAVE_SPEC) {
		  if (!DO_FLAT) {
		     VERT_SAVE_SPEC( 0 );
		     VERT_SAVE_SPEC( 1 );
		     VERT_COPY_SPEC1( v[0] );
		     VERT_COPY_SPEC1( v[1] );
		  }
		  VERT_SAVE_SPEC( 2 );
		  VERT_COPY_SPEC1( v[2] );
	       }
	    }
	    else {
	       GLfloat (*vbcolor)[4] = VB->BackfaceColorPtr->data;
	       (void) vbcolor;

	       if (!DO_FLAT) {
		  VERT_SAVE_RGBA( 0 );
		  VERT_SAVE_RGBA( 1 );
	       }
	       VERT_SAVE_RGBA( 2 );

	       if (VB->BackfaceColorPtr->stride) {
		  ASSERT(VB->BackfaceColorPtr->stride == 4*sizeof(GLfloat));

		  if (!DO_FLAT) {
		     VERT_SET_RGBA( v[0], vbcolor[e0] );
		     VERT_SET_RGBA( v[1], vbcolor[e1] );
		  }
		  VERT_SET_RGBA( v[2], vbcolor[e2] );
	       }
	       else {
		  if (!DO_FLAT) {
		     VERT_SET_RGBA( v[0], vbcolor[0] );
		     VERT_SET_RGBA( v[1], vbcolor[0] );
		  }
		  VERT_SET_RGBA( v[2], vbcolor[0] );
	       }

	       if (HAVE_SPEC && VB->BackfaceSecondaryColorPtr) {
		  GLfloat (*vbspec)[4] = VB->BackfaceSecondaryColorPtr->data;
		  ASSERT(VB->BackfaceSecondaryColorPtr->stride == 4*sizeof(GLfloat));

		  if (!DO_FLAT) {
		     VERT_SAVE_SPEC( 0 );
		     VERT_SAVE_SPEC( 1 );
		     VERT_SET_SPEC( v[0], vbspec[e0] );
		     VERT_SET_SPEC( v[1], vbspec[e1] );
		  }
		  VERT_SAVE_SPEC( 2 );
		  VERT_SET_SPEC( v[2], vbspec[e2] );
	       }
	    }
	 }
      }


      if (DO_OFFSET)
      {
	 offset = ctx->Polygon.OffsetUnits * DEPTH_SCALE;
	 z[0] = VERT_Z(v[0]);
	 z[1] = VERT_Z(v[1]);
	 z[2] = VERT_Z(v[2]);
	 if (cc * cc > 1e-16) {
	    GLfloat ic	= 1.0 / cc;
	    GLfloat ez	= z[0] - z[2];
	    GLfloat fz	= z[1] - z[2];
	    GLfloat a	= ey*fz - ez*fy;
	    GLfloat b	= ez*fx - ex*fz;
	    GLfloat ac	= a * ic;
	    GLfloat bc	= b * ic;
	    if ( ac < 0.0f ) ac = -ac;
	    if ( bc < 0.0f ) bc = -bc;
	    offset += MAX2( ac, bc ) * ctx->Polygon.OffsetFactor / ctx->DrawBuffer->_MRD;
	 }
	 offset *= ctx->DrawBuffer->_MRD * (REVERSE_DEPTH ? -1.0 : 1.0);
      }
   }

   if (DO_FLAT) {
      VERT_SAVE_RGBA( 0 );
      VERT_SAVE_RGBA( 1 );
      VERT_COPY_RGBA( v[0], v[2] );
      VERT_COPY_RGBA( v[1], v[2] );
      if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	 VERT_SAVE_SPEC( 0 );
	 VERT_SAVE_SPEC( 1 );
	 VERT_COPY_SPEC( v[0], v[2] );
	 VERT_COPY_SPEC( v[1], v[2] );
      }
   }

   if (mode == GL_POINT) {
      if (DO_OFFSET && ctx->Polygon.OffsetPoint) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
      }
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         UNFILLED_TRI( ctx, GL_POINT, e0, e1, e2 );
         UNSET_STENCIL(facing);
      } else {
         UNFILLED_TRI( ctx, GL_POINT, e0, e1, e2 );
      }
   } else if (mode == GL_LINE) {
      if (DO_OFFSET && ctx->Polygon.OffsetLine) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
      }
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         UNFILLED_TRI( ctx, GL_LINE, e0, e1, e2 );
         UNSET_STENCIL(facing);
      } else {
         UNFILLED_TRI( ctx, GL_LINE, e0, e1, e2 );
      }
   } else {
      if (DO_OFFSET && ctx->Polygon.OffsetFill) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
      }
      if (DO_UNFILLED)
	 RASTERIZE( GL_TRIANGLES );
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         TRI( v[0], v[1], v[2] );
         UNSET_STENCIL(facing);
      } else {
         TRI( v[0], v[1], v[2] );
      }
   }

   if (DO_OFFSET)
   {
      VERT_SET_Z(v[0], z[0]);
      VERT_SET_Z(v[1], z[1]);
      VERT_SET_Z(v[2], z[2]);
   }

   if (DO_TWOSIDE && facing == 1) {
      if (!DO_FLAT) {
	 VERT_RESTORE_RGBA( 0 );
	 VERT_RESTORE_RGBA( 1 );
      }
      VERT_RESTORE_RGBA( 2 );
      if (HAVE_SPEC) {
	 if (!DO_FLAT) {
	    VERT_RESTORE_SPEC( 0 );
	    VERT_RESTORE_SPEC( 1 );
	 }
	 VERT_RESTORE_SPEC( 2 );
      }
   }


   if (DO_FLAT) {
	 VERT_RESTORE_RGBA( 0 );
	 VERT_RESTORE_RGBA( 1 );
	 if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	    VERT_RESTORE_SPEC( 0 );
	    VERT_RESTORE_SPEC( 1 );
	 }
   }
}
#endif

#if DO_QUAD
#if DO_FULL_QUAD
static void TAG(quadr)( GLcontext *ctx,
		       GLuint e0, GLuint e1, GLuint e2, GLuint e3 )
{
   struct vertex_buffer *VB = &TNL_CONTEXT( ctx )->vb;
   VERTEX *v[4];
   GLfloat offset = 0;
   GLfloat z[4] = { 0 };
   GLenum mode = GL_FILL;
   GLuint facing = 0;
   LOCAL_VARS(4);

   v[0] = (VERTEX *)GET_VERTEX(e0);
   v[1] = (VERTEX *)GET_VERTEX(e1);
   v[2] = (VERTEX *)GET_VERTEX(e2);
   v[3] = (VERTEX *)GET_VERTEX(e3);

   if (DO_TWOSIDE || DO_OFFSET || DO_UNFILLED || DO_TWOSTENCIL)
   {
      GLfloat ex = VERT_X(v[2]) - VERT_X(v[0]);
      GLfloat ey = VERT_Y(v[2]) - VERT_Y(v[0]);
      GLfloat fx = VERT_X(v[3]) - VERT_X(v[1]);
      GLfloat fy = VERT_Y(v[3]) - VERT_Y(v[1]);
      GLfloat cc = ex*fy - ey*fx;

      if (DO_TWOSIDE || DO_UNFILLED || DO_TWOSTENCIL)
      {
	 facing = AREA_IS_CCW( cc ) ^ ctx->Polygon._FrontBit;

	 if (DO_UNFILLED) {
	    if (facing) {
	       mode = ctx->Polygon.BackMode;
	       if (ctx->Polygon.CullFlag &&
		   ctx->Polygon.CullFaceMode != GL_FRONT) {
		  return;
	       }
	    } else {
	       mode = ctx->Polygon.FrontMode;
	       if (ctx->Polygon.CullFlag &&
		   ctx->Polygon.CullFaceMode != GL_BACK) {
		  return;
	       }
	    }
	 }

	 if (DO_TWOSIDE && facing == 1) {
	    GLfloat (*vbcolor)[4] = VB->BackfaceColorPtr->data;
	    (void)vbcolor;

	    if (HAVE_BACK_COLORS) {
	       if (!DO_FLAT) {
		  VERT_SAVE_RGBA( 0 );
		  VERT_SAVE_RGBA( 1 );
		  VERT_SAVE_RGBA( 2 );
		  VERT_COPY_RGBA1( v[0] );
		  VERT_COPY_RGBA1( v[1] );
		  VERT_COPY_RGBA1( v[2] );
	       }
	       VERT_SAVE_RGBA( 3 );
	       VERT_COPY_RGBA1( v[3] );
	       if (HAVE_SPEC) {
		  if (!DO_FLAT) {
		     VERT_SAVE_SPEC( 0 );
		     VERT_SAVE_SPEC( 1 );
		     VERT_SAVE_SPEC( 2 );
		     VERT_COPY_SPEC1( v[0] );
		     VERT_COPY_SPEC1( v[1] );
		     VERT_COPY_SPEC1( v[2] );
		  }
		  VERT_SAVE_SPEC( 3 );
		  VERT_COPY_SPEC1( v[3] );
	       }
	    }
	    else {
	       if (!DO_FLAT) {
		  VERT_SAVE_RGBA( 0 );
		  VERT_SAVE_RGBA( 1 );
		  VERT_SAVE_RGBA( 2 );
	       }
	       VERT_SAVE_RGBA( 3 );

	       if (VB->BackfaceColorPtr->stride) {
		  if (!DO_FLAT) {
		     VERT_SET_RGBA( v[0], vbcolor[e0] );
		     VERT_SET_RGBA( v[1], vbcolor[e1] );
		     VERT_SET_RGBA( v[2], vbcolor[e2] );
		  }
		  VERT_SET_RGBA( v[3], vbcolor[e3] );
	       }
	       else {
		  if (!DO_FLAT) {
		     VERT_SET_RGBA( v[0], vbcolor[0] );
		     VERT_SET_RGBA( v[1], vbcolor[0] );
		     VERT_SET_RGBA( v[2], vbcolor[0] );
		  }
		  VERT_SET_RGBA( v[3], vbcolor[0] );
	       }

	       if (HAVE_SPEC && VB->BackfaceSecondaryColorPtr) {
		  GLfloat (*vbspec)[4] = VB->BackfaceSecondaryColorPtr->data;
		  ASSERT(VB->BackfaceSecondaryColorPtr->stride==4*sizeof(GLfloat));

		  if (!DO_FLAT) {
		     VERT_SAVE_SPEC( 0 );
		     VERT_SAVE_SPEC( 1 );
		     VERT_SAVE_SPEC( 2 );
		     VERT_SET_SPEC( v[0], vbspec[e0] );
		     VERT_SET_SPEC( v[1], vbspec[e1] );
		     VERT_SET_SPEC( v[2], vbspec[e2] );
		  }
		  VERT_SAVE_SPEC( 3 );
		  VERT_SET_SPEC( v[3], vbspec[e3] );
	       }
	    }
	 }
      }


      if (DO_OFFSET)
      {
	 offset = ctx->Polygon.OffsetUnits * DEPTH_SCALE;
	 z[0] = VERT_Z(v[0]);
	 z[1] = VERT_Z(v[1]);
	 z[2] = VERT_Z(v[2]);
	 z[3] = VERT_Z(v[3]);
	 if (cc * cc > 1e-16) {
	    GLfloat ez = z[2] - z[0];
	    GLfloat fz = z[3] - z[1];
	    GLfloat a	= ey*fz - ez*fy;
	    GLfloat b	= ez*fx - ex*fz;
	    GLfloat ic	= 1.0 / cc;
	    GLfloat ac	= a * ic;
	    GLfloat bc	= b * ic;
	    if ( ac < 0.0f ) ac = -ac;
	    if ( bc < 0.0f ) bc = -bc;
	    offset += MAX2( ac, bc ) * ctx->Polygon.OffsetFactor / ctx->DrawBuffer->_MRD;
	 }
	 offset *= ctx->DrawBuffer->_MRD * (REVERSE_DEPTH ? -1.0 : 1.0);
      }
   }

   if (DO_FLAT) {
	 VERT_SAVE_RGBA( 0 );
	 VERT_SAVE_RGBA( 1 );
	 VERT_SAVE_RGBA( 2 );
	 VERT_COPY_RGBA( v[0], v[3] );
	 VERT_COPY_RGBA( v[1], v[3] );
	 VERT_COPY_RGBA( v[2], v[3] );
	 if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	    VERT_SAVE_SPEC( 0 );
	    VERT_SAVE_SPEC( 1 );
	    VERT_SAVE_SPEC( 2 );
	    VERT_COPY_SPEC( v[0], v[3] );
	    VERT_COPY_SPEC( v[1], v[3] );
	    VERT_COPY_SPEC( v[2], v[3] );
	 }
   }

   if (mode == GL_POINT) {
      if (( DO_OFFSET) && ctx->Polygon.OffsetPoint) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
	 VERT_Z_ADD(v[3], offset);
      }
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         UNFILLED_QUAD( ctx, GL_POINT, e0, e1, e2, e3 );
         UNSET_STENCIL(facing);
      } else {
         UNFILLED_QUAD( ctx, GL_POINT, e0, e1, e2, e3 );
      }
   } else if (mode == GL_LINE) {
      if (DO_OFFSET && ctx->Polygon.OffsetLine) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
	 VERT_Z_ADD(v[3], offset);
      }
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         UNFILLED_QUAD( ctx, GL_LINE, e0, e1, e2, e3 );
         UNSET_STENCIL(facing);
      } else {
         UNFILLED_QUAD( ctx, GL_LINE, e0, e1, e2, e3 );
      }
   } else {
      if (DO_OFFSET && ctx->Polygon.OffsetFill) {
	 VERT_Z_ADD(v[0], offset);
	 VERT_Z_ADD(v[1], offset);
	 VERT_Z_ADD(v[2], offset);
	 VERT_Z_ADD(v[3], offset);
      }
      RASTERIZE( GL_QUADS );
      if (DO_TWOSTENCIL && !HAVE_STENCIL_TWOSIDE && ctx->Stencil.TestTwoSide) {
         SETUP_STENCIL(facing);
         QUAD( (v[0]), (v[1]), (v[2]), (v[3]) );
         UNSET_STENCIL(facing);
      } else {
         QUAD( (v[0]), (v[1]), (v[2]), (v[3]) );
      }
   }

   if (DO_OFFSET)
   {
      VERT_SET_Z(v[0], z[0]);
      VERT_SET_Z(v[1], z[1]);
      VERT_SET_Z(v[2], z[2]);
      VERT_SET_Z(v[3], z[3]);
   }

   if (DO_TWOSIDE && facing == 1) {
      if (!DO_FLAT) {
	 VERT_RESTORE_RGBA( 0 );
	 VERT_RESTORE_RGBA( 1 );
	 VERT_RESTORE_RGBA( 2 );
      }
      VERT_RESTORE_RGBA( 3 );
      if (HAVE_SPEC) {
	 if (!DO_FLAT) {
	    VERT_RESTORE_SPEC( 0 );
	    VERT_RESTORE_SPEC( 1 );
	    VERT_RESTORE_SPEC( 2 );
	 }
	 VERT_RESTORE_SPEC( 3 );
      }
   }


   if (DO_FLAT) {
	 VERT_RESTORE_RGBA( 0 );
	 VERT_RESTORE_RGBA( 1 );
	 VERT_RESTORE_RGBA( 2 );
	 if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	    VERT_RESTORE_SPEC( 0 );
	    VERT_RESTORE_SPEC( 1 );
	    VERT_RESTORE_SPEC( 2 );
	 }
   }
}
#else
static void TAG(quadr)( GLcontext *ctx, GLuint e0,
		       GLuint e1, GLuint e2, GLuint e3 )
{
   if (DO_UNFILLED) {
      struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
      GLubyte ef1 = VB->EdgeFlag[e1];
      GLubyte ef3 = VB->EdgeFlag[e3];
      VB->EdgeFlag[e1] = 0;
      TAG(triangle)( ctx, e0, e1, e3 );
      VB->EdgeFlag[e1] = ef1;
      VB->EdgeFlag[e3] = 0;
      TAG(triangle)( ctx, e1, e2, e3 );
      VB->EdgeFlag[e3] = ef3;
   } else {
      TAG(triangle)( ctx, e0, e1, e3 );
      TAG(triangle)( ctx, e1, e2, e3 );
   }
}
#endif
#endif

#if DO_LINE
static void TAG(line)( GLcontext *ctx, GLuint e0, GLuint e1 )
{
   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
   VERTEX *v[2];
   LOCAL_VARS(2);

   v[0] = (VERTEX *)GET_VERTEX(e0);
   v[1] = (VERTEX *)GET_VERTEX(e1);

   if (DO_FLAT) {
	 VERT_SAVE_RGBA( 0 );
	 VERT_COPY_RGBA( v[0], v[1] );
	 if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	    VERT_SAVE_SPEC( 0 );
	    VERT_COPY_SPEC( v[0], v[1] );
	 }
   }

   LINE( v[0], v[1] );

   if (DO_FLAT) {
	 VERT_RESTORE_RGBA( 0 );

	 if (HAVE_SPEC && VB->AttribPtr[_TNL_ATTRIB_COLOR1]) {
	    VERT_RESTORE_SPEC( 0 );
	 }
   }
}
#endif

#if DO_POINTS
static void TAG(points)( GLcontext *ctx, GLuint first, GLuint last )
{
   struct vertex_buffer *VB = &TNL_CONTEXT( ctx )->vb;
   GLuint i;
   LOCAL_VARS(1);

   if (VB->Elts == 0) {
      for ( i = first ; i < last ; i++ ) {
	 if ( VB->ClipMask[i] == 0 ) {
	    VERTEX *v = (VERTEX *)GET_VERTEX(i);
	    POINT( v );
	 }
      }
   } else {
      for ( i = first ; i < last ; i++ ) {
	 GLuint e = VB->Elts[i];
	 if ( VB->ClipMask[e] == 0 ) {
	    VERTEX *v = (VERTEX *)GET_VERTEX(e);
	    POINT( v );
	 }
      }
   }
}
#endif

static void TAG(init)( void )
{
#if DO_QUAD
   TAB[IND].quad = TAG(quadr);
#endif
#if DO_TRI
   TAB[IND].triangle = TAG(triangle);
#endif
#if DO_LINE
   TAB[IND].line = TAG(line);
#endif
#if DO_POINTS
   TAB[IND].points = TAG(points);
#endif
}

#undef IND
#undef TAG

#if HAVE_BACK_COLORS
#undef VERT_SET_RGBA
#endif

#if !HAVE_SPEC
#undef VERT_SET_SPEC
#undef VERT_COPY_SPEC
#undef VERT_SAVE_SPEC
#undef VERT_RESTORE_SPEC
#if HAVE_BACK_COLORS
#undef VERT_COPY_SPEC1
#endif
#else
#if HAVE_BACK_COLORS
#undef VERT_SET_SPEC
#endif
#endif

#if !HAVE_BACK_COLORS
#undef VERT_COPY_SPEC1
#undef VERT_COPY_RGBA1
#endif

#ifndef INSANE_VERTICES
#undef VERT_SET_Z
#undef VERT_Z_ADD
#endif