s/BlendEquatioRGB/BlendEquationRGB/
[mesa.git] / src / mesa / swrast / s_texture.c
index f99075fc54579cb809feca79ba8d84fa5c01ab1e..b5a4509c07ea91cd320683c81bc7cf48a508bbd6 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_texture.c,v 1.81 2003/02/27 19:40:45 kschultz Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.1
  *
- * Copyright (C) 1999-2003  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,7 +27,6 @@
 #include "context.h"
 #include "colormac.h"
 #include "macros.h"
-#include "mmath.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:
 {                                                                      \
    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) {                 \
+   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) {                  \
+   else if (wrapMode == GL_MIRRORED_REPEAT) {                          \
       const GLint flr = IFLOOR(S);                                     \
       if (flr & 1)                                                     \
          U = 1.0F - (S - (GLfloat) flr);       /* flr is odd */        \
       if (I1 >= (GLint) SIZE)                                          \
          I1 = SIZE - 1;                                                        \
    }                                                                   \
-   else if (wrapMode == GL_MIRROR_CLAMP_ATI) {                         \
+   else if (wrapMode == GL_MIRROR_CLAMP_EXT) {                         \
       U = (GLfloat) fabs(S);                                           \
       if (U >= 1.0F)                                                   \
          U = (GLfloat) SIZE;                                           \
       I0 = IFLOOR(U);                                                  \
       I1 = I0 + 1;                                                     \
    }                                                                   \
-   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_ATI) {                 \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_EXT) {                 \
       U = (GLfloat) fabs(S);                                           \
       if (U >= 1.0F)                                                   \
          U = (GLfloat) SIZE;                                           \
       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) {                  \
+   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) {                  \
+   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_ATI) {                         \
+   else if (wrapMode == GL_MIRROR_CLAMP_EXT) {                         \
       /* s limited to [0,1] */                                         \
       /* i limited to [0,size-1] */                                    \
       const GLfloat u = (GLfloat) fabs(S);                             \
       else                                                             \
          I = IFLOOR(u * SIZE);                                         \
    }                                                                   \
-   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_ATI) {                 \
+   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);                                \
       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;                                                        \
@@ -322,7 +370,8 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
             }
          }
          else {
-            if (CHAN_TYPE == GL_UNSIGNED_BYTE && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -332,7 +381,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                      rgba[i][BCOMP] = rgba[i][ACOMP] = c;
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -358,7 +409,8 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
             }
          }
          else {
-            if (CHAN_TYPE == GL_UNSIGNED_BYTE && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -367,7 +419,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                   rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = c;
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -392,7 +446,8 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
             }
          }
          else {
-            if (CHAN_TYPE == GL_UNSIGNED_BYTE && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -400,7 +455,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                   rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -428,7 +485,8 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
             }
          }
          else {
-            if (CHAN_TYPE == GL_UNSIGNED_BYTE && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -439,7 +497,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                   rgba[i][ACOMP] = a;
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -470,7 +530,8 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
             }
          }
          else {
-            if (CHAN_TYPE == GL_UNSIGNED_BYTE && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -480,7 +541,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                   rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -497,23 +560,24 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
          break;
       case GL_RGBA:
          /* replace RGBA with RGBA */
-         if (!table->FloatTable) {
+         if (table->FloatTable) {
             const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
-            const GLchan *lut = (const GLchan *) table->Table;
+            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);
-               rgba[i][RCOMP] = lut[jR * 4 + 0];
-               rgba[i][GCOMP] = lut[jG * 4 + 1];
-               rgba[i][BCOMP] = lut[jB * 4 + 2];
-               rgba[i][ACOMP] = lut[jA * 4 + 3];
+               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 && table->Size == 256) {
+#if CHAN_TYPE == GL_UNSIGNED_BYTE
+            if (table->Size == 256) {
                /* common case */
                const GLchan *lut = (const GLchan *) table->Table;
                GLuint i;
@@ -524,7 +588,9 @@ _swrast_texture_table_lookup(const struct gl_color_table *table,
                   rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
                }
             }
-            else {
+            else
+#endif
+            {
                const GLfloat scale = (GLfloat) (table->Size - 1) / CHAN_MAXF;
                const GLfloat *lut = (const GLfloat *) table->Table;
                GLuint i;
@@ -721,7 +787,7 @@ sample_1d_nearest(GLcontext *ctx,
       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);
       }
@@ -773,7 +839,7 @@ sample_1d_linear(GLcontext *ctx,
          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);
          }
@@ -782,7 +848,7 @@ sample_1d_linear(GLcontext *ctx,
          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);
          }
@@ -820,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]);
    }
 }
 
@@ -836,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]);
    }
 }
 
@@ -864,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]);
@@ -894,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]);
@@ -919,7 +985,7 @@ sample_nearest_1d( GLcontext *ctx, GLuint texUnit,
                    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]);
@@ -935,7 +1001,7 @@ sample_linear_1d( GLcontext *ctx, GLuint texUnit,
                   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]);
@@ -968,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:
@@ -1003,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:
@@ -1050,7 +1116,7 @@ sample_2d_nearest(GLcontext *ctx,
       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);
       }
@@ -1118,7 +1184,7 @@ sample_2d_linear(GLcontext *ctx,
          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);
          }
@@ -1127,7 +1193,7 @@ sample_2d_linear(GLcontext *ctx,
          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);
          }
@@ -1136,7 +1202,7 @@ sample_2d_linear(GLcontext *ctx,
          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);
          }
@@ -1145,7 +1211,7 @@ sample_2d_linear(GLcontext *ctx,
          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);
          }
@@ -1200,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);
@@ -1225,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];
@@ -1271,7 +1338,7 @@ sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
    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]);
    }
 }
 
@@ -1288,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]);
    }
 }
 
@@ -1306,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]);
@@ -1337,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]);
@@ -1364,18 +1431,19 @@ sample_2d_linear_mipmap_linear_repeat( GLcontext *ctx,
    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]);
@@ -1392,7 +1460,7 @@ sample_nearest_2d( GLcontext *ctx, GLuint texUnit,
                    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]);
@@ -1408,7 +1476,7 @@ sample_linear_2d( GLcontext *ctx, GLuint texUnit,
                   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]);
@@ -1430,7 +1498,7 @@ opt_sample_rgb_2d( GLcontext *ctx, GLuint texUnit,
                    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;
@@ -1442,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;
@@ -1469,7 +1538,7 @@ opt_sample_rgba_2d( GLcontext *ctx, GLuint texUnit,
                     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;
@@ -1481,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;
@@ -1502,14 +1572,15 @@ sample_lambda_2d( GLcontext *ctx, GLuint texUnit,
                   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->Width == tImg->RowStride))
-      && (tImg->Format != GL_COLOR_INDEX);
+      && (tImg->Format != GL_COLOR_INDEX)
+      && tImg->_IsPowerOfTwo;
 
    ASSERT(lambda != NULL);
    compute_min_mag_ranges(SWRAST_CONTEXT(ctx)->_MinMagThresh[texUnit],
@@ -1520,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,
@@ -1558,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
@@ -1577,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,
@@ -1639,7 +1710,7 @@ sample_3d_nearest(GLcontext *ctx,
       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);
       }
@@ -1722,7 +1793,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1731,7 +1802,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1740,7 +1811,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1749,7 +1820,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1759,7 +1830,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1768,7 +1839,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1777,7 +1848,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1786,7 +1857,7 @@ sample_3d_linear(GLcontext *ctx,
          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);
          }
@@ -1852,7 +1923,7 @@ sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
    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]);
    }
 }
 
@@ -1868,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]);
    }
 }
 
@@ -1885,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]);
@@ -1914,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]);
@@ -1938,7 +2009,7 @@ sample_nearest_3d(GLcontext *ctx, GLuint texUnit,
                   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]);
@@ -1954,7 +2025,7 @@ sample_linear_3d( GLcontext *ctx, GLuint texUnit,
                  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]);
@@ -1986,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:
@@ -2021,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:
@@ -2065,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;
@@ -2084,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;
@@ -2098,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;
@@ -2334,7 +2405,7 @@ sample_nearest_rect(GLcontext *ctx, GLuint texUnit,
                     const GLfloat texcoords[][4], const GLfloat lambda[],
                     GLchan rgba[][4])
 {
-   const struct gl_texture_image *img = tObj->Image[0];
+   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;
@@ -2378,7 +2449,7 @@ sample_nearest_rect(GLcontext *ctx, GLuint texUnit,
       col = CLAMP(col, 0, width_minus_1);
       row = CLAMP(row, 0, height_minus_1);
 
-      (*img->FetchTexel)(img, col, row, 0, (GLvoid *) rgba[i]);
+      img->FetchTexelc(img, col, row, 0, rgba[i]);
    }
 }
 
@@ -2389,7 +2460,7 @@ sample_linear_rect(GLcontext *ctx, GLuint texUnit,
                    const GLfloat texcoords[][4],
                   const GLfloat lambda[], GLchan rgba[][4])
 {
-   const struct gl_texture_image *img = tObj->Image[0];
+   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;
@@ -2445,10 +2516,10 @@ sample_linear_rect(GLcontext *ctx, GLuint texUnit,
       row1 = CLAMP(row1, 0, height_minus_1);
 
       /* get four texel samples */
-      (*img->FetchTexel)(img, col0, row0, 0, (GLvoid *) t00);
-      (*img->FetchTexel)(img, col1, row0, 0, (GLvoid *) t10);
-      (*img->FetchTexel)(img, col0, row1, 0, (GLvoid *) t01);
-      (*img->FetchTexel)(img, col1, row1, 0, (GLvoid *) t11);
+      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);
@@ -2519,7 +2590,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
                       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;
    GLchan ambient;
@@ -2528,7 +2599,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
 
    (void) unit;
 
-   ASSERT(tObj->Image[tObj->BaseLevel]->Format == GL_DEPTH_COMPONENT);
+   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);
@@ -2564,7 +2635,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
          /* 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:
@@ -2655,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) {
@@ -2810,7 +2881,7 @@ sample_depth_texture2(const GLcontext *ctx,
 {
    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;
    GLchan ambient;
@@ -2872,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++;
@@ -2925,7 +2996,7 @@ _swrast_choose_texture_sample_func( GLcontext *ctx,
                                    const struct gl_texture_object *t )
 {
    const GLboolean needLambda = (GLboolean) (t->MinFilter != t->MagFilter);
-   const GLenum format = t->Image[t->BaseLevel]->Format;
+   const GLenum format = t->Image[0][t->BaseLevel]->Format;
 
    if (!t->Complete) {
       return &null_sample_func;
@@ -2962,14 +3033,16 @@ _swrast_choose_texture_sample_func( GLcontext *ctx,
          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) {
+             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->Image[baseLevel]->Border == 0 &&
-                  t->Image[baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
+                  t->_IsPowerOfTwo &&
+                  t->Image[0][baseLevel]->Border == 0 &&
+                  t->Image[0][baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
             return &opt_sample_rgba_2d;
          }
          else {
@@ -3681,7 +3754,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
 #if CHAN_TYPE == GL_FLOAT
                rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP]) + arg1[i][ACOMP]) * Amult;
 #else
-               GLuint a = (PROD(arg0[i][ACOMP], arg2[i][ACOMP])
+               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);
@@ -3790,14 +3863,16 @@ texture_apply( 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
-       || format == GL_YCBCR_MESA) {
+   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:
@@ -4147,11 +4222,14 @@ _swrast_texture_span( GLcontext *ctx, struct sw_span *span )
 
          /* adjust texture lod (lambda) */
          if (span->arrayMask & SPAN_LAMBDA) {
-            if (texUnit->LodBias != 0.0F) {
+            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] += texUnit->LodBias;
+                  lambda[i] += bias;
                }
             }
 
@@ -4171,6 +4249,7 @@ _swrast_texture_span( GLcontext *ctx, struct sw_span *span )
          swrast->TextureSample[unit]( ctx, unit, texUnit->_Current, span->end,
                          (const GLfloat (*)[4]) span->array->texcoords[unit],
                          lambda, texels );
+
          /* GL_SGI_texture_color_table */
          if (texUnit->ColorTableEnabled) {
             _swrast_texture_table_lookup(&texUnit->ColorTable, span->end, texels);