Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  * Version:  7.5
  4.  *
  5.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  6.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a
  9.  * copy of this software and associated documentation files (the "Software"),
  10.  * to deal in the Software without restriction, including without limitation
  11.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12.  * and/or sell copies of the Software, and to permit persons to whom the
  13.  * Software is furnished to do so, subject to the following conditions:
  14.  *
  15.  * The above copyright notice and this permission notice shall be included
  16.  * in all copies or substantial portions of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  21.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26.  
  27. /**
  28.  * \file swrast/s_span.c
  29.  * \brief Span processing functions used by all rasterization functions.
  30.  * This is where all the per-fragment tests are performed
  31.  * \author Brian Paul
  32.  */
  33.  
  34. #include "main/glheader.h"
  35. #include "main/colormac.h"
  36. #include "main/macros.h"
  37. #include "main/imports.h"
  38. #include "main/image.h"
  39.  
  40. #include "s_atifragshader.h"
  41. #include "s_alpha.h"
  42. #include "s_blend.h"
  43. #include "s_context.h"
  44. #include "s_depth.h"
  45. #include "s_fog.h"
  46. #include "s_logic.h"
  47. #include "s_masking.h"
  48. #include "s_fragprog.h"
  49. #include "s_span.h"
  50. #include "s_stencil.h"
  51. #include "s_texcombine.h"
  52.  
  53.  
  54. /**
  55.  * Set default fragment attributes for the span using the
  56.  * current raster values.  Used prior to glDraw/CopyPixels
  57.  * and glBitmap.
  58.  */
  59. void
  60. _swrast_span_default_attribs(struct gl_context *ctx, SWspan *span)
  61. {
  62.    GLchan r, g, b, a;
  63.    /* Z*/
  64.    {
  65.       const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
  66.       if (ctx->DrawBuffer->Visual.depthBits <= 16)
  67.          span->z = FloatToFixed(ctx->Current.RasterPos[2] * depthMax + 0.5F);
  68.       else {
  69.          GLfloat tmpf = ctx->Current.RasterPos[2] * depthMax;
  70.          tmpf = MIN2(tmpf, depthMax);
  71.          span->z = (GLint)tmpf;
  72.       }
  73.       span->zStep = 0;
  74.       span->interpMask |= SPAN_Z;
  75.    }
  76.  
  77.    /* W (for perspective correction) */
  78.    span->attrStart[FRAG_ATTRIB_WPOS][3] = 1.0;
  79.    span->attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0;
  80.    span->attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0;
  81.  
  82.    /* primary color, or color index */
  83.    UNCLAMPED_FLOAT_TO_CHAN(r, ctx->Current.RasterColor[0]);
  84.    UNCLAMPED_FLOAT_TO_CHAN(g, ctx->Current.RasterColor[1]);
  85.    UNCLAMPED_FLOAT_TO_CHAN(b, ctx->Current.RasterColor[2]);
  86.    UNCLAMPED_FLOAT_TO_CHAN(a, ctx->Current.RasterColor[3]);
  87. #if CHAN_TYPE == GL_FLOAT
  88.    span->red = r;
  89.    span->green = g;
  90.    span->blue = b;
  91.    span->alpha = a;
  92. #else
  93.    span->red   = IntToFixed(r);
  94.    span->green = IntToFixed(g);
  95.    span->blue  = IntToFixed(b);
  96.    span->alpha = IntToFixed(a);
  97. #endif
  98.    span->redStep = 0;
  99.    span->greenStep = 0;
  100.    span->blueStep = 0;
  101.    span->alphaStep = 0;
  102.    span->interpMask |= SPAN_RGBA;
  103.  
  104.    COPY_4V(span->attrStart[FRAG_ATTRIB_COL0], ctx->Current.RasterColor);
  105.    ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
  106.    ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
  107.  
  108.    /* Secondary color */
  109.    if (ctx->Light.Enabled || ctx->Fog.ColorSumEnabled)
  110.    {
  111.       COPY_4V(span->attrStart[FRAG_ATTRIB_COL1], ctx->Current.RasterSecondaryColor);
  112.       ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL1], 0.0, 0.0, 0.0, 0.0);
  113.       ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL1], 0.0, 0.0, 0.0, 0.0);
  114.    }
  115.  
  116.    /* fog */
  117.    {
  118.       const SWcontext *swrast = SWRAST_CONTEXT(ctx);
  119.       GLfloat fogVal; /* a coord or a blend factor */
  120.       if (swrast->_PreferPixelFog) {
  121.          /* fog blend factors will be computed from fog coordinates per pixel */
  122.          fogVal = ctx->Current.RasterDistance;
  123.       }
  124.       else {
  125.          /* fog blend factor should be computed from fogcoord now */
  126.          fogVal = _swrast_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
  127.       }
  128.       span->attrStart[FRAG_ATTRIB_FOGC][0] = fogVal;
  129.       span->attrStepX[FRAG_ATTRIB_FOGC][0] = 0.0;
  130.       span->attrStepY[FRAG_ATTRIB_FOGC][0] = 0.0;
  131.    }
  132.  
  133.    /* texcoords */
  134.    {
  135.       GLuint i;
  136.       for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
  137.          const GLuint attr = FRAG_ATTRIB_TEX0 + i;
  138.          const GLfloat *tc = ctx->Current.RasterTexCoords[i];
  139.          if (ctx->FragmentProgram._Current || ctx->ATIFragmentShader._Enabled) {
  140.             COPY_4V(span->attrStart[attr], tc);
  141.          }
  142.          else if (tc[3] > 0.0F) {
  143.             /* use (s/q, t/q, r/q, 1) */
  144.             span->attrStart[attr][0] = tc[0] / tc[3];
  145.             span->attrStart[attr][1] = tc[1] / tc[3];
  146.             span->attrStart[attr][2] = tc[2] / tc[3];
  147.             span->attrStart[attr][3] = 1.0;
  148.          }
  149.          else {
  150.             ASSIGN_4V(span->attrStart[attr], 0.0F, 0.0F, 0.0F, 1.0F);
  151.          }
  152.          ASSIGN_4V(span->attrStepX[attr], 0.0F, 0.0F, 0.0F, 0.0F);
  153.          ASSIGN_4V(span->attrStepY[attr], 0.0F, 0.0F, 0.0F, 0.0F);
  154.       }
  155.    }
  156. }
  157.  
  158.  
  159. /**
  160.  * Interpolate the active attributes (and'd with attrMask) to
  161.  * fill in span->array->attribs[].
  162.  * Perspective correction will be done.  The point/line/triangle function
  163.  * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
  164.  */
  165. static INLINE void
  166. interpolate_active_attribs(struct gl_context *ctx, SWspan *span, GLbitfield attrMask)
  167. {
  168.    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
  169.  
  170.    /*
  171.     * Don't overwrite existing array values, such as colors that may have
  172.     * been produced by glDraw/CopyPixels.
  173.     */
  174.    attrMask &= ~span->arrayAttribs;
  175.  
  176.    ATTRIB_LOOP_BEGIN
  177.       if (attrMask & (1 << attr)) {
  178.          const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
  179.          GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3];
  180.          const GLfloat dv0dx = span->attrStepX[attr][0];
  181.          const GLfloat dv1dx = span->attrStepX[attr][1];
  182.          const GLfloat dv2dx = span->attrStepX[attr][2];
  183.          const GLfloat dv3dx = span->attrStepX[attr][3];
  184.          GLfloat v0 = span->attrStart[attr][0] + span->leftClip * dv0dx;
  185.          GLfloat v1 = span->attrStart[attr][1] + span->leftClip * dv1dx;
  186.          GLfloat v2 = span->attrStart[attr][2] + span->leftClip * dv2dx;
  187.          GLfloat v3 = span->attrStart[attr][3] + span->leftClip * dv3dx;
  188.          GLuint k;
  189.          for (k = 0; k < span->end; k++) {
  190.             const GLfloat invW = 1.0f / w;
  191.             span->array->attribs[attr][k][0] = v0 * invW;
  192.             span->array->attribs[attr][k][1] = v1 * invW;
  193.             span->array->attribs[attr][k][2] = v2 * invW;
  194.             span->array->attribs[attr][k][3] = v3 * invW;
  195.             v0 += dv0dx;
  196.             v1 += dv1dx;
  197.             v2 += dv2dx;
  198.             v3 += dv3dx;
  199.             w += dwdx;
  200.          }
  201.          ASSERT((span->arrayAttribs & (1 << attr)) == 0);
  202.          span->arrayAttribs |= (1 << attr);
  203.       }
  204.    ATTRIB_LOOP_END
  205. }
  206.  
  207.  
  208. /**
  209.  * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
  210.  * color array.
  211.  */
  212. static INLINE void
  213. interpolate_int_colors(struct gl_context *ctx, SWspan *span)
  214. {
  215.    const GLuint n = span->end;
  216.    GLuint i;
  217.  
  218. #if CHAN_BITS != 32
  219.    ASSERT(!(span->arrayMask & SPAN_RGBA));
  220. #endif
  221.  
  222.    switch (span->array->ChanType) {
  223. #if CHAN_BITS != 32
  224.    case GL_UNSIGNED_BYTE:
  225.       {
  226.          GLubyte (*rgba)[4] = span->array->rgba8;
  227.          if (span->interpMask & SPAN_FLAT) {
  228.             GLubyte color[4];
  229.             color[RCOMP] = FixedToInt(span->red);
  230.             color[GCOMP] = FixedToInt(span->green);
  231.             color[BCOMP] = FixedToInt(span->blue);
  232.             color[ACOMP] = FixedToInt(span->alpha);
  233.             for (i = 0; i < n; i++) {
  234.                COPY_4UBV(rgba[i], color);
  235.             }
  236.          }
  237.          else {
  238.             GLfixed r = span->red;
  239.             GLfixed g = span->green;
  240.             GLfixed b = span->blue;
  241.             GLfixed a = span->alpha;
  242.             GLint dr = span->redStep;
  243.             GLint dg = span->greenStep;
  244.             GLint db = span->blueStep;
  245.             GLint da = span->alphaStep;
  246.             for (i = 0; i < n; i++) {
  247.                rgba[i][RCOMP] = FixedToChan(r);
  248.                rgba[i][GCOMP] = FixedToChan(g);
  249.                rgba[i][BCOMP] = FixedToChan(b);
  250.                rgba[i][ACOMP] = FixedToChan(a);
  251.                r += dr;
  252.                g += dg;
  253.                b += db;
  254.                a += da;
  255.             }
  256.          }
  257.       }
  258.       break;
  259.    case GL_UNSIGNED_SHORT:
  260.       {
  261.          GLushort (*rgba)[4] = span->array->rgba16;
  262.          if (span->interpMask & SPAN_FLAT) {
  263.             GLushort color[4];
  264.             color[RCOMP] = FixedToInt(span->red);
  265.             color[GCOMP] = FixedToInt(span->green);
  266.             color[BCOMP] = FixedToInt(span->blue);
  267.             color[ACOMP] = FixedToInt(span->alpha);
  268.             for (i = 0; i < n; i++) {
  269.                COPY_4V(rgba[i], color);
  270.             }
  271.          }
  272.          else {
  273.             GLushort (*rgba)[4] = span->array->rgba16;
  274.             GLfixed r, g, b, a;
  275.             GLint dr, dg, db, da;
  276.             r = span->red;
  277.             g = span->green;
  278.             b = span->blue;
  279.             a = span->alpha;
  280.             dr = span->redStep;
  281.             dg = span->greenStep;
  282.             db = span->blueStep;
  283.             da = span->alphaStep;
  284.             for (i = 0; i < n; i++) {
  285.                rgba[i][RCOMP] = FixedToChan(r);
  286.                rgba[i][GCOMP] = FixedToChan(g);
  287.                rgba[i][BCOMP] = FixedToChan(b);
  288.                rgba[i][ACOMP] = FixedToChan(a);
  289.                r += dr;
  290.                g += dg;
  291.                b += db;
  292.                a += da;
  293.             }
  294.          }
  295.       }
  296.       break;
  297. #endif
  298.    case GL_FLOAT:
  299.       interpolate_active_attribs(ctx, span, FRAG_BIT_COL0);
  300.       break;
  301.    default:
  302.       _mesa_problem(NULL, "bad datatype in interpolate_int_colors");
  303.    }
  304.    span->arrayMask |= SPAN_RGBA;
  305. }
  306.  
  307.  
  308. /**
  309.  * Populate the FRAG_ATTRIB_COL0 array.
  310.  */
  311. static INLINE void
  312. interpolate_float_colors(SWspan *span)
  313. {
  314.    GLfloat (*col0)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
  315.    const GLuint n = span->end;
  316.    GLuint i;
  317.  
  318.    assert(!(span->arrayAttribs & FRAG_BIT_COL0));
  319.  
  320.    if (span->arrayMask & SPAN_RGBA) {
  321.       /* convert array of int colors */
  322.       for (i = 0; i < n; i++) {
  323.          col0[i][0] = UBYTE_TO_FLOAT(span->array->rgba8[i][0]);
  324.          col0[i][1] = UBYTE_TO_FLOAT(span->array->rgba8[i][1]);
  325.          col0[i][2] = UBYTE_TO_FLOAT(span->array->rgba8[i][2]);
  326.          col0[i][3] = UBYTE_TO_FLOAT(span->array->rgba8[i][3]);
  327.       }
  328.    }
  329.    else {
  330.       /* interpolate red/green/blue/alpha to get float colors */
  331.       ASSERT(span->interpMask & SPAN_RGBA);
  332.       if (span->interpMask & SPAN_FLAT) {
  333.          GLfloat r = FixedToFloat(span->red);
  334.          GLfloat g = FixedToFloat(span->green);
  335.          GLfloat b = FixedToFloat(span->blue);
  336.          GLfloat a = FixedToFloat(span->alpha);
  337.          for (i = 0; i < n; i++) {
  338.             ASSIGN_4V(col0[i], r, g, b, a);
  339.          }
  340.       }
  341.       else {
  342.          GLfloat r = FixedToFloat(span->red);
  343.          GLfloat g = FixedToFloat(span->green);
  344.          GLfloat b = FixedToFloat(span->blue);
  345.          GLfloat a = FixedToFloat(span->alpha);
  346.          GLfloat dr = FixedToFloat(span->redStep);
  347.          GLfloat dg = FixedToFloat(span->greenStep);
  348.          GLfloat db = FixedToFloat(span->blueStep);
  349.          GLfloat da = FixedToFloat(span->alphaStep);
  350.          for (i = 0; i < n; i++) {
  351.             col0[i][0] = r;
  352.             col0[i][1] = g;
  353.             col0[i][2] = b;
  354.             col0[i][3] = a;
  355.             r += dr;
  356.             g += dg;
  357.             b += db;
  358.             a += da;
  359.          }
  360.       }
  361.    }
  362.  
  363.    span->arrayAttribs |= FRAG_BIT_COL0;
  364.    span->array->ChanType = GL_FLOAT;
  365. }
  366.  
  367.  
  368.  
  369. /**
  370.  * Fill in the span.zArray array from the span->z, zStep values.
  371.  */
  372. void
  373. _swrast_span_interpolate_z( const struct gl_context *ctx, SWspan *span )
  374. {
  375.    const GLuint n = span->end;
  376.    GLuint i;
  377.  
  378.    ASSERT(!(span->arrayMask & SPAN_Z));
  379.  
  380.    if (ctx->DrawBuffer->Visual.depthBits <= 16) {
  381.       GLfixed zval = span->z;
  382.       GLuint *z = span->array->z;
  383.       for (i = 0; i < n; i++) {
  384.          z[i] = FixedToInt(zval);
  385.          zval += span->zStep;
  386.       }
  387.    }
  388.    else {
  389.       /* Deep Z buffer, no fixed->int shift */
  390.       GLuint zval = span->z;
  391.       GLuint *z = span->array->z;
  392.       for (i = 0; i < n; i++) {
  393.          z[i] = zval;
  394.          zval += span->zStep;
  395.       }
  396.    }
  397.    span->interpMask &= ~SPAN_Z;
  398.    span->arrayMask |= SPAN_Z;
  399. }
  400.  
  401.  
  402. /**
  403.  * Compute mipmap LOD from partial derivatives.
  404.  * This the ideal solution, as given in the OpenGL spec.
  405.  */
  406. GLfloat
  407. _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
  408.                        GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
  409.                        GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
  410. {
  411.    GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ);
  412.    GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ);
  413.    GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ);
  414.    GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ);
  415.    GLfloat x = SQRTF(dudx * dudx + dvdx * dvdx);
  416.    GLfloat y = SQRTF(dudy * dudy + dvdy * dvdy);
  417.    GLfloat rho = MAX2(x, y);
  418.    GLfloat lambda = LOG2(rho);
  419.    return lambda;
  420. }
  421.  
  422.  
  423. /**
  424.  * Compute mipmap LOD from partial derivatives.
  425.  * This is a faster approximation than above function.
  426.  */
  427. #if 0
  428. GLfloat
  429. _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
  430.                      GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
  431.                      GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
  432. {
  433.    GLfloat dsdx2 = (s + dsdx) / (q + dqdx) - s * invQ;
  434.    GLfloat dtdx2 = (t + dtdx) / (q + dqdx) - t * invQ;
  435.    GLfloat dsdy2 = (s + dsdy) / (q + dqdy) - s * invQ;
  436.    GLfloat dtdy2 = (t + dtdy) / (q + dqdy) - t * invQ;
  437.    GLfloat maxU, maxV, rho, lambda;
  438.    dsdx2 = FABSF(dsdx2);
  439.    dsdy2 = FABSF(dsdy2);
  440.    dtdx2 = FABSF(dtdx2);
  441.    dtdy2 = FABSF(dtdy2);
  442.    maxU = MAX2(dsdx2, dsdy2) * texW;
  443.    maxV = MAX2(dtdx2, dtdy2) * texH;
  444.    rho = MAX2(maxU, maxV);
  445.    lambda = LOG2(rho);
  446.    return lambda;
  447. }
  448. #endif
  449.  
  450.  
  451. /**
  452.  * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
  453.  * using the attrStart/Step values.
  454.  *
  455.  * This function only used during fixed-function fragment processing.
  456.  *
  457.  * Note: in the places where we divide by Q (or mult by invQ) we're
  458.  * really doing two things: perspective correction and texcoord
  459.  * projection.  Remember, for texcoord (s,t,r,q) we need to index
  460.  * texels with (s/q, t/q, r/q).
  461.  */
  462. static void
  463. interpolate_texcoords(struct gl_context *ctx, SWspan *span)
  464. {
  465.    const GLuint maxUnit
  466.       = (ctx->Texture._EnabledCoordUnits > 1) ? ctx->Const.MaxTextureUnits : 1;
  467.    GLuint u;
  468.  
  469.    /* XXX CoordUnits vs. ImageUnits */
  470.    for (u = 0; u < maxUnit; u++) {
  471.       if (ctx->Texture._EnabledCoordUnits & (1 << u)) {
  472.          const GLuint attr = FRAG_ATTRIB_TEX0 + u;
  473.          const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current;
  474.          GLfloat texW, texH;
  475.          GLboolean needLambda;
  476.          GLfloat (*texcoord)[4] = span->array->attribs[attr];
  477.          GLfloat *lambda = span->array->lambda[u];
  478.          const GLfloat dsdx = span->attrStepX[attr][0];
  479.          const GLfloat dsdy = span->attrStepY[attr][0];
  480.          const GLfloat dtdx = span->attrStepX[attr][1];
  481.          const GLfloat dtdy = span->attrStepY[attr][1];
  482.          const GLfloat drdx = span->attrStepX[attr][2];
  483.          const GLfloat dqdx = span->attrStepX[attr][3];
  484.          const GLfloat dqdy = span->attrStepY[attr][3];
  485.          GLfloat s = span->attrStart[attr][0] + span->leftClip * dsdx;
  486.          GLfloat t = span->attrStart[attr][1] + span->leftClip * dtdx;
  487.          GLfloat r = span->attrStart[attr][2] + span->leftClip * drdx;
  488.          GLfloat q = span->attrStart[attr][3] + span->leftClip * dqdx;
  489.  
  490.          if (obj) {
  491.             const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
  492.             needLambda = (obj->MinFilter != obj->MagFilter)
  493.                || ctx->FragmentProgram._Current;
  494.             texW = img->WidthScale;
  495.             texH = img->HeightScale;
  496.          }
  497.          else {
  498.             /* using a fragment program */
  499.             texW = 1.0;
  500.             texH = 1.0;
  501.             needLambda = GL_FALSE;
  502.          }
  503.  
  504.          if (needLambda) {
  505.             GLuint i;
  506.             if (ctx->FragmentProgram._Current
  507.                 || ctx->ATIFragmentShader._Enabled) {
  508.                /* do perspective correction but don't divide s, t, r by q */
  509.                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
  510.                GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx;
  511.                for (i = 0; i < span->end; i++) {
  512.                   const GLfloat invW = 1.0F / w;
  513.                   texcoord[i][0] = s * invW;
  514.                   texcoord[i][1] = t * invW;
  515.                   texcoord[i][2] = r * invW;
  516.                   texcoord[i][3] = q * invW;
  517.                   lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
  518.                                                      dqdx, dqdy, texW, texH,
  519.                                                      s, t, q, invW);
  520.                   s += dsdx;
  521.                   t += dtdx;
  522.                   r += drdx;
  523.                   q += dqdx;
  524.                   w += dwdx;
  525.                }
  526.             }
  527.             else {
  528.                for (i = 0; i < span->end; i++) {
  529.                   const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
  530.                   texcoord[i][0] = s * invQ;
  531.                   texcoord[i][1] = t * invQ;
  532.                   texcoord[i][2] = r * invQ;
  533.                   texcoord[i][3] = q;
  534.                   lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
  535.                                                      dqdx, dqdy, texW, texH,
  536.                                                      s, t, q, invQ);
  537.                   s += dsdx;
  538.                   t += dtdx;
  539.                   r += drdx;
  540.                   q += dqdx;
  541.                }
  542.             }
  543.             span->arrayMask |= SPAN_LAMBDA;
  544.          }
  545.          else {
  546.             GLuint i;
  547.             if (ctx->FragmentProgram._Current ||
  548.                 ctx->ATIFragmentShader._Enabled) {
  549.                /* do perspective correction but don't divide s, t, r by q */
  550.                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
  551.                GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx;
  552.                for (i = 0; i < span->end; i++) {
  553.                   const GLfloat invW = 1.0F / w;
  554.                   texcoord[i][0] = s * invW;
  555.                   texcoord[i][1] = t * invW;
  556.                   texcoord[i][2] = r * invW;
  557.                   texcoord[i][3] = q * invW;
  558.                   lambda[i] = 0.0;
  559.                   s += dsdx;
  560.                   t += dtdx;
  561.                   r += drdx;
  562.                   q += dqdx;
  563.                   w += dwdx;
  564.                }
  565.             }
  566.             else if (dqdx == 0.0F) {
  567.                /* Ortho projection or polygon's parallel to window X axis */
  568.                const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
  569.                for (i = 0; i < span->end; i++) {
  570.                   texcoord[i][0] = s * invQ;
  571.                   texcoord[i][1] = t * invQ;
  572.                   texcoord[i][2] = r * invQ;
  573.                   texcoord[i][3] = q;
  574.                   lambda[i] = 0.0;
  575.                   s += dsdx;
  576.                   t += dtdx;
  577.                   r += drdx;
  578.                }
  579.             }
  580.             else {
  581.                for (i = 0; i < span->end; i++) {
  582.                   const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
  583.                   texcoord[i][0] = s * invQ;
  584.                   texcoord[i][1] = t * invQ;
  585.                   texcoord[i][2] = r * invQ;
  586.                   texcoord[i][3] = q;
  587.                   lambda[i] = 0.0;
  588.                   s += dsdx;
  589.                   t += dtdx;
  590.                   r += drdx;
  591.                   q += dqdx;
  592.                }
  593.             }
  594.          } /* lambda */
  595.       } /* if */
  596.    } /* for */
  597. }
  598.  
  599.  
  600. /**
  601.  * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
  602.  */
  603. static INLINE void
  604. interpolate_wpos(struct gl_context *ctx, SWspan *span)
  605. {
  606.    GLfloat (*wpos)[4] = span->array->attribs[FRAG_ATTRIB_WPOS];
  607.    GLuint i;
  608.    const GLfloat zScale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
  609.    GLfloat w, dw;
  610.  
  611.    if (span->arrayMask & SPAN_XY) {
  612.       for (i = 0; i < span->end; i++) {
  613.          wpos[i][0] = (GLfloat) span->array->x[i];
  614.          wpos[i][1] = (GLfloat) span->array->y[i];
  615.       }
  616.    }
  617.    else {
  618.       for (i = 0; i < span->end; i++) {
  619.          wpos[i][0] = (GLfloat) span->x + i;
  620.          wpos[i][1] = (GLfloat) span->y;
  621.       }
  622.    }
  623.  
  624.    dw = span->attrStepX[FRAG_ATTRIB_WPOS][3];
  625.    w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dw;
  626.    for (i = 0; i < span->end; i++) {
  627.       wpos[i][2] = (GLfloat) span->array->z[i] * zScale;
  628.       wpos[i][3] = w;
  629.       w += dw;
  630.    }
  631. }
  632.  
  633.  
  634. /**
  635.  * Apply the current polygon stipple pattern to a span of pixels.
  636.  */
  637. static INLINE void
  638. stipple_polygon_span(struct gl_context *ctx, SWspan *span)
  639. {
  640.    GLubyte *mask = span->array->mask;
  641.  
  642.    ASSERT(ctx->Polygon.StippleFlag);
  643.  
  644.    if (span->arrayMask & SPAN_XY) {
  645.       /* arrays of x/y pixel coords */
  646.       GLuint i;
  647.       for (i = 0; i < span->end; i++) {
  648.          const GLint col = span->array->x[i] % 32;
  649.          const GLint row = span->array->y[i] % 32;
  650.          const GLuint stipple = ctx->PolygonStipple[row];
  651.          if (((1 << col) & stipple) == 0) {
  652.             mask[i] = 0;
  653.          }
  654.       }
  655.    }
  656.    else {
  657.       /* horizontal span of pixels */
  658.       const GLuint highBit = 1 << 31;
  659.       const GLuint stipple = ctx->PolygonStipple[span->y % 32];
  660.       GLuint i, m = highBit >> (GLuint) (span->x % 32);
  661.       for (i = 0; i < span->end; i++) {
  662.          if ((m & stipple) == 0) {
  663.             mask[i] = 0;
  664.          }
  665.          m = m >> 1;
  666.          if (m == 0) {
  667.             m = highBit;
  668.          }
  669.       }
  670.    }
  671.    span->writeAll = GL_FALSE;
  672. }
  673.  
  674.  
  675. /**
  676.  * Clip a pixel span to the current buffer/window boundaries:
  677.  * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax.  This will accomplish
  678.  * window clipping and scissoring.
  679.  * Return:   GL_TRUE   some pixels still visible
  680.  *           GL_FALSE  nothing visible
  681.  */
  682. static INLINE GLuint
  683. clip_span( struct gl_context *ctx, SWspan *span )
  684. {
  685.    const GLint xmin = ctx->DrawBuffer->_Xmin;
  686.    const GLint xmax = ctx->DrawBuffer->_Xmax;
  687.    const GLint ymin = ctx->DrawBuffer->_Ymin;
  688.    const GLint ymax = ctx->DrawBuffer->_Ymax;
  689.  
  690.    span->leftClip = 0;
  691.  
  692.    if (span->arrayMask & SPAN_XY) {
  693.       /* arrays of x/y pixel coords */
  694.       const GLint *x = span->array->x;
  695.       const GLint *y = span->array->y;
  696.       const GLint n = span->end;
  697.       GLubyte *mask = span->array->mask;
  698.       GLint i;
  699.       if (span->arrayMask & SPAN_MASK) {
  700.          /* note: using & intead of && to reduce branches */
  701.          for (i = 0; i < n; i++) {
  702.             mask[i] &= (x[i] >= xmin) & (x[i] < xmax)
  703.                      & (y[i] >= ymin) & (y[i] < ymax);
  704.          }
  705.       }
  706.       else {
  707.          /* note: using & intead of && to reduce branches */
  708.          for (i = 0; i < n; i++) {
  709.             mask[i] = (x[i] >= xmin) & (x[i] < xmax)
  710.                     & (y[i] >= ymin) & (y[i] < ymax);
  711.          }
  712.       }
  713.       return GL_TRUE;  /* some pixels visible */
  714.    }
  715.    else {
  716.       /* horizontal span of pixels */
  717.       const GLint x = span->x;
  718.       const GLint y = span->y;
  719.       GLint n = span->end;
  720.  
  721.       /* Trivial rejection tests */
  722.       if (y < ymin || y >= ymax || x + n <= xmin || x >= xmax) {
  723.          span->end = 0;
  724.          return GL_FALSE;  /* all pixels clipped */
  725.       }
  726.  
  727.       /* Clip to right */
  728.       if (x + n > xmax) {
  729.          ASSERT(x < xmax);
  730.          n = span->end = xmax - x;
  731.       }
  732.  
  733.       /* Clip to the left */
  734.       if (x < xmin) {
  735.          const GLint leftClip = xmin - x;
  736.          GLuint i;
  737.  
  738.          ASSERT(leftClip > 0);
  739.          ASSERT(x + n > xmin);
  740.  
  741.          /* Clip 'leftClip' pixels from the left side.
  742.           * The span->leftClip field will be applied when we interpolate
  743.           * fragment attributes.
  744.           * For arrays of values, shift them left.
  745.           */
  746.          for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
  747.             if (span->interpMask & (1 << i)) {
  748.                GLuint j;
  749.                for (j = 0; j < 4; j++) {
  750.                   span->attrStart[i][j] += leftClip * span->attrStepX[i][j];
  751.                }
  752.             }
  753.          }
  754.  
  755.          span->red += leftClip * span->redStep;
  756.          span->green += leftClip * span->greenStep;
  757.          span->blue += leftClip * span->blueStep;
  758.          span->alpha += leftClip * span->alphaStep;
  759.          span->index += leftClip * span->indexStep;
  760.          span->z += leftClip * span->zStep;
  761.          span->intTex[0] += leftClip * span->intTexStep[0];
  762.          span->intTex[1] += leftClip * span->intTexStep[1];
  763.  
  764. #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
  765.          memcpy(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
  766.  
  767.          for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
  768.             if (span->arrayAttribs & (1 << i)) {
  769.                /* shift array elements left by 'leftClip' */
  770.                SHIFT_ARRAY(span->array->attribs[i], leftClip, n - leftClip);
  771.             }
  772.          }
  773.  
  774.          SHIFT_ARRAY(span->array->mask, leftClip, n - leftClip);
  775.          SHIFT_ARRAY(span->array->rgba8, leftClip, n - leftClip);
  776.          SHIFT_ARRAY(span->array->rgba16, leftClip, n - leftClip);
  777.          SHIFT_ARRAY(span->array->x, leftClip, n - leftClip);
  778.          SHIFT_ARRAY(span->array->y, leftClip, n - leftClip);
  779.          SHIFT_ARRAY(span->array->z, leftClip, n - leftClip);
  780.          SHIFT_ARRAY(span->array->index, leftClip, n - leftClip);
  781.          for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
  782.             SHIFT_ARRAY(span->array->lambda[i], leftClip, n - leftClip);
  783.          }
  784.          SHIFT_ARRAY(span->array->coverage, leftClip, n - leftClip);
  785.  
  786. #undef SHIFT_ARRAY
  787.  
  788.          span->leftClip = leftClip;
  789.          span->x = xmin;
  790.          span->end -= leftClip;
  791.          span->writeAll = GL_FALSE;
  792.       }
  793.  
  794.       ASSERT(span->x >= xmin);
  795.       ASSERT(span->x + span->end <= xmax);
  796.       ASSERT(span->y >= ymin);
  797.       ASSERT(span->y < ymax);
  798.  
  799.       return GL_TRUE;  /* some pixels visible */
  800.    }
  801. }
  802.  
  803.  
  804. /**
  805.  * Add specular colors to primary colors.
  806.  * Only called during fixed-function operation.
  807.  * Result is float color array (FRAG_ATTRIB_COL0).
  808.  */
  809. static INLINE void
  810. add_specular(struct gl_context *ctx, SWspan *span)
  811. {
  812.    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
  813.    const GLubyte *mask = span->array->mask;
  814.    GLfloat (*col0)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
  815.    GLfloat (*col1)[4] = span->array->attribs[FRAG_ATTRIB_COL1];
  816.    GLuint i;
  817.  
  818.    ASSERT(!ctx->FragmentProgram._Current);
  819.    ASSERT(span->arrayMask & SPAN_RGBA);
  820.    ASSERT(swrast->_ActiveAttribMask & FRAG_BIT_COL1);
  821.    (void) swrast; /* silence warning */
  822.  
  823.    if (span->array->ChanType == GL_FLOAT) {
  824.       if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) {
  825.          interpolate_active_attribs(ctx, span, FRAG_BIT_COL0);
  826.       }
  827.    }
  828.    else {
  829.       /* need float colors */
  830.       if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) {
  831.          interpolate_float_colors(span);
  832.       }
  833.    }
  834.  
  835.    if ((span->arrayAttribs & FRAG_BIT_COL1) == 0) {
  836.       /* XXX could avoid this and interpolate COL1 in the loop below */
  837.       interpolate_active_attribs(ctx, span, FRAG_BIT_COL1);
  838.    }
  839.  
  840.    ASSERT(span->arrayAttribs & FRAG_BIT_COL0);
  841.    ASSERT(span->arrayAttribs & FRAG_BIT_COL1);
  842.  
  843.    for (i = 0; i < span->end; i++) {
  844.       if (mask[i]) {
  845.          col0[i][0] += col1[i][0];
  846.          col0[i][1] += col1[i][1];
  847.          col0[i][2] += col1[i][2];
  848.       }
  849.    }
  850.  
  851.    span->array->ChanType = GL_FLOAT;
  852. }
  853.  
  854.  
  855. /**
  856.  * Apply antialiasing coverage value to alpha values.
  857.  */
  858. static INLINE void
  859. apply_aa_coverage(SWspan *span)
  860. {
  861.    const GLfloat *coverage = span->array->coverage;
  862.    GLuint i;
  863.    if (span->array->ChanType == GL_UNSIGNED_BYTE) {
  864.       GLubyte (*rgba)[4] = span->array->rgba8;
  865.       for (i = 0; i < span->end; i++) {
  866.          const GLfloat a = rgba[i][ACOMP] * coverage[i];
  867.          rgba[i][ACOMP] = (GLubyte) CLAMP(a, 0.0, 255.0);
  868.          ASSERT(coverage[i] >= 0.0);
  869.          ASSERT(coverage[i] <= 1.0);
  870.       }
  871.    }
  872.    else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
  873.       GLushort (*rgba)[4] = span->array->rgba16;
  874.       for (i = 0; i < span->end; i++) {
  875.          const GLfloat a = rgba[i][ACOMP] * coverage[i];
  876.          rgba[i][ACOMP] = (GLushort) CLAMP(a, 0.0, 65535.0);
  877.       }
  878.    }
  879.    else {
  880.       GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
  881.       for (i = 0; i < span->end; i++) {
  882.          rgba[i][ACOMP] = rgba[i][ACOMP] * coverage[i];
  883.          /* clamp later */
  884.       }
  885.    }
  886. }
  887.  
  888.  
  889. /**
  890.  * Clamp span's float colors to [0,1]
  891.  */
  892. static INLINE void
  893. clamp_colors(SWspan *span)
  894. {
  895.    GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
  896.    GLuint i;
  897.    ASSERT(span->array->ChanType == GL_FLOAT);
  898.    for (i = 0; i < span->end; i++) {
  899.       rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
  900.       rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
  901.       rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
  902.       rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
  903.    }
  904. }
  905.  
  906.  
  907. /**
  908.  * Convert the span's color arrays to the given type.
  909.  * The only way 'output' can be greater than zero is when we have a fragment
  910.  * program that writes to gl_FragData[1] or higher.
  911.  * \param output  which fragment program color output is being processed
  912.  */
  913. static INLINE void
  914. convert_color_type(SWspan *span, GLenum newType, GLuint output)
  915. {
  916.    GLvoid *src, *dst;
  917.  
  918.    if (output > 0 || span->array->ChanType == GL_FLOAT) {
  919.       src = span->array->attribs[FRAG_ATTRIB_COL0 + output];
  920.       span->array->ChanType = GL_FLOAT;
  921.    }
  922.    else if (span->array->ChanType == GL_UNSIGNED_BYTE) {
  923.       src = span->array->rgba8;
  924.    }
  925.    else {
  926.       ASSERT(span->array->ChanType == GL_UNSIGNED_SHORT);
  927.       src = span->array->rgba16;
  928.    }
  929.  
  930.    if (newType == GL_UNSIGNED_BYTE) {
  931.       dst = span->array->rgba8;
  932.    }
  933.    else if (newType == GL_UNSIGNED_SHORT) {
  934.       dst = span->array->rgba16;
  935.    }
  936.    else {
  937.       dst = span->array->attribs[FRAG_ATTRIB_COL0];
  938.    }
  939.  
  940.    _mesa_convert_colors(span->array->ChanType, src,
  941.                         newType, dst,
  942.                         span->end, span->array->mask);
  943.  
  944.    span->array->ChanType = newType;
  945.    span->array->rgba = dst;
  946. }
  947.  
  948.  
  949.  
  950. /**
  951.  * Apply fragment shader, fragment program or normal texturing to span.
  952.  */
  953. static INLINE void
  954. shade_texture_span(struct gl_context *ctx, SWspan *span)
  955. {
  956.    GLbitfield inputsRead;
  957.  
  958.    /* Determine which fragment attributes are actually needed */
  959.    if (ctx->FragmentProgram._Current) {
  960.       inputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
  961.    }
  962.    else {
  963.       /* XXX we could be a bit smarter about this */
  964.       inputsRead = ~0;
  965.    }
  966.  
  967.    if (ctx->FragmentProgram._Current ||
  968.        ctx->ATIFragmentShader._Enabled) {
  969.       /* programmable shading */
  970.       if (span->primitive == GL_BITMAP && span->array->ChanType != GL_FLOAT) {
  971.          convert_color_type(span, GL_FLOAT, 0);
  972.       }
  973.       else {
  974.          span->array->rgba = (void *) span->array->attribs[FRAG_ATTRIB_COL0];
  975.       }
  976.  
  977.       if (span->primitive != GL_POINT ||
  978.           (span->interpMask & SPAN_RGBA) ||
  979.           ctx->Point.PointSprite) {
  980.          /* for single-pixel points, we populated the arrays already */
  981.          interpolate_active_attribs(ctx, span, ~0);
  982.       }
  983.       span->array->ChanType = GL_FLOAT;
  984.  
  985.       if (!(span->arrayMask & SPAN_Z))
  986.          _swrast_span_interpolate_z (ctx, span);
  987.  
  988. #if 0
  989.       if (inputsRead & FRAG_BIT_WPOS)
  990. #else
  991.       /* XXX always interpolate wpos so that DDX/DDY work */
  992. #endif
  993.          interpolate_wpos(ctx, span);
  994.  
  995.       /* Run fragment program/shader now */
  996.       if (ctx->FragmentProgram._Current) {
  997.          _swrast_exec_fragment_program(ctx, span);
  998.       }
  999.       else {
  1000.          ASSERT(ctx->ATIFragmentShader._Enabled);
  1001.          _swrast_exec_fragment_shader(ctx, span);
  1002.       }
  1003.    }
  1004.    else if (ctx->Texture._EnabledCoordUnits) {
  1005.       /* conventional texturing */
  1006.  
  1007. #if CHAN_BITS == 32
  1008.       if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) {
  1009.          interpolate_int_colors(ctx, span);
  1010.       }
  1011. #else
  1012.       if (!(span->arrayMask & SPAN_RGBA))
  1013.          interpolate_int_colors(ctx, span);
  1014. #endif
  1015.       if ((span->arrayAttribs & FRAG_BITS_TEX_ANY) == 0x0)
  1016.          interpolate_texcoords(ctx, span);
  1017.  
  1018.       _swrast_texture_span(ctx, span);
  1019.    }
  1020. }
  1021.  
  1022.  
  1023.  
  1024. /**
  1025.  * Apply all the per-fragment operations to a span.
  1026.  * This now includes texturing (_swrast_write_texture_span() is history).
  1027.  * This function may modify any of the array values in the span.
  1028.  * span->interpMask and span->arrayMask may be changed but will be restored
  1029.  * to their original values before returning.
  1030.  */
  1031. void
  1032. _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
  1033. {
  1034.    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
  1035.    const GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
  1036.    const GLbitfield origInterpMask = span->interpMask;
  1037.    const GLbitfield origArrayMask = span->arrayMask;
  1038.    const GLbitfield origArrayAttribs = span->arrayAttribs;
  1039.    const GLenum origChanType = span->array->ChanType;
  1040.    void * const origRgba = span->array->rgba;
  1041.    const GLboolean shader = (ctx->FragmentProgram._Current
  1042.                              || ctx->ATIFragmentShader._Enabled);
  1043.    const GLboolean shaderOrTexture = shader || ctx->Texture._EnabledCoordUnits;
  1044.    struct gl_framebuffer *fb = ctx->DrawBuffer;
  1045.  
  1046.    /*
  1047.    printf("%s()  interp 0x%x  array 0x%x\n", __FUNCTION__,
  1048.           span->interpMask, span->arrayMask);
  1049.    */
  1050.  
  1051.    ASSERT(span->primitive == GL_POINT ||
  1052.           span->primitive == GL_LINE ||
  1053.           span->primitive == GL_POLYGON ||
  1054.           span->primitive == GL_BITMAP);
  1055.  
  1056.    /* Fragment write masks */
  1057.    if (span->arrayMask & SPAN_MASK) {
  1058.       /* mask was initialized by caller, probably glBitmap */
  1059.       span->writeAll = GL_FALSE;
  1060.    }
  1061.    else {
  1062.       memset(span->array->mask, 1, span->end);
  1063.       span->writeAll = GL_TRUE;
  1064.    }
  1065.  
  1066.    /* Clip to window/scissor box */
  1067.    if (!clip_span(ctx, span)) {
  1068.       return;
  1069.    }
  1070.  
  1071.    ASSERT(span->end <= MAX_WIDTH);
  1072.  
  1073.    /* Depth bounds test */
  1074.    if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) {
  1075.       if (!_swrast_depth_bounds_test(ctx, span)) {
  1076.          return;
  1077.       }
  1078.    }
  1079.  
  1080. #ifdef DEBUG
  1081.    /* Make sure all fragments are within window bounds */
  1082.    if (span->arrayMask & SPAN_XY) {
  1083.       /* array of pixel locations */
  1084.       GLuint i;
  1085.       for (i = 0; i < span->end; i++) {
  1086.          if (span->array->mask[i]) {
  1087.             assert(span->array->x[i] >= fb->_Xmin);
  1088.             assert(span->array->x[i] < fb->_Xmax);
  1089.             assert(span->array->y[i] >= fb->_Ymin);
  1090.             assert(span->array->y[i] < fb->_Ymax);
  1091.          }
  1092.       }
  1093.    }
  1094. #endif
  1095.  
  1096.    /* Polygon Stippling */
  1097.    if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) {
  1098.       stipple_polygon_span(ctx, span);
  1099.    }
  1100.  
  1101.    /* This is the normal place to compute the fragment color/Z
  1102.     * from texturing or shading.
  1103.     */
  1104.    if (shaderOrTexture && !swrast->_DeferredTexture) {
  1105.       shade_texture_span(ctx, span);
  1106.    }
  1107.  
  1108.    /* Do the alpha test */
  1109.    if (ctx->Color.AlphaEnabled) {
  1110.       if (!_swrast_alpha_test(ctx, span)) {
  1111.          /* all fragments failed test */
  1112.          goto end;
  1113.       }
  1114.    }
  1115.  
  1116.    /* Stencil and Z testing */
  1117.    if (ctx->Stencil._Enabled || ctx->Depth.Test) {
  1118.       if (!(span->arrayMask & SPAN_Z))
  1119.          _swrast_span_interpolate_z(ctx, span);
  1120.  
  1121.       if (ctx->Transform.DepthClamp)
  1122.          _swrast_depth_clamp_span(ctx, span);
  1123.  
  1124.       if (ctx->Stencil._Enabled) {
  1125.          /* Combined Z/stencil tests */
  1126.          if (!_swrast_stencil_and_ztest_span(ctx, span)) {
  1127.             /* all fragments failed test */
  1128.             goto end;
  1129.          }
  1130.       }
  1131.       else if (fb->Visual.depthBits > 0) {
  1132.          /* Just regular depth testing */
  1133.          ASSERT(ctx->Depth.Test);
  1134.          ASSERT(span->arrayMask & SPAN_Z);
  1135.          if (!_swrast_depth_test_span(ctx, span)) {
  1136.             /* all fragments failed test */
  1137.             goto end;
  1138.          }
  1139.       }
  1140.    }
  1141.  
  1142.    if (ctx->Query.CurrentOcclusionObject) {
  1143.       /* update count of 'passed' fragments */
  1144.       struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;
  1145.       GLuint i;
  1146.       for (i = 0; i < span->end; i++)
  1147.          q->Result += span->array->mask[i];
  1148.    }
  1149.  
  1150.    /* We had to wait until now to check for glColorMask(0,0,0,0) because of
  1151.     * the occlusion test.
  1152.     */
  1153.    if (fb->_NumColorDrawBuffers == 1 && colorMask[0] == 0x0) {
  1154.       /* no colors to write */
  1155.       goto end;
  1156.    }
  1157.  
  1158.    /* If we were able to defer fragment color computation to now, there's
  1159.     * a good chance that many fragments will have already been killed by
  1160.     * Z/stencil testing.
  1161.     */
  1162.    if (shaderOrTexture && swrast->_DeferredTexture) {
  1163.       shade_texture_span(ctx, span);
  1164.    }
  1165.  
  1166. #if CHAN_BITS == 32
  1167.    if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) {
  1168.       interpolate_active_attribs(ctx, span, FRAG_BIT_COL0);
  1169.    }
  1170. #else
  1171.    if ((span->arrayMask & SPAN_RGBA) == 0) {
  1172.       interpolate_int_colors(ctx, span);
  1173.    }
  1174. #endif
  1175.  
  1176.    ASSERT(span->arrayMask & SPAN_RGBA);
  1177.  
  1178.    if (span->primitive == GL_BITMAP || !swrast->SpecularVertexAdd) {
  1179.       /* Add primary and specular (diffuse + specular) colors */
  1180.       if (!shader) {
  1181.          if (ctx->Fog.ColorSumEnabled ||
  1182.              (ctx->Light.Enabled &&
  1183.               ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
  1184.             add_specular(ctx, span);
  1185.          }
  1186.       }
  1187.    }
  1188.  
  1189.    /* Fog */
  1190.    if (swrast->_FogEnabled) {
  1191.       _swrast_fog_rgba_span(ctx, span);
  1192.    }
  1193.  
  1194.    /* Antialias coverage application */
  1195.    if (span->arrayMask & SPAN_COVERAGE) {
  1196.       apply_aa_coverage(span);
  1197.    }
  1198.  
  1199.    /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
  1200.    if (ctx->Color.ClampFragmentColor == GL_TRUE &&
  1201.        span->array->ChanType == GL_FLOAT) {
  1202.       clamp_colors(span);
  1203.    }
  1204.  
  1205.    /*
  1206.     * Write to renderbuffers.
  1207.     * Depending on glDrawBuffer() state and the which color outputs are
  1208.     * written by the fragment shader, we may either replicate one color to
  1209.     * all renderbuffers or write a different color to each renderbuffer.
  1210.     * multiFragOutputs=TRUE for the later case.
  1211.     */
  1212.    {
  1213.       const GLuint numBuffers = fb->_NumColorDrawBuffers;
  1214.       const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
  1215.       const GLboolean multiFragOutputs =
  1216.          (fp && fp->Base.OutputsWritten >= (1 << FRAG_RESULT_DATA0));
  1217.       GLuint buf;
  1218.  
  1219.       for (buf = 0; buf < numBuffers; buf++) {
  1220.          struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
  1221.  
  1222.          /* color[fragOutput] will be written to buffer[buf] */
  1223.  
  1224.          if (rb) {
  1225.             GLchan rgbaSave[MAX_WIDTH][4];
  1226.             const GLuint fragOutput = multiFragOutputs ? buf : 0;
  1227.  
  1228.             /* set span->array->rgba to colors for render buffer's datatype */
  1229.             if (rb->DataType != span->array->ChanType || fragOutput > 0) {
  1230.                convert_color_type(span, rb->DataType, fragOutput);
  1231.             }
  1232.             else {
  1233.                if (rb->DataType == GL_UNSIGNED_BYTE) {
  1234.                   span->array->rgba = span->array->rgba8;
  1235.                }
  1236.                else if (rb->DataType == GL_UNSIGNED_SHORT) {
  1237.                   span->array->rgba = (void *) span->array->rgba16;
  1238.                }
  1239.                else {
  1240.                   span->array->rgba = (void *)
  1241.                      span->array->attribs[FRAG_ATTRIB_COL0];
  1242.                }
  1243.             }
  1244.  
  1245.             if (!multiFragOutputs && numBuffers > 1) {
  1246.                /* save colors for second, third renderbuffer writes */
  1247.                memcpy(rgbaSave, span->array->rgba,
  1248.                       4 * span->end * sizeof(GLchan));
  1249.             }
  1250.  
  1251.             ASSERT(rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB ||
  1252.                    rb->_BaseFormat == GL_ALPHA);
  1253.  
  1254.             if (ctx->Color._LogicOpEnabled) {
  1255.                _swrast_logicop_rgba_span(ctx, rb, span);
  1256.             }
  1257.             else if ((ctx->Color.BlendEnabled >> buf) & 1) {
  1258.                _swrast_blend_span(ctx, rb, span);
  1259.             }
  1260.  
  1261.             if (colorMask[buf] != 0xffffffff) {
  1262.                _swrast_mask_rgba_span(ctx, rb, span, buf);
  1263.             }
  1264.  
  1265.             if (span->arrayMask & SPAN_XY) {
  1266.                /* array of pixel coords */
  1267.                ASSERT(rb->PutValues);
  1268.                rb->PutValues(ctx, rb, span->end,
  1269.                              span->array->x, span->array->y,
  1270.                              span->array->rgba, span->array->mask);
  1271.             }
  1272.             else {
  1273.                /* horizontal run of pixels */
  1274.                ASSERT(rb->PutRow);
  1275.                rb->PutRow(ctx, rb, span->end, span->x, span->y,
  1276.                           span->array->rgba,
  1277.                           span->writeAll ? NULL: span->array->mask);
  1278.             }
  1279.  
  1280.             if (!multiFragOutputs && numBuffers > 1) {
  1281.                /* restore original span values */
  1282.                memcpy(span->array->rgba, rgbaSave,
  1283.                       4 * span->end * sizeof(GLchan));
  1284.             }
  1285.  
  1286.          } /* if rb */
  1287.       } /* for buf */
  1288.    }
  1289.  
  1290. end:
  1291.    /* restore these values before returning */
  1292.    span->interpMask = origInterpMask;
  1293.    span->arrayMask = origArrayMask;
  1294.    span->arrayAttribs = origArrayAttribs;
  1295.    span->array->ChanType = origChanType;
  1296.    span->array->rgba = origRgba;
  1297. }
  1298.  
  1299.  
  1300. /**
  1301.  * Read RGBA pixels from a renderbuffer.  Clipping will be done to prevent
  1302.  * reading ouside the buffer's boundaries.
  1303.  * \param dstType  datatype for returned colors
  1304.  * \param rgba  the returned colors
  1305.  */
  1306. void
  1307. _swrast_read_rgba_span( struct gl_context *ctx, struct gl_renderbuffer *rb,
  1308.                         GLuint n, GLint x, GLint y, GLenum dstType,
  1309.                         GLvoid *rgba)
  1310. {
  1311.    const GLint bufWidth = (GLint) rb->Width;
  1312.    const GLint bufHeight = (GLint) rb->Height;
  1313.  
  1314.    if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) {
  1315.       /* completely above, below, or right */
  1316.       /* XXX maybe leave rgba values undefined? */
  1317.       memset(rgba, 0, 4 * n * sizeof(GLchan));
  1318.    }
  1319.    else {
  1320.       GLint skip, length;
  1321.       if (x < 0) {
  1322.          /* left edge clipping */
  1323.          skip = -x;
  1324.          length = (GLint) n - skip;
  1325.          if (length < 0) {
  1326.             /* completely left of window */
  1327.             return;
  1328.          }
  1329.          if (length > bufWidth) {
  1330.             length = bufWidth;
  1331.          }
  1332.       }
  1333.       else if ((GLint) (x + n) > bufWidth) {
  1334.          /* right edge clipping */
  1335.          skip = 0;
  1336.          length = bufWidth - x;
  1337.          if (length < 0) {
  1338.             /* completely to right of window */
  1339.             return;
  1340.          }
  1341.       }
  1342.       else {
  1343.          /* no clipping */
  1344.          skip = 0;
  1345.          length = (GLint) n;
  1346.       }
  1347.  
  1348.       ASSERT(rb);
  1349.       ASSERT(rb->GetRow);
  1350.       ASSERT(rb->_BaseFormat == GL_RGB || rb->_BaseFormat == GL_RGBA ||
  1351.              rb->_BaseFormat == GL_ALPHA);
  1352.  
  1353.       if (rb->DataType == dstType) {
  1354.          rb->GetRow(ctx, rb, length, x + skip, y,
  1355.                     (GLubyte *) rgba + skip * RGBA_PIXEL_SIZE(rb->DataType));
  1356.       }
  1357.       else {
  1358.          GLuint temp[MAX_WIDTH * 4];
  1359.          rb->GetRow(ctx, rb, length, x + skip, y, temp);
  1360.          _mesa_convert_colors(rb->DataType, temp,
  1361.                    dstType, (GLubyte *) rgba + skip * RGBA_PIXEL_SIZE(dstType),
  1362.                    length, NULL);
  1363.       }
  1364.    }
  1365. }
  1366.  
  1367.  
  1368. /**
  1369.  * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
  1370.  * reading values outside the buffer bounds.
  1371.  * We can use this for reading any format/type of renderbuffer.
  1372.  * \param valueSize is the size in bytes of each value (pixel) put into the
  1373.  *                  values array.
  1374.  */
  1375. void
  1376. _swrast_get_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
  1377.                    GLuint count, const GLint x[], const GLint y[],
  1378.                    void *values, GLuint valueSize)
  1379. {
  1380.    GLuint i, inCount = 0, inStart = 0;
  1381.  
  1382.    for (i = 0; i < count; i++) {
  1383.       if (x[i] >= 0 && y[i] >= 0 &&
  1384.           x[i] < (GLint) rb->Width && y[i] < (GLint) rb->Height) {
  1385.          /* inside */
  1386.          if (inCount == 0)
  1387.             inStart = i;
  1388.          inCount++;
  1389.       }
  1390.       else {
  1391.          if (inCount > 0) {
  1392.             /* read [inStart, inStart + inCount) */
  1393.             rb->GetValues(ctx, rb, inCount, x + inStart, y + inStart,
  1394.                           (GLubyte *) values + inStart * valueSize);
  1395.             inCount = 0;
  1396.          }
  1397.       }
  1398.    }
  1399.    if (inCount > 0) {
  1400.       /* read last values */
  1401.       rb->GetValues(ctx, rb, inCount, x + inStart, y + inStart,
  1402.                     (GLubyte *) values + inStart * valueSize);
  1403.    }
  1404. }
  1405.  
  1406.  
  1407. /**
  1408.  * Wrapper for gl_renderbuffer::PutRow() which does clipping.
  1409.  * \param valueSize  size of each value (pixel) in bytes
  1410.  */
  1411. void
  1412. _swrast_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
  1413.                 GLuint count, GLint x, GLint y,
  1414.                 const GLvoid *values, GLuint valueSize)
  1415. {
  1416.    GLint skip = 0;
  1417.  
  1418.    if (y < 0 || y >= (GLint) rb->Height)
  1419.       return; /* above or below */
  1420.  
  1421.    if (x + (GLint) count <= 0 || x >= (GLint) rb->Width)
  1422.       return; /* entirely left or right */
  1423.  
  1424.    if ((GLint) (x + count) > (GLint) rb->Width) {
  1425.       /* right clip */
  1426.       GLint clip = x + count - rb->Width;
  1427.       count -= clip;
  1428.    }
  1429.  
  1430.    if (x < 0) {
  1431.       /* left clip */
  1432.       skip = -x;
  1433.       x = 0;
  1434.       count -= skip;
  1435.    }
  1436.  
  1437.    rb->PutRow(ctx, rb, count, x, y,
  1438.               (const GLubyte *) values + skip * valueSize, NULL);
  1439. }
  1440.  
  1441.  
  1442. /**
  1443.  * Wrapper for gl_renderbuffer::GetRow() which does clipping.
  1444.  * \param valueSize  size of each value (pixel) in bytes
  1445.  */
  1446. void
  1447. _swrast_get_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
  1448.                 GLuint count, GLint x, GLint y,
  1449.                 GLvoid *values, GLuint valueSize)
  1450. {
  1451.    GLint skip = 0;
  1452.  
  1453.    if (y < 0 || y >= (GLint) rb->Height)
  1454.       return; /* above or below */
  1455.  
  1456.    if (x + (GLint) count <= 0 || x >= (GLint) rb->Width)
  1457.       return; /* entirely left or right */
  1458.  
  1459.    if (x + count > rb->Width) {
  1460.       /* right clip */
  1461.       GLint clip = x + count - rb->Width;
  1462.       count -= clip;
  1463.    }
  1464.  
  1465.    if (x < 0) {
  1466.       /* left clip */
  1467.       skip = -x;
  1468.       x = 0;
  1469.       count -= skip;
  1470.    }
  1471.  
  1472.    rb->GetRow(ctx, rb, count, x, y, (GLubyte *) values + skip * valueSize);
  1473. }
  1474.  
  1475.  
  1476. /**
  1477.  * Get RGBA pixels from the given renderbuffer.
  1478.  * Used by blending, logicop and masking functions.
  1479.  * \return pointer to the colors we read.
  1480.  */
  1481. void *
  1482. _swrast_get_dest_rgba(struct gl_context *ctx, struct gl_renderbuffer *rb,
  1483.                       SWspan *span)
  1484. {
  1485.    const GLuint pixelSize = RGBA_PIXEL_SIZE(span->array->ChanType);
  1486.    void *rbPixels;
  1487.  
  1488.    /* Point rbPixels to a temporary space */
  1489.    rbPixels = span->array->attribs[FRAG_ATTRIB_MAX - 1];
  1490.  
  1491.    /* Get destination values from renderbuffer */
  1492.    if (span->arrayMask & SPAN_XY) {
  1493.       _swrast_get_values(ctx, rb, span->end, span->array->x, span->array->y,
  1494.                          rbPixels, pixelSize);
  1495.    }
  1496.    else {
  1497.       _swrast_get_row(ctx, rb, span->end, span->x, span->y,
  1498.                       rbPixels, pixelSize);
  1499.    }
  1500.  
  1501.    return rbPixels;
  1502. }
  1503.