added missing \'s
[mesa.git] / src / mesa / swrast / s_texture.c
index 220d3a43660411d1530c2b6a9be72d6d561c53fc..1520fd079d0c336668ca52ab8e7c8d5b0ae7e49f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: s_texture.c,v 1.20 2001/03/26 19:42:40 brianp Exp $ */
+/* $Id: s_texture.c,v 1.32 2001/06/01 13:23:27 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
@@ -31,6 +31,7 @@
 #include "macros.h"
 #include "mmath.h"
 #include "mem.h"
+#include "texformat.h"
 #include "teximage.h"
 
 #include "s_context.h"
 #include "s_texture.h"
 
 
-/* XXX this is temporary, until GL/glext.h is updated. */
-#ifndef GL_DOT3_RGB_ARB
-#define GL_DOT3_RGB_ARB 0x86AE
-#endif
-#ifndef GL_DOT3_RGBA_ARB
-#define GL_DOT3_RGBA_ARB 0x86AF
-#endif
-
-/* XXX this is temporary, until GL/glext.h is updated. */
-#ifndef GL_CLAMP_TO_BORDER_ARB
-#define GL_CLAMP_TO_BORDER_ARB 0x812D
-#endif
-
-
 /*
  * These values are used in the fixed-point arithmetic used
  * for linear filtering.
 
 
 
+/*
+ * Note, the FRAC macro has to work perfectly.  Otherwise you'll sometimes
+ * see 1-pixel bands of improperly weighted linear-sampled texels.  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.
@@ -284,9 +281,15 @@ sample_1d_nearest(GLcontext *ctx,
    /* skip over the border, if any */
    i += img->Border;
 
-   (*img->FetchTexel)(img, i, 0, 0, (GLvoid *) rgba);
-   if (img->Format == GL_COLOR_INDEX) {
-      palette_sample(ctx, tObj, rgba[0], rgba);
+   if (i < 0 || i >= (GLint) img->Width) {
+      /* Need this test for GL_CLAMP_TO_BORDER_ARB mode */
+      COPY_CHAN4(rgba, tObj->BorderColor);
+   }
+   else {
+      (*img->FetchTexel)(img, i, 0, 0, (GLvoid *) rgba);
+      if (img->Format == GL_COLOR_INDEX) {
+         palette_sample(ctx, tObj, rgba[0], rgba);
+      }
    }
 }
 
@@ -320,9 +323,15 @@ sample_1d_linear(GLcontext *ctx,
 
    {
       const GLfloat a = FRAC(u);
+
+#if CHAN_BITS == 32
+      const GLfloat w0 = (1.0F-a);
+      const GLfloat w1 =       a ;
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       /* compute sample weights in fixed point in [0,WEIGHT_SCALE] */
-      const GLint w0 = IROUND((1.0F-a) * WEIGHT_SCALE);
-      const GLint w1 = IROUND(      a  * WEIGHT_SCALE);
+      const GLint w0 = IROUND_POS((1.0F - a) * WEIGHT_SCALE);
+      const GLint w1 = IROUND_POS(        a  * WEIGHT_SCALE);
+#endif
 
       GLchan t0[4], t1[4];  /* texels */
 
@@ -345,10 +354,18 @@ sample_1d_linear(GLcontext *ctx,
          }
       }
 
+#if CHAN_BITS == 32
+      rgba[0] = w0 * t0[0] + w1 * t1[0];
+      rgba[1] = w0 * t0[1] + w1 * t1[1];
+      rgba[2] = w0 * t0[2] + w1 * t1[2];
+      rgba[3] = w0 * t0[3] + w1 * t1[3];
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       rgba[0] = (GLchan) ((w0 * t0[0] + w1 * t1[0]) >> WEIGHT_SHIFT);
       rgba[1] = (GLchan) ((w0 * t0[1] + w1 * t1[1]) >> WEIGHT_SHIFT);
       rgba[2] = (GLchan) ((w0 * t0[2] + w1 * t1[2]) >> WEIGHT_SHIFT);
       rgba[3] = (GLchan) ((w0 * t0[3] + w1 * t1[3]) >> WEIGHT_SHIFT);
+#endif
+
    }
 }
 
@@ -568,9 +585,15 @@ sample_2d_nearest(GLcontext *ctx,
    i += img->Border;
    j += img->Border;
 
-   (*img->FetchTexel)(img, i, j, 0, (GLvoid *) rgba);
-   if (img->Format == GL_COLOR_INDEX) {
-      palette_sample(ctx, tObj, rgba[0], rgba);
+   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);
+   }
+   else {
+      (*img->FetchTexel)(img, i, j, 0, (GLvoid *) rgba);
+      if (img->Format == GL_COLOR_INDEX) {
+         palette_sample(ctx, tObj, rgba[0], rgba);
+      }
    }
 }
 
@@ -613,11 +636,20 @@ sample_2d_linear(GLcontext *ctx,
    {
       const GLfloat a = FRAC(u);
       const GLfloat b = FRAC(v);
+
+#if CHAN_BITS == 32
+      const GLfloat w00 = (1.0F-a) * (1.0F-b);
+      const GLfloat w10 =       a  * (1.0F-b);
+      const GLfloat w01 = (1.0F-a) *       b ;
+      const GLfloat w11 =       a  *       b ;
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       /* compute sample weights in fixed point in [0,WEIGHT_SCALE] */
-      const GLint w00 = IROUND((1.0F-a) * (1.0F-b) * WEIGHT_SCALE);
-      const GLint w10 = IROUND(      a  * (1.0F-b) * WEIGHT_SCALE);
-      const GLint w01 = IROUND((1.0F-a) *       b  * WEIGHT_SCALE);
-      const GLint w11 = IROUND(      a  *       b  * WEIGHT_SCALE);
+      const GLint w00 = IROUND_POS((1.0F-a) * (1.0F-b) * WEIGHT_SCALE);
+      const GLint w10 = IROUND_POS(      a  * (1.0F-b) * WEIGHT_SCALE);
+      const GLint w01 = IROUND_POS((1.0F-a) *       b  * WEIGHT_SCALE);
+      const GLint w11 = IROUND_POS(      a  *       b  * WEIGHT_SCALE);
+#endif
+
       GLchan t00[4];
       GLchan t10[4];
       GLchan t01[4];
@@ -659,11 +691,18 @@ sample_2d_linear(GLcontext *ctx,
             palette_sample(ctx, tObj, t11[0], t11);
          }
       }
-
+#if CHAN_BITS == 32
+      rgba[0] = w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0];
+      rgba[1] = w00 * t00[1] + w10 * t10[1] + w01 * t01[1] + w11 * t11[1];
+      rgba[2] = w00 * t00[2] + w10 * t10[2] + w01 * t01[2] + w11 * t11[2];
+      rgba[3] = w00 * t00[3] + w10 * t10[3] + w01 * t01[3] + w11 * t11[3];
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       rgba[0] = (GLchan) ((w00 * t00[0] + w10 * t10[0] + w01 * t01[0] + w11 * t11[0]) >> WEIGHT_SHIFT);
       rgba[1] = (GLchan) ((w00 * t00[1] + w10 * t10[1] + w01 * t01[1] + w11 * t11[1]) >> WEIGHT_SHIFT);
       rgba[2] = (GLchan) ((w00 * t00[2] + w10 * t10[2] + w01 * t01[2] + w11 * t11[2]) >> WEIGHT_SHIFT);
       rgba[3] = (GLchan) ((w00 * t00[3] + w10 * t10[3] + w01 * t01[3] + w11 * t11[3]) >> WEIGHT_SHIFT);
+#endif
+
    }
 
 }
@@ -982,9 +1021,17 @@ sample_3d_nearest(GLcontext *ctx,
    COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapT, t, height, j);
    COMPUTE_NEAREST_TEXEL_LOCATION(tObj->WrapR, r, depth,  k);
 
-   (*img->FetchTexel)(img, i, j, k, (GLvoid *) rgba);
-   if (img->Format == GL_COLOR_INDEX) {
-      palette_sample(ctx, tObj, rgba[0], rgba);
+   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_ARB mode */
+      COPY_CHAN4(rgba, tObj->BorderColor);
+   }
+   else {
+      (*img->FetchTexel)(img, i, j, k, (GLvoid *) rgba);
+      if (img->Format == GL_COLOR_INDEX) {
+         palette_sample(ctx, tObj, rgba[0], rgba);
+      }
    }
 }
 
@@ -1034,15 +1081,28 @@ sample_3d_linear(GLcontext *ctx,
       const GLfloat a = FRAC(u);
       const GLfloat b = FRAC(v);
       const GLfloat c = FRAC(w);
+
+#if CHAN_BITS == 32
+      /* compute sample weights in fixed point in [0,WEIGHT_SCALE] */
+      GLfloat w000 = (1.0F-a) * (1.0F-b) * (1.0F-c);
+      GLfloat w100 =       a  * (1.0F-b) * (1.0F-c);
+      GLfloat w010 = (1.0F-a) *       b  * (1.0F-c);
+      GLfloat w110 =       a  *       b  * (1.0F-c);
+      GLfloat w001 = (1.0F-a) * (1.0F-b) *       c ;
+      GLfloat w101 =       a  * (1.0F-b) *       c ;
+      GLfloat w011 = (1.0F-a) *       b  *       c ;
+      GLfloat w111 =       a  *       b  *       c ;
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       /* compute sample weights in fixed point in [0,WEIGHT_SCALE] */
-      GLint w000 = IROUND((1.0F-a) * (1.0F-b) * (1.0F-c) * WEIGHT_SCALE);
-      GLint w100 = IROUND(      a  * (1.0F-b) * (1.0F-c) * WEIGHT_SCALE);
-      GLint w010 = IROUND((1.0F-a) *       b  * (1.0F-c) * WEIGHT_SCALE);
-      GLint w110 = IROUND(      a  *       b  * (1.0F-c) * WEIGHT_SCALE);
-      GLint w001 = IROUND((1.0F-a) * (1.0F-b) *       c  * WEIGHT_SCALE);
-      GLint w101 = IROUND(      a  * (1.0F-b) *       c  * WEIGHT_SCALE);
-      GLint w011 = IROUND((1.0F-a) *       b  *       c  * WEIGHT_SCALE);
-      GLint w111 = IROUND(      a  *       b  *       c  * WEIGHT_SCALE);
+      GLint w000 = IROUND_POS((1.0F-a) * (1.0F-b) * (1.0F-c) * WEIGHT_SCALE);
+      GLint w100 = IROUND_POS(      a  * (1.0F-b) * (1.0F-c) * WEIGHT_SCALE);
+      GLint w010 = IROUND_POS((1.0F-a) *       b  * (1.0F-c) * WEIGHT_SCALE);
+      GLint w110 = IROUND_POS(      a  *       b  * (1.0F-c) * WEIGHT_SCALE);
+      GLint w001 = IROUND_POS((1.0F-a) * (1.0F-b) *       c  * WEIGHT_SCALE);
+      GLint w101 = IROUND_POS(      a  * (1.0F-b) *       c  * WEIGHT_SCALE);
+      GLint w011 = IROUND_POS((1.0F-a) *       b  *       c  * WEIGHT_SCALE);
+      GLint w111 = IROUND_POS(      a  *       b  *       c  * WEIGHT_SCALE);
+#endif
 
       GLchan t000[4], t010[4], t001[4], t011[4];
       GLchan t100[4], t110[4], t101[4], t111[4];
@@ -1121,9 +1181,19 @@ sample_3d_linear(GLcontext *ctx,
          }
       }
 
+#if CHAN_BITS == 32
+      rgba[0] = w000*t000[0] + w010*t010[0] + w001*t001[0] + w011*t011[0] +
+                w100*t100[0] + w110*t110[0] + w101*t101[0] + w111*t111[0];
+      rgba[1] = w000*t000[1] + w010*t010[1] + w001*t001[1] + w011*t011[1] +
+                w100*t100[1] + w110*t110[1] + w101*t101[1] + w111*t111[1];
+      rgba[2] = w000*t000[2] + w010*t010[2] + w001*t001[2] + w011*t011[2] +
+                w100*t100[2] + w110*t110[2] + w101*t101[2] + w111*t111[2];
+      rgba[3] = w000*t000[3] + w010*t010[3] + w001*t001[3] + w011*t011[3] +
+                w100*t100[3] + w110*t110[3] + w101*t101[3] + w111*t111[3];
+#else /* CHAN_BITS == 8 || CHAN_BITS == 16 */
       rgba[0] = (GLchan) (
                  (w000*t000[0] + w010*t010[0] + w001*t001[0] + w011*t011[0] +
-                  w100*t100[0] + w110*t110[0] + w101*t101[0] + w111*t111[0]  )
+                  w100*t100[0] + w110*t110[0] + w101*t101[0] + w111*t111[0] )
                  >> WEIGHT_SHIFT);
       rgba[1] = (GLchan) (
                  (w000*t000[1] + w010*t010[1] + w001*t001[1] + w011*t011[1] +
@@ -1137,6 +1207,8 @@ sample_3d_linear(GLcontext *ctx,
                  (w000*t000[3] + w010*t010[3] + w001*t001[3] + w011*t011[3] +
                   w100*t100[3] + w110*t110[3] + w101*t101[3] + w111*t111[3] )
                  >> WEIGHT_SHIFT);
+#endif
+
    }
 }
 
@@ -1671,15 +1743,13 @@ _swrast_choose_texture_sample_func( GLcontext *ctx, GLuint texUnit,
                if (t->WrapS == GL_REPEAT &&
                    t->WrapT == GL_REPEAT &&
                    t->Image[baseLevel]->Border == 0 &&
-                   t->Image[baseLevel]->Format == GL_RGB &&
-                   t->Image[baseLevel]->Type == CHAN_TYPE) {
+                   t->Image[baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGB) {
                   swrast->TextureSample[texUnit] = opt_sample_rgb_2d;
                }
                else if (t->WrapS == GL_REPEAT &&
                         t->WrapT == GL_REPEAT &&
                         t->Image[baseLevel]->Border == 0 &&
-                        t->Image[baseLevel]->Format==GL_RGBA &&
-                        t->Image[baseLevel]->Type == CHAN_TYPE) {
+                        t->Image[baseLevel]->TexFormat->MesaFormat == MESA_FORMAT_RGBA) {
                   swrast->TextureSample[texUnit] = opt_sample_rgba_2d;
                }
                else
@@ -1728,14 +1798,16 @@ texture_combine(const GLcontext *ctx,
                 CONST GLchan (*texel)[4],
                 GLchan (*rgba)[4])
 {
-   GLchan ccolor [3][3*MAX_WIDTH][4];
    const GLchan (*argRGB [3])[4];
    const GLchan (*argA [3])[4];
    GLuint i, j;
    const GLuint RGBshift = textureUnit->CombineScaleShiftRGB;
    const GLuint Ashift   = textureUnit->CombineScaleShiftA;
+   DEFMNARRAY(GLchan, ccolor, 3, 3 * MAX_WIDTH, 4);  /* mac 32k limitation */
+   CHECKARRAY(ccolor, return);  /* mac 32k limitation */
 
-   ASSERT(ctx->Extensions.EXT_texture_env_combine);
+   ASSERT(ctx->Extensions.EXT_texture_env_combine ||
+          ctx->Extensions.ARB_texture_env_combine);
 
    /*
     * Do operand setup for up to 3 operands.  Loop over the terms.
@@ -1777,14 +1849,16 @@ texture_combine(const GLcontext *ctx,
          case GL_CONSTANT_EXT:
             {
                GLchan (*c)[4] = ccolor[j];
-               GLchan red, green, blue;
+               GLchan red, green, blue, alpha;
                UNCLAMPED_FLOAT_TO_CHAN(red,   textureUnit->EnvColor[0]);
                UNCLAMPED_FLOAT_TO_CHAN(green, textureUnit->EnvColor[1]);
                UNCLAMPED_FLOAT_TO_CHAN(blue,  textureUnit->EnvColor[2]);
+               UNCLAMPED_FLOAT_TO_CHAN(alpha, textureUnit->EnvColor[3]);
                for (i = 0; i < n; i++) {
                   c[i][RCOMP] = red;
                   c[i][GCOMP] = green;
                   c[i][BCOMP] = blue;
+                  c[i][ACOMP] = alpha;
                }
                argRGB[j] = (const GLchan (*)[4]) ccolor[j];
             }
@@ -1797,6 +1871,7 @@ texture_combine(const GLcontext *ctx,
          const GLchan (*src)[4] = argRGB[j];
          GLchan (*dst)[4] = ccolor[j];
 
+         /* point to new arg[j] storage */
          argRGB[j] = (const GLchan (*)[4]) ccolor[j];
 
          if (textureUnit->CombineOperandRGB[j] == GL_ONE_MINUS_SRC_COLOR) {
@@ -1807,7 +1882,6 @@ texture_combine(const GLcontext *ctx,
             }
          }
          else if (textureUnit->CombineOperandRGB[j] == GL_SRC_ALPHA) {
-            src = (const GLchan (*)[4]) argA[j];
             for (i = 0; i < n; i++) {
                dst[i][RCOMP] = src[i][ACOMP];
                dst[i][GCOMP] = src[i][ACOMP];
@@ -1816,7 +1890,6 @@ texture_combine(const GLcontext *ctx,
          }
          else {
             ASSERT(textureUnit->CombineOperandRGB[j] ==GL_ONE_MINUS_SRC_ALPHA);
-            src = (const GLchan (*)[4]) argA[j];
             for (i = 0; i < n; i++) {
                dst[i][RCOMP] = CHAN_MAX - src[i][ACOMP];
                dst[i][GCOMP] = CHAN_MAX - src[i][ACOMP];
@@ -1940,13 +2013,27 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
+      case GL_SUBTRACT_ARB:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
+            for (i = 0; i < n; i++) {
+               GLint r = ((GLint) arg0[i][RCOMP] - (GLint) arg1[i][RCOMP]) << RGBshift;
+               GLint g = ((GLint) arg0[i][GCOMP] - (GLint) arg1[i][GCOMP]) << RGBshift;
+               GLint b = ((GLint) arg0[i][BCOMP] - (GLint) arg1[i][BCOMP]) << RGBshift;
+               rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
+               rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
+               rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
+            }
+         }
+         break;
       case GL_DOT3_RGB_EXT:
       case GL_DOT3_RGBA_EXT:
       case GL_DOT3_RGB_ARB:
       case GL_DOT3_RGBA_ARB:
          {
-            const GLubyte (*arg0)[4] = (const GLubyte (*)[4]) argRGB[0];
-            const GLubyte (*arg1)[4] = (const GLubyte (*)[4]) argRGB[1];
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
            /* ATI's EXT extension has a constant scale by 4.  The ARB
             * one will likely remove this restriction, and we should
             * drop the EXT extension in favour of the ARB one.
@@ -1958,9 +2045,8 @@ texture_combine(const GLcontext *ctx,
                                   (GLint)arg1[i][GCOMP] - 128) +
                            S_PROD((GLint)arg0[i][BCOMP] - 128,
                                   (GLint)arg1[i][BCOMP] - 128)) >> 6;
-               rgba[i][RCOMP] = (GLubyte) CLAMP(dot, 0, 255);
-               rgba[i][GCOMP] = (GLubyte) CLAMP(dot, 0, 255);
-               rgba[i][BCOMP] = (GLubyte) CLAMP(dot, 0, 255);
+               dot = CLAMP(dot, 0, 255);
+               rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = (GLchan) dot;
             }
          }
          break;
@@ -2031,6 +2117,17 @@ texture_combine(const GLcontext *ctx,
             }
          }
          break;
+      case GL_SUBTRACT_ARB:
+         {
+            const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0];
+            const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];
+            for (i = 0; i < n; i++) {
+               GLint a = ((GLint) arg0[i][ACOMP] - (GLint) arg1[i][ACOMP]) << RGBshift;
+               rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
+            }
+         }
+         break;
+
       default:
          _mesa_problem(NULL, "invalid combine mode");
    }
@@ -2043,6 +2140,7 @@ texture_combine(const GLcontext *ctx,
         rgba[i][ACOMP] = rgba[i][RCOMP];
       }
    }
+   UNDEFARRAY(ccolor);  /* mac 32k limitation */
 }
 #undef PROD
 
@@ -2532,8 +2630,8 @@ sample_depth_texture(const GLcontext *ctx,
 
          if (0) {
             /* compute a single weighted depth sample and do one comparison */
-            const GLfloat a = FRAC(u);
-            const GLfloat b = FRAC(v);
+            const GLfloat a = FRAC(u + 1.0F);
+            const GLfloat b = FRAC(v + 1.0F);
             const GLfloat w00 = (1.0F - a) * (1.0F - b);
             const GLfloat w10 = (       a) * (1.0F - b);
             const GLfloat w01 = (1.0F - a) * (       b);