mesa/st: introduce PIPE_CAP_NO_CLIP_ON_COPY_TEX
[mesa.git] / src / mesa / tnl / t_vb_render.c
index 02d97f46d2d646345df33cdb2f36078ee390db81..02f521c7fb38f228767ced9c0c828c6dd1bd80f4 100644 (file)
@@ -1,10 +1,7 @@
-/* $Id: t_vb_render.c,v 1.10 2001/01/16 05:29:43 keithw Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
  *
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
+ * 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"),
  * 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.
+ * THE AUTHORS OR COPYRIGHT HOLDERS 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.
  *
- * Author:
- *    Keith Whitwell <keithw@valinux.com>
+ * Authors:
+ *    Keith Whitwell <keithw@vmware.com>
  */
 
 
  * This file makes calls to project vertices and to the point, line
  * and triangle rasterizers via the function pointers:
  *
- *    context->Driver.BuildProjectedVertices()
- *
- *    context->Driver.PointsFunc()
- *    context->Driver.LineFunc()
- *    context->Driver.TriangleFunc()
- *    context->Driver.QuadFunc() 
- *
- *    context->Driver.RenderTabVerts[] 
- *    context->Driver.RenderTabElts[]
+ *    context->Driver.Render.*
  *
- * None of these may be null.
  */
 
 
-#include "glheader.h"
-#include "context.h"
-#include "colormac.h"
-#include "macros.h"
-#include "mem.h"
-#include "mtypes.h"
-#include "mmath.h"
+#include <stdio.h>
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/enums.h"
+#include "main/macros.h"
 
-#include "math/m_matrix.h"
+#include "main/mtypes.h"
 #include "math/m_xform.h"
+#include "util/bitscan.h"
 
 #include "t_pipeline.h"
 
 
 
-typedef void (*clip_line_func)( GLcontext *ctx,
-                               GLuint i, GLuint j,
-                               GLubyte mask);
-
-typedef void (*clip_poly_func)( GLcontext *ctx,
-                               GLuint n, GLuint vlist[],
-                               GLubyte mask );
-
-
-
-
-struct render_stage_data {
-
-   /* Clipping functions for current state.
-    */
-   interp_func interp;         /* Clip interpolation function */
-   copy_pv_func copypv;                /* Flatshade fixup function */
-   GLuint _ClipInputs;         /* Inputs referenced by interpfunc */
-};
-
-#define RENDER_STAGE_DATA(stage) ((struct render_stage_data *)stage->private)
-
-                                 
-/**********************************************************************/
-/*           Interpolate between pairs of vertices                    */
-/**********************************************************************/
-
-
-#define LINTERP_SZ( t, vec, to, a, b, sz )                     \
-do {                                                           \
-   switch (sz) {                                               \
-   case 4: vec[to][3] = LINTERP( t, vec[a][3], vec[b][3] );    \
-   case 3: vec[to][2] = LINTERP( t, vec[a][2], vec[b][2] );    \
-   case 2: vec[to][1] = LINTERP( t, vec[a][1], vec[b][1] );    \
-   case 1: vec[to][0] = LINTERP( t, vec[a][0], vec[b][0] );    \
-   }                                                           \
-} while(0)
-
-
-#if 1
-
-#define LINTERP_RGBA(nr, t, out, a, b) {       \
-   int i;                                      \
-   for (i = 0; i < nr; i++) {                  \
-      GLfloat fa = CHAN_TO_FLOAT(a[i]);                \
-      GLfloat fb = CHAN_TO_FLOAT(b[i]);                \
-      GLfloat fo = LINTERP(t, fa, fb);         \
-      CLAMPED_FLOAT_TO_CHAN(out[i], fo);       \
-   }                                           \
-}
-
-#else
-
-#define LINTERP_RGBA(nr, t, out, a, b) {                       \
-   int n;                                                      \
-   const GLuint ti = FloatToInt(t*256.0F);                     \
-   const GLubyte *Ib = (const GLubyte *)&a[0];                 \
-   const GLubyte *Jb = (const GLubyte *)&b[0];                 \
-   GLubyte *Ob = (GLubyte *)&out[0];                           \
-                                                               \
-   for (n = 0 ; n < nr ; n++)                                  \
-      Ob[n] = (GLubyte) (Ib[n] + ((ti * (Jb[n] - Ib[n]))/256));        \
-}
-
-#endif
-
-
-
-
-#define INTERP_RGBA    0x1
-#define INTERP_TEX     0x2
-#define INTERP_INDEX   0x4
-#define INTERP_SPEC    0x8
-#define INTERP_FOG     0x10
-#define INTERP_EDGE    0x20
-#define MAX_INTERP     0x40
-
-static interp_func interp_tab[MAX_INTERP];
-static copy_pv_func copy_tab[MAX_INTERP];
-
-
-#define IND (0)
-#define NAME interp_none
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG)
-#define NAME interp_FOG
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_TEX)
-#define NAME interp_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_TEX)
-#define NAME interp_FOG_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_EDGE)
-#define NAME interp_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_EDGE)
-#define NAME interp_FOG_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_TEX|INTERP_EDGE)
-#define NAME interp_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_TEX|INTERP_EDGE)
-#define NAME interp_FOG_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA)
-#define NAME interp_RGBA
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC)
-#define NAME interp_RGBA_SPEC
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_FOG)
-#define NAME interp_RGBA_FOG
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_FOG)
-#define NAME interp_RGBA_SPEC_FOG
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_TEX)
-#define NAME interp_RGBA_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_TEX)
-#define NAME interp_RGBA_SPEC_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_FOG|INTERP_TEX)
-#define NAME interp_RGBA_FOG_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_TEX)
-#define NAME interp_RGBA_SPEC_FOG_TEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_INDEX)
-#define NAME interp_INDEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_INDEX)
-#define NAME interp_FOG_INDEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_TEX|INTERP_INDEX)
-#define NAME interp_TEX_INDEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_TEX|INTERP_INDEX)
-#define NAME interp_FOG_TEX_INDEX
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_EDGE)
-#define NAME interp_RGBA_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_EDGE)
-#define NAME interp_RGBA_SPEC_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_FOG|INTERP_EDGE)
-#define NAME interp_RGBA_FOG_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_EDGE)
-#define NAME interp_RGBA_SPEC_FOG_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_TEX|INTERP_EDGE)
-#define NAME interp_RGBA_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_TEX|INTERP_EDGE)
-#define NAME interp_RGBA_SPEC_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_FOG|INTERP_TEX|INTERP_EDGE)
-#define NAME interp_RGBA_FOG_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_TEX|INTERP_EDGE)
-#define NAME interp_RGBA_SPEC_FOG_TEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_INDEX|INTERP_EDGE)
-#define NAME interp_INDEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_INDEX|INTERP_EDGE)
-#define NAME interp_FOG_INDEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_TEX|INTERP_INDEX|INTERP_EDGE)
-#define NAME interp_TEX_INDEX_EDGE
-#include "t_vb_interptmp.h"
-
-#define IND (INTERP_FOG|INTERP_TEX|INTERP_INDEX|INTERP_EDGE)
-#define NAME interp_FOG_TEX_INDEX_EDGE
-#include "t_vb_interptmp.h"
-
-
-#define IND (INTERP_RGBA)
-#define NAME copy_RGBA
-#include "t_vb_flattmp.h"
-
-#define IND (INTERP_RGBA|INTERP_SPEC)
-#define NAME copy_RGBA_SPEC
-#include "t_vb_flattmp.h"
-
-#define IND (INTERP_INDEX)
-#define NAME copy_INDEX
-#include "t_vb_flattmp.h"
-
-
-
-
-static void interp_invalid( GLcontext *ctx,
-                           GLfloat t, 
-                           GLuint dst, GLuint in, GLuint out,
-                           GLboolean boundary )
-{
-   (void)(ctx && t && in && out && boundary);
-   fprintf(stderr, "Invalid interpolation function in t_vbrender.c\n");
-}
-
-static void copy_invalid( GLcontext *ctx, GLuint dst, GLuint src )
-{
-   (void)(ctx && dst && src);
-   fprintf(stderr, "Invalid copy function in t_vbrender.c\n");
-}
-
-
-static void interp_init( void )
-{
-   GLuint i;
-
-   /* Use the maximal function as the default.  I don't believe any of
-    * the non-implemented combinations are reachable, but this gives
-    * some safety from crashes.
-    */
-   for (i = 0 ; i < Elements(interp_tab) ; i++) {
-      interp_tab[i] = interp_invalid;
-      copy_tab[i] = copy_invalid;
-   }
-
-   interp_tab[0] = interp_none;
-   interp_tab[INTERP_FOG] = interp_FOG;
-   interp_tab[INTERP_TEX] = interp_TEX;
-   interp_tab[INTERP_FOG|INTERP_TEX] = interp_FOG_TEX;
-   interp_tab[INTERP_EDGE] = interp_EDGE;
-   interp_tab[INTERP_FOG|INTERP_EDGE] = interp_FOG_EDGE;
-   interp_tab[INTERP_TEX|INTERP_EDGE] = interp_TEX_EDGE;
-   interp_tab[INTERP_FOG|INTERP_TEX|INTERP_EDGE] = interp_FOG_TEX_EDGE;
-
-   interp_tab[INTERP_RGBA] = interp_RGBA;
-   interp_tab[INTERP_RGBA|INTERP_SPEC] = interp_RGBA_SPEC;
-   interp_tab[INTERP_RGBA|INTERP_FOG] = interp_RGBA_FOG;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_FOG] = interp_RGBA_SPEC_FOG;
-   interp_tab[INTERP_RGBA|INTERP_TEX] = interp_RGBA_TEX;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_TEX] = interp_RGBA_SPEC_TEX;
-   interp_tab[INTERP_RGBA|INTERP_FOG|INTERP_TEX] = interp_RGBA_FOG_TEX;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_TEX] = 
-      interp_RGBA_SPEC_FOG_TEX;
-   interp_tab[INTERP_INDEX] = interp_INDEX;
-   interp_tab[INTERP_FOG|INTERP_INDEX] = interp_FOG_INDEX;
-   interp_tab[INTERP_TEX|INTERP_INDEX] = interp_TEX_INDEX;
-   interp_tab[INTERP_FOG|INTERP_TEX|INTERP_INDEX] = interp_FOG_TEX_INDEX;
-   interp_tab[INTERP_RGBA|INTERP_EDGE] = interp_RGBA_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_EDGE] = interp_RGBA_SPEC_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_FOG|INTERP_EDGE] = interp_RGBA_FOG_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_EDGE] = 
-      interp_RGBA_SPEC_FOG_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_TEX|INTERP_EDGE] = interp_RGBA_TEX_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_TEX|INTERP_EDGE] = 
-      interp_RGBA_SPEC_TEX_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_FOG|INTERP_TEX|INTERP_EDGE] = 
-      interp_RGBA_FOG_TEX_EDGE;
-   interp_tab[INTERP_RGBA|INTERP_SPEC|INTERP_FOG|INTERP_TEX|INTERP_EDGE] = 
-      interp_RGBA_SPEC_FOG_TEX_EDGE;
-   interp_tab[INTERP_INDEX|INTERP_EDGE] = interp_INDEX_EDGE;
-   interp_tab[INTERP_FOG|INTERP_INDEX|INTERP_EDGE] = interp_FOG_INDEX_EDGE;
-   interp_tab[INTERP_TEX|INTERP_INDEX|INTERP_EDGE] = interp_TEX_INDEX_EDGE;
-   interp_tab[INTERP_FOG|INTERP_TEX|INTERP_INDEX|INTERP_EDGE] = 
-      interp_FOG_TEX_INDEX_EDGE;
-
-
-   copy_tab[INTERP_RGBA] = copy_RGBA;
-   copy_tab[INTERP_RGBA|INTERP_SPEC] = copy_RGBA_SPEC;
-   copy_tab[INTERP_INDEX] = copy_INDEX;
-
-}
-
-
 /**********************************************************************/
 /*                        Clip single primitives                      */
 /**********************************************************************/
 
 
-#if defined(USE_IEEE) 
-#define NEGATIVE(x) ((*(GLuint *)&x) & (1<<31))
-#define DIFFERENT_SIGNS(x,y) (((*(GLuint *)&x)^(*(GLuint *)&y)) & (1<<31))
-#else
-#define NEGATIVE(x) (x < 0)
-#define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0)
-/* Could just use (x*y<0) except for the flatshading requirements.
- * Maybe there's a better way?
- */
-#endif
-
 #define W(i) coord[i][3]
 #define Z(i) coord[i][2]
 #define Y(i) coord[i][1]
@@ -390,52 +65,25 @@ static void interp_init( void )
 #define TAG(x) x##_4
 #include "t_vb_cliptmp.h"
 
-#define W(i) 1.0
-#define Z(i) coord[i][2]
-#define Y(i) coord[i][1]
-#define X(i) coord[i][0]
-#define SIZE 3
-#define TAG(x) x##_3
-#include "t_vb_cliptmp.h"
-
-#define W(i) 1.0
-#define Z(i) 0.0
-#define Y(i) coord[i][1]
-#define X(i) coord[i][0]
-#define SIZE 2
-#define TAG(x) x##_2
-#include "t_vb_cliptmp.h"
-
-static clip_poly_func clip_poly_tab[5] = {
-   0,
-   0,
-   clip_polygon_2,
-   clip_polygon_3,
-   clip_polygon_4
-};
-
-static clip_line_func clip_line_tab[5] = {
-   0,
-   0,
-   clip_line_2,
-   clip_line_3,
-   clip_line_4
-};
 
 
 /**********************************************************************/
 /*              Clip and render whole begin/end objects               */
 /**********************************************************************/
 
-#define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
+#define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
 
 
+/* This does NOT include the CLIP_USER_BIT! */
+#define CLIPMASK (CLIP_FRUSTUM_BITS | CLIP_CULL_BIT)
+
+
 /* Vertices, with the possibility of clipping.
  */
 #define RENDER_POINTS( start, count ) \
-   ctx->Driver.PointsFunc( ctx, start, count )
+   tnl->Driver.Render.Points( ctx, start, count )
 
 #define RENDER_LINE( v1, v2 )                  \
 do {                                           \
@@ -443,8 +91,8 @@ do {                                         \
    GLubyte ormask = c1|c2;                     \
    if (!ormask)                                        \
       LineFunc( ctx, v1, v2 );                 \
-   else if (!(c1 & c2 & 0x3f))                 \
-      clip_line_tab[sz]( ctx, v1, v2, ormask );        \
+   else if (!(c1 & c2 & CLIPMASK))                     \
+      clip_line_4( ctx, v1, v2, ormask );      \
 } while (0)
 
 #define RENDER_TRI( v1, v2, v3 )                       \
@@ -453,11 +101,8 @@ do {                                                       \
    GLubyte ormask = c1|c2|c3;                          \
    if (!ormask)                                                \
       TriangleFunc( ctx, v1, v2, v3 );                 \
-   else if (!(c1 & c2 & c3 & 0x3f)) {                  \
-      GLuint vlist[MAX_CLIPPED_VERTICES];              \
-      ASSIGN_3V(vlist, v3, v1, v2 );                   \
-      clip_poly_tab[sz]( ctx, 3, vlist, ormask );      \
-   }                                                   \
+   else if (!(c1 & c2 & c3 & CLIPMASK))                        \
+      clip_tri_4( ctx, v1, v2, v3, ormask );           \
 } while (0)
 
 #define RENDER_QUAD( v1, v2, v3, v4 )                  \
@@ -467,29 +112,27 @@ do {                                                      \
    GLubyte ormask = c1|c2|c3|c4;                       \
    if (!ormask)                                                \
       QuadFunc( ctx, v1, v2, v3, v4 );                 \
-   else if (!(c1 & c2 & c3 & c4 & 0x3f)) {             \
-      GLuint vlist[MAX_CLIPPED_VERTICES];              \
-      ASSIGN_4V(vlist, v4, v1, v2, v3 );               \
-      clip_poly_tab[sz]( ctx, 4, vlist, ormask );      \
-   }                                                   \
+   else if (!(c1 & c2 & c3 & c4 & CLIPMASK))           \
+      clip_quad_4( ctx, v1, v2, v3, v4, ormask );      \
 } while (0)
 
 
-#define LOCAL_VARS                                                     \
-    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;                  \
-    const GLuint * const elt = VB->Elts;                               \
-    const GLubyte *mask = VB->ClipMask;                                        \
-    const GLuint sz = VB->ClipPtr->size;                               \
-    const line_func LineFunc = ctx->Driver.LineFunc;                   \
-    const triangle_func TriangleFunc = ctx->Driver.TriangleFunc;       \
-    const quad_func QuadFunc = ctx->Driver.QuadFunc;                   \
-    (void) (LineFunc && TriangleFunc && QuadFunc);                     \
-    (void) elt; (void) mask; (void) sz;
+#define LOCAL_VARS                                             \
+   TNLcontext *tnl = TNL_CONTEXT(ctx);                         \
+   struct vertex_buffer *VB = &tnl->vb;                                \
+   const GLuint * const elt = VB->Elts;                                \
+   const GLubyte *mask = VB->ClipMask;                         \
+   const GLuint sz = VB->ClipPtr->size;                                \
+   const tnl_line_func LineFunc = tnl->Driver.Render.Line;             \
+   const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
+   const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad;             \
+   const GLboolean stipple = ctx->Line.StippleFlag;            \
+   (void) (LineFunc && TriangleFunc && QuadFunc);              \
+   (void) elt; (void) mask; (void) sz; (void) stipple;
 
 #define TAG(x) clip_##x##_verts
-#define INIT(x) ctx->Driver.RenderPrimitive( ctx, x )
-#define RESET_STIPPLE ctx->Driver.ResetLineStipple( ctx )
-#define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE;
+#define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
+#define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
 #define PRESERVE_VB_DEFS
 #include "t_vb_rendertmp.h"
 
@@ -503,12 +146,47 @@ do {                                                      \
 #define TAG(x) clip_##x##_elts
 #include "t_vb_rendertmp.h"
 
+/* TODO: do this for all primitives, verts and elts:
+ */
+static void clip_elt_triangles( struct gl_context *ctx,
+                               GLuint start,
+                               GLuint count,
+                               GLuint flags )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   tnl_render_func render_tris = tnl->Driver.Render.PrimTabElts[GL_TRIANGLES];
+   struct vertex_buffer *VB = &tnl->vb;
+   const GLuint * const elt = VB->Elts;
+   GLubyte *mask = VB->ClipMask;
+   GLuint last = count-2;
+   GLuint j;
+   (void) flags;
+
+   tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
+
+   for (j=start; j < last; j+=3 ) {
+      GLubyte c1 = mask[elt[j]];
+      GLubyte c2 = mask[elt[j+1]];
+      GLubyte c3 = mask[elt[j+2]];
+      GLubyte ormask = c1|c2|c3;
+      if (ormask) {
+        if (start < j)
+           render_tris( ctx, start, j, 0 );
+        if (!(c1&c2&c3&CLIPMASK))
+           clip_tri_4( ctx, elt[j], elt[j+1], elt[j+2], ormask );
+        start = j+3;
+      }
+   }
+
+   if (start < j)
+      render_tris( ctx, start, j, 0 );
+}
 
 /**********************************************************************/
 /*                  Render whole begin/end objects                    */
 /**********************************************************************/
 
-#define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
+#define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
 
@@ -516,32 +194,33 @@ do {                                                      \
 /* Vertices, no clipping.
  */
 #define RENDER_POINTS( start, count ) \
-   ctx->Driver.PointsFunc( ctx, start, count )
+   tnl->Driver.Render.Points( ctx, start, count )
 
 #define RENDER_LINE( v1, v2 ) \
    LineFunc( ctx, v1, v2 )
 
-#define RENDER_TRI( v1, v2, v3 )               \
+#define RENDER_TRI( v1, v2, v3 ) \
    TriangleFunc( ctx, v1, v2, v3 )
 
-#define RENDER_QUAD( v1, v2, v3, v4 )  \
+#define RENDER_QUAD( v1, v2, v3, v4 ) \
    QuadFunc( ctx, v1, v2, v3, v4 )
 
 #define TAG(x) _tnl_##x##_verts
 
-#define LOCAL_VARS                                                     \
-    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;                  \
-    const GLuint * const elt = VB->Elts;                               \
-    const line_func LineFunc = ctx->Driver.LineFunc;                   \
-    const triangle_func TriangleFunc = ctx->Driver.TriangleFunc;       \
-    const quad_func QuadFunc = ctx->Driver.QuadFunc;                   \
-    (void) (LineFunc && TriangleFunc && QuadFunc);                     \
-    (void) elt;
-
-#define RESET_STIPPLE ctx->Driver.ResetLineStipple( ctx )
-#define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE;
-#define INIT(x) ctx->Driver.RenderPrimitive( ctx, x )
-#define RENDER_TAB_QUALIFIER 
+#define LOCAL_VARS                                             \
+   TNLcontext *tnl = TNL_CONTEXT(ctx);                         \
+   struct vertex_buffer *VB = &tnl->vb;                                \
+   const GLuint * const elt = VB->Elts;                                \
+   const tnl_line_func LineFunc = tnl->Driver.Render.Line;             \
+   const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
+   const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad;             \
+   const GLboolean stipple = ctx->Line.StippleFlag;            \
+   (void) (LineFunc && TriangleFunc && QuadFunc);              \
+   (void) elt; (void) stipple
+
+#define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
+#define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
+#define RENDER_TAB_QUALIFIER
 #define PRESERVE_VB_DEFS
 #include "t_vb_rendertmp.h"
 
@@ -554,6 +233,27 @@ do {                                                       \
 #include "t_vb_rendertmp.h"
 
 
+/**********************************************************************/
+/*              Helper functions for drivers                  */
+/**********************************************************************/
+
+void _tnl_RenderClippedPolygon( struct gl_context *ctx, const GLuint *elts, GLuint n )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   struct vertex_buffer *VB = &tnl->vb;
+   GLuint *tmp = VB->Elts;
+
+   VB->Elts = (GLuint *)elts;
+   tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
+   VB->Elts = tmp;
+}
+
+void _tnl_RenderClippedLine( struct gl_context *ctx, GLuint ii, GLuint jj )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   tnl->Driver.Render.Line( ctx, ii, jj );
+}
+
 
 
 /**********************************************************************/
@@ -561,57 +261,72 @@ do {                                                      \
 /**********************************************************************/
 
 
-static GLboolean run_render( GLcontext *ctx,
-                            struct gl_pipeline_stage *stage )
+static GLboolean run_render( struct gl_context *ctx,
+                            struct tnl_pipeline_stage *stage )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
-   struct vertex_buffer *VB = &tnl->vb; 
-   GLuint new_inputs = stage->changed_inputs;
-   render_func *tab;
+   struct vertex_buffer *VB = &tnl->vb;
+   tnl_render_func *tab;
    GLint pass = 0;
 
-   VB->interpfunc = RENDER_STAGE_DATA(stage)->interp;
-   VB->copypvfunc = RENDER_STAGE_DATA(stage)->copypv;
-
    /* Allow the drivers to lock before projected verts are built so
     * that window coordinates are guarenteed not to change before
     * rendering.
     */
-   ctx->Driver.RenderStart( ctx );
-   
+   assert(tnl->Driver.Render.Start);
+
+   tnl->Driver.Render.Start( ctx );
+
+   assert(tnl->Driver.Render.BuildVertices);
+   assert(tnl->Driver.Render.PrimitiveNotify);
+   assert(tnl->Driver.Render.Points);
+   assert(tnl->Driver.Render.Line);
+   assert(tnl->Driver.Render.Triangle);
+   assert(tnl->Driver.Render.Quad);
+   assert(tnl->Driver.Render.ResetLineStipple);
+   assert(tnl->Driver.Render.Interp);
+   assert(tnl->Driver.Render.CopyPV);
+   assert(tnl->Driver.Render.ClippedLine);
+   assert(tnl->Driver.Render.ClippedPolygon);
+   assert(tnl->Driver.Render.Finish);
+
+   tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, ~0 );
+
    if (VB->ClipOrMask) {
       tab = VB->Elts ? clip_render_tab_elts : clip_render_tab_verts;
-
-      if (new_inputs & VB->importable_data) 
-        VB->import_data( ctx,
-                         new_inputs & VB->importable_data,
-                         VEC_NOT_WRITEABLE|VEC_BAD_STRIDE);
+      clip_render_tab_elts[GL_TRIANGLES] = clip_elt_triangles;
    }
    else {
-      tab = VB->Elts ? ctx->Driver.RenderTabElts : ctx->Driver.RenderTabVerts;
-   } 
-
-   ctx->Driver.BuildProjectedVertices( ctx, 0, VB->Count, new_inputs );
+      tab = (VB->Elts ?
+            tnl->Driver.Render.PrimTabElts :
+            tnl->Driver.Render.PrimTabVerts);
+   }
 
    do
    {
-      GLuint i, length, flags = 0;
-      for (i = 0 ; !(flags & PRIM_LAST) ; i += length)
+      GLuint i;
+
+      for (i = 0 ; i < VB->PrimitiveCount ; i++)
       {
-        flags = VB->Primitive[i];
-        length= VB->PrimitiveLength[i];        
-        ASSERT(length || (flags & PRIM_LAST));
-        ASSERT((flags & PRIM_MODE_MASK) <= GL_POLYGON+1);
-/*      fprintf(stderr, "Render %s %d..%d\n", */
-/*              _mesa_prim_name[flags&PRIM_MODE_MASK], i, i+length); */
+        GLuint prim = _tnl_translate_prim(&VB->Primitive[i]);
+        GLuint start = VB->Primitive[i].start;
+        GLuint length = VB->Primitive[i].count;
+
+        assert((prim & PRIM_MODE_MASK) <= GL_POLYGON);
+
+        if (MESA_VERBOSE & VERBOSE_PRIMS)
+           _mesa_debug(NULL, "MESA prim %s %d..%d\n",
+                       _mesa_enum_to_string(prim & PRIM_MODE_MASK),
+                       start, start+length);
+
         if (length)
-           tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
+           tab[prim & PRIM_MODE_MASK]( ctx, start, start + length, prim );
       }
-   } while (ctx->Driver.MultipassFunc &&
-           ctx->Driver.MultipassFunc( ctx, ++pass ));
+   } while (tnl->Driver.Render.Multipass &&
+           tnl->Driver.Render.Multipass( ctx, ++pass ));
 
+   tnl->Driver.Render.Finish( ctx );
 
-   ctx->Driver.RenderFinish( ctx );
    return GL_FALSE;            /* finished the pipe */
 }
 
@@ -622,127 +337,14 @@ static GLboolean run_render( GLcontext *ctx,
 
 
 
-/* Quite a bit of work involved in finding out the inputs for the
- * render stage.  This function also identifies which vertex
- * interpolation function to use, as these are essentially the same
- * question.
- */
-static void check_render( GLcontext *ctx, struct gl_pipeline_stage *stage )
-{
-   struct render_stage_data *store = RENDER_STAGE_DATA(stage);
-   GLuint interp = 0;
-   GLuint copy = 0;
-   GLuint inputs = VERT_CLIP;
-   GLuint i;
-
-   if (ctx->Visual.RGBAflag)
-   {
-      interp |= INTERP_RGBA;
-      inputs |= VERT_RGBA;
-
-      if (ctx->_TriangleCaps & DD_SEPERATE_SPECULAR) {
-         interp |= INTERP_SPEC;
-        inputs |= VERT_SPEC_RGB;
-      }
-
-      if (ctx->Texture._ReallyEnabled) {
-        interp |= INTERP_TEX;
-
-        for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
-           if (ctx->Texture.Unit[i]._ReallyEnabled)
-              inputs |= VERT_TEX(i);
-        }
-      }
-   }
-   else 
-   {
-      interp |= INTERP_INDEX;
-      inputs |= VERT_INDEX;
-   }
 
-   if (ctx->Point._Attenuated) 
-      inputs |= VERT_POINT_SIZE;
 
-   /* How do drivers turn this off?
-    */
-   if (ctx->Fog.Enabled) {
-      interp |= INTERP_FOG;
-      inputs |= VERT_FOG_COORD;
-   }
-
-   if (ctx->_TriangleCaps & DD_TRI_UNFILLED) {
-      inputs |= VERT_EDGE;
-      interp |= INTERP_EDGE;
-   }
-
-   if (ctx->RenderMode==GL_FEEDBACK) {
-      interp |= INTERP_TEX;
-      inputs |= VERT_TEX_ANY;
-   }
-
-   if (ctx->_TriangleCaps & DD_FLATSHADE) {
-      copy = interp & (INTERP_RGBA|INTERP_SPEC|INTERP_INDEX);
-      interp &= ~copy;
-   }
-
-   store->copypv = copy_tab[copy];
-   store->interp = interp_tab[interp];
-   stage->inputs = inputs;
-}
-
-
-/* Called the first time stage->check() is invoked.
- */
-static void alloc_render_data( GLcontext *ctx, 
-                                struct gl_pipeline_stage *stage )
+const struct tnl_pipeline_stage _tnl_render_stage =
 {
-   struct render_stage_data *store;
-   static GLboolean first_time = 1;
-
-   if (first_time) {
-      interp_init();
-      first_time = 0;
-   }
-
-   stage->private = MALLOC(sizeof(*store));
-   if (!stage->private)
-      return;
-
-   /* Now do the check.
-    */
-   stage->check = check_render;
-   stage->check( ctx, stage );
-}
-
-
-
-static void dtr( struct gl_pipeline_stage *stage )
-{
-   struct render_stage_data *store = RENDER_STAGE_DATA(stage);
-   if (store) {
-      FREE( store );
-      stage->private = 0;
-   }
-}
-
-
-const struct gl_pipeline_stage _tnl_render_stage = 
-{ 
-   "render",
-   (_NEW_BUFFERS |
-    _DD_NEW_SEPERATE_SPECULAR |
-    _DD_NEW_FLATSHADE |
-    _NEW_TEXTURE|
-    _NEW_LIGHT|
-    _NEW_POINT|
-    _NEW_FOG|
-    _DD_NEW_TRI_UNFILLED |
-    _NEW_RENDERMODE),          /* re-check (new inputs, interp function) */
-   0,                          /* re-run (always runs) */
-   GL_TRUE,                    /* active */
-   0, 0,                       /* inputs (set in check_render), outputs */
-   0, 0,                       /* changed_inputs, private */
-   dtr,                                /* destructor */
-   alloc_render_data,          /* check - initially set to alloc data */
+   "render",                   /* name */
+   NULL,                       /* private data */
+   NULL,                       /* creator */
+   NULL,                       /* destructor */
+   NULL,                       /* validate */
    run_render                  /* run */
 };