implement arb_vertex_program in hw for r200. Code contains still some hacks, generic...
[mesa.git] / src / mesa / drivers / glide / fxsetup.c
index dc2db8e3198c273d3e88266a8f45d23338dfb11a..240e5e0b5970dc2e77d0dcb8d56a15c2a45398fe 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.3
+ * Version:  4.0
  *
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2001  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"),
  * 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.
- *
- *
- * Original Mesa / 3Dfx device driver (C) 1999 David Bucciarelli, by the
- * terms stated above.
- *
- * Thank you for your contribution, David!
- *
- * Please make note of the above copyright/license statement.  If you
- * contributed code or bug fixes to this code under the previous (GNU
- * Library) license and object to the new license, your code will be
- * removed at your request.  Please see the Mesa docs/COPYRIGHT file
- * for more information.
- *
- * Additional Mesa/3Dfx driver developers:
- *   Daryll Strauss <daryll@precisioninsight.com>
- *   Keith Whitwell <keith@precisioninsight.com>
- *
- * See fxapi.h for more revision/author details.
  */
 
+/* Authors:
+ *    David Bucciarelli
+ *    Brian Paul
+ *    Daryll Strauss
+ *    Keith Whitwell
+ *    Daniel Borca
+ *    Hiroshi Morii
+ */
 
 /* fxsetup.c - 3Dfx VooDoo rendering mode setup functions */
 
 
 #include "fxdrv.h"
 #include "enums.h"
-
+#include "tnl.h"
 #include "tnl/t_context.h"
+#include "swrast.h"
+#include "texstore.h"
 
-static GLuint fxGetTexSetConfiguration(GLcontext * ctx,
-                                      struct gl_texture_object *tObj0,
-                                      struct gl_texture_object *tObj1);
-static void fxSetupTextureSingleTMU_NoLock(GLcontext * ctx,
-                                          GLuint textureset);
-static void fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa,
-                                   struct gl_texture_object *tObj0,
-                                   struct gl_texture_object *tObj1);
-static void fxSetupTexture_NoLock(GLcontext * ctx);
-static void fxSetupTexture(GLcontext * ctx);
-static void fxSetupBlend(GLcontext * ctx);
-static void fxSetupDepthTest(GLcontext * ctx);
-static void fxSetupScissor(GLcontext * ctx);
-static void fxSetupCull(GLcontext * ctx);
-static void fx_print_state_flags(const char *msg, GLuint flags);
-/*static GLboolean fxMultipassBlend(struct vertex_buffer *, GLuint);*/
-static GLboolean fxMultipassTexture(GLcontext *, GLuint);
 
 static void
 fxTexValidate(GLcontext * ctx, struct gl_texture_object *tObj)
@@ -80,69 +54,111 @@ fxTexValidate(GLcontext * ctx, struct gl_texture_object *tObj)
    tfxTexInfo *ti = fxTMGetTexInfo(tObj);
    GLint minl, maxl;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxTexValidate(...) Start\n");
-   }
-
    if (ti->validated) {
-      if (MESA_VERBOSE & VERBOSE_DRIVER) {
-        fprintf(stderr,
-                "fxmesa: fxTexValidate(...) End (validated=GL_TRUE)\n");
+      if (TDFX_DEBUG & VERBOSE_DRIVER) {
+        fprintf(stderr, "fxTexValidate(NOP)\n");
       }
       return;
    }
 
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxTexValidate(%p (%d))\n", (void *)tObj, tObj->Name);
+   }
+
    ti->tObj = tObj;
    minl = ti->minLevel = tObj->BaseLevel;
-   maxl = ti->maxLevel = MIN2(tObj->MaxLevel, tObj->Image[0]->MaxLog2);
+   maxl = ti->maxLevel = MIN2(tObj->MaxLevel, tObj->Image[0][0]->MaxLog2);
 
-   fxTexGetInfo(tObj->Image[minl]->Width, tObj->Image[minl]->Height,
+#if FX_RESCALE_BIG_TEXURES_HACK
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   /* [dBorca]
+    * Fake textures larger than HW supports:
+    * 1) we have mipmaps. Then we just push up to the first supported
+    *    LOD. A possible drawback is that Mesa will ignore the skipped
+    *    LODs on further texture handling.
+    *    Will this interfere with GL_TEXTURE_[MIN|BASE]_LEVEL? How?
+    * 2) we don't have mipmaps. We need to rescale the big LOD in place.
+    *    The above approach is somehow dumb! we might have rescaled
+    *    once in TexImage2D to accomodate aspect ratio, and now we
+    *    are rescaling again. The thing is, in TexImage2D we don't
+    *    know whether we'll hit 1) or 2) by the time of validation.
+    */
+   if ((tObj->MinFilter == GL_NEAREST) || (tObj->MinFilter == GL_LINEAR)) {
+      /* no mipmaps! */
+      struct gl_texture_image *texImage = tObj->Image[0][minl];
+      tfxMipMapLevel *mml = FX_MIPMAP_DATA(texImage);
+      GLint _w, _h, maxSize = 1 << fxMesa->textureMaxLod;
+      if ((mml->width > maxSize) || (mml->height > maxSize)) {
+         /* need to rescale */
+         GLint texelBytes = texImage->TexFormat->TexelBytes;
+         GLvoid *texImage_Data = texImage->Data;
+         _w = MIN2(texImage->Width, maxSize);
+         _h = MIN2(texImage->Height, maxSize);
+         if (TDFX_DEBUG & VERBOSE_TEXTURE) {
+            fprintf(stderr, "fxTexValidate: rescaling %d x %d -> %d x %d\n",
+                            texImage->Width, texImage->Height, _w, _h);
+         }
+         /* we should leave these as is and... (!) */
+         texImage->Width = _w;
+         texImage->Height = _h;
+         fxTexGetInfo(_w, _h, NULL, NULL, NULL, NULL,
+                      &(mml->wScale), &(mml->hScale));
+         _w *= mml->wScale;
+         _h *= mml->hScale;
+         texImage->Data = _mesa_malloc(_w * _h * texelBytes);
+         _mesa_rescale_teximage2d(texelBytes,
+                                  mml->width,
+                                  _w * texelBytes, /* dst stride */
+                                  mml->width, mml->height, /* src */
+                                  _w, _h, /* dst */
+                                  texImage_Data /*src*/, texImage->Data /*dst*/ );
+         _mesa_free(texImage_Data);
+         mml->width = _w;
+         mml->height = _h;
+         /* (!) ... and set mml->wScale = _w / texImage->Width */
+      }
+   } else {
+      /* mipmapping */
+      if (maxl - minl > fxMesa->textureMaxLod) {
+         /* skip a certain number of LODs */
+         minl += maxl - fxMesa->textureMaxLod;
+         if (TDFX_DEBUG & VERBOSE_TEXTURE) {
+            fprintf(stderr, "fxTexValidate: skipping %d LODs\n", minl - ti->minLevel);
+         }
+         ti->minLevel = tObj->BaseLevel = minl;
+      }
+   }
+}
+#endif
+
+   fxTexGetInfo(tObj->Image[0][minl]->Width, tObj->Image[0][minl]->Height,
                &(FX_largeLodLog2(ti->info)), &(FX_aspectRatioLog2(ti->info)),
                &(ti->sScale), &(ti->tScale),
-               &(ti->int_sScale), &(ti->int_tScale), NULL, NULL);
+               NULL, NULL);
 
    if ((tObj->MinFilter != GL_NEAREST) && (tObj->MinFilter != GL_LINEAR))
-      fxTexGetInfo(tObj->Image[maxl]->Width, tObj->Image[maxl]->Height,
+      fxTexGetInfo(tObj->Image[0][maxl]->Width, tObj->Image[0][maxl]->Height,
                   &(FX_smallLodLog2(ti->info)), NULL,
-                  NULL, NULL, NULL, NULL, NULL, NULL);
+                  NULL, NULL, NULL, NULL);
    else
       FX_smallLodLog2(ti->info) = FX_largeLodLog2(ti->info);
 
-   fxTexGetFormat(tObj->Image[minl]->IntFormat, &(ti->info.format),
-                 &(ti->baseLevelInternalFormat));
-
-   switch (tObj->WrapS) {
-   case GL_CLAMP_TO_EDGE:
-      /* What's this really mean compared to GL_CLAMP? */
-   case GL_CLAMP:
-      ti->sClamp = 1;
-      break;
-   case GL_REPEAT:
-      ti->sClamp = 0;
-      break;
-   default:
-      ;                                /* silence compiler warning */
-   }
-   switch (tObj->WrapT) {
-   case GL_CLAMP_TO_EDGE:
-      /* What's this really mean compared to GL_CLAMP? */
-   case GL_CLAMP:
-      ti->tClamp = 1;
-      break;
-   case GL_REPEAT:
-      ti->tClamp = 0;
-      break;
-   default:
-      ;                                /* silence compiler warning */
+   /* [dBorca] this is necessary because of fxDDCompressedTexImage2D */
+   if (ti->padded) {
+      struct gl_texture_image *texImage = tObj->Image[0][minl];
+      tfxMipMapLevel *mml = FX_MIPMAP_DATA(texImage);
+      if (mml->wScale != 1 || mml->hScale != 1) {
+         ti->sScale /= mml->wScale;
+         ti->tScale /= mml->hScale;
+      }
    }
 
+   ti->baseLevelInternalFormat = tObj->Image[0][minl]->Format;
+
    ti->validated = GL_TRUE;
 
    ti->info.data = NULL;
-
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxTexValidate(...) End\n");
-   }
 }
 
 static void
@@ -302,8 +318,8 @@ fxGetTexSetConfiguration(GLcontext * ctx,
 
    unitsmode |= (ifmt | envmode);
 
-   if (MESA_VERBOSE & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
-      fxPrintUnitsMode("unitsmode", unitsmode);
+   if (TDFX_DEBUG & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
+      fxPrintUnitsMode("fxGetTexSetConfiguration", unitsmode);
 
    return unitsmode;
 }
@@ -320,6 +336,12 @@ fxSetupSingleTMU_NoLock(fxMesaContext fxMesa, struct gl_texture_object *tObj)
    tfxTexInfo *ti = fxTMGetTexInfo(tObj);
    int tmu;
 
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupSingleTMU_NoLock(%p (%d))\n", (void *)tObj, tObj->Name);
+   }
+
+   ti->lastTimeUsed = fxMesa->texBindNumber;
+
    /* Make sure we're not loaded incorrectly */
    if (ti->isInTM) {
       if (ti->LODblend) {
@@ -338,9 +360,7 @@ fxSetupSingleTMU_NoLock(fxMesaContext fxMesa, struct gl_texture_object *tObj)
         fxTMMoveInTM_NoLock(fxMesa, tObj, FX_TMU_SPLIT);
       else {
         if (fxMesa->haveTwoTMUs) {
-           if (fxMesa->freeTexMem[FX_TMU0] >
-               FX_grTexTextureMemRequired_NoLock(GR_MIPMAPLEVELMASK_BOTH,
-                                                 &(ti->info))) {
+            if (fxTMCheckStartAddr(fxMesa, FX_TMU0, ti)) {
               fxTMMoveInTM_NoLock(fxMesa, tObj, FX_TMU0);
            }
            else {
@@ -353,27 +373,25 @@ fxSetupSingleTMU_NoLock(fxMesaContext fxMesa, struct gl_texture_object *tObj)
    }
 
    if (ti->LODblend && ti->whichTMU == FX_TMU_SPLIT) {
+      /* broadcast */
       if ((ti->info.format == GR_TEXFMT_P_8)
          && (!fxMesa->haveGlobalPaletteTexture)) {
-        if (MESA_VERBOSE & VERBOSE_DRIVER) {
-           fprintf(stderr, "fxmesa: uploading texture palette\n");
+        if (TDFX_DEBUG & VERBOSE_DRIVER) {
+           fprintf(stderr, "fxSetupSingleTMU_NoLock: uploading texture palette\n");
         }
-        FX_grTexDownloadTable_NoLock(GR_TMU0, GR_TEXTABLE_PALETTE,
-                                     &(ti->palette));
-        FX_grTexDownloadTable_NoLock(GR_TMU1, GR_TEXTABLE_PALETTE,
-                                     &(ti->palette));
+        grTexDownloadTable(ti->paltype, &(ti->palette));
       }
 
-      FX_grTexClampMode_NoLock(GR_TMU0, ti->sClamp, ti->tClamp);
-      FX_grTexClampMode_NoLock(GR_TMU1, ti->sClamp, ti->tClamp);
-      FX_grTexFilterMode_NoLock(GR_TMU0, ti->minFilt, ti->maxFilt);
-      FX_grTexFilterMode_NoLock(GR_TMU1, ti->minFilt, ti->maxFilt);
-      FX_grTexMipMapMode_NoLock(GR_TMU0, ti->mmMode, ti->LODblend);
-      FX_grTexMipMapMode_NoLock(GR_TMU1, ti->mmMode, ti->LODblend);
+      grTexClampMode(GR_TMU0, ti->sClamp, ti->tClamp);
+      grTexClampMode(GR_TMU1, ti->sClamp, ti->tClamp);
+      grTexFilterMode(GR_TMU0, ti->minFilt, ti->maxFilt);
+      grTexFilterMode(GR_TMU1, ti->minFilt, ti->maxFilt);
+      grTexMipMapMode(GR_TMU0, ti->mmMode, ti->LODblend);
+      grTexMipMapMode(GR_TMU1, ti->mmMode, ti->LODblend);
 
-      FX_grTexSource_NoLock(GR_TMU0, ti->tm[FX_TMU0]->startAddr,
+      grTexSource(GR_TMU0, ti->tm[FX_TMU0]->startAddr,
                            GR_MIPMAPLEVELMASK_ODD, &(ti->info));
-      FX_grTexSource_NoLock(GR_TMU1, ti->tm[FX_TMU1]->startAddr,
+      grTexSource(GR_TMU1, ti->tm[FX_TMU1]->startAddr,
                            GR_MIPMAPLEVELMASK_EVEN, &(ti->info));
    }
    else {
@@ -382,103 +400,120 @@ fxSetupSingleTMU_NoLock(fxMesaContext fxMesa, struct gl_texture_object *tObj)
       else
         tmu = ti->whichTMU;
 
+      /* pointcast */
       if ((ti->info.format == GR_TEXFMT_P_8)
          && (!fxMesa->haveGlobalPaletteTexture)) {
-        if (MESA_VERBOSE & VERBOSE_DRIVER) {
-           fprintf(stderr, "fxmesa: uploading texture palette\n");
+        if (TDFX_DEBUG & VERBOSE_DRIVER) {
+           fprintf(stderr, "fxSetupSingleTMU_NoLock: uploading texture palette\n");
         }
-        FX_grTexDownloadTable_NoLock(tmu, GR_TEXTABLE_PALETTE,
-                                     &(ti->palette));
+        fxMesa->Glide.grTexDownloadTableExt(tmu, ti->paltype, &(ti->palette));
       }
 
       /* KW: The alternative is to do the download to the other tmu.  If
        * we get to this point, I think it means we are thrashing the
        * texture memory, so perhaps it's not a good idea.  
        */
-      if (ti->LODblend && (MESA_VERBOSE & VERBOSE_DRIVER))
-        fprintf(stderr, "fxmesa: not blending texture - only on one tmu\n");
+      if (ti->LODblend && (TDFX_DEBUG & VERBOSE_DRIVER)) {
+        fprintf(stderr, "fxSetupSingleTMU_NoLock: not blending texture - only one tmu\n");
+      }
 
-      FX_grTexClampMode_NoLock(tmu, ti->sClamp, ti->tClamp);
-      FX_grTexFilterMode_NoLock(tmu, ti->minFilt, ti->maxFilt);
-      FX_grTexMipMapMode_NoLock(tmu, ti->mmMode, FXFALSE);
+      grTexClampMode(tmu, ti->sClamp, ti->tClamp);
+      grTexFilterMode(tmu, ti->minFilt, ti->maxFilt);
+      grTexMipMapMode(tmu, ti->mmMode, FXFALSE);
 
-      FX_grTexSource_NoLock(tmu, ti->tm[tmu]->startAddr,
-                           GR_MIPMAPLEVELMASK_BOTH, &(ti->info));
+      grTexSource(tmu, ti->tm[tmu]->startAddr, GR_MIPMAPLEVELMASK_BOTH, &(ti->info));
    }
 }
 
 static void
 fxSelectSingleTMUSrc_NoLock(fxMesaContext fxMesa, GLint tmu, FxBool LODblend)
 {
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSelectSingleTMUSrc(%d,%d)\n", tmu, LODblend);
+   struct tdfx_texcombine tex0, tex1;
+
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSelectSingleTMUSrc_NoLock(%d, %d)\n", tmu, LODblend);
    }
 
+   tex0.InvertRGB     = FXFALSE;
+   tex0.InvertAlpha   = FXFALSE;
+   tex1.InvertRGB     = FXFALSE;
+   tex1.InvertAlpha   = FXFALSE;
+
    if (LODblend) {
-      FX_grTexCombine_NoLock(GR_TMU0,
-                            GR_COMBINE_FUNCTION_BLEND,
-                            GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION,
-                            GR_COMBINE_FUNCTION_BLEND,
-                            GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION,
-                            FXFALSE, FXFALSE);
-
-      if (fxMesa->haveTwoTMUs)
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
+      tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND;
+      tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION;
+      tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND;
+      tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION;
+
+      tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+      tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+      tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+      tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
       fxMesa->tmuSrc = FX_TMU_SPLIT;
    }
    else {
       if (tmu != FX_TMU1) {
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-        if (fxMesa->haveTwoTMUs) {
-           FX_grTexCombine_NoLock(GR_TMU1,
-                                  GR_COMBINE_FUNCTION_ZERO,
-                                  GR_COMBINE_FACTOR_NONE,
-                                  GR_COMBINE_FUNCTION_ZERO,
-                                  GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-        }
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_ZERO;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_ZERO;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
         fxMesa->tmuSrc = FX_TMU0;
       }
       else {
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
 
-        /* GR_COMBINE_FUNCTION_SCALE_OTHER doesn't work ?!? */
-
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_BLEND,
-                               GR_COMBINE_FACTOR_ONE,
-                               GR_COMBINE_FUNCTION_BLEND,
-                               GR_COMBINE_FACTOR_ONE, FXFALSE, FXFALSE);
+        /* correct values to set TMU0 in passthrough mode */
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
 
         fxMesa->tmuSrc = FX_TMU1;
       }
    }
+
+   grTexCombine(GR_TMU0,
+                tex0.FunctionRGB,
+                tex0.FactorRGB,
+                tex0.FunctionAlpha,
+                tex0.FactorAlpha,
+                tex0.InvertRGB,
+                tex0.InvertAlpha);
+   if (fxMesa->haveTwoTMUs) {
+      grTexCombine(GR_TMU1,
+                   tex1.FunctionRGB,
+                   tex1.FactorRGB,
+                   tex1.FunctionAlpha,
+                   tex1.FactorAlpha,
+                   tex1.InvertRGB,
+                   tex1.InvertAlpha);
+   }
 }
 
 static void
 fxSetupTextureSingleTMU_NoLock(GLcontext * ctx, GLuint textureset)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   struct tdfx_combine alphaComb, colorComb;
    GrCombineLocal_t localc, locala;
    GLuint unitsmode;
    GLint ifmt;
    tfxTexInfo *ti;
-   struct gl_texture_object *tObj = ctx->Texture.Unit[textureset].Current2D;
+   struct gl_texture_object *tObj = ctx->Texture.Unit[textureset]._Current;
    int tmu;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTextureSingleTMU(...) Start\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupTextureSingleTMU_NoLock(%d)\n", textureset);
    }
 
    ti = fxTMGetTexInfo(tObj);
@@ -519,84 +554,170 @@ fxSetupTextureSingleTMU_NoLock(GLcontext * ctx, GLuint textureset)
    else
       localc = GR_COMBINE_LOCAL_CONSTANT;
 
-   if (MESA_VERBOSE & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
-      fprintf(stderr, "fxMesa: fxSetupTextureSingleTMU, envmode is %s\n",
+   if (TDFX_DEBUG & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
+      fprintf(stderr, "fxSetupTextureSingleTMU_NoLock: envmode is %s\n",
              _mesa_lookup_enum_by_nr(ctx->Texture.Unit[textureset].EnvMode));
 
+   alphaComb.Local    = locala;
+   alphaComb.Invert   = FXFALSE;
+   colorComb.Local    = localc;
+   colorComb.Invert   = FXFALSE;
+
    switch (ctx->Texture.Unit[textureset].EnvMode) {
    case GL_DECAL:
-      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                              GR_COMBINE_FACTOR_NONE,
-                              locala, GR_COMBINE_OTHER_NONE, FXFALSE);
+      alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+      alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+      alphaComb.Other    = GR_COMBINE_OTHER_NONE;
 
-      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_BLEND,
-                              GR_COMBINE_FACTOR_TEXTURE_ALPHA,
-                              localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+      colorComb.Function = GR_COMBINE_FUNCTION_BLEND;
+      colorComb.Factor   = GR_COMBINE_FACTOR_TEXTURE_ALPHA;
+      colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
       break;
    case GL_MODULATE:
-      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                              GR_COMBINE_FACTOR_LOCAL,
-                              locala, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
-
-      if (ifmt == GL_ALPHA)
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                                 GR_COMBINE_FACTOR_NONE,
-                                 localc, GR_COMBINE_OTHER_NONE, FXFALSE);
-      else
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+      alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+      alphaComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+      alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+
+      if (ifmt == GL_ALPHA) {
+         colorComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         colorComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         colorComb.Other    = GR_COMBINE_OTHER_NONE;
+      } else {
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         colorComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
       break;
    case GL_BLEND:
-#if 0
-      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                              GR_COMBINE_FACTOR_LOCAL,
-                              locala, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
-      if (ifmt == GL_ALPHA)
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                                 GR_COMBINE_FACTOR_NONE,
-                                 localc, GR_COMBINE_OTHER_NONE, FXFALSE);
-      else
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 localc, GR_COMBINE_OTHER_TEXTURE, FXTRUE);
-      ctx->Driver.MultipassFunc = fxMultipassBlend;
-#else
-      if (MESA_VERBOSE & VERBOSE_DRIVER)
-        fprintf(stderr, "fx Driver: GL_BLEND not yet supported\n");
-#endif
+      if (ifmt == GL_LUMINANCE || ifmt == GL_RGB) {
+         /* Av = Af */
+         alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+      }
+      else if (ifmt == GL_INTENSITY) {
+         /* Av = Af * (1 - It) + Ac * It */
+         alphaComb.Function = GR_COMBINE_FUNCTION_BLEND;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_TEXTURE_ALPHA;
+         alphaComb.Other    = GR_COMBINE_OTHER_CONSTANT;
+      }
+      else {
+         /* Av = Af * At */
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
+
+      if (ifmt == GL_ALPHA) {
+         colorComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         colorComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         colorComb.Other    = GR_COMBINE_OTHER_NONE;
+      } else {
+         if (fxMesa->type >= GR_SSTTYPE_Voodoo2) {
+            colorComb.Function = GR_COMBINE_FUNCTION_BLEND;
+            colorComb.Factor   = GR_COMBINE_FACTOR_TEXTURE_RGB;
+            colorComb.Other    = GR_COMBINE_OTHER_CONSTANT;
+         } else if (ifmt == GL_INTENSITY) {
+            /* just a hack: RGB == ALPHA */
+            colorComb.Function = GR_COMBINE_FUNCTION_BLEND;
+            colorComb.Factor   = GR_COMBINE_FACTOR_TEXTURE_ALPHA;
+            colorComb.Other    = GR_COMBINE_OTHER_CONSTANT;
+         } else {
+            /* Only Voodoo^2 can GL_BLEND (GR_COMBINE_FACTOR_TEXTURE_RGB)
+             * These settings assume that the TexEnv color is black and
+             * incoming fragment color is white.
+             */
+            colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+            colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+            colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+            colorComb.Invert   = FXTRUE;
+            _mesa_problem(NULL, "can't GL_BLEND with SST1");
+         }
+      }
+
+      grConstantColorValue(
+         (((GLuint)(ctx->Texture.Unit[textureset].EnvColor[0] * 255.0f))      ) |
+         (((GLuint)(ctx->Texture.Unit[textureset].EnvColor[1] * 255.0f)) <<  8) |
+         (((GLuint)(ctx->Texture.Unit[textureset].EnvColor[2] * 255.0f)) << 16) |
+         (((GLuint)(ctx->Texture.Unit[textureset].EnvColor[3] * 255.0f)) << 24));
       break;
    case GL_REPLACE:
-      if ((ifmt == GL_RGB) || (ifmt == GL_LUMINANCE))
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                                 GR_COMBINE_FACTOR_NONE,
-                                 locala, GR_COMBINE_OTHER_NONE, FXFALSE);
-      else
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_ONE,
-                                 locala, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
-
-      if (ifmt == GL_ALPHA)
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                                 GR_COMBINE_FACTOR_NONE,
-                                 localc, GR_COMBINE_OTHER_NONE, FXFALSE);
-      else
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_ONE,
-                                 localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+      if ((ifmt == GL_RGB) || (ifmt == GL_LUMINANCE)) {
+         alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+      } else {
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
+
+      if (ifmt == GL_ALPHA) {
+         colorComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         colorComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         colorComb.Other    = GR_COMBINE_OTHER_NONE;
+      } else {
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
+      break;
+   case GL_ADD:
+      if (ifmt == GL_ALPHA ||
+          ifmt == GL_LUMINANCE_ALPHA ||
+          ifmt == GL_RGBA) {
+         /* product of texel and fragment alpha */
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
+      else if (ifmt == GL_LUMINANCE || ifmt == GL_RGB) {
+         /* fragment alpha is unchanged */
+         alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+      }
+      else {
+         /* sum of texel and fragment alpha */
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
+
+      if (ifmt == GL_ALPHA) {
+         /* rgb unchanged */
+         colorComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         colorComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         colorComb.Other    = GR_COMBINE_OTHER_NONE;
+      }
+      else {
+         /* sum of texel and fragment rgb */
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+         colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+      }
       break;
    default:
-      if (MESA_VERBOSE & VERBOSE_DRIVER)
-        fprintf(stderr, "fx Driver: %x Texture.EnvMode not yet supported\n",
+      if (TDFX_DEBUG & VERBOSE_DRIVER) {
+        fprintf(stderr, "fxSetupTextureSingleTMU_NoLock: %x Texture.EnvMode not yet supported\n",
                 ctx->Texture.Unit[textureset].EnvMode);
-      break;
+      }
+      return;
    }
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTextureSingleTMU(...) End\n");
-   }
+   grAlphaCombine(alphaComb.Function,
+                  alphaComb.Factor,
+                  alphaComb.Local,
+                  alphaComb.Other,
+                  alphaComb.Invert);
+   grColorCombine(colorComb.Function,
+                  colorComb.Factor,
+                  colorComb.Local,
+                  colorComb.Other,
+                  colorComb.Invert);
 }
 
+#if 00
 static void
 fxSetupTextureSingleTMU(GLcontext * ctx, GLuint textureset)
 {
@@ -604,6 +725,8 @@ fxSetupTextureSingleTMU(GLcontext * ctx, GLuint textureset)
    fxSetupTextureSingleTMU_NoLock(ctx, textureset);
    END_BOARD_LOCK();
 }
+#endif
+
 
 /************************* Double Texture Set ***************************/
 
@@ -624,8 +747,8 @@ fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa,
    GLuint tstate = 0;
    int tmu0 = 0, tmu1 = 1;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupDoubleTMU(...)\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupDoubleTMU_NoLock(...)\n");
    }
 
    /* We shouldn't need to do this. There is something wrong with
@@ -714,35 +837,37 @@ fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa,
       }
    }
 
+   /* [dBorca] Hack alert:
+    * we put these in reverse order, so that if we can't
+    * do _REAL_ pointcast, the TMU0 table gets broadcasted
+    */
    if (!fxMesa->haveGlobalPaletteTexture) {
-      if (ti0->info.format == GR_TEXFMT_P_8) {
-        if (MESA_VERBOSE & VERBOSE_DRIVER) {
-           fprintf(stderr, "fxmesa: uploading texture palette TMU0\n");
+      /* pointcast */
+      if (ti1->info.format == GR_TEXFMT_P_8) {
+        if (TDFX_DEBUG & VERBOSE_DRIVER) {
+           fprintf(stderr, "fxSetupDoubleTMU_NoLock: uploading texture palette for TMU1\n");
         }
-        FX_grTexDownloadTable_NoLock(tmu0, GR_TEXTABLE_PALETTE,
-                                     &(ti0->palette));
+        fxMesa->Glide.grTexDownloadTableExt(ti1->whichTMU, ti1->paltype, &(ti1->palette));
       }
-
-      if (ti1->info.format == GR_TEXFMT_P_8) {
-        if (MESA_VERBOSE & VERBOSE_DRIVER) {
-           fprintf(stderr, "fxmesa: uploading texture palette TMU1\n");
+      if (ti0->info.format == GR_TEXFMT_P_8) {
+        if (TDFX_DEBUG & VERBOSE_DRIVER) {
+           fprintf(stderr, "fxSetupDoubleTMU_NoLock: uploading texture palette for TMU0\n");
         }
-        FX_grTexDownloadTable_NoLock(tmu1, GR_TEXTABLE_PALETTE,
-                                     &(ti1->palette));
+        fxMesa->Glide.grTexDownloadTableExt(ti0->whichTMU, ti0->paltype, &(ti0->palette));
       }
    }
 
-   FX_grTexSource_NoLock(tmu0, ti0->tm[tmu0]->startAddr,
+   grTexSource(tmu0, ti0->tm[tmu0]->startAddr,
                         GR_MIPMAPLEVELMASK_BOTH, &(ti0->info));
-   FX_grTexClampMode_NoLock(tmu0, ti0->sClamp, ti0->tClamp);
-   FX_grTexFilterMode_NoLock(tmu0, ti0->minFilt, ti0->maxFilt);
-   FX_grTexMipMapMode_NoLock(tmu0, ti0->mmMode, FXFALSE);
+   grTexClampMode(tmu0, ti0->sClamp, ti0->tClamp);
+   grTexFilterMode(tmu0, ti0->minFilt, ti0->maxFilt);
+   grTexMipMapMode(tmu0, ti0->mmMode, FXFALSE);
 
-   FX_grTexSource_NoLock(tmu1, ti1->tm[tmu1]->startAddr,
+   grTexSource(tmu1, ti1->tm[tmu1]->startAddr,
                         GR_MIPMAPLEVELMASK_BOTH, &(ti1->info));
-   FX_grTexClampMode_NoLock(tmu1, ti1->sClamp, ti1->tClamp);
-   FX_grTexFilterMode_NoLock(tmu1, ti1->minFilt, ti1->maxFilt);
-   FX_grTexMipMapMode_NoLock(tmu1, ti1->mmMode, FXFALSE);
+   grTexClampMode(tmu1, ti1->sClamp, ti1->tClamp);
+   grTexFilterMode(tmu1, ti1->minFilt, ti1->maxFilt);
+   grTexMipMapMode(tmu1, ti1->mmMode, FXFALSE);
 
 #undef T0_NOT_IN_TMU
 #undef T1_NOT_IN_TMU
@@ -755,16 +880,18 @@ fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa,
 static void
 fxSetupTextureDoubleTMU_NoLock(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   struct tdfx_combine alphaComb, colorComb;
+   struct tdfx_texcombine tex0, tex1;
    GrCombineLocal_t localc, locala;
    tfxTexInfo *ti0, *ti1;
-   struct gl_texture_object *tObj0 = ctx->Texture.Unit[0].Current2D;
-   struct gl_texture_object *tObj1 = ctx->Texture.Unit[1].Current2D;
+   struct gl_texture_object *tObj0 = ctx->Texture.Unit[1]._Current;
+   struct gl_texture_object *tObj1 = ctx->Texture.Unit[0]._Current;
    GLuint envmode, ifmt, unitsmode;
    int tmu0 = 0, tmu1 = 1;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTextureDoubleTMU(...) Start\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupTextureDoubleTMU_NoLock(...)\n");
    }
 
    ti0 = fxTMGetTexInfo(tObj0);
@@ -799,8 +926,8 @@ fxSetupTextureDoubleTMU_NoLock(GLcontext * ctx)
       localc = GR_COMBINE_LOCAL_CONSTANT;
 
 
-   if (MESA_VERBOSE & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
-      fprintf(stderr, "fxMesa: fxSetupTextureDoubleTMU, envmode is %s/%s\n",
+   if (TDFX_DEBUG & (VERBOSE_DRIVER | VERBOSE_TEXTURE))
+      fprintf(stderr, "fxSetupTextureDoubleTMU_NoLock: envmode is %s/%s\n",
              _mesa_lookup_enum_by_nr(ctx->Texture.Unit[0].EnvMode),
              _mesa_lookup_enum_by_nr(ctx->Texture.Unit[1].EnvMode));
 
@@ -810,135 +937,129 @@ fxSetupTextureDoubleTMU_NoLock(GLcontext * ctx)
       tmu1 = 0;
    }
    fxMesa->tmuSrc = FX_TMU_BOTH;
+
+   tex0.InvertRGB     = FXFALSE;
+   tex0.InvertAlpha   = FXFALSE;
+   tex1.InvertRGB     = FXFALSE;
+   tex1.InvertAlpha   = FXFALSE;
+   alphaComb.Local    = locala;
+   alphaComb.Invert   = FXFALSE;
+   colorComb.Local    = localc;
+   colorComb.Invert   = FXFALSE;
+
    switch (envmode) {
    case (FX_UM_E0_MODULATE | FX_UM_E1_MODULATE):
       {
         GLboolean isalpha[FX_NUM_TMU];
 
-        if (ti0->baseLevelInternalFormat == GL_ALPHA)
-           isalpha[tmu0] = GL_TRUE;
-        else
-           isalpha[tmu0] = GL_FALSE;
-
-        if (ti1->baseLevelInternalFormat == GL_ALPHA)
-           isalpha[tmu1] = GL_TRUE;
-        else
-           isalpha[tmu1] = GL_FALSE;
-
-        if (isalpha[FX_TMU1])
-           FX_grTexCombine_NoLock(GR_TMU1,
-                                  GR_COMBINE_FUNCTION_ZERO,
-                                  GR_COMBINE_FACTOR_NONE,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE, FXTRUE, FXFALSE);
-        else
-           FX_grTexCombine_NoLock(GR_TMU1,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-
-        if (isalpha[FX_TMU0])
-           FX_grTexCombine_NoLock(GR_TMU0,
-                                  GR_COMBINE_FUNCTION_BLEND_OTHER,
-                                  GR_COMBINE_FACTOR_ONE,
-                                  GR_COMBINE_FUNCTION_BLEND_OTHER,
-                                  GR_COMBINE_FACTOR_LOCAL, FXFALSE, FXFALSE);
-        else
-           FX_grTexCombine_NoLock(GR_TMU0,
-                                  GR_COMBINE_FUNCTION_BLEND_OTHER,
-                                  GR_COMBINE_FACTOR_LOCAL,
-                                  GR_COMBINE_FUNCTION_BLEND_OTHER,
-                                  GR_COMBINE_FACTOR_LOCAL, FXFALSE, FXFALSE);
-
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
-
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 locala, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+        isalpha[tmu0] = (ti0->baseLevelInternalFormat == GL_ALPHA);
+        isalpha[tmu1] = (ti1->baseLevelInternalFormat == GL_ALPHA);
+
+        if (isalpha[FX_TMU1]) {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_ZERO;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+            tex1.InvertRGB     = FXTRUE;
+        } else {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+         }
+
+        if (isalpha[FX_TMU0]) {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_LOCAL;
+        } else {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_LOCAL;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_LOCAL;
+         }
+
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         colorComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
         break;
       }
    case (FX_UM_E0_REPLACE | FX_UM_E1_BLEND):   /* Only for GLQuake */
-      if (tmu1 == FX_TMU1) {
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXTRUE, FXFALSE);
-
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_LOCAL,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_LOCAL, FXFALSE, FXFALSE);
+      if (tmu0 == FX_TMU1) {
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+         tex1.InvertRGB     = FXTRUE;
+
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_LOCAL;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_LOCAL;
       }
       else {
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_ONE_MINUS_LOCAL,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_ONE_MINUS_LOCAL,
-                               FXFALSE, FXFALSE);
-      }
-
-      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                              GR_COMBINE_FACTOR_NONE,
-                              locala, GR_COMBINE_OTHER_NONE, FXFALSE);
-
-      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                              GR_COMBINE_FACTOR_ONE,
-                              localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE_MINUS_LOCAL;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE_MINUS_LOCAL;
+      }
+
+      alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+      alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+      alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+
+      colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+      colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+      colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
       break;
    case (FX_UM_E0_REPLACE | FX_UM_E1_MODULATE):        /* Quake 2 and 3 */
       if (tmu1 == FX_TMU1) {
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_ZERO,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXTRUE);
-
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_LOCAL,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_LOCAL, FXFALSE, FXFALSE);
-
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_ZERO;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+         tex1.InvertAlpha   = FXTRUE;
+
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_LOCAL;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_LOCAL;
       }
       else {
-        FX_grTexCombine_NoLock(GR_TMU1,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE,
-                               GR_COMBINE_FUNCTION_LOCAL,
-                               GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-
-        FX_grTexCombine_NoLock(GR_TMU0,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_LOCAL,
-                               GR_COMBINE_FUNCTION_BLEND_OTHER,
-                               GR_COMBINE_FACTOR_ONE, FXFALSE, FXFALSE);
-      }
-
-      if (ti0->baseLevelInternalFormat == GL_RGB)
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                                 GR_COMBINE_FACTOR_NONE,
-                                 locala, GR_COMBINE_OTHER_NONE, FXFALSE);
-      else
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_ONE,
-                                 locala, GR_COMBINE_OTHER_NONE, FXFALSE);
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_LOCAL;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_BLEND_OTHER;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
+      }
 
+      if (ti0->baseLevelInternalFormat == GL_RGB) {
+         alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+      } else {
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+      }
 
-      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                              GR_COMBINE_FACTOR_ONE,
-                              localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+      colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+      colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+      colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
       break;
 
 
@@ -946,59 +1067,148 @@ fxSetupTextureDoubleTMU_NoLock(GLcontext * ctx)
       {
         GLboolean isalpha[FX_NUM_TMU];
 
-        if (ti0->baseLevelInternalFormat == GL_ALPHA)
-           isalpha[tmu0] = GL_TRUE;
-        else
-           isalpha[tmu0] = GL_FALSE;
+        isalpha[tmu0] = (ti0->baseLevelInternalFormat == GL_ALPHA);
+        isalpha[tmu1] = (ti1->baseLevelInternalFormat == GL_ALPHA);
+
+        if (isalpha[FX_TMU1]) {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_ZERO;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+            tex1.InvertRGB     = FXTRUE;
+        } else {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+         }
+
+        if (isalpha[FX_TMU0]) {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_SCALE_OTHER;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
+        } else {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
+         }
+
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         colorComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_LOCAL;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+        break;
+      }
 
-        if (ti1->baseLevelInternalFormat == GL_ALPHA)
-           isalpha[tmu1] = GL_TRUE;
-        else
-           isalpha[tmu1] = GL_FALSE;
-
-        if (isalpha[FX_TMU1])
-           FX_grTexCombine_NoLock(GR_TMU1,
-                                  GR_COMBINE_FUNCTION_ZERO,
-                                  GR_COMBINE_FACTOR_NONE,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE, FXTRUE, FXFALSE);
-        else
-           FX_grTexCombine_NoLock(GR_TMU1,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE,
-                                  GR_COMBINE_FUNCTION_LOCAL,
-                                  GR_COMBINE_FACTOR_NONE, FXFALSE, FXFALSE);
-
-        if (isalpha[FX_TMU0])
-           FX_grTexCombine_NoLock(GR_TMU0,
-                                  GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                  GR_COMBINE_FACTOR_ONE,
-                                  GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
-                                  GR_COMBINE_FACTOR_ONE, FXFALSE, FXFALSE);
-        else
-           FX_grTexCombine_NoLock(GR_TMU0,
-                                  GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
-                                  GR_COMBINE_FACTOR_ONE,
-                                  GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
-                                  GR_COMBINE_FACTOR_ONE, FXFALSE, FXFALSE);
-
-        FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 localc, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
-
-        FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
-                                 GR_COMBINE_FACTOR_LOCAL,
-                                 locala, GR_COMBINE_OTHER_TEXTURE, FXFALSE);
+   case (FX_UM_E0_REPLACE | FX_UM_E1_ADD):     /* Vulpine Sky */
+      {
+        GLboolean isalpha[FX_NUM_TMU];
+
+        isalpha[tmu0] = (ti0->baseLevelInternalFormat == GL_ALPHA);
+        isalpha[tmu1] = (ti1->baseLevelInternalFormat == GL_ALPHA);
+
+        if (isalpha[FX_TMU1]) {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_ZERO;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+            tex1.InvertRGB     = FXTRUE;
+        } else {
+            tex1.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+            tex1.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+            tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+         }
+
+        if (isalpha[FX_TMU0]) {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_SCALE_OTHER;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
+        } else {
+            tex0.FunctionRGB   = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorRGB     = GR_COMBINE_FACTOR_ONE;
+            tex0.FunctionAlpha = GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL;
+            tex0.FactorAlpha   = GR_COMBINE_FACTOR_ONE;
+         }
+
+         colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+
+         alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+         alphaComb.Factor   = GR_COMBINE_FACTOR_ONE;
+         alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
         break;
       }
+
+   case (FX_UM_E0_MODULATE | FX_UM_E1_REPLACE): /* Homeworld2 */
+      {
+         tex1.FunctionRGB   = GR_COMBINE_FUNCTION_ZERO;
+         tex1.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex1.FunctionAlpha = GR_COMBINE_FUNCTION_ZERO;
+         tex1.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
+         tex0.FunctionRGB   = GR_COMBINE_FUNCTION_LOCAL;
+         tex0.FactorRGB     = GR_COMBINE_FACTOR_NONE;
+         tex0.FunctionAlpha = GR_COMBINE_FUNCTION_LOCAL;
+         tex0.FactorAlpha   = GR_COMBINE_FACTOR_NONE;
+
+         if (ifmt & (FX_UM_E0_RGB | FX_UM_E0_LUMINANCE)) {
+            alphaComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+            alphaComb.Factor   = GR_COMBINE_FACTOR_NONE;
+            alphaComb.Other    = GR_COMBINE_OTHER_NONE;
+         } else {
+            alphaComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+            alphaComb.Factor   = GR_COMBINE_FACTOR_ONE;
+            alphaComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+         }
+
+         if (ifmt & FX_UM_E0_ALPHA) {
+            colorComb.Function = GR_COMBINE_FUNCTION_LOCAL;
+            colorComb.Factor   = GR_COMBINE_FACTOR_NONE;
+            colorComb.Other    = GR_COMBINE_OTHER_NONE;
+         } else {
+            colorComb.Function = GR_COMBINE_FUNCTION_SCALE_OTHER;
+            colorComb.Factor   = GR_COMBINE_FACTOR_ONE;
+            colorComb.Other    = GR_COMBINE_OTHER_TEXTURE;
+         }
+         break;
+      }
    default:
-      fprintf(stderr, "Unexpected dual texture mode encountered\n");
-      break;
+      fprintf(stderr, "fxSetupTextureDoubleTMU_NoLock: Unexpected dual texture mode encountered\n");
+      return;
    }
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTextureDoubleTMU(...) End\n");
-   }
+   grAlphaCombine(alphaComb.Function,
+                  alphaComb.Factor,
+                  alphaComb.Local,
+                  alphaComb.Other,
+                  alphaComb.Invert);
+   grColorCombine(colorComb.Function,
+                  colorComb.Factor,
+                  colorComb.Local,
+                  colorComb.Other,
+                  colorComb.Invert);
+   grTexCombine(GR_TMU0,
+                tex0.FunctionRGB,
+                tex0.FactorRGB,
+                tex0.FunctionAlpha,
+                tex0.FactorAlpha,
+                tex0.InvertRGB,
+                tex0.InvertAlpha);
+   grTexCombine(GR_TMU1,
+                tex1.FunctionRGB,
+                tex1.FactorRGB,
+                tex1.FunctionAlpha,
+                tex1.FactorAlpha,
+                tex1.InvertRGB,
+                tex1.InvertAlpha);
 }
 
 /************************* No Texture ***************************/
@@ -1006,11 +1216,11 @@ fxSetupTextureDoubleTMU_NoLock(GLcontext * ctx)
 static void
 fxSetupTextureNone_NoLock(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    GrCombineLocal_t localc, locala;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTextureNone(...)\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupTextureNone_NoLock(...)\n");
    }
 
    if ((ctx->Light.ShadeModel == GL_SMOOTH) || 1 ||
@@ -1025,17 +1235,23 @@ fxSetupTextureNone_NoLock(GLcontext * ctx)
    else
       localc = GR_COMBINE_LOCAL_CONSTANT;
 
-   FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                           GR_COMBINE_FACTOR_NONE,
-                           locala, GR_COMBINE_OTHER_NONE, FXFALSE);
+   grAlphaCombine(GR_COMBINE_FUNCTION_LOCAL,
+                  GR_COMBINE_FACTOR_NONE,
+                  locala,
+                  GR_COMBINE_OTHER_NONE,
+                  FXFALSE);
 
-   FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
-                           GR_COMBINE_FACTOR_NONE,
-                           localc, GR_COMBINE_OTHER_NONE, FXFALSE);
+   grColorCombine(GR_COMBINE_FUNCTION_LOCAL,
+                  GR_COMBINE_FACTOR_NONE,
+                  localc,
+                  GR_COMBINE_OTHER_NONE,
+                  FXFALSE);
 
    fxMesa->lastUnitsMode = FX_UM_NONE;
 }
 
+#include "fxsetup.h"
+
 /************************************************************************/
 /************************** Texture Mode SetUp **************************/
 /************************************************************************/
@@ -1043,49 +1259,48 @@ fxSetupTextureNone_NoLock(GLcontext * ctx)
 static void
 fxSetupTexture_NoLock(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
-   GLuint tex2Denabled;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxSetupTexture(...)\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "fxSetupTexture_NoLock(...)\n");
    }
 
-   /* Disable multipass texturing.
-    */
-   ctx->Driver.MultipassFunc = 0;
-
-   /* Texture Combine, Color Combine and Alpha Combine.
-    */
-   tex2Denabled = (ctx->Texture._ReallyEnabled & TEXTURE0_2D);
-
-   if (fxMesa->emulateTwoTMUs)
-      tex2Denabled |= (ctx->Texture._ReallyEnabled & TEXTURE1_2D);
-
-   switch (tex2Denabled) {
-   case TEXTURE0_2D:
-      fxSetupTextureSingleTMU_NoLock(ctx, 0);
-      break;
-   case TEXTURE1_2D:
-      fxSetupTextureSingleTMU_NoLock(ctx, 1);
-      break;
-   case (TEXTURE0_2D | TEXTURE1_2D):
-      if (fxMesa->haveTwoTMUs)
-        fxSetupTextureDoubleTMU_NoLock(ctx);
+   if (fxMesa->HaveCmbExt) {
+      /* Texture Combine, Color Combine and Alpha Combine. */
+      if ((ctx->Texture.Unit[0]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) &&
+          (ctx->Texture.Unit[1]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) &&
+          fxMesa->haveTwoTMUs) {
+         fxSetupTextureDoubleTMUNapalm_NoLock(ctx);
+      }
+      else if (ctx->Texture.Unit[0]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) {
+         fxSetupTextureSingleTMUNapalm_NoLock(ctx, 0);
+      }
+      else if (ctx->Texture.Unit[1]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) {
+         fxSetupTextureSingleTMUNapalm_NoLock(ctx, 1);
+      }
       else {
-        if (MESA_VERBOSE & VERBOSE_DRIVER)
-           fprintf(stderr, "fxmesa: enabling fake multitexture\n");
-
-        fxSetupTextureSingleTMU_NoLock(ctx, 0);
-        ctx->Driver.MultipassFunc = fxMultipassTexture;
+         fxSetupTextureNoneNapalm_NoLock(ctx);
+      }
+   } else {
+      /* Texture Combine, Color Combine and Alpha Combine. */
+      if ((ctx->Texture.Unit[0]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) &&
+          (ctx->Texture.Unit[1]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) &&
+          fxMesa->haveTwoTMUs) {
+         fxSetupTextureDoubleTMU_NoLock(ctx);
+      }
+      else if (ctx->Texture.Unit[0]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) {
+         fxSetupTextureSingleTMU_NoLock(ctx, 0);
+      }
+      else if (ctx->Texture.Unit[1]._ReallyEnabled & (TEXTURE_1D_BIT|TEXTURE_2D_BIT)) {
+         fxSetupTextureSingleTMU_NoLock(ctx, 1);
+      }
+      else {
+         fxSetupTextureNone_NoLock(ctx);
       }
-      break;
-   default:
-      fxSetupTextureNone_NoLock(ctx);
-      break;
    }
 }
 
-static void
+void
 fxSetupTexture(GLcontext * ctx)
 {
    BEGIN_BOARD_LOCK();
@@ -1097,117 +1312,185 @@ fxSetupTexture(GLcontext * ctx)
 /**************************** Blend SetUp *******************************/
 /************************************************************************/
 
-/* XXX consider supporting GL_INGR_blend_func_separate */
 void
-fxDDBlendFunc(GLcontext * ctx, GLenum sfactor, GLenum dfactor)
+fxDDBlendFuncSeparate(GLcontext * ctx, GLenum sfactor, GLenum dfactor, GLenum asfactor, GLenum adfactor)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
+   GLboolean isNapalm = (fxMesa->type >= GR_SSTTYPE_Voodoo4);
+   GLboolean have32bpp = (fxMesa->colDepth == 32);
+   GLboolean haveAlpha = fxMesa->haveHwAlpha;
    GrAlphaBlendFnc_t sfact, dfact, asfact, adfact;
 
-   /* From the Glide documentation:
-      For alpha source and destination blend function factor
-      parameters, Voodoo Graphics supports only
-      GR_BLEND_ZERO and GR_BLEND_ONE.
+   /*
+    * 15/16 BPP alpha channel alpha blending modes
+    *   0x0    AZERO           Zero
+    *   0x4    AONE            One
+    *
+    * 32 BPP alpha channel alpha blending modes
+    *   0x0    AZERO           Zero
+    *   0x1    ASRC_ALPHA      Source alpha
+    *   0x3    ADST_ALPHA      Destination alpha
+    *   0x4    AONE            One
+    *   0x5    AOMSRC_ALPHA    1 - Source alpha
+    *   0x7    AOMDST_ALPHA    1 - Destination alpha
+    *
+    * If we don't have HW alpha buffer:
+    *   DST_ALPHA == 1
+    *   ONE_MINUS_DST_ALPHA == 0
+    * Unsupported modes are:
+    *   1 if used as src blending factor
+    *   0 if used as dst blending factor
     */
 
    switch (sfactor) {
    case GL_ZERO:
-      asfact = sfact = GR_BLEND_ZERO;
+      sfact = GR_BLEND_ZERO;
       break;
    case GL_ONE:
-      asfact = sfact = GR_BLEND_ONE;
+      sfact = GR_BLEND_ONE;
       break;
    case GL_DST_COLOR:
       sfact = GR_BLEND_DST_COLOR;
-      asfact = GR_BLEND_ONE;
       break;
    case GL_ONE_MINUS_DST_COLOR:
       sfact = GR_BLEND_ONE_MINUS_DST_COLOR;
-      asfact = GR_BLEND_ONE;
       break;
    case GL_SRC_ALPHA:
       sfact = GR_BLEND_SRC_ALPHA;
-      asfact = GR_BLEND_ONE;
       break;
    case GL_ONE_MINUS_SRC_ALPHA:
       sfact = GR_BLEND_ONE_MINUS_SRC_ALPHA;
-      asfact = GR_BLEND_ONE;
       break;
    case GL_DST_ALPHA:
-      sfact = GR_BLEND_DST_ALPHA;
-      asfact = GR_BLEND_ONE;
+      sfact = haveAlpha ? GR_BLEND_DST_ALPHA : GR_BLEND_ONE/*bad*/;
       break;
    case GL_ONE_MINUS_DST_ALPHA:
-      sfact = GR_BLEND_ONE_MINUS_DST_ALPHA;
-      asfact = GR_BLEND_ONE;
+      sfact = haveAlpha ? GR_BLEND_ONE_MINUS_DST_ALPHA : GR_BLEND_ZERO/*bad*/;
       break;
    case GL_SRC_ALPHA_SATURATE:
       sfact = GR_BLEND_ALPHA_SATURATE;
-      asfact = GR_BLEND_ONE;
       break;
    case GL_SRC_COLOR:
+      if (isNapalm) {
+         sfact = GR_BLEND_SAME_COLOR_EXT;
+         break;
+      }
    case GL_ONE_MINUS_SRC_COLOR:
-      /* USELESS */
-      asfact = sfact = GR_BLEND_ONE;
-      break;
+      if (isNapalm) {
+         sfact = GR_BLEND_ONE_MINUS_SAME_COLOR_EXT;
+         break;
+      }
    default:
-      asfact = sfact = GR_BLEND_ONE;
+      sfact = GR_BLEND_ONE;
       break;
    }
 
-   if ((sfact != us->blendSrcFuncRGB) || (asfact != us->blendSrcFuncAlpha)) {
-      us->blendSrcFuncRGB = sfact;
-      us->blendSrcFuncAlpha = asfact;
-      fxMesa->new_state |= FX_NEW_BLEND;
+   switch (asfactor) {
+   case GL_ZERO:
+      asfact = GR_BLEND_ZERO;
+      break;
+   case GL_ONE:
+      asfact = GR_BLEND_ONE;
+      break;
+   case GL_SRC_COLOR:
+   case GL_SRC_ALPHA:
+      asfact = have32bpp ? GR_BLEND_SRC_ALPHA : GR_BLEND_ONE/*bad*/;
+      break;
+   case GL_ONE_MINUS_SRC_COLOR:
+   case GL_ONE_MINUS_SRC_ALPHA:
+      asfact = have32bpp ? GR_BLEND_ONE_MINUS_SRC_ALPHA : GR_BLEND_ONE/*bad*/;
+      break;
+   case GL_DST_COLOR:
+   case GL_DST_ALPHA:
+      asfact = (have32bpp && haveAlpha) ? GR_BLEND_DST_ALPHA : GR_BLEND_ONE/*bad*/;
+      break;
+   case GL_ONE_MINUS_DST_COLOR:
+   case GL_ONE_MINUS_DST_ALPHA:
+      asfact = (have32bpp && haveAlpha) ? GR_BLEND_ONE_MINUS_DST_ALPHA : GR_BLEND_ZERO/*bad*/;
+      break;
+   case GL_SRC_ALPHA_SATURATE:
+      asfact = GR_BLEND_ONE;
+      break;
+   default:
+      asfact = GR_BLEND_ONE;
+      break;
    }
 
    switch (dfactor) {
    case GL_ZERO:
-      adfact = dfact = GR_BLEND_ZERO;
+      dfact = GR_BLEND_ZERO;
       break;
    case GL_ONE:
-      adfact = dfact = GR_BLEND_ONE;
+      dfact = GR_BLEND_ONE;
       break;
    case GL_SRC_COLOR:
       dfact = GR_BLEND_SRC_COLOR;
-      adfact = GR_BLEND_ZERO;
       break;
    case GL_ONE_MINUS_SRC_COLOR:
       dfact = GR_BLEND_ONE_MINUS_SRC_COLOR;
-      adfact = GR_BLEND_ZERO;
       break;
    case GL_SRC_ALPHA:
       dfact = GR_BLEND_SRC_ALPHA;
-      adfact = GR_BLEND_ZERO;
       break;
    case GL_ONE_MINUS_SRC_ALPHA:
       dfact = GR_BLEND_ONE_MINUS_SRC_ALPHA;
-      adfact = GR_BLEND_ZERO;
       break;
    case GL_DST_ALPHA:
-      /* dfact=GR_BLEND_DST_ALPHA; */
-      /* We can't do DST_ALPHA */
-      dfact = GR_BLEND_ONE;
-      adfact = GR_BLEND_ZERO;
+      dfact = haveAlpha ? GR_BLEND_DST_ALPHA : GR_BLEND_ONE/*bad*/;
       break;
    case GL_ONE_MINUS_DST_ALPHA:
-      /* dfact=GR_BLEND_ONE_MINUS_DST_ALPHA; */
-      /* We can't do DST_ALPHA */
+      dfact = haveAlpha ? GR_BLEND_ONE_MINUS_DST_ALPHA : GR_BLEND_ZERO/*bad*/;
+      break;
+   case GL_DST_COLOR:
+      if (isNapalm) {
+         dfact = GR_BLEND_SAME_COLOR_EXT;
+         break;
+      }
+   case GL_ONE_MINUS_DST_COLOR:
+      if (isNapalm) {
+         dfact = GR_BLEND_ONE_MINUS_SAME_COLOR_EXT;
+         break;
+      }
+   default:
       dfact = GR_BLEND_ZERO;
+      break;
+   }
+
+   switch (adfactor) {
+   case GL_ZERO:
       adfact = GR_BLEND_ZERO;
       break;
-   case GL_SRC_ALPHA_SATURATE:
+   case GL_ONE:
+      adfact = GR_BLEND_ONE;
+      break;
+   case GL_SRC_COLOR:
+   case GL_SRC_ALPHA:
+      adfact = have32bpp ? GR_BLEND_SRC_ALPHA : GR_BLEND_ZERO/*bad*/;
+      break;
+   case GL_ONE_MINUS_SRC_COLOR:
+   case GL_ONE_MINUS_SRC_ALPHA:
+      adfact = have32bpp ? GR_BLEND_ONE_MINUS_SRC_ALPHA : GR_BLEND_ZERO/*bad*/;
+      break;
    case GL_DST_COLOR:
+   case GL_DST_ALPHA:
+      adfact = (have32bpp && haveAlpha) ? GR_BLEND_DST_ALPHA : GR_BLEND_ONE/*bad*/;
+      break;
    case GL_ONE_MINUS_DST_COLOR:
-      /* USELESS */
-      adfact = dfact = GR_BLEND_ZERO;
+   case GL_ONE_MINUS_DST_ALPHA:
+      adfact = (have32bpp && haveAlpha) ? GR_BLEND_ONE_MINUS_DST_ALPHA : GR_BLEND_ZERO/*bad*/;
       break;
    default:
-      adfact = dfact = GR_BLEND_ZERO;
+      adfact = GR_BLEND_ZERO;
       break;
    }
 
+   if ((sfact != us->blendSrcFuncRGB) || (asfact != us->blendSrcFuncAlpha)) {
+      us->blendSrcFuncRGB = sfact;
+      us->blendSrcFuncAlpha = asfact;
+      fxMesa->new_state |= FX_NEW_BLEND;
+   }
+
    if ((dfact != us->blendDstFuncRGB) || (adfact != us->blendDstFuncAlpha)) {
       us->blendDstFuncRGB = dfact;
       us->blendDstFuncAlpha = adfact;
@@ -1215,18 +1498,77 @@ fxDDBlendFunc(GLcontext * ctx, GLenum sfactor, GLenum dfactor)
    }
 }
 
-static void
-fxSetupBlend(GLcontext * ctx)
+void
+fxDDBlendEquationSeparate(GLcontext * ctx, GLenum modeRGB, GLenum modeA)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
-   tfxUnitsState *us = &fxMesa->unitsState;
+ fxMesaContext fxMesa = FX_CONTEXT(ctx);
+ tfxUnitsState *us = &fxMesa->unitsState;
+ GrAlphaBlendOp_t q;
+
+ switch (modeRGB) {
+        case GL_FUNC_ADD:
+             q = GR_BLEND_OP_ADD;
+             break;
+        case GL_FUNC_SUBTRACT:
+             q = GR_BLEND_OP_SUB;
+             break;
+        case GL_FUNC_REVERSE_SUBTRACT:
+             q = GR_BLEND_OP_REVSUB;
+             break;
+        default:
+             q = us->blendEqRGB;
+ }
+ if (q != us->blendEqRGB) {
+    us->blendEqRGB = q;
+    fxMesa->new_state |= FX_NEW_BLEND;
+ }
+
+ switch (modeA) {
+        case GL_FUNC_ADD:
+             q = GR_BLEND_OP_ADD;
+             break;
+        case GL_FUNC_SUBTRACT:
+             q = GR_BLEND_OP_SUB;
+             break;
+        case GL_FUNC_REVERSE_SUBTRACT:
+             q = GR_BLEND_OP_REVSUB;
+             break;
+        default:
+             q = us->blendEqAlpha;
+ }
+ if (q != us->blendEqAlpha) {
+    us->blendEqAlpha = q;
+    fxMesa->new_state |= FX_NEW_BLEND;
+ }
+}
 
-   if (us->blendEnabled)
-      FX_grAlphaBlendFunction(us->blendSrcFuncRGB, us->blendDstFuncRGB,
-                             us->blendSrcFuncAlpha, us->blendDstFuncAlpha);
-   else
-      FX_grAlphaBlendFunction(GR_BLEND_ONE, GR_BLEND_ZERO, GR_BLEND_ONE,
-                             GR_BLEND_ZERO);
+void
+fxSetupBlend(GLcontext * ctx)
+{
+ fxMesaContext fxMesa = FX_CONTEXT(ctx);
+ tfxUnitsState *us = &fxMesa->unitsState;
+
+ if (fxMesa->HavePixExt) {
+    if (us->blendEnabled) {
+       fxMesa->Glide.grAlphaBlendFunctionExt(us->blendSrcFuncRGB, us->blendDstFuncRGB,
+                                             us->blendEqRGB,
+                                             us->blendSrcFuncAlpha, us->blendDstFuncAlpha,
+                                             us->blendEqAlpha);
+    } else {
+       fxMesa->Glide.grAlphaBlendFunctionExt(GR_BLEND_ONE, GR_BLEND_ZERO,
+                                             GR_BLEND_OP_ADD,
+                                             GR_BLEND_ONE, GR_BLEND_ZERO,
+                                             GR_BLEND_OP_ADD);
+    }
+ } else {
+    if (us->blendEnabled) {
+       grAlphaBlendFunction(us->blendSrcFuncRGB, us->blendDstFuncRGB,
+                            us->blendSrcFuncAlpha, us->blendDstFuncAlpha);
+    } else {
+       grAlphaBlendFunction(GR_BLEND_ONE, GR_BLEND_ZERO,
+                            GR_BLEND_ONE, GR_BLEND_ZERO);
+    }
+ }
 }
 
 /************************************************************************/
@@ -1234,50 +1576,17 @@ fxSetupBlend(GLcontext * ctx)
 /************************************************************************/
 
 void
-fxDDAlphaFunc(GLcontext * ctx, GLenum func, GLchan ref)
+fxDDAlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
-   GrCmpFnc_t newfunc;
-
-   switch (func) {
-   case GL_NEVER:
-      newfunc = GR_CMP_NEVER;
-      break;
-   case GL_LESS:
-      newfunc = GR_CMP_LESS;
-      break;
-   case GL_EQUAL:
-      newfunc = GR_CMP_EQUAL;
-      break;
-   case GL_LEQUAL:
-      newfunc = GR_CMP_LEQUAL;
-      break;
-   case GL_GREATER:
-      newfunc = GR_CMP_GREATER;
-      break;
-   case GL_NOTEQUAL:
-      newfunc = GR_CMP_NOTEQUAL;
-      break;
-   case GL_GEQUAL:
-      newfunc = GR_CMP_GEQUAL;
-      break;
-   case GL_ALWAYS:
-      newfunc = GR_CMP_ALWAYS;
-      break;
-   default:
-      fprintf(stderr, "fx Driver: internal error in fxDDAlphaFunc()\n");
-      fxCloseHardware();
-      exit(-1);
-      break;
-   }
-
-   if (newfunc != us->alphaTestFunc) {
-      us->alphaTestFunc = newfunc;
-      fxMesa->new_state |= FX_NEW_ALPHA;
-   }
 
-   if (ref != us->alphaTestRefValue) {
+   if (
+       (us->alphaTestFunc != func)
+       ||
+       (us->alphaTestRefValue != ref)
+      ) {
+      us->alphaTestFunc = func;
       us->alphaTestRefValue = ref;
       fxMesa->new_state |= FX_NEW_ALPHA;
    }
@@ -1286,15 +1595,16 @@ fxDDAlphaFunc(GLcontext * ctx, GLenum func, GLchan ref)
 static void
 fxSetupAlphaTest(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
 
    if (us->alphaTestEnabled) {
-      FX_grAlphaTestFunction(us->alphaTestFunc);
-      FX_grAlphaTestReferenceValue(us->alphaTestRefValue);
+      GrAlpha_t ref = (GLint) (us->alphaTestRefValue * 255.0);
+      grAlphaTestFunction(us->alphaTestFunc - GL_NEVER + GR_CMP_NEVER);
+      grAlphaTestReferenceValue(ref);
    }
    else
-      FX_grAlphaTestFunction(GR_CMP_ALWAYS);
+      grAlphaTestFunction(GR_CMP_ALWAYS);
 }
 
 /************************************************************************/
@@ -1304,53 +1614,19 @@ fxSetupAlphaTest(GLcontext * ctx)
 void
 fxDDDepthFunc(GLcontext * ctx, GLenum func)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
-   GrCmpFnc_t dfunc;
 
-   switch (func) {
-   case GL_NEVER:
-      dfunc = GR_CMP_NEVER;
-      break;
-   case GL_LESS:
-      dfunc = GR_CMP_LESS;
-      break;
-   case GL_GEQUAL:
-      dfunc = GR_CMP_GEQUAL;
-      break;
-   case GL_LEQUAL:
-      dfunc = GR_CMP_LEQUAL;
-      break;
-   case GL_GREATER:
-      dfunc = GR_CMP_GREATER;
-      break;
-   case GL_NOTEQUAL:
-      dfunc = GR_CMP_NOTEQUAL;
-      break;
-   case GL_EQUAL:
-      dfunc = GR_CMP_EQUAL;
-      break;
-   case GL_ALWAYS:
-      dfunc = GR_CMP_ALWAYS;
-      break;
-   default:
-      fprintf(stderr, "fx Driver: internal error in fxDDDepthFunc()\n");
-      fxCloseHardware();
-      exit(-1);
-      break;
-   }
-
-   if (dfunc != us->depthTestFunc) {
-      us->depthTestFunc = dfunc;
+   if (us->depthTestFunc != func) {
+      us->depthTestFunc = func;
       fxMesa->new_state |= FX_NEW_DEPTH;
    }
-
 }
 
 void
 fxDDDepthMask(GLcontext * ctx, GLboolean flag)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
 
    if (flag != us->depthMask) {
@@ -1359,19 +1635,170 @@ fxDDDepthMask(GLcontext * ctx, GLboolean flag)
    }
 }
 
-static void
+void
 fxSetupDepthTest(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
 
    if (us->depthTestEnabled) {
-      FX_grDepthBufferFunction(us->depthTestFunc);
-      FX_grDepthMask(us->depthMask);
+      grDepthBufferFunction(us->depthTestFunc - GL_NEVER + GR_CMP_NEVER);
+      grDepthMask(us->depthMask);
    }
    else {
-      FX_grDepthBufferFunction(GR_CMP_ALWAYS);
-      FX_grDepthMask(FXFALSE);
+      grDepthBufferFunction(GR_CMP_ALWAYS);
+      grDepthMask(FXFALSE);
+   }
+}
+
+/************************************************************************/
+/************************** Stencil SetUp *******************************/
+/************************************************************************/
+
+static GrStencil_t convertGLStencilOp( GLenum op )
+{
+   switch ( op ) {
+   case GL_KEEP:
+      return GR_STENCILOP_KEEP;
+   case GL_ZERO:
+      return GR_STENCILOP_ZERO;
+   case GL_REPLACE:
+      return GR_STENCILOP_REPLACE;
+   case GL_INCR:
+      return GR_STENCILOP_INCR_CLAMP;
+   case GL_DECR:
+      return GR_STENCILOP_DECR_CLAMP;
+   case GL_INVERT:
+      return GR_STENCILOP_INVERT;
+   case GL_INCR_WRAP_EXT:
+      return GR_STENCILOP_INCR_WRAP;
+   case GL_DECR_WRAP_EXT:
+      return GR_STENCILOP_DECR_WRAP;
+   default:
+      _mesa_problem( NULL, "bad stencil op in convertGLStencilOp" );
+   }
+   return GR_STENCILOP_KEEP;   /* never get, silence compiler warning */
+}
+
+void
+fxDDStencilFuncSeparate (GLcontext *ctx, GLenum face, GLenum func,
+                         GLint ref, GLuint mask)
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   tfxUnitsState *us = &fxMesa->unitsState;
+
+   if (ctx->Stencil.ActiveFace) {
+      return;
+   }
+
+   if (
+       (us->stencilFunction != func)
+       ||
+       (us->stencilRefValue != ref)
+       ||
+       (us->stencilValueMask != mask)
+      ) {
+      us->stencilFunction = func;
+      us->stencilRefValue = ref;
+      us->stencilValueMask = mask;
+      fxMesa->new_state |= FX_NEW_STENCIL;
+   }
+}
+
+void
+fxDDStencilMaskSeparate (GLcontext *ctx, GLenum face, GLuint mask)
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   tfxUnitsState *us = &fxMesa->unitsState;
+
+   if (ctx->Stencil.ActiveFace) {
+      return;
+   }
+
+   if (us->stencilWriteMask != mask) {
+      us->stencilWriteMask = mask;
+      fxMesa->new_state |= FX_NEW_STENCIL;
+   }
+}
+
+void
+fxDDStencilOpSeparate (GLcontext *ctx, GLenum face, GLenum sfail,
+                       GLenum zfail, GLenum zpass)
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   tfxUnitsState *us = &fxMesa->unitsState;
+
+   if (ctx->Stencil.ActiveFace) {
+      return;
+   }
+
+   if (
+       (us->stencilFailFunc != sfail)
+       ||
+       (us->stencilZFailFunc != zfail)
+       ||
+       (us->stencilZPassFunc != zpass)
+      ) {
+      us->stencilFailFunc = sfail;
+      us->stencilZFailFunc = zfail;
+      us->stencilZPassFunc = zpass;
+      fxMesa->new_state |= FX_NEW_STENCIL;
+   }
+}
+
+void
+fxSetupStencil (GLcontext * ctx)
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   tfxUnitsState *us = &fxMesa->unitsState;
+
+   if (us->stencilEnabled) {
+      GrCmpFnc_t stencilFailFunc = GR_STENCILOP_KEEP;
+      GrCmpFnc_t stencilZFailFunc = GR_STENCILOP_KEEP;
+      GrCmpFnc_t stencilZPassFunc = GR_STENCILOP_KEEP;
+      if (!fxMesa->multipass) {
+         stencilFailFunc = convertGLStencilOp(us->stencilFailFunc);
+         stencilZFailFunc = convertGLStencilOp(us->stencilZFailFunc);
+         stencilZPassFunc = convertGLStencilOp(us->stencilZPassFunc);
+      }
+      grEnable(GR_STENCIL_MODE_EXT);
+      fxMesa->Glide.grStencilOpExt(stencilFailFunc,
+                                   stencilZFailFunc,
+                                   stencilZPassFunc);
+      fxMesa->Glide.grStencilFuncExt(us->stencilFunction - GL_NEVER + GR_CMP_NEVER,
+                                     us->stencilRefValue,
+                                     us->stencilValueMask);
+      fxMesa->Glide.grStencilMaskExt(us->stencilWriteMask);
+   } else {
+      grDisable(GR_STENCIL_MODE_EXT);
+   }
+}
+
+void
+fxSetupStencilFace (GLcontext * ctx, GLint face)
+{
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   tfxUnitsState *us = &fxMesa->unitsState;
+
+   if (us->stencilEnabled) {
+      GrCmpFnc_t stencilFailFunc = GR_STENCILOP_KEEP;
+      GrCmpFnc_t stencilZFailFunc = GR_STENCILOP_KEEP;
+      GrCmpFnc_t stencilZPassFunc = GR_STENCILOP_KEEP;
+      if (!fxMesa->multipass) {
+         stencilFailFunc = convertGLStencilOp(ctx->Stencil.FailFunc[face]);
+         stencilZFailFunc = convertGLStencilOp(ctx->Stencil.ZFailFunc[face]);
+         stencilZPassFunc = convertGLStencilOp(ctx->Stencil.ZPassFunc[face]);
+      }
+      grEnable(GR_STENCIL_MODE_EXT);
+      fxMesa->Glide.grStencilOpExt(stencilFailFunc,
+                                   stencilZFailFunc,
+                                   stencilZPassFunc);
+      fxMesa->Glide.grStencilFuncExt(ctx->Stencil.Function[face] - GL_NEVER + GR_CMP_NEVER,
+                                     ctx->Stencil.Ref[face],
+                                     ctx->Stencil.ValueMask[face]);
+      fxMesa->Glide.grStencilMaskExt(ctx->Stencil.WriteMask[face]);
+   } else {
+      grDisable(GR_STENCIL_MODE_EXT);
    }
 }
 
@@ -1383,7 +1810,7 @@ void
 fxDDColorMask(GLcontext * ctx,
              GLboolean r, GLboolean g, GLboolean b, GLboolean a)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    fxMesa->new_state |= FX_NEW_COLOR_MASK;
    (void) r;
    (void) g;
@@ -1391,19 +1818,24 @@ fxDDColorMask(GLcontext * ctx,
    (void) a;
 }
 
-static void
+void
 fxSetupColorMask(GLcontext * ctx)
 {
    fxMesaContext fxMesa = FX_CONTEXT(ctx);
 
-   if (ctx->Color.DrawBuffer == GL_NONE) {
-      FX_grColorMask(FXFALSE, FXFALSE);
+   if (fxMesa->colDepth == 32) {
+      /* 32bpp mode */
+      fxMesa->Glide.grColorMaskExt(ctx->Color.ColorMask[RCOMP],
+                                   ctx->Color.ColorMask[GCOMP],
+                                   ctx->Color.ColorMask[BCOMP],
+                                   ctx->Color.ColorMask[ACOMP] && fxMesa->haveHwAlpha);
    }
    else {
-      FX_grColorMask(ctx->Color.ColorMask[RCOMP] ||
-                    ctx->Color.ColorMask[GCOMP] ||
-                    ctx->Color.ColorMask[BCOMP],
-                    ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
+      /* 15/16 bpp mode */
+      grColorMask(ctx->Color.ColorMask[RCOMP] |
+                  ctx->Color.ColorMask[GCOMP] |
+                  ctx->Color.ColorMask[BCOMP],
+                  ctx->Color.ColorMask[ACOMP] && fxMesa->haveHwAlpha);
    }
 }
 
@@ -1429,7 +1861,7 @@ fxSetupFog(GLcontext * ctx)
       col[1] = (unsigned int) (255 * ctx->Fog.Color[1]);
       col[2] = (unsigned int) (255 * ctx->Fog.Color[2]);
       col[3] = (unsigned int) (255 * ctx->Fog.Color[3]);
-      FX_grFogColorValue(FXCOLOR4(col));
+      grFogColorValue(FXCOLOR4(col));
 
       if (fxMesa->fogTableMode != ctx->Fog.Mode ||
          fxMesa->fogDensity != ctx->Fog.Density ||
@@ -1440,6 +1872,14 @@ fxSetupFog(GLcontext * ctx)
         case GL_LINEAR:
            guFogGenerateLinear(fxMesa->fogTable, ctx->Fog.Start,
                                ctx->Fog.End);
+           if (fxMesa->fogTable[0] > 63) {
+              /* [dBorca] Hack alert:
+               * As per Glide3 Programming Guide:
+               * The difference between consecutive fog values
+               * must be less than 64.
+               */
+              fxMesa->fogTable[0] = 63;
+           }
            break;
         case GL_EXP:
            guFogGenerateExp(fxMesa->fogTable, ctx->Fog.Density);
@@ -1456,11 +1896,19 @@ fxSetupFog(GLcontext * ctx)
         fxMesa->fogEnd = ctx->Fog.End;
       }
 
-      FX_grFogTable(fxMesa->fogTable);
-      FX_grFogMode(GR_FOG_WITH_TABLE);
+      grFogTable(fxMesa->fogTable);
+      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) {
+         grVertexLayout(GR_PARAM_FOG_EXT, GR_VERTEX_FOG_OFFSET << 2,
+                                          GR_PARAM_ENABLE);
+         grFogMode(GR_FOG_WITH_TABLE_ON_FOGCOORD_EXT);
+      } else {
+         grVertexLayout(GR_PARAM_FOG_EXT, GR_VERTEX_FOG_OFFSET << 2,
+                                          GR_PARAM_DISABLE);
+         grFogMode(GR_FOG_WITH_TABLE_ON_Q);
+      }
    }
    else {
-      FX_grFogMode(GR_FOG_DISABLE);
+      grFogMode(GR_FOG_DISABLE);
    }
 }
 
@@ -1468,6 +1916,25 @@ void
 fxDDFogfv(GLcontext * ctx, GLenum pname, const GLfloat * params)
 {
    FX_CONTEXT(ctx)->new_state |= FX_NEW_FOG;
+   switch (pname) {
+      case GL_FOG_COORDINATE_SOURCE_EXT: {
+         GLenum p = (GLenum)*params;
+         if (p == GL_FOG_COORDINATE_EXT) {
+            _swrast_allow_vertex_fog(ctx, GL_TRUE);
+            _swrast_allow_pixel_fog(ctx, GL_FALSE);
+            _tnl_allow_vertex_fog( ctx, GL_TRUE);
+            _tnl_allow_pixel_fog( ctx, GL_FALSE);
+         } else {
+            _swrast_allow_vertex_fog(ctx, GL_FALSE);
+            _swrast_allow_pixel_fog(ctx, GL_TRUE);
+            _tnl_allow_vertex_fog( ctx, GL_FALSE);
+            _tnl_allow_pixel_fog( ctx, GL_TRUE);
+         }
+         break;
+      }
+      default:
+         ;
+   }
 }
 
 /************************************************************************/
@@ -1478,36 +1945,40 @@ fxDDFogfv(GLcontext * ctx, GLenum pname, const GLfloat * params)
 void
 fxSetScissorValues(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    int xmin, xmax;
-   int ymin, ymax, check;
+   int ymin, ymax;
 
    if (ctx->Scissor.Enabled) {
       xmin = ctx->Scissor.X;
       xmax = ctx->Scissor.X + ctx->Scissor.Width;
       ymin = ctx->Scissor.Y;
       ymax = ctx->Scissor.Y + ctx->Scissor.Height;
-      check = 1;
+
+      if (xmin < 0)
+         xmin = 0;
+      if (xmax > fxMesa->width)
+         xmax = fxMesa->width;
+      if (ymin < fxMesa->screen_height - fxMesa->height)
+         ymin = fxMesa->screen_height - fxMesa->height;
+      if (ymax > fxMesa->screen_height - 0)
+         ymax = fxMesa->screen_height - 0;
    }
    else {
       xmin = 0;
       ymin = 0;
       xmax = fxMesa->width;
       ymax = fxMesa->height;
-      check = 0;
    }
-   if (xmin < fxMesa->clipMinX)
-      xmin = fxMesa->clipMinX;
-   if (xmax > fxMesa->clipMaxX)
-      xmax = fxMesa->clipMaxX;
-   if (ymin < fxMesa->screen_height - fxMesa->clipMaxY)
-      ymin = fxMesa->screen_height - fxMesa->clipMaxY;
-   if (ymax > fxMesa->screen_height - fxMesa->clipMinY)
-      ymax = fxMesa->screen_height - fxMesa->clipMinY;
-   FX_grClipWindow_NoLock(xmin, ymin, xmax, ymax);
+
+   fxMesa->clipMinX = xmin;
+   fxMesa->clipMinY = ymin;
+   fxMesa->clipMaxX = xmax;
+   fxMesa->clipMaxY = ymax;
+   grClipWindow(xmin, ymin, xmax, ymax);
 }
 
-static void
+void
 fxSetupScissor(GLcontext * ctx)
 {
    BEGIN_BOARD_LOCK();
@@ -1541,34 +2012,39 @@ fxDDFrontFace(GLcontext * ctx, GLenum mode)
 }
 
 
-static void
+void
 fxSetupCull(GLcontext * ctx)
 {
-   if (ctx->Polygon.CullFlag) {
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
+   GrCullMode_t mode = GR_CULL_DISABLE;
+
+   if (ctx->Polygon.CullFlag && (fxMesa->raster_primitive == GL_TRIANGLES)) {
       switch (ctx->Polygon.CullFaceMode) {
       case GL_BACK:
         if (ctx->Polygon.FrontFace == GL_CCW)
-           FX_CONTEXT(ctx)->cullMode = GR_CULL_NEGATIVE;
+           mode = GR_CULL_NEGATIVE;
         else
-           FX_CONTEXT(ctx)->cullMode = GR_CULL_POSITIVE;
+           mode = GR_CULL_POSITIVE;
         break;
       case GL_FRONT:
         if (ctx->Polygon.FrontFace == GL_CCW)
-           FX_CONTEXT(ctx)->cullMode = GR_CULL_POSITIVE;
+           mode = GR_CULL_POSITIVE;
         else
-           FX_CONTEXT(ctx)->cullMode = GR_CULL_NEGATIVE;
+           mode = GR_CULL_NEGATIVE;
         break;
       case GL_FRONT_AND_BACK:
-        FX_CONTEXT(ctx)->cullMode = GR_CULL_DISABLE;
-        break;
+        /* Handled as a fallback on triangles in tdfx_tris.c */
+        return;
       default:
+        ASSERT(0);
         break;
       }
    }
-   else
-      FX_CONTEXT(ctx)->cullMode = GR_CULL_DISABLE;
 
-   FX_grCullMode(FX_CONTEXT(ctx)->cullMode);
+   if (fxMesa->cullMode != mode) {
+      fxMesa->cullMode = mode;
+      grCullMode(mode);
+   }
 }
 
 
@@ -1579,11 +2055,12 @@ fxSetupCull(GLcontext * ctx)
 void
 fxDDEnable(GLcontext * ctx, GLenum cap, GLboolean state)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    tfxUnitsState *us = &fxMesa->unitsState;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER) {
-      fprintf(stderr, "fxmesa: fxDDEnable(...)\n");
+   if (TDFX_DEBUG & VERBOSE_DRIVER) {
+      fprintf(stderr, "%s(%s)\n", state ? "fxDDEnable" : "fxDDDisable",
+             _mesa_lookup_enum_by_nr(cap));
    }
 
    switch (cap) {
@@ -1605,12 +2082,18 @@ fxDDEnable(GLcontext * ctx, GLenum cap, GLboolean state)
         fxMesa->new_state |= FX_NEW_DEPTH;
       }
       break;
+   case GL_STENCIL_TEST:
+      if (fxMesa->haveHwStencil && state != us->stencilEnabled) {
+        us->stencilEnabled = state;
+        fxMesa->new_state |= FX_NEW_STENCIL;
+      }
+      break;
    case GL_DITHER:
       if (state) {
-        FX_grDitherMode(GR_DITHER_4x4);
+        grDitherMode(GR_DITHER_4x4);
       }
       else {
-        FX_grDitherMode(GR_DITHER_DISABLE);
+        grDitherMode(GR_DITHER_DISABLE);
       }
       break;
    case GL_SCISSOR_TEST:
@@ -1629,6 +2112,7 @@ fxDDEnable(GLcontext * ctx, GLenum cap, GLboolean state)
    case GL_LINE_STIPPLE:
    case GL_POINT_SMOOTH:
    case GL_POLYGON_SMOOTH:
+   case GL_TEXTURE_1D:
    case GL_TEXTURE_2D:
       fxMesa->new_state |= FX_NEW_TEXTURING;
       break;
@@ -1637,129 +2121,7 @@ fxDDEnable(GLcontext * ctx, GLenum cap, GLboolean state)
    }
 }
 
-#if 0
-/*
-  Multipass to do GL_BLEND texture functions
-  Cf*(1-Ct) has already been written to the buffer during the first pass
-  Cc*Ct gets written during the second pass (in this function)
-  Everything gets reset in the third call (in this function)
-*/
-static GLboolean
-fxMultipassBlend(struct vertex_buffer *VB, GLuint pass)
-{
-   GLcontext *ctx = VB->ctx;
-   fxMesaContext fxMesa = FX_CONTEXT(ctx);
 
-   switch (pass) {
-   case 1:
-      /* Add Cc*Ct */
-      fxMesa->restoreUnitsState = fxMesa->unitsState;
-      if (ctx->Depth.Mask) {
-        /* We don't want to check or change the depth buffers */
-        switch (ctx->Depth.Func) {
-        case GL_NEVER:
-        case GL_ALWAYS:
-           break;
-        default:
-           fxDDDepthFunc(ctx, GL_EQUAL);
-           break;
-        }
-        fxDDDepthMask(ctx, FALSE);
-      }
-      /* Enable Cc*Ct mode */
-      /* XXX Set the Constant Color ? */
-      fxDDEnable(ctx, GL_BLEND, GL_TRUE);
-      fxDDBlendFunc(ctx, XXX, XXX);
-      fxSetupTextureSingleTMU(ctx, XXX);
-      fxSetupBlend(ctx);
-      fxSetupDepthTest(ctx);
-      break;
-
-   case 2:
-      /* Reset everything back to normal */
-      fxMesa->unitsState = fxMesa->restoreUnitsState;
-      fxMesa->setup_gone |= XXX;
-      fxSetupTextureSingleTMU(ctx, XXX);
-      fxSetupBlend(ctx);
-      fxSetupDepthTest(ctx);
-      break;
-   }
-
-   return pass == 1;
-}
-#endif
-
-/************************************************************************/
-/******************** Fake Multitexture Support *************************/
-/************************************************************************/
-
-/* Its considered cheeky to try to fake ARB multitexture by doing
- * multipass rendering, because it is not possible to emulate the full
- * spec in this way.  The fact is that the voodoo 2 supports only a
- * subset of the possible multitexturing modes, and it is possible to
- * support almost the same subset using multipass blending on the
- * voodoo 1.  In all other cases for both voodoo 1 and 2, we fall back
- * to software rendering, satisfying the spec if not the user.  
- */
-static GLboolean
-fxMultipassTexture(GLcontext * ctx, GLuint pass)
-{
-   fxMesaContext fxMesa = FX_CONTEXT(ctx);
-   TNLcontext *tnl = TNL_CONTEXT(ctx);
-   fxVertex *v = fxMesa->verts;
-   fxVertex *last = fxMesa->verts + tnl->vb.Count;
-
-   switch (pass) {
-   case 1:
-      if (MESA_VERBOSE &
-         (VERBOSE_DRIVER | VERBOSE_PIPELINE | VERBOSE_TEXTURE))
-           fprintf(stderr, "fxmesa: Second texture pass\n");
-
-      for (; v != last; v++) {
-        v->f[S0COORD] = v->f[S1COORD];
-        v->f[T0COORD] = v->f[T1COORD];
-      }
-
-      fxMesa->restoreUnitsState = fxMesa->unitsState;
-      fxMesa->tmu_source[0] = 1;
-
-      if (ctx->Depth.Mask) {
-        switch (ctx->Depth.Func) {
-        case GL_NEVER:
-        case GL_ALWAYS:
-           break;
-        default:
-           fxDDDepthFunc(ctx, GL_EQUAL);
-           break;
-        }
-
-        fxDDDepthMask(ctx, GL_FALSE);
-      }
-
-      if (ctx->Texture.Unit[1].EnvMode == GL_MODULATE) {
-        fxDDEnable(ctx, GL_BLEND, GL_TRUE);
-        fxDDBlendFunc(ctx, GL_DST_COLOR, GL_ZERO);
-      }
-
-      fxSetupTextureSingleTMU(ctx, 1);
-      fxSetupBlend(ctx);
-      fxSetupDepthTest(ctx);
-      break;
-
-   case 2:
-      /* Restore original state.  
-       */
-      fxMesa->tmu_source[0] = 0;
-      fxMesa->unitsState = fxMesa->restoreUnitsState;
-      fxMesa->setup_gone |= SETUP_TMU0;
-      fxSetupTextureSingleTMU(ctx, 0);
-      fxSetupBlend(ctx);
-      fxSetupDepthTest(ctx);
-      break;
-   }
-
-   return pass == 1;
-}
 
 
 /************************************************************************/
@@ -1784,7 +2146,7 @@ static void
 fx_print_state_flags(const char *msg, GLuint flags)
 {
    fprintf(stderr,
-          "%s: (0x%x) %s%s%s%s%s%s%s\n",
+          "%s: (0x%x) %s%s%s%s%s%s%s%s\n",
           msg,
           flags,
           (flags & FX_NEW_TEXTURING) ? "texture, " : "",
@@ -1793,17 +2155,18 @@ fx_print_state_flags(const char *msg, GLuint flags)
           (flags & FX_NEW_FOG) ? "fog, " : "",
           (flags & FX_NEW_SCISSOR) ? "scissor, " : "",
           (flags & FX_NEW_COLOR_MASK) ? "colormask, " : "",
-          (flags & FX_NEW_CULL) ? "cull, " : "");
+          (flags & FX_NEW_CULL) ? "cull, " : "",
+          (flags & FX_NEW_STENCIL) ? "stencil, " : "");
 }
 
 void
 fxSetupFXUnits(GLcontext * ctx)
 {
-   fxMesaContext fxMesa = (fxMesaContext) ctx->DriverCtx;
+   fxMesaContext fxMesa = FX_CONTEXT(ctx);
    GLuint newstate = fxMesa->new_state;
 
-   if (MESA_VERBOSE & VERBOSE_DRIVER)
-      fx_print_state_flags("fxmesa: fxSetupFXUnits", newstate);
+   if (TDFX_DEBUG & VERBOSE_DRIVER)
+      fx_print_state_flags("fxSetupFXUnits", newstate);
 
    if (newstate) {
       if (newstate & FX_NEW_TEXTURING)
@@ -1818,6 +2181,9 @@ fxSetupFXUnits(GLcontext * ctx)
       if (newstate & FX_NEW_DEPTH)
         fxSetupDepthTest(ctx);
 
+      if (newstate & FX_NEW_STENCIL)
+        fxSetupStencil(ctx);
+
       if (newstate & FX_NEW_FOG)
         fxSetupFog(ctx);
 
@@ -1830,9 +2196,6 @@ fxSetupFXUnits(GLcontext * ctx)
       if (newstate & FX_NEW_CULL)
         fxSetupCull(ctx);
 
-      fxMesa->draw_point = fxMesa->initial_point;
-      fxMesa->draw_line = fxMesa->initial_line;
-      fxMesa->draw_tri = fxMesa->initial_tri;
       fxMesa->new_state = 0;
    }
 }