swrast: fix incorrect tri culling in selection/feedback mode.
[mesa.git] / src / mesa / swrast / s_texfilter.c
index c9e9db132f126b76bc2c88e0332bda9c3653629d..dd59314cd9671a5d90ea5a5ac251b1b386135bcb 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.0.3
+ * Version:  7.3
  *
- * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2008  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"),
  */
 
 
-#include "glheader.h"
-#include "context.h"
-#include "colormac.h"
-#include "imports.h"
-#include "texformat.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/colormac.h"
+#include "main/imports.h"
+#include "main/texformat.h"
 
 #include "s_context.h"
 #include "s_texfilter.h"
 
 
-/**
- * Constants for integer linear interpolation.
+/*
+ * Note, the FRAC macro has to work perfectly.  Otherwise you'll sometimes
+ * see 1-pixel bands of improperly weighted linear-filtered textures.
+ * The tests/texwrap.c demo is a good test.
+ * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
+ * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
  */
-#define ILERP_SCALE 65536.0F
-#define ILERP_SHIFT 16
+#define FRAC(f)  ((f) - IFLOOR(f))
+
 
 
 /**
- * Linear interpolation macros
+ * Linear interpolation macro
  */
 #define LERP(T, A, B)  ( (A) + (T) * ((B) - (A)) )
-#define ILERP(IT, A, B)  ( (A) + (((IT) * ((B) - (A))) >> ILERP_SHIFT) )
 
 
 /**
@@ -65,21 +68,6 @@ lerp_2d(GLfloat a, GLfloat b,
 }
 
 
-/**
- * Do 2D/biliner interpolation of integer values.
- * \sa lerp_2d
- */
-static INLINE GLint
-ilerp_2d(GLint ia, GLint ib,
-         GLint v00, GLint v10, GLint v01, GLint v11)
-{
-   /* fixed point interpolants in [0, ILERP_SCALE] */
-   const GLint temp0 = ILERP(ia, v00, v10);
-   const GLint temp1 = ILERP(ia, v01, v11);
-   return ILERP(ib, temp0, temp1);
-}
-
-
 /**
  * Do 3D/trilinear interpolation of float values.
  * \sa lerp_2d
@@ -99,51 +87,16 @@ lerp_3d(GLfloat a, GLfloat b, GLfloat c,
 }
 
 
-/**
- * Do 3D/trilinear interpolation of integer values.
- * \sa lerp_2d
- */
-static INLINE GLint
-ilerp_3d(GLint ia, GLint ib, GLint ic,
-         GLint v000, GLint v100, GLint v010, GLint v110,
-         GLint v001, GLint v101, GLint v011, GLint v111)
-{
-   /* fixed point interpolants in [0, ILERP_SCALE] */
-   const GLint temp00 = ILERP(ia, v000, v100);
-   const GLint temp10 = ILERP(ia, v010, v110);
-   const GLint temp01 = ILERP(ia, v001, v101);
-   const GLint temp11 = ILERP(ia, v011, v111);
-   const GLint temp0 = ILERP(ib, temp00, temp10);
-   const GLint temp1 = ILERP(ib, temp01, temp11);
-   return ILERP(ic, temp0, temp1);
-}
-
-
 /**
  * Do linear interpolation of colors.
  */
 static INLINE void
-lerp_rgba(GLchan result[4], GLfloat t, const GLchan a[4], const GLchan b[4])
+lerp_rgba(GLfloat result[4], GLfloat t, const GLfloat a[4], const GLfloat b[4])
 {
-#if CHAN_TYPE == GL_FLOAT
    result[0] = LERP(t, a[0], b[0]);
    result[1] = LERP(t, a[1], b[1]);
    result[2] = LERP(t, a[2], b[2]);
    result[3] = LERP(t, a[3], b[3]);
-#elif CHAN_TYPE == GL_UNSIGNED_SHORT
-   result[0] = (GLchan) (LERP(t, a[0], b[0]) + 0.5);
-   result[1] = (GLchan) (LERP(t, a[1], b[1]) + 0.5);
-   result[2] = (GLchan) (LERP(t, a[2], b[2]) + 0.5);
-   result[3] = (GLchan) (LERP(t, a[3], b[3]) + 0.5);
-#else
-   /* fixed point interpolants in [0, ILERP_SCALE] */
-   const GLint it = IROUND_POS(t * ILERP_SCALE);
-   ASSERT(CHAN_TYPE == GL_UNSIGNED_BYTE);
-   result[0] = ILERP(it, a[0], b[0]);
-   result[1] = ILERP(it, a[1], b[1]);
-   result[2] = ILERP(it, a[2], b[2]);
-   result[3] = ILERP(it, a[3], b[3]);
-#endif
 }
 
 
@@ -151,29 +104,14 @@ lerp_rgba(GLchan result[4], GLfloat t, const GLchan a[4], const GLchan b[4])
  * Do bilinear interpolation of colors.
  */
 static INLINE void
-lerp_rgba_2d(GLchan result[4], GLfloat a, GLfloat b,
-             const GLchan t00[4], const GLchan t10[4],
-             const GLchan t01[4], const GLchan t11[4])
+lerp_rgba_2d(GLfloat result[4], GLfloat a, GLfloat b,
+             const GLfloat t00[4], const GLfloat t10[4],
+             const GLfloat t01[4], const GLfloat t11[4])
 {
-#if CHAN_TYPE == GL_FLOAT
    result[0] = lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]);
    result[1] = lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]);
    result[2] = lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]);
    result[3] = lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]);
-#elif CHAN_TYPE == GL_UNSIGNED_SHORT
-   result[0] = (GLchan) (lerp_2d(a, b, t00[0], t10[0], t01[0], t11[0]) + 0.5);
-   result[1] = (GLchan) (lerp_2d(a, b, t00[1], t10[1], t01[1], t11[1]) + 0.5);
-   result[2] = (GLchan) (lerp_2d(a, b, t00[2], t10[2], t01[2], t11[2]) + 0.5);
-   result[3] = (GLchan) (lerp_2d(a, b, t00[3], t10[3], t01[3], t11[3]) + 0.5);
-#else
-   const GLint ia = IROUND_POS(a * ILERP_SCALE);
-   const GLint ib = IROUND_POS(b * ILERP_SCALE);
-   ASSERT(CHAN_TYPE == GL_UNSIGNED_BYTE);
-   result[0] = ilerp_2d(ia, ib, t00[0], t10[0], t01[0], t11[0]);
-   result[1] = ilerp_2d(ia, ib, t00[1], t10[1], t01[1], t11[1]);
-   result[2] = ilerp_2d(ia, ib, t00[2], t10[2], t01[2], t11[2]);
-   result[3] = ilerp_2d(ia, ib, t00[3], t10[3], t01[3], t11[3]);
-#endif
 }
 
 
@@ -181,34 +119,18 @@ lerp_rgba_2d(GLchan result[4], GLfloat a, GLfloat b,
  * Do trilinear interpolation of colors.
  */
 static INLINE void
-lerp_rgba_3d(GLchan result[4], GLfloat a, GLfloat b, GLfloat c,
-             const GLchan t000[4], const GLchan t100[4],
-             const GLchan t010[4], const GLchan t110[4],
-             const GLchan t001[4], const GLchan t101[4],
-             const GLchan t011[4], const GLchan t111[4])
+lerp_rgba_3d(GLfloat result[4], GLfloat a, GLfloat b, GLfloat c,
+             const GLfloat t000[4], const GLfloat t100[4],
+             const GLfloat t010[4], const GLfloat t110[4],
+             const GLfloat t001[4], const GLfloat t101[4],
+             const GLfloat t011[4], const GLfloat t111[4])
 {
    GLuint k;
    /* compiler should unroll these short loops */
-#if CHAN_TYPE == GL_FLOAT
    for (k = 0; k < 4; k++) {
       result[k] = lerp_3d(a, b, c, t000[k], t100[k], t010[k], t110[k],
                                    t001[k], t101[k], t011[k], t111[k]);
    }
-#elif CHAN_TYPE == GL_UNSIGNED_SHORT
-   for (k = 0; k < 4; k++) {
-      result[k] = (GLchan)(lerp_3d(a, b, c,
-                                   t000[k], t100[k], t010[k], t110[k],
-                                   t001[k], t101[k], t011[k], t111[k]) + 0.5F);
-   }
-#else
-   GLint ia = IROUND_POS(a * ILERP_SCALE);
-   GLint ib = IROUND_POS(b * ILERP_SCALE);
-   GLint ic = IROUND_POS(c * ILERP_SCALE);
-   for (k = 0; k < 4; k++) {
-      result[k] = ilerp_3d(ia, ib, ic, t000[k], t100[k], t010[k], t110[k],
-                                       t001[k], t101[k], t011[k], t111[k]);
-   }
-#endif
 }
 
 
@@ -216,265 +138,444 @@ lerp_rgba_3d(GLchan result[4], GLfloat a, GLfloat b, GLfloat c,
  * If A is a signed integer, A % B doesn't give the right value for A < 0
  * (in terms of texture repeat).  Just casting to unsigned fixes that.
  */
-#define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
+#define REMAINDER(A, B) (((A) + (B) * 1024) % (B))
 
 
 /**
  * Used to compute texel locations for linear sampling.
  * Input:
  *    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
+ *    s = texcoord in [0,1]
+ *    size = width (or height or depth) of texture
  * Output:
- *    U = texcoord in [0, width]
- *    I0, I1 = two nearest texel indexes
+ *    i0, i1 = returns two nearest texel indexes
+ *    weight = returns blend factor between texels
  */
-#define COMPUTE_LINEAR_TEXEL_LOCATIONS(wrapMode, S, U, SIZE, I0, I1)   \
-{                                                                      \
-   switch (wrapMode) {                                                 \
-   case GL_REPEAT:                                                     \
-      U = S * SIZE - 0.5F;                                             \
-      if (img->_IsPowerOfTwo) {                                                \
-         I0 = IFLOOR(U) & (SIZE - 1);                                  \
-         I1 = (I0 + 1) & (SIZE - 1);                                   \
-      }                                                                        \
-      else {                                                           \
-         I0 = REMAINDER(IFLOOR(U), SIZE);                              \
-         I1 = REMAINDER(I0 + 1, SIZE);                                 \
-      }                                                                        \
-      break;                                                           \
-   case GL_CLAMP_TO_EDGE:                                              \
-      if (S <= 0.0F)                                                   \
-         U = 0.0F;                                                     \
-      else if (S >= 1.0F)                                              \
-         U = (GLfloat) SIZE;                                           \
-      else                                                             \
-         U = S * SIZE;                                                 \
-      U -= 0.5F;                                                       \
-      I0 = IFLOOR(U);                                                  \
-      I1 = I0 + 1;                                                     \
-      if (I0 < 0)                                                      \
-         I0 = 0;                                                       \
-      if (I1 >= (GLint) SIZE)                                          \
-         I1 = SIZE - 1;                                                        \
-      break;                                                           \
-   case GL_CLAMP_TO_BORDER:                                            \
-      {                                                                        \
-         const GLfloat min = -1.0F / (2.0F * SIZE);                    \
-         const GLfloat max = 1.0F - min;                               \
-         if (S <= min)                                                 \
-            U = min * SIZE;                                            \
-         else if (S >= max)                                            \
-            U = max * SIZE;                                            \
-         else                                                          \
-            U = S * SIZE;                                              \
-         U -= 0.5F;                                                    \
-         I0 = IFLOOR(U);                                               \
-         I1 = I0 + 1;                                                  \
-      }                                                                        \
-      break;                                                           \
-   case 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;                                             \
-      }                                                                        \
-      break;                                                           \
-   case GL_MIRROR_CLAMP_EXT:                                           \
-      U = FABSF(S);                                                    \
-      if (U >= 1.0F)                                                   \
-         U = (GLfloat) SIZE;                                           \
-      else                                                             \
-         U *= SIZE;                                                    \
-      U -= 0.5F;                                                       \
-      I0 = IFLOOR(U);                                                  \
-      I1 = I0 + 1;                                                     \
-      break;                                                           \
-   case GL_MIRROR_CLAMP_TO_EDGE_EXT:                                   \
-      U = FABSF(S);                                                    \
-      if (U >= 1.0F)                                                   \
-         U = (GLfloat) SIZE;                                           \
-      else                                                             \
-         U *= SIZE;                                                    \
-      U -= 0.5F;                                                       \
-      I0 = IFLOOR(U);                                                  \
-      I1 = I0 + 1;                                                     \
-      if (I0 < 0)                                                      \
-         I0 = 0;                                                       \
-      if (I1 >= (GLint) SIZE)                                          \
-         I1 = SIZE - 1;                                                        \
-      break;                                                           \
-   case GL_MIRROR_CLAMP_TO_BORDER_EXT:                                 \
-      {                                                                        \
-         const GLfloat min = -1.0F / (2.0F * SIZE);                    \
-         const GLfloat max = 1.0F - min;                               \
-         U = FABSF(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;                                                  \
-      }                                                                        \
-      break;                                                           \
-   case GL_CLAMP:                                                      \
-      if (S <= 0.0F)                                                   \
-         U = 0.0F;                                                     \
-      else if (S >= 1.0F)                                              \
-         U = (GLfloat) SIZE;                                           \
-      else                                                             \
-         U = S * SIZE;                                                 \
-      U -= 0.5F;                                                       \
-      I0 = IFLOOR(U);                                                  \
-      I1 = I0 + 1;                                                     \
-      break;                                                           \
-   default:                                                            \
-      _mesa_problem(ctx, "Bad wrap mode");                             \
-   }                                                                   \
+static INLINE void
+linear_texel_locations(GLenum wrapMode,
+                       const struct gl_texture_image *img,
+                       GLint size, GLfloat s,
+                       GLint *i0, GLint *i1, GLfloat *weight)
+{
+   GLfloat u;
+   switch (wrapMode) {
+   case GL_REPEAT:
+      u = s * size - 0.5F;
+      if (img->_IsPowerOfTwo) {
+         *i0 = IFLOOR(u) & (size - 1);
+         *i1 = (*i0 + 1) & (size - 1);
+      }
+      else {
+         *i0 = REMAINDER(IFLOOR(u), size);
+         *i1 = REMAINDER(*i0 + 1, size);
+      }
+      break;
+   case GL_CLAMP_TO_EDGE:
+      if (s <= 0.0F)
+         u = 0.0F;
+      else if (s >= 1.0F)
+         u = (GLfloat) size;
+      else
+         u = s * size;
+      u -= 0.5F;
+      *i0 = IFLOOR(u);
+      *i1 = *i0 + 1;
+      if (*i0 < 0)
+         *i0 = 0;
+      if (*i1 >= (GLint) size)
+         *i1 = size - 1;
+      break;
+   case GL_CLAMP_TO_BORDER:
+      {
+         const GLfloat min = -1.0F / (2.0F * size);
+         const GLfloat max = 1.0F - min;
+         if (s <= min)
+            u = min * size;
+         else if (s >= max)
+            u = max * size;
+         else
+            u = s * size;
+         u -= 0.5F;
+         *i0 = IFLOOR(u);
+         *i1 = *i0 + 1;
+      }
+      break;
+   case GL_MIRRORED_REPEAT:
+      {
+         const GLint flr = IFLOOR(s);
+         if (flr & 1)
+            u = 1.0F - (s - (GLfloat) flr);
+         else
+            u = s - (GLfloat) flr;
+         u = (u * size) - 0.5F;
+         *i0 = IFLOOR(u);
+         *i1 = *i0 + 1;
+         if (*i0 < 0)
+            *i0 = 0;
+         if (*i1 >= (GLint) size)
+            *i1 = size - 1;
+      }
+      break;
+   case GL_MIRROR_CLAMP_EXT:
+      u = FABSF(s);
+      if (u >= 1.0F)
+         u = (GLfloat) size;
+      else
+         u *= size;
+      u -= 0.5F;
+      *i0 = IFLOOR(u);
+      *i1 = *i0 + 1;
+      break;
+   case GL_MIRROR_CLAMP_TO_EDGE_EXT:
+      u = FABSF(s);
+      if (u >= 1.0F)
+         u = (GLfloat) size;
+      else
+         u *= size;
+      u -= 0.5F;
+      *i0 = IFLOOR(u);
+      *i1 = *i0 + 1;
+      if (*i0 < 0)
+         *i0 = 0;
+      if (*i1 >= (GLint) size)
+         *i1 = size - 1;
+      break;
+   case GL_MIRROR_CLAMP_TO_BORDER_EXT:
+      {
+         const GLfloat min = -1.0F / (2.0F * size);
+         const GLfloat max = 1.0F - min;
+         u = FABSF(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;
+      }
+      break;
+   case GL_CLAMP:
+      if (s <= 0.0F)
+         u = 0.0F;
+      else if (s >= 1.0F)
+         u = (GLfloat) size;
+      else
+         u = s * size;
+      u -= 0.5F;
+      *i0 = IFLOOR(u);
+      *i1 = *i0 + 1;
+      break;
+   default:
+      _mesa_problem(NULL, "Bad wrap mode");
+      u = 0.0F;
+   }
+   *weight = FRAC(u);
 }
 
 
 /**
  * Used to compute texel location for nearest sampling.
  */
-#define COMPUTE_NEAREST_TEXEL_LOCATION(wrapMode, S, SIZE, I)           \
-{                                                                      \
-   switch (wrapMode) {                                                 \
-   case GL_REPEAT:                                                     \
-      /* s limited to [0,1) */                                         \
-      /* i limited to [0,size-1] */                                    \
-      I = IFLOOR(S * SIZE);                                            \
-      if (img->_IsPowerOfTwo)                                          \
-         I &= (SIZE - 1);                                              \
-      else                                                             \
-         I = REMAINDER(I, SIZE);                                       \
-      break;                                                           \
-   case GL_CLAMP_TO_EDGE:                                              \
-      {                                                                        \
-         /* 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;                               \
-         if (S < min)                                                  \
-            I = 0;                                                     \
-         else if (S > max)                                             \
-            I = SIZE - 1;                                              \
-         else                                                          \
-            I = IFLOOR(S * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case GL_CLAMP_TO_BORDER:                                            \
-      {                                                                        \
-         /* s limited to [min,max] */                                  \
-         /* i limited to [-1, size] */                                 \
-         const GLfloat min = -1.0F / (2.0F * SIZE);                    \
-         const GLfloat max = 1.0F - min;                               \
-         if (S <= min)                                                 \
-            I = -1;                                                    \
-         else if (S >= max)                                            \
-            I = SIZE;                                                  \
-         else                                                          \
-            I = IFLOOR(S * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case GL_MIRRORED_REPEAT:                                            \
-      {                                                                        \
-         const GLfloat min = 1.0F / (2.0F * SIZE);                     \
-         const GLfloat max = 1.0F - min;                               \
-         const GLint flr = IFLOOR(S);                                  \
-         GLfloat u;                                                    \
-         if (flr & 1)                                                  \
-            u = 1.0F - (S - (GLfloat) flr);    /* flr is odd */        \
-         else                                                          \
-            u = S - (GLfloat) flr;             /* flr is even */       \
-         if (u < min)                                                  \
-            I = 0;                                                     \
-         else if (u > max)                                             \
-            I = SIZE - 1;                                              \
-         else                                                          \
-            I = IFLOOR(u * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case GL_MIRROR_CLAMP_EXT:                                           \
-      {                                                                        \
-         /* s limited to [0,1] */                                      \
-         /* i limited to [0,size-1] */                                 \
-         const GLfloat u = FABSF(S);                                   \
-         if (u <= 0.0F)                                                        \
-            I = 0;                                                     \
-         else if (u >= 1.0F)                                           \
-            I = SIZE - 1;                                              \
-         else                                                          \
-            I = IFLOOR(u * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case 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 = FABSF(S);                                   \
-         if (u < min)                                                  \
-            I = 0;                                                     \
-         else if (u > max)                                             \
-            I = SIZE - 1;                                              \
-         else                                                          \
-            I = IFLOOR(u * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case 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 = FABSF(S);                                   \
-         if (u < min)                                                  \
-            I = -1;                                                    \
-         else if (u > max)                                             \
-            I = SIZE;                                                  \
-         else                                                          \
-            I = IFLOOR(u * SIZE);                                      \
-      }                                                                        \
-      break;                                                           \
-   case GL_CLAMP:                                                      \
-      /* s limited to [0,1] */                                         \
-      /* i limited to [0,size-1] */                                    \
-      if (S <= 0.0F)                                                   \
-         I = 0;                                                                \
-      else if (S >= 1.0F)                                              \
-         I = SIZE - 1;                                                 \
-      else                                                             \
-         I = IFLOOR(S * SIZE);                                         \
-      break;                                                           \
-   default:                                                            \
-      _mesa_problem(ctx, "Bad wrap mode");                             \
-   }                                                                   \
+static INLINE GLint
+nearest_texel_location(GLenum wrapMode,
+                       const struct gl_texture_image *img,
+                       GLint size, GLfloat s)
+{
+   GLint i;
+
+   switch (wrapMode) {
+   case GL_REPEAT:
+      /* s limited to [0,1) */
+      /* i limited to [0,size-1] */
+      i = IFLOOR(s * size);
+      if (img->_IsPowerOfTwo)
+         i &= (size - 1);
+      else
+         i = REMAINDER(i, size);
+      return i;
+   case GL_CLAMP_TO_EDGE:
+      {
+         /* 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;
+         if (s < min)
+            i = 0;
+         else if (s > max)
+            i = size - 1;
+         else
+            i = IFLOOR(s * size);
+      }
+      return i;
+   case GL_CLAMP_TO_BORDER:
+      {
+         /* s limited to [min,max] */
+         /* i limited to [-1, size] */
+         const GLfloat min = -1.0F / (2.0F * size);
+         const GLfloat max = 1.0F - min;
+         if (s <= min)
+            i = -1;
+         else if (s >= max)
+            i = size;
+         else
+            i = IFLOOR(s * size);
+      }
+      return i;
+   case GL_MIRRORED_REPEAT:
+      {
+         const GLfloat min = 1.0F / (2.0F * size);
+         const GLfloat max = 1.0F - min;
+         const GLint flr = IFLOOR(s);
+         GLfloat u;
+         if (flr & 1)
+            u = 1.0F - (s - (GLfloat) flr);
+         else
+            u = s - (GLfloat) flr;
+         if (u < min)
+            i = 0;
+         else if (u > max)
+            i = size - 1;
+         else
+            i = IFLOOR(u * size);
+      }
+      return i;
+   case GL_MIRROR_CLAMP_EXT:
+      {
+         /* s limited to [0,1] */
+         /* i limited to [0,size-1] */
+         const GLfloat u = FABSF(s);
+         if (u <= 0.0F)
+            i = 0;
+         else if (u >= 1.0F)
+            i = size - 1;
+         else
+            i = IFLOOR(u * size);
+      }
+      return i;
+   case 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 = FABSF(s);
+         if (u < min)
+            i = 0;
+         else if (u > max)
+            i = size - 1;
+         else
+            i = IFLOOR(u * size);
+      }
+      return i;
+   case 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 = FABSF(s);
+         if (u < min)
+            i = -1;
+         else if (u > max)
+            i = size;
+         else
+            i = IFLOOR(u * size);
+      }
+      return i;
+   case GL_CLAMP:
+      /* s limited to [0,1] */
+      /* i limited to [0,size-1] */
+      if (s <= 0.0F)
+         i = 0;
+      else if (s >= 1.0F)
+         i = size - 1;
+      else
+         i = IFLOOR(s * size);
+      return i;
+   default:
+      _mesa_problem(NULL, "Bad wrap mode");
+      return 0;
+   }
 }
 
 
 /* Power of two image sizes only */
-#define COMPUTE_LINEAR_REPEAT_TEXEL_LOCATION(S, U, SIZE, I0, I1)       \
-{                                                                      \
-   U = S * SIZE - 0.5F;                                                        \
-   I0 = IFLOOR(U) & (SIZE - 1);                                                \
-   I1 = (I0 + 1) & (SIZE - 1);                                         \
+static INLINE void
+linear_repeat_texel_location(GLuint size, GLfloat s,
+                             GLint *i0, GLint *i1, GLfloat *weight)
+{
+   GLfloat u = s * size - 0.5F;
+   *i0 = IFLOOR(u) & (size - 1);
+   *i1 = (*i0 + 1) & (size - 1);
+   *weight = FRAC(u);
+}
+
+
+/**
+ * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
+ */
+static INLINE GLint
+clamp_rect_coord_nearest(GLenum wrapMode, GLfloat coord, GLint max)
+{
+   switch (wrapMode) {
+   case GL_CLAMP:
+      return IFLOOR( CLAMP(coord, 0.0F, max - 1) );
+   case GL_CLAMP_TO_EDGE:
+      return IFLOOR( CLAMP(coord, 0.5F, max - 0.5F) );
+   case GL_CLAMP_TO_BORDER:
+      return IFLOOR( CLAMP(coord, -0.5F, max + 0.5F) );
+   default:
+      _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_nearest");
+      return 0;
+   }
+}
+
+
+/**
+ * As above, but GL_LINEAR filtering.
+ */
+static INLINE void
+clamp_rect_coord_linear(GLenum wrapMode, GLfloat coord, GLint max,
+                        GLint *i0out, GLint *i1out, GLfloat *weight)
+{
+   GLfloat fcol;
+   GLint i0, i1;
+   switch (wrapMode) {
+   case GL_CLAMP:
+      /* Not exactly what the spec says, but it matches NVIDIA output */
+      fcol = CLAMP(coord - 0.5F, 0.0, max-1);
+      i0 = IFLOOR(fcol);
+      i1 = i0 + 1;
+      break;
+   case GL_CLAMP_TO_EDGE:
+      fcol = CLAMP(coord, 0.5F, max - 0.5F);
+      fcol -= 0.5F;
+      i0 = IFLOOR(fcol);
+      i1 = i0 + 1;
+      if (i1 > max - 1)
+         i1 = max - 1;
+      break;
+   case GL_CLAMP_TO_BORDER:
+      fcol = CLAMP(coord, -0.5F, max + 0.5F);
+      fcol -= 0.5F;
+      i0 = IFLOOR(fcol);
+      i1 = i0 + 1;
+      break;
+   default:
+      _mesa_problem(NULL, "bad wrapMode in clamp_rect_coord_linear");
+      i0 = i1 = 0;
+      fcol = 0.0F;
+   }
+   *i0out = i0;
+   *i1out = i1;
+   *weight = FRAC(fcol);
+}
+
+
+/**
+ * Compute nearest integer texcoords for given texobj and coordinate.
+ */
+static INLINE void
+nearest_texcoord(const struct gl_texture_object *texObj,
+                 const GLfloat texcoord[4],
+                 GLint *i, GLint *j, GLint *k)
+{
+   const GLint baseLevel = texObj->BaseLevel;
+   const struct gl_texture_image *img = texObj->Image[0][baseLevel];
+   const GLint width = img->Width;
+   const GLint height = img->Height;
+   const GLint depth = img->Depth;
+
+   switch (texObj->Target) {
+   case GL_TEXTURE_RECTANGLE_ARB:
+      *i = clamp_rect_coord_nearest(texObj->WrapS, texcoord[0], width);
+      *j = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
+      *k = 0;
+      break;
+   case GL_TEXTURE_1D:
+      *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
+      *j = 0;
+      *k = 0;
+      break;
+   case GL_TEXTURE_2D:
+      *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
+      *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
+      *k = 0;
+      break;
+   case GL_TEXTURE_1D_ARRAY_EXT:
+      *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
+      *j = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
+      *k = 0;
+      break;
+   case GL_TEXTURE_2D_ARRAY_EXT:
+      *i = nearest_texel_location(texObj->WrapS, img, width, texcoord[0]);
+      *j = nearest_texel_location(texObj->WrapT, img, height, texcoord[1]);
+      *k = clamp_rect_coord_nearest(texObj->WrapR, texcoord[2], depth);
+      break;
+   default:
+      *i = *j = *k = 0;
+   }
 }
 
 
+/**
+ * Compute linear integer texcoords for given texobj and coordinate.
+ */
+static INLINE void
+linear_texcoord(const struct gl_texture_object *texObj,
+                const GLfloat texcoord[4],
+                GLint *i0, GLint *i1, GLint *j0, GLint *j1, GLint *slice,
+                GLfloat *wi, GLfloat *wj)
+{
+   const GLint baseLevel = texObj->BaseLevel;
+   const struct gl_texture_image *img = texObj->Image[0][baseLevel];
+   const GLint width = img->Width;
+   const GLint height = img->Height;
+   const GLint depth = img->Depth;
+
+   switch (texObj->Target) {
+   case GL_TEXTURE_RECTANGLE_ARB:
+      clamp_rect_coord_linear(texObj->WrapS, texcoord[0],
+                              width, i0, i1, wi);
+      clamp_rect_coord_linear(texObj->WrapT, texcoord[1],
+                              height, j0, j1, wj);
+      *slice = 0;
+      break;
+
+   case GL_TEXTURE_1D:
+   case GL_TEXTURE_2D:
+      linear_texel_locations(texObj->WrapS, img, width,
+                             texcoord[0], i0, i1, wi);
+      linear_texel_locations(texObj->WrapT, img, height,
+                             texcoord[1], j0, j1, wj);
+      *slice = 0;
+      break;
+
+   case GL_TEXTURE_1D_ARRAY_EXT:
+      linear_texel_locations(texObj->WrapS, img, width,
+                             texcoord[0], i0, i1, wi);
+      *j0 = clamp_rect_coord_nearest(texObj->WrapT, texcoord[1], height);
+      *j1 = *j0;
+      *slice = 0;
+      break;
+
+   case GL_TEXTURE_2D_ARRAY_EXT:
+      linear_texel_locations(texObj->WrapS, img, width,
+                             texcoord[0], i0, i1, wi);
+      linear_texel_locations(texObj->WrapT, img, height,
+                             texcoord[1], j0, j1, wj);
+      *slice = clamp_rect_coord_nearest(texObj->WrapR, texcoord[2], depth);
+      break;
+
+   default:
+      *slice = 0;
+   }
+}
+
+
+
 /**
  * For linear interpolation between mipmap levels N and N+1, this function
  * computes N.
@@ -513,17 +614,6 @@ nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
 
 
 
-/*
- * Note, the FRAC macro has to work perfectly.  Otherwise you'll sometimes
- * see 1-pixel bands of improperly weighted linear-filtered textures.
- * The tests/texwrap.c demo is a good test.
- * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
- * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
- */
-#define FRAC(f)  ((f) - IFLOOR(f))
-
-
-
 /*
  * Bitflags for texture border color sampling.
  */
@@ -536,7 +626,7 @@ nearest_mipmap_level(const struct gl_texture_object *tObj, GLfloat lambda)
 
 
 
-/*
+/**
  * The lambda[] array values are always monotonic.  Either the whole span
  * will be minified, magnified, or split between the two.  This function
  * determines the subranges in [0, n-1] that are to be minified or magnified.
@@ -645,51 +735,88 @@ compute_min_mag_ranges(const struct gl_texture_object *tObj,
 }
 
 
+/**
+ * When we sample the border color, it must be interpreted according to
+ * the base texture format.  Ex: if the texture base format it GL_ALPHA,
+ * we return (0,0,0,BorderAlpha).
+ */
+static INLINE void
+get_border_color(const struct gl_texture_object *tObj,
+                 const struct gl_texture_image *img,
+                 GLfloat rgba[4])
+{
+   switch (img->TexFormat->BaseFormat) {
+   case GL_RGB:
+      rgba[0] = tObj->BorderColor[0];
+      rgba[1] = tObj->BorderColor[1];
+      rgba[2] = tObj->BorderColor[2];
+      rgba[3] = 1.0F;
+      break;
+   case GL_ALPHA:
+      rgba[0] = rgba[1] = rgba[2] = 0.0;
+      rgba[3] = tObj->BorderColor[3];
+      break;
+   case GL_LUMINANCE:
+      rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
+      rgba[3] = 1.0;
+      break;
+   case GL_LUMINANCE_ALPHA:
+      rgba[0] = rgba[1] = rgba[2] = tObj->BorderColor[0];
+      rgba[3] = tObj->BorderColor[3];
+      break;
+   case GL_INTENSITY:
+      rgba[0] = rgba[1] = rgba[2] = rgba[3] = tObj->BorderColor[0];
+      break;
+   default:
+      COPY_4V(rgba, tObj->BorderColor);
+   }
+}
+
+
 /**********************************************************************/
 /*                    1-D Texture Sampling Functions                  */
 /**********************************************************************/
 
-/*
+/**
  * Return the texture sample for coordinate (s) using GL_NEAREST filter.
  */
-static void
+static INLINE void
 sample_1d_nearest(GLcontext *ctx,
                   const struct gl_texture_object *tObj,
                   const struct gl_texture_image *img,
-                  const GLfloat texcoord[4], GLchan rgba[4])
+                  const GLfloat texcoord[4], GLfloat rgba[4])
 {
    const GLint width = img->Width2;  /* without border, power of two */
    GLint i;
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width, i);
+   i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
    /* skip over the border, if any */
    i += img->Border;
    if (i < 0 || i >= (GLint) img->Width) {
       /* Need this test for GL_CLAMP_TO_BORDER mode */
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      get_border_color(tObj, img, rgba);
    }
    else {
-      img->FetchTexelc(img, i, 0, 0, rgba);
+      img->FetchTexelf(img, i, 0, 0, rgba);
    }
 }
 
 
-/*
+/**
  * Return the texture sample for coordinate (s) using GL_LINEAR filter.
  */
-static void
+static INLINE void
 sample_1d_linear(GLcontext *ctx,
                  const struct gl_texture_object *tObj,
                  const struct gl_texture_image *img,
-                 const GLfloat texcoord[4], GLchan rgba[4])
+                 const GLfloat texcoord[4], GLfloat rgba[4])
 {
    const GLint width = img->Width2;
    GLint i0, i1;
-   GLfloat u;
    GLbitfield useBorderColor = 0x0;
    GLfloat a;
-   GLchan t0[4], t1[4];  /* texels */
+   GLfloat t0[4], t1[4];  /* texels */
 
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width, i0, i1);
+   linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
 
    if (img->Border) {
       i0 += img->Border;
@@ -702,19 +829,18 @@ sample_1d_linear(GLcontext *ctx,
 
    /* fetch texel colors */
    if (useBorderColor & I0BIT) {
-      COPY_CHAN4(t0, tObj->_BorderChan);
+      get_border_color(tObj, img, t0);
    }
    else {
-      img->FetchTexelc(img, i0, 0, 0, t0);
+      img->FetchTexelf(img, i0, 0, 0, t0);
    }
    if (useBorderColor & I1BIT) {
-      COPY_CHAN4(t1, tObj->_BorderChan);
+      get_border_color(tObj, img, t1);
    }
    else {
-      img->FetchTexelc(img, i1, 0, 0, t1);
+      img->FetchTexelf(img, i1, 0, 0, t1);
    }
 
-   a = FRAC(u);
    lerp_rgba(rgba, a, t0, t1);
 }
 
@@ -723,7 +849,7 @@ static void
 sample_1d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
                                  GLuint n, const GLfloat texcoord[][4],
-                                 const GLfloat lambda[], GLchan rgba[][4])
+                                 const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -738,7 +864,7 @@ static void
 sample_1d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -753,7 +879,7 @@ static void
 sample_1d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -764,7 +890,7 @@ sample_1d_nearest_mipmap_linear(GLcontext *ctx,
                            texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];
+         GLfloat t0[4], t1[4];
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -774,12 +900,11 @@ sample_1d_nearest_mipmap_linear(GLcontext *ctx,
 }
 
 
-
 static void
 sample_1d_linear_mipmap_linear(GLcontext *ctx,
                                const struct gl_texture_object *tObj,
                                GLuint n, const GLfloat texcoord[][4],
-                               const GLfloat lambda[], GLchan rgba[][4])
+                               const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -790,7 +915,7 @@ sample_1d_linear_mipmap_linear(GLcontext *ctx,
                           texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];
+         GLfloat t0[4], t1[4];
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -800,48 +925,44 @@ sample_1d_linear_mipmap_linear(GLcontext *ctx,
 }
 
 
-
+/** Sample 1D texture, nearest filtering for both min/magnification */
 static void
 sample_nearest_1d( GLcontext *ctx,
                    const struct gl_texture_object *tObj, GLuint n,
                    const GLfloat texcoords[][4], const GLfloat lambda[],
-                   GLchan rgba[][4] )
+                   GLfloat rgba[][4] )
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_1d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-
+/** Sample 1D texture, linear filtering for both min/magnification */
 static void
 sample_linear_1d( GLcontext *ctx,
                   const struct gl_texture_object *tObj, GLuint n,
                   const GLfloat texcoords[][4], const GLfloat lambda[],
-                  GLchan rgba[][4] )
+                  GLfloat rgba[][4] )
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_1d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-/*
- * Given an (s) texture coordinate and lambda (level of detail) value,
- * return a texture sample.
- *
- */
+/** Sample 1D texture, using lambda to choose between min/magnification */
 static void
 sample_lambda_1d( GLcontext *ctx,
                   const struct gl_texture_object *tObj, GLuint n,
                   const GLfloat texcoords[][4],
-                  const GLfloat lambda[], GLchan rgba[][4] )
+                  const GLfloat lambda[], GLfloat rgba[][4] )
 {
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
@@ -913,7 +1034,7 @@ sample_lambda_1d( GLcontext *ctx,
 /**********************************************************************/
 
 
-/*
+/**
  * Return the texture sample for coordinate (s,t) using GL_NEAREST filter.
  */
 static INLINE void
@@ -921,15 +1042,15 @@ sample_2d_nearest(GLcontext *ctx,
                   const struct gl_texture_object *tObj,
                   const struct gl_texture_image *img,
                   const GLfloat texcoord[4],
-                  GLchan rgba[])
+                  GLfloat rgba[])
 {
    const GLint width = img->Width2;    /* without border, power of two */
    const GLint height = img->Height2;  /* without border, power of two */
    GLint i, j;
    (void) ctx;
 
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width,  i);
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoord[1], height, j);
+   i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
+   j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
 
    /* skip over the border, if any */
    i += img->Border;
@@ -937,15 +1058,14 @@ sample_2d_nearest(GLcontext *ctx,
 
    if (i < 0 || i >= (GLint) img->Width || j < 0 || j >= (GLint) img->Height) {
       /* Need this test for GL_CLAMP_TO_BORDER mode */
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      get_border_color(tObj, img, rgba);
    }
    else {
-      img->FetchTexelc(img, i, j, 0, rgba);
+      img->FetchTexelf(img, i, j, 0, rgba);
    }
 }
 
 
-
 /**
  * Return the texture sample for coordinate (s,t) using GL_LINEAR filter.
  * New sampling code contributed by Lynn Quam <quam@ai.sri.com>.
@@ -955,18 +1075,17 @@ sample_2d_linear(GLcontext *ctx,
                  const struct gl_texture_object *tObj,
                  const struct gl_texture_image *img,
                  const GLfloat texcoord[4],
-                 GLchan rgba[])
+                 GLfloat rgba[])
 {
    const GLint width = img->Width2;
    const GLint height = img->Height2;
    GLint i0, j0, i1, j1;
    GLbitfield useBorderColor = 0x0;
-   GLfloat u, v;
    GLfloat a, b;
-   GLchan t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
+   GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
 
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width,  i0, i1);
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoord[1], v, height, j0, j1);
+   linear_texel_locations(tObj->WrapS, img, width, texcoord[0],  &i0, &i1, &a);
+   linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
 
    if (img->Border) {
       i0 += img->Border;
@@ -983,37 +1102,35 @@ sample_2d_linear(GLcontext *ctx,
 
    /* fetch four texel colors */
    if (useBorderColor & (I0BIT | J0BIT)) {
-      COPY_CHAN4(t00, tObj->_BorderChan);
+      get_border_color(tObj, img, t00);
    }
    else {
-      img->FetchTexelc(img, i0, j0, 0, t00);
+      img->FetchTexelf(img, i0, j0, 0, t00);
    }
    if (useBorderColor & (I1BIT | J0BIT)) {
-      COPY_CHAN4(t10, tObj->_BorderChan);
+      get_border_color(tObj, img, t10);
    }
    else {
-      img->FetchTexelc(img, i1, j0, 0, t10);
+      img->FetchTexelf(img, i1, j0, 0, t10);
    }
    if (useBorderColor & (I0BIT | J1BIT)) {
-      COPY_CHAN4(t01, tObj->_BorderChan);
+      get_border_color(tObj, img, t01);
    }
    else {
-      img->FetchTexelc(img, i0, j1, 0, t01);
+      img->FetchTexelf(img, i0, j1, 0, t01);
    }
    if (useBorderColor & (I1BIT | J1BIT)) {
-      COPY_CHAN4(t11, tObj->_BorderChan);
+      get_border_color(tObj, img, t11);
    }
    else {
-      img->FetchTexelc(img, i1, j1, 0, t11);
+      img->FetchTexelf(img, i1, j1, 0, t11);
    }
 
-   a = FRAC(u);
-   b = FRAC(v);
    lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
 }
 
 
-/*
+/**
  * As above, but we know WRAP_S == REPEAT and WRAP_T == REPEAT.
  * We don't have to worry about the texture border.
  */
@@ -1022,14 +1139,13 @@ sample_2d_linear_repeat(GLcontext *ctx,
                         const struct gl_texture_object *tObj,
                         const struct gl_texture_image *img,
                         const GLfloat texcoord[4],
-                        GLchan rgba[])
+                        GLfloat rgba[])
 {
    const GLint width = img->Width2;
    const GLint height = img->Height2;
    GLint i0, j0, i1, j1;
-   GLfloat u, v;
-   GLfloat a, b;
-   GLchan t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
+   GLfloat wi, wj;
+   GLfloat t00[4], t10[4], t01[4], t11[4]; /* sampled texel colors */
 
    (void) ctx;
 
@@ -1039,26 +1155,23 @@ sample_2d_linear_repeat(GLcontext *ctx,
    ASSERT(img->TexFormat->BaseFormat != 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);
+   linear_repeat_texel_location(width,  texcoord[0], &i0, &i1, &wi);
+   linear_repeat_texel_location(height, texcoord[1], &j0, &j1, &wj);
 
-   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);
+   img->FetchTexelf(img, i0, j0, 0, t00);
+   img->FetchTexelf(img, i1, j0, 0, t10);
+   img->FetchTexelf(img, i0, j1, 0, t01);
+   img->FetchTexelf(img, i1, j1, 0, t11);
 
-   a = FRAC(u);
-   b = FRAC(v);
-   lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
+   lerp_rgba_2d(rgba, wi, wj, t00, t10, t01, t11);
 }
 
 
-
 static void
 sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
                                  GLuint n, const GLfloat texcoord[][4],
-                                 const GLfloat lambda[], GLchan rgba[][4])
+                                 const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    for (i = 0; i < n; i++) {
@@ -1068,12 +1181,11 @@ sample_2d_nearest_mipmap_nearest(GLcontext *ctx,
 }
 
 
-
 static void
 sample_2d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1084,12 +1196,11 @@ sample_2d_linear_mipmap_nearest(GLcontext *ctx,
 }
 
 
-
 static void
 sample_2d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1100,7 +1211,7 @@ sample_2d_nearest_mipmap_linear(GLcontext *ctx,
                            texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -1110,13 +1221,11 @@ sample_2d_nearest_mipmap_linear(GLcontext *ctx,
 }
 
 
-
-/* Trilinear filtering */
 static void
 sample_2d_linear_mipmap_linear( GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4] )
+                                const GLfloat lambda[], GLfloat rgba[][4] )
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1127,7 +1236,7 @@ sample_2d_linear_mipmap_linear( GLcontext *ctx,
                           texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -1138,10 +1247,10 @@ 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, const GLfloat texcoord[][4],
-                                       const GLfloat lambda[], GLchan rgba[][4] )
+sample_2d_linear_mipmap_linear_repeat(GLcontext *ctx,
+                                      const struct gl_texture_object *tObj,
+                                      GLuint n, const GLfloat texcoord[][4],
+                                      const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1154,37 +1263,40 @@ sample_2d_linear_mipmap_linear_repeat( GLcontext *ctx,
                                  texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         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);
+         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);
          lerp_rgba(rgba[i], f, t0, t1);
       }
    }
 }
 
 
+/** Sample 2D texture, nearest filtering for both min/magnification */
 static void
-sample_nearest_2d( GLcontext *ctx,
-                   const struct gl_texture_object *tObj, GLuint n,
-                   const GLfloat texcoords[][4],
-                   const GLfloat lambda[], GLchan rgba[][4] )
+sample_nearest_2d(GLcontext *ctx,
+                  const struct gl_texture_object *tObj, GLuint n,
+                  const GLfloat texcoords[][4],
+                  const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_2d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-
+/** Sample 2D texture, linear filtering for both min/magnification */
 static void
-sample_linear_2d( GLcontext *ctx,
-                  const struct gl_texture_object *tObj, GLuint n,
-                  const GLfloat texcoords[][4],
-                  const GLfloat lambda[], GLchan rgba[][4] )
+sample_linear_2d(GLcontext *ctx,
+                 const struct gl_texture_object *tObj, GLuint n,
+                 const GLfloat texcoords[][4],
+                 const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
@@ -1193,19 +1305,19 @@ sample_linear_2d( GLcontext *ctx,
        tObj->WrapT == GL_REPEAT &&
        image->_IsPowerOfTwo &&
        image->Border == 0) {
-      for (i=0;i<n;i++) {
+      for (i = 0; i < n; i++) {
          sample_2d_linear_repeat(ctx, tObj, image, texcoords[i], rgba[i]);
       }
    }
    else {
-      for (i=0;i<n;i++) {
+      for (i = 0; i < n; i++) {
          sample_2d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
       }
    }
 }
 
 
-/*
+/**
  * Optimized 2-D texture sampling:
  *    S and T wrap mode == GL_REPEAT
  *    GL_NEAREST min/mag filter
@@ -1214,10 +1326,10 @@ sample_linear_2d( GLcontext *ctx,
  *    Format = GL_RGB
  */
 static void
-opt_sample_rgb_2d( GLcontext *ctx,
-                   const struct gl_texture_object *tObj,
-                   GLuint n, const GLfloat texcoords[][4],
-                   const GLfloat lambda[], GLchan rgba[][4] )
+opt_sample_rgb_2d(GLcontext *ctx,
+                  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][tObj->BaseLevel];
    const GLfloat width = (GLfloat) img->Width;
@@ -1246,7 +1358,7 @@ opt_sample_rgb_2d( GLcontext *ctx,
 }
 
 
-/*
+/**
  * Optimized 2-D texture sampling:
  *    S and T wrap mode == GL_REPEAT
  *    GL_NEAREST min/mag filter
@@ -1255,10 +1367,10 @@ opt_sample_rgb_2d( GLcontext *ctx,
  *    Format = GL_RGBA
  */
 static void
-opt_sample_rgba_2d( GLcontext *ctx,
-                    const struct gl_texture_object *tObj,
-                    GLuint n, const GLfloat texcoords[][4],
-                    const GLfloat lambda[], GLchan rgba[][4] )
+opt_sample_rgba_2d(GLcontext *ctx,
+                   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][tObj->BaseLevel];
    const GLfloat width = (GLfloat) img->Width;
@@ -1280,20 +1392,17 @@ opt_sample_rgba_2d( GLcontext *ctx,
       const GLint row = IFLOOR(texcoords[i][1] * height) & rowMask;
       const GLint pos = (row << shift) | col;
       const GLchan *texel = ((GLchan *) img->Data) + (pos << 2);    /* pos*4 */
-      COPY_CHAN4(rgba[i], texel);
+      COPY_4V(rgba[i], texel);
    }
 }
 
 
-/*
- * Given an array of texture coordinate and lambda (level of detail)
- * values, return an array of texture sample.
- */
+/** Sample 2D texture, using lambda to choose between min/magnification */
 static void
-sample_lambda_2d( GLcontext *ctx,
-                  const struct gl_texture_object *tObj,
-                  GLuint n, const GLfloat texcoords[][4],
-                  const GLfloat lambda[], GLchan rgba[][4] )
+sample_lambda_2d(GLcontext *ctx,
+                 const struct gl_texture_object *tObj,
+                 GLuint n, const GLfloat texcoords[][4],
+                 const GLfloat lambda[], GLfloat rgba[][4])
 {
    const struct gl_texture_image *tImg = tObj->Image[0][tObj->BaseLevel];
    GLuint minStart, minEnd;  /* texels with minification */
@@ -1316,6 +1425,7 @@ sample_lambda_2d( GLcontext *ctx,
       case GL_NEAREST:
          if (repeatNoBorderPOT) {
             switch (tImg->TexFormat->MesaFormat) {
+#if 0
             case MESA_FORMAT_RGB:
                opt_sample_rgb_2d(ctx, tObj, m, texcoords + minStart,
                                  NULL, rgba + minStart);
@@ -1324,6 +1434,7 @@ sample_lambda_2d( GLcontext *ctx,
               opt_sample_rgba_2d(ctx, tObj, m, texcoords + minStart,
                                   NULL, rgba + minStart);
                break;
+#endif
             default:
                sample_nearest_2d(ctx, tObj, m, texcoords + minStart,
                                  NULL, rgba + minStart );
@@ -1373,6 +1484,7 @@ sample_lambda_2d( GLcontext *ctx,
       case GL_NEAREST:
          if (repeatNoBorderPOT) {
             switch (tImg->TexFormat->MesaFormat) {
+#if 0
             case MESA_FORMAT_RGB:
                opt_sample_rgb_2d(ctx, tObj, m, texcoords + magStart,
                                  NULL, rgba + magStart);
@@ -1381,6 +1493,7 @@ sample_lambda_2d( GLcontext *ctx,
               opt_sample_rgba_2d(ctx, tObj, m, texcoords + magStart,
                                   NULL, rgba + magStart);
                break;
+#endif
             default:
                sample_nearest_2d(ctx, tObj, m, texcoords + magStart,
                                  NULL, rgba + magStart );
@@ -1407,15 +1520,15 @@ sample_lambda_2d( GLcontext *ctx,
 /*                    3-D Texture Sampling Functions                  */
 /**********************************************************************/
 
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
  */
-static void
+static INLINE void
 sample_3d_nearest(GLcontext *ctx,
                   const struct gl_texture_object *tObj,
                   const struct gl_texture_image *img,
                   const GLfloat texcoord[4],
-                  GLchan rgba[4])
+                  GLfloat rgba[4])
 {
    const GLint width = img->Width2;     /* without border, power of two */
    const GLint height = img->Height2;   /* without border, power of two */
@@ -1423,24 +1536,23 @@ sample_3d_nearest(GLcontext *ctx,
    GLint i, j, k;
    (void) ctx;
 
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width,  i);
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoord[1], height, j);
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapR, texcoord[2], depth,  k);
+   i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
+   j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
+   k = nearest_texel_location(tObj->WrapR, img, depth, texcoord[2]);
 
    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 mode */
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      get_border_color(tObj, img, rgba);
    }
    else {
-      img->FetchTexelc(img, i, j, k, rgba);
+      img->FetchTexelf(img, i, j, k, rgba);
    }
 }
 
 
-
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
  */
 static void
@@ -1448,21 +1560,20 @@ sample_3d_linear(GLcontext *ctx,
                  const struct gl_texture_object *tObj,
                  const struct gl_texture_image *img,
                  const GLfloat texcoord[4],
-                 GLchan rgba[4])
+                 GLfloat rgba[4])
 {
    const GLint width = img->Width2;
    const GLint height = img->Height2;
    const GLint depth = img->Depth2;
    GLint i0, j0, k0, i1, j1, k1;
    GLbitfield useBorderColor = 0x0;
-   GLfloat u, v, w;
    GLfloat a, b, c;
-   GLchan t000[4], t010[4], t001[4], t011[4];
-   GLchan t100[4], t110[4], t101[4], t111[4];
+   GLfloat t000[4], t010[4], t001[4], t011[4];
+   GLfloat t100[4], t110[4], t101[4], t111[4];
 
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width,  i0, i1);
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoord[1], v, height, j0, j1);
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapR, texcoord[2], w, depth,  k0, k1);
+   linear_texel_locations(tObj->WrapS, img, width, texcoord[0],  &i0, &i1, &a);
+   linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
+   linear_texel_locations(tObj->WrapR, img, depth, texcoord[2],  &k0, &k1, &c);
 
    if (img->Border) {
       i0 += img->Border;
@@ -1484,69 +1595,65 @@ sample_3d_linear(GLcontext *ctx,
 
    /* Fetch texels */
    if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
-      COPY_CHAN4(t000, tObj->_BorderChan);
+      get_border_color(tObj, img, t000);
    }
    else {
-      img->FetchTexelc(img, i0, j0, k0, t000);
+      img->FetchTexelf(img, i0, j0, k0, t000);
    }
    if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
-      COPY_CHAN4(t100, tObj->_BorderChan);
+      get_border_color(tObj, img, t100);
    }
    else {
-      img->FetchTexelc(img, i1, j0, k0, t100);
+      img->FetchTexelf(img, i1, j0, k0, t100);
    }
    if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
-      COPY_CHAN4(t010, tObj->_BorderChan);
+      get_border_color(tObj, img, t010);
    }
    else {
-      img->FetchTexelc(img, i0, j1, k0, t010);
+      img->FetchTexelf(img, i0, j1, k0, t010);
    }
    if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
-      COPY_CHAN4(t110, tObj->_BorderChan);
+      get_border_color(tObj, img, t110);
    }
    else {
-      img->FetchTexelc(img, i1, j1, k0, t110);
+      img->FetchTexelf(img, i1, j1, k0, t110);
    }
 
    if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
-      COPY_CHAN4(t001, tObj->_BorderChan);
+      get_border_color(tObj, img, t001);
    }
    else {
-      img->FetchTexelc(img, i0, j0, k1, t001);
+      img->FetchTexelf(img, i0, j0, k1, t001);
    }
    if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
-      COPY_CHAN4(t101, tObj->_BorderChan);
+      get_border_color(tObj, img, t101);
    }
    else {
-      img->FetchTexelc(img, i1, j0, k1, t101);
+      img->FetchTexelf(img, i1, j0, k1, t101);
    }
    if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
-      COPY_CHAN4(t011, tObj->_BorderChan);
+      get_border_color(tObj, img, t011);
    }
    else {
-      img->FetchTexelc(img, i0, j1, k1, t011);
+      img->FetchTexelf(img, i0, j1, k1, t011);
    }
    if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
-      COPY_CHAN4(t111, tObj->_BorderChan);
+      get_border_color(tObj, img, t111);
    }
    else {
-      img->FetchTexelc(img, i1, j1, k1, t111);
+      img->FetchTexelf(img, i1, j1, k1, t111);
    }
 
    /* trilinear interpolation of samples */
-   a = FRAC(u);
-   b = FRAC(v);
-   c = FRAC(w);
    lerp_rgba_3d(rgba, a, b, c, t000, t100, t010, t110, t001, t101, t011, t111);
 }
 
 
-
 static void
 sample_3d_nearest_mipmap_nearest(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
                                  GLuint n, const GLfloat texcoord[][4],
-                                 const GLfloat lambda[], GLchan rgba[][4] )
+                                 const GLfloat lambda[], GLfloat rgba[][4] )
 {
    GLuint i;
    for (i = 0; i < n; i++) {
@@ -1560,7 +1667,7 @@ static void
 sample_3d_linear_mipmap_nearest(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1575,7 +1682,7 @@ static void
 sample_3d_nearest_mipmap_linear(GLcontext *ctx,
                                 const struct gl_texture_object *tObj,
                                 GLuint n, const GLfloat texcoord[][4],
-                                const GLfloat lambda[], GLchan rgba[][4])
+                                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1586,7 +1693,7 @@ sample_3d_nearest_mipmap_linear(GLcontext *ctx,
                            texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -1600,7 +1707,7 @@ static void
 sample_3d_linear_mipmap_linear(GLcontext *ctx,
                                const struct gl_texture_object *tObj,
                                GLuint n, const GLfloat texcoord[][4],
-                               const GLfloat lambda[], GLchan rgba[][4])
+                               const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1611,7 +1718,7 @@ sample_3d_linear_mipmap_linear(GLcontext *ctx,
                           texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          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);
@@ -1621,46 +1728,44 @@ sample_3d_linear_mipmap_linear(GLcontext *ctx,
 }
 
 
+/** Sample 3D texture, nearest filtering for both min/magnification */
 static void
 sample_nearest_3d(GLcontext *ctx,
                   const struct gl_texture_object *tObj, GLuint n,
                   const GLfloat texcoords[][4], const GLfloat lambda[],
-                  GLchan rgba[][4])
+                  GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_3d_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-
+/** Sample 3D texture, linear filtering for both min/magnification */
 static void
-sample_linear_3d( GLcontext *ctx,
-                  const struct gl_texture_object *tObj, GLuint n,
-                  const GLfloat texcoords[][4],
-                 const GLfloat lambda[], GLchan rgba[][4] )
+sample_linear_3d(GLcontext *ctx,
+                 const struct gl_texture_object *tObj, GLuint n,
+                 const GLfloat texcoords[][4],
+                const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_3d_linear(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-/*
- * Given an (s,t,r) texture coordinate and lambda (level of detail) value,
- * return a texture sample.
- */
+/** Sample 3D texture, using lambda to choose between min/magnification */
 static void
-sample_lambda_3d( GLcontext *ctx,
-                  const struct gl_texture_object *tObj, GLuint n,
-                  const GLfloat texcoords[][4], const GLfloat lambda[],
-                  GLchan rgba[][4] )
+sample_lambda_3d(GLcontext *ctx,
+                 const struct gl_texture_object *tObj, GLuint n,
+                 const GLfloat texcoords[][4], const GLfloat lambda[],
+                 GLfloat rgba[][4])
 {
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
@@ -1811,7 +1916,7 @@ static void
 sample_nearest_cube(GLcontext *ctx,
                    const struct gl_texture_object *tObj, GLuint n,
                     const GLfloat texcoords[][4], const GLfloat lambda[],
-                    GLchan rgba[][4])
+                    GLfloat rgba[][4])
 {
    GLuint i;
    (void) lambda;
@@ -1829,7 +1934,7 @@ static void
 sample_linear_cube(GLcontext *ctx,
                   const struct gl_texture_object *tObj, GLuint n,
                    const GLfloat texcoords[][4],
-                  const GLfloat lambda[], GLchan rgba[][4])
+                  const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    (void) lambda;
@@ -1847,7 +1952,7 @@ static void
 sample_cube_nearest_mipmap_nearest(GLcontext *ctx,
                                    const struct gl_texture_object *tObj,
                                    GLuint n, const GLfloat texcoord[][4],
-                                   const GLfloat lambda[], GLchan rgba[][4])
+                                   const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1876,7 +1981,7 @@ static void
 sample_cube_linear_mipmap_nearest(GLcontext *ctx,
                                   const struct gl_texture_object *tObj,
                                   GLuint n, const GLfloat texcoord[][4],
-                                  const GLfloat lambda[], GLchan rgba[][4])
+                                  const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1895,7 +2000,7 @@ static void
 sample_cube_nearest_mipmap_linear(GLcontext *ctx,
                                   const struct gl_texture_object *tObj,
                                   GLuint n, const GLfloat texcoord[][4],
-                                  const GLfloat lambda[], GLchan rgba[][4])
+                                  const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1910,7 +2015,7 @@ sample_cube_nearest_mipmap_linear(GLcontext *ctx,
                            newCoord, rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          sample_2d_nearest(ctx, tObj, images[level  ], newCoord, t0);
          sample_2d_nearest(ctx, tObj, images[level+1], newCoord, t1);
@@ -1924,7 +2029,7 @@ static void
 sample_cube_linear_mipmap_linear(GLcontext *ctx,
                                  const struct gl_texture_object *tObj,
                                  GLuint n, const GLfloat texcoord[][4],
-                                 const GLfloat lambda[], GLchan rgba[][4])
+                                 const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -1939,7 +2044,7 @@ sample_cube_linear_mipmap_linear(GLcontext *ctx,
                           newCoord, rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];
+         GLfloat t0[4], t1[4];
          const GLfloat f = FRAC(lambda[i]);
          sample_2d_linear(ctx, tObj, images[level  ], newCoord, t0);
          sample_2d_linear(ctx, tObj, images[level+1], newCoord, t1);
@@ -1949,11 +2054,12 @@ sample_cube_linear_mipmap_linear(GLcontext *ctx,
 }
 
 
+/** Sample cube texture, using lambda to choose between min/magnification */
 static void
-sample_lambda_cube( GLcontext *ctx,
-                   const struct gl_texture_object *tObj, GLuint n,
-                   const GLfloat texcoords[][4], const GLfloat lambda[],
-                   GLchan rgba[][4])
+sample_lambda_cube(GLcontext *ctx,
+                  const struct gl_texture_object *tObj, GLuint n,
+                  const GLfloat texcoords[][4], const GLfloat lambda[],
+                  GLfloat rgba[][4])
 {
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
@@ -2023,70 +2129,15 @@ sample_lambda_cube( GLcontext *ctx,
 /**********************************************************************/
 
 
-/**
- * Do clamp/wrap for a texture rectangle coord, GL_NEAREST filter mode.
- */
-static INLINE GLint
-clamp_rect_coord_nearest(GLenum wrapMode, GLfloat coord, GLint max)
-{
-   if (wrapMode == GL_CLAMP) {
-      return IFLOOR( CLAMP(coord, 0.0F, max - 1) );
-   }
-   else if (wrapMode == GL_CLAMP_TO_EDGE) {
-      return IFLOOR( CLAMP(coord, 0.5F, max - 0.5F) );
-   }
-   else {
-      return IFLOOR( CLAMP(coord, -0.5F, max + 0.5F) );
-   }
-}
-
-
-/*
- * As above, but GL_LINEAR filtering.
- */
-static INLINE void
-clamp_rect_coord_linear(GLenum wrapMode, GLfloat coord, GLint max,
-                        GLint *i0out, GLint *i1out)
-{
-   GLfloat fcol;
-   GLint i0, i1;
-   if (wrapMode == GL_CLAMP) {
-      /* Not exactly what the spec says, but it matches NVIDIA output */
-      fcol = CLAMP(coord - 0.5F, 0.0, max-1);
-      i0 = IFLOOR(fcol);
-      i1 = i0 + 1;
-   }
-   else if (wrapMode == GL_CLAMP_TO_EDGE) {
-      fcol = CLAMP(coord, 0.5F, max - 0.5F);
-      fcol -= 0.5F;
-      i0 = IFLOOR(fcol);
-      i1 = i0 + 1;
-      if (i1 > max - 1)
-         i1 = max - 1;
-   }
-   else {
-      ASSERT(wrapMode == GL_CLAMP_TO_BORDER);
-      fcol = CLAMP(coord, -0.5F, max + 0.5F);
-      fcol -= 0.5F;
-      i0 = IFLOOR(fcol);
-      i1 = i0 + 1;
-   }
-   *i0out = i0;
-   *i1out = i1;
-}
-
-
 static void
 sample_nearest_rect(GLcontext *ctx,
                    const struct gl_texture_object *tObj, GLuint n,
                     const GLfloat texcoords[][4], const GLfloat lambda[],
-                    GLchan rgba[][4])
+                    GLfloat 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;
+   const GLint width = img->Width;
+   const GLint height = img->Height;
    GLuint i;
 
    (void) ctx;
@@ -2104,10 +2155,10 @@ sample_nearest_rect(GLcontext *ctx,
       GLint row, col;
       col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
       row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
-      if (col < 0 || col > width_minus_1 || row < 0 || row > height_minus_1)
-         COPY_CHAN4(rgba[i], tObj->_BorderChan);
+      if (col < 0 || col >= width || row < 0 || row >= height)
+         get_border_color(tObj, img, rgba[i]);
       else
-         img->FetchTexelc(img, col, row, 0, rgba[i]);
+         img->FetchTexelf(img, col, row, 0, rgba[i]);
    }
 }
 
@@ -2116,13 +2167,11 @@ static void
 sample_linear_rect(GLcontext *ctx,
                   const struct gl_texture_object *tObj, GLuint n,
                    const GLfloat texcoords[][4],
-                  const GLfloat lambda[], GLchan rgba[][4])
+                  const GLfloat lambda[], GLfloat 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;
+   const GLint width = img->Width;
+   const GLint height = img->Height;
    GLuint i;
 
    (void) ctx;
@@ -2136,100 +2185,55 @@ sample_linear_rect(GLcontext *ctx,
           tObj->WrapT == GL_CLAMP_TO_BORDER);
    ASSERT(img->TexFormat->BaseFormat != GL_COLOR_INDEX);
 
-   /* XXX lots of opportunity for optimization in this loop */
    for (i = 0; i < n; i++) {
-      GLfloat frow, fcol;
       GLint i0, j0, i1, j1;
-      GLchan t00[4], t01[4], t10[4], t11[4];
+      GLfloat t00[4], t01[4], t10[4], t11[4];
       GLfloat a, b;
       GLbitfield useBorderColor = 0x0;
 
-      /* NOTE: we DO NOT use [0, 1] texture coordinates! */
-      if (tObj->WrapS == GL_CLAMP) {
-         /* Not exactly what the spec says, but it matches NVIDIA output */
-         fcol = CLAMP(texcoords[i][0] - 0.5F, 0.0, width_minus_1);
-         i0 = IFLOOR(fcol);
-         i1 = i0 + 1;
-      }
-      else if (tObj->WrapS == GL_CLAMP_TO_EDGE) {
-         fcol = CLAMP(texcoords[i][0], 0.5F, width - 0.5F);
-         fcol -= 0.5F;
-         i0 = IFLOOR(fcol);
-         i1 = i0 + 1;
-         if (i1 > width_minus_1)
-            i1 = width_minus_1;
-      }
-      else {
-         ASSERT(tObj->WrapS == GL_CLAMP_TO_BORDER);
-         fcol = CLAMP(texcoords[i][0], -0.5F, width + 0.5F);
-         fcol -= 0.5F;
-         i0 = IFLOOR(fcol);
-         i1 = i0 + 1;
-      }
-
-      if (tObj->WrapT == GL_CLAMP) {
-         /* Not exactly what the spec says, but it matches NVIDIA output */
-         frow = CLAMP(texcoords[i][1] - 0.5F, 0.0, width_minus_1);
-         j0 = IFLOOR(frow);
-         j1 = j0 + 1;
-      }
-      else if (tObj->WrapT == GL_CLAMP_TO_EDGE) {
-         frow = CLAMP(texcoords[i][1], 0.5F, height - 0.5F);
-         frow -= 0.5F;
-         j0 = IFLOOR(frow);
-         j1 = j0 + 1;
-         if (j1 > height_minus_1)
-            j1 = height_minus_1;
-      }
-      else {
-         ASSERT(tObj->WrapT == GL_CLAMP_TO_BORDER);
-         frow = CLAMP(texcoords[i][1], -0.5F, height + 0.5F);
-         frow -= 0.5F;
-         j0 = IFLOOR(frow);
-         j1 = j0 + 1;
-      }
+      clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0], width,
+                              &i0, &i1, &a);
+      clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1], height,
+                              &j0, &j1, &b);
 
       /* compute integer rows/columns */
-      if (i0 < 0 || i0 > width_minus_1)   useBorderColor |= I0BIT;
-      if (i1 < 0 || i1 > width_minus_1)   useBorderColor |= I1BIT;
-      if (j0 < 0 || j0 > height_minus_1)  useBorderColor |= J0BIT;
-      if (j1 < 0 || j1 > height_minus_1)  useBorderColor |= J1BIT;
+      if (i0 < 0 || i0 >= width)   useBorderColor |= I0BIT;
+      if (i1 < 0 || i1 >= width)   useBorderColor |= I1BIT;
+      if (j0 < 0 || j0 >= height)  useBorderColor |= J0BIT;
+      if (j1 < 0 || j1 >= height)  useBorderColor |= J1BIT;
 
       /* get four texel samples */
       if (useBorderColor & (I0BIT | J0BIT))
-         COPY_CHAN4(t00, tObj->_BorderChan);
+         get_border_color(tObj, img, t00);
       else
-         img->FetchTexelc(img, i0, j0, 0, t00);
+         img->FetchTexelf(img, i0, j0, 0, t00);
 
       if (useBorderColor & (I1BIT | J0BIT))
-         COPY_CHAN4(t10, tObj->_BorderChan);
+         get_border_color(tObj, img, t10);
       else
-         img->FetchTexelc(img, i1, j0, 0, t10);
+         img->FetchTexelf(img, i1, j0, 0, t10);
 
       if (useBorderColor & (I0BIT | J1BIT))
-         COPY_CHAN4(t01, tObj->_BorderChan);
+         get_border_color(tObj, img, t01);
       else
-         img->FetchTexelc(img, i0, j1, 0, t01);
+         img->FetchTexelf(img, i0, j1, 0, t01);
 
       if (useBorderColor & (I1BIT | J1BIT))
-         COPY_CHAN4(t11, tObj->_BorderChan);
+         get_border_color(tObj, img, t11);
       else
-         img->FetchTexelc(img, i1, j1, 0, t11);
-
-      /* compute interpolants */
-      a = FRAC(fcol);
-      b = FRAC(frow);
+         img->FetchTexelf(img, i1, j1, 0, t11);
 
       lerp_rgba_2d(rgba[i], a, b, t00, t10, t01, t11);
    }
 }
 
 
+/** Sample Rect texture, using lambda to choose between min/magnification */
 static void
-sample_lambda_rect( GLcontext *ctx,
-                   const struct gl_texture_object *tObj, GLuint n,
-                   const GLfloat texcoords[][4], const GLfloat lambda[],
-                   GLchan rgba[][4])
+sample_lambda_rect(GLcontext *ctx,
+                  const struct gl_texture_object *tObj, GLuint n,
+                  const GLfloat texcoords[][4], const GLfloat lambda[],
+                  GLfloat rgba[][4])
 {
    GLuint minStart, minEnd, magStart, magEnd;
 
@@ -2241,22 +2245,22 @@ sample_lambda_rect( GLcontext *ctx,
 
    if (minStart < minEnd) {
       if (tObj->MinFilter == GL_NEAREST) {
-         sample_nearest_rect( ctx, tObj, minEnd - minStart,
-                              texcoords + minStart, NULL, rgba + minStart);
+         sample_nearest_rect(ctx, tObj, minEnd - minStart,
+                             texcoords + minStart, NULL, rgba + minStart);
       }
       else {
-         sample_linear_rect( ctx, tObj, minEnd - minStart,
-                             texcoords + minStart, NULL, rgba + minStart);
+         sample_linear_rect(ctx, tObj, minEnd - minStart,
+                            texcoords + minStart, NULL, rgba + minStart);
       }
    }
    if (magStart < magEnd) {
       if (tObj->MagFilter == GL_NEAREST) {
-         sample_nearest_rect( ctx, tObj, magEnd - magStart,
-                              texcoords + magStart, NULL, rgba + magStart);
+         sample_nearest_rect(ctx, tObj, magEnd - magStart,
+                             texcoords + magStart, NULL, rgba + magStart);
       }
       else {
-         sample_linear_rect( ctx, tObj, magEnd - magStart,
-                             texcoords + magStart, NULL, rgba + magStart);
+         sample_linear_rect(ctx, tObj, magEnd - magStart,
+                            texcoords + magStart, NULL, rgba + magStart);
       }
    }
 }
@@ -2267,7 +2271,7 @@ sample_lambda_rect( GLcontext *ctx,
 /*                2D Texture Array Sampling Functions                 */
 /**********************************************************************/
 
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
  */
 static void
@@ -2275,7 +2279,7 @@ sample_2d_array_nearest(GLcontext *ctx,
                         const struct gl_texture_object *tObj,
                         const struct gl_texture_image *img,
                         const GLfloat texcoord[4],
-                        GLchan rgba[4])
+                        GLfloat rgba[4])
 {
    const GLint width = img->Width2;     /* without border, power of two */
    const GLint height = img->Height2;   /* without border, power of two */
@@ -2284,24 +2288,23 @@ sample_2d_array_nearest(GLcontext *ctx,
    GLint array;
    (void) ctx;
 
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width,  i);
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoord[1], height, j);
+   i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
+   j = nearest_texel_location(tObj->WrapT, img, height, texcoord[1]);
    array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
 
    if (i < 0 || i >= (GLint) img->Width ||
        j < 0 || j >= (GLint) img->Height ||
        array < 0 || array >= (GLint) img->Depth) {
       /* Need this test for GL_CLAMP_TO_BORDER mode */
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      get_border_color(tObj, img, rgba);
    }
    else {
-      img->FetchTexelc(img, i, j, array, rgba);
+      img->FetchTexelf(img, i, j, array, rgba);
    }
 }
 
 
-
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
  */
 static void
@@ -2309,7 +2312,7 @@ sample_2d_array_linear(GLcontext *ctx,
                        const struct gl_texture_object *tObj,
                        const struct gl_texture_image *img,
                        const GLfloat texcoord[4],
-                       GLchan rgba[4])
+                       GLfloat rgba[4])
 {
    const GLint width = img->Width2;
    const GLint height = img->Height2;
@@ -2317,16 +2320,15 @@ sample_2d_array_linear(GLcontext *ctx,
    GLint i0, j0, i1, j1;
    GLint array;
    GLbitfield useBorderColor = 0x0;
-   GLfloat u, v;
    GLfloat a, b;
-   GLchan t00[4], t01[4], t10[4], t11[4];
+   GLfloat t00[4], t01[4], t10[4], t11[4];
 
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width,  i0, i1);
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapT, texcoord[1], v, height, j0, j1);
+   linear_texel_locations(tObj->WrapS, img, width,  texcoord[0], &i0, &i1, &a);
+   linear_texel_locations(tObj->WrapT, img, height, texcoord[1], &j0, &j1, &b);
    array = clamp_rect_coord_nearest(tObj->WrapR, texcoord[2], depth);
 
    if (array < 0 || array >= depth) {
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      COPY_4V(rgba, tObj->BorderColor);
    }
    else {
       if (img->Border) {
@@ -2345,44 +2347,41 @@ sample_2d_array_linear(GLcontext *ctx,
 
       /* Fetch texels */
       if (useBorderColor & (I0BIT | J0BIT)) {
-        COPY_CHAN4(t00, tObj->_BorderChan);
+         get_border_color(tObj, img, t00);
       }
       else {
-        img->FetchTexelc(img, i0, j0, array, t00);
+        img->FetchTexelf(img, i0, j0, array, t00);
       }
       if (useBorderColor & (I1BIT | J0BIT)) {
-        COPY_CHAN4(t10, tObj->_BorderChan);
+         get_border_color(tObj, img, t10);
       }
       else {
-        img->FetchTexelc(img, i1, j0, array, t10);
+        img->FetchTexelf(img, i1, j0, array, t10);
       }
       if (useBorderColor & (I0BIT | J1BIT)) {
-        COPY_CHAN4(t01, tObj->_BorderChan);
+         get_border_color(tObj, img, t01);
       }
       else {
-        img->FetchTexelc(img, i0, j1, array, t01);
+        img->FetchTexelf(img, i0, j1, array, t01);
       }
       if (useBorderColor & (I1BIT | J1BIT)) {
-        COPY_CHAN4(t11, tObj->_BorderChan);
+         get_border_color(tObj, img, t11);
       }
       else {
-        img->FetchTexelc(img, i1, j1, array, t11);
+        img->FetchTexelf(img, i1, j1, array, t11);
       }
       
       /* trilinear interpolation of samples */
-      a = FRAC(u);
-      b = FRAC(v);
       lerp_rgba_2d(rgba, a, b, t00, t10, t01, t11);
    }
 }
 
 
-
 static void
 sample_2d_array_nearest_mipmap_nearest(GLcontext *ctx,
                                        const struct gl_texture_object *tObj,
                                        GLuint n, const GLfloat texcoord[][4],
-                                       const GLfloat lambda[], GLchan rgba[][4] )
+                                       const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    for (i = 0; i < n; i++) {
@@ -2397,7 +2396,7 @@ static void
 sample_2d_array_linear_mipmap_nearest(GLcontext *ctx,
                                       const struct gl_texture_object *tObj,
                                       GLuint n, const GLfloat texcoord[][4],
-                                      const GLfloat lambda[], GLchan rgba[][4])
+                                      const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2413,7 +2412,7 @@ static void
 sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
                                       const struct gl_texture_object *tObj,
                                       GLuint n, const GLfloat texcoord[][4],
-                                      const GLfloat lambda[], GLchan rgba[][4])
+                                      const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2424,10 +2423,12 @@ sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
                                  texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
-         sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
+         sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level  ],
+                                 texcoord[i], t0);
+         sample_2d_array_nearest(ctx, tObj, tObj->Image[0][level+1],
+                                 texcoord[i], t1);
          lerp_rgba(rgba[i], f, t0, t1);
       }
    }
@@ -2436,9 +2437,9 @@ sample_2d_array_nearest_mipmap_linear(GLcontext *ctx,
 
 static void
 sample_2d_array_linear_mipmap_linear(GLcontext *ctx,
-                               const struct gl_texture_object *tObj,
-                               GLuint n, const GLfloat texcoord[][4],
-                               const GLfloat lambda[], GLchan rgba[][4])
+                                     const struct gl_texture_object *tObj,
+                                     GLuint n, const GLfloat texcoord[][4],
+                                     const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2449,56 +2450,57 @@ sample_2d_array_linear_mipmap_linear(GLcontext *ctx,
                           texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
-         sample_2d_array_linear(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
-         sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
+         sample_2d_array_linear(ctx, tObj, tObj->Image[0][level  ],
+                                texcoord[i], t0);
+         sample_2d_array_linear(ctx, tObj, tObj->Image[0][level+1],
+                                texcoord[i], t1);
          lerp_rgba(rgba[i], f, t0, t1);
       }
    }
 }
 
 
+/** Sample 2D Array texture, nearest filtering for both min/magnification */
 static void
 sample_nearest_2d_array(GLcontext *ctx,
-                  const struct gl_texture_object *tObj, GLuint n,
-                  const GLfloat texcoords[][4], const GLfloat lambda[],
-                  GLchan rgba[][4])
+                        const struct gl_texture_object *tObj, GLuint n,
+                        const GLfloat texcoords[][4], const GLfloat lambda[],
+                        GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_2d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
 
+/** Sample 2D Array texture, linear filtering for both min/magnification */
 static void
 sample_linear_2d_array(GLcontext *ctx,
                        const struct gl_texture_object *tObj, GLuint n,
                        const GLfloat texcoords[][4],
-                       const GLfloat lambda[], GLchan rgba[][4])
+                       const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_2d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-/*
- * Given an (s,t,r) texture coordinate and lambda (level of detail) value,
- * return a texture sample.
- */
+/** Sample 2D Array texture, using lambda to choose between min/magnification */
 static void
 sample_lambda_2d_array(GLcontext *ctx,
                        const struct gl_texture_object *tObj, GLuint n,
                        const GLfloat texcoords[][4], const GLfloat lambda[],
-                       GLchan rgba[][4])
+                       GLfloat rgba[][4])
 {
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
@@ -2523,8 +2525,10 @@ sample_lambda_2d_array(GLcontext *ctx,
                                    texcoords[i], rgba[i]);
          break;
       case GL_NEAREST_MIPMAP_NEAREST:
-         sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m, texcoords + minStart,
-                                                lambda + minStart, rgba + minStart);
+         sample_2d_array_nearest_mipmap_nearest(ctx, tObj, m,
+                                                texcoords + minStart,
+                                                lambda + minStart,
+                                                rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_NEAREST:
          sample_2d_array_linear_mipmap_nearest(ctx, tObj, m, 
@@ -2533,8 +2537,10 @@ sample_lambda_2d_array(GLcontext *ctx,
                                                rgba + minStart);
          break;
       case GL_NEAREST_MIPMAP_LINEAR:
-         sample_2d_array_nearest_mipmap_linear(ctx, tObj, m, texcoords + minStart,
-                                               lambda + minStart, rgba + minStart);
+         sample_2d_array_nearest_mipmap_linear(ctx, tObj, m,
+                                               texcoords + minStart,
+                                               lambda + minStart,
+                                               rgba + minStart);
          break;
       case GL_LINEAR_MIPMAP_LINEAR:
          sample_2d_array_linear_mipmap_linear(ctx, tObj, m, 
@@ -2575,7 +2581,7 @@ sample_lambda_2d_array(GLcontext *ctx,
 /*                1D Texture Array Sampling Functions                 */
 /**********************************************************************/
 
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_NEAREST filter.
  */
 static void
@@ -2583,7 +2589,7 @@ sample_1d_array_nearest(GLcontext *ctx,
                         const struct gl_texture_object *tObj,
                         const struct gl_texture_image *img,
                         const GLfloat texcoord[4],
-                        GLchan rgba[4])
+                        GLfloat rgba[4])
 {
    const GLint width = img->Width2;     /* without border, power of two */
    const GLint height = img->Height;
@@ -2591,22 +2597,21 @@ sample_1d_array_nearest(GLcontext *ctx,
    GLint array;
    (void) ctx;
 
-   COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoord[0], width,  i);
+   i = nearest_texel_location(tObj->WrapS, img, width, texcoord[0]);
    array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
 
    if (i < 0 || i >= (GLint) img->Width ||
        array < 0 || array >= (GLint) img->Height) {
       /* Need this test for GL_CLAMP_TO_BORDER mode */
-      COPY_CHAN4(rgba, tObj->_BorderChan);
+      get_border_color(tObj, img, rgba);
    }
    else {
-      img->FetchTexelc(img, i, array, 0, rgba);
+      img->FetchTexelf(img, i, array, 0, rgba);
    }
 }
 
 
-
-/*
+/**
  * Return the texture sample for coordinate (s,t,r) using GL_LINEAR filter.
  */
 static void
@@ -2614,18 +2619,17 @@ sample_1d_array_linear(GLcontext *ctx,
                        const struct gl_texture_object *tObj,
                        const struct gl_texture_image *img,
                        const GLfloat texcoord[4],
-                       GLchan rgba[4])
+                       GLfloat rgba[4])
 {
    const GLint width = img->Width2;
    const GLint height = img->Height;
    GLint i0, i1;
    GLint array;
    GLbitfield useBorderColor = 0x0;
-   GLfloat u;
    GLfloat a;
-   GLchan t0[4], t1[4];
+   GLfloat t0[4], t1[4];
 
-   COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoord[0], u, width,  i0, i1);
+   linear_texel_locations(tObj->WrapS, img, width, texcoord[0], &i0, &i1, &a);
    array = clamp_rect_coord_nearest(tObj->WrapT, texcoord[1], height);
 
    if (img->Border) {
@@ -2642,30 +2646,28 @@ sample_1d_array_linear(GLcontext *ctx,
 
    /* Fetch texels */
    if (useBorderColor & (I0BIT | K0BIT)) {
-      COPY_CHAN4(t0, tObj->_BorderChan);
+      get_border_color(tObj, img, t0);
    }
    else {
-      img->FetchTexelc(img, i0, array, 0, t0);
+      img->FetchTexelf(img, i0, array, 0, t0);
    }
    if (useBorderColor & (I1BIT | K0BIT)) {
-      COPY_CHAN4(t1, tObj->_BorderChan);
+      get_border_color(tObj, img, t1);
    }
    else {
-      img->FetchTexelc(img, i1, array, 0, t1);
+      img->FetchTexelf(img, i1, array, 0, t1);
    }
 
    /* bilinear interpolation of samples */
-   a = FRAC(u);
    lerp_rgba(rgba, a, t0, t1);
 }
 
 
-
 static void
 sample_1d_array_nearest_mipmap_nearest(GLcontext *ctx,
                                        const struct gl_texture_object *tObj,
                                        GLuint n, const GLfloat texcoord[][4],
-                                       const GLfloat lambda[], GLchan rgba[][4] )
+                                       const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    for (i = 0; i < n; i++) {
@@ -2680,7 +2682,7 @@ static void
 sample_1d_array_linear_mipmap_nearest(GLcontext *ctx,
                                       const struct gl_texture_object *tObj,
                                       GLuint n, const GLfloat texcoord[][4],
-                                      const GLfloat lambda[], GLchan rgba[][4])
+                                      const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2696,7 +2698,7 @@ static void
 sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
                                       const struct gl_texture_object *tObj,
                                       GLuint n, const GLfloat texcoord[][4],
-                                      const GLfloat lambda[], GLchan rgba[][4])
+                                      const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2707,7 +2709,7 @@ sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
                                  texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
          sample_1d_array_nearest(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
@@ -2719,9 +2721,9 @@ sample_1d_array_nearest_mipmap_linear(GLcontext *ctx,
 
 static void
 sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
-                               const struct gl_texture_object *tObj,
-                               GLuint n, const GLfloat texcoord[][4],
-                               const GLfloat lambda[], GLchan rgba[][4])
+                                     const struct gl_texture_object *tObj,
+                                     GLuint n, const GLfloat texcoord[][4],
+                                     const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    ASSERT(lambda != NULL);
@@ -2732,7 +2734,7 @@ sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
                           texcoord[i], rgba[i]);
       }
       else {
-         GLchan t0[4], t1[4];  /* texels */
+         GLfloat t0[4], t1[4];  /* texels */
          const GLfloat f = FRAC(lambda[i]);
          sample_1d_array_linear(ctx, tObj, tObj->Image[0][level  ], texcoord[i], t0);
          sample_1d_array_linear(ctx, tObj, tObj->Image[0][level+1], texcoord[i], t1);
@@ -2742,46 +2744,44 @@ sample_1d_array_linear_mipmap_linear(GLcontext *ctx,
 }
 
 
+/** Sample 1D Array texture, nearest filtering for both min/magnification */
 static void
 sample_nearest_1d_array(GLcontext *ctx,
-                  const struct gl_texture_object *tObj, GLuint n,
-                  const GLfloat texcoords[][4], const GLfloat lambda[],
-                  GLchan rgba[][4])
+                        const struct gl_texture_object *tObj, GLuint n,
+                        const GLfloat texcoords[][4], const GLfloat lambda[],
+                        GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_1d_array_nearest(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-
+/** Sample 1D Array texture, linear filtering for both min/magnification */
 static void
 sample_linear_1d_array(GLcontext *ctx,
                        const struct gl_texture_object *tObj, GLuint n,
                        const GLfloat texcoords[][4],
-                       const GLfloat lambda[], GLchan rgba[][4])
+                       const GLfloat lambda[], GLfloat rgba[][4])
 {
    GLuint i;
    struct gl_texture_image *image = tObj->Image[0][tObj->BaseLevel];
    (void) lambda;
-   for (i=0;i<n;i++) {
+   for (i = 0; i < n; i++) {
       sample_1d_array_linear(ctx, tObj, image, texcoords[i], rgba[i]);
    }
 }
 
 
-/*
- * Given an (s,t,r) texture coordinate and lambda (level of detail) value,
- * return a texture sample.
- */
+/** Sample 1D Array texture, using lambda to choose between min/magnification */
 static void
 sample_lambda_1d_array(GLcontext *ctx,
                        const struct gl_texture_object *tObj, GLuint n,
                        const GLfloat texcoords[][4], const GLfloat lambda[],
-                       GLchan rgba[][4])
+                       GLfloat rgba[][4])
 {
    GLuint minStart, minEnd;  /* texels with minification */
    GLuint magStart, magEnd;  /* texels with magnification */
@@ -2852,16 +2852,110 @@ sample_lambda_1d_array(GLcontext *ctx,
 }
 
 
+/**
+ * Compare texcoord against depth sample.  Return 1.0 or the ambient value.
+ */
+static INLINE GLfloat
+shadow_compare(GLenum function, GLfloat coord, GLfloat depthSample,
+               GLfloat ambient)
+{
+   switch (function) {
+   case GL_LEQUAL:
+      return (coord <= depthSample) ? 1.0F : ambient;
+   case GL_GEQUAL:
+      return (coord >= depthSample) ? 1.0F : ambient;
+   case GL_LESS:
+      return (coord < depthSample) ? 1.0F : ambient;
+   case GL_GREATER:
+      return (coord > depthSample) ? 1.0F : ambient;
+   case GL_EQUAL:
+      return (coord == depthSample) ? 1.0F : ambient;
+   case GL_NOTEQUAL:
+      return (coord != depthSample) ? 1.0F : ambient;
+   case GL_ALWAYS:
+      return 1.0F;
+   case GL_NEVER:
+      return ambient;
+   case GL_NONE:
+      return depthSample;
+   default:
+      _mesa_problem(NULL, "Bad compare func in shadow_compare");
+      return ambient;
+   }
+}
+
 
+/**
+ * Compare texcoord against four depth samples.
+ */
+static INLINE GLfloat
+shadow_compare4(GLenum function, GLfloat coord,
+                GLfloat depth00, GLfloat depth01,
+                GLfloat depth10, GLfloat depth11,
+                GLfloat ambient, GLfloat wi, GLfloat wj)
+{
+   const GLfloat d = (1.0F - (GLfloat) ambient) * 0.25F;
+   GLfloat luminance = 1.0F;
+
+   switch (function) {
+   case GL_LEQUAL:
+      if (depth00 <= coord)  luminance -= d;
+      if (depth01 <= coord)  luminance -= d;
+      if (depth10 <= coord)  luminance -= d;
+      if (depth11 <= coord)  luminance -= d;
+      return luminance;
+   case GL_GEQUAL:
+      if (depth00 >= coord)  luminance -= d;
+      if (depth01 >= coord)  luminance -= d;
+      if (depth10 >= coord)  luminance -= d;
+      if (depth11 >= coord)  luminance -= d;
+      return luminance;
+   case GL_LESS:
+      if (depth00 < coord)  luminance -= d;
+      if (depth01 < coord)  luminance -= d;
+      if (depth10 < coord)  luminance -= d;
+      if (depth11 < coord)  luminance -= d;
+      return luminance;
+   case GL_GREATER:
+      if (depth00 > coord)  luminance -= d;
+      if (depth01 > coord)  luminance -= d;
+      if (depth10 > coord)  luminance -= d;
+      if (depth11 > coord)  luminance -= d;
+      return luminance;
+   case GL_EQUAL:
+      if (depth00 == coord)  luminance -= d;
+      if (depth01 == coord)  luminance -= d;
+      if (depth10 == coord)  luminance -= d;
+      if (depth11 == coord)  luminance -= d;
+      return luminance;
+   case GL_NOTEQUAL:
+      if (depth00 != coord)  luminance -= d;
+      if (depth01 != coord)  luminance -= d;
+      if (depth10 != coord)  luminance -= d;
+      if (depth11 != coord)  luminance -= d;
+      return luminance;
+   case GL_ALWAYS:
+      return 0.0;
+   case GL_NEVER:
+      return ambient;
+   case GL_NONE:
+      /* ordinary bilinear filtering */
+      return lerp_2d(wi, wj, depth00, depth10, depth01, depth11);
+   default:
+      _mesa_problem(NULL, "Bad compare func in sample_depth_texture");
+      return 0.0F;
+   }
+}
 
-/*
+
+/**
  * Sample a shadow/depth texture.
  */
 static void
 sample_depth_texture( GLcontext *ctx,
                       const struct gl_texture_object *tObj, GLuint n,
                       const GLfloat texcoords[][4], const GLfloat lambda[],
-                      GLchan texel[][4] )
+                      GLfloat texel[][4] )
 {
    const GLint baseLevel = tObj->BaseLevel;
    const struct gl_texture_image *img = tObj->Image[0][baseLevel];
@@ -2870,9 +2964,9 @@ sample_depth_texture( GLcontext *ctx,
    const GLint depth = img->Depth;
    const GLuint compare_coord = (tObj->Target == GL_TEXTURE_2D_ARRAY_EXT)
        ? 3 : 2;
-   GLchan ambient;
+   GLfloat ambient;
    GLenum function;
-   GLchan result;
+   GLfloat result;
 
    (void) lambda;
 
@@ -2885,53 +2979,20 @@ sample_depth_texture( GLcontext *ctx,
           tObj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
           tObj->Target == GL_TEXTURE_2D_ARRAY_EXT);
 
-   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
+   ambient = tObj->CompareFailValue;
 
    /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
 
-   function = tObj->_Function;
+   function = (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) ?
+      tObj->CompareFunc : GL_NONE;
+
    if (tObj->MagFilter == GL_NEAREST) {
       GLuint i;
       for (i = 0; i < n; i++) {
          GLfloat depthSample;
          GLint col, row, slice;
 
-         switch (tObj->Target) {
-         case GL_TEXTURE_RECTANGLE_ARB:
-            col = clamp_rect_coord_nearest(tObj->WrapS, texcoords[i][0], width);
-            row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
-            slice = 0;
-            break;
-            
-         case GL_TEXTURE_1D:
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0],
-                                           width, col);
-            row = 0;
-            slice = 0;
-            break;
-
-         case GL_TEXTURE_2D:
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0],
-                                           width, col);
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoords[i][1],
-                                           height, row);
-            slice = 0;
-            break;
-
-         case GL_TEXTURE_1D_ARRAY_EXT:
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0],
-                                           width, col);
-            row = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
-            slice = 0;
-
-         case GL_TEXTURE_2D_ARRAY_EXT:
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapS, texcoords[i][0],
-                                           width, col);
-            COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, texcoords[i][1],
-                                           height, row);
-            slice = clamp_rect_coord_nearest(tObj->WrapR, texcoords[i][2], depth);
-            break;
-         }
+         nearest_texcoord(tObj, texcoords[i], &col, &row, &slice);
 
          if (col >= 0 && row >= 0 && col < width && row < height && 
              slice >= 0 && slice < depth) {
@@ -2941,57 +3002,18 @@ sample_depth_texture( GLcontext *ctx,
             depthSample = tObj->BorderColor[0];
          }
 
-         switch (function) {
-         case GL_LEQUAL:
-            result = (texcoords[i][compare_coord] <= depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_GEQUAL:
-            result = (texcoords[i][compare_coord] >= depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_LESS:
-            result = (texcoords[i][compare_coord] < depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_GREATER:
-            result = (texcoords[i][compare_coord] > depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_EQUAL:
-            result = (texcoords[i][compare_coord] == depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_NOTEQUAL:
-            result = (texcoords[i][compare_coord] != depthSample) ? CHAN_MAX : ambient;
-            break;
-         case GL_ALWAYS:
-            result = CHAN_MAX;
-            break;
-         case GL_NEVER:
-            result = ambient;
-            break;
-         case GL_NONE:
-            CLAMPED_FLOAT_TO_CHAN(result, depthSample);
-            break;
-         default:
-            _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
-            return;
-         }
+         result = shadow_compare(function, texcoords[i][compare_coord],
+                                 depthSample, ambient);
 
          switch (tObj->DepthMode) {
          case GL_LUMINANCE:
-            texel[i][RCOMP] = result;
-            texel[i][GCOMP] = result;
-            texel[i][BCOMP] = result;
-            texel[i][ACOMP] = CHAN_MAX;
+            ASSIGN_4V(texel[i], result, result, result, 1.0F);
             break;
          case GL_INTENSITY:
-            texel[i][RCOMP] = result;
-            texel[i][GCOMP] = result;
-            texel[i][BCOMP] = result;
-            texel[i][ACOMP] = result;
+            ASSIGN_4V(texel[i], result, result, result, result);
             break;
          case GL_ALPHA:
-            texel[i][RCOMP] = 0;
-            texel[i][GCOMP] = 0;
-            texel[i][BCOMP] = 0;
-            texel[i][ACOMP] = result;
+            ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
             break;
          default:
             _mesa_problem(ctx, "Bad depth texture mode");
@@ -3005,42 +3027,11 @@ sample_depth_texture( GLcontext *ctx,
          GLfloat depth00, depth01, depth10, depth11;
          GLint i0, i1, j0, j1;
          GLint slice;
-         GLfloat u, v;
+         GLfloat wi, wj;
          GLuint useBorderTexel;
 
-         switch (tObj->Target) {
-         case GL_TEXTURE_RECTANGLE_ARB:
-            clamp_rect_coord_linear(tObj->WrapS, texcoords[i][0],
-                                    width, &i0, &i1);
-            clamp_rect_coord_linear(tObj->WrapT, texcoords[i][1],
-                                    height, &j0, &j1);
-            slice = 0;
-            break;
-
-         case GL_TEXTURE_1D:
-         case GL_TEXTURE_2D:
-            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);
-            slice = 0;
-            break;
-
-         case GL_TEXTURE_1D_ARRAY_EXT:
-            COMPUTE_LINEAR_TEXEL_LOCATIONS(tObj->WrapS, texcoords[i][0],
-                                           u, width, i0, i1);
-            j0 = clamp_rect_coord_nearest(tObj->WrapT, texcoords[i][1], height);
-            j1 = j0;
-            slice = 0;
-
-         case GL_TEXTURE_2D_ARRAY_EXT:
-            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);
-            slice = clamp_rect_coord_nearest(tObj->WrapR, texcoords[i][2], depth);
-            break;
-         }
+         linear_texcoord(tObj, texcoords[i], &i0, &i1, &j0, &j1, &slice,
+                         &wi, &wj);
 
          useBorderTexel = 0;
          if (img->Border) {
@@ -3099,217 +3090,29 @@ sample_depth_texture( GLcontext *ctx,
             }
          }
 
-         if (0) {
-            /* compute a single weighted depth sample and do one comparison */
-            const GLfloat a = FRAC(u + 1.0F);
-            const GLfloat b = FRAC(v + 1.0F);
-            const GLfloat depthSample
-               = lerp_2d(a, b, depth00, depth10, depth01, depth11);
-            if ((depthSample <= texcoords[i][compare_coord] && function == GL_LEQUAL) ||
-                (depthSample >= texcoords[i][compare_coord] && function == GL_GEQUAL)) {
-               result  = ambient;
-            }
-            else {
-               result = CHAN_MAX;
-            }
-         }
-         else {
-            /* Do four depth/R comparisons and compute a weighted result.
-             * If this touches on somebody's I.P., I'll remove this code
-             * upon request.
-             */
-            const GLfloat d = (CHAN_MAXF - (GLfloat) ambient) * 0.25F;
-            GLfloat luminance = CHAN_MAXF;
-
-            switch (function) {
-            case GL_LEQUAL:
-               if (depth00 <= texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 <= texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 <= texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 <= texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_GEQUAL:
-               if (depth00 >= texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 >= texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 >= texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 >= texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_LESS:
-               if (depth00 < texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 < texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 < texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 < texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_GREATER:
-               if (depth00 > texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 > texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 > texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 > texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_EQUAL:
-               if (depth00 == texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 == texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 == texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 == texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_NOTEQUAL:
-               if (depth00 != texcoords[i][compare_coord])  luminance -= d;
-               if (depth01 != texcoords[i][compare_coord])  luminance -= d;
-               if (depth10 != texcoords[i][compare_coord])  luminance -= d;
-               if (depth11 != texcoords[i][compare_coord])  luminance -= d;
-               result = (GLchan) luminance;
-               break;
-            case GL_ALWAYS:
-               result = 0;
-               break;
-            case GL_NEVER:
-               result = CHAN_MAX;
-               break;
-            case GL_NONE:
-               /* ordinary bilinear filtering */
-               {
-                  const GLfloat a = FRAC(u + 1.0F);
-                  const GLfloat b = FRAC(v + 1.0F);
-                  const GLfloat depthSample
-                     = lerp_2d(a, b, depth00, depth10, depth01, depth11);
-                  CLAMPED_FLOAT_TO_CHAN(result, depthSample);
-               }
-               break;
-            default:
-               _mesa_problem(ctx, "Bad compare func in sample_depth_texture");
-               return;
-            }
-         }
+         result = shadow_compare4(function, texcoords[i][compare_coord],
+                                  depth00, depth01, depth10, depth11,
+                                  ambient, wi, wj);
 
          switch (tObj->DepthMode) {
          case GL_LUMINANCE:
-            texel[i][RCOMP] = result;
-            texel[i][GCOMP] = result;
-            texel[i][BCOMP] = result;
-            texel[i][ACOMP] = CHAN_MAX;
+            ASSIGN_4V(texel[i], result, result, result, 1.0F);
             break;
          case GL_INTENSITY:
-            texel[i][RCOMP] = result;
-            texel[i][GCOMP] = result;
-            texel[i][BCOMP] = result;
-            texel[i][ACOMP] = result;
+            ASSIGN_4V(texel[i], result, result, result, result);
             break;
          case GL_ALPHA:
-            texel[i][RCOMP] = 0;
-            texel[i][GCOMP] = 0;
-            texel[i][BCOMP] = 0;
-            texel[i][ACOMP] = result;
+            ASSIGN_4V(texel[i], 0.0F, 0.0F, 0.0F, result);
             break;
          default:
             _mesa_problem(ctx, "Bad depth texture mode");
          }
+
       }  /* for */
    }  /* if filter */
 }
 
 
-#if 0
-/*
- * Experimental depth texture sampling function.
- */
-static void
-sample_depth_texture2(const GLcontext *ctx,
-                     const struct gl_texture_unit *texUnit,
-                     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[0][baseLevel];
-   const GLuint width = texImage->Width;
-   const GLuint height = texImage->Height;
-   GLchan ambient;
-   GLboolean lequal, gequal;
-
-   if (texObj->Target != GL_TEXTURE_2D) {
-      _mesa_problem(ctx, "only 2-D depth textures supported at this time");
-      return;
-   }
-
-   if (texObj->MinFilter != texObj->MagFilter) {
-      _mesa_problem(ctx, "mipmapped depth textures not supported at this time");
-      return;
-   }
-
-   /* XXX the GL_SGIX_shadow extension spec doesn't say what to do if
-    * GL_TEXTURE_COMPARE_SGIX == GL_TRUE but the current texture object
-    * isn't a depth texture.
-    */
-   if (texImage->TexFormat->BaseFormat != GL_DEPTH_COMPONENT) {
-      _mesa_problem(ctx,"GL_TEXTURE_COMPARE_SGIX enabled with non-depth texture");
-      return;
-   }
-
-   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
-
-   if (texObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
-      lequal = GL_TRUE;
-      gequal = GL_FALSE;
-   }
-   else {
-      lequal = GL_FALSE;
-      gequal = GL_TRUE;
-   }
-
-   {
-      GLuint i;
-      for (i = 0; i < n; i++) {
-         const GLint K = 3;
-         GLint col, row, ii, jj, imin, imax, jmin, jmax, samples, count;
-         GLfloat w;
-         GLchan lum;
-         COMPUTE_NEAREST_TEXEL_LOCATION(texObj->WrapS, texcoords[i][0],
-                                       width, col);
-         COMPUTE_NEAREST_TEXEL_LOCATION(texObj->WrapT, texcoords[i][1],
-                                       height, row);
-
-         imin = col - K;
-         imax = col + K;
-         jmin = row - K;
-         jmax = row + K;
-
-         if (imin < 0)  imin = 0;
-         if (imax >= width)  imax = width - 1;
-         if (jmin < 0)  jmin = 0;
-         if (jmax >= height) jmax = height - 1;
-
-         samples = (imax - imin + 1) * (jmax - jmin + 1);
-         count = 0;
-         for (jj = jmin; jj <= jmax; jj++) {
-            for (ii = imin; ii <= imax; ii++) {
-               GLfloat depthSample;
-               texImage->FetchTexelf(texImage, ii, jj, 0, &depthSample);
-               if ((depthSample <= r[i] && lequal) ||
-                   (depthSample >= r[i] && gequal)) {
-                  count++;
-               }
-            }
-         }
-
-         w = (GLfloat) count / (GLfloat) samples;
-         w = CHAN_MAXF - w * (CHAN_MAXF - (GLfloat) ambient);
-         lum = (GLint) w;
-
-         texel[i][RCOMP] = lum;
-         texel[i][GCOMP] = lum;
-         texel[i][BCOMP] = lum;
-         texel[i][ACOMP] = CHAN_MAX;
-      }
-   }
-}
-#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
@@ -3320,7 +3123,7 @@ static void
 null_sample_func( GLcontext *ctx,
                  const struct gl_texture_object *tObj, GLuint n,
                  const GLfloat texcoords[][4], const GLfloat lambda[],
-                 GLchan rgba[][4])
+                 GLfloat rgba[][4])
 {
    GLuint i;
    (void) ctx;
@@ -3377,6 +3180,7 @@ _swrast_choose_texture_sample_func( GLcontext *ctx,
          }
          else {
             /* check for a few optimized cases */
+#if 0
             const struct gl_texture_image *img = t->Image[0][t->BaseLevel];
             ASSERT(t->MinFilter == GL_NEAREST);
             if (t->WrapS == GL_REPEAT &&
@@ -3393,6 +3197,10 @@ _swrast_choose_texture_sample_func( GLcontext *ctx,
                      img->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
                return &opt_sample_rgba_2d;
             }
+#else
+            if (0)
+               ;
+#endif
             else {
                return &sample_nearest_2d;
             }