Home | History | Annotate | Download | only in swrast
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 
     26 /*
     27  * When the device driver doesn't implement triangle rasterization it
     28  * can hook in _swrast_Triangle, which eventually calls one of these
     29  * functions to draw triangles.
     30  */
     31 
     32 #include "main/glheader.h"
     33 #include "main/context.h"
     34 #include "main/imports.h"
     35 #include "main/macros.h"
     36 #include "main/mtypes.h"
     37 #include "main/state.h"
     38 #include "main/samplerobj.h"
     39 #include "main/stencil.h"
     40 #include "main/teximage.h"
     41 #include "program/prog_instruction.h"
     42 
     43 #include "s_aatriangle.h"
     44 #include "s_context.h"
     45 #include "s_feedback.h"
     46 #include "s_span.h"
     47 #include "s_triangle.h"
     48 
     49 
     50 /**
     51  * Test if a triangle should be culled.  Used for feedback and selection mode.
     52  * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
     53  */
     54 GLboolean
     55 _swrast_culltriangle( struct gl_context *ctx,
     56                       const SWvertex *v0,
     57                       const SWvertex *v1,
     58                       const SWvertex *v2 )
     59 {
     60    SWcontext *swrast = SWRAST_CONTEXT(ctx);
     61    GLfloat ex = v1->attrib[VARYING_SLOT_POS][0] - v0->attrib[VARYING_SLOT_POS][0];
     62    GLfloat ey = v1->attrib[VARYING_SLOT_POS][1] - v0->attrib[VARYING_SLOT_POS][1];
     63    GLfloat fx = v2->attrib[VARYING_SLOT_POS][0] - v0->attrib[VARYING_SLOT_POS][0];
     64    GLfloat fy = v2->attrib[VARYING_SLOT_POS][1] - v0->attrib[VARYING_SLOT_POS][1];
     65    GLfloat c = ex*fy-ey*fx;
     66 
     67    if (c * swrast->_BackfaceSign * swrast->_BackfaceCullSign <= 0.0F)
     68       return GL_FALSE;
     69 
     70    return GL_TRUE;
     71 }
     72 
     73 
     74 
     75 /*
     76  * Render a flat-shaded RGBA triangle.
     77  */
     78 #define NAME flat_rgba_triangle
     79 #define INTERP_Z 1
     80 #define SETUP_CODE				\
     81    assert(ctx->Texture._EnabledCoordUnits == 0);\
     82    assert(ctx->Light.ShadeModel==GL_FLAT);	\
     83    span.interpMask |= SPAN_RGBA;		\
     84    span.red = ChanToFixed(v2->color[0]);	\
     85    span.green = ChanToFixed(v2->color[1]);	\
     86    span.blue = ChanToFixed(v2->color[2]);	\
     87    span.alpha = ChanToFixed(v2->color[3]);	\
     88    span.redStep = 0;				\
     89    span.greenStep = 0;				\
     90    span.blueStep = 0;				\
     91    span.alphaStep = 0;
     92 #define RENDER_SPAN( span )  _swrast_write_rgba_span(ctx, &span);
     93 #include "s_tritemp.h"
     94 
     95 
     96 
     97 /*
     98  * Render a smooth-shaded RGBA triangle.
     99  */
    100 #define NAME smooth_rgba_triangle
    101 #define INTERP_Z 1
    102 #define INTERP_RGB 1
    103 #define INTERP_ALPHA 1
    104 #define SETUP_CODE				\
    105    {						\
    106       /* texturing must be off */		\
    107       assert(ctx->Texture._EnabledCoordUnits == 0);	\
    108       assert(ctx->Light.ShadeModel==GL_SMOOTH);	\
    109    }
    110 #define RENDER_SPAN( span )  _swrast_write_rgba_span(ctx, &span);
    111 #include "s_tritemp.h"
    112 
    113 
    114 
    115 /*
    116  * Render an RGB, GL_DECAL, textured triangle.
    117  * Interpolate S,T only w/out mipmapping or perspective correction.
    118  *
    119  * No fog.  No depth testing.
    120  */
    121 #define NAME simple_textured_triangle
    122 #define INTERP_INT_TEX 1
    123 #define S_SCALE twidth
    124 #define T_SCALE theight
    125 
    126 #define SETUP_CODE							\
    127    struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];	\
    128    const struct gl_texture_object *obj = 				\
    129       ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
    130    const struct gl_texture_image *texImg =				\
    131       _mesa_base_tex_image(obj);					\
    132    const struct swrast_texture_image *swImg =				\
    133       swrast_texture_image_const(texImg);				\
    134    const GLfloat twidth = (GLfloat) texImg->Width;			\
    135    const GLfloat theight = (GLfloat) texImg->Height;			\
    136    const GLint twidth_log2 = texImg->WidthLog2;				\
    137    const GLubyte *texture = (const GLubyte *) swImg->ImageSlices[0];	\
    138    const GLint smask = texImg->Width - 1;				\
    139    const GLint tmask = texImg->Height - 1;				\
    140    assert(texImg->TexFormat == MESA_FORMAT_BGR_UNORM8);			\
    141    if (!rb || !texture) {						\
    142       return;								\
    143    }
    144 
    145 #define RENDER_SPAN( span )						\
    146    GLuint i;								\
    147    GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8;			\
    148    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */		\
    149    span.intTex[1] -= FIXED_HALF;					\
    150    for (i = 0; i < span.end; i++) {					\
    151       GLint s = FixedToInt(span.intTex[0]) & smask;			\
    152       GLint t = FixedToInt(span.intTex[1]) & tmask;			\
    153       GLint pos = (t << twidth_log2) + s;				\
    154       pos = pos + pos + pos;  /* multiply by 3 */			\
    155       rgba[i][RCOMP] = texture[pos+2];					\
    156       rgba[i][GCOMP] = texture[pos+1];					\
    157       rgba[i][BCOMP] = texture[pos+0];					\
    158       rgba[i][ACOMP] = 0xff;                                            \
    159       span.intTex[0] += span.intTexStep[0];				\
    160       span.intTex[1] += span.intTexStep[1];				\
    161    }									\
    162    _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, span.end,                 \
    163                    span.x, span.y, rgba, NULL);
    164 
    165 #include "s_tritemp.h"
    166 
    167 
    168 
    169 /*
    170  * Render an RGB, GL_DECAL, textured triangle.
    171  * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
    172  * perspective correction.
    173  * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
    174  *
    175  * No fog.
    176  */
    177 #define NAME simple_z_textured_triangle
    178 #define INTERP_Z 1
    179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
    180 #define INTERP_INT_TEX 1
    181 #define S_SCALE twidth
    182 #define T_SCALE theight
    183 
    184 #define SETUP_CODE							\
    185    struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];	\
    186    const struct gl_texture_object *obj = 				\
    187       ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
    188    const struct gl_texture_image *texImg = 				\
    189       _mesa_base_tex_image(obj);					\
    190    const struct swrast_texture_image *swImg =				\
    191       swrast_texture_image_const(texImg);				\
    192    const GLfloat twidth = (GLfloat) texImg->Width;			\
    193    const GLfloat theight = (GLfloat) texImg->Height;			\
    194    const GLint twidth_log2 = texImg->WidthLog2;				\
    195    const GLubyte *texture = (const GLubyte *) swImg->ImageSlices[0];	\
    196    const GLint smask = texImg->Width - 1;				\
    197    const GLint tmask = texImg->Height - 1;				\
    198    assert(texImg->TexFormat == MESA_FORMAT_BGR_UNORM8);			\
    199    if (!rb || !texture) {						\
    200       return;								\
    201    }
    202 
    203 #define RENDER_SPAN( span )						\
    204    GLuint i;				    				\
    205    GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8;			\
    206    GLubyte *mask = swrast->SpanArrays->mask;                            \
    207    span.intTex[0] -= FIXED_HALF; /* off-by-one error? */		\
    208    span.intTex[1] -= FIXED_HALF;					\
    209    for (i = 0; i < span.end; i++) {					\
    210       const GLuint z = FixedToDepth(span.z);				\
    211       if (z < zRow[i]) {						\
    212          GLint s = FixedToInt(span.intTex[0]) & smask;			\
    213          GLint t = FixedToInt(span.intTex[1]) & tmask;			\
    214          GLint pos = (t << twidth_log2) + s;				\
    215          pos = pos + pos + pos;  /* multiply by 3 */			\
    216          rgba[i][RCOMP] = texture[pos+2];				\
    217          rgba[i][GCOMP] = texture[pos+1];				\
    218          rgba[i][BCOMP] = texture[pos+0];				\
    219          rgba[i][ACOMP] = 0xff;          				\
    220          zRow[i] = z;							\
    221          mask[i] = 1;							\
    222       }									\
    223       else {								\
    224          mask[i] = 0;							\
    225       }									\
    226       span.intTex[0] += span.intTexStep[0];				\
    227       span.intTex[1] += span.intTexStep[1];				\
    228       span.z += span.zStep;						\
    229    }									\
    230    _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE,                           \
    231                    span.end, span.x, span.y, rgba, mask);
    232 
    233 #include "s_tritemp.h"
    234 
    235 
    236 #if CHAN_TYPE != GL_FLOAT
    237 
    238 struct affine_info
    239 {
    240    GLenum filter;
    241    GLenum format;
    242    GLenum envmode;
    243    GLint smask, tmask;
    244    GLint twidth_log2;
    245    const GLchan *texture;
    246    GLfixed er, eg, eb, ea;
    247    GLint tbytesline, tsize;
    248 };
    249 
    250 
    251 static inline GLint
    252 ilerp(GLint t, GLint a, GLint b)
    253 {
    254    return a + ((t * (b - a)) >> FIXED_SHIFT);
    255 }
    256 
    257 static inline GLint
    258 ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
    259 {
    260    const GLint temp0 = ilerp(ia, v00, v10);
    261    const GLint temp1 = ilerp(ia, v01, v11);
    262    return ilerp(ib, temp0, temp1);
    263 }
    264 
    265 
    266 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
    267  * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
    268  * texture env modes.
    269  */
    270 static inline void
    271 affine_span(struct gl_context *ctx, SWspan *span,
    272             struct affine_info *info)
    273 {
    274    GLchan sample[4];  /* the filtered texture sample */
    275    const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
    276 
    277    /* Instead of defining a function for each mode, a test is done
    278     * between the outer and inner loops. This is to reduce code size
    279     * and complexity. Observe that an optimizing compiler kills
    280     * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
    281     */
    282 
    283 #define NEAREST_RGB		\
    284    sample[RCOMP] = tex00[2];	\
    285    sample[GCOMP] = tex00[1];	\
    286    sample[BCOMP] = tex00[0];	\
    287    sample[ACOMP] = CHAN_MAX;
    288 
    289 #define LINEAR_RGB							\
    290    sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
    291    sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
    292    sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
    293    sample[ACOMP] = CHAN_MAX;
    294 
    295 #define NEAREST_RGBA  \
    296    sample[RCOMP] = tex00[3];	\
    297    sample[GCOMP] = tex00[2];	\
    298    sample[BCOMP] = tex00[1];	\
    299    sample[ACOMP] = tex00[0];
    300 
    301 #define LINEAR_RGBA							\
    302    sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
    303    sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
    304    sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
    305    sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
    306 
    307 #define MODULATE							  \
    308    dest[RCOMP] = span->red   * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
    309    dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
    310    dest[BCOMP] = span->blue  * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
    311    dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
    312 
    313 #define DECAL								\
    314    dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red +		\
    315                ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT))	\
    316                >> (FIXED_SHIFT + 8);					\
    317    dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green +		\
    318                ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT))	\
    319                >> (FIXED_SHIFT + 8);					\
    320    dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue +		\
    321                ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT))	\
    322                >> (FIXED_SHIFT + 8);					\
    323    dest[ACOMP] = FixedToInt(span->alpha)
    324 
    325 #define BLEND								\
    326    dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red		\
    327                + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8);	\
    328    dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green		\
    329                + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8);	\
    330    dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue		\
    331                + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8);	\
    332    dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
    333 
    334 #define REPLACE  COPY_CHAN4(dest, sample)
    335 
    336 #define ADD								\
    337    {									\
    338       GLint rSum = FixedToInt(span->red)   + (GLint) sample[RCOMP];	\
    339       GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP];	\
    340       GLint bSum = FixedToInt(span->blue)  + (GLint) sample[BCOMP];	\
    341       dest[RCOMP] = MIN2(rSum, CHAN_MAX);				\
    342       dest[GCOMP] = MIN2(gSum, CHAN_MAX);				\
    343       dest[BCOMP] = MIN2(bSum, CHAN_MAX);				\
    344       dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
    345   }
    346 
    347 /* shortcuts */
    348 
    349 #define NEAREST_RGB_REPLACE		\
    350    NEAREST_RGB;				\
    351    dest[0] = sample[0];			\
    352    dest[1] = sample[1];			\
    353    dest[2] = sample[2];			\
    354    dest[3] = FixedToInt(span->alpha);
    355 
    356 #define NEAREST_RGBA_REPLACE  \
    357    dest[RCOMP] = tex00[3]; \
    358    dest[GCOMP] = tex00[2]; \
    359    dest[BCOMP] = tex00[1]; \
    360    dest[ACOMP] = tex00[0]
    361 
    362 #define SPAN_NEAREST(DO_TEX, COMPS)					\
    363 	for (i = 0; i < span->end; i++) {				\
    364            /* Isn't it necessary to use FixedFloor below?? */		\
    365            GLint s = FixedToInt(span->intTex[0]) & info->smask;		\
    366            GLint t = FixedToInt(span->intTex[1]) & info->tmask;		\
    367            GLint pos = (t << info->twidth_log2) + s;			\
    368            const GLchan *tex00 = info->texture + COMPS * pos;		\
    369            DO_TEX;							\
    370            span->red += span->redStep;					\
    371 	   span->green += span->greenStep;				\
    372            span->blue += span->blueStep;				\
    373 	   span->alpha += span->alphaStep;				\
    374 	   span->intTex[0] += span->intTexStep[0];			\
    375 	   span->intTex[1] += span->intTexStep[1];			\
    376            dest += 4;							\
    377 	}
    378 
    379 #define SPAN_LINEAR(DO_TEX, COMPS)					\
    380 	for (i = 0; i < span->end; i++) {				\
    381            /* Isn't it necessary to use FixedFloor below?? */		\
    382            const GLint s = FixedToInt(span->intTex[0]) & info->smask;	\
    383            const GLint t = FixedToInt(span->intTex[1]) & info->tmask;	\
    384            const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK;	\
    385            const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK;	\
    386            const GLint pos = (t << info->twidth_log2) + s;		\
    387            const GLchan *tex00 = info->texture + COMPS * pos;		\
    388            const GLchan *tex10 = tex00 + info->tbytesline;		\
    389            const GLchan *tex01 = tex00 + COMPS;				\
    390            const GLchan *tex11 = tex10 + COMPS;				\
    391            if (t == info->tmask) {					\
    392               tex10 -= info->tsize;					\
    393               tex11 -= info->tsize;					\
    394            }								\
    395            if (s == info->smask) {					\
    396               tex01 -= info->tbytesline;				\
    397               tex11 -= info->tbytesline;				\
    398            }								\
    399            DO_TEX;							\
    400            span->red += span->redStep;					\
    401 	   span->green += span->greenStep;				\
    402            span->blue += span->blueStep;				\
    403 	   span->alpha += span->alphaStep;				\
    404 	   span->intTex[0] += span->intTexStep[0];			\
    405 	   span->intTex[1] += span->intTexStep[1];			\
    406            dest += 4;							\
    407 	}
    408 
    409 
    410    GLuint i;
    411    GLchan *dest = span->array->rgba[0];
    412 
    413    /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
    414    ctx->Texture._EnabledCoordUnits = 0x0;
    415 
    416    span->intTex[0] -= FIXED_HALF;
    417    span->intTex[1] -= FIXED_HALF;
    418    switch (info->filter) {
    419    case GL_NEAREST:
    420       switch (info->format) {
    421       case MESA_FORMAT_BGR_UNORM8:
    422          switch (info->envmode) {
    423          case GL_MODULATE:
    424             SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
    425             break;
    426          case GL_DECAL:
    427          case GL_REPLACE:
    428             SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
    429             break;
    430          case GL_BLEND:
    431             SPAN_NEAREST(NEAREST_RGB;BLEND,3);
    432             break;
    433          case GL_ADD:
    434             SPAN_NEAREST(NEAREST_RGB;ADD,3);
    435             break;
    436          default:
    437             _mesa_problem(ctx, "bad tex env mode in SPAN_LINEAR");
    438             return;
    439          }
    440          break;
    441       case MESA_FORMAT_A8B8G8R8_UNORM:
    442          switch(info->envmode) {
    443          case GL_MODULATE:
    444             SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
    445             break;
    446          case GL_DECAL:
    447             SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
    448             break;
    449          case GL_BLEND:
    450             SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
    451             break;
    452          case GL_ADD:
    453             SPAN_NEAREST(NEAREST_RGBA;ADD,4);
    454             break;
    455          case GL_REPLACE:
    456             SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
    457             break;
    458          default:
    459             _mesa_problem(ctx, "bad tex env mode (2) in SPAN_LINEAR");
    460             return;
    461          }
    462          break;
    463       }
    464       break;
    465 
    466    case GL_LINEAR:
    467       span->intTex[0] -= FIXED_HALF;
    468       span->intTex[1] -= FIXED_HALF;
    469       switch (info->format) {
    470       case MESA_FORMAT_BGR_UNORM8:
    471          switch (info->envmode) {
    472          case GL_MODULATE:
    473             SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
    474             break;
    475          case GL_DECAL:
    476          case GL_REPLACE:
    477             SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
    478             break;
    479          case GL_BLEND:
    480             SPAN_LINEAR(LINEAR_RGB;BLEND,3);
    481             break;
    482          case GL_ADD:
    483             SPAN_LINEAR(LINEAR_RGB;ADD,3);
    484             break;
    485          default:
    486             _mesa_problem(ctx, "bad tex env mode (3) in SPAN_LINEAR");
    487             return;
    488          }
    489          break;
    490       case MESA_FORMAT_A8B8G8R8_UNORM:
    491          switch (info->envmode) {
    492          case GL_MODULATE:
    493             SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
    494             break;
    495          case GL_DECAL:
    496             SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
    497             break;
    498          case GL_BLEND:
    499             SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
    500             break;
    501          case GL_ADD:
    502             SPAN_LINEAR(LINEAR_RGBA;ADD,4);
    503             break;
    504          case GL_REPLACE:
    505             SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
    506             break;
    507          default:
    508             _mesa_problem(ctx, "bad tex env mode (4) in SPAN_LINEAR");
    509             return;
    510          }
    511          break;
    512       }
    513       break;
    514    }
    515    span->interpMask &= ~SPAN_RGBA;
    516    assert(span->arrayMask & SPAN_RGBA);
    517 
    518    _swrast_write_rgba_span(ctx, span);
    519 
    520    /* re-enable texture units */
    521    ctx->Texture._EnabledCoordUnits = texEnableSave;
    522 
    523 #undef SPAN_NEAREST
    524 #undef SPAN_LINEAR
    525 }
    526 
    527 
    528 
    529 /*
    530  * Render an RGB/RGBA textured triangle without perspective correction.
    531  */
    532 #define NAME affine_textured_triangle
    533 #define INTERP_Z 1
    534 #define INTERP_RGB 1
    535 #define INTERP_ALPHA 1
    536 #define INTERP_INT_TEX 1
    537 #define S_SCALE twidth
    538 #define T_SCALE theight
    539 
    540 #define SETUP_CODE							\
    541    struct affine_info info;						\
    542    struct gl_texture_unit *unit = ctx->Texture.Unit+0;			\
    543    const struct gl_texture_object *obj = 				\
    544       ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
    545    const struct gl_texture_image *texImg = 				\
    546       _mesa_base_tex_image(obj);					\
    547    const struct swrast_texture_image *swImg =				\
    548       swrast_texture_image_const(texImg);				\
    549    const GLfloat twidth = (GLfloat) texImg->Width;			\
    550    const GLfloat theight = (GLfloat) texImg->Height;			\
    551    info.texture = (const GLchan *) swImg->ImageSlices[0];		\
    552    info.twidth_log2 = texImg->WidthLog2;				\
    553    info.smask = texImg->Width - 1;					\
    554    info.tmask = texImg->Height - 1;					\
    555    info.format = texImg->TexFormat;					\
    556    info.filter = obj->Sampler.MinFilter;				\
    557    info.envmode = unit->EnvMode;					\
    558    info.er = 0;					\
    559    info.eg = 0;					\
    560    info.eb = 0;					\
    561    span.arrayMask |= SPAN_RGBA;						\
    562 									\
    563    if (info.envmode == GL_BLEND) {					\
    564       /* potential off-by-one error here? (1.0f -> 2048 -> 0) */	\
    565       info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF);	\
    566       info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF);	\
    567       info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF);	\
    568       info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF);	\
    569    }									\
    570    if (!info.texture) {							\
    571       /* this shouldn't happen */					\
    572       return;								\
    573    }									\
    574 									\
    575    switch (info.format) {						\
    576    case MESA_FORMAT_BGR_UNORM8:						\
    577       info.tbytesline = texImg->Width * 3;				\
    578       break;								\
    579    case MESA_FORMAT_A8B8G8R8_UNORM:						\
    580       info.tbytesline = texImg->Width * 4;				\
    581       break;								\
    582    default:								\
    583       _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
    584       return;								\
    585    }									\
    586    info.tsize = texImg->Height * info.tbytesline;
    587 
    588 #define RENDER_SPAN( span )   affine_span(ctx, &span, &info);
    589 
    590 #include "s_tritemp.h"
    591 
    592 
    593 
    594 struct persp_info
    595 {
    596    GLenum filter;
    597    GLenum format;
    598    GLenum envmode;
    599    GLint smask, tmask;
    600    GLint twidth_log2;
    601    const GLchan *texture;
    602    GLfixed er, eg, eb, ea;   /* texture env color */
    603    GLint tbytesline, tsize;
    604 };
    605 
    606 
    607 static inline void
    608 fast_persp_span(struct gl_context *ctx, SWspan *span,
    609 		struct persp_info *info)
    610 {
    611    GLchan sample[4];  /* the filtered texture sample */
    612 
    613   /* Instead of defining a function for each mode, a test is done
    614    * between the outer and inner loops. This is to reduce code size
    615    * and complexity. Observe that an optimizing compiler kills
    616    * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
    617    */
    618 #define SPAN_NEAREST(DO_TEX,COMP)					\
    619 	for (i = 0; i < span->end; i++) {				\
    620            GLdouble invQ = tex_coord[2] ?				\
    621                                  (1.0 / tex_coord[2]) : 1.0;            \
    622            GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);		\
    623            GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);		\
    624            GLint s = IFLOOR(s_tmp) & info->smask;	        	\
    625            GLint t = IFLOOR(t_tmp) & info->tmask;	        	\
    626            GLint pos = (t << info->twidth_log2) + s;			\
    627            const GLchan *tex00 = info->texture + COMP * pos;		\
    628            DO_TEX;							\
    629            span->red += span->redStep;					\
    630 	   span->green += span->greenStep;				\
    631            span->blue += span->blueStep;				\
    632 	   span->alpha += span->alphaStep;				\
    633 	   tex_coord[0] += tex_step[0];					\
    634 	   tex_coord[1] += tex_step[1];					\
    635 	   tex_coord[2] += tex_step[2];					\
    636            dest += 4;							\
    637 	}
    638 
    639 #define SPAN_LINEAR(DO_TEX,COMP)					\
    640 	for (i = 0; i < span->end; i++) {				\
    641            GLdouble invQ = tex_coord[2] ?				\
    642                                  (1.0 / tex_coord[2]) : 1.0;            \
    643            const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ);	\
    644            const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ);	\
    645            const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF;	\
    646            const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF;      \
    647            const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask;	\
    648            const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask;	\
    649            const GLfixed sf = s_fix & FIXED_FRAC_MASK;			\
    650            const GLfixed tf = t_fix & FIXED_FRAC_MASK;			\
    651            const GLint pos = (t << info->twidth_log2) + s;		\
    652            const GLchan *tex00 = info->texture + COMP * pos;		\
    653            const GLchan *tex10 = tex00 + info->tbytesline;		\
    654            const GLchan *tex01 = tex00 + COMP;				\
    655            const GLchan *tex11 = tex10 + COMP;				\
    656            if (t == info->tmask) {					\
    657               tex10 -= info->tsize;					\
    658               tex11 -= info->tsize;					\
    659            }								\
    660            if (s == info->smask) {					\
    661               tex01 -= info->tbytesline;				\
    662               tex11 -= info->tbytesline;				\
    663            }								\
    664            DO_TEX;							\
    665            span->red   += span->redStep;				\
    666 	   span->green += span->greenStep;				\
    667            span->blue  += span->blueStep;				\
    668 	   span->alpha += span->alphaStep;				\
    669 	   tex_coord[0] += tex_step[0];					\
    670 	   tex_coord[1] += tex_step[1];					\
    671 	   tex_coord[2] += tex_step[2];					\
    672            dest += 4;							\
    673 	}
    674 
    675    GLuint i;
    676    GLfloat tex_coord[3], tex_step[3];
    677    GLchan *dest = span->array->rgba[0];
    678 
    679    const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
    680    ctx->Texture._EnabledCoordUnits = 0;
    681 
    682    tex_coord[0] = span->attrStart[VARYING_SLOT_TEX0][0]  * (info->smask + 1);
    683    tex_step[0] = span->attrStepX[VARYING_SLOT_TEX0][0] * (info->smask + 1);
    684    tex_coord[1] = span->attrStart[VARYING_SLOT_TEX0][1] * (info->tmask + 1);
    685    tex_step[1] = span->attrStepX[VARYING_SLOT_TEX0][1] * (info->tmask + 1);
    686    /* span->attrStart[VARYING_SLOT_TEX0][2] only if 3D-texturing, here only 2D */
    687    tex_coord[2] = span->attrStart[VARYING_SLOT_TEX0][3];
    688    tex_step[2] = span->attrStepX[VARYING_SLOT_TEX0][3];
    689 
    690    switch (info->filter) {
    691    case GL_NEAREST:
    692       switch (info->format) {
    693       case MESA_FORMAT_BGR_UNORM8:
    694          switch (info->envmode) {
    695          case GL_MODULATE:
    696             SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
    697             break;
    698          case GL_DECAL:
    699          case GL_REPLACE:
    700             SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
    701             break;
    702          case GL_BLEND:
    703             SPAN_NEAREST(NEAREST_RGB;BLEND,3);
    704             break;
    705          case GL_ADD:
    706             SPAN_NEAREST(NEAREST_RGB;ADD,3);
    707             break;
    708          default:
    709             _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR");
    710             return;
    711          }
    712          break;
    713       case MESA_FORMAT_A8B8G8R8_UNORM:
    714          switch(info->envmode) {
    715          case GL_MODULATE:
    716             SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
    717             break;
    718          case GL_DECAL:
    719             SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
    720             break;
    721          case GL_BLEND:
    722             SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
    723             break;
    724          case GL_ADD:
    725             SPAN_NEAREST(NEAREST_RGBA;ADD,4);
    726             break;
    727          case GL_REPLACE:
    728             SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
    729             break;
    730          default:
    731             _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR");
    732             return;
    733          }
    734          break;
    735       }
    736       break;
    737 
    738    case GL_LINEAR:
    739       switch (info->format) {
    740       case MESA_FORMAT_BGR_UNORM8:
    741          switch (info->envmode) {
    742          case GL_MODULATE:
    743             SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
    744             break;
    745          case GL_DECAL:
    746          case GL_REPLACE:
    747             SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
    748             break;
    749          case GL_BLEND:
    750             SPAN_LINEAR(LINEAR_RGB;BLEND,3);
    751             break;
    752          case GL_ADD:
    753             SPAN_LINEAR(LINEAR_RGB;ADD,3);
    754             break;
    755          default:
    756             _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR");
    757             return;
    758          }
    759          break;
    760       case MESA_FORMAT_A8B8G8R8_UNORM:
    761          switch (info->envmode) {
    762          case GL_MODULATE:
    763             SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
    764             break;
    765          case GL_DECAL:
    766             SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
    767             break;
    768          case GL_BLEND:
    769             SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
    770             break;
    771          case GL_ADD:
    772             SPAN_LINEAR(LINEAR_RGBA;ADD,4);
    773             break;
    774          case GL_REPLACE:
    775             SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
    776             break;
    777          default:
    778             _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR");
    779             return;
    780          }
    781          break;
    782       }
    783       break;
    784    }
    785 
    786    assert(span->arrayMask & SPAN_RGBA);
    787    _swrast_write_rgba_span(ctx, span);
    788 
    789 #undef SPAN_NEAREST
    790 #undef SPAN_LINEAR
    791 
    792    /* restore state */
    793    ctx->Texture._EnabledCoordUnits = texEnableSave;
    794 }
    795 
    796 
    797 /*
    798  * Render an perspective corrected RGB/RGBA textured triangle.
    799  * The Q (aka V in Mesa) coordinate must be zero such that the divide
    800  * by interpolated Q/W comes out right.
    801  *
    802  */
    803 #define NAME persp_textured_triangle
    804 #define INTERP_Z 1
    805 #define INTERP_RGB 1
    806 #define INTERP_ALPHA 1
    807 #define INTERP_ATTRIBS 1
    808 
    809 #define SETUP_CODE							\
    810    struct persp_info info;						\
    811    const struct gl_texture_unit *unit = ctx->Texture.Unit+0;		\
    812    const struct gl_texture_object *obj = 				\
    813       ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];		\
    814    const struct gl_texture_image *texImg = 				\
    815       _mesa_base_tex_image(obj);					\
    816    const struct swrast_texture_image *swImg =				\
    817       swrast_texture_image_const(texImg);				\
    818    info.texture = (const GLchan *) swImg->ImageSlices[0];		\
    819    info.twidth_log2 = texImg->WidthLog2;				\
    820    info.smask = texImg->Width - 1;					\
    821    info.tmask = texImg->Height - 1;					\
    822    info.format = texImg->TexFormat;					\
    823    info.filter = obj->Sampler.MinFilter;				\
    824    info.envmode = unit->EnvMode;					\
    825    info.er = 0;					\
    826    info.eg = 0;					\
    827    info.eb = 0;					\
    828 									\
    829    if (info.envmode == GL_BLEND) {					\
    830       /* potential off-by-one error here? (1.0f -> 2048 -> 0) */	\
    831       info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF);	\
    832       info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF);	\
    833       info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF);	\
    834       info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF);	\
    835    }									\
    836    if (!info.texture) {							\
    837       /* this shouldn't happen */					\
    838       return;								\
    839    }									\
    840 									\
    841    switch (info.format) {						\
    842    case MESA_FORMAT_BGR_UNORM8:						\
    843       info.tbytesline = texImg->Width * 3;				\
    844       break;								\
    845    case MESA_FORMAT_A8B8G8R8_UNORM:						\
    846       info.tbytesline = texImg->Width * 4;				\
    847       break;								\
    848    default:								\
    849       _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
    850       return;								\
    851    }									\
    852    info.tsize = texImg->Height * info.tbytesline;
    853 
    854 #define RENDER_SPAN( span )			\
    855    span.interpMask &= ~SPAN_RGBA;		\
    856    span.arrayMask |= SPAN_RGBA;			\
    857    fast_persp_span(ctx, &span, &info);
    858 
    859 #include "s_tritemp.h"
    860 
    861 #endif /*CHAN_TYPE != GL_FLOAT*/
    862 
    863 
    864 
    865 /*
    866  * Render an RGBA triangle with arbitrary attributes.
    867  */
    868 #define NAME general_triangle
    869 #define INTERP_Z 1
    870 #define INTERP_RGB 1
    871 #define INTERP_ALPHA 1
    872 #define INTERP_ATTRIBS 1
    873 #define RENDER_SPAN( span )   _swrast_write_rgba_span(ctx, &span);
    874 #include "s_tritemp.h"
    875 
    876 
    877 
    878 
    879 /*
    880  * Special tri function for occlusion testing
    881  */
    882 #define NAME occlusion_zless_16_triangle
    883 #define INTERP_Z 1
    884 #define SETUP_CODE							\
    885    struct gl_renderbuffer *rb =                                         \
    886       ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;           \
    887    struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;	\
    888    assert(ctx->Depth.Test);						\
    889    assert(!ctx->Depth.Mask);						\
    890    assert(ctx->Depth.Func == GL_LESS);					\
    891    assert(rb->Format == MESA_FORMAT_Z_UNORM16);                               \
    892    if (!q) {								\
    893       return;								\
    894    }
    895 #define RENDER_SPAN( span )						\
    896    {                                                                    \
    897       GLuint i;								\
    898       const GLushort *zRow = (const GLushort *)				\
    899          _swrast_pixel_address(rb, span.x, span.y);                     \
    900       for (i = 0; i < span.end; i++) {					\
    901          GLuint z = FixedToDepth(span.z);				\
    902          if (z < zRow[i]) {						\
    903             q->Result++;						\
    904          }								\
    905          span.z += span.zStep;						\
    906       }									\
    907    }
    908 #include "s_tritemp.h"
    909 
    910 
    911 
    912 static void
    913 nodraw_triangle( struct gl_context *ctx,
    914                  const SWvertex *v0,
    915                  const SWvertex *v1,
    916                  const SWvertex *v2 )
    917 {
    918    (void) (ctx && v0 && v1 && v2);
    919 }
    920 
    921 
    922 /*
    923  * This is used when separate specular color is enabled, but not
    924  * texturing.  We add the specular color to the primary color,
    925  * draw the triangle, then restore the original primary color.
    926  * Inefficient, but seldom needed.
    927  */
    928 void
    929 _swrast_add_spec_terms_triangle(struct gl_context *ctx, const SWvertex *v0,
    930                                 const SWvertex *v1, const SWvertex *v2)
    931 {
    932    SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
    933    SWvertex *ncv1 = (SWvertex *)v1;
    934    SWvertex *ncv2 = (SWvertex *)v2;
    935    GLfloat rSum, gSum, bSum;
    936    GLchan cSave[3][4];
    937 
    938    /* save original colors */
    939    COPY_CHAN4( cSave[0], ncv0->color );
    940    COPY_CHAN4( cSave[1], ncv1->color );
    941    COPY_CHAN4( cSave[2], ncv2->color );
    942    /* sum v0 */
    943    rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[VARYING_SLOT_COL1][0];
    944    gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[VARYING_SLOT_COL1][1];
    945    bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[VARYING_SLOT_COL1][2];
    946    UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
    947    UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
    948    UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
    949    /* sum v1 */
    950    rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[VARYING_SLOT_COL1][0];
    951    gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[VARYING_SLOT_COL1][1];
    952    bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[VARYING_SLOT_COL1][2];
    953    UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
    954    UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
    955    UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
    956    /* sum v2 */
    957    rSum = CHAN_TO_FLOAT(ncv2->color[0]) + ncv2->attrib[VARYING_SLOT_COL1][0];
    958    gSum = CHAN_TO_FLOAT(ncv2->color[1]) + ncv2->attrib[VARYING_SLOT_COL1][1];
    959    bSum = CHAN_TO_FLOAT(ncv2->color[2]) + ncv2->attrib[VARYING_SLOT_COL1][2];
    960    UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[0], rSum);
    961    UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[1], gSum);
    962    UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[2], bSum);
    963    /* draw */
    964    SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
    965    /* restore original colors */
    966    COPY_CHAN4( ncv0->color, cSave[0] );
    967    COPY_CHAN4( ncv1->color, cSave[1] );
    968    COPY_CHAN4( ncv2->color, cSave[2] );
    969 }
    970 
    971 
    972 
    973 #ifdef DEBUG
    974 
    975 /* record the current triangle function name */
    976 const char *_mesa_triFuncName = NULL;
    977 
    978 #define USE(triFunc)				\
    979 do {						\
    980     _mesa_triFuncName = #triFunc;		\
    981     /*printf("%s\n", _mesa_triFuncName);*/	\
    982     swrast->Triangle = triFunc;			\
    983 } while (0)
    984 
    985 #else
    986 
    987 #define USE(triFunc)  swrast->Triangle = triFunc;
    988 
    989 #endif
    990 
    991 
    992 
    993 
    994 /*
    995  * Determine which triangle rendering function to use given the current
    996  * rendering context.
    997  *
    998  * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
    999  * remove tests to this code.
   1000  */
   1001 void
   1002 _swrast_choose_triangle( struct gl_context *ctx )
   1003 {
   1004    SWcontext *swrast = SWRAST_CONTEXT(ctx);
   1005 
   1006    if (ctx->Polygon.CullFlag &&
   1007        ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
   1008       USE(nodraw_triangle);
   1009       return;
   1010    }
   1011 
   1012    if (ctx->RenderMode==GL_RENDER) {
   1013       struct gl_renderbuffer *depthRb =
   1014          ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
   1015 
   1016       if (ctx->Polygon.SmoothFlag) {
   1017          _swrast_set_aa_triangle_function(ctx);
   1018          assert(swrast->Triangle);
   1019          return;
   1020       }
   1021 
   1022       /* special case for occlusion testing */
   1023       if (ctx->Query.CurrentOcclusionObject &&
   1024           ctx->Depth.Test &&
   1025           ctx->Depth.Mask == GL_FALSE &&
   1026           ctx->Depth.Func == GL_LESS &&
   1027           !_mesa_stencil_is_enabled(ctx) &&
   1028           depthRb &&
   1029           depthRb->Format == MESA_FORMAT_Z_UNORM16) {
   1030          if (ctx->Color.ColorMask[0][0] == 0 &&
   1031 	     ctx->Color.ColorMask[0][1] == 0 &&
   1032 	     ctx->Color.ColorMask[0][2] == 0 &&
   1033 	     ctx->Color.ColorMask[0][3] == 0) {
   1034             USE(occlusion_zless_16_triangle);
   1035             return;
   1036          }
   1037       }
   1038 
   1039       /*
   1040        * XXX should examine swrast->_ActiveAttribMask to determine what
   1041        * needs to be interpolated.
   1042        */
   1043       if (ctx->Texture._EnabledCoordUnits ||
   1044 	  _swrast_use_fragment_program(ctx) ||
   1045           _mesa_ati_fragment_shader_enabled(ctx) ||
   1046           _mesa_need_secondary_color(ctx) ||
   1047           swrast->_FogEnabled) {
   1048          /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
   1049          const struct gl_texture_object *texObj2D;
   1050          const struct gl_sampler_object *samp;
   1051          const struct gl_texture_image *texImg;
   1052          const struct swrast_texture_image *swImg;
   1053          GLenum minFilter, magFilter, envMode;
   1054          mesa_format format;
   1055          texObj2D = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
   1056          if (ctx->Texture.Unit[0].Sampler)
   1057             samp = ctx->Texture.Unit[0].Sampler;
   1058          else if (texObj2D)
   1059             samp = &texObj2D->Sampler;
   1060          else
   1061             samp = NULL;
   1062 
   1063          texImg = texObj2D ? _mesa_base_tex_image(texObj2D) : NULL;
   1064          swImg = swrast_texture_image_const(texImg);
   1065 
   1066          format = texImg ? texImg->TexFormat : MESA_FORMAT_NONE;
   1067          minFilter = texObj2D ? samp->MinFilter : GL_NONE;
   1068          magFilter = texObj2D ? samp->MagFilter : GL_NONE;
   1069          envMode = ctx->Texture.Unit[0].EnvMode;
   1070 
   1071          /* First see if we can use an optimized 2-D texture function */
   1072          if (ctx->Texture._EnabledCoordUnits == 0x1
   1073              && !_swrast_use_fragment_program(ctx)
   1074              && !_mesa_ati_fragment_shader_enabled(ctx)
   1075              && ctx->Texture._MaxEnabledTexImageUnit == 0
   1076              && ctx->Texture.Unit[0]._Current->Target == GL_TEXTURE_2D
   1077              && samp->WrapS == GL_REPEAT
   1078              && samp->WrapT == GL_REPEAT
   1079              && texObj2D->_Swizzle == SWIZZLE_NOOP
   1080              && swImg->_IsPowerOfTwo
   1081              && texImg->Border == 0
   1082              && (_mesa_format_row_stride(format, texImg->Width) ==
   1083                  swImg->RowStride)
   1084              && (format == MESA_FORMAT_BGR_UNORM8 || format == MESA_FORMAT_A8B8G8R8_UNORM)
   1085              && minFilter == magFilter
   1086              && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
   1087              && !swrast->_FogEnabled
   1088              && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT
   1089              && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
   1090 	    if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
   1091 	       if (minFilter == GL_NEAREST
   1092 		   && format == MESA_FORMAT_BGR_UNORM8
   1093 		   && (envMode == GL_REPLACE || envMode == GL_DECAL)
   1094 		   && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
   1095 			&& ctx->Depth.Func == GL_LESS
   1096 			&& ctx->Depth.Mask == GL_TRUE)
   1097 		       || swrast->_RasterMask == TEXTURE_BIT)
   1098 		   && ctx->Polygon.StippleFlag == GL_FALSE
   1099                    && ctx->DrawBuffer->Visual.depthBits <= 16) {
   1100 		  if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
   1101 		     USE(simple_z_textured_triangle);
   1102 		  }
   1103 		  else {
   1104 		     USE(simple_textured_triangle);
   1105 		  }
   1106 	       }
   1107 	       else {
   1108 #if CHAN_BITS != 8
   1109                   USE(general_triangle);
   1110 #else
   1111                   if (format == MESA_FORMAT_A8B8G8R8_UNORM && !_mesa_little_endian()) {
   1112                      /* We only handle RGBA8888 correctly on little endian
   1113                       * in the optimized code above.
   1114                       */
   1115                      USE(general_triangle);
   1116                   }
   1117                   else {
   1118                      USE(affine_textured_triangle);
   1119                  }
   1120 #endif
   1121 	       }
   1122 	    }
   1123 	    else {
   1124 #if CHAN_BITS != 8
   1125                USE(general_triangle);
   1126 #else
   1127                USE(persp_textured_triangle);
   1128 #endif
   1129 	    }
   1130 	 }
   1131          else {
   1132             /* general case textured triangles */
   1133             USE(general_triangle);
   1134          }
   1135       }
   1136       else {
   1137          assert(!swrast->_FogEnabled);
   1138          assert(!_mesa_need_secondary_color(ctx));
   1139 	 if (ctx->Light.ShadeModel==GL_SMOOTH) {
   1140 	    /* smooth shaded, no texturing, stippled or some raster ops */
   1141 #if CHAN_BITS != 8
   1142                USE(general_triangle);
   1143 #else
   1144                USE(smooth_rgba_triangle);
   1145 #endif
   1146 	 }
   1147 	 else {
   1148 	    /* flat shaded, no texturing, stippled or some raster ops */
   1149 #if CHAN_BITS != 8
   1150             USE(general_triangle);
   1151 #else
   1152             USE(flat_rgba_triangle);
   1153 #endif
   1154 	 }
   1155       }
   1156    }
   1157    else if (ctx->RenderMode==GL_FEEDBACK) {
   1158       USE(_swrast_feedback_triangle);
   1159    }
   1160    else {
   1161       /* GL_SELECT mode */
   1162       USE(_swrast_select_triangle);
   1163    }
   1164 }
   1165