mesa: remove gl_renderbuffer::DataType
[mesa.git] / src / mesa / swrast / s_triangle.c
index ab08b7325a552818aaca606eac1bc8234b1516d2..124aa5f8edd2f2005a3938c9c185a1bf1c25dde4 100644 (file)
@@ -34,8 +34,9 @@
 #include "main/colormac.h"
 #include "main/imports.h"
 #include "main/macros.h"
-#include "main/texformat.h"
-#include "shader/prog_instruction.h"
+#include "main/mtypes.h"
+#include "main/state.h"
+#include "program/prog_instruction.h"
 
 #include "s_aatriangle.h"
 #include "s_context.h"
 #include "s_triangle.h"
 
 
-/*
- * Just used for feedback mode.
+/**
+ * Test if a triangle should be culled.  Used for feedback and selection mode.
+ * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
  */
 GLboolean
-_swrast_culltriangle( GLcontext *ctx,
+_swrast_culltriangle( struct gl_context *ctx,
                       const SWvertex *v0,
                       const SWvertex *v1,
                       const SWvertex *v2 )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    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)->_BackfaceCullSign > 0)
-      return 0;
+   if (c * swrast->_BackfaceSign * swrast->_BackfaceCullSign <= 0.0F)
+      return GL_FALSE;
 
-   return 1;
+   return GL_TRUE;
 }
 
 
 
-/*
- * Render a smooth or flat-shaded color index triangle.
- */
-#define NAME ci_triangle
-#define INTERP_Z 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"
-
-
-
 /*
  * Render a flat-shaded RGBA triangle.
  */
@@ -132,22 +123,26 @@ _swrast_culltriangle( GLcontext *ctx,
 
 #define SETUP_CODE                                                     \
    struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
-   struct gl_texture_object *obj =                                     \
+   const 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;         \
-   const GLint twidth_log2 = obj->Image[0][b]->WidthLog2;              \
-   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;                   \
+   const struct gl_texture_image *texImg =                             \
+      obj->Image[0][obj->BaseLevel];                                   \
+   const struct swrast_texture_image *swImg =                          \
+      swrast_texture_image_const(texImg);                              \
+   const GLfloat twidth = (GLfloat) texImg->Width;                     \
+   const GLfloat theight = (GLfloat) texImg->Height;                   \
+   const GLint twidth_log2 = texImg->WidthLog2;                                \
+   const GLubyte *texture = (const GLubyte *) swImg->Map;              \
+   const GLint smask = texImg->Width - 1;                              \
+   const GLint tmask = texImg->Height - 1;                             \
+   ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888);                    \
    if (!rb || !texture) {                                              \
       return;                                                          \
    }
 
 #define RENDER_SPAN( span )                                            \
    GLuint i;                                                           \
-   GLchan rgb[MAX_WIDTH][3];                                           \
+   GLubyte rgba[MAX_WIDTH][4];                                         \
    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */               \
    span.intTex[1] -= FIXED_HALF;                                       \
    for (i = 0; i < span.end; i++) {                                    \
@@ -155,13 +150,15 @@ _swrast_culltriangle( GLcontext *ctx,
       GLint t = FixedToInt(span.intTex[1]) & tmask;                    \
       GLint pos = (t << twidth_log2) + s;                              \
       pos = pos + pos + pos;  /* multiply by 3 */                      \
-      rgb[i][RCOMP] = texture[pos];                                    \
-      rgb[i][GCOMP] = texture[pos+1];                                  \
-      rgb[i][BCOMP] = texture[pos+2];                                  \
+      rgba[i][RCOMP] = texture[pos+2];                                 \
+      rgba[i][GCOMP] = texture[pos+1];                                 \
+      rgba[i][BCOMP] = texture[pos+0];                                 \
+      rgba[i][ACOMP] = 0xff;                                            \
       span.intTex[0] += span.intTexStep[0];                            \
       span.intTex[1] += span.intTexStep[1];                            \
    }                                                                   \
-   rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
+   _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, span.end,                 \
+                   span.x, span.y, rgba, NULL);
 
 #include "s_tritemp.h"
 
@@ -184,22 +181,26 @@ _swrast_culltriangle( GLcontext *ctx,
 
 #define SETUP_CODE                                                     \
    struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
-   struct gl_texture_object *obj =                                     \
+   const 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;         \
-   const GLint twidth_log2 = obj->Image[0][b]->WidthLog2;              \
-   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;                   \
+   const struct gl_texture_image *texImg =                             \
+       obj->Image[0][obj->BaseLevel];                                  \
+   const struct swrast_texture_image *swImg =                          \
+      swrast_texture_image_const(texImg);                              \
+   const GLfloat twidth = (GLfloat) texImg->Width;                     \
+   const GLfloat theight = (GLfloat) texImg->Height;                   \
+   const GLint twidth_log2 = texImg->WidthLog2;                                \
+   const GLubyte *texture = (const GLubyte *) swImg->Map;              \
+   const GLint smask = texImg->Width - 1;                              \
+   const GLint tmask = texImg->Height - 1;                             \
+   ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888);                    \
    if (!rb || !texture) {                                              \
       return;                                                          \
    }
 
 #define RENDER_SPAN( span )                                            \
    GLuint i;                                                           \
-   GLchan rgb[MAX_WIDTH][3];                                           \
+   GLubyte rgba[MAX_WIDTH][4];                                         \
    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */               \
    span.intTex[1] -= FIXED_HALF;                                       \
    for (i = 0; i < span.end; i++) {                                    \
@@ -209,9 +210,10 @@ _swrast_culltriangle( GLcontext *ctx,
          GLint t = FixedToInt(span.intTex[1]) & tmask;                 \
          GLint pos = (t << twidth_log2) + s;                           \
          pos = pos + pos + pos;  /* multiply by 3 */                   \
-         rgb[i][RCOMP] = texture[pos];                                 \
-         rgb[i][GCOMP] = texture[pos+1];                               \
-         rgb[i][BCOMP] = texture[pos+2];                               \
+         rgba[i][RCOMP] = texture[pos+2];                              \
+         rgba[i][GCOMP] = texture[pos+1];                              \
+         rgba[i][BCOMP] = texture[pos+0];                              \
+         rgba[i][ACOMP] = 0xff;                                        \
          zRow[i] = z;                                                  \
          span.array->mask[i] = 1;                                      \
       }                                                                        \
@@ -222,7 +224,8 @@ _swrast_culltriangle( GLcontext *ctx,
       span.intTex[1] += span.intTexStep[1];                            \
       span.z += span.zStep;                                            \
    }                                                                   \
-   rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
+   _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE,                           \
+                   span.end, span.x, span.y, rgba, span.array->mask);
 
 #include "s_tritemp.h"
 
@@ -242,13 +245,13 @@ struct affine_info
 };
 
 
-static INLINE GLint
+static inline GLint
 ilerp(GLint t, GLint a, GLint b)
 {
    return a + ((t * (b - a)) >> FIXED_SHIFT);
 }
 
-static INLINE GLint
+static inline GLint
 ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
 {
    const GLint temp0 = ilerp(ia, v00, v10);
@@ -261,12 +264,12 @@ ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
  * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
  * texture env modes.
  */
-static INLINE void
-affine_span(GLcontext *ctx, SWspan *span,
+static inline void
+affine_span(struct gl_context *ctx, SWspan *span,
             struct affine_info *info)
 {
    GLchan sample[4];  /* the filtered texture sample */
-   const GLuint texEnableSave = ctx->Texture._EnabledUnits;
+   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
@@ -274,25 +277,29 @@ affine_span(GLcontext *ctx, SWspan *span,
     * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
     */
 
-#define NEAREST_RGB                    \
-   sample[RCOMP] = tex00[RCOMP];       \
-   sample[GCOMP] = tex00[GCOMP];       \
-   sample[BCOMP] = tex00[BCOMP];       \
-   sample[ACOMP] = CHAN_MAX
+#define NEAREST_RGB            \
+   sample[RCOMP] = tex00[2];   \
+   sample[GCOMP] = tex00[1];   \
+   sample[BCOMP] = tex00[0];   \
+   sample[ACOMP] = CHAN_MAX;
 
 #define LINEAR_RGB                                                     \
-   sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
+   sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
    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[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
    sample[ACOMP] = CHAN_MAX;
 
-#define NEAREST_RGBA  COPY_CHAN4(sample, tex00)
+#define NEAREST_RGBA  \
+   sample[RCOMP] = tex00[3];   \
+   sample[GCOMP] = tex00[2];   \
+   sample[BCOMP] = tex00[1];   \
+   sample[ACOMP] = tex00[0];
 
 #define LINEAR_RGBA                                                    \
-   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])
+   sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
+   sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
+   sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
+   sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
 
 #define MODULATE                                                         \
    dest[RCOMP] = span->red   * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
@@ -343,7 +350,11 @@ affine_span(GLcontext *ctx, SWspan *span,
    dest[2] = sample[2];                        \
    dest[3] = FixedToInt(span->alpha);
 
-#define NEAREST_RGBA_REPLACE  COPY_CHAN4(dest, tex00)
+#define NEAREST_RGBA_REPLACE  \
+   dest[RCOMP] = tex00[3]; \
+   dest[GCOMP] = tex00[2]; \
+   dest[BCOMP] = tex00[1]; \
+   dest[ACOMP] = tex00[0]
 
 #define SPAN_NEAREST(DO_TEX, COMPS)                                    \
        for (i = 0; i < span->end; i++) {                               \
@@ -397,14 +408,14 @@ affine_span(GLcontext *ctx, SWspan *span,
    GLchan *dest = span->array->rgba[0];
 
    /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
-   ctx->Texture._EnabledUnits = 0x0;
+   ctx->Texture._EnabledCoordUnits = 0x0;
 
    span->intTex[0] -= FIXED_HALF;
    span->intTex[1] -= FIXED_HALF;
    switch (info->filter) {
    case GL_NEAREST:
       switch (info->format) {
-      case GL_RGB:
+      case MESA_FORMAT_RGB888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
@@ -424,7 +435,7 @@ affine_span(GLcontext *ctx, SWspan *span,
             return;
          }
          break;
-      case GL_RGBA:
+      case MESA_FORMAT_RGBA8888:
          switch(info->envmode) {
          case GL_MODULATE:
             SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
@@ -453,7 +464,7 @@ affine_span(GLcontext *ctx, SWspan *span,
       span->intTex[0] -= FIXED_HALF;
       span->intTex[1] -= FIXED_HALF;
       switch (info->format) {
-      case GL_RGB:
+      case MESA_FORMAT_RGB888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
@@ -473,7 +484,7 @@ affine_span(GLcontext *ctx, SWspan *span,
             return;
          }
          break;
-      case GL_RGBA:
+      case MESA_FORMAT_RGBA8888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
@@ -504,7 +515,7 @@ affine_span(GLcontext *ctx, SWspan *span,
    _swrast_write_rgba_span(ctx, span);
 
    /* re-enable texture units */
-   ctx->Texture._EnabledUnits = texEnableSave;
+   ctx->Texture._EnabledCoordUnits = texEnableSave;
 
 #undef SPAN_NEAREST
 #undef SPAN_LINEAR
@@ -526,18 +537,24 @@ affine_span(GLcontext *ctx, SWspan *span,
 #define SETUP_CODE                                                     \
    struct affine_info info;                                            \
    struct gl_texture_unit *unit = ctx->Texture.Unit+0;                 \
-   struct gl_texture_object *obj =                                     \
+   const 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;         \
-   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]->_BaseFormat;                                \
-   info.filter = obj->MinFilter;                                       \
+   const struct gl_texture_image *texImg =                             \
+      obj->Image[0][obj->BaseLevel];                                   \
+   const struct swrast_texture_image *swImg =                          \
+      swrast_texture_image_const(texImg);                              \
+   const GLfloat twidth = (GLfloat) texImg->Width;                     \
+   const GLfloat theight = (GLfloat) texImg->Height;                   \
+   info.texture = (const GLchan *) swImg->Map;                         \
+   info.twidth_log2 = texImg->WidthLog2;                               \
+   info.smask = texImg->Width - 1;                                     \
+   info.tmask = texImg->Height - 1;                                    \
+   info.format = texImg->TexFormat;                                    \
+   info.filter = obj->Sampler.MinFilter;                               \
    info.envmode = unit->EnvMode;                                       \
+   info.er = 0;                                        \
+   info.eg = 0;                                        \
+   info.eb = 0;                                        \
    span.arrayMask |= SPAN_RGBA;                                                \
                                                                        \
    if (info.envmode == GL_BLEND) {                                     \
@@ -553,25 +570,17 @@ affine_span(GLcontext *ctx, SWspan *span,
    }                                                                   \
                                                                        \
    switch (info.format) {                                              \
-   case GL_ALPHA:                                                      \
-   case GL_LUMINANCE:                                                  \
-   case GL_INTENSITY:                                                  \
-      info.tbytesline = obj->Image[0][b]->Width;                       \
-      break;                                                           \
-   case GL_LUMINANCE_ALPHA:                                            \
-      info.tbytesline = obj->Image[0][b]->Width * 2;                   \
-      break;                                                           \
-   case GL_RGB:                                                                \
-      info.tbytesline = obj->Image[0][b]->Width * 3;                   \
+   case MESA_FORMAT_RGB888:                                            \
+      info.tbytesline = texImg->Width * 3;                             \
       break;                                                           \
-   case GL_RGBA:                                                       \
-      info.tbytesline = obj->Image[0][b]->Width * 4;                   \
+   case MESA_FORMAT_RGBA8888:                                          \
+      info.tbytesline = texImg->Width * 4;                             \
       break;                                                           \
    default:                                                            \
       _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
       return;                                                          \
    }                                                                   \
-   info.tsize = obj->Image[0][b]->Height * info.tbytesline;
+   info.tsize = texImg->Height * info.tbytesline;
 
 #define RENDER_SPAN( span )   affine_span(ctx, &span, &info);
 
@@ -592,8 +601,8 @@ struct persp_info
 };
 
 
-static INLINE void
-fast_persp_span(GLcontext *ctx, SWspan *span,
+static inline void
+fast_persp_span(struct gl_context *ctx, SWspan *span,
                struct persp_info *info)
 {
    GLchan sample[4];  /* the filtered texture sample */
@@ -664,8 +673,8 @@ fast_persp_span(GLcontext *ctx, SWspan *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->attrStart[FRAG_ATTRIB_TEX0][0]  * (info->smask + 1);
    tex_step[0] = span->attrStepX[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
@@ -678,7 +687,7 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
    switch (info->filter) {
    case GL_NEAREST:
       switch (info->format) {
-      case GL_RGB:
+      case MESA_FORMAT_RGB888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
@@ -698,7 +707,7 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
             return;
          }
          break;
-      case GL_RGBA:
+      case MESA_FORMAT_RGBA8888:
          switch(info->envmode) {
          case GL_MODULATE:
             SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
@@ -725,7 +734,7 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
 
    case GL_LINEAR:
       switch (info->format) {
-      case GL_RGB:
+      case MESA_FORMAT_RGB888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
@@ -745,7 +754,7 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
             return;
          }
          break;
-      case GL_RGBA:
+      case MESA_FORMAT_RGBA8888:
          switch (info->envmode) {
          case GL_MODULATE:
             SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
@@ -778,7 +787,7 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
 #undef SPAN_LINEAR
 
    /* restore state */
-   ctx->Texture._EnabledUnits = savedTexEnable;
+   ctx->Texture._EnabledCoordUnits = texEnableSave;
 }
 
 
@@ -797,16 +806,22 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
 #define SETUP_CODE                                                     \
    struct persp_info info;                                             \
    const struct gl_texture_unit *unit = ctx->Texture.Unit+0;           \
-   struct gl_texture_object *obj =                                     \
+   const 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]->_BaseFormat;                                \
-   info.filter = obj->MinFilter;                                       \
+   const struct gl_texture_image *texImg =                             \
+      obj->Image[0][obj->BaseLevel];                                   \
+   const struct swrast_texture_image *swImg =                          \
+      swrast_texture_image_const(texImg);                              \
+   info.texture = (const GLchan *) swImg->Map;                         \
+   info.twidth_log2 = texImg->WidthLog2;                               \
+   info.smask = texImg->Width - 1;                                     \
+   info.tmask = texImg->Height - 1;                                    \
+   info.format = texImg->TexFormat;                                    \
+   info.filter = obj->Sampler.MinFilter;                               \
    info.envmode = unit->EnvMode;                                       \
+   info.er = 0;                                        \
+   info.eg = 0;                                        \
+   info.eb = 0;                                        \
                                                                        \
    if (info.envmode == GL_BLEND) {                                     \
       /* potential off-by-one error here? (1.0f -> 2048 -> 0) */       \
@@ -821,25 +836,17 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
    }                                                                   \
                                                                        \
    switch (info.format) {                                              \
-   case GL_ALPHA:                                                      \
-   case GL_LUMINANCE:                                                  \
-   case GL_INTENSITY:                                                  \
-      info.tbytesline = obj->Image[0][b]->Width;                       \
+   case MESA_FORMAT_RGB888:                                            \
+      info.tbytesline = texImg->Width * 3;                             \
       break;                                                           \
-   case GL_LUMINANCE_ALPHA:                                            \
-      info.tbytesline = obj->Image[0][b]->Width * 2;                   \
-      break;                                                           \
-   case GL_RGB:                                                                \
-      info.tbytesline = obj->Image[0][b]->Width * 3;                   \
-      break;                                                           \
-   case GL_RGBA:                                                       \
-      info.tbytesline = obj->Image[0][b]->Width * 4;                   \
+   case MESA_FORMAT_RGBA8888:                                          \
+      info.tbytesline = texImg->Width * 4;                             \
       break;                                                           \
    default:                                                            \
       _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
       return;                                                          \
    }                                                                   \
-   info.tsize = obj->Image[0][b]->Height * info.tbytesline;
+   info.tsize = texImg->Height * info.tbytesline;
 
 #define RENDER_SPAN( span )                    \
    span.interpMask &= ~SPAN_RGBA;              \
@@ -869,22 +876,24 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
 /*
  * Special tri function for occlusion testing
  */
-#define NAME occlusion_zless_triangle
+#define NAME occlusion_zless_16_triangle
 #define INTERP_Z 1
 #define SETUP_CODE                                                     \
-   struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer;         \
+   struct gl_renderbuffer *rb =                                         \
+      ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;           \
    struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;      \
    ASSERT(ctx->Depth.Test);                                            \
    ASSERT(!ctx->Depth.Mask);                                           \
    ASSERT(ctx->Depth.Func == GL_LESS);                                 \
+   assert(rb->Format == MESA_FORMAT_Z16);                               \
    if (!q) {                                                           \
       return;                                                          \
    }
 #define RENDER_SPAN( span )                                            \
-   if (rb->DepthBits <= 16) {                                          \
+   {                                                                    \
       GLuint i;                                                                \
       const GLushort *zRow = (const GLushort *)                                \
-         rb->GetPointer(ctx, rb, span.x, span.y);                      \
+         _swrast_pixel_address(rb, span.x, span.y);                     \
       for (i = 0; i < span.end; i++) {                                 \
          GLuint z = FixedToDepth(span.z);                              \
          if (z < zRow[i]) {                                            \
@@ -892,24 +901,13 @@ fast_persp_span(GLcontext *ctx, SWspan *span,
          }                                                             \
          span.z += span.zStep;                                         \
       }                                                                        \
-   }                                                                   \
-   else {                                                              \
-      GLuint i;                                                                \
-      const GLuint *zRow = (const GLuint *)                            \
-         rb->GetPointer(ctx, rb, span.x, span.y);                      \
-      for (i = 0; i < span.end; i++) {                                 \
-         if ((GLuint)span.z < zRow[i]) {                               \
-            q->Result++;                                               \
-         }                                                             \
-         span.z += span.zStep;                                         \
-      }                                                                        \
    }
 #include "s_tritemp.h"
 
 
 
 static void
-nodraw_triangle( GLcontext *ctx,
+nodraw_triangle( struct gl_context *ctx,
                  const SWvertex *v0,
                  const SWvertex *v1,
                  const SWvertex *v2 )
@@ -925,7 +923,7 @@ nodraw_triangle( GLcontext *ctx,
  * Inefficient, but seldom needed.
  */
 void
-_swrast_add_spec_terms_triangle(GLcontext *ctx, const SWvertex *v0,
+_swrast_add_spec_terms_triangle(struct gl_context *ctx, const SWvertex *v0,
                                 const SWvertex *v1, const SWvertex *v2)
 {
    SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
@@ -998,10 +996,9 @@ do {                                               \
  * remove tests to this code.
  */
 void
-_swrast_choose_triangle( GLcontext *ctx )
+_swrast_choose_triangle( struct gl_context *ctx )
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   const GLboolean rgbmode = ctx->Visual.rgbMode;
 
    if (ctx->Polygon.CullFlag &&
        ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
@@ -1010,6 +1007,8 @@ _swrast_choose_triangle( GLcontext *ctx )
    }
 
    if (ctx->RenderMode==GL_RENDER) {
+      struct gl_renderbuffer *depthRb =
+         ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
 
       if (ctx->Polygon.SmoothFlag) {
          _swrast_set_aa_triangle_function(ctx);
@@ -1022,24 +1021,18 @@ _swrast_choose_triangle( GLcontext *ctx )
           ctx->Depth.Test &&
           ctx->Depth.Mask == GL_FALSE &&
           ctx->Depth.Func == GL_LESS &&
-          !ctx->Stencil.Enabled) {
-         if ((rgbmode &&
-              ctx->Color.ColorMask[0] == 0 &&
-              ctx->Color.ColorMask[1] == 0 &&
-              ctx->Color.ColorMask[2] == 0 &&
-              ctx->Color.ColorMask[3] == 0)
-             ||
-             (!rgbmode && ctx->Color.IndexMask == 0)) {
-            USE(occlusion_zless_triangle);
+          !ctx->Stencil._Enabled &&
+          depthRb &&
+          depthRb->Format == MESA_FORMAT_Z16) {
+         if (ctx->Color.ColorMask[0][0] == 0 &&
+            ctx->Color.ColorMask[0][1] == 0 &&
+            ctx->Color.ColorMask[0][2] == 0 &&
+            ctx->Color.ColorMask[0][3] == 0) {
+            USE(occlusion_zless_16_triangle);
             return;
          }
       }
 
-      if (!rgbmode) {
-         USE(ci_triangle);
-         return;
-      }
-
       /*
        * XXX should examine swrast->_ActiveAttribMask to determine what
        * needs to be interpolated.
@@ -1047,33 +1040,37 @@ _swrast_choose_triangle( GLcontext *ctx )
       if (ctx->Texture._EnabledCoordUnits ||
           ctx->FragmentProgram._Current ||
           ctx->ATIFragmentShader._Enabled ||
-          NEED_SECONDARY_COLOR(ctx) ||
+          _mesa_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_image *texImg;
+         const struct swrast_texture_image *swImg;
          GLenum minFilter, magFilter, envMode;
-         GLint format;
+         gl_format format;
          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;
-         magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
+         swImg = swrast_texture_image_const(texImg);
+
+         format = texImg ? texImg->TexFormat : MESA_FORMAT_NONE;
+         minFilter = texObj2D ? texObj2D->Sampler.MinFilter : GL_NONE;
+         magFilter = texObj2D ? texObj2D->Sampler.MagFilter : GL_NONE;
          envMode = ctx->Texture.Unit[0].EnvMode;
 
          /* First see if we can use an optimized 2-D texture function */
          if (ctx->Texture._EnabledCoordUnits == 0x1
              && !ctx->FragmentProgram._Current
              && !ctx->ATIFragmentShader._Enabled
+             && ctx->Texture._EnabledUnits == 0x1
              && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
-             && texObj2D->WrapS == GL_REPEAT
-             && texObj2D->WrapT == GL_REPEAT
+             && texObj2D->Sampler.WrapS == GL_REPEAT
+             && texObj2D->Sampler.WrapT == GL_REPEAT
              && texObj2D->_Swizzle == SWIZZLE_NOOP
-             && texImg->_IsPowerOfTwo
+             && swImg->_IsPowerOfTwo
              && texImg->Border == 0
-             && texImg->Width == texImg->RowStride
-             && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
+             && texImg->Width == swImg->RowStride
+             && (format == MESA_FORMAT_RGB888 || format == MESA_FORMAT_RGBA8888)
              && minFilter == magFilter
              && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
              && !swrast->_FogEnabled
@@ -1081,7 +1078,7 @@ _swrast_choose_triangle( GLcontext *ctx )
              && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
            if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
               if (minFilter == GL_NEAREST
-                  && format == MESA_FORMAT_RGB
+                  && format == MESA_FORMAT_RGB888
                   && (envMode == GL_REPLACE || envMode == GL_DECAL)
                   && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
                        && ctx->Depth.Func == GL_LESS
@@ -1100,7 +1097,15 @@ _swrast_choose_triangle( GLcontext *ctx )
 #if CHAN_BITS != 8
                   USE(general_triangle);
 #else
-                  USE(affine_textured_triangle);
+                  if (format == MESA_FORMAT_RGBA8888 && !_mesa_little_endian()) {
+                     /* We only handle RGBA8888 correctly on little endian
+                      * in the optimized code above.
+                      */
+                     USE(general_triangle);
+                  }
+                  else {
+                     USE(affine_textured_triangle);
+                 }
 #endif
               }
            }
@@ -1119,7 +1124,7 @@ _swrast_choose_triangle( GLcontext *ctx )
       }
       else {
          ASSERT(!swrast->_FogEnabled);
-         ASSERT(!NEED_SECONDARY_COLOR(ctx));
+         ASSERT(!_mesa_need_secondary_color(ctx));
         if (ctx->Light.ShadeModel==GL_SMOOTH) {
            /* smooth shaded, no texturing, stippled or some raster ops */
 #if CHAN_BITS != 8