swrast: silence unused var warnings in non-debug builds
[mesa.git] / src / mesa / swrast / s_texcombine.c
index 95e2c082ba721055813881de7fa739a8a9922de4..c67c356c1e01f305dcc7c98f17f36073b76d467b 100644 (file)
@@ -27,9 +27,9 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/colormac.h"
-#include "main/image.h"
 #include "main/imports.h"
-#include "shader/prog_instruction.h"
+#include "main/pixeltransfer.h"
+#include "program/prog_instruction.h"
 
 #include "s_context.h"
 #include "s_texcombine.h"
@@ -45,10 +45,14 @@ typedef float (*float4_array)[4];
 /**
  * Return array of texels for given unit.
  */
-static INLINE float4_array
+static inline float4_array
 get_texel_array(SWcontext *swrast, GLuint unit)
 {
+#ifdef _OPENMP
+   return (float4_array) (swrast->TexelBuffer + unit * MAX_WIDTH * 4 * omp_get_num_threads() + (MAX_WIDTH * 4 * omp_get_thread_num()));
+#else
    return (float4_array) (swrast->TexelBuffer + unit * MAX_WIDTH * 4);
+#endif
 }
 
 
@@ -72,7 +76,7 @@ get_texel_array(SWcontext *swrast, GLuint unit)
  * \param rgba         incoming/result fragment colors
  */
 static void
-texture_combine( GLcontext *ctx, GLuint unit, GLuint n,
+texture_combine( struct gl_context *ctx, GLuint unit, GLuint n,
                  const float4_array primary_rgba,
                  const GLfloat *texelBuffer,
                  GLchan (*rgbaChan)[4] )
@@ -86,10 +90,28 @@ texture_combine( 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;
 
+   /* alloc temp pixel buffers */
+   rgba = (float4_array) malloc(4 * n * sizeof(GLfloat));
+   if (!rgba) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+      return;
+   }
+
+   for (i = 0; i < numArgsRGB || i < numArgsA; i++) {
+      ccolor[i] = (float4_array) malloc(4 * n * sizeof(GLfloat));
+      if (!ccolor[i]) {
+         while (i) {
+            free(ccolor[i]);
+            i--;
+         }
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+         return;
+      }
+   }
+
    for (i = 0; i < n; i++) {
       rgba[i][RCOMP] = CHAN_TO_FLOAT(rgbaChan[i][RCOMP]);
       rgba[i][GCOMP] = CHAN_TO_FLOAT(rgbaChan[i][GCOMP]);
@@ -163,7 +185,7 @@ texture_combine( GLcontext *ctx, GLuint unit, GLuint n,
                const GLuint srcUnit = srcRGB - GL_TEXTURE0;
                ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
                if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
-                  return;
+                  goto end;
                argRGB[term] = get_texel_array(swrast, srcUnit);
             }
       }
@@ -253,7 +275,7 @@ texture_combine( GLcontext *ctx, GLuint unit, GLuint n,
                const GLuint srcUnit = srcA - GL_TEXTURE0;
                ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
                if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
-                  return;
+                  goto end;
                argA[term] = get_texel_array(swrast, srcUnit);
             }
       }
@@ -368,7 +390,7 @@ texture_combine( 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);
+            dot = CLAMP(dot, 0.0F, 1.0F);
             rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = dot;
          }
          break;
@@ -411,7 +433,7 @@ texture_combine( GLcontext *ctx, GLuint unit, GLuint n,
             rgba[i][BCOMP] = 0.0;
             rgba[i][ACOMP] = 1.0;
         }
-         return; /* no alpha processing */
+         goto end; /* no alpha processing */
       default:
          _mesa_problem(ctx, "invalid combine mode");
       }
@@ -519,6 +541,12 @@ texture_combine( 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]);
    }
+
+end:
+   for (i = 0; i < numArgsRGB || i < numArgsA; i++) {
+      free(ccolor[i]);
+   }
+   free(rgba);
 }
 
 
@@ -556,12 +584,39 @@ swizzle_texels(GLuint swizzle, GLuint count, float4_array texels)
  * Apply texture mapping to a span of fragments.
  */
 void
-_swrast_texture_span( GLcontext *ctx, SWspan *span )
+_swrast_texture_span( struct gl_context *ctx, SWspan *span )
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLfloat primary_rgba[MAX_WIDTH][4];
+   float4_array primary_rgba;
    GLuint unit;
 
+   if (!swrast->TexelBuffer) {
+#ifdef _OPENMP
+      const GLint maxThreads = omp_get_max_threads();
+#else
+      const GLint maxThreads = 1;
+#endif
+
+      /* TexelBuffer is also global and normally shared by all SWspan
+       * instances; when running with multiple threads, create one per
+       * thread.
+       */
+      swrast->TexelBuffer =
+        (GLfloat *) MALLOC(ctx->Const.MaxTextureImageUnits * maxThreads *
+                           MAX_WIDTH * 4 * sizeof(GLfloat));
+      if (!swrast->TexelBuffer) {
+        _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_combine");
+        return;
+      }
+   }
+
+   primary_rgba = (float4_array) malloc(span->end * 4 * sizeof(GLfloat));
+
+   if (!primary_rgba) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture_span");
+      return;
+   }
+
    ASSERT(span->end <= MAX_WIDTH);
 
    /*
@@ -600,9 +655,9 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
 
          /* adjust texture lod (lambda) */
          if (span->arrayMask & SPAN_LAMBDA) {
-            if (texUnit->LodBias + curObj->LodBias != 0.0F) {
+            if (texUnit->LodBias + curObj->Sampler.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 + curObj->Sampler.LodBias,
                                           -ctx->Const.MaxTextureLodBias,
                                           ctx->Const.MaxTextureLodBias);
                GLuint i;
@@ -611,10 +666,11 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                }
             }
 
-            if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
+            if (curObj->Sampler.MinLod != -1000.0 ||
+                curObj->Sampler.MaxLod != 1000.0) {
                /* apply LOD clamping to lambda */
-               const GLfloat min = curObj->MinLod;
-               const GLfloat max = curObj->MaxLod;
+               const GLfloat min = curObj->Sampler.MinLod;
+               const GLfloat max = curObj->Sampler.MaxLod;
                GLuint i;
                for (i = 0; i < span->end; i++) {
                   GLfloat l = lambda[i];
@@ -655,9 +711,9 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
 
          /* adjust texture lod (lambda) */
          if (span->arrayMask & SPAN_LAMBDA) {
-            if (texUnit->LodBias + curObj->LodBias != 0.0F) {
+            if (texUnit->LodBias + curObj->Sampler.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 + curObj->Sampler.LodBias,
                                           -ctx->Const.MaxTextureLodBias,
                                           ctx->Const.MaxTextureLodBias);
                GLuint i;
@@ -666,10 +722,11 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                }
             }
 
-            if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
+            if (curObj->Sampler.MinLod != -1000.0 ||
+                curObj->Sampler.MaxLod != 1000.0) {
                /* apply LOD clamping to lambda */
-               const GLfloat min = curObj->MinLod;
-               const GLfloat max = curObj->MaxLod;
+               const GLfloat min = curObj->Sampler.MinLod;
+               const GLfloat max = curObj->Sampler.MaxLod;
                GLuint i;
                for (i = 0; i < span->end; i++) {
                   GLfloat l = lambda[i];
@@ -677,16 +734,23 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                }
             }
          }
+         else if (curObj->Sampler.MaxAnisotropy > 1.0 &&
+                  curObj->Sampler.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);
-         }
-
          /* GL_EXT_texture_swizzle */
          if (curObj->_Swizzle != SWIZZLE_NOOP) {
             swizzle_texels(curObj->_Swizzle, span->end, texels);
@@ -706,4 +770,6 @@ _swrast_texture_span( GLcontext *ctx, SWspan *span )
                           span->array->rgba );
       }
    }
+
+   free(primary_rgba);
 }