st/program: use nir_lower_clip_disable instead of nir_lower_clip_vs conditionally
[mesa.git] / src / mesa / swrast / s_texcombine.c
index f783f56add608d3712851288860ebc60b349a29b..1ee906effd981393895b5024a6e8cd0ed7d53d96 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.5
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
 #include "main/glheader.h"
 #include "main/context.h"
-#include "main/colormac.h"
-#include "main/image.h"
-#include "main/imports.h"
+
 #include "main/macros.h"
-#include "main/pixel.h"
-#include "shader/prog_instruction.h"
+#include "main/pixeltransfer.h"
+#include "main/samplerobj.h"
+#include "program/prog_instruction.h"
 
 #include "s_context.h"
 #include "s_texcombine.h"
@@ -47,11 +46,14 @@ typedef float (*float4_array)[4];
 /**
  * Return array of texels for given unit.
  */
-static INLINE float4_array
-get_texel_array(const GLfloat *texelBuffer, GLuint unit, GLuint numTexels)
+static inline float4_array
+get_texel_array(SWcontext *swrast, GLuint unit)
 {
-   return (float4_array)
-      (texelBuffer + unit * numTexels * 4 * sizeof(GLfloat));
+#ifdef _OPENMP
+   return (float4_array) (swrast->TexelBuffer + unit * SWRAST_MAX_WIDTH * 4 * omp_get_num_threads() + (SWRAST_MAX_WIDTH * 4 * omp_get_thread_num()));
+#else
+   return (float4_array) (swrast->TexelBuffer + unit * SWRAST_MAX_WIDTH * 4);
+#endif
 }
 
 
@@ -68,19 +70,22 @@ get_texel_array(const GLfloat *texelBuffer, GLuint unit, GLuint numTexels)
  *
  * \param ctx          rendering context
  * \param unit         the texture combiner unit
- * \param n            number of fragments to process (span width)
  * \param primary_rgba incoming fragment color array
  * \param texelBuffer  pointer to texel colors for all texture units
- * 
- * \param rgba         incoming/result fragment colors
+ *
+ * \param span         two fields are used in this function:
+ *                       span->end: number of fragments to process
+ *                       span->array->rgba: incoming/result fragment colors
  */
 static void
-texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
+texture_combine( struct gl_context *ctx, GLuint unit,
                  const float4_array primary_rgba,
                  const GLfloat *texelBuffer,
-                 GLchan (*rgbaChan)[4] )
+                 SWspan *span )
 {
-   const struct gl_texture_unit *textureUnit = &(ctx->Texture.Unit[unit]);
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const struct gl_fixedfunc_texture_unit *textureUnit =
+      &ctx->Texture.FixedFuncUnit[unit];
    const struct gl_tex_env_combine_state *combine = textureUnit->_CurrentCombine;
    float4_array argRGB[MAX_COMBINER_TERMS];
    float4_array argA[MAX_COMBINER_TERMS];
@@ -88,11 +93,30 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
    const GLfloat scaleA = (GLfloat) (1 << combine->ScaleShiftA);
    const GLuint numArgsRGB = combine->_NumArgsRGB;
    const GLuint numArgsA = combine->_NumArgsA;
-   GLfloat ccolor[MAX_COMBINER_TERMS][MAX_WIDTH][4]; /* temp color buffers */
-   GLfloat rgba[MAX_WIDTH][4];
+   float4_array ccolor[4], rgba;
    GLuint i, term;
+   GLuint n = span->end;
+   GLchan (*rgbaChan)[4] = span->array->rgba;
+
+   /* alloc temp pixel buffers */
+   rgba = malloc(4 * n * sizeof(GLfloat));
+   if (!rgba) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+      return;
+   }
 
-   ASSERT(CONST_SWRAST_CONTEXT(ctx)->_AnyTextureCombine);
+   for (i = 0; i < numArgsRGB || i < numArgsA; i++) {
+      ccolor[i] = malloc(4 * n * sizeof(GLfloat));
+      if (!ccolor[i]) {
+         while (i) {
+            free(ccolor[i]);
+            i--;
+         }
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+         free(rgba);
+         return;
+      }
+   }
 
    for (i = 0; i < n; i++) {
       rgba[i][RCOMP] = CHAN_TO_FLOAT(rgbaChan[i][RCOMP]);
@@ -120,7 +144,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
 
       switch (srcRGB) {
          case GL_TEXTURE:
-            argRGB[term] = get_texel_array(texelBuffer, unit, n);
+            argRGB[term] = get_texel_array(swrast, unit);
             break;
          case GL_PRIMARY_COLOR:
             argRGB[term] = primary_rgba;
@@ -165,10 +189,10 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
             /* ARB_texture_env_crossbar source */
             {
                const GLuint srcUnit = srcRGB - GL_TEXTURE0;
-               ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
-               if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
-                  return;
-               argRGB[term] = get_texel_array(texelBuffer, srcUnit, n);
+               assert(srcUnit < ctx->Const.MaxTextureUnits);
+               if (!ctx->Texture.Unit[srcUnit]._Current)
+                  goto end;
+               argRGB[term] = get_texel_array(swrast, srcUnit);
             }
       }
 
@@ -189,15 +213,15 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
             break;
          case GL_SRC_ALPHA:
             for (i = 0; i < n; i++) {
-               dst[i][RCOMP] = src[i][ACOMP];
-               dst[i][GCOMP] = src[i][ACOMP];
+               dst[i][RCOMP] =
+               dst[i][GCOMP] =
                dst[i][BCOMP] = src[i][ACOMP];
             }
             break;
          case GL_ONE_MINUS_SRC_ALPHA:
             for (i = 0; i < n; i++) {
-               dst[i][RCOMP] = 1.0F - src[i][ACOMP];
-               dst[i][GCOMP] = 1.0F - src[i][ACOMP];
+               dst[i][RCOMP] =
+               dst[i][GCOMP] =
                dst[i][BCOMP] = 1.0F - src[i][ACOMP];
             }
             break;
@@ -216,7 +240,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
 
       switch (srcA) {
          case GL_TEXTURE:
-            argA[term] = get_texel_array(texelBuffer, unit, n);
+            argA[term] = get_texel_array(swrast, unit);
             break;
          case GL_PRIMARY_COLOR:
             argA[term] = primary_rgba;
@@ -255,10 +279,10 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
             /* ARB_texture_env_crossbar source */
             {
                const GLuint srcUnit = srcA - GL_TEXTURE0;
-               ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
-               if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
-                  return;
-               argA[term] = get_texel_array(texelBuffer, srcUnit, n);
+               assert(srcUnit < ctx->Const.MaxTextureUnits);
+               if (!ctx->Texture.Unit[srcUnit]._Current)
+                  goto end;
+               argA[term] = get_texel_array(swrast, srcUnit);
             }
       }
 
@@ -319,19 +343,19 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
          if (textureUnit->EnvMode == GL_COMBINE4_NV) {
             /* (a * b) + (c * d) - 0.5 */
             for (i = 0; i < n; i++) {
-               rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP] *
-                                 arg2[i][RCOMP] + arg3[i][RCOMP] - 0.5) * scaleRGB;
-               rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP] *
-                                 arg2[i][GCOMP] + arg3[i][GCOMP] - 0.5) * scaleRGB;
-               rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP] *
-                                 arg2[i][BCOMP] + arg3[i][BCOMP] - 0.5) * scaleRGB;
+               rgba[i][RCOMP] = (arg0[i][RCOMP] * arg1[i][RCOMP] +
+                                 arg2[i][RCOMP] * arg3[i][RCOMP] - 0.5F) * scaleRGB;
+               rgba[i][GCOMP] = (arg0[i][GCOMP] * arg1[i][GCOMP] +
+                                 arg2[i][GCOMP] * arg3[i][GCOMP] - 0.5F) * scaleRGB;
+               rgba[i][BCOMP] = (arg0[i][BCOMP] * arg1[i][BCOMP] +
+                                 arg2[i][BCOMP] * arg3[i][BCOMP] - 0.5F) * scaleRGB;
             }
          }
          else {
             for (i = 0; i < n; i++) {
-               rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP] - 0.5) * scaleRGB;
-               rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP] - 0.5) * scaleRGB;
-               rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP] - 0.5) * scaleRGB;
+               rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP] - 0.5F) * scaleRGB;
+               rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP] - 0.5F) * scaleRGB;
+               rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP] - 0.5F) * scaleRGB;
             }
          }
          break;
@@ -361,7 +385,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
                            (arg0[i][BCOMP] - 0.5F) * (arg1[i][BCOMP] - 0.5F))
                * 4.0F;
             dot = CLAMP(dot, 0.0F, 1.0F);
-            rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLfloat) dot;
+            rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = dot;
          }
          break;
       case GL_DOT3_RGB:
@@ -372,8 +396,8 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
                            (arg0[i][GCOMP] - 0.5F) * (arg1[i][GCOMP] - 0.5F) +
                            (arg0[i][BCOMP] - 0.5F) * (arg1[i][BCOMP] - 0.5F))
                * 4.0F * scaleRGB;
-            dot = CLAMP(dot, 0.0, 1.0F);
-            rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLfloat) dot;
+            dot = CLAMP(dot, 0.0F, 1.0F);
+            rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = dot;
          }
          break;
       case GL_MODULATE_ADD_ATI:
@@ -389,11 +413,11 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
       case GL_MODULATE_SIGNED_ADD_ATI:
          for (i = 0; i < n; i++) {
             rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) +
-                              arg1[i][RCOMP] - 0.5) * scaleRGB;
+                              arg1[i][RCOMP] - 0.5F) * scaleRGB;
             rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) +
-                              arg1[i][GCOMP] - 0.5) * scaleRGB;
+                              arg1[i][GCOMP] - 0.5F) * scaleRGB;
             rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) +
-                              arg1[i][BCOMP] - 0.5) * scaleRGB;
+                              arg1[i][BCOMP] - 0.5F) * scaleRGB;
         }
          break;
       case GL_MODULATE_SUBTRACT_ATI:
@@ -406,25 +430,6 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
                               arg1[i][BCOMP]) * scaleRGB;
         }
          break;
-      case GL_BUMP_ENVMAP_ATI:
-         {
-            /* this produces a fixed rgba color, and the coord calc is done elsewhere */
-            for (i = 0; i < n; i++) {
-            /* rgba result is 0,0,0,1 */
-#if CHAN_TYPE == GL_FLOAT
-               rgba[i][RCOMP] = 0.0;
-               rgba[i][GCOMP] = 0.0;
-               rgba[i][BCOMP] = 0.0;
-               rgba[i][ACOMP] = 1.0;
-#else
-               rgba[i][RCOMP] = 0;
-               rgba[i][GCOMP] = 0;
-               rgba[i][BCOMP] = 0;
-               rgba[i][ACOMP] = CHAN_MAX;
-#endif
-            }
-        }
-         return; /* no alpha processing */
       default:
          _mesa_problem(ctx, "invalid combine mode");
       }
@@ -440,8 +445,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
       switch (combine->ModeA) {
       case GL_REPLACE:
          for (i = 0; i < n; i++) {
-            GLfloat a = arg0[i][ACOMP] * scaleA;
-            rgba[i][ACOMP] = (GLfloat) MIN2(a, 1.0F);
+            rgba[i][ACOMP] = arg0[i][ACOMP] * scaleA;
          }
          break;
       case GL_MODULATE:
@@ -470,7 +474,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
             for (i = 0; i < n; i++) {
                rgba[i][ACOMP] = (arg0[i][ACOMP] * arg1[i][ACOMP] +
                                  arg2[i][ACOMP] * arg3[i][ACOMP] -
-                                 0.5) * scaleA;
+                                 0.5F) * scaleA;
             }
          }
          else {
@@ -481,7 +485,7 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
          }
          break;
       case GL_INTERPOLATE:
-         for (i=0; i<n; i++) {
+         for (i = 0; i < n; i++) {
             rgba[i][ACOMP] = (arg0[i][ACOMP] * arg2[i][ACOMP] +
                               arg1[i][ACOMP] * (1.0F - arg2[i][ACOMP]))
                * scaleA;
@@ -533,8 +537,17 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
       UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][BCOMP], rgba[i][BCOMP]);
       UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][ACOMP], rgba[i][ACOMP]);
    }
-}
+   /* The span->array->rgba values are of CHAN type so set
+    * span->array->ChanType field accordingly.
+    */
+   span->array->ChanType = CHAN_TYPE;
 
+end:
+   for (i = 0; i < numArgsRGB || i < numArgsA; i++) {
+      free(ccolor[i]);
+   }
+   free(rgba);
+}
 
 
 /**
@@ -568,400 +581,60 @@ swizzle_texels(GLuint swizzle, GLuint count, float4_array texels)
 
 
 /**
- * Apply a conventional OpenGL texture env mode (REPLACE, ADD, BLEND,
- * MODULATE, or DECAL) to an array of fragments.
- * Input:  textureUnit - pointer to texture unit to apply
- *         format - base internal texture format
- *         n - number of fragments
- *         primary_rgba - primary colors (may alias rgba for single texture)
- *         texels - array of texel colors
- * InOut:  rgba - incoming fragment colors modified by texel colors
- *                according to the texture environment mode.
+ * Apply texture mapping to a span of fragments.
  */
-static void
-texture_apply( const GLcontext *ctx,
-               const struct gl_texture_unit *texUnit,
-               GLuint n,
-               float4_array primary_rgba,
-               float4_array texel,
-               GLchan rgbaChan[][4] )
+void
+_swrast_texture_span( struct gl_context *ctx, SWspan *span )
 {
-   GLint baseLevel;
-   GLuint i;
-   GLfloat Rc, Gc, Bc, Ac;
-   GLenum format;
-   GLfloat rgba[MAX_WIDTH][4];
-
-   (void) primary_rgba;
-
-   ASSERT(texUnit);
-   ASSERT(texUnit->_Current);
-
-   baseLevel = texUnit->_Current->BaseLevel;
-   ASSERT(texUnit->_Current->Image[0][baseLevel]);
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   float4_array primary_rgba;
+   GLuint unit;
 
-   format = texUnit->_Current->Image[0][baseLevel]->_BaseFormat;
+   if (!swrast->TexelBuffer) {
+#ifdef _OPENMP
+      const GLint maxThreads = omp_get_max_threads();
+
+      /* TexelBuffer memory allocation needs to be done in a critical section
+       * as this code runs in a parallel loop.
+       * When entering the section, first check if TexelBuffer has been
+       * initialized already by another thread while this thread was waiting.
+       */
+      #pragma omp critical
+      if (!swrast->TexelBuffer) {
+#else
+      const GLint maxThreads = 1;
+#endif
 
-   if (format == GL_COLOR_INDEX || format == GL_YCBCR_MESA) {
-      format = GL_RGBA;  /* a bit of a hack */
-   }
-   else if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
-      format = texUnit->_Current->DepthMode;
-   }
+      /* TexelBuffer is also global and normally shared by all SWspan
+       * instances; when running with multiple threads, create one per
+       * thread.
+       */
+      swrast->TexelBuffer =
+        malloc(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits * maxThreads *
+                           SWRAST_MAX_WIDTH * 4 * sizeof(GLfloat));
+#ifdef _OPENMP
+      } /* critical section */
+#endif
 
-   if (texUnit->EnvMode != GL_REPLACE) {
-      /* convert GLchan colors to GLfloat */
-      for (i = 0; i < n; i++) {
-         rgba[i][RCOMP] = CHAN_TO_FLOAT(rgbaChan[i][RCOMP]);
-         rgba[i][GCOMP] = CHAN_TO_FLOAT(rgbaChan[i][GCOMP]);
-         rgba[i][BCOMP] = CHAN_TO_FLOAT(rgbaChan[i][BCOMP]);
-         rgba[i][ACOMP] = CHAN_TO_FLOAT(rgbaChan[i][ACOMP]);
+      if (!swrast->TexelBuffer) {
+        _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+        return;
       }
    }
 
-   switch (texUnit->EnvMode) {
-      case GL_REPLACE:
-        switch (format) {
-           case GL_ALPHA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf */
-                  /* Av = At */
-                  rgba[i][ACOMP] = texel[i][ACOMP];
-              }
-              break;
-           case GL_LUMINANCE:
-              for (i=0;i<n;i++) {
-                 /* Cv = Lt */
-                  GLfloat Lt = texel[i][RCOMP];
-                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = Lt;
-                  /* Av = Af */
-              }
-              break;
-           case GL_LUMINANCE_ALPHA:
-              for (i=0;i<n;i++) {
-                  GLfloat Lt = texel[i][RCOMP];
-                 /* Cv = Lt */
-                 rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = Lt;
-                 /* Av = At */
-                 rgba[i][ACOMP] = texel[i][ACOMP];
-              }
-              break;
-           case GL_INTENSITY:
-              for (i=0;i<n;i++) {
-                 /* Cv = It */
-                  GLfloat It = texel[i][RCOMP];
-                  rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = It;
-                  /* Av = It */
-                  rgba[i][ACOMP] = It;
-              }
-              break;
-           case GL_RGB:
-              for (i=0;i<n;i++) {
-                 /* Cv = Ct */
-                 rgba[i][RCOMP] = texel[i][RCOMP];
-                 rgba[i][GCOMP] = texel[i][GCOMP];
-                 rgba[i][BCOMP] = texel[i][BCOMP];
-                 /* Av = Af */
-              }
-              break;
-           case GL_RGBA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Ct */
-                 rgba[i][RCOMP] = texel[i][RCOMP];
-                 rgba[i][GCOMP] = texel[i][GCOMP];
-                 rgba[i][BCOMP] = texel[i][BCOMP];
-                 /* Av = At */
-                 rgba[i][ACOMP] = texel[i][ACOMP];
-              }
-              break;
-            default:
-               _mesa_problem(ctx, "Bad format (GL_REPLACE) in texture_apply");
-               return;
-        }
-        break;
-
-      case GL_MODULATE:
-         switch (format) {
-           case GL_ALPHA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf */
-                 /* Av = AfAt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-           case GL_LUMINANCE:
-              for (i=0;i<n;i++) {
-                 /* Cv = LtCf */
-                  GLfloat Lt = texel[i][RCOMP];
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * Lt;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * Lt;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * Lt;
-                 /* Av = Af */
-              }
-              break;
-           case GL_LUMINANCE_ALPHA:
-              for (i=0;i<n;i++) {
-                 /* Cv = CfLt */
-                  GLfloat Lt = texel[i][RCOMP];
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * Lt;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * Lt;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * Lt;
-                 /* Av = AfAt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-           case GL_INTENSITY:
-              for (i=0;i<n;i++) {
-                 /* Cv = CfIt */
-                  GLfloat It = texel[i][RCOMP];
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * It;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * It;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * It;
-                 /* Av = AfIt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * It;
-              }
-              break;
-           case GL_RGB:
-              for (i=0;i<n;i++) {
-                 /* Cv = CfCt */
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * texel[i][RCOMP];
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * texel[i][GCOMP];
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * texel[i][BCOMP];
-                 /* Av = Af */
-              }
-              break;
-           case GL_RGBA:
-              for (i=0;i<n;i++) {
-                 /* Cv = CfCt */
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * texel[i][RCOMP];
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * texel[i][GCOMP];
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * texel[i][BCOMP];
-                 /* Av = AfAt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-            default:
-               _mesa_problem(ctx, "Bad format (GL_MODULATE) in texture_apply");
-               return;
-        }
-        break;
-
-      case GL_DECAL:
-         switch (format) {
-            case GL_ALPHA:
-            case GL_LUMINANCE:
-            case GL_LUMINANCE_ALPHA:
-            case GL_INTENSITY:
-               /* undefined */
-               break;
-           case GL_RGB:
-              for (i=0;i<n;i++) {
-                 /* Cv = Ct */
-                 rgba[i][RCOMP] = texel[i][RCOMP];
-                 rgba[i][GCOMP] = texel[i][GCOMP];
-                 rgba[i][BCOMP] = texel[i][BCOMP];
-                 /* Av = Af */
-              }
-              break;
-           case GL_RGBA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-At) + CtAt */
-                 GLfloat t = texel[i][ACOMP], s = 1.0F - t;
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * s + texel[i][RCOMP] * t;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * s + texel[i][GCOMP] * t;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * s + texel[i][BCOMP] * t;
-                 /* Av = Af */
-              }
-              break;
-            default:
-               _mesa_problem(ctx, "Bad format (GL_DECAL) in texture_apply");
-               return;
-        }
-        break;
-
-      case GL_BLEND:
-         Rc = texUnit->EnvColor[0];
-         Gc = texUnit->EnvColor[1];
-         Bc = texUnit->EnvColor[2];
-         Ac = texUnit->EnvColor[3];
-        switch (format) {
-           case GL_ALPHA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf */
-                 /* Av = AfAt */
-                  rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-            case GL_LUMINANCE:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-Lt) + CcLt */
-                 GLfloat Lt = texel[i][RCOMP], s = 1.0F - Lt;
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * s + Rc * Lt;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * s + Gc * Lt;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * s + Bc * Lt;
-                 /* Av = Af */
-              }
-              break;
-           case GL_LUMINANCE_ALPHA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-Lt) + CcLt */
-                 GLfloat Lt = texel[i][RCOMP], s = 1.0F - Lt;
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * s + Rc * Lt;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * s + Gc * Lt;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * s + Bc * Lt;
-                 /* Av = AfAt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-            case GL_INTENSITY:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-It) + CcIt */
-                 GLfloat It = texel[i][RCOMP], s = 1.0F - It;
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * s + Rc * It;
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * s + Gc * It;
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * s + Bc * It;
-                  /* Av = Af(1-It) + Ac*It */
-                  rgba[i][ACOMP] = rgba[i][ACOMP] * s + Ac * It;
-               }
-               break;
-           case GL_RGB:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-Ct) + CcCt */
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * (1.0F - texel[i][RCOMP])
-                     + Rc * texel[i][RCOMP];
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * (1.0F - texel[i][GCOMP])
-                     + Gc * texel[i][GCOMP];
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * (1.0F - texel[i][BCOMP])
-                     + Bc * texel[i][BCOMP];
-                 /* Av = Af */
-              }
-              break;
-           case GL_RGBA:
-              for (i=0;i<n;i++) {
-                 /* Cv = Cf(1-Ct) + CcCt */
-                 rgba[i][RCOMP] = rgba[i][RCOMP] * (1.0F - texel[i][RCOMP])
-                     + Rc * texel[i][RCOMP];
-                 rgba[i][GCOMP] = rgba[i][GCOMP] * (1.0F - texel[i][GCOMP])
-                     + Gc * texel[i][GCOMP];
-                 rgba[i][BCOMP] = rgba[i][BCOMP] * (1.0F - texel[i][BCOMP])
-                     + Bc * texel[i][BCOMP];
-                 /* Av = AfAt */
-                 rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-              }
-              break;
-            default:
-               _mesa_problem(ctx, "Bad format (GL_BLEND) in texture_apply");
-               return;
-        }
-        break;
-
-      case GL_ADD:  /* GL_EXT_texture_add_env */
-         switch (format) {
-            case GL_ALPHA:
-               for (i=0;i<n;i++) {
-                  /* Rv = Rf */
-                  /* Gv = Gf */
-                  /* Bv = Bf */
-                  rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-               }
-               break;
-            case GL_LUMINANCE:
-               for (i=0;i<n;i++) {
-                  GLfloat Lt = texel[i][RCOMP];
-                  GLfloat r = rgba[i][RCOMP] + Lt;
-                  GLfloat g = rgba[i][GCOMP] + Lt;
-                  GLfloat b = rgba[i][BCOMP] + Lt;
-                  rgba[i][RCOMP] = MIN2(r, 1.0F);
-                  rgba[i][GCOMP] = MIN2(g, 1.0F);
-                  rgba[i][BCOMP] = MIN2(b, 1.0F);
-                  /* Av = Af */
-               }
-               break;
-            case GL_LUMINANCE_ALPHA:
-               for (i=0;i<n;i++) {
-                  GLfloat Lt = texel[i][RCOMP];
-                  GLfloat r = rgba[i][RCOMP] + Lt;
-                  GLfloat g = rgba[i][GCOMP] + Lt;
-                  GLfloat b = rgba[i][BCOMP] + Lt;
-                  rgba[i][RCOMP] = MIN2(r, 1.0F);
-                  rgba[i][GCOMP] = MIN2(g, 1.0F);
-                  rgba[i][BCOMP] = MIN2(b, 1.0F);
-                  rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-               }
-               break;
-            case GL_INTENSITY:
-               for (i=0;i<n;i++) {
-                  GLfloat It = texel[i][RCOMP];
-                  GLfloat r = rgba[i][RCOMP] + It;
-                  GLfloat g = rgba[i][GCOMP] + It;
-                  GLfloat b = rgba[i][BCOMP] + It;
-                  GLfloat a = rgba[i][ACOMP] + It;
-                  rgba[i][RCOMP] = MIN2(r, 1.0F);
-                  rgba[i][GCOMP] = MIN2(g, 1.0F);
-                  rgba[i][BCOMP] = MIN2(b, 1.0F);
-                  rgba[i][ACOMP] = MIN2(a, 1.0F);
-               }
-               break;
-           case GL_RGB:
-              for (i=0;i<n;i++) {
-                  GLfloat r = rgba[i][RCOMP] + texel[i][RCOMP];
-                  GLfloat g = rgba[i][GCOMP] + texel[i][GCOMP];
-                  GLfloat b = rgba[i][BCOMP] + texel[i][BCOMP];
-                 rgba[i][RCOMP] = MIN2(r, 1.0F);
-                 rgba[i][GCOMP] = MIN2(g, 1.0F);
-                 rgba[i][BCOMP] = MIN2(b, 1.0F);
-                 /* Av = Af */
-              }
-              break;
-           case GL_RGBA:
-              for (i=0;i<n;i++) {
-                  GLfloat r = rgba[i][RCOMP] + texel[i][RCOMP];
-                  GLfloat g = rgba[i][GCOMP] + texel[i][GCOMP];
-                  GLfloat b = rgba[i][BCOMP] + texel[i][BCOMP];
-                 rgba[i][RCOMP] = MIN2(r, 1.0F);
-                 rgba[i][GCOMP] = MIN2(g, 1.0F);
-                 rgba[i][BCOMP] = MIN2(b, 1.0F);
-                  rgba[i][ACOMP] = rgba[i][ACOMP] * texel[i][ACOMP];
-               }
-               break;
-            default:
-               _mesa_problem(ctx, "Bad format (GL_ADD) in texture_apply");
-               return;
-        }
-        break;
-
-      default:
-         _mesa_problem(ctx, "Bad env mode in texture_apply");
-         return;
-   }
+   primary_rgba = malloc(span->end * 4 * sizeof(GLfloat));
 
-   /* convert GLfloat colors to GLchan */
-   for (i = 0; i < n; i++) {
-      CLAMPED_FLOAT_TO_CHAN(rgbaChan[i][RCOMP], rgba[i][RCOMP]);
-      CLAMPED_FLOAT_TO_CHAN(rgbaChan[i][GCOMP], rgba[i][GCOMP]);
-      CLAMPED_FLOAT_TO_CHAN(rgbaChan[i][BCOMP], rgba[i][BCOMP]);
-      CLAMPED_FLOAT_TO_CHAN(rgbaChan[i][ACOMP], rgba[i][ACOMP]);
+   if (!primary_rgba) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_span");
+      return;
    }
-}
 
-
-
-/**
- * Apply texture mapping to a span of fragments.
- */
-void
-_swrast_texture_span( GLcontext *ctx, SWspan *span )
-{
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLfloat primary_rgba[MAX_WIDTH][4];
-   GLuint unit;
-
-   ASSERT(span->end <= MAX_WIDTH);
+   assert(span->end <= SWRAST_MAX_WIDTH);
 
    /*
     * Save copy of the incoming fragment colors (the GL_PRIMARY_COLOR)
     */
-   if (swrast->_AnyTextureCombine) {
+   if (swrast->_TextureCombinePrimary) {
       GLuint i;
       for (i = 0; i < span->end; i++) {
          primary_rgba[i][RCOMP] = CHAN_TO_FLOAT(span->array->rgba[i][RCOMP]);
@@ -971,104 +644,25 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
       }
    }
 
-   /* First must sample all bump maps */
-   for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-      if (ctx->Texture.Unit[unit]._ReallyEnabled &&
-         ctx->Texture.Unit[unit]._CurrentCombine->ModeRGB == GL_BUMP_ENVMAP_ATI) {
-         const GLfloat (*texcoords)[4]
-            = (const GLfloat (*)[4])
-            span->array->attribs[FRAG_ATTRIB_TEX0 + unit];
-         GLfloat (*targetcoords)[4]
-            = (GLfloat (*)[4])
-            span->array->attribs[FRAG_ATTRIB_TEX0 +
-               ctx->Texture.Unit[unit].BumpTarget - GL_TEXTURE0];
-
-         const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         const struct gl_texture_object *curObj = texUnit->_Current;
-         GLfloat *lambda = span->array->lambda[unit];
-#if 0
-         GLchan (*texels)[4] = (GLchan (*)[4])
-            (swrast->TexelBuffer + unit * (span->end * 4 * sizeof(GLchan)));
-#else
-         float4_array texels = get_texel_array(swrast->TexelBuffer, unit,
-                                               span->end);
-#endif
-         GLuint i;
-         GLfloat rotMatrix00 = ctx->Texture.Unit[unit].RotMatrix[0];
-         GLfloat rotMatrix01 = ctx->Texture.Unit[unit].RotMatrix[1];
-         GLfloat rotMatrix10 = ctx->Texture.Unit[unit].RotMatrix[2];
-         GLfloat rotMatrix11 = ctx->Texture.Unit[unit].RotMatrix[3];
-
-         /* adjust texture lod (lambda) */
-         if (span->arrayMask & SPAN_LAMBDA) {
-            if (texUnit->LodBias + curObj->LodBias != 0.0F) {
-               /* apply LOD bias, but don't clamp yet */
-               const GLfloat bias = CLAMP(texUnit->LodBias + curObj->LodBias,
-                                          -ctx->Const.MaxTextureLodBias,
-                                          ctx->Const.MaxTextureLodBias);
-               GLuint i;
-               for (i = 0; i < span->end; i++) {
-                  lambda[i] += bias;
-               }
-            }
-
-            if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
-               /* apply LOD clamping to lambda */
-               const GLfloat min = curObj->MinLod;
-               const GLfloat max = curObj->MaxLod;
-               GLuint i;
-               for (i = 0; i < span->end; i++) {
-                  GLfloat l = lambda[i];
-                  lambda[i] = CLAMP(l, min, max);
-               }
-            }
-         }
-
-         /* Sample the texture (span->end = number of fragments) */
-         swrast->TextureSample[unit]( ctx, texUnit->_Current, span->end,
-                                      texcoords, lambda, texels );
-
-         /* manipulate the span values of the bump target
-            not sure this can work correctly even ignoring
-            the problem that channel is unsigned */
-         for (i = 0; i < span->end; i++) {
-#if CHAN_TYPE == GL_FLOAT
-            targetcoords[i][0] += (texels[i][0] * rotMatrix00 + texels[i][1] *
-                                  rotMatrix01) / targetcoords[i][3];
-            targetcoords[i][1] += (texels[i][0] * rotMatrix10 + texels[i][1] *
-                                  rotMatrix11) / targetcoords[i][3];
-#else
-            targetcoords[i][0] += (CHAN_TO_FLOAT(texels[i][1]) * rotMatrix00 +
-                                  CHAN_TO_FLOAT(texels[i][1]) * rotMatrix01) /
-                                  targetcoords[i][3];
-            targetcoords[i][1] += (CHAN_TO_FLOAT(texels[i][0]) * rotMatrix10 + 
-                                  CHAN_TO_FLOAT(texels[i][1]) * rotMatrix11) /
-                                  targetcoords[i][3];
-#endif
-         }
-      }
-   }
-
    /*
     * Must do all texture sampling before combining in order to
-    * accomodate GL_ARB_texture_env_crossbar.
+    * accommodate GL_ARB_texture_env_crossbar.
     */
    for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-      if (ctx->Texture.Unit[unit]._ReallyEnabled &&
-         ctx->Texture.Unit[unit]._CurrentCombine->ModeRGB != GL_BUMP_ENVMAP_ATI) {
+      const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+      if (texUnit->_Current) {
          const GLfloat (*texcoords)[4] = (const GLfloat (*)[4])
-            span->array->attribs[FRAG_ATTRIB_TEX0 + unit];
-         const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+            span->array->attribs[VARYING_SLOT_TEX0 + unit];
          const struct gl_texture_object *curObj = texUnit->_Current;
+         const struct gl_sampler_object *samp = _mesa_get_samplerobj(ctx, unit);
          GLfloat *lambda = span->array->lambda[unit];
-         float4_array texels =
-            get_texel_array(swrast->TexelBuffer, unit, span->end);
+         float4_array texels = get_texel_array(swrast, unit);
 
          /* adjust texture lod (lambda) */
          if (span->arrayMask & SPAN_LAMBDA) {
-            if (texUnit->LodBias + curObj->LodBias != 0.0F) {
+            if (texUnit->LodBias + samp->LodBias != 0.0F) {
                /* apply LOD bias, but don't clamp yet */
-               const GLfloat bias = CLAMP(texUnit->LodBias + curObj->LodBias,
+               const GLfloat bias = CLAMP(texUnit->LodBias + samp->LodBias,
                                           -ctx->Const.MaxTextureLodBias,
                                           ctx->Const.MaxTextureLodBias);
                GLuint i;
@@ -1077,10 +671,11 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                }
             }
 
-            if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
+            if (samp->MinLod != -1000.0F ||
+                samp->MaxLod != 1000.0F) {
                /* apply LOD clamping to lambda */
-               const GLfloat min = curObj->MinLod;
-               const GLfloat max = curObj->MaxLod;
+               const GLfloat min = samp->MinLod;
+               const GLfloat max = samp->MaxLod;
                GLuint i;
                for (i = 0; i < span->end; i++) {
                   GLfloat l = lambda[i];
@@ -1088,15 +683,23 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                }
             }
          }
+         else if (samp->MaxAnisotropy > 1.0F &&
+                  samp->MinFilter == GL_LINEAR_MIPMAP_LINEAR) {
+            /* sample_lambda_2d_aniso is beeing used as texture_sample_func,
+             * it requires the current SWspan *span as an additional parameter.
+             * In order to keep the same function signature, the unused lambda
+             * parameter will be modified to actually contain the SWspan pointer.
+             * This is a Hack. To make it right, the texture_sample_func
+             * signature and all implementing functions need to be modified.
+             */
+            /* "hide" SWspan struct; cast to (GLfloat *) to suppress warning */
+            lambda = (GLfloat *)span;
+         }
 
          /* Sample the texture (span->end = number of fragments) */
-         swrast->TextureSample[unit]( ctx, texUnit->_Current, span->end,
-                                      texcoords, lambda, texels );
-
-         /* GL_SGI_texture_color_table */
-         if (texUnit->ColorTableEnabled) {
-            _mesa_lookup_rgba_float(&texUnit->ColorTable, span->end, texels);
-         }
+         swrast->TextureSample[unit]( ctx, samp,
+                                      ctx->Texture.Unit[unit]._Current,
+                                      span->end, texcoords, lambda, texels );
 
          /* GL_EXT_texture_swizzle */
          if (curObj->_Swizzle != SWIZZLE_NOOP) {
@@ -1105,28 +708,14 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
       }
    }
 
-
    /*
     * OK, now apply the texture (aka texture combine/blend).
     * We modify the span->color.rgba values.
     */
    for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-      if (ctx->Texture.Unit[unit]._ReallyEnabled) {
-         const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-         if (texUnit->_CurrentCombine != &texUnit->_EnvMode ) {
-            texture_combine( ctx, unit, span->end,
-                             primary_rgba,
-                             swrast->TexelBuffer,
-                             span->array->rgba );
-         }
-         else {
-            /* conventional texture blend */
-            float4_array texels =
-               get_texel_array(swrast->TexelBuffer, unit, span->end);
-            texture_apply( ctx, texUnit, span->end,
-                           primary_rgba, texels,
-                           span->array->rgba );
-         }
-      }
+      if (ctx->Texture.Unit[unit]._Current)
+         texture_combine(ctx, unit, primary_rgba, swrast->TexelBuffer, span);
    }
+
+   free(primary_rgba);
 }