glsl: Handle continuation characters in preprocessor.
[mesa.git] / src / mesa / swrast / s_triangle.c
index 4533980455ef793a5894fcc5c8b7daaa22aa7abd..9260e35066fc57409bcd4b5cd133c01891ab5671 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.3
+ * Version:  7.3
  *
- * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  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"),
  * functions to draw triangles.
  */
 
-#include "glheader.h"
-#include "context.h"
-#include "colormac.h"
-#include "imports.h"
-#include "macros.h"
-#include "texformat.h"
-#include "teximage.h"
-#include "texstate.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/colormac.h"
+#include "main/imports.h"
+#include "main/macros.h"
+#include "main/texformat.h"
+#include "shader/prog_instruction.h"
 
 #include "s_aatriangle.h"
 #include "s_context.h"
-#include "s_depth.h"
 #include "s_feedback.h"
 #include "s_span.h"
 #include "s_triangle.h"
@@ -55,13 +53,13 @@ _swrast_culltriangle( GLcontext *ctx,
                       const SWvertex *v1,
                       const SWvertex *v2 )
 {
-   GLfloat ex = v1->win[0] - v0->win[0];
-   GLfloat ey = v1->win[1] - v0->win[1];
-   GLfloat fx = v2->win[0] - v0->win[0];
-   GLfloat fy = v2->win[1] - v0->win[1];
+   GLfloat ex = v1->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
+   GLfloat ey = v1->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
+   GLfloat fx = v2->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
+   GLfloat fy = v2->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
    GLfloat c = ex*fy-ey*fx;
 
-   if (c * SWRAST_CONTEXT(ctx)->_BackfaceSign > 0)
+   if (c * SWRAST_CONTEXT(ctx)->_BackfaceCullSign > 0)
       return 0;
 
    return 1;
@@ -70,26 +68,11 @@ _swrast_culltriangle( GLcontext *ctx,
 
 
 /*
- * Render a flat-shaded color index triangle.
+ * Render a smooth or flat-shaded color index triangle.
  */
-#define NAME flat_ci_triangle
+#define NAME ci_triangle
 #define INTERP_Z 1
-#define INTERP_FOG 1
-#define SETUP_CODE                     \
-   span.interpMask |= SPAN_INDEX;      \
-   span.index = FloatToFixed(v2->index);\
-   span.indexStep = 0;
-#define RENDER_SPAN( span )  _swrast_write_index_span(ctx, &span);
-#include "s_tritemp.h"
-
-
-
-/*
- * Render a smooth-shaded color index triangle.
- */
-#define NAME smooth_ci_triangle
-#define INTERP_Z 1
-#define INTERP_FOG 1
+#define INTERP_ATTRIBS 1  /* just for fog */
 #define INTERP_INDEX 1
 #define RENDER_SPAN( span )  _swrast_write_index_span(ctx, &span);
 #include "s_tritemp.h"
@@ -101,7 +84,6 @@ _swrast_culltriangle( GLcontext *ctx,
  */
 #define NAME flat_rgba_triangle
 #define INTERP_Z 1
-#define INTERP_FOG 1
 #define SETUP_CODE                             \
    ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
    ASSERT(ctx->Light.ShadeModel==GL_FLAT);     \
@@ -124,7 +106,6 @@ _swrast_culltriangle( GLcontext *ctx,
  */
 #define NAME smooth_rgba_triangle
 #define INTERP_Z 1
-#define INTERP_FOG 1
 #define INTERP_RGB 1
 #define INTERP_ALPHA 1
 #define SETUP_CODE                             \
@@ -142,7 +123,7 @@ _swrast_culltriangle( GLcontext *ctx,
  * Render an RGB, GL_DECAL, textured triangle.
  * Interpolate S,T only w/out mipmapping or perspective correction.
  *
- * No fog.
+ * No fog.  No depth testing.
  */
 #define NAME simple_textured_triangle
 #define INTERP_INT_TEX 1
@@ -150,8 +131,9 @@ _swrast_culltriangle( GLcontext *ctx,
 #define T_SCALE theight
 
 #define SETUP_CODE                                                     \
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);                             \
-   struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D;     \
+   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
+   struct gl_texture_object *obj =                                     \
+      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];               \
    const GLint b = obj->BaseLevel;                                     \
    const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width;           \
    const GLfloat theight = (GLfloat) obj->Image[0][b]->Height;         \
@@ -159,13 +141,13 @@ _swrast_culltriangle( GLcontext *ctx,
    const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data;    \
    const GLint smask = obj->Image[0][b]->Width - 1;                    \
    const GLint tmask = obj->Image[0][b]->Height - 1;                   \
-   if (!texture) {                                                     \
-      /* this shouldn't happen */                                      \
+   if (!rb || !texture) {                                              \
       return;                                                          \
    }
 
-#define RENDER_SPAN( span  )                                           \
+#define RENDER_SPAN( span                                            \
    GLuint i;                                                           \
+   GLchan rgb[MAX_WIDTH][3];                                           \
    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */               \
    span.intTex[1] -= FIXED_HALF;                                       \
    for (i = 0; i < span.end; i++) {                                    \
@@ -173,15 +155,14 @@ _swrast_culltriangle( GLcontext *ctx,
       GLint t = FixedToInt(span.intTex[1]) & tmask;                    \
       GLint pos = (t << twidth_log2) + s;                              \
       pos = pos + pos + pos;  /* multiply by 3 */                      \
-      span.array->rgb[i][RCOMP] = texture[pos];                                \
-      span.array->rgb[i][GCOMP] = texture[pos+1];                      \
-      span.array->rgb[i][BCOMP] = texture[pos+2];                      \
+      rgb[i][RCOMP] = texture[pos];                                    \
+      rgb[i][GCOMP] = texture[pos+1];                                  \
+      rgb[i][BCOMP] = texture[pos+2];                                  \
       span.intTex[0] += span.intTexStep[0];                            \
       span.intTex[1] += span.intTexStep[1];                            \
    }                                                                   \
-   (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y,       \
-                                  (CONST GLchan (*)[3]) span.array->rgb,\
-                                  NULL );
+   rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
+
 #include "s_tritemp.h"
 
 
@@ -202,8 +183,9 @@ _swrast_culltriangle( GLcontext *ctx,
 #define T_SCALE theight
 
 #define SETUP_CODE                                                     \
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);                             \
-   struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D;     \
+   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
+   struct gl_texture_object *obj =                                     \
+      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];               \
    const GLint b = obj->BaseLevel;                                     \
    const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width;           \
    const GLfloat theight = (GLfloat) obj->Image[0][b]->Height;         \
@@ -211,25 +193,25 @@ _swrast_culltriangle( GLcontext *ctx,
    const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data;    \
    const GLint smask = obj->Image[0][b]->Width - 1;                    \
    const GLint tmask = obj->Image[0][b]->Height - 1;                   \
-   if (!texture) {                                                     \
-      /* this shouldn't happen */                                      \
+   if (!rb || !texture) {                                              \
       return;                                                          \
    }
 
 #define RENDER_SPAN( span )                                            \
    GLuint i;                                                           \
+   GLchan rgb[MAX_WIDTH][3];                                           \
    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */               \
    span.intTex[1] -= FIXED_HALF;                                       \
    for (i = 0; i < span.end; i++) {                                    \
-      const GLdepth z = FixedToDepth(span.z);                          \
+      const GLuint z = FixedToDepth(span.z);                           \
       if (z < zRow[i]) {                                               \
          GLint s = FixedToInt(span.intTex[0]) & smask;                 \
          GLint t = FixedToInt(span.intTex[1]) & tmask;                 \
          GLint pos = (t << twidth_log2) + s;                           \
          pos = pos + pos + pos;  /* multiply by 3 */                   \
-         span.array->rgb[i][RCOMP] = texture[pos];                     \
-         span.array->rgb[i][GCOMP] = texture[pos+1];                   \
-         span.array->rgb[i][BCOMP] = texture[pos+2];                   \
+         rgb[i][RCOMP] = texture[pos];                                 \
+         rgb[i][GCOMP] = texture[pos+1];                               \
+         rgb[i][BCOMP] = texture[pos+2];                               \
          zRow[i] = z;                                                  \
          span.array->mask[i] = 1;                                      \
       }                                                                        \
@@ -240,11 +222,9 @@ _swrast_culltriangle( GLcontext *ctx,
       span.intTex[1] += span.intTexStep[1];                            \
       span.z += span.zStep;                                            \
    }                                                                   \
-   (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y,       \
-                                  (CONST GLchan (*)[3]) span.array->rgb,\
-                                  span.array->mask );
-#include "s_tritemp.h"
+   rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
 
+#include "s_tritemp.h"
 
 
 #if CHAN_TYPE != GL_FLOAT
@@ -262,15 +242,31 @@ struct affine_info
 };
 
 
+static INLINE GLint
+ilerp(GLint t, GLint a, GLint b)
+{
+   return a + ((t * (b - a)) >> FIXED_SHIFT);
+}
+
+static INLINE GLint
+ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
+{
+   const GLint temp0 = ilerp(ia, v00, v10);
+   const GLint temp1 = ilerp(ia, v01, v11);
+   return ilerp(ib, temp0, temp1);
+}
+
+
 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
  * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
  * texture env modes.
  */
 static INLINE void
-affine_span(GLcontext *ctx, struct sw_span *span,
+affine_span(GLcontext *ctx, SWspan *span,
             struct affine_info *info)
 {
    GLchan sample[4];  /* the filtered texture sample */
+   const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
 
    /* Instead of defining a function for each mode, a test is done
     * between the outer and inner loops. This is to reduce code size
@@ -285,25 +281,18 @@ affine_span(GLcontext *ctx, struct sw_span *span,
    sample[ACOMP] = CHAN_MAX
 
 #define LINEAR_RGB                                                     \
-   sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) +             \
-             tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
-   sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) +             \
-             tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
-   sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) +             \
-             tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
-   sample[ACOMP] = CHAN_MAX
+   sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
+   sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
+   sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
+   sample[ACOMP] = CHAN_MAX;
 
 #define NEAREST_RGBA  COPY_CHAN4(sample, tex00)
 
 #define LINEAR_RGBA                                                    \
-   sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) +             \
-               tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
-   sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) +             \
-               tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
-   sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) +             \
-               tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
-   sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) +             \
-               tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
+   sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
+   sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
+   sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
+   sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
 
 #define MODULATE                                                         \
    dest[RCOMP] = span->red   * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
@@ -356,13 +345,13 @@ affine_span(GLcontext *ctx, struct sw_span *span,
 
 #define NEAREST_RGBA_REPLACE  COPY_CHAN4(dest, tex00)
 
-#define SPAN_NEAREST(DO_TEX,COMP)                                      \
+#define SPAN_NEAREST(DO_TEX, COMPS)                                    \
        for (i = 0; i < span->end; i++) {                               \
            /* Isn't it necessary to use FixedFloor below?? */          \
            GLint s = FixedToInt(span->intTex[0]) & info->smask;                \
            GLint t = FixedToInt(span->intTex[1]) & info->tmask;                \
            GLint pos = (t << info->twidth_log2) + s;                   \
-           const GLchan *tex00 = info->texture + COMP * pos;           \
+           const GLchan *tex00 = info->texture + COMPS * pos;          \
            DO_TEX;                                                     \
            span->red += span->redStep;                                 \
           span->green += span->greenStep;                              \
@@ -373,22 +362,18 @@ affine_span(GLcontext *ctx, struct sw_span *span,
            dest += 4;                                                  \
        }
 
-#define SPAN_LINEAR(DO_TEX,COMP)                                       \
+#define SPAN_LINEAR(DO_TEX, COMPS)                                     \
        for (i = 0; i < span->end; i++) {                               \
            /* Isn't it necessary to use FixedFloor below?? */          \
-           GLint s = FixedToInt(span->intTex[0]) & info->smask;                \
-           GLint t = FixedToInt(span->intTex[1]) & info->tmask;                \
-           GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK;             \
-           GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK;             \
-           GLfixed si = FIXED_FRAC_MASK - sf;                          \
-           GLfixed ti = FIXED_FRAC_MASK - tf;                          \
-           GLint pos = (t << info->twidth_log2) + s;                   \
-           const GLchan *tex00 = info->texture + COMP * pos;           \
+           const GLint s = FixedToInt(span->intTex[0]) & info->smask;  \
+           const GLint t = FixedToInt(span->intTex[1]) & info->tmask;  \
+           const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK;       \
+           const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK;       \
+           const GLint pos = (t << info->twidth_log2) + s;             \
+           const GLchan *tex00 = info->texture + COMPS * pos;          \
            const GLchan *tex10 = tex00 + info->tbytesline;             \
-           const GLchan *tex01 = tex00 + COMP;                         \
-           const GLchan *tex11 = tex10 + COMP;                         \
-           (void) ti;                                                  \
-           (void) si;                                                  \
+           const GLchan *tex01 = tex00 + COMPS;                                \
+           const GLchan *tex11 = tex10 + COMPS;                                \
            if (t == info->tmask) {                                     \
               tex10 -= info->tsize;                                    \
               tex11 -= info->tsize;                                    \
@@ -411,6 +396,9 @@ affine_span(GLcontext *ctx, struct sw_span *span,
    GLuint i;
    GLchan *dest = span->array->rgba[0];
 
+   /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
+   ctx->Texture._EnabledCoordUnits = 0x0;
+
    span->intTex[0] -= FIXED_HALF;
    span->intTex[1] -= FIXED_HALF;
    switch (info->filter) {
@@ -512,8 +500,12 @@ affine_span(GLcontext *ctx, struct sw_span *span,
    }
    span->interpMask &= ~SPAN_RGBA;
    ASSERT(span->arrayMask & SPAN_RGBA);
+
    _swrast_write_rgba_span(ctx, span);
 
+   /* re-enable texture units */
+   ctx->Texture._EnabledCoordUnits = texEnableSave;
+
 #undef SPAN_NEAREST
 #undef SPAN_LINEAR
 }
@@ -525,7 +517,6 @@ affine_span(GLcontext *ctx, struct sw_span *span,
  */
 #define NAME affine_textured_triangle
 #define INTERP_Z 1
-#define INTERP_FOG 1
 #define INTERP_RGB 1
 #define INTERP_ALPHA 1
 #define INTERP_INT_TEX 1
@@ -535,7 +526,8 @@ affine_span(GLcontext *ctx, struct sw_span *span,
 #define SETUP_CODE                                                     \
    struct affine_info info;                                            \
    struct gl_texture_unit *unit = ctx->Texture.Unit+0;                 \
-   struct gl_texture_object *obj = unit->Current2D;                    \
+   struct gl_texture_object *obj =                                     \
+      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];               \
    const GLint b = obj->BaseLevel;                                     \
    const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width;           \
    const GLfloat theight = (GLfloat) obj->Image[0][b]->Height;         \
@@ -543,7 +535,7 @@ affine_span(GLcontext *ctx, struct sw_span *span,
    info.twidth_log2 = obj->Image[0][b]->WidthLog2;                     \
    info.smask = obj->Image[0][b]->Width - 1;                           \
    info.tmask = obj->Image[0][b]->Height - 1;                          \
-   info.format = obj->Image[0][b]->Format;                             \
+   info.format = obj->Image[0][b]->_BaseFormat;                                \
    info.filter = obj->MinFilter;                                       \
    info.envmode = unit->EnvMode;                                       \
    span.arrayMask |= SPAN_RGBA;                                                \
@@ -601,7 +593,7 @@ struct persp_info
 
 
 static INLINE void
-fast_persp_span(GLcontext *ctx, struct sw_span *span,
+fast_persp_span(GLcontext *ctx, SWspan *span,
                struct persp_info *info)
 {
    GLchan sample[4];  /* the filtered texture sample */
@@ -636,23 +628,19 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
        for (i = 0; i < span->end; i++) {                               \
            GLdouble invQ = tex_coord[2] ?                              \
                                  (1.0 / tex_coord[2]) : 1.0;            \
-           GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);            \
-           GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);            \
-           GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF;           \
-           GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF;           \
-           GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask;      \
-           GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask;      \
-           GLfixed sf = s_fix & FIXED_FRAC_MASK;                       \
-           GLfixed tf = t_fix & FIXED_FRAC_MASK;                       \
-           GLfixed si = FIXED_FRAC_MASK - sf;                          \
-           GLfixed ti = FIXED_FRAC_MASK - tf;                          \
-           GLint pos = (t << info->twidth_log2) + s;                   \
+           const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);      \
+           const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);      \
+           const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF;     \
+           const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF;      \
+           const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask;        \
+           const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask;        \
+           const GLfixed sf = s_fix & FIXED_FRAC_MASK;                 \
+           const GLfixed tf = t_fix & FIXED_FRAC_MASK;                 \
+           const GLint pos = (t << info->twidth_log2) + s;             \
            const GLchan *tex00 = info->texture + COMP * pos;           \
            const GLchan *tex10 = tex00 + info->tbytesline;             \
            const GLchan *tex01 = tex00 + COMP;                         \
            const GLchan *tex11 = tex10 + COMP;                         \
-           (void) ti;                                                  \
-           (void) si;                                                  \
            if (t == info->tmask) {                                     \
               tex10 -= info->tsize;                                    \
               tex11 -= info->tsize;                                    \
@@ -676,16 +664,16 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
    GLfloat tex_coord[3], tex_step[3];
    GLchan *dest = span->array->rgba[0];
 
-   const GLuint savedTexEnable = ctx->Texture._EnabledUnits;
-   ctx->Texture._EnabledUnits = 0;
+   const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
+   ctx->Texture._EnabledCoordUnits = 0;
 
-   tex_coord[0] = span->tex[0][0]  * (info->smask + 1);
-   tex_step[0] = span->texStepX[0][0] * (info->smask + 1);
-   tex_coord[1] = span->tex[0][1] * (info->tmask + 1);
-   tex_step[1] = span->texStepX[0][1] * (info->tmask + 1);
-   /* span->tex[0][2] only if 3D-texturing, here only 2D */
-   tex_coord[2] = span->tex[0][3];
-   tex_step[2] = span->texStepX[0][3];
+   tex_coord[0] = span->attrStart[FRAG_ATTRIB_TEX0][0]  * (info->smask + 1);
+   tex_step[0] = span->attrStepX[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
+   tex_coord[1] = span->attrStart[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
+   tex_step[1] = span->attrStepX[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
+   /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
+   tex_coord[2] = span->attrStart[FRAG_ATTRIB_TEX0][3];
+   tex_step[2] = span->attrStepX[FRAG_ATTRIB_TEX0][3];
 
    switch (info->filter) {
    case GL_NEAREST:
@@ -790,7 +778,7 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
 #undef SPAN_LINEAR
 
    /* restore state */
-   ctx->Texture._EnabledUnits = savedTexEnable;
+   ctx->Texture._EnabledCoordUnits = texEnableSave;
 }
 
 
@@ -802,22 +790,21 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
  */
 #define NAME persp_textured_triangle
 #define INTERP_Z 1
-#define INTERP_W 1
-#define INTERP_FOG 1
 #define INTERP_RGB 1
 #define INTERP_ALPHA 1
-#define INTERP_TEX 1
+#define INTERP_ATTRIBS 1
 
 #define SETUP_CODE                                                     \
    struct persp_info info;                                             \
    const struct gl_texture_unit *unit = ctx->Texture.Unit+0;           \
-   const struct gl_texture_object *obj = unit->Current2D;              \
+   struct gl_texture_object *obj =                                     \
+      ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];               \
    const GLint b = obj->BaseLevel;                                     \
    info.texture = (const GLchan *) obj->Image[0][b]->Data;             \
    info.twidth_log2 = obj->Image[0][b]->WidthLog2;                     \
    info.smask = obj->Image[0][b]->Width - 1;                           \
    info.tmask = obj->Image[0][b]->Height - 1;                          \
-   info.format = obj->Image[0][b]->Format;                             \
+   info.format = obj->Image[0][b]->_BaseFormat;                                \
    info.filter = obj->MinFilter;                                       \
    info.envmode = unit->EnvMode;                                       \
                                                                        \
@@ -861,69 +848,47 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
 
 #include "s_tritemp.h"
 
+#endif /*CHAN_TYPE != GL_FLOAT*/
 
-#endif /* CHAN_BITS != GL_FLOAT */
-
-                
 
 
 /*
- * Render a smooth-shaded, textured, RGBA triangle.
- * Interpolate S,T,R with perspective correction, w/out mipmapping.
+ * Render an RGBA triangle with arbitrary attributes.
  */
-#define NAME general_textured_triangle
+#define NAME general_triangle
 #define INTERP_Z 1
-#define INTERP_W 1
-#define INTERP_FOG 1
 #define INTERP_RGB 1
-#define INTERP_SPEC 1
 #define INTERP_ALPHA 1
-#define INTERP_TEX 1
+#define INTERP_ATTRIBS 1
 #define RENDER_SPAN( span )   _swrast_write_rgba_span(ctx, &span);
 #include "s_tritemp.h"
 
 
 
-/*
- * This is the big one!
- * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
- * Yup, it's slow.
- */
-#define NAME multitextured_triangle
-#define INTERP_Z 1
-#define INTERP_W 1
-#define INTERP_FOG 1
-#define INTERP_RGB 1
-#define INTERP_ALPHA 1
-#define INTERP_SPEC 1
-#define INTERP_MULTITEX 1
-#define RENDER_SPAN( span )   _swrast_write_rgba_span(ctx, &span);
-#include "s_tritemp.h"
-
-
 
 /*
  * Special tri function for occlusion testing
  */
 #define NAME occlusion_zless_triangle
 #define INTERP_Z 1
-#define SETUP_CODE                                             \
-   ASSERT(ctx->Depth.Test);                                    \
-   ASSERT(!ctx->Depth.Mask);                                   \
-   ASSERT(ctx->Depth.Func == GL_LESS);                         \
-   if (ctx->OcclusionResult && !ctx->Occlusion.Active) {       \
-      return;                                                  \
+#define SETUP_CODE                                                     \
+   struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer;         \
+   struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;      \
+   ASSERT(ctx->Depth.Test);                                            \
+   ASSERT(!ctx->Depth.Mask);                                           \
+   ASSERT(ctx->Depth.Func == GL_LESS);                                 \
+   if (!q) {                                                           \
+      return;                                                          \
    }
 #define RENDER_SPAN( span )                                            \
-   if (ctx->Visual.depthBits <= 16) {                                  \
+   if (rb->DepthBits <= 16) {                                          \
       GLuint i;                                                                \
       const GLushort *zRow = (const GLushort *)                                \
-         _swrast_zbuffer_address(ctx, span.x, span.y);                 \
+         rb->GetPointer(ctx, rb, span.x, span.y);                      \
       for (i = 0; i < span.end; i++) {                                 \
-         GLdepth z = FixedToDepth(span.z);                             \
+         GLuint z = FixedToDepth(span.z);                              \
          if (z < zRow[i]) {                                            \
-            ctx->OcclusionResult = GL_TRUE;                            \
-            ctx->Occlusion.PassedCounter++;                            \
+            q->Result++;                                               \
          }                                                             \
          span.z += span.zStep;                                         \
       }                                                                        \
@@ -931,11 +896,10 @@ fast_persp_span(GLcontext *ctx, struct sw_span *span,
    else {                                                              \
       GLuint i;                                                                \
       const GLuint *zRow = (const GLuint *)                            \
-         _swrast_zbuffer_address(ctx, span.x, span.y);                 \
+         rb->GetPointer(ctx, rb, span.x, span.y);                      \
       for (i = 0; i < span.end; i++) {                                 \
          if ((GLuint)span.z < zRow[i]) {                               \
-            ctx->OcclusionResult = GL_TRUE;                            \
-            ctx->Occlusion.PassedCounter++;                            \
+            q->Result++;                                               \
          }                                                             \
          span.z += span.zStep;                                         \
       }                                                                        \
@@ -960,51 +924,47 @@ nodraw_triangle( GLcontext *ctx,
  * draw the triangle, then restore the original primary color.
  * Inefficient, but seldom needed.
  */
-void _swrast_add_spec_terms_triangle( GLcontext *ctx,
-                                     const SWvertex *v0,
-                                     const SWvertex *v1,
-                                     const SWvertex *v2 )
+void
+_swrast_add_spec_terms_triangle(GLcontext *ctx, const SWvertex *v0,
+                                const SWvertex *v1, const SWvertex *v2)
 {
    SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
    SWvertex *ncv1 = (SWvertex *)v1;
    SWvertex *ncv2 = (SWvertex *)v2;
-#if CHAN_TYPE == GL_FLOAT
    GLfloat rSum, gSum, bSum;
-#else
-   GLint rSum, gSum, bSum;
-#endif
-   GLchan c[3][4];
+   GLchan cSave[3][4];
+
    /* save original colors */
-   COPY_CHAN4( c[0], ncv0->color );
-   COPY_CHAN4( c[1], ncv1->color );
-   COPY_CHAN4( c[2], ncv2->color );
+   COPY_CHAN4( cSave[0], ncv0->color );
+   COPY_CHAN4( cSave[1], ncv1->color );
+   COPY_CHAN4( cSave[2], ncv2->color );
    /* sum v0 */
-   rSum = ncv0->color[0] + ncv0->specular[0];
-   gSum = ncv0->color[1] + ncv0->specular[1];
-   bSum = ncv0->color[2] + ncv0->specular[2];
-   ncv0->color[0] = MIN2(rSum, CHAN_MAX);
-   ncv0->color[1] = MIN2(gSum, CHAN_MAX);
-   ncv0->color[2] = MIN2(bSum, CHAN_MAX);
+   rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[FRAG_ATTRIB_COL1][0];
+   gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[FRAG_ATTRIB_COL1][1];
+   bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[FRAG_ATTRIB_COL1][2];
+   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
    /* sum v1 */
-   rSum = ncv1->color[0] + ncv1->specular[0];
-   gSum = ncv1->color[1] + ncv1->specular[1];
-   bSum = ncv1->color[2] + ncv1->specular[2];
-   ncv1->color[0] = MIN2(rSum, CHAN_MAX);
-   ncv1->color[1] = MIN2(gSum, CHAN_MAX);
-   ncv1->color[2] = MIN2(bSum, CHAN_MAX);
+   rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[FRAG_ATTRIB_COL1][0];
+   gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[FRAG_ATTRIB_COL1][1];
+   bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[FRAG_ATTRIB_COL1][2];
+   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
    /* sum v2 */
-   rSum = ncv2->color[0] + ncv2->specular[0];
-   gSum = ncv2->color[1] + ncv2->specular[1];
-   bSum = ncv2->color[2] + ncv2->specular[2];
-   ncv2->color[0] = MIN2(rSum, CHAN_MAX);
-   ncv2->color[1] = MIN2(gSum, CHAN_MAX);
-   ncv2->color[2] = MIN2(bSum, CHAN_MAX);
+   rSum = CHAN_TO_FLOAT(ncv2->color[0]) + ncv2->attrib[FRAG_ATTRIB_COL1][0];
+   gSum = CHAN_TO_FLOAT(ncv2->color[1]) + ncv2->attrib[FRAG_ATTRIB_COL1][1];
+   bSum = CHAN_TO_FLOAT(ncv2->color[2]) + ncv2->attrib[FRAG_ATTRIB_COL1][2];
+   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[0], rSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[1], gSum);
+   UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[2], bSum);
    /* draw */
    SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
    /* restore original colors */
-   COPY_CHAN4( ncv0->color, c[0] );
-   COPY_CHAN4( ncv1->color, c[1] );
-   COPY_CHAN4( ncv2->color, c[2] );
+   COPY_CHAN4( ncv0->color, cSave[0] );
+   COPY_CHAN4( ncv1->color, cSave[1] );
+   COPY_CHAN4( ncv2->color, cSave[2] );
 }
 
 
@@ -1058,11 +1018,11 @@ _swrast_choose_triangle( GLcontext *ctx )
       }
 
       /* special case for occlusion testing */
-      if ((ctx->Depth.OcclusionTest || ctx->Occlusion.Active) &&
+      if (ctx->Query.CurrentOcclusionObject &&
           ctx->Depth.Test &&
           ctx->Depth.Mask == GL_FALSE &&
           ctx->Depth.Func == GL_LESS &&
-          !ctx->Stencil.Enabled) {
+          !ctx->Stencil._Enabled) {
          if ((rgbmode &&
               ctx->Color.ColorMask[0] == 0 &&
               ctx->Color.ColorMask[1] == 0 &&
@@ -1075,13 +1035,27 @@ _swrast_choose_triangle( GLcontext *ctx )
          }
       }
 
-      if (ctx->Texture._EnabledCoordUnits || ctx->FragmentProgram._Active) {
+      if (!rgbmode) {
+         USE(ci_triangle);
+         return;
+      }
+
+      /*
+       * XXX should examine swrast->_ActiveAttribMask to determine what
+       * needs to be interpolated.
+       */
+      if (ctx->Texture._EnabledCoordUnits ||
+          ctx->FragmentProgram._Current ||
+          ctx->ATIFragmentShader._Enabled ||
+          NEED_SECONDARY_COLOR(ctx) ||
+          swrast->_FogEnabled) {
          /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
-        const struct gl_texture_object *texObj2D;
+         const struct gl_texture_object *texObj2D;
          const struct gl_texture_image *texImg;
          GLenum minFilter, magFilter, envMode;
          GLint format;
-         texObj2D = ctx->Texture.Unit[0].Current2D;
+         texObj2D = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
+
          texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL;
          format = texImg ? texImg->TexFormat->MesaFormat : -1;
          minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
@@ -1090,17 +1064,21 @@ _swrast_choose_triangle( GLcontext *ctx )
 
          /* First see if we can use an optimized 2-D texture function */
          if (ctx->Texture._EnabledCoordUnits == 0x1
-             && !ctx->FragmentProgram._Active
+             && !ctx->FragmentProgram._Current
+             && !ctx->ATIFragmentShader._Enabled
              && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
              && texObj2D->WrapS == GL_REPEAT
-            && texObj2D->WrapT == GL_REPEAT
-             && texObj2D->_IsPowerOfTwo
+             && texObj2D->WrapT == GL_REPEAT
+             && texObj2D->_Swizzle == SWIZZLE_NOOP
+             && texImg->_IsPowerOfTwo
              && texImg->Border == 0
              && texImg->Width == texImg->RowStride
              && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
-            && minFilter == magFilter
-            && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
-            && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) {
+             && minFilter == magFilter
+             && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
+             && !swrast->_FogEnabled
+             && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT
+             && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
            if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
               if (minFilter == GL_NEAREST
                   && format == MESA_FORMAT_RGB
@@ -1110,7 +1088,7 @@ _swrast_choose_triangle( GLcontext *ctx )
                        && ctx->Depth.Mask == GL_TRUE)
                       || swrast->_RasterMask == TEXTURE_BIT)
                   && ctx->Polygon.StippleFlag == GL_FALSE
-                   && ctx->Visual.depthBits <= 16) {
+                   && ctx->DrawBuffer->Visual.depthBits <= 16) {
                  if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
                     USE(simple_z_textured_triangle);
                  }
@@ -1119,16 +1097,16 @@ _swrast_choose_triangle( GLcontext *ctx )
                  }
               }
               else {
-#if (CHAN_BITS == 16 || CHAN_BITS == 32)
-                  USE(general_textured_triangle);
+#if CHAN_BITS != 8
+                  USE(general_triangle);
 #else
                   USE(affine_textured_triangle);
 #endif
               }
            }
            else {
-#if (CHAN_BITS == 16 || CHAN_BITS == 32)
-               USE(general_textured_triangle);
+#if CHAN_BITS != 8
+               USE(general_triangle);
 #else
                USE(persp_textured_triangle);
 #endif
@@ -1136,33 +1114,27 @@ _swrast_choose_triangle( GLcontext *ctx )
         }
          else {
             /* general case textured triangles */
-            if (ctx->Texture._EnabledCoordUnits > 1) {
-               USE(multitextured_triangle);
-            }
-            else {
-               USE(general_textured_triangle);
-            }
+            USE(general_triangle);
          }
       }
       else {
-         ASSERT(!ctx->Texture._EnabledCoordUnits);
+         ASSERT(!swrast->_FogEnabled);
+         ASSERT(!NEED_SECONDARY_COLOR(ctx));
         if (ctx->Light.ShadeModel==GL_SMOOTH) {
            /* smooth shaded, no texturing, stippled or some raster ops */
-            if (rgbmode) {
-              USE(smooth_rgba_triangle);
-            }
-            else {
-               USE(smooth_ci_triangle);
-            }
+#if CHAN_BITS != 8
+               USE(general_triangle);
+#else
+               USE(smooth_rgba_triangle);
+#endif
         }
         else {
            /* flat shaded, no texturing, stippled or some raster ops */
-            if (rgbmode) {
-              USE(flat_rgba_triangle);
-            }
-            else {
-               USE(flat_ci_triangle);
-            }
+#if CHAN_BITS != 8
+            USE(general_triangle);
+#else
+            USE(flat_rgba_triangle);
+#endif
         }
       }
    }