fixup draw/depth region handling in i830 along lines of i915
[mesa.git] / src / mesa / drivers / glide / fxtris.c
index 4270ceb0afec0be7244176726ee1b3752979685f..0b9b3be5829613ea1fcc643ba1f97d92188f2547 100644 (file)
@@ -35,8 +35,6 @@
 #include "mtypes.h"
 #include "macros.h"
 #include "colormac.h"
-#include "nvfragprog.h"
-
 #include "swrast/swrast.h"
 #include "swrast_setup/swrast_setup.h"
 #include "tnl/t_context.h"
@@ -45,7 +43,7 @@
 #include "fxdrv.h"
 
 
-GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass);
+static GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass);
 
 
 /*
@@ -63,6 +61,19 @@ GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass);
 static void fxRasterPrimitive( GLcontext *ctx, GLenum prim );
 static void fxRenderPrimitive( GLcontext *ctx, GLenum prim );
 
+static GLenum reduced_prim[GL_POLYGON+1] = {
+   GL_POINTS,
+   GL_LINES,
+   GL_LINES,
+   GL_LINES,
+   GL_TRIANGLES,
+   GL_TRIANGLES,
+   GL_TRIANGLES,
+   GL_TRIANGLES,
+   GL_TRIANGLES,
+   GL_TRIANGLES
+};
+
 /***********************************************************************
  *          Macros for t_dd_tritmp.h to draw basic primitives          *
  ***********************************************************************/
@@ -127,25 +138,26 @@ do {                                              \
  *              Fallback to swrast for basic primitives                *
  ***********************************************************************/
 
-/* Build an SWvertex from a hardware vertex. 
+/* Build an SWvertex from a hardware vertex.
  *
  * This code is hit only when a mix of accelerated and unaccelerated
  * primitives are being drawn, and only for the unaccelerated
- * primitives.  
+ * primitives.
  */
-static void 
+static void
 fx_translate_vertex( GLcontext *ctx, const GrVertex *src, SWvertex *dst)
 {
    fxMesaContext fxMesa = FX_CONTEXT(ctx);
    GLuint ts0 = fxMesa->tmu_source[0];
    GLuint ts1 = fxMesa->tmu_source[1];
-   GLfloat w = 1.0 / src->oow;
+   GLfloat w = 1.0F / src->oow;
 
    dst->win[0] = src->x;
    dst->win[1] = src->y;
    dst->win[2] = src->ooz;
    dst->win[3] = src->oow;
 
+#if FX_PACKEDCOLOR
    dst->color[0] = src->pargb[2];
    dst->color[1] = src->pargb[1];
    dst->color[2] = src->pargb[0];
@@ -154,6 +166,16 @@ fx_translate_vertex( GLcontext *ctx, const GrVertex *src, SWvertex *dst)
    dst->specular[0] = src->pspec[2];
    dst->specular[1] = src->pspec[1];
    dst->specular[2] = src->pspec[0];
+#else  /* !FX_PACKEDCOLOR */
+   dst->color[0] = src->r;
+   dst->color[1] = src->g;
+   dst->color[2] = src->b;
+   dst->color[3] = src->a;
+
+   dst->specular[0] = src->r1;
+   dst->specular[1] = src->g1;
+   dst->specular[2] = src->g1;
+#endif /* !FX_PACKEDCOLOR */
 
    dst->texcoord[ts0][0] = fxMesa->inv_s0scale * src->tmuvtx[0].sow * w;
    dst->texcoord[ts0][1] = fxMesa->inv_t0scale * src->tmuvtx[0].tow * w;
@@ -161,7 +183,7 @@ fx_translate_vertex( GLcontext *ctx, const GrVertex *src, SWvertex *dst)
    if (fxMesa->stw_hint_state & GR_STWHINT_W_DIFF_TMU0)
       dst->texcoord[ts0][3] = src->tmuvtx[0].oow * w;
    else
-      dst->texcoord[ts0][3] = 1.0;
+      dst->texcoord[ts0][3] = 1.0F;
 
    if (fxMesa->SetupIndex & SETUP_TMU1) {
       dst->texcoord[ts1][0] = fxMesa->inv_s1scale * src->tmuvtx[1].sow * w;
@@ -170,17 +192,17 @@ fx_translate_vertex( GLcontext *ctx, const GrVertex *src, SWvertex *dst)
       if (fxMesa->stw_hint_state & GR_STWHINT_W_DIFF_TMU1)
         dst->texcoord[ts1][3] = src->tmuvtx[1].oow * w;
       else
-        dst->texcoord[ts1][3] = 1.0;
+        dst->texcoord[ts1][3] = 1.0F;
    }
 
    dst->pointSize = src->psize;
 }
 
 
-static void 
-fx_fallback_tri( fxMesaContext fxMesa, 
-                  GrVertex *v0, 
-                  GrVertex *v1, 
+static void
+fx_fallback_tri( fxMesaContext fxMesa,
+                  GrVertex *v0,
+                  GrVertex *v1,
                   GrVertex *v2 )
 {
    GLcontext *ctx = fxMesa->glCtx;
@@ -193,7 +215,7 @@ fx_fallback_tri( fxMesaContext fxMesa,
 }
 
 
-static void 
+static void
 fx_fallback_line( fxMesaContext fxMesa,
                    GrVertex *v0,
                    GrVertex *v1 )
@@ -206,8 +228,8 @@ fx_fallback_line( fxMesaContext fxMesa,
 }
 
 
-static void 
-fx_fallback_point( fxMesaContext fxMesa, 
+static void
+fx_fallback_point( fxMesaContext fxMesa,
                     GrVertex *v0 )
 {
    GLcontext *ctx = fxMesa->glCtx;
@@ -227,8 +249,12 @@ static void fx_print_vertex( GLcontext *ctx, const GrVertex *v )
  fprintf(stderr, "\tvertex at %p\n", (void *) v);
 
  fprintf(stderr, "\tx %f y %f z %f oow %f\n", v->x, v->y, v->ooz, v->oow);
+#if FX_PACKEDCOLOR
  fprintf(stderr, "\tr %d g %d b %d a %d\n", v->pargb[2], v->pargb[1], v->pargb[0], v->pargb[3]);
-   
+#else  /* !FX_PACKEDCOLOR */
+ fprintf(stderr, "\tr %f g %f b %f a %f\n", v->r, v->g, v->b, v->a);
+#endif /* !FX_PACKEDCOLOR */
+
  fprintf(stderr, "\n");
 }
 
@@ -289,7 +315,7 @@ static void fx_draw_point_sprite ( fxMesaContext fxMesa,
  GLfloat u1scale = fxMesa->s1scale * w;
  GLfloat v1scale = fxMesa->t1scale * w;
 
- radius = psize / 2.;
+ radius = psize / 2.0F;
  _v_[0] = *v0;
  _v_[1] = *v0;
  _v_[2] = *v0;
@@ -353,7 +379,7 @@ static void fx_draw_point_wide ( fxMesaContext fxMesa,
  const GLcontext *ctx = fxMesa->glCtx;
  const GLfloat psize = (ctx->_TriangleCaps & DD_POINT_ATTEN)
                        ? CLAMP(v0->psize, ctx->Point.MinSize, ctx->Point.MaxSize)
-                      : ctx->Point._Size; /* clamped */
+                       : ctx->Point._Size; /* clamped */
 
  if (ctx->Point.PointSprite) {
     fx_draw_point_sprite(fxMesa, v0, psize);
@@ -364,10 +390,10 @@ static void fx_draw_point_wide ( fxMesaContext fxMesa,
  _v_[1] = &vtxB;
  _v_[2] = &vtxC;
 
- radius = psize / 2.;
+ radius = psize / 2.0F;
  n = IROUND(psize * 2); /* radius x 4 */
  if (n < 4) n = 4;
- oon = 1.0 / (GLfloat)n;
+ oon = 1.0F / (GLfloat)n;
 
  /* CLIP_LOOP ?!? */
  /* point coverage? */
@@ -430,17 +456,17 @@ static void fx_draw_point_wide_aa ( fxMesaContext fxMesa,
  const GLcontext *ctx = fxMesa->glCtx;
  const GLfloat psize = (ctx->_TriangleCaps & DD_POINT_ATTEN)
                        ? CLAMP(v0->psize, ctx->Point.MinSize, ctx->Point.MaxSize)
-                      : ctx->Point._Size; /* clamped */
+                       : ctx->Point._Size; /* clamped */
 
  if (ctx->Point.PointSprite) {
     fx_draw_point_sprite(fxMesa, v0, psize);
     return;
  }
 
- radius = psize / 2.;
+ radius = psize / 2.0F;
  n = IROUND(psize * 2); /* radius x 4 */
  if (n < 4) n = 4;
- oon = 1.0 / (GLfloat)n;
+ oon = 1.0F / (GLfloat)n;
 
  /* CLIP_LOOP ?!? */
  /* point coverage? */
@@ -471,8 +497,9 @@ static void fx_draw_point_wide_aa ( fxMesaContext fxMesa,
 #define FX_OFFSET_BIT     0x2
 #define FX_TWOSIDE_BIT     0x4
 #define FX_FLAT_BIT        0x8
-#define FX_FALLBACK_BIT    0x10
-#define FX_MAX_TRIFUNC     0x20
+#define FX_TWOSTENCIL_BIT  0x10
+#define FX_FALLBACK_BIT    0x20
+#define FX_MAX_TRIFUNC     0x40
 
 static struct {
    tnl_points_func     points;
@@ -486,6 +513,7 @@ static struct {
 #define DO_UNFILLED (IND & FX_UNFILLED_BIT)
 #define DO_TWOSIDE  (IND & FX_TWOSIDE_BIT)
 #define DO_FLAT     (IND & FX_FLAT_BIT)
+#define DO_TWOSTENCIL (IND & FX_TWOSTENCIL_BIT)
 #define DO_TRI       1
 #define DO_QUAD      1
 #define DO_LINE      1
@@ -505,15 +533,11 @@ static struct {
 #define VERT_X(_v) _v->x
 #define VERT_Y(_v) _v->y
 #define VERT_Z(_v) _v->ooz
+#define AREA_IS_CCW( a ) IS_NEGATIVE( a )
 #define GET_VERTEX(e) (fxMesa->verts + e)
 
-#ifdef USE_IEEE
-#define AREA_IS_CCW( a ) (((fi_type *)&(a))->i < 0)
-#else
-#define AREA_IS_CCW( a ) (a < 0)
-#endif
-
 
+#if FX_PACKEDCOLOR
 #define VERT_SET_RGBA( dst, f )                        \
 do {                                           \
    UNCLAMPED_FLOAT_TO_UBYTE(dst->pargb[2], f[0]);\
@@ -553,15 +577,89 @@ do {                                              \
    fxMesaContext fxMesa = FX_CONTEXT(ctx);     \
    GLubyte color[n][4], spec[n][4];            \
    (void) color; (void) spec;
+#else  /* !FX_PACKEDCOLOR */
+#define VERT_SET_RGBA( dst, f )        \
+do {                           \
+   CNORM(dst->r, f[0]);                \
+   CNORM(dst->g, f[1]);                \
+   CNORM(dst->b, f[2]);                \
+   CNORM(dst->a, f[3]);                \
+} while (0)
+
+#define VERT_COPY_RGBA( v0, v1 )               \
+do {                                           \
+   COPY_FLOAT(v0->r, v1->r);                   \
+   COPY_FLOAT(v0->g, v1->g);                   \
+   COPY_FLOAT(v0->b, v1->b);                   \
+   COPY_FLOAT(v0->a, v1->a);                   \
+} while (0)
+
+#define VERT_SAVE_RGBA( idx )                          \
+do {                                           \
+   COPY_FLOAT(color[idx][0], v[idx]->r);       \
+   COPY_FLOAT(color[idx][1], v[idx]->g);       \
+   COPY_FLOAT(color[idx][2], v[idx]->b);       \
+   COPY_FLOAT(color[idx][3], v[idx]->a);       \
+} while (0)
 
+#define VERT_RESTORE_RGBA( idx )               \
+do {                                           \
+   COPY_FLOAT(v[idx]->r, color[idx][0]);       \
+   COPY_FLOAT(v[idx]->g, color[idx][1]);       \
+   COPY_FLOAT(v[idx]->b, color[idx][2]);       \
+   COPY_FLOAT(v[idx]->a, color[idx][3]);       \
+} while (0)
+
+
+#define VERT_SET_SPEC( dst, f )        \
+do {                           \
+   CNORM(dst->r1, f[0]);       \
+   CNORM(dst->g1, f[1]);       \
+   CNORM(dst->b1, f[2]);       \
+} while (0)
+
+#define VERT_COPY_SPEC( v0, v1 )               \
+do {                                           \
+   COPY_FLOAT(v0->r1, v1->r1);                 \
+   COPY_FLOAT(v0->g1, v1->g1);                 \
+   COPY_FLOAT(v0->b1, v1->b1);                 \
+} while (0)
+
+#define VERT_SAVE_SPEC( idx )                          \
+do {                                           \
+   COPY_FLOAT(spec[idx][0], v[idx]->r1);       \
+   COPY_FLOAT(spec[idx][1], v[idx]->g1);       \
+   COPY_FLOAT(spec[idx][2], v[idx]->b1);       \
+} while (0)
+
+#define VERT_RESTORE_SPEC( idx )               \
+do {                                           \
+   COPY_FLOAT(v[idx]->r1, spec[idx][0]);       \
+   COPY_FLOAT(v[idx]->g1, spec[idx][1]);       \
+   COPY_FLOAT(v[idx]->b1, spec[idx][2]);       \
+} while (0)
+
+
+#define LOCAL_VARS(n)                          \
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);     \
+   GLfloat color[n][4], spec[n][4];            \
+   (void) color; (void) spec;
+#endif /* !FX_PACKEDCOLOR */
+
+
+/***********************************************************************
+ *            Twoside stencil                                          *
+ ***********************************************************************/
+#define SETUP_STENCIL(f) if (f) fxSetupStencilFace(ctx, f)
+#define UNSET_STENCIL(f) if (f) fxSetupStencil(ctx)
 
 
 /***********************************************************************
  *            Functions to draw basic unfilled primitives              *
  ***********************************************************************/
 
-#define RASTERIZE(x) if (fxMesa->raster_primitive != x) \
-                        fxRasterPrimitive( ctx, x )
+#define RASTERIZE(x) if (fxMesa->raster_primitive != reduced_prim[x]) \
+                        fxRasterPrimitive( ctx, reduced_prim[x] )
 #define RENDER_PRIMITIVE fxMesa->render_primitive
 #define IND FX_FALLBACK_BIT
 #define TAG(x) x
@@ -706,6 +804,142 @@ do {                                              \
 #include "tnl_dd/t_dd_tritmp.h"
 
 
+/* 2-sided stencil begin */
+#define IND (FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_UNFILLED_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_unfilled_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_unfilled_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_unfilled_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_unfilled_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_unfilled_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_unfilled_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_unfilled_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT| \
+            FX_FALLBACK_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_unfilled_fallback_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+
+/* Fx doesn't support provoking-vertex flat-shading?
+ */
+#define IND (FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_UNFILLED_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_unfilled_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_unfilled_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_unfilled_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_unfilled_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_unfilled_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_OFFSET_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_offset_unfilled_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_UNFILLED_BIT|FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_unfilled_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+
+#define IND (FX_TWOSIDE_BIT|FX_OFFSET_BIT|FX_UNFILLED_BIT| \
+            FX_FALLBACK_BIT|FX_FLAT_BIT|FX_TWOSTENCIL_BIT)
+#define TAG(x) x##_twoside_offset_unfilled_fallback_flat_twostencil
+#include "tnl_dd/t_dd_tritmp.h"
+/* 2-sided stencil end */
+
+
 static void init_rast_tab( void )
 {
    init();
@@ -741,6 +975,42 @@ static void init_rast_tab( void )
    init_offset_unfilled_fallback_flat();
    init_twoside_unfilled_fallback_flat();
    init_twoside_offset_unfilled_fallback_flat();
+
+   /* 2-sided stencil begin */
+   init_twostencil();
+   init_offset_twostencil();
+   init_twoside_twostencil();
+   init_twoside_offset_twostencil();
+   init_unfilled_twostencil();
+   init_offset_unfilled_twostencil();
+   init_twoside_unfilled_twostencil();
+   init_twoside_offset_unfilled_twostencil();
+   init_fallback_twostencil();
+   init_offset_fallback_twostencil();
+   init_twoside_fallback_twostencil();
+   init_twoside_offset_fallback_twostencil();
+   init_unfilled_fallback_twostencil();
+   init_offset_unfilled_fallback_twostencil();
+   init_twoside_unfilled_fallback_twostencil();
+   init_twoside_offset_unfilled_fallback_twostencil();
+
+   init_flat_twostencil();
+   init_offset_flat_twostencil();
+   init_twoside_flat_twostencil();
+   init_twoside_offset_flat_twostencil();
+   init_unfilled_flat_twostencil();
+   init_offset_unfilled_flat_twostencil();
+   init_twoside_unfilled_flat_twostencil();
+   init_twoside_offset_unfilled_flat_twostencil();
+   init_fallback_flat_twostencil();
+   init_offset_fallback_flat_twostencil();
+   init_twoside_fallback_flat_twostencil();
+   init_twoside_offset_fallback_flat_twostencil();
+   init_unfilled_fallback_flat_twostencil();
+   init_offset_unfilled_fallback_flat_twostencil();
+   init_twoside_unfilled_fallback_flat_twostencil();
+   init_twoside_offset_unfilled_fallback_flat_twostencil();
+   /* 2-sided stencil end */
 }
 
 
@@ -847,7 +1117,7 @@ static void fx_render_vb_line_loop( GLcontext *ctx,
    grDrawVertexArrayContiguous( GR_LINE_STRIP, count-j,
                                 fxVB + j, sizeof(GrVertex));
 
-   if (flags & PRIM_END) 
+   if (flags & PRIM_END)
       grDrawLine( fxVB + (count - 1),
                   fxVB + start );
 
@@ -1034,7 +1304,7 @@ static void fx_render_vb_noop( GLcontext *ctx,
 static void (*fx_render_tab_verts[GL_POLYGON+2])(GLcontext *,
                                                   GLuint,
                                                   GLuint,
-                                                  GLuint) = 
+                                                  GLuint) =
 {
    fx_render_vb_points,
    fx_render_vb_lines,
@@ -1089,8 +1359,8 @@ static void (*fx_render_tab_verts[GL_POLYGON+2])(GLcontext *,
     const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts;      \
     (void) elt;
 
-#define RESET_STIPPLE 
-#define RESET_OCCLUSION 
+#define RESET_STIPPLE
+#define RESET_OCCLUSION
 #define PRESERVE_VB_DEFS
 
 /* Elts, no clipping.
@@ -1117,7 +1387,7 @@ static void (*fx_render_tab_verts[GL_POLYGON+2])(GLcontext *,
 
 
 
-static void fxRenderClippedPoly( GLcontext *ctx, const GLuint *elts, 
+static void fxRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
                                   GLuint n )
 {
    fxMesaContext fxMesa = FX_CONTEXT(ctx);
@@ -1125,12 +1395,12 @@ static void fxRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
    struct vertex_buffer *VB = &tnl->vb;
    GLuint prim = fxMesa->render_primitive;
 
-   /* Render the new vertices as an unclipped polygon. 
+   /* Render the new vertices as an unclipped polygon.
     */
    {
       GLuint *tmp = VB->Elts;
       VB->Elts = (GLuint *)elts;
-      tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, 
+      tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n,
                                                  PRIM_BEGIN|PRIM_END );
       VB->Elts = tmp;
    }
@@ -1142,7 +1412,7 @@ static void fxRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
 }
 
 
-static void fxFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts, 
+static void fxFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
                                       GLuint n )
 {
    int i;
@@ -1174,7 +1444,7 @@ static void fxFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
 #define TRI_FALLBACK (DD_TRI_SMOOTH | DD_TRI_STIPPLE)
 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK | LINE_FALLBACK | TRI_FALLBACK)
 #define ANY_RASTER_FLAGS (DD_FLATSHADE | DD_TRI_LIGHT_TWOSIDE | DD_TRI_OFFSET \
-                         | DD_TRI_UNFILLED)
+                         | DD_TRI_UNFILLED | DD_TRI_TWOSTENCIL)
 
 
 
@@ -1187,6 +1457,7 @@ void fxDDChooseRenderState(GLcontext *ctx)
 
    if (flags & (ANY_FALLBACK_FLAGS|ANY_RASTER_FLAGS)) {
       if (flags & ANY_RASTER_FLAGS) {
+        if (flags & DD_TRI_TWOSTENCIL)       index |= FX_TWOSTENCIL_BIT;
         if (flags & DD_TRI_LIGHT_TWOSIDE)    index |= FX_TWOSIDE_BIT;
         if (flags & DD_TRI_OFFSET)           index |= FX_OFFSET_BIT;
         if (flags & DD_TRI_UNFILLED)         index |= FX_UNFILLED_BIT;
@@ -1306,15 +1577,15 @@ static void fxRunPipeline( GLcontext *ctx )
       if (t0->_Current && FX_TEXTURE_DATA(t0)) {
          fxMesa->s0scale = FX_TEXTURE_DATA(t0)->sScale;
          fxMesa->t0scale = FX_TEXTURE_DATA(t0)->tScale;
-         fxMesa->inv_s0scale = 1.0 / fxMesa->s0scale;
-         fxMesa->inv_t0scale = 1.0 / fxMesa->t0scale;
+         fxMesa->inv_s0scale = 1.0F / fxMesa->s0scale;
+         fxMesa->inv_t0scale = 1.0F / fxMesa->t0scale;
       }
 
       if (t1->_Current && FX_TEXTURE_DATA(t1)) {
          fxMesa->s1scale = FX_TEXTURE_DATA(t1)->sScale;
          fxMesa->t1scale = FX_TEXTURE_DATA(t1)->tScale;
-         fxMesa->inv_s1scale = 1.0 / fxMesa->s1scale;
-         fxMesa->inv_t1scale = 1.0 / fxMesa->t1scale;
+         fxMesa->inv_s1scale = 1.0F / fxMesa->s1scale;
+         fxMesa->inv_t1scale = 1.0F / fxMesa->t1scale;
       }
    }
 
@@ -1324,20 +1595,6 @@ static void fxRunPipeline( GLcontext *ctx )
 }
 
 
-static GLenum reduced_prim[GL_POLYGON+1] = {
-   GL_POINTS,
-   GL_LINES,
-   GL_LINES,
-   GL_LINES,
-   GL_TRIANGLES,
-   GL_TRIANGLES,
-   GL_TRIANGLES,
-   GL_TRIANGLES,
-   GL_TRIANGLES,
-   GL_TRIANGLES
-};
-
-
 
 /* Always called between RenderStart and RenderFinish --> We already
  * hold the lock.
@@ -1353,7 +1610,7 @@ static void fxRasterPrimitive( GLcontext *ctx, GLenum prim )
 
 
 
-/* Determine the rasterized primitive when not drawing unfilled 
+/* Determine the rasterized primitive when not drawing unfilled
  * polygons.
  */
 static void fxRenderPrimitive( GLcontext *ctx, GLenum prim )
@@ -1365,7 +1622,7 @@ static void fxRenderPrimitive( GLcontext *ctx, GLenum prim )
 
    if (rprim == GL_TRIANGLES && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
       return;
-       
+
    if (fxMesa->raster_primitive != rprim) {
       fxRasterPrimitive( ctx, rprim );
    }
@@ -1396,7 +1653,7 @@ static char *fallbackStrings[] = {
    "Texture border",
    "glColorMask",
    "blend mode",
-   "line stipple"
+   "multitex"
 };
 
 
@@ -1432,7 +1689,7 @@ void fxCheckIsInHardware( GLcontext *ctx )
         tnl->Driver.Render.Start = fxCheckTexSizes;
         tnl->Driver.Render.Finish = fxRenderFinish;
         tnl->Driver.Render.PrimitiveNotify = fxRenderPrimitive;
-        tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; 
+        tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
         tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine;
         tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
         tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
@@ -1444,7 +1701,15 @@ void fxCheckIsInHardware( GLcontext *ctx )
             fprintf(stderr, "Voodoo ! leave SW 0x%08x %s\n", oldfallback, getFallbackString(oldfallback));
          }
       }
-      tnl->Driver.Render.Multipass = (HAVE_SPEC && NEED_SECONDARY_COLOR(ctx)) ? fxMultipass_ColorSum : NULL;
+      tnl->Driver.Render.Multipass = NULL;
+      if (HAVE_SPEC && NEED_SECONDARY_COLOR(ctx)) {
+         tnl->Driver.Render.Multipass = fxMultipass_ColorSum;
+         /* obey stencil, but do not change it */
+         fxMesa->multipass = GL_TRUE;
+         if (fxMesa->unitsState.stencilEnabled) {
+            fxMesa->new_state |= FX_NEW_STENCIL;
+         }
+      }
    }
 }
 
@@ -1462,33 +1727,39 @@ void fxDDInitTriFuncs( GLcontext *ctx )
    tnl->Driver.Render.Start = fxCheckTexSizes;
    tnl->Driver.Render.Finish = fxRenderFinish;
    tnl->Driver.Render.PrimitiveNotify = fxRenderPrimitive;
-   tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; 
+   tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
    tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine;
    tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
    tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
    tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
    tnl->Driver.Render.BuildVertices = fxBuildVertices;
    tnl->Driver.Render.Multipass = NULL;
-   
+
    (void) fx_print_vertex;
 }
 
 
 /* [dBorca] Hack alert:
  * doesn't work with blending.
- * XXX todo - need to take care of stencil.
  */
-GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass)
+static GLboolean
+fxMultipass_ColorSum (GLcontext *ctx, GLuint pass)
 {
  fxMesaContext fxMesa = FX_CONTEXT(ctx);
+ tfxUnitsState *us = &fxMesa->unitsState;
 
  static int t0 = 0;
  static int t1 = 0;
 
  switch (pass) {
         case 1: /* first pass: the TEXTURED triangles are drawn */
+             /* set stencil's real values */
+             fxMesa->multipass = GL_FALSE;
+             if (us->stencilEnabled) {
+                fxSetupStencil(ctx);
+             }
              /* save per-pass data */
-             fxMesa->restoreUnitsState = fxMesa->unitsState;
+             fxMesa->restoreUnitsState = *us;
              /* turn off texturing */
              t0 = ctx->Texture.Unit[0]._ReallyEnabled;
              t1 = ctx->Texture.Unit[1]._ReallyEnabled;
@@ -1499,30 +1770,37 @@ GLboolean fxMultipass_ColorSum (GLcontext *ctx, GLuint pass)
              fxDDBlendFuncSeparate(ctx, GL_ONE, GL_ONE, GL_ZERO, GL_ONE);
              fxDDEnable(ctx, GL_BLEND, GL_TRUE);
              /* make sure we draw only where we want to */
-             if (ctx->Depth.Mask) {
-                switch (ctx->Depth.Func) {
-                       case GL_NEVER:
-                       case GL_ALWAYS:
-                       break;
-                default:
-                       fxDDDepthFunc( ctx, GL_EQUAL );
-                       break;
+             if (us->depthTestEnabled) {
+                switch (us->depthTestFunc) {
+                   default:
+                      fxDDDepthFunc(ctx, GL_EQUAL);
+                   case GL_NEVER:
+                   case GL_ALWAYS:
+                      ;
                 }
-                fxDDDepthMask( ctx, GL_FALSE );
+                fxDDDepthMask(ctx, GL_FALSE);
              }
              /* switch to secondary colors */
+#if FX_PACKEDCOLOR
              grVertexLayout(GR_PARAM_PARGB, GR_VERTEX_PSPEC_OFFSET << 2, GR_PARAM_ENABLE);
+#else  /* !FX_PACKEDCOLOR */
+             grVertexLayout(GR_PARAM_RGB, GR_VERTEX_SPEC_OFFSET << 2, GR_PARAM_ENABLE);
+#endif /* !FX_PACKEDCOLOR */
              /* don't advertise new state */
              fxMesa->new_state = 0;
              break;
         case 2: /* 2nd pass (last): the secondary color is summed over texture */
              /* restore original state */
-             fxMesa->unitsState = fxMesa->restoreUnitsState;
+             *us = fxMesa->restoreUnitsState;
              /* restore texturing */
              ctx->Texture.Unit[0]._ReallyEnabled = t0;
              ctx->Texture.Unit[1]._ReallyEnabled = t1;
              /* revert to primary colors */
+#if FX_PACKEDCOLOR
              grVertexLayout(GR_PARAM_PARGB, GR_VERTEX_PARGB_OFFSET << 2, GR_PARAM_ENABLE);
+#else  /* !FX_PACKEDCOLOR */
+             grVertexLayout(GR_PARAM_RGB, GR_VERTEX_RGB_OFFSET << 2, GR_PARAM_ENABLE);
+#endif /* !FX_PACKEDCOLOR */
              break;
         default:
              assert(0); /* NOTREACHED */