s/BlendEquatioRGB/BlendEquationRGB/
[mesa.git] / src / mesa / swrast / s_texture.c
index f5f4bb5d6324ec47f81adf1bc8ce6c598ed93b08..b5a4509c07ea91cd320683c81bc7cf48a508bbd6 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_texture.c,v 1.60 2002/04/12 21:17:28 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  4.1
+ * Version:  6.1
  *
- * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  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"),
@@ -29,8 +27,7 @@
 #include "context.h"
 #include "colormac.h"
 #include "macros.h"
-#include "mmath.h"
-#include "mem.h"
+#include "imports.h"
 #include "texformat.h"
 #include "teximage.h"
 
 #define WEIGHT_SHIFT 16
 
 
+/*
+ * Compute the remainder of a divided by b, but be careful with
+ * negative values so that GL_REPEAT mode works right.
+ */
+static INLINE GLint
+repeat_remainder(GLint a, GLint b)
+{
+   if (a >= 0)
+      return a % b;
+   else
+      return (a + 1) % b + b - 1;
+}
+
+
 /*
  * Used to compute texel locations for linear sampling.
  * Input:
- *    wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER_ARB
+ *    wrapMode = GL_REPEAT, GL_CLAMP, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
  *    S = texcoord in [0,1]
  *    SIZE = width (or height or depth) of texture
  * Output:
 {                                                                      \
    if (wrapMode == GL_REPEAT) {                                                \
       U = S * SIZE - 0.5F;                                             \
-      I0 = IFLOOR(U) & (SIZE - 1);                                     \
-      I1 = (I0 + 1) & (SIZE - 1);                                      \
+      if (tObj->_IsPowerOfTwo) {                                       \
+         I0 = IFLOOR(U) & (SIZE - 1);                                  \
+         I1 = (I0 + 1) & (SIZE - 1);                                   \
+      }                                                                        \
+      else {                                                           \
+         I0 = repeat_remainder(IFLOOR(U), SIZE);                       \
+         I1 = repeat_remainder(I0 + 1, SIZE);                          \
+      }                                                                        \
    }                                                                   \
    else if (wrapMode == GL_CLAMP_TO_EDGE) {                            \
       if (S <= 0.0F)                                                   \
@@ -78,7 +95,7 @@
       if (I1 >= (GLint) SIZE)                                          \
          I1 = SIZE - 1;                                                        \
    }                                                                   \
-   else  if (wrapMode == GL_CLAMP_TO_BORDER_ARB) {                     \
+   else if (wrapMode == GL_CLAMP_TO_BORDER) {                          \
       const GLfloat min = -1.0F / (2.0F * SIZE);                       \
       const GLfloat max = 1.0F - min;                                  \
       if (S <= min)                                                    \
       I0 = IFLOOR(U);                                                  \
       I1 = I0 + 1;                                                     \
    }                                                                   \
-   else if (wrapMode == GL_MIRRORED_REPEAT_ARB) {                      \
+   else if (wrapMode == GL_MIRRORED_REPEAT) {                          \
       const GLint flr = IFLOOR(S);                                     \
       if (flr & 1)                                                     \
          U = 1.0F - (S - (GLfloat) flr);       /* flr is odd */        \
       else                                                             \
          U = S - (GLfloat) flr;                /* flr is even */               \
+      U = (U * SIZE) - 0.5F;                                           \
+      I0 = IFLOOR(U);                                                  \
+      I1 = I0 + 1;                                                     \
+      if (I0 < 0)                                                      \
+         I0 = 0;                                                       \
+      if (I1 >= (GLint) SIZE)                                          \
+         I1 = SIZE - 1;                                                        \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_EXT) {                         \
+      U = (GLfloat) fabs(S);                                           \
+      if (U >= 1.0F)                                                   \
+         U = (GLfloat) SIZE;                                           \
+      else                                                             \
+         U *= SIZE;                                                    \
+      U -= 0.5F;                                                       \
+      I0 = IFLOOR(U);                                                  \
+      I1 = I0 + 1;                                                     \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_EXT) {                 \
+      U = (GLfloat) fabs(S);                                           \
+      if (U >= 1.0F)                                                   \
+         U = (GLfloat) SIZE;                                           \
+      else                                                             \
+         U *= SIZE;                                                    \
+      U -= 0.5F;                                                       \
       I0 = IFLOOR(U);                                                  \
       I1 = I0 + 1;                                                     \
       if (I0 < 0)                                                      \
       if (I1 >= (GLint) SIZE)                                          \
          I1 = SIZE - 1;                                                        \
    }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_BORDER_EXT) {               \
+      const GLfloat min = -1.0F / (2.0F * SIZE);                       \
+      const GLfloat max = 1.0F - min;                                  \
+      U = (GLfloat) fabs(S);                                           \
+      if (U <= min)                                                    \
+         U = min * SIZE;                                               \
+      else if (U >= max)                                               \
+         U = max * SIZE;                                               \
+      else                                                             \
+         U *= SIZE;                                                    \
+      U -= 0.5F;                                                       \
+      I0 = IFLOOR(U);                                                  \
+      I1 = I0 + 1;                                                     \
+   }                                                                   \
    else {                                                              \
       ASSERT(wrapMode == GL_CLAMP);                                    \
       if (S <= 0.0F)                                                   \
       /* s limited to [0,1) */                                         \
       /* i limited to [0,size-1] */                                    \
       I = IFLOOR(S * SIZE);                                            \
-      I &= (SIZE - 1);                                                 \
+      if (tObj->_IsPowerOfTwo)                                         \
+         I &= (SIZE - 1);                                              \
+      else                                                             \
+         I = repeat_remainder(I, SIZE);                                        \
    }                                                                   \
    else if (wrapMode == GL_CLAMP_TO_EDGE) {                            \
       /* s limited to [min,max] */                                     \
       else                                                             \
          I = IFLOOR(S * SIZE);                                         \
    }                                                                   \
-   else if (wrapMode == GL_CLAMP_TO_BORDER_ARB) {                      \
+   else if (wrapMode == GL_CLAMP_TO_BORDER) {                          \
       /* s limited to [min,max] */                                     \
       /* i limited to [-1, size] */                                    \
       const GLfloat min = -1.0F / (2.0F * SIZE);                       \
       else                                                             \
          I = IFLOOR(S * SIZE);                                         \
    }                                                                   \
-   else if (wrapMode == GL_MIRRORED_REPEAT_ARB) {                      \
+   else if (wrapMode == GL_MIRRORED_REPEAT) {                          \
       const GLfloat min = 1.0F / (2.0F * SIZE);                                \
       const GLfloat max = 1.0F - min;                                  \
       const GLint flr = IFLOOR(S);                                     \
       else                                                             \
          I = IFLOOR(u * SIZE);                                         \
    }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_EXT) {                         \
+      /* s limited to [0,1] */                                         \
+      /* i limited to [0,size-1] */                                    \
+      const GLfloat u = (GLfloat) fabs(S);                             \
+      if (u <= 0.0F)                                                   \
+         I = 0;                                                                \
+      else if (u >= 1.0F)                                              \
+         I = SIZE - 1;                                                 \
+      else                                                             \
+         I = IFLOOR(u * SIZE);                                         \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_EXT) {                 \
+      /* s limited to [min,max] */                                     \
+      /* i limited to [0, size-1] */                                   \
+      const GLfloat min = 1.0F / (2.0F * SIZE);                                \
+      const GLfloat max = 1.0F - min;                                  \
+      const GLfloat u = (GLfloat) fabs(S);                             \
+      if (u < min)                                                     \
+         I = 0;                                                                \
+      else if (u > max)                                                        \
+         I = SIZE - 1;                                                 \
+      else                                                             \
+         I = IFLOOR(u * SIZE);                                         \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_BORDER_EXT) {               \
+      /* s limited to [min,max] */                                     \
+      /* i limited to [0, size-1] */                                   \
+      const GLfloat min = -1.0F / (2.0F * SIZE);                       \
+      const GLfloat max = 1.0F - min;                                  \
+      const GLfloat u = (GLfloat) fabs(S);                             \
+      if (u < min)                                                     \
+         I = -1;                                                       \
+      else if (u > max)                                                        \
+         I = SIZE;                                                     \
+      else                                                             \
+         I = IFLOOR(u * SIZE);                                         \
+   }                                                                   \
    else {                                                              \
       ASSERT(wrapMode == GL_CLAMP);                                    \
       /* s limited to [0,1] */                                         \
 }
 
 
+/* Power of two image sizes only */
 #define COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(S, U, SIZE, I0, I1)       \
 {                                                                      \
    U = S * SIZE - 0.5F;                                                        \
 #define K1BIT  32
 
 
+/*
+ * Do the lookup for GL_SGI_texture_color_table.
+ */
+void
+_swrast_texture_table_lookup(const struct gl_color_table *table,
+                             GLuint n, GLchan rgba[][4])
+{
+   if (!table->Table || table->Size == 0)
+      return;
+
+   switch (table->Format) {
+      case GL_INTENSITY:
+         /* replace RGBA with I */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+               GLchan c;
+               CLAMPED_FLOAT_TO_CHAN(c, lut[j]);
+               rgba[i][RCOMP] = rgba[i][GCOMP] =
+                  rgba[i][BCOMP] = rgba[i][ACOMP] = c;
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  const GLchan c = lut[rgba[i][RCOMP]];
+                  rgba[i][RCOMP] = rgba[i][GCOMP] =
+                     rgba[i][BCOMP] = rgba[i][ACOMP] = c;
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+                  rgba[i][RCOMP] = rgba[i][GCOMP] =
+                     rgba[i][BCOMP] = rgba[i][ACOMP] = lut[j];
+               }
+            }
+         }
+         break;
+      case GL_LUMINANCE:
+         /* replace RGB with L */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+               GLchan c;
+               CLAMPED_FLOAT_TO_CHAN(c, lut[j]);
+               rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = c;
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  const GLchan c = lut[rgba[i][RCOMP]];
+                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = c;
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = lut[j];
+               }
+            }
+         }
+         break;
+      case GL_ALPHA:
+         /* replace A with A */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+               GLchan c;
+               CLAMPED_FLOAT_TO_CHAN(c, lut[j]);
+               rgba[i][ACOMP] = c;
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+                  rgba[i][ACOMP] = lut[j];
+               }
+            }
+         }
+         break;
+      case GL_LUMINANCE_ALPHA:
+         /* replace RGBA with LLLA */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+               GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+               GLchan luminance, alpha;
+               CLAMPED_FLOAT_TO_CHAN(luminance, lut[jL * 2 + 0]);
+               CLAMPED_FLOAT_TO_CHAN(alpha, lut[jA * 2 + 1]);
+               rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = luminance;
+               rgba[i][ACOMP] = alpha;;
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLchan l = lut[rgba[i][RCOMP] * 2 + 0];
+                  GLchan a = lut[rgba[i][ACOMP] * 2 + 1];;
+                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = l;
+                  rgba[i][ACOMP] = a;
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+                  GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+                  GLchan luminance = lut[jL * 2 + 0];
+                  GLchan alpha     = lut[jA * 2 + 1];
+                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = luminance;
+                  rgba[i][ACOMP] = alpha;
+               }
+            }
+         }
+         break;
+      case GL_RGB:
+         /* replace RGB with RGB */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+               GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
+               GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 3 + 0]);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 3 + 1]);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 3 + 2]);
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 3 + 0];
+                  rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 3 + 1];
+                  rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+                  GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
+                  GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
+                  rgba[i][RCOMP] = lut[jR * 3 + 0];
+                  rgba[i][GCOMP] = lut[jG * 3 + 1];
+                  rgba[i][BCOMP] = lut[jB * 3 + 2];
+               }
+            }
+         }
+         break;
+      case GL_RGBA:
+         /* replace RGBA with RGBA */
+         if (table->FloatTable) {
+            const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+            const GLfloat *lut = (const GLfloat *) table->Table;
+            GLuint i;
+            for (i = 0; i < n; i++) {
+               GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+               GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
+               GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
+               GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
+               CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
+            }
+         }
+         else {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
+               /* common case */
+               const GLchan *lut = (const GLchan *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 4 + 0];
+                  rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 4 + 1];
+                  rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 4 + 2];
+                  rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
+               }
+            }
+            else
+#endif
+            {
+               const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
+               const GLfloat *lut = (const GLfloat *) table->Table;
+               GLuint i;
+               for (i = 0; i < n; i++) {
+                  GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
+                  GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
+                  GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
+                  GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
+                  CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
+                  CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
+                  CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
+                  CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
+               }
+            }
+         }
+         break;
+      default:
+         _mesa_problem(NULL, "Bad format in _swrast_texture_table_lookup");
+         return;
+   }
+}
+
+
 
 /*
  * Get texture palette entry.
@@ -416,11 +783,11 @@ sample_1d_nearest(GLcontext *ctx,
    i += img->Border;
 
    if (i < 0 || i >= (GLint) img->Width) {
-      /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      /* Need this test for GL_CLAMP_TO_BORDER mode */
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
-      (*img->FetchTexel)(img, i, 0, 0, (GLvoid *) rgba);
+      img->FetchTexelc(img, i, 0, 0, rgba);
       if (img->Format == GL_COLOR_INDEX) {
          palette_sample(ctx, tObj, rgba[0], rgba);
       }
@@ -469,19 +836,19 @@ sample_1d_linear(GLcontext *ctx,
       GLchan t0[4], t1[4];  /* texels */
 
       if (useBorderColor & I0BIT) {
-         COPY_CHAN4(t0, tObj->BorderColor);
+         COPY_CHAN4(t0, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, 0, 0, (GLvoid *) t0);
+         img->FetchTexelc(img, i0, 0, 0, t0);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t0[0], t0);
          }
       }
       if (useBorderColor & I1BIT) {
-         COPY_CHAN4(t1, tObj->BorderColor);
+         COPY_CHAN4(t1, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, 0, 0, (GLvoid *) t1);
+         img->FetchTexelc(img, i1, 0, 0, t1);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t1[0], t1);
          }
@@ -511,7 +878,7 @@ sample_1d_linear(GLcontext *ctx,
 static void
 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
-                                 GLuint n, GLfloat texcoord[][4],
+                                 GLuint n, const GLfloat texcoord[][4],
                                  const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -519,7 +886,7 @@ sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_1d_nearest(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_1d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -527,7 +894,7 @@ sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
 static void
 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -535,7 +902,7 @@ sample_1d_linear_mipmap_nearest(GLcontext *ctx,
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_1d_linear(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_1d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -554,7 +921,7 @@ sample_1d_linear_mipmap_nearest(GLcontext *ctx,
 static void
 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -563,14 +930,14 @@ sample_1d_nearest_mipmap_linear(GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_1d_nearest(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                            texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];
          const GLfloat f = FRAC(lambda[i]);
-         sample_1d_nearest(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_1d_nearest(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_1d_nearest(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_1d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -584,7 +951,7 @@ sample_1d_nearest_mipmap_linear(GLcontext *ctx,
 static void
 sample_1d_linear_mipmap_linear(GLcontext *ctx,
                                const struct gl_texture_object *tObj,
-                               GLuint n, GLfloat texcoord[][4],
+                               GLuint n, const GLfloat texcoord[][4],
                                const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -593,14 +960,14 @@ sample_1d_linear_mipmap_linear(GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_1d_linear(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                           texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];
          const GLfloat f = FRAC(lambda[i]);
-         sample_1d_linear(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_1d_linear(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_1d_linear(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_1d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -614,11 +981,11 @@ sample_1d_linear_mipmap_linear(GLcontext *ctx,
 static void
 sample_nearest_1d( GLcontext *ctx, GLuint texUnit,
                    const struct gl_texture_object *tObj, GLuint n,
-                   GLfloat texcoords[][4], const GLfloat lambda[],
+                   const GLfloat texcoords[][4], const GLfloat lambda[],
                    GLchan rgba[][4] )
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -630,11 +997,11 @@ sample_nearest_1d( GLcontext *ctx, GLuint texUnit,
 static void
 sample_linear_1d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4], const GLfloat lambda[],
+                  const GLfloat texcoords[][4], const GLfloat lambda[],
                   GLchan rgba[][4] )
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -650,7 +1017,7 @@ sample_linear_1d( GLcontext *ctx, GLuint texUnit,
 static void
 sample_lambda_1d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4],
+                  const GLfloat texcoords[][4],
                   const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint minStart, minEnd;  /* texels with minification */
@@ -667,12 +1034,12 @@ sample_lambda_1d( GLcontext *ctx, GLuint texUnit,
       switch (tObj->MinFilter) {
       case GL_NEAREST:
          for (i = minStart; i < minEnd; i++)
-            sample_1d_nearest(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                               texcoords[i], rgba[i]);
          break;
       case GL_LINEAR:
          for (i = minStart; i < minEnd; i++)
-            sample_1d_linear(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                              texcoords[i], rgba[i]);
          break;
       case GL_NEAREST_MIPMAP_NEAREST:
@@ -702,12 +1069,12 @@ sample_lambda_1d( GLcontext *ctx, GLuint texUnit,
       switch (tObj->MagFilter) {
       case GL_NEAREST:
          for (i = magStart; i < magEnd; i++)
-            sample_1d_nearest(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_1d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                               texcoords[i], rgba[i]);
          break;
       case GL_LINEAR:
          for (i = magStart; i < magEnd; i++)
-            sample_1d_linear(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_1d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                              texcoords[i], rgba[i]);
          break;
       default:
@@ -745,11 +1112,11 @@ sample_2d_nearest(GLcontext *ctx,
    j += img->Border;
 
    if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
-      /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      /* Need this test for GL_CLAMP_TO_BORDER mode */
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
-      (*img->FetchTexel)(img, i, j, 0, (GLvoid *) rgba);
+      img->FetchTexelc(img, i, j, 0, rgba);
       if (img->Format == GL_COLOR_INDEX) {
          palette_sample(ctx, tObj, rgba[0], rgba);
       }
@@ -814,37 +1181,37 @@ sample_2d_linear(GLcontext *ctx,
       GLchan t11[4];
 
       if (useBorderColor & (I0BIT | J0BIT)) {
-         COPY_CHAN4(t00, tObj->BorderColor);
+         COPY_CHAN4(t00, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j0, 0, (GLvoid *) t00);
+         img->FetchTexelc(img, i0, j0, 0, t00);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t00[0], t00);
          }
       }
       if (useBorderColor & (I1BIT | J0BIT)) {
-         COPY_CHAN4(t10, tObj->BorderColor);
+         COPY_CHAN4(t10, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j0, 0, (GLvoid *) t10);
+         img->FetchTexelc(img, i1, j0, 0, t10);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t10[0], t10);
          }
       }
       if (useBorderColor & (I0BIT | J1BIT)) {
-         COPY_CHAN4(t01, tObj->BorderColor);
+         COPY_CHAN4(t01, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j1, 0, (GLvoid *) t01);
+         img->FetchTexelc(img, i0, j1, 0, t01);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t01[0], t01);
          }
       }
       if (useBorderColor & (I1BIT | J1BIT)) {
-         COPY_CHAN4(t11, tObj->BorderColor);
+         COPY_CHAN4(t11, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j1, 0, (GLvoid *) t11);
+         img->FetchTexelc(img, i1, j1, 0, t11);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t11[0], t11);
          }
@@ -899,6 +1266,7 @@ sample_2d_linear_repeat(GLcontext *ctx,
    ASSERT(tObj->WrapT == GL_REPEAT);
    ASSERT(img->Border == 0);
    ASSERT(img->Format != GL_COLOR_INDEX);
+   ASSERT(img->_IsPowerOfTwo);
 
    COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord[0], u, width,  i0, i1);
    COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(texcoord[1], v, height, j0, j1);
@@ -924,10 +1292,10 @@ sample_2d_linear_repeat(GLcontext *ctx,
       GLchan t01[4];
       GLchan t11[4];
 
-      (*img->FetchTexel)(img, i0, j0, 0, (GLvoid *) t00);
-      (*img->FetchTexel)(img, i1, j0, 0, (GLvoid *) t10);
-      (*img->FetchTexel)(img, i0, j1, 0, (GLvoid *) t01);
-      (*img->FetchTexel)(img, i1, j1, 0, (GLvoid *) t11);
+      img->FetchTexelc(img, i0, j0, 0, t00);
+      img->FetchTexelc(img, i1, j0, 0, t10);
+      img->FetchTexelc(img, i0, j1, 0, t01);
+      img->FetchTexelc(img, i1, j1, 0, t11);
 
 #if CHAN_TYPE == GL_FLOAT
       rgba[0] = w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0];
@@ -963,14 +1331,14 @@ sample_2d_linear_repeat(GLcontext *ctx,
 static void
 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
-                                 GLuint n, GLfloat texcoord[][4],
+                                 GLuint n, const GLfloat texcoord[][4],
                                  const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_2d_nearest(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_2d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -979,7 +1347,7 @@ sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
 static void
 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -987,7 +1355,7 @@ sample_2d_linear_mipmap_nearest(GLcontext *ctx,
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_2d_linear(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_2d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -996,7 +1364,7 @@ sample_2d_linear_mipmap_nearest(GLcontext *ctx,
 static void
 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1005,14 +1373,14 @@ sample_2d_nearest_mipmap_linear(GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_2d_nearest(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_2d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                            texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_2d_nearest(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_2d_nearest(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_2d_nearest(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_2d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -1027,7 +1395,7 @@ sample_2d_nearest_mipmap_linear(GLcontext *ctx,
 static void
 sample_2d_linear_mipmap_linear( GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
@@ -1036,14 +1404,14 @@ sample_2d_linear_mipmap_linear( GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_2d_linear(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_2d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                           texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_2d_linear(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_2d_linear(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_2d_linear(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_2d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -1056,25 +1424,26 @@ sample_2d_linear_mipmap_linear( GLcontext *ctx,
 static void
 sample_2d_linear_mipmap_linear_repeat( GLcontext *ctx,
                                        const struct gl_texture_object *tObj,
-                                       GLuint n, GLfloat texcoord[][4],
+                                       GLuint n, const GLfloat texcoord[][4],
                                        const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
    ASSERT(lambda != NULL);
    ASSERT(tObj->WrapS == GL_REPEAT);
    ASSERT(tObj->WrapT == GL_REPEAT);
+   ASSERT(tObj->_IsPowerOfTwo);
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_2d_linear_repeat(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                                  texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_2d_linear_repeat(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_2d_linear_repeat(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_2d_linear_repeat(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -1087,11 +1456,11 @@ sample_2d_linear_mipmap_linear_repeat( GLcontext *ctx,
 static void
 sample_nearest_2d( GLcontext *ctx, GLuint texUnit,
                    const struct gl_texture_object *tObj, GLuint n,
-                   GLfloat texcoords[][4],
+                   const GLfloat texcoords[][4],
                    const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -1103,11 +1472,11 @@ sample_nearest_2d( GLcontext *ctx, GLuint texUnit,
 static void
 sample_linear_2d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4],
+                  const GLfloat texcoords[][4],
                   const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -1119,16 +1488,17 @@ sample_linear_2d( GLcontext *ctx, GLuint texUnit,
  * Optimized 2-D texture sampling:
  *    S and T wrap mode == GL_REPEAT
  *    GL_NEAREST min/mag filter
- *    No border
+ *    No border, 
+ *    RowStride == Width,
  *    Format = GL_RGB
  */
 static void
 opt_sample_rgb_2d( GLcontext *ctx, GLuint texUnit,
                    const struct gl_texture_object *tObj,
-                   GLuint n, GLfloat texcoords[][4],
+                   GLuint n, const GLfloat texcoords[][4],
                    const GLfloat lambda[], GLchan rgba[][4] )
 {
-   const struct gl_texture_image *img = tObj->Image[tObj->BaseLevel];
+   const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
    const GLfloat width = (GLfloat) img->Width;
    const GLfloat height = (GLfloat) img->Height;
    const GLint colMask = img->Width - 1;
@@ -1140,6 +1510,7 @@ opt_sample_rgb_2d( GLcontext *ctx, GLuint texUnit,
    ASSERT(tObj->WrapT==GL_REPEAT);
    ASSERT(img->Border==0);
    ASSERT(img->Format==GL_RGB);
+   ASSERT(img->_IsPowerOfTwo);
 
    for (k=0; k<n; k++) {
       GLint i = IFLOOR(texcoords[k][0] * width) & colMask;
@@ -1158,15 +1529,16 @@ opt_sample_rgb_2d( GLcontext *ctx, GLuint texUnit,
  *    S and T wrap mode == GL_REPEAT
  *    GL_NEAREST min/mag filter
  *    No border
+ *    RowStride == Width,
  *    Format = GL_RGBA
  */
 static void
 opt_sample_rgba_2d( GLcontext *ctx, GLuint texUnit,
                     const struct gl_texture_object *tObj,
-                    GLuint n, GLfloat texcoords[][4],
+                    GLuint n, const GLfloat texcoords[][4],
                     const GLfloat lambda[], GLchan rgba[][4] )
 {
-   const struct gl_texture_image *img = tObj->Image[tObj->BaseLevel];
+   const struct gl_texture_image *img = tObj->Image[0][tObj->BaseLevel];
    const GLfloat width = (GLfloat) img->Width;
    const GLfloat height = (GLfloat) img->Height;
    const GLint colMask = img->Width - 1;
@@ -1178,6 +1550,7 @@ opt_sample_rgba_2d( GLcontext *ctx, GLuint texUnit,
    ASSERT(tObj->WrapT==GL_REPEAT);
    ASSERT(img->Border==0);
    ASSERT(img->Format==GL_RGBA);
+   ASSERT(img->_IsPowerOfTwo);
 
    for (i = 0; i < n; i++) {
       const GLint col = IFLOOR(texcoords[i][0] * width) & colMask;
@@ -1196,17 +1569,18 @@ opt_sample_rgba_2d( GLcontext *ctx, GLuint texUnit,
 static void
 sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj,
-                  GLuint n, GLfloat texcoords[][4],
+                  GLuint n, const GLfloat texcoords[][4],
                   const GLfloat lambda[], GLchan rgba[][4] )
 {
-   const struct gl_texture_image *tImg = tObj->Image[tObj->BaseLevel];
+   const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
 
-   const GLboolean repeatNoBorder = (tObj->WrapS == GL_REPEAT)
+   const GLboolean repeatNoBorderPOT = (tObj->WrapS == GL_REPEAT)
       && (tObj->WrapT == GL_REPEAT)
-      && (tImg->Border == 0)
-      && (tImg->Format != GL_COLOR_INDEX);
+      && (tImg->Border == 0 && (tImg->Width == tImg->RowStride))
+      && (tImg->Format != GL_COLOR_INDEX)
+      && tImg->_IsPowerOfTwo;
 
    ASSERT(lambda != NULL);
    compute_min_mag_ranges(SWRAST_CONTEXT(ctx)->_MinMagThresh[texUnit],
@@ -1217,7 +1591,7 @@ sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
       const GLuint m = minEnd - minStart;
       switch (tObj->MinFilter) {
       case GL_NEAREST:
-         if (repeatNoBorder) {
+         if (repeatNoBorderPOT) {
             switch (tImg->Format) {
             case GL_RGB:
                opt_sample_rgb_2d(ctx, texUnit, tObj, m, texcoords + minStart,
@@ -1242,7 +1616,8 @@ sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
                          NULL, rgba + minStart);
          break;
       case GL_NEAREST_MIPMAP_NEAREST:
-         sample_2d_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
+         sample_2d_nearest_mipmap_nearest(ctx, tObj, m,
+                                          texcoords + minStart,
                                           lambda + minStart, rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_NEAREST:
@@ -1254,7 +1629,7 @@ sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
                                          lambda + minStart, rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_LINEAR:
-         if (repeatNoBorder)
+         if (repeatNoBorderPOT)
             sample_2d_linear_mipmap_linear_repeat(ctx, tObj, m,
                   texcoords + minStart, lambda + minStart, rgba + minStart);
          else
@@ -1273,7 +1648,7 @@ sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
 
       switch (tObj->MagFilter) {
       case GL_NEAREST:
-         if (repeatNoBorder) {
+         if (repeatNoBorderPOT) {
             switch (tImg->Format) {
             case GL_RGB:
                opt_sample_rgb_2d(ctx, texUnit, tObj, m, texcoords + magStart,
@@ -1331,11 +1706,11 @@ sample_3d_nearest(GLcontext *ctx,
    if (i < 0 || i >= (GLint) img->Width ||
        j < 0 || j >= (GLint) img->Height ||
        k < 0 || k >= (GLint) img->Depth) {
-      /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      /* Need this test for GL_CLAMP_TO_BORDER mode */
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
-      (*img->FetchTexel)(img, i, j, k, (GLvoid *) rgba);
+      img->FetchTexelc(img, i, j, k, rgba);
       if (img->Format == GL_COLOR_INDEX) {
          palette_sample(ctx, tObj, rgba[0], rgba);
       }
@@ -1415,74 +1790,74 @@ sample_3d_linear(GLcontext *ctx,
       GLchan t100[4], t110[4], t101[4], t111[4];
 
       if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
-         COPY_CHAN4(t000, tObj->BorderColor);
+         COPY_CHAN4(t000, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j0, k0, (GLvoid *) t000);
+         img->FetchTexelc(img, i0, j0, k0, t000);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t000[0], t000);
          }
       }
       if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
-         COPY_CHAN4(t100, tObj->BorderColor);
+         COPY_CHAN4(t100, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j0, k0, (GLvoid *) t100);
+         img->FetchTexelc(img, i1, j0, k0, t100);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t100[0], t100);
          }
       }
       if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
-         COPY_CHAN4(t010, tObj->BorderColor);
+         COPY_CHAN4(t010, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j1, k0, (GLvoid *) t010);
+         img->FetchTexelc(img, i0, j1, k0, t010);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t010[0], t010);
          }
       }
       if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
-         COPY_CHAN4(t110, tObj->BorderColor);
+         COPY_CHAN4(t110, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j1, k0, (GLvoid *) t110);
+         img->FetchTexelc(img, i1, j1, k0, t110);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t110[0], t110);
          }
       }
 
       if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
-         COPY_CHAN4(t001, tObj->BorderColor);
+         COPY_CHAN4(t001, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j0, k1, (GLvoid *) t001);
+         img->FetchTexelc(img, i0, j0, k1, t001);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t001[0], t001);
          }
       }
       if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
-         COPY_CHAN4(t101, tObj->BorderColor);
+         COPY_CHAN4(t101, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j0, k1, (GLvoid *) t101);
+         img->FetchTexelc(img, i1, j0, k1, t101);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t101[0], t101);
          }
       }
       if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
-         COPY_CHAN4(t011, tObj->BorderColor);
+         COPY_CHAN4(t011, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i0, j1, k1, (GLvoid *) t011);
+         img->FetchTexelc(img, i0, j1, k1, t011);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t011[0], t011);
          }
       }
       if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
-         COPY_CHAN4(t111, tObj->BorderColor);
+         COPY_CHAN4(t111, tObj->_BorderChan);
       }
       else {
-         (*img->FetchTexel)(img, i1, j1, k1, (GLvoid *) t111);
+         img->FetchTexelc(img, i1, j1, k1, t111);
          if (img->Format == GL_COLOR_INDEX) {
             palette_sample(ctx, tObj, t111[0], t111);
          }
@@ -1541,14 +1916,14 @@ sample_3d_linear(GLcontext *ctx,
 static void
 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
-                                 GLuint n, GLfloat texcoord[][4],
+                                 GLuint n, const GLfloat texcoord[][4],
                                  const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_3d_nearest(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_3d_nearest(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -1556,7 +1931,7 @@ sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
 static void
 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1564,7 +1939,7 @@ sample_3d_linear_mipmap_nearest(GLcontext *ctx,
    for (i = 0; i < n; i++) {
       GLint level;
       COMPUTE_NEAREST_MIPMAP_LEVEL(tObj, lambda[i], level);
-      sample_3d_linear(ctx, tObj, tObj->Image[level], texcoord[i], rgba[i]);
+      sample_3d_linear(ctx, tObj, tObj->Image[0][level], texcoord[i], rgba[i]);
    }
 }
 
@@ -1572,7 +1947,7 @@ sample_3d_linear_mipmap_nearest(GLcontext *ctx,
 static void
 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
-                                GLuint n, GLfloat texcoord[][4],
+                                GLuint n, const GLfloat texcoord[][4],
                                 const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1581,14 +1956,14 @@ sample_3d_nearest_mipmap_linear(GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_3d_nearest(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                            texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_3d_nearest(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_3d_nearest(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_3d_nearest(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_3d_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -1601,7 +1976,7 @@ sample_3d_nearest_mipmap_linear(GLcontext *ctx,
 static void
 sample_3d_linear_mipmap_linear(GLcontext *ctx,
                                const struct gl_texture_object *tObj,
-                               GLuint n, GLfloat texcoord[][4],
+                               GLuint n, const GLfloat texcoord[][4],
                                const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1610,14 +1985,14 @@ sample_3d_linear_mipmap_linear(GLcontext *ctx,
       GLint level;
       COMPUTE_LINEAR_MIPMAP_LEVEL(tObj, lambda[i], level);
       if (level >= tObj->_MaxLevel) {
-         sample_3d_linear(ctx, tObj, tObj->Image[tObj->_MaxLevel],
+         sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->_MaxLevel],
                           texcoord[i], rgba[i]);
       }
       else {
          GLchan t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_3d_linear(ctx, tObj, tObj->Image[level  ], texcoord[i], t0);
-         sample_3d_linear(ctx, tObj, tObj->Image[level+1], texcoord[i], t1);
+         sample_3d_linear(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
+         sample_3d_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
          rgba[i][RCOMP] = CHAN_CAST ((1.0F-f) * t0[RCOMP] + f * t1[RCOMP]);
          rgba[i][GCOMP] = CHAN_CAST ((1.0F-f) * t0[GCOMP] + f * t1[GCOMP]);
          rgba[i][BCOMP] = CHAN_CAST ((1.0F-f) * t0[BCOMP] + f * t1[BCOMP]);
@@ -1630,11 +2005,11 @@ sample_3d_linear_mipmap_linear(GLcontext *ctx,
 static void
 sample_nearest_3d(GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4], const GLfloat lambda[],
+                  const GLfloat texcoords[][4], const GLfloat lambda[],
                   GLchan rgba[][4])
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -1646,11 +2021,11 @@ sample_nearest_3d(GLcontext *ctx, GLuint texUnit,
 static void
 sample_linear_3d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4],
+                  const GLfloat texcoords[][4],
                  const GLfloat lambda[], GLchan rgba[][4] )
 {
    GLuint i;
-   struct gl_texture_image *image = tObj->Image[tObj->BaseLevel];
+   struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
    for (i=0;i<n;i++) {
       sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
@@ -1665,7 +2040,7 @@ sample_linear_3d( GLcontext *ctx, GLuint texUnit,
 static void
 sample_lambda_3d( GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                  GLfloat texcoords[][4], const GLfloat lambda[],
+                  const GLfloat texcoords[][4], const GLfloat lambda[],
                   GLchan rgba[][4] )
 {
    GLuint minStart, minEnd;  /* texels with minification */
@@ -1682,12 +2057,12 @@ sample_lambda_3d( GLcontext *ctx, GLuint texUnit,
       switch (tObj->MinFilter) {
       case GL_NEAREST:
          for (i = minStart; i < minEnd; i++)
-            sample_3d_nearest(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                               texcoords[i], rgba[i]);
          break;
       case GL_LINEAR:
          for (i = minStart; i < minEnd; i++)
-            sample_3d_linear(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                              texcoords[i], rgba[i]);
          break;
       case GL_NEAREST_MIPMAP_NEAREST:
@@ -1717,12 +2092,12 @@ sample_lambda_3d( GLcontext *ctx, GLuint texUnit,
       switch (tObj->MagFilter) {
       case GL_NEAREST:
          for (i = magStart; i < magEnd; i++)
-            sample_3d_nearest(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_3d_nearest(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                               texcoords[i], rgba[i]);
          break;
       case GL_LINEAR:
          for (i = magStart; i < magEnd; i++)
-            sample_3d_linear(ctx, tObj, tObj->Image[tObj->BaseLevel],
+            sample_3d_linear(ctx, tObj, tObj->Image[0][tObj->BaseLevel],
                              texcoords[i], rgba[i]);
          break;
       default:
@@ -1761,18 +2136,18 @@ choose_cube_face(const struct gl_texture_object *texObj,
    const GLfloat ry = texcoord[1];
    const GLfloat rz = texcoord[2];
    const struct gl_texture_image **imgArray;
-   const GLfloat arx = ABSF(rx),   ary = ABSF(ry),   arz = ABSF(rz);
+   const GLfloat arx = FABSF(rx),   ary = FABSF(ry),   arz = FABSF(rz);
    GLfloat sc, tc, ma;
 
    if (arx > ary && arx > arz) {
       if (rx >= 0.0F) {
-         imgArray = (const struct gl_texture_image **) texObj->Image;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_POS_X];
          sc = -rz;
          tc = -ry;
          ma = arx;
       }
       else {
-         imgArray = (const struct gl_texture_image **) texObj->NegX;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_NEG_X];
          sc = rz;
          tc = -ry;
          ma = arx;
@@ -1780,13 +2155,13 @@ choose_cube_face(const struct gl_texture_object *texObj,
    }
    else if (ary > arx && ary > arz) {
       if (ry >= 0.0F) {
-         imgArray = (const struct gl_texture_image **) texObj->PosY;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_POS_Y];
          sc = rx;
          tc = rz;
          ma = ary;
       }
       else {
-         imgArray = (const struct gl_texture_image **) texObj->NegY;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_NEG_Y];
          sc = rx;
          tc = -rz;
          ma = ary;
@@ -1794,13 +2169,13 @@ choose_cube_face(const struct gl_texture_object *texObj,
    }
    else {
       if (rz > 0.0F) {
-         imgArray = (const struct gl_texture_image **) texObj->PosZ;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_POS_Z];
          sc = rx;
          tc = -ry;
          ma = arz;
       }
       else {
-         imgArray = (const struct gl_texture_image **) texObj->NegZ;
+         imgArray = (const struct gl_texture_image **) texObj->Image[FACE_NEG_Z];
          sc = -rx;
          tc = -ry;
          ma = arz;
@@ -1816,7 +2191,7 @@ choose_cube_face(const struct gl_texture_object *texObj,
 static void
 sample_nearest_cube(GLcontext *ctx, GLuint texUnit,
                    const struct gl_texture_object *tObj, GLuint n,
-                    GLfloat texcoords[][4], const GLfloat lambda[],
+                    const GLfloat texcoords[][4], const GLfloat lambda[],
                     GLchan rgba[][4])
 {
    GLuint i;
@@ -1834,7 +2209,7 @@ sample_nearest_cube(GLcontext *ctx, GLuint texUnit,
 static void
 sample_linear_cube(GLcontext *ctx, GLuint texUnit,
                   const struct gl_texture_object *tObj, GLuint n,
-                   GLfloat texcoords[][4],
+                   const GLfloat texcoords[][4],
                   const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1850,9 +2225,9 @@ sample_linear_cube(GLcontext *ctx, GLuint texUnit,
 
 
 static void
-sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
+sample_cube_nearest_mipmap_nearest(GLcontext *ctx, GLuint texUnit,
                                    const struct gl_texture_object *tObj,
-                                   GLuint n, GLfloat texcoord[][4],
+                                   GLuint n, const GLfloat texcoord[][4],
                                    const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1869,9 +2244,9 @@ sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
 
 
 static void
-sample_cube_linear_mipmap_nearest(GLcontext *ctx,
+sample_cube_linear_mipmap_nearest(GLcontext *ctx, GLuint texUnit,
                                   const struct gl_texture_object *tObj,
-                                  GLuint n, GLfloat texcoord[][4],
+                                  GLuint n, const GLfloat texcoord[][4],
                                   const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1888,9 +2263,9 @@ sample_cube_linear_mipmap_nearest(GLcontext *ctx,
 
 
 static void
-sample_cube_nearest_mipmap_linear(GLcontext *ctx,
+sample_cube_nearest_mipmap_linear(GLcontext *ctx, GLuint texUnit,
                                   const struct gl_texture_object *tObj,
-                                  GLuint n, GLfloat texcoord[][4],
+                                  GLuint n, const GLfloat texcoord[][4],
                                   const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1920,9 +2295,9 @@ sample_cube_nearest_mipmap_linear(GLcontext *ctx,
 
 
 static void
-sample_cube_linear_mipmap_linear(GLcontext *ctx,
+sample_cube_linear_mipmap_linear(GLcontext *ctx, GLuint texUnit,
                                  const struct gl_texture_object *tObj,
-                                 GLuint n, GLfloat texcoord[][4],
+                                 GLuint n, const GLfloat texcoord[][4],
                                  const GLfloat lambda[], GLchan rgba[][4])
 {
    GLuint i;
@@ -1954,7 +2329,7 @@ sample_cube_linear_mipmap_linear(GLcontext *ctx,
 static void
 sample_lambda_cube( GLcontext *ctx, GLuint texUnit,
                    const struct gl_texture_object *tObj, GLuint n,
-                   GLfloat texcoords[][4], const GLfloat lambda[],
+                   const GLfloat texcoords[][4], const GLfloat lambda[],
                    GLchan rgba[][4])
 {
    GLuint minStart, minEnd;  /* texels with minification */
@@ -1977,19 +2352,23 @@ sample_lambda_cube( GLcontext *ctx, GLuint texUnit,
                             lambda + minStart, rgba + minStart);
          break;
       case GL_NEAREST_MIPMAP_NEAREST:
-         sample_cube_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
+         sample_cube_nearest_mipmap_nearest(ctx, texUnit, tObj, m,
+                                            texcoords + minStart,
                                            lambda + minStart, rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_NEAREST:
-         sample_cube_linear_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
+         sample_cube_linear_mipmap_nearest(ctx, texUnit, tObj, m,
+                                           texcoords + minStart,
                                            lambda + minStart, rgba + minStart);
          break;
       case GL_NEAREST_MIPMAP_LINEAR:
-         sample_cube_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
+         sample_cube_nearest_mipmap_linear(ctx, texUnit, tObj, m,
+                                           texcoords + minStart,
                                            lambda + minStart, rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_LINEAR:
-         sample_cube_linear_mipmap_linear(ctx, tObj, m, texcoords + minStart,
+         sample_cube_linear_mipmap_linear(ctx, texUnit, tObj, m,
+                                          texcoords + minStart,
                                           lambda + minStart, rgba + minStart);
          break;
       default:
@@ -2016,27 +2395,216 @@ sample_lambda_cube( GLcontext *ctx, GLuint texUnit,
 }
 
 
+/**********************************************************************/
+/*               Texture Rectangle Sampling Functions                 */
+/**********************************************************************/
+
+static void
+sample_nearest_rect(GLcontext *ctx, GLuint texUnit,
+                   const struct gl_texture_object *tObj, GLuint n,
+                    const GLfloat texcoords[][4], const GLfloat lambda[],
+                    GLchan rgba[][4])
+{
+   const struct gl_texture_image *img = tObj->Image[0][0];
+   const GLfloat width = (GLfloat) img->Width;
+   const GLfloat height = (GLfloat) img->Height;
+   const GLint width_minus_1 = img->Width - 1;
+   const GLint height_minus_1 = img->Height - 1;
+   GLuint i;
+
+   (void) texUnit;
+   (void) lambda;
+
+   ASSERT(tObj->WrapS == GL_CLAMP ||
+          tObj->WrapS == GL_CLAMP_TO_EDGE ||
+          tObj->WrapS == GL_CLAMP_TO_BORDER);
+   ASSERT(tObj->WrapT == GL_CLAMP ||
+          tObj->WrapT == GL_CLAMP_TO_EDGE ||
+          tObj->WrapT == GL_CLAMP_TO_BORDER);
+   ASSERT(img->Format != GL_COLOR_INDEX);
+
+   /* XXX move Wrap mode tests outside of loops for common cases */
+   for (i = 0; i < n; i++) {
+      GLint row, col;
+      /* NOTE: we DO NOT use [0, 1] texture coordinates! */
+      if (tObj->WrapS == GL_CLAMP) {
+         col = IFLOOR( CLAMP(texcoords[i][0], 0.0F, width) );
+      }
+      else if (tObj->WrapS == GL_CLAMP_TO_EDGE) {
+         col = IFLOOR( CLAMP(texcoords[i][0], 0.5F, width - 0.5F) );
+      }
+      else {
+         col = IFLOOR( CLAMP(texcoords[i][0], -0.5F, width + 0.5F) );
+      }
+      if (tObj->WrapT == GL_CLAMP) {
+         row = IFLOOR( CLAMP(texcoords[i][1], 0.0F, height) );
+      }
+      else if (tObj->WrapT == GL_CLAMP_TO_EDGE) {
+         row = IFLOOR( CLAMP(texcoords[i][1], 0.5F, height - 0.5F) );
+      }
+      else {
+         row = IFLOOR( CLAMP(texcoords[i][1], -0.5F, height + 0.5F) );
+      }
+
+      col = CLAMP(col, 0, width_minus_1);
+      row = CLAMP(row, 0, height_minus_1);
+
+      img->FetchTexelc(img, col, row, 0, rgba[i]);
+   }
+}
+
+
+static void
+sample_linear_rect(GLcontext *ctx, GLuint texUnit,
+                  const struct gl_texture_object *tObj, GLuint n,
+                   const GLfloat texcoords[][4],
+                  const GLfloat lambda[], GLchan rgba[][4])
+{
+   const struct gl_texture_image *img = tObj->Image[0][0];
+   const GLfloat width = (GLfloat) img->Width;
+   const GLfloat height = (GLfloat) img->Height;
+   const GLint width_minus_1 = img->Width - 1;
+   const GLint height_minus_1 = img->Height - 1;
+   GLuint i;
+
+   (void) texUnit;
+   (void) lambda;
+
+   ASSERT(tObj->WrapS == GL_CLAMP ||
+          tObj->WrapS == GL_CLAMP_TO_EDGE ||
+          tObj->WrapS == GL_CLAMP_TO_BORDER);
+   ASSERT(tObj->WrapT == GL_CLAMP ||
+          tObj->WrapT == GL_CLAMP_TO_EDGE ||
+          tObj->WrapT == GL_CLAMP_TO_BORDER);
+   ASSERT(img->Format != GL_COLOR_INDEX);
+
+   /* XXX lots of opportunity for optimization in this loop */
+   for (i = 0; i < n; i++) {
+      GLfloat frow, fcol;
+      GLint row0, col0, row1, col1;
+      GLchan t00[4], t01[4], t10[4], t11[4];
+      GLfloat a, b, w00, w01, w10, w11;
+
+      /* NOTE: we DO NOT use [0, 1] texture coordinates! */
+      if (tObj->WrapS == GL_CLAMP) {
+         fcol = CLAMP(texcoords[i][0], 0.0F, width);
+      }
+      else if (tObj->WrapS == GL_CLAMP_TO_EDGE) {
+         fcol = CLAMP(texcoords[i][0], 0.5F, width - 0.5F);
+      }
+      else {
+         fcol = CLAMP(texcoords[i][0], -0.5F, width + 0.5F);
+      }
+      if (tObj->WrapT == GL_CLAMP) {
+         frow = CLAMP(texcoords[i][1], 0.0F, height);
+      }
+      else if (tObj->WrapT == GL_CLAMP_TO_EDGE) {
+         frow = CLAMP(texcoords[i][1], 0.5F, height - 0.5F);
+      }
+      else {
+         frow = CLAMP(texcoords[i][1], -0.5F, height + 0.5F);
+      }
+
+      /* compute integer rows/columns */
+      col0 = IFLOOR(fcol);
+      col1 = col0 + 1;
+      col0 = CLAMP(col0, 0, width_minus_1);
+      col1 = CLAMP(col1, 0, width_minus_1);
+      row0 = IFLOOR(frow);
+      row1 = row0 + 1;
+      row0 = CLAMP(row0, 0, height_minus_1);
+      row1 = CLAMP(row1, 0, height_minus_1);
+
+      /* get four texel samples */
+      img->FetchTexelc(img, col0, row0, 0, t00);
+      img->FetchTexelc(img, col1, row0, 0, t10);
+      img->FetchTexelc(img, col0, row1, 0, t01);
+      img->FetchTexelc(img, col1, row1, 0, t11);
+
+      /* compute sample weights */
+      a = FRAC(fcol);
+      b = FRAC(frow);
+      w00 = (1.0F-a) * (1.0F-b);
+      w10 =       a  * (1.0F-b);
+      w01 = (1.0F-a) *       b ;
+      w11 =       a  *       b ;
+
+      /* compute weighted average of samples */
+      rgba[i][0] = 
+         (GLchan) (w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0]);
+      rgba[i][1] = 
+         (GLchan) (w00 * t00[1] + w10 * t10[1] + w01 * t01[1] + w11 * t11[1]);
+      rgba[i][2] = 
+         (GLchan) (w00 * t00[2] + w10 * t10[2] + w01 * t01[2] + w11 * t11[2]);
+      rgba[i][3] = 
+         (GLchan) (w00 * t00[3] + w10 * t10[3] + w01 * t01[3] + w11 * t11[3]);
+   }
+}
+
+
+static void
+sample_lambda_rect( GLcontext *ctx, GLuint texUnit,
+                   const struct gl_texture_object *tObj, GLuint n,
+                   const GLfloat texcoords[][4], const GLfloat lambda[],
+                   GLchan rgba[][4])
+{
+   GLuint minStart, minEnd, magStart, magEnd;
+
+   /* We only need lambda to decide between minification and magnification.
+    * There is no mipmapping with rectangular textures.
+    */
+   compute_min_mag_ranges(SWRAST_CONTEXT(ctx)->_MinMagThresh[texUnit],
+                          n, lambda, &minStart, &minEnd, &magStart, &magEnd);
+
+   if (minStart < minEnd) {
+      if (tObj->MinFilter == GL_NEAREST) {
+         sample_nearest_rect( ctx, texUnit, tObj, minEnd - minStart,
+                              texcoords + minStart, NULL, rgba + minStart);
+      }
+      else {
+         sample_linear_rect( ctx, texUnit, tObj, minEnd - minStart,
+                             texcoords + minStart, NULL, rgba + minStart);
+      }
+   }
+   if (magStart < magEnd) {
+      if (tObj->MagFilter == GL_NEAREST) {
+         sample_nearest_rect( ctx, texUnit, tObj, magEnd - magStart,
+                              texcoords + magStart, NULL, rgba + magStart);
+      }
+      else {
+         sample_linear_rect( ctx, texUnit, tObj, magEnd - magStart,
+                             texcoords + magStart, NULL, rgba + magStart);
+      }
+   }
+}
+
+
+
 /*
  * Sample a shadow/depth texture.
  */
 static void
 sample_depth_texture( GLcontext *ctx, GLuint unit,
                       const struct gl_texture_object *tObj, GLuint n,
-                      GLfloat texcoords[][4], const GLfloat lambda[],
+                      const GLfloat texcoords[][4], const GLfloat lambda[],
                       GLchan texel[][4] )
 {
    const GLint baseLevel = tObj->BaseLevel;
-   const struct gl_texture_image *texImage = tObj->Image[baseLevel];
+   const struct gl_texture_image *texImage = tObj->Image[0][baseLevel];
    const GLuint width = texImage->Width;
    const GLuint height = texImage->Height;
-   const GLchan ambient = tObj->ShadowAmbient;
+   GLchan ambient;
    GLenum function;
    GLchan result;
 
    (void) unit;
 
-   ASSERT(tObj->Image[tObj->BaseLevel]->Format == GL_DEPTH_COMPONENT);
-   ASSERT(tObj->Dimensions == 1 || tObj->Dimensions == 2);
+   ASSERT(tObj->Image[0][tObj->BaseLevel]->Format == GL_DEPTH_COMPONENT);
+   ASSERT(tObj->Target == GL_TEXTURE_1D ||
+          tObj->Target == GL_TEXTURE_2D ||
+          tObj->Target == GL_TEXTURE_RECTANGLE_NV);
+
+   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
 
    /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
 
@@ -2064,9 +2632,10 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
       for (i = 0; i < n; i++) {
          GLfloat depthSample;
          GLint col, row;
+         /* XXX fix for texture rectangle! */
          COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0], width, col);
          COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoords[i][1], height, row);
-         depthSample = *((const GLfloat *) texImage->Data + row * width + col);
+         texImage->FetchTexelf(texImage, col, row, 0, &depthSample);
 
          switch (function) {
          case GL_LEQUAL:
@@ -2134,6 +2703,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
          GLfloat u, v;
          GLuint useBorderTexel;
 
+         /* XXX fix for texture rectangle! */
          COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoords[i][0], u, width, i0, i1);
          COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoords[i][1], v, height,j0, j1);
 
@@ -2156,25 +2726,25 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
             depth00 = 1.0;
          }
          else {
-            depth00 = *((const GLfloat *) texImage->Data + j0 * width + i0);
+            texImage->FetchTexelf(texImage, i0, j0, 0, &depth00);
          }
          if (useBorderTexel & (I1BIT | J0BIT)) {
             depth10 = 1.0;
          }
          else {
-            depth10 = *((const GLfloat *) texImage->Data + j0 * width + i1);
+            texImage->FetchTexelf(texImage, i1, j0, 0, &depth10);
          }
          if (useBorderTexel & (I0BIT | J1BIT)) {
             depth01 = 1.0;
          }
          else {
-            depth01 = *((const GLfloat *) texImage->Data + j1 * width + i0);
+            texImage->FetchTexelf(texImage, i0, j1, 0, &depth01);
          }
          if (useBorderTexel & (I1BIT | J1BIT)) {
             depth11 = 1.0;
          }
          else {
-            depth11 = *((const GLfloat *) texImage->Data + j1 * width + i1);
+            texImage->FetchTexelf(texImage, i1, j1, 0, &depth11);
          }
 
          if (0) {
@@ -2250,7 +2820,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
                result = 0;
                break;
             case GL_NEVER:
-               result = CHAN_MAXF;
+               result = CHAN_MAX;
                break;
             case GL_NONE:
                /* ordinary bilinear filtering */
@@ -2306,18 +2876,18 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
 static void
 sample_depth_texture2(const GLcontext *ctx,
                      const struct gl_texture_unit *texUnit,
-                     GLuint n, GLfloat texcoords[][4],
+                     GLuint n, const GLfloat texcoords[][4],
                      GLchan texel[][4])
 {
    const struct gl_texture_object *texObj = texUnit->_Current;
    const GLint baseLevel = texObj->BaseLevel;
-   const struct gl_texture_image *texImage = texObj->Image[baseLevel];
+   const struct gl_texture_image *texImage = texObj->Image[0][baseLevel];
    const GLuint width = texImage->Width;
    const GLuint height = texImage->Height;
-   const GLchan ambient = texObj->ShadowAmbient;
+   GLchan ambient;
    GLboolean lequal, gequal;
 
-   if (texObj->Dimensions != 2) {
+   if (texObj->Target != GL_TEXTURE_2D) {
       _mesa_problem(ctx, "only 2-D depth textures supported at this time");
       return;
    }
@@ -2336,6 +2906,8 @@ sample_depth_texture2(const GLcontext *ctx,
       return;
    }
 
+   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
+
    if (texObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
       lequal = GL_TRUE;
       gequal = GL_FALSE;
@@ -2371,8 +2943,8 @@ sample_depth_texture2(const GLcontext *ctx,
          count = 0;
          for (jj = jmin; jj <= jmax; jj++) {
             for (ii = imin; ii <= imax; ii++) {
-               GLfloat depthSample = *((const GLfloat *) texImage->Data
-                                       + jj * width + ii);
+               GLfloat depthSample;
+               texImage->FetchTexelf(texImage, ii, jj, 0, &depthSample);
                if ((depthSample <= r[i] && lequal) ||
                    (depthSample >= r[i] && gequal)) {
                   count++;
@@ -2394,121 +2966,130 @@ sample_depth_texture2(const GLcontext *ctx,
 #endif
 
 
+/**
+ * We use this function when a texture object is in an "incomplete" state.
+ * When a fragment program attempts to sample an incomplete texture we
+ * return black.
+ * Note: frag progs don't observe texture enable/disable flags.
+ */
 static void
 null_sample_func( GLcontext *ctx, GLuint texUnit,
                  const struct gl_texture_object *tObj, GLuint n,
-                 GLfloat texcoords[][4], const GLfloat lambda[],
+                 const GLfloat texcoords[][4], const GLfloat lambda[],
                  GLchan rgba[][4])
 {
+   (void) ctx;
+   (void) texUnit;
+   (void) tObj;
+   (void) texcoords;
+   (void) lambda;
+   _mesa_bzero(rgba, n * 4 * sizeof(GLchan));
 }
 
 
 
-/**********************************************************************/
-/*                       Texture Sampling Setup                       */
-/**********************************************************************/
-
-
-/*
+/**
  * Setup the texture sampling function for this texture object.
  */
-void
-_swrast_choose_texture_sample_func( GLcontext *ctx, GLuint texUnit,
+texture_sample_func
+_swrast_choose_texture_sample_func( GLcontext *ctx,
                                    const struct gl_texture_object *t )
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
+   const GLenum format = t->Image[0][t->BaseLevel]->Format;
 
    if (!t->Complete) {
-      swrast->TextureSample[texUnit] = null_sample_func;
+      return &null_sample_func;
    }
-   else {
-      const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
-      const GLenum format = t->Image[t->BaseLevel]->Format;
 
-      if (needLambda) {
-         /* Compute min/mag filter threshold */
-         if (t->MagFilter == GL_LINEAR
-             && (t->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
-                 t->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
-            swrast->_MinMagThresh[texUnit] = 0.5F;
+   switch (t->Target) {
+   case GL_TEXTURE_1D:
+      if (format == GL_DEPTH_COMPONENT) {
+         return &sample_depth_texture;
+      }
+      else if (needLambda) {
+         return &sample_lambda_1d;
+      }
+      else if (t->MinFilter == GL_LINEAR) {
+         return &sample_linear_1d;
+      }
+      else {
+         ASSERT(t->MinFilter == GL_NEAREST);
+         return &sample_nearest_1d;
+      }
+      break;
+   case GL_TEXTURE_2D:
+      if (format == GL_DEPTH_COMPONENT) {
+         return &sample_depth_texture;
+      }
+      else if (needLambda) {
+         return &sample_lambda_2d;
+      }
+      else if (t->MinFilter == GL_LINEAR) {
+         return &sample_linear_2d;
+      }
+      else {
+         GLint baseLevel = t->BaseLevel;
+         ASSERT(t->MinFilter == GL_NEAREST);
+         if (t->WrapS == GL_REPEAT &&
+             t->WrapT == GL_REPEAT &&
+             t->_IsPowerOfTwo &&
+             t->Image[0][baseLevel]->Border == 0 &&
+             t->Image[0][baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
+            return &opt_sample_rgb_2d;
+         }
+         else if (t->WrapS == GL_REPEAT &&
+                  t->WrapT == GL_REPEAT &&
+                  t->_IsPowerOfTwo &&
+                  t->Image[0][baseLevel]->Border == 0 &&
+                  t->Image[0][baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
+            return &opt_sample_rgba_2d;
          }
          else {
-            swrast->_MinMagThresh[texUnit] = 0.0F;
+            return &sample_nearest_2d;
          }
       }
-
-      switch (t->Dimensions) {
-         case 1:
-            if (format == GL_DEPTH_COMPONENT) {
-               swrast->TextureSample[texUnit] = sample_depth_texture;
-            }
-            else if (needLambda) {
-               swrast->TextureSample[texUnit] = sample_lambda_1d;
-            }
-            else if (t->MinFilter == GL_LINEAR) {
-               swrast->TextureSample[texUnit] = sample_linear_1d;
-            }
-            else {
-               ASSERT(t->MinFilter == GL_NEAREST);
-               swrast->TextureSample[texUnit] = sample_nearest_1d;
-            }
-            break;
-         case 2:
-            if (format == GL_DEPTH_COMPONENT) {
-               swrast->TextureSample[texUnit] = sample_depth_texture;
-            }
-            else if (needLambda) {
-               swrast->TextureSample[texUnit] = sample_lambda_2d;
-            }
-            else if (t->MinFilter == GL_LINEAR) {
-               swrast->TextureSample[texUnit] = sample_linear_2d;
-            }
-            else {
-               GLint baseLevel = t->BaseLevel;
-               ASSERT(t->MinFilter == GL_NEAREST);
-               if (t->WrapS == GL_REPEAT &&
-                   t->WrapT == GL_REPEAT &&
-                   t->Image[baseLevel]->Border == 0 &&
-                   t->Image[baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
-                  swrast->TextureSample[texUnit] = opt_sample_rgb_2d;
-               }
-               else if (t->WrapS == GL_REPEAT &&
-                        t->WrapT == GL_REPEAT &&
-                        t->Image[baseLevel]->Border == 0 &&
-                        t->Image[baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
-                  swrast->TextureSample[texUnit] = opt_sample_rgba_2d;
-               }
-               else
-                  swrast->TextureSample[texUnit] = sample_nearest_2d;
-            }
-            break;
-         case 3:
-            if (needLambda) {
-               swrast->TextureSample[texUnit] = sample_lambda_3d;
-            }
-            else if (t->MinFilter == GL_LINEAR) {
-               swrast->TextureSample[texUnit] = sample_linear_3d;
-            }
-            else {
-               ASSERT(t->MinFilter == GL_NEAREST);
-               swrast->TextureSample[texUnit] = sample_nearest_3d;
-            }
-            break;
-         case 6: /* cube map */
-            if (needLambda) {
-               swrast->TextureSample[texUnit] = sample_lambda_cube;
-            }
-            else if (t->MinFilter == GL_LINEAR) {
-               swrast->TextureSample[texUnit] = sample_linear_cube;
-            }
-            else {
-               ASSERT(t->MinFilter == GL_NEAREST);
-               swrast->TextureSample[texUnit] = sample_nearest_cube;
-            }
-            break;
-         default:
-            _mesa_problem(ctx, "invalid dimensions in _swrast_choose_texture_sample_func");
+      break;
+   case GL_TEXTURE_3D:
+      if (needLambda) {
+         return &sample_lambda_3d;
+      }
+      else if (t->MinFilter == GL_LINEAR) {
+         return &sample_linear_3d;
+      }
+      else {
+         ASSERT(t->MinFilter == GL_NEAREST);
+         return &sample_nearest_3d;
       }
+      break;
+   case GL_TEXTURE_CUBE_MAP:
+      if (needLambda) {
+         return &sample_lambda_cube;
+      }
+      else if (t->MinFilter == GL_LINEAR) {
+         return &sample_linear_cube;
+      }
+      else {
+         ASSERT(t->MinFilter == GL_NEAREST);
+         return &sample_nearest_cube;
+      }
+      break;
+   case GL_TEXTURE_RECTANGLE_NV:
+      if (needLambda) {
+         return &sample_lambda_rect;
+      }
+      else if (t->MinFilter == GL_LINEAR) {
+         return &sample_linear_rect;
+      }
+      else {
+         ASSERT(t->MinFilter == GL_NEAREST);
+         return &sample_nearest_rect;
+      }
+      break;
+   default:
+      _mesa_problem(ctx,
+                    "invalid target in _swrast_choose_texture_sample_func");
+      return &null_sample_func;
    }
 }
 
@@ -2516,31 +3097,53 @@ _swrast_choose_texture_sample_func( GLcontext *ctx, GLuint texUnit,
 #define PROD(A,B)   ( (GLuint)(A) * ((GLuint)(B)+1) )
 #define S_PROD(A,B) ( (GLint)(A) * ((GLint)(B)+1) )
 
+
+/**
+ * Do texture application for GL_ARB/EXT_texture_env_combine.
+ * This function also supports GL_{EXT,ARB}_texture_env_dot3 and
+ * GL_ATI_texture_env_combine3
+ *
+ * \param ctx          rendering context
+ * \param textureUnit  the texture unit to apply
+ * \param n            number of fragments to process (span width)
+ * \param primary_rgba incoming fragment color array
+ * \param texelBuffer  pointer to texel colors for all texture units
+ * 
+ * \param rgba         incoming colors, which get modified here
+ */
 static INLINE void
-texture_combine(const GLcontext *ctx,
-                const struct gl_texture_unit *textureUnit,
-                GLuint n,
-                CONST GLchan (*primary_rgba)[4],
-                CONST GLchan (*texel)[4],
-                GLchan (*rgba)[4])
+texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
+                 CONST GLchan (*primary_rgba)[4],
+                 CONST GLchan *texelBuffer,
+                 GLchan (*rgba)[4] )
 {
+   const struct gl_texture_unit *textureUnit = &(ctx->Texture.Unit[unit]);
    const GLchan (*argRGB [3])[4];
    const GLchan (*argA [3])[4];
-   GLuint i, j;
    const GLuint RGBshift = textureUnit->CombineScaleShiftRGB;
    const GLuint Ashift   = textureUnit->CombineScaleShiftA;
 #if CHAN_TYPE == GL_FLOAT
    const GLchan RGBmult = (GLfloat) (1 << RGBshift);
    const GLchan Amult = (GLfloat) (1 << Ashift);
+   static const GLchan one[4] = { 1.0, 1.0, 1.0, 1.0 };
+   static const GLchan zero[4] = { 0.0, 0.0, 0.0, 0.0 };
 #else
    const GLint half = (CHAN_MAX + 1) / 2;
+   static const GLchan one[4] = { CHAN_MAX, CHAN_MAX, CHAN_MAX, CHAN_MAX };
+   static const GLchan zero[4] = { 0, 0, 0, 0 };
 #endif
+   GLuint i, j;
+   GLuint numColorArgs;
+   GLuint numAlphaArgs;
 
+   /* GLchan ccolor[3][4]; */
    DEFMNARRAY(GLchan, ccolor, 3, 3 * MAX_WIDTH, 4);  /* mac 32k limitation */
    CHECKARRAY(ccolor, return);  /* mac 32k limitation */
 
    ASSERT(ctx->Extensions.EXT_texture_env_combine ||
           ctx->Extensions.ARB_texture_env_combine);
+   ASSERT(SWRAST_CONTEXT(ctx)->_AnyTextureCombine);
+
 
    /*
    printf("modeRGB 0x%x  modeA 0x%x  srcRGB1 0x%x  srcA1 0x%x  srcRGB2 0x%x  srcA2 0x%x\n",
@@ -2555,41 +3158,70 @@ texture_combine(const GLcontext *ctx,
    /*
     * Do operand setup for up to 3 operands.  Loop over the terms.
     */
-   for (j = 0; j < 3; j++) {
-      switch (textureUnit->CombineSourceA[j]) {
-         case GL_TEXTURE:
-            argA[j] = texel;
-            break;
-         case GL_PRIMARY_COLOR_EXT:
-            argA[j] = primary_rgba;
-            break;
-         case GL_PREVIOUS_EXT:
-            argA[j] = (const GLchan (*)[4]) rgba;
-            break;
-         case GL_CONSTANT_EXT:
-            {
-               GLchan alpha, (*c)[4] = ccolor[j];
-               UNCLAMPED_FLOAT_TO_CHAN(alpha, textureUnit->EnvColor[3]);
-               for (i = 0; i < n; i++)
-                  c[i][ACOMP] = alpha;
-               argA[j] = (const GLchan (*)[4]) ccolor[j];
-            }
-            break;
-         default:
-            _mesa_problem(ctx, "invalid combine source");
-      }
+   switch (textureUnit->CombineModeRGB) {
+      case GL_REPLACE:
+        numColorArgs = 1;
+        break;
+      case GL_MODULATE:
+      case GL_ADD:
+      case GL_ADD_SIGNED:
+      case GL_SUBTRACT:
+      case GL_DOT3_RGB:
+      case GL_DOT3_RGBA:
+      case GL_DOT3_RGB_EXT:
+      case GL_DOT3_RGBA_EXT:
+        numColorArgs = 2;
+        break;
+      case GL_INTERPOLATE:
+      case GL_MODULATE_ADD_ATI:
+      case GL_MODULATE_SIGNED_ADD_ATI:
+      case GL_MODULATE_SUBTRACT_ATI:
+        numColorArgs = 3;
+        break;
+      default:
+        numColorArgs = 0;
+        ASSERT(0);
+        break;
+   }
 
-      switch (textureUnit->CombineSourceRGB[j]) {
+   switch (textureUnit->CombineModeA) {
+      case GL_REPLACE:
+        numAlphaArgs = 1;
+        break;
+      case GL_MODULATE:
+      case GL_ADD:
+      case GL_ADD_SIGNED:
+      case GL_SUBTRACT:
+        numAlphaArgs = 2;
+        break;
+      case GL_INTERPOLATE:
+      case GL_MODULATE_ADD_ATI:
+      case GL_MODULATE_SIGNED_ADD_ATI:
+      case GL_MODULATE_SUBTRACT_ATI:
+        numAlphaArgs = 3;
+        break;
+      default:
+        numAlphaArgs = 0;
+        ASSERT(0);
+        break;
+   }
+
+   for (j = 0; j < numColorArgs; j++) {
+      const GLenum srcRGB = textureUnit->CombineSourceRGB[j];
+
+
+      switch (srcRGB) {
          case GL_TEXTURE:
-            argRGB[j] = texel;
+            argRGB[j] = (const GLchan (*)[4])
+               (texelBuffer + unit * (n * 4 * sizeof(GLchan)));
             break;
-         case GL_PRIMARY_COLOR_EXT:
+         case GL_PRIMARY_COLOR:
             argRGB[j] = primary_rgba;
             break;
-         case GL_PREVIOUS_EXT:
+         case GL_PREVIOUS:
             argRGB[j] = (const GLchan (*)[4]) rgba;
             break;
-         case GL_CONSTANT_EXT:
+         case GL_CONSTANT:
             {
                GLchan (*c)[4] = ccolor[j];
                GLchan red, green, blue, alpha;
@@ -2606,8 +3238,24 @@ texture_combine(const GLcontext *ctx,
                argRGB[j] = (const GLchan (*)[4]) ccolor[j];
             }
             break;
+        /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
+         */
+        case GL_ZERO:
+            argRGB[j] = & zero;
+            break;
+        case GL_ONE:
+            argRGB[j] = & one;
+            break;
          default:
-            _mesa_problem(ctx, "invalid combine source");
+            /* ARB_texture_env_crossbar source */
+            {
+               const GLuint srcUnit = srcRGB - GL_TEXTURE0;
+               ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
+               if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
+                  return;
+               argRGB[j] = (const GLchan (*)[4])
+                  (texelBuffer + srcUnit * (n * 4 * sizeof(GLchan)));
+            }
       }
 
       if (textureUnit->CombineOperandRGB[j] != GL_SRC_COLOR) {
@@ -2640,6 +3288,51 @@ texture_combine(const GLcontext *ctx,
             }
          }
       }
+   }
+
+
+   for (j = 0; j < numAlphaArgs; j++) {
+      const GLenum srcA = textureUnit->CombineSourceA[j];
+
+      switch (srcA) {
+         case GL_TEXTURE:
+            argA[j] = (const GLchan (*)[4])
+               (texelBuffer + unit * (n * 4 * sizeof(GLchan)));
+            break;
+         case GL_PRIMARY_COLOR:
+            argA[j] = primary_rgba;
+            break;
+         case GL_PREVIOUS:
+            argA[j] = (const GLchan (*)[4]) rgba;
+            break;
+         case GL_CONSTANT:
+            {
+               GLchan alpha, (*c)[4] = ccolor[j];
+               UNCLAMPED_FLOAT_TO_CHAN(alpha, textureUnit->EnvColor[3]);
+               for (i = 0; i < n; i++)
+                  c[i][ACOMP] = alpha;
+               argA[j] = (const GLchan (*)[4]) ccolor[j];
+            }
+            break;
+        /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
+         */
+        case GL_ZERO:
+            argA[j] = & zero;
+            break;
+        case GL_ONE:
+            argA[j] = & one;
+            break;
+         default:
+            /* ARB_texture_env_crossbar source */
+            {
+               const GLuint srcUnit = srcA - GL_TEXTURE0;
+               ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
+               if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
+                  return;
+               argA[j] = (const GLchan (*)[4])
+                  (texelBuffer + srcUnit * (n * 4 * sizeof(GLchan)));
+            }
+      }
 
       if (textureUnit->CombineOperandA[j] == GL_ONE_MINUS_SRC_ALPHA) {
          const GLchan (*src)[4] = argA[j];
@@ -2649,17 +3342,6 @@ texture_combine(const GLcontext *ctx,
             dst[i][ACOMP] = CHAN_MAX - src[i][ACOMP];
          }
       }
-
-      if (textureUnit->CombineModeRGB == GL_REPLACE &&
-          textureUnit->CombineModeA == GL_REPLACE) {
-         break;      /*  done, we need only arg0  */
-      }
-
-      if (j == 1 &&
-          textureUnit->CombineModeRGB != GL_INTERPOLATE_EXT &&
-          textureUnit->CombineModeA != GL_INTERPOLATE_EXT) {
-         break;      /*  arg0 and arg1 are done. we don't need arg2. */
-      }
    }
 
    /*
@@ -2737,7 +3419,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_ADD_SIGNED_EXT:
+      case GL_ADD_SIGNED:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
@@ -2760,7 +3442,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_INTERPOLATE_EXT:
+      case GL_INTERPOLATE:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
@@ -2793,7 +3475,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_SUBTRACT_ARB:
+      case GL_SUBTRACT:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
@@ -2825,6 +3507,7 @@ texture_combine(const GLcontext *ctx,
                              (arg0[i][GCOMP]-0.5F) * (arg1[i][GCOMP]-0.5F) +
                              (arg0[i][BCOMP]-0.5F) * (arg1[i][BCOMP]-0.5F))
                             * 4.0F;
+               dot = CLAMP(dot, 0.0F, CHAN_MAXF);
 #else
                GLint dot = (S_PROD((GLint)arg0[i][RCOMP] - half,
                                   (GLint)arg1[i][RCOMP] - half) +
@@ -2832,14 +3515,14 @@ texture_combine(const GLcontext *ctx,
                                   (GLint)arg1[i][GCOMP] - half) +
                            S_PROD((GLint)arg0[i][BCOMP] - half,
                                   (GLint)arg1[i][BCOMP] - half)) >> 6;
-#endif
                dot = CLAMP(dot, 0, CHAN_MAX);
+#endif
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLchan) dot;
             }
          }
          break;
-      case GL_DOT3_RGB_ARB:
-      case GL_DOT3_RGBA_ARB:
+      case GL_DOT3_RGB:
+      case GL_DOT3_RGBA:
          {
             /* DO scale the result by 1 2 or 4 */
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
@@ -2849,7 +3532,8 @@ texture_combine(const GLcontext *ctx,
                GLchan dot = ((arg0[i][RCOMP]-0.5F) * (arg1[i][RCOMP]-0.5F) +
                              (arg0[i][GCOMP]-0.5F) * (arg1[i][GCOMP]-0.5F) +
                              (arg0[i][BCOMP]-0.5F) * (arg1[i][BCOMP]-0.5F))
-                            * 4.0F;
+                            * 4.0F * RGBmult;
+               dot = CLAMP(dot, 0.0, CHAN_MAXF);
 #else
                GLint dot = (S_PROD((GLint)arg0[i][RCOMP] - half,
                                   (GLint)arg1[i][RCOMP] - half) +
@@ -2857,12 +3541,100 @@ texture_combine(const GLcontext *ctx,
                                   (GLint)arg1[i][GCOMP] - half) +
                            S_PROD((GLint)arg0[i][BCOMP] - half,
                                   (GLint)arg1[i][BCOMP] - half)) >> 6;
+               dot <<= RGBshift;
+               dot = CLAMP(dot, 0, CHAN_MAX);
 #endif
-               dot = CLAMP(dot, 0, CHAN_MAX) << RGBshift;
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLchan) dot;
             }
          }
          break;
+      case GL_MODULATE_ADD_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argRGB[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - RGBshift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) + arg1[i][RCOMP]) * RGBmult;
+               rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) + arg1[i][GCOMP]) * RGBmult;
+               rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) + arg1[i][BCOMP]) * RGBmult;
+#else
+               GLuint r = (PROD(arg0[i][RCOMP], arg2[i][RCOMP])
+                           + ((GLuint) arg1[i][RCOMP] << CHAN_BITS)) >> shift;
+               GLuint g = (PROD(arg0[i][GCOMP], arg2[i][GCOMP])
+                           + ((GLuint) arg1[i][GCOMP] << CHAN_BITS)) >> shift;
+               GLuint b = (PROD(arg0[i][BCOMP], arg2[i][BCOMP])
+                           + ((GLuint) arg1[i][BCOMP] << CHAN_BITS)) >> shift;
+               rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX);
+               rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX);
+               rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);
+#endif
+            }
+        }
+         break;
+      case GL_MODULATE_SIGNED_ADD_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argRGB[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - RGBshift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) + arg1[i][RCOMP] - 0.5) * RGBmult;
+               rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) + arg1[i][GCOMP] - 0.5) * RGBmult;
+               rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) + arg1[i][BCOMP] - 0.5) * RGBmult;
+#else
+               GLint r = (S_PROD(arg0[i][RCOMP], arg2[i][RCOMP])
+                         + (((GLint) arg1[i][RCOMP] - half) << CHAN_BITS))
+                   >> shift;
+               GLint g = (S_PROD(arg0[i][GCOMP], arg2[i][GCOMP])
+                         + (((GLint) arg1[i][GCOMP] - half) << CHAN_BITS))
+                   >> shift;
+               GLint b = (S_PROD(arg0[i][BCOMP], arg2[i][BCOMP])
+                         + (((GLint) arg1[i][BCOMP] - half) << CHAN_BITS))
+                   >> shift;
+               rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
+               rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
+               rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
+#endif
+            }
+        }
+         break;
+      case GL_MODULATE_SUBTRACT_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argRGB[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - RGBshift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) - arg1[i][RCOMP]) * RGBmult;
+               rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) - arg1[i][GCOMP]) * RGBmult;
+               rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) - arg1[i][BCOMP]) * RGBmult;
+#else
+               GLint r = (S_PROD(arg0[i][RCOMP], arg2[i][RCOMP])
+                         - ((GLint) arg1[i][RCOMP] << CHAN_BITS))
+                   >> shift;
+               GLint g = (S_PROD(arg0[i][GCOMP], arg2[i][GCOMP])
+                         - ((GLint) arg1[i][GCOMP] << CHAN_BITS))
+                   >> shift;
+               GLint b = (S_PROD(arg0[i][BCOMP], arg2[i][BCOMP])
+                         - ((GLint) arg1[i][BCOMP] << CHAN_BITS))
+                   >> shift;
+               rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
+               rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
+               rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
+#endif
+            }
+        }
+         break;
       default:
          _mesa_problem(ctx, "invalid combine mode");
    }
@@ -2919,7 +3691,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_ADD_SIGNED_EXT:
+      case GL_ADD_SIGNED:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
@@ -2934,7 +3706,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_INTERPOLATE_EXT:
+      case GL_INTERPOLATE:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
@@ -2956,7 +3728,7 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-      case GL_SUBTRACT_ARB:
+      case GL_SUBTRACT:
          {
             const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
             const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
@@ -2970,7 +3742,66 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
-
+      case GL_MODULATE_ADD_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argA[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - Ashift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP]) + arg1[i][ACOMP]) * Amult;
+#else
+               GLint a = (PROD(arg0[i][ACOMP], arg2[i][ACOMP])
+                          + ((GLuint) arg1[i][ACOMP] << CHAN_BITS))
+                   >> shift;
+               rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+#endif
+            }
+         }
+         break;
+      case GL_MODULATE_SIGNED_ADD_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argA[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - Ashift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP]) + arg1[i][ACOMP] - 0.5F) * Amult;
+#else
+               GLint a = (S_PROD(arg0[i][ACOMP], arg2[i][ACOMP])
+                         + (((GLint) arg1[i][ACOMP] - half) << CHAN_BITS))
+                   >> shift;
+               rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+#endif
+            }
+         }
+         break;
+      case GL_MODULATE_SUBTRACT_ATI:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argA[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argA[1];
+            const GLchan (*arg2)[4] = (const GLchan (*)[4]) argA[2];
+#if CHAN_TYPE != GL_FLOAT
+            const GLint shift = CHAN_BITS - Ashift;
+#endif
+            for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+               rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP]) - arg1[i][ACOMP]) * Amult;
+#else
+               GLint a = (S_PROD(arg0[i][ACOMP], arg2[i][ACOMP]) 
+                         - ((GLint) arg1[i][ACOMP] << CHAN_BITS))
+                   >> shift;
+               rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+#endif
+            }
+         }
+         break;
       default:
          _mesa_problem(ctx, "invalid combine mode");
    }
@@ -2981,7 +3812,7 @@ texture_combine(const GLcontext *ctx,
     * GL_DOT3.
     */
    if (textureUnit->CombineModeRGB == GL_DOT3_RGBA_EXT ||
-       textureUnit->CombineModeRGB == GL_DOT3_RGBA_ARB) {
+       textureUnit->CombineModeRGB == GL_DOT3_RGBA) {
       for (i = 0; i < n; i++) {
         rgba[i][ACOMP] = rgba[i][RCOMP];
       }
@@ -2991,14 +3822,23 @@ texture_combine(const GLcontext *ctx,
 #undef PROD
 
 
+/**
+ * Implement NVIDIA's GL_NV_texture_env_combine4 extension when
+ * texUnit->EnvMode == GL_COMBINE4_NV.
+ */
+static INLINE void
+texture_combine4( const GLcontext *ctx, GLuint unit, GLuint n,
+                  CONST GLchan (*primary_rgba)[4],
+                  CONST GLchan *texelBuffer,
+                  GLchan (*rgba)[4] )
+{
+}
 
-/**********************************************************************/
-/*                      Texture Application                           */
-/**********************************************************************/
 
 
-/*
- * Combine incoming fragment color with texel color to produce output color.
+/**
+ * Apply a conventional OpenGL texture env mode (REPLACE, ADD, BLEND,
+ * MODULATE, or DECAL) to an array of fragments.
  * Input:  textureUnit - pointer to texture unit to apply
  *         format - base internal texture format
  *         n - number of fragments
@@ -3008,7 +3848,7 @@ texture_combine(const GLcontext *ctx,
  *                according to the texture environment mode.
  */
 static void
-apply_texture( const GLcontext *ctx,
+texture_apply( const GLcontext *ctx,
                const struct gl_texture_unit *texUnit,
                GLuint n,
                CONST GLchan primary_rgba[][4], CONST GLchan texel[][4],
@@ -3023,13 +3863,16 @@ apply_texture( const GLcontext *ctx,
    ASSERT(texUnit->_Current);
 
    baseLevel = texUnit->_Current->BaseLevel;
-   ASSERT(texUnit->_Current->Image[baseLevel]);
+   ASSERT(texUnit->_Current->Image[0][baseLevel]);
 
-   format = texUnit->_Current->Image[baseLevel]->Format;
+   format = texUnit->_Current->Image[0][baseLevel]->Format;
 
-   if (format == GL_COLOR_INDEX || format == GL_DEPTH_COMPONENT) {
+   if (format == GL_COLOR_INDEX || format == GL_YCBCR_MESA) {
       format = GL_RGBA;  /* a bit of a hack */
    }
+   else if (format == GL_DEPTH_COMPONENT) {
+      format = texUnit->_Current->DepthMode;
+   }
 
    switch (texUnit->EnvMode) {
       case GL_REPLACE:
@@ -3087,7 +3930,7 @@ apply_texture( const GLcontext *ctx,
               }
               break;
             default:
-               _mesa_problem(ctx, "Bad format (GL_REPLACE) in apply_texture");
+               _mesa_problem(ctx, "Bad format (GL_REPLACE) in texture_apply");
                return;
         }
         break;
@@ -3153,7 +3996,7 @@ apply_texture( const GLcontext *ctx,
               }
               break;
             default:
-               _mesa_problem(ctx, "Bad format (GL_MODULATE) in apply_texture");
+               _mesa_problem(ctx, "Bad format (GL_MODULATE) in texture_apply");
                return;
         }
         break;
@@ -3186,7 +4029,7 @@ apply_texture( const GLcontext *ctx,
               }
               break;
             default:
-               _mesa_problem(ctx, "Bad format (GL_DECAL) in apply_texture");
+               _mesa_problem(ctx, "Bad format (GL_DECAL) in texture_apply");
                return;
         }
         break;
@@ -3256,7 +4099,7 @@ apply_texture( const GLcontext *ctx,
               }
               break;
             default:
-               _mesa_problem(ctx, "Bad format (GL_BLEND) in apply_texture");
+               _mesa_problem(ctx, "Bad format (GL_BLEND) in texture_apply");
                return;
         }
         break;
@@ -3333,63 +4176,60 @@ apply_texture( const GLcontext *ctx,
                }
                break;
             default:
-               _mesa_problem(ctx, "Bad format (GL_ADD) in apply_texture");
+               _mesa_problem(ctx, "Bad format (GL_ADD) in texture_apply");
                return;
         }
         break;
 
-      case GL_COMBINE_EXT:
-         texture_combine(ctx, texUnit, n, primary_rgba, texel, rgba);
-         break;
-
       default:
-         _mesa_problem(ctx, "Bad env mode in apply_texture");
+         _mesa_problem(ctx, "Bad env mode in texture_apply");
          return;
    }
 }
 
 
 
-/*
- * Apply a unit of texture mapping to the incoming fragments.
+/**
+ * Apply texture mapping to a span of fragments.
  */
 void
-_swrast_texture_fragments( GLcontext *ctx, GLuint texUnit,
-                          struct sw_span *span,
-                          CONST GLchan primary_rgba[][4])
+_swrast_texture_span( GLcontext *ctx, struct sw_span *span )
 {
-   const GLuint mask = TEXTURE0_ANY << (texUnit * 4);
-   GLfloat (*texcoords)[4] = span->texcoords[texUnit];
-   GLfloat *lambda = span->lambda[texUnit];
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   GLchan primary_rgba[MAX_WIDTH][4];
+   GLuint unit;
 
+   ASSERT(span->end < MAX_WIDTH);
+   ASSERT(span->arrayMask & SPAN_TEXTURE);
 
-   if (ctx->Texture._ReallyEnabled & mask) {
-      const struct gl_texture_unit *textureUnit = &ctx->Texture.Unit[texUnit];
+   /*
+    * Save copy of the incoming fragment colors (the GL_PRIMARY_COLOR)
+    */
+   if (swrast->_AnyTextureCombine)
+      MEMCPY(primary_rgba, span->array->rgba, 4 * span->end * sizeof(GLchan));
 
-      ASSERT(span->arrayMask & SPAN_TEXTURE);
-      
-      if (textureUnit->_Current) {   /* XXX need this? */
-         const struct gl_texture_object *curObj = textureUnit->_Current;
-         GLchan texel[MAX_WIDTH][4];
-         
-         if (span->arrayMask | SPAN_LAMBDA) {
-#if 0
-            float min, max;
-            int i;
-            min = max = lambda[0];
-            for (i = 1; i < span->end; i++) {
-               if (lambda[i] > max)
-                  max = lambda[i];
-               if (lambda[i] < min)
-                  min = lambda[i];
-            }
-            printf("min/max %g / %g\n", min, max);
-#endif
-            if (textureUnit->LodBias != 0.0F) {
+   /*
+    * Must do all texture sampling before combining in order to
+    * accomodate GL_ARB_texture_env_crossbar.
+    */
+   for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
+      if (ctx->Texture.Unit[unit]._ReallyEnabled) {
+         const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+         const struct gl_texture_object *curObj = texUnit->_Current;
+         GLfloat *lambda = span->array->lambda[unit];
+         GLchan (*texels)[4] = (GLchan (*)[4])
+            (swrast->TexelBuffer + unit * (span->end * 4 * sizeof(GLchan)));
+
+         /* adjust texture lod (lambda) */
+         if (span->arrayMask & SPAN_LAMBDA) {
+            if (texUnit->LodBias + curObj->LodBias != 0.0F) {
                /* apply LOD bias, but don't clamp yet */
+               const GLfloat bias = CLAMP(texUnit->LodBias + curObj->LodBias,
+                                          -ctx->Const.MaxTextureLodBias,
+                                          ctx->Const.MaxTextureLodBias);
                GLuint i;
-               for (i=0;i<span->end;i++) {
-                  lambda[i] += textureUnit->LodBias;
+               for (i = 0; i < span->end; i++) {
+                  lambda[i] += bias;
                }
             }
 
@@ -3398,60 +4238,55 @@ _swrast_texture_fragments( GLcontext *ctx, GLuint texUnit,
                const GLfloat min = curObj->MinLod;
                const GLfloat max = curObj->MaxLod;
                GLuint i;
-               for (i=0;i<span->end;i++) {
+               for (i = 0; i < span->end; i++) {
                   GLfloat l = lambda[i];
                   lambda[i] = CLAMP(l, min, max);
                }
             }
          }
 
-         /* Sample the texture for n fragments */
-         SWRAST_CONTEXT(ctx)->TextureSample[texUnit]( ctx, texUnit,
-                                                      textureUnit->_Current,
-                                                      span->end, texcoords,
-                                                      lambda, texel );
+         /* Sample the texture (span->end fragments) */
+         swrast->TextureSample[unit]( ctx, unit, texUnit->_Current, span->end,
+                         (const GLfloat (*)[4]) span->array->texcoords[unit],
+                         lambda, texels );
 
-         apply_texture( ctx, textureUnit, span->end, primary_rgba,
-                        (const GLchan (*)[4]) texel, span->color.rgba );
+         /* GL_SGI_texture_color_table */
+         if (texUnit->ColorTableEnabled) {
+            _swrast_texture_table_lookup(&texUnit->ColorTable, span->end, texels);
+         }
       }
    }
-}
-
-
-/*
- * Apply multiple texture stages (or just unit 0) to the span.
- * At some point in the future we'll probably modify this so that
- * texels from any texture unit are available in any combiner unit.
- * That'll require doing all the texture sampling first, and then
- * all the application (blending) afterward.
- */
-void
-_swrast_multitexture_fragments( GLcontext *ctx, struct sw_span *span )
-{
-   if (ctx->Texture._ReallyEnabled & ~TEXTURE0_ANY) {
-      /* multitexture */
-      GLchan primary_rgba[MAX_WIDTH][4];
-      GLuint unit;
-
-      ASSERT(span->end < MAX_WIDTH);
-      ASSERT(span->arrayMask & SPAN_TEXTURE);
-
-      /* save copy of the span colors (the GL_PRIMARY_COLOR) */
-      MEMCPY(primary_rgba, span->color.rgba, 4 * span->end * sizeof(GLchan));
 
-      /* loop over texture units, modifying the span->color.rgba values */
-      for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-         if (ctx->Texture.Unit[unit]._ReallyEnabled) {
-            _swrast_texture_fragments( ctx, unit, span,
-                                      (CONST GLchan (*)[4]) primary_rgba);
+   /*
+    * OK, now apply the texture (aka texture combine/blend).
+    * We modify the span->color.rgba values.
+    */
+   for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
+      if (ctx->Texture.Unit[unit]._ReallyEnabled) {
+         const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+         if (texUnit->EnvMode == GL_COMBINE) {
+            /* GL_ARB/EXT_texture_env_combine */
+            texture_combine( ctx, unit, span->end,
+                             (CONST GLchan (*)[4]) primary_rgba,
+                             swrast->TexelBuffer,
+                             span->array->rgba );
+         }
+         else if (texUnit->EnvMode == GL_COMBINE4_NV) {
+            /* GL_NV_texture_env_combine4 */
+            texture_combine4( ctx, unit, span->end,
+                              (CONST GLchan (*)[4]) primary_rgba,
+                              swrast->TexelBuffer,
+                              span->array->rgba );
+         }
+         else {
+            /* conventional texture blend */
+            const GLchan (*texels)[4] = (const GLchan (*)[4])
+               (swrast->TexelBuffer + unit *
+                (span->end * 4 * sizeof(GLchan)));
+            texture_apply( ctx, texUnit, span->end,
+                           (CONST GLchan (*)[4]) primary_rgba, texels,
+                           span->array->rgba );
          }
       }
    }
-   else {
-      /* Just unit 0 enabled */
-      ASSERT(ctx->Texture._ReallyEnabled & TEXTURE0_ANY);
-
-      _swrast_texture_fragments( ctx, 0, span,
-                                (CONST GLchan (*)[4]) span->color.rgba);
-   }
 }