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