more clean-ups
[mesa.git] / src / mesa / swrast / s_texture.c
index b8428c9a66a902cf155222d800227f244cc1c5cc..60091e3743ee7069064cbe5acf536bb5a9a7ef13 100644 (file)
@@ -1,8 +1,8 @@
-/* $Id: s_texture.c,v 1.67 2002/09/23 16:37:15 brianp Exp $ */
+/* $Id: s_texture.c,v 1.75 2002/11/12 19:27:24 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  4.1
+ * Version:  5.0
  *
  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  *
@@ -30,7 +30,7 @@
 #include "colormac.h"
 #include "macros.h"
 #include "mmath.h"
-#include "mem.h"
+#include "imports.h"
 #include "texformat.h"
 #include "teximage.h"
 
          U = 1.0F - (S - (GLfloat) flr);       /* flr is odd */        \
       else                                                             \
          U = S - (GLfloat) flr;                /* flr is even */               \
+      U = (U * SIZE) - 0.5F;                                           \
+      I0 = IFLOOR(U);                                                  \
+      I1 = I0 + 1;                                                     \
+      if (I0 < 0)                                                      \
+         I0 = 0;                                                       \
+      if (I1 >= (GLint) SIZE)                                          \
+         I1 = SIZE - 1;                                                        \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_ATI) {                         \
+      U = (GLfloat) fabs(S);                                           \
+      if (U >= 1.0F)                                                   \
+         U = (GLfloat) SIZE;                                           \
+      else                                                             \
+         U *= SIZE;                                                    \
+      U -= 0.5F;                                                       \
+      I0 = IFLOOR(U);                                                  \
+      I1 = I0 + 1;                                                     \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_ATI) {                 \
+      U = (GLfloat) fabs(S);                                           \
+      if (U >= 1.0F)                                                   \
+         U = (GLfloat) SIZE;                                           \
+      else                                                             \
+         U *= SIZE;                                                    \
+      U -= 0.5F;                                                       \
       I0 = IFLOOR(U);                                                  \
       I1 = I0 + 1;                                                     \
       if (I0 < 0)                                                      \
       else                                                             \
          I = IFLOOR(u * SIZE);                                         \
    }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_ATI) {                         \
+      /* s limited to [0,1] */                                         \
+      /* i limited to [0,size-1] */                                    \
+      const GLfloat u = (GLfloat) fabs(S);                             \
+      if (u <= 0.0F)                                                   \
+         I = 0;                                                                \
+      else if (u >= 1.0F)                                              \
+         I = SIZE - 1;                                                 \
+      else                                                             \
+         I = IFLOOR(u * SIZE);                                         \
+   }                                                                   \
+   else if (wrapMode == GL_MIRROR_CLAMP_TO_EDGE_ATI) {                 \
+      /* s limited to [min,max] */                                     \
+      /* i limited to [0, size-1] */                                   \
+      const GLfloat min = 1.0F / (2.0F * SIZE);                                \
+      const GLfloat max = 1.0F - min;                                  \
+      const GLfloat u = (GLfloat) fabs(S);                             \
+      if (u < min)                                                     \
+         I = 0;                                                                \
+      else if (u > max)                                                        \
+         I = SIZE - 1;                                                 \
+      else                                                             \
+         I = IFLOOR(u * SIZE);                                         \
+   }                                                                   \
    else {                                                              \
       ASSERT(wrapMode == GL_CLAMP);                                    \
       /* s limited to [0,1] */                                         \
@@ -417,7 +466,7 @@ sample_1d_nearest(GLcontext *ctx,
 
    if (i < 0 || i >= (GLint) img->Width) {
       /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
       (*img->FetchTexel)(img, i, 0, 0, (GLvoid *) rgba);
@@ -469,7 +518,7 @@ sample_1d_linear(GLcontext *ctx,
       GLchan t0[4], t1[4];  /* texels */
 
       if (useBorderColor & I0BIT) {
-         COPY_CHAN4(t0, tObj->BorderColor);
+         COPY_CHAN4(t0, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, 0, 0, (GLvoid *) t0);
@@ -478,7 +527,7 @@ sample_1d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & I1BIT) {
-         COPY_CHAN4(t1, tObj->BorderColor);
+         COPY_CHAN4(t1, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, 0, 0, (GLvoid *) t1);
@@ -746,7 +795,7 @@ 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_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
       (*img->FetchTexel)(img, i, j, 0, (GLvoid *) rgba);
@@ -814,7 +863,7 @@ sample_2d_linear(GLcontext *ctx,
       GLchan t11[4];
 
       if (useBorderColor & (I0BIT | J0BIT)) {
-         COPY_CHAN4(t00, tObj->BorderColor);
+         COPY_CHAN4(t00, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j0, 0, (GLvoid *) t00);
@@ -823,7 +872,7 @@ sample_2d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J0BIT)) {
-         COPY_CHAN4(t10, tObj->BorderColor);
+         COPY_CHAN4(t10, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j0, 0, (GLvoid *) t10);
@@ -832,7 +881,7 @@ sample_2d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I0BIT | J1BIT)) {
-         COPY_CHAN4(t01, tObj->BorderColor);
+         COPY_CHAN4(t01, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j1, 0, (GLvoid *) t01);
@@ -841,7 +890,7 @@ sample_2d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J1BIT)) {
-         COPY_CHAN4(t11, tObj->BorderColor);
+         COPY_CHAN4(t11, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j1, 0, (GLvoid *) t11);
@@ -1334,7 +1383,7 @@ sample_3d_nearest(GLcontext *ctx,
        j < 0 || j >= (GLint) img->Height ||
        k < 0 || k >= (GLint) img->Depth) {
       /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
-      COPY_CHAN4(rgba, tObj->BorderColor);
+      COPY_CHAN4(rgba, tObj->_BorderChan);
    }
    else {
       (*img->FetchTexel)(img, i, j, k, (GLvoid *) rgba);
@@ -1417,7 +1466,7 @@ sample_3d_linear(GLcontext *ctx,
       GLchan t100[4], t110[4], t101[4], t111[4];
 
       if (useBorderColor & (I0BIT | J0BIT | K0BIT)) {
-         COPY_CHAN4(t000, tObj->BorderColor);
+         COPY_CHAN4(t000, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j0, k0, (GLvoid *) t000);
@@ -1426,7 +1475,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J0BIT | K0BIT)) {
-         COPY_CHAN4(t100, tObj->BorderColor);
+         COPY_CHAN4(t100, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j0, k0, (GLvoid *) t100);
@@ -1435,7 +1484,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I0BIT | J1BIT | K0BIT)) {
-         COPY_CHAN4(t010, tObj->BorderColor);
+         COPY_CHAN4(t010, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j1, k0, (GLvoid *) t010);
@@ -1444,7 +1493,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J1BIT | K0BIT)) {
-         COPY_CHAN4(t110, tObj->BorderColor);
+         COPY_CHAN4(t110, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j1, k0, (GLvoid *) t110);
@@ -1454,7 +1503,7 @@ sample_3d_linear(GLcontext *ctx,
       }
 
       if (useBorderColor & (I0BIT | J0BIT | K1BIT)) {
-         COPY_CHAN4(t001, tObj->BorderColor);
+         COPY_CHAN4(t001, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j0, k1, (GLvoid *) t001);
@@ -1463,7 +1512,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J0BIT | K1BIT)) {
-         COPY_CHAN4(t101, tObj->BorderColor);
+         COPY_CHAN4(t101, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j0, k1, (GLvoid *) t101);
@@ -1472,7 +1521,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I0BIT | J1BIT | K1BIT)) {
-         COPY_CHAN4(t011, tObj->BorderColor);
+         COPY_CHAN4(t011, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i0, j1, k1, (GLvoid *) t011);
@@ -1481,7 +1530,7 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
       if (useBorderColor & (I1BIT | J1BIT | K1BIT)) {
-         COPY_CHAN4(t111, tObj->BorderColor);
+         COPY_CHAN4(t111, tObj->_BorderChan);
       }
       else {
          (*img->FetchTexel)(img, i1, j1, k1, (GLvoid *) t111);
@@ -2153,10 +2202,14 @@ sample_linear_rect(GLcontext *ctx, GLuint texUnit,
       w11 =       a  *       b ;
 
       /* compute weighted average of samples */
-      rgba[i][0] = w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0];
-      rgba[i][1] = w00 * t00[1] + w10 * t10[1] + w01 * t01[1] + w11 * t11[1];
-      rgba[i][2] = w00 * t00[2] + w10 * t10[2] + w01 * t01[2] + w11 * t11[2];
-      rgba[i][3] = w00 * t00[3] + w10 * t10[3] + w01 * t01[3] + w11 * t11[3];
+      rgba[i][0] = 
+         (GLchan) (w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0]);
+      rgba[i][1] = 
+         (GLchan) (w00 * t00[1] + w10 * t10[1] + w01 * t01[1] + w11 * t11[1]);
+      rgba[i][2] = 
+         (GLchan) (w00 * t00[2] + w10 * t10[2] + w01 * t01[2] + w11 * t11[2]);
+      rgba[i][3] = 
+         (GLchan) (w00 * t00[3] + w10 * t10[3] + w01 * t01[3] + w11 * t11[3]);
    }
 }
 
@@ -2212,7 +2265,7 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
    const struct gl_texture_image *texImage = tObj->Image[baseLevel];
    const GLuint width = texImage->Width;
    const GLuint height = texImage->Height;
-   const GLchan ambient = tObj->ShadowAmbient;
+   GLchan ambient;
    GLenum function;
    GLchan result;
 
@@ -2223,6 +2276,8 @@ sample_depth_texture( GLcontext *ctx, GLuint unit,
           tObj->Target == GL_TEXTURE_2D ||
           tObj->Target == GL_TEXTURE_RECTANGLE_NV);
 
+   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
+
    /* XXXX if tObj->MinFilter != tObj->MagFilter, we're ignoring lambda */
 
    /* XXX this could be precomputed and saved in the texture object */
@@ -2501,7 +2556,7 @@ sample_depth_texture2(const GLcontext *ctx,
    const struct gl_texture_image *texImage = texObj->Image[baseLevel];
    const GLuint width = texImage->Width;
    const GLuint height = texImage->Height;
-   const GLchan ambient = texObj->ShadowAmbient;
+   GLchan ambient;
    GLboolean lequal, gequal;
 
    if (texObj->Target != GL_TEXTURE_2D) {
@@ -2523,6 +2578,8 @@ sample_depth_texture2(const GLcontext *ctx,
       return;
    }
 
+   UNCLAMPED_FLOAT_TO_CHAN(ambient, tObj->ShadowAmbient);
+
    if (texObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
       lequal = GL_TRUE;
       gequal = GL_FALSE;
@@ -3082,8 +3139,8 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
                GLchan dot = ((arg0[i][RCOMP]-0.5F) * (arg1[i][RCOMP]-0.5F) +
                              (arg0[i][GCOMP]-0.5F) * (arg1[i][GCOMP]-0.5F) +
                              (arg0[i][BCOMP]-0.5F) * (arg1[i][BCOMP]-0.5F))
-                            * 4.0F;
-               dot = CLAMP(dot, 0.0, CHAN_MAXF) * RGBmult;
+                            * 4.0F * RGBmult;
+               dot = CLAMP(dot, 0.0, CHAN_MAXF);
 #else
                GLint dot = (S_PROD((GLint)arg0[i][RCOMP] - half,
                                   (GLint)arg1[i][RCOMP] - half) +
@@ -3091,7 +3148,8 @@ texture_combine( const GLcontext *ctx, GLuint unit, GLuint n,
                                   (GLint)arg1[i][GCOMP] - half) +
                            S_PROD((GLint)arg0[i][BCOMP] - half,
                                   (GLint)arg1[i][BCOMP] - half)) >> 6;
-               dot = CLAMP(dot, 0, CHAN_MAX) << RGBshift;
+               dot <<= RGBshift;
+               dot = CLAMP(dot, 0, CHAN_MAX);
 #endif
                rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLchan) dot;
             }