s_tritemp.h revision fcd7c37fd3d0f61cf6ac81170bc0b3fca64ad9bb
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.0
4 *
5 * Copyright (C) 1999-2007  Brian Paul   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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/*
26 * Triangle Rasterizer Template
27 *
28 * This file is #include'd to generate custom triangle rasterizers.
29 *
30 * The following macros may be defined to indicate what auxillary information
31 * must be interpolated across the triangle:
32 *    INTERP_Z        - if defined, interpolate integer Z values
33 *    INTERP_RGB      - if defined, interpolate integer RGB values
34 *    INTERP_ALPHA    - if defined, interpolate integer Alpha values
35 *    INTERP_INDEX    - if defined, interpolate color index values
36 *    INTERP_INT_TEX  - if defined, interpolate integer ST texcoords
37 *                         (fast, simple 2-D texture mapping, without
38 *                         perspective correction)
39 *    INTERP_ATTRIBS  - if defined, interpolate arbitrary attribs (texcoords,
40 *                         varying vars, etc)  This also causes W to be
41 *                         computed for perspective correction).
42 *
43 * When one can directly address pixels in the color buffer the following
44 * macros can be defined and used to compute pixel addresses during
45 * rasterization (see pRow):
46 *    PIXEL_TYPE          - the datatype of a pixel (GLubyte, GLushort, GLuint)
47 *    BYTES_PER_ROW       - number of bytes per row in the color buffer
48 *    PIXEL_ADDRESS(X,Y)  - returns the address of pixel at (X,Y) where
49 *                          Y==0 at bottom of screen and increases upward.
50 *
51 * Similarly, for direct depth buffer access, this type is used for depth
52 * buffer addressing (see zRow):
53 *    DEPTH_TYPE          - either GLushort or GLuint
54 *
55 * Optionally, one may provide one-time setup code per triangle:
56 *    SETUP_CODE    - code which is to be executed once per triangle
57 *
58 * The following macro MUST be defined:
59 *    RENDER_SPAN(span) - code to write a span of pixels.
60 *
61 * This code was designed for the origin to be in the lower-left corner.
62 *
63 * Inspired by triangle rasterizer code written by Allen Akin.  Thanks Allen!
64 *
65 *
66 * Some notes on rasterization accuracy:
67 *
68 * This code uses fixed point arithmetic (the GLfixed type) to iterate
69 * over the triangle edges and interpolate ancillary data (such as Z,
70 * color, secondary color, etc).  The number of fractional bits in
71 * GLfixed and the value of SUB_PIXEL_BITS has a direct bearing on the
72 * accuracy of rasterization.
73 *
74 * If SUB_PIXEL_BITS=4 then we'll snap the vertices to the nearest
75 * 1/16 of a pixel.  If we're walking up a long, nearly vertical edge
76 * (dx=1/16, dy=1024) we'll need 4 + 10 = 14 fractional bits in
77 * GLfixed to walk the edge without error.  If the maximum viewport
78 * height is 4K pixels, then we'll need 4 + 12 = 16 fractional bits.
79 *
80 * Historically, Mesa has used 11 fractional bits in GLfixed, snaps
81 * vertices to 1/16 pixel and allowed a maximum viewport height of 2K
82 * pixels.  11 fractional bits is actually insufficient for accurately
83 * rasterizing some triangles.  More recently, the maximum viewport
84 * height was increased to 4K pixels.  Thus, Mesa should be using 16
85 * fractional bits in GLfixed.  Unfortunately, there may be some issues
86 * with setting FIXED_FRAC_BITS=16, such as multiplication overflow.
87 * This will have to be examined in some detail...
88 *
89 * For now, if you find rasterization errors, particularly with tall,
90 * sliver triangles, try increasing FIXED_FRAC_BITS and/or decreasing
91 * SUB_PIXEL_BITS.
92 */
93
94
95/*
96 * Some code we unfortunately need to prevent negative interpolated colors.
97 */
98#ifndef CLAMP_INTERPOLANT
99#define CLAMP_INTERPOLANT(CHANNEL, CHANNELSTEP, LEN)		\
100do {								\
101   GLfixed endVal = span.CHANNEL + (LEN) * span.CHANNELSTEP;	\
102   if (endVal < 0) {						\
103      span.CHANNEL -= endVal;					\
104   }								\
105   if (span.CHANNEL < 0) {					\
106      span.CHANNEL = 0;						\
107   }								\
108} while (0)
109#endif
110
111
112static void NAME(GLcontext *ctx, const SWvertex *v0,
113                                 const SWvertex *v1,
114                                 const SWvertex *v2 )
115{
116   typedef struct {
117      const SWvertex *v0, *v1;   /* Y(v0) < Y(v1) */
118      GLfloat dx;	/* X(v1) - X(v0) */
119      GLfloat dy;	/* Y(v1) - Y(v0) */
120      GLfloat dxdy;	/* dx/dy */
121      GLfixed fdxdy;	/* dx/dy in fixed-point */
122      GLfloat adjy;	/* adjust from v[0]->fy to fsy, scaled */
123      GLfixed fsx;	/* first sample point x coord */
124      GLfixed fsy;
125      GLfixed fx0;	/* fixed pt X of lower endpoint */
126      GLint lines;	/* number of lines to be sampled on this edge */
127   } EdgeT;
128
129   const SWcontext *swrast = SWRAST_CONTEXT(ctx);
130#ifdef INTERP_Z
131   const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
132   const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
133   const GLfloat maxDepth = ctx->DrawBuffer->_DepthMaxF;
134#define FixedToDepth(F)  ((F) >> fixedToDepthShift)
135#endif
136   EdgeT eMaj, eTop, eBot;
137   GLfloat oneOverArea;
138   const SWvertex *vMin, *vMid, *vMax;  /* Y(vMin)<=Y(vMid)<=Y(vMax) */
139   GLfloat bf = SWRAST_CONTEXT(ctx)->_BackfaceCullSign;
140   const GLint snapMask = ~((FIXED_ONE / (1 << SUB_PIXEL_BITS)) - 1); /* for x/y coord snapping */
141   GLfixed vMin_fx, vMin_fy, vMid_fx, vMid_fy, vMax_fx, vMax_fy;
142
143   SWspan span;
144
145   (void) swrast;
146
147   INIT_SPAN(span, GL_POLYGON);
148   span.y = 0; /* silence warnings */
149
150#ifdef INTERP_Z
151   (void) fixedToDepthShift;
152#endif
153
154   /*
155   printf("%s()\n", __FUNCTION__);
156   printf("  %g, %g, %g\n",
157          v0->attrib[FRAG_ATTRIB_WPOS][0],
158          v0->attrib[FRAG_ATTRIB_WPOS][1],
159          v0->attrib[FRAG_ATTRIB_WPOS][2]);
160   printf("  %g, %g, %g\n",
161          v1->attrib[FRAG_ATTRIB_WPOS][0],
162          v1->attrib[FRAG_ATTRIB_WPOS][1],
163          v1->attrib[FRAG_ATTRIB_WPOS][2]);
164   printf("  %g, %g, %g\n",
165          v2->attrib[FRAG_ATTRIB_WPOS][0],
166          v2->attrib[FRAG_ATTRIB_WPOS][1],
167          v2->attrib[FRAG_ATTRIB_WPOS][2]);
168   */
169
170   /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
171    * And find the order of the 3 vertices along the Y axis.
172    */
173   {
174      const GLfixed fy0 = FloatToFixed(v0->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) & snapMask;
175      const GLfixed fy1 = FloatToFixed(v1->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) & snapMask;
176      const GLfixed fy2 = FloatToFixed(v2->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) & snapMask;
177      if (fy0 <= fy1) {
178         if (fy1 <= fy2) {
179            /* y0 <= y1 <= y2 */
180            vMin = v0;   vMid = v1;   vMax = v2;
181            vMin_fy = fy0;  vMid_fy = fy1;  vMax_fy = fy2;
182         }
183         else if (fy2 <= fy0) {
184            /* y2 <= y0 <= y1 */
185            vMin = v2;   vMid = v0;   vMax = v1;
186            vMin_fy = fy2;  vMid_fy = fy0;  vMax_fy = fy1;
187         }
188         else {
189            /* y0 <= y2 <= y1 */
190            vMin = v0;   vMid = v2;   vMax = v1;
191            vMin_fy = fy0;  vMid_fy = fy2;  vMax_fy = fy1;
192            bf = -bf;
193         }
194      }
195      else {
196         if (fy0 <= fy2) {
197            /* y1 <= y0 <= y2 */
198            vMin = v1;   vMid = v0;   vMax = v2;
199            vMin_fy = fy1;  vMid_fy = fy0;  vMax_fy = fy2;
200            bf = -bf;
201         }
202         else if (fy2 <= fy1) {
203            /* y2 <= y1 <= y0 */
204            vMin = v2;   vMid = v1;   vMax = v0;
205            vMin_fy = fy2;  vMid_fy = fy1;  vMax_fy = fy0;
206            bf = -bf;
207         }
208         else {
209            /* y1 <= y2 <= y0 */
210            vMin = v1;   vMid = v2;   vMax = v0;
211            vMin_fy = fy1;  vMid_fy = fy2;  vMax_fy = fy0;
212         }
213      }
214
215      /* fixed point X coords */
216      vMin_fx = FloatToFixed(vMin->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) & snapMask;
217      vMid_fx = FloatToFixed(vMid->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) & snapMask;
218      vMax_fx = FloatToFixed(vMax->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) & snapMask;
219   }
220
221   /* vertex/edge relationship */
222   eMaj.v0 = vMin;   eMaj.v1 = vMax;   /*TODO: .v1's not needed */
223   eTop.v0 = vMid;   eTop.v1 = vMax;
224   eBot.v0 = vMin;   eBot.v1 = vMid;
225
226   /* compute deltas for each edge:  vertex[upper] - vertex[lower] */
227   eMaj.dx = FixedToFloat(vMax_fx - vMin_fx);
228   eMaj.dy = FixedToFloat(vMax_fy - vMin_fy);
229   eTop.dx = FixedToFloat(vMax_fx - vMid_fx);
230   eTop.dy = FixedToFloat(vMax_fy - vMid_fy);
231   eBot.dx = FixedToFloat(vMid_fx - vMin_fx);
232   eBot.dy = FixedToFloat(vMid_fy - vMin_fy);
233
234   /* compute area, oneOverArea and perform backface culling */
235   {
236      const GLfloat area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;
237      /* Do backface culling */
238
239      if (area * bf < 0.0)
240         return;
241
242      if (IS_INF_OR_NAN(area) || area == 0.0F)
243         return;
244
245      oneOverArea = 1.0F / area;
246
247      /* 0 = front, 1 = back */
248      span.facing = oneOverArea * swrast->_BackfaceSign > 0.0F;
249   }
250
251   /* Edge setup.  For a triangle strip these could be reused... */
252   {
253      eMaj.fsy = FixedCeil(vMin_fy);
254      eMaj.lines = FixedToInt(FixedCeil(vMax_fy - eMaj.fsy));
255      if (eMaj.lines > 0) {
256         eMaj.dxdy = eMaj.dx / eMaj.dy;
257         eMaj.fdxdy = SignedFloatToFixed(eMaj.dxdy);
258         eMaj.adjy = (GLfloat) (eMaj.fsy - vMin_fy);  /* SCALED! */
259         eMaj.fx0 = vMin_fx;
260         eMaj.fsx = eMaj.fx0 + (GLfixed) (eMaj.adjy * eMaj.dxdy);
261      }
262      else {
263         return;  /*CULLED*/
264      }
265
266      eTop.fsy = FixedCeil(vMid_fy);
267      eTop.lines = FixedToInt(FixedCeil(vMax_fy - eTop.fsy));
268      if (eTop.lines > 0) {
269         eTop.dxdy = eTop.dx / eTop.dy;
270         eTop.fdxdy = SignedFloatToFixed(eTop.dxdy);
271         eTop.adjy = (GLfloat) (eTop.fsy - vMid_fy); /* SCALED! */
272         eTop.fx0 = vMid_fx;
273         eTop.fsx = eTop.fx0 + (GLfixed) (eTop.adjy * eTop.dxdy);
274      }
275
276      eBot.fsy = FixedCeil(vMin_fy);
277      eBot.lines = FixedToInt(FixedCeil(vMid_fy - eBot.fsy));
278      if (eBot.lines > 0) {
279         eBot.dxdy = eBot.dx / eBot.dy;
280         eBot.fdxdy = SignedFloatToFixed(eBot.dxdy);
281         eBot.adjy = (GLfloat) (eBot.fsy - vMin_fy);  /* SCALED! */
282         eBot.fx0 = vMin_fx;
283         eBot.fsx = eBot.fx0 + (GLfixed) (eBot.adjy * eBot.dxdy);
284      }
285   }
286
287   /*
288    * Conceptually, we view a triangle as two subtriangles
289    * separated by a perfectly horizontal line.  The edge that is
290    * intersected by this line is one with maximal absolute dy; we
291    * call it a ``major'' edge.  The other two edges are the
292    * ``top'' edge (for the upper subtriangle) and the ``bottom''
293    * edge (for the lower subtriangle).  If either of these two
294    * edges is horizontal or very close to horizontal, the
295    * corresponding subtriangle might cover zero sample points;
296    * we take care to handle such cases, for performance as well
297    * as correctness.
298    *
299    * By stepping rasterization parameters along the major edge,
300    * we can avoid recomputing them at the discontinuity where
301    * the top and bottom edges meet.  However, this forces us to
302    * be able to scan both left-to-right and right-to-left.
303    * Also, we must determine whether the major edge is at the
304    * left or right side of the triangle.  We do this by
305    * computing the magnitude of the cross-product of the major
306    * and top edges.  Since this magnitude depends on the sine of
307    * the angle between the two edges, its sign tells us whether
308    * we turn to the left or to the right when travelling along
309    * the major edge to the top edge, and from this we infer
310    * whether the major edge is on the left or the right.
311    *
312    * Serendipitously, this cross-product magnitude is also a
313    * value we need to compute the iteration parameter
314    * derivatives for the triangle, and it can be used to perform
315    * backface culling because its sign tells us whether the
316    * triangle is clockwise or counterclockwise.  In this code we
317    * refer to it as ``area'' because it's also proportional to
318    * the pixel area of the triangle.
319    */
320
321   {
322      GLint scan_from_left_to_right;  /* true if scanning left-to-right */
323#ifdef INTERP_INDEX
324      GLfloat didx, didy;
325#endif
326
327      /*
328       * Execute user-supplied setup code
329       */
330#ifdef SETUP_CODE
331      SETUP_CODE
332#endif
333
334      scan_from_left_to_right = (oneOverArea < 0.0F);
335
336
337      /* compute d?/dx and d?/dy derivatives */
338#ifdef INTERP_Z
339      span.interpMask |= SPAN_Z;
340      {
341         GLfloat eMaj_dz = vMax->attrib[FRAG_ATTRIB_WPOS][2] - vMin->attrib[FRAG_ATTRIB_WPOS][2];
342         GLfloat eBot_dz = vMid->attrib[FRAG_ATTRIB_WPOS][2] - vMin->attrib[FRAG_ATTRIB_WPOS][2];
343         span.attrStepX[FRAG_ATTRIB_WPOS][2] = oneOverArea * (eMaj_dz * eBot.dy - eMaj.dy * eBot_dz);
344         if (span.attrStepX[FRAG_ATTRIB_WPOS][2] > maxDepth ||
345             span.attrStepX[FRAG_ATTRIB_WPOS][2] < -maxDepth) {
346            /* probably a sliver triangle */
347            span.attrStepX[FRAG_ATTRIB_WPOS][2] = 0.0;
348            span.attrStepY[FRAG_ATTRIB_WPOS][2] = 0.0;
349         }
350         else {
351            span.attrStepY[FRAG_ATTRIB_WPOS][2] = oneOverArea * (eMaj.dx * eBot_dz - eMaj_dz * eBot.dx);
352         }
353         if (depthBits <= 16)
354            span.zStep = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_WPOS][2]);
355         else
356            span.zStep = (GLint) span.attrStepX[FRAG_ATTRIB_WPOS][2];
357      }
358#endif
359#ifdef INTERP_RGB
360      span.interpMask |= SPAN_RGBA;
361      if (ctx->Light.ShadeModel == GL_SMOOTH) {
362         GLfloat eMaj_dr = (GLfloat) (vMax->color[RCOMP] - vMin->color[RCOMP]);
363         GLfloat eBot_dr = (GLfloat) (vMid->color[RCOMP] - vMin->color[RCOMP]);
364         GLfloat eMaj_dg = (GLfloat) (vMax->color[GCOMP] - vMin->color[GCOMP]);
365         GLfloat eBot_dg = (GLfloat) (vMid->color[GCOMP] - vMin->color[GCOMP]);
366         GLfloat eMaj_db = (GLfloat) (vMax->color[BCOMP] - vMin->color[BCOMP]);
367         GLfloat eBot_db = (GLfloat) (vMid->color[BCOMP] - vMin->color[BCOMP]);
368#  ifdef INTERP_ALPHA
369         GLfloat eMaj_da = (GLfloat) (vMax->color[ACOMP] - vMin->color[ACOMP]);
370         GLfloat eBot_da = (GLfloat) (vMid->color[ACOMP] - vMin->color[ACOMP]);
371#  endif
372         span.attrStepX[FRAG_ATTRIB_COL0][0] = oneOverArea * (eMaj_dr * eBot.dy - eMaj.dy * eBot_dr);
373         span.attrStepY[FRAG_ATTRIB_COL0][0] = oneOverArea * (eMaj.dx * eBot_dr - eMaj_dr * eBot.dx);
374         span.attrStepX[FRAG_ATTRIB_COL0][1] = oneOverArea * (eMaj_dg * eBot.dy - eMaj.dy * eBot_dg);
375         span.attrStepY[FRAG_ATTRIB_COL0][1] = oneOverArea * (eMaj.dx * eBot_dg - eMaj_dg * eBot.dx);
376         span.attrStepX[FRAG_ATTRIB_COL0][2] = oneOverArea * (eMaj_db * eBot.dy - eMaj.dy * eBot_db);
377         span.attrStepY[FRAG_ATTRIB_COL0][2] = oneOverArea * (eMaj.dx * eBot_db - eMaj_db * eBot.dx);
378         span.redStep   = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_COL0][0]);
379         span.greenStep = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_COL0][1]);
380         span.blueStep  = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_COL0][2]);
381#  ifdef INTERP_ALPHA
382         span.attrStepX[FRAG_ATTRIB_COL0][3] = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
383         span.attrStepY[FRAG_ATTRIB_COL0][3] = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
384         span.alphaStep = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_COL0][3]);
385#  endif /* INTERP_ALPHA */
386      }
387      else {
388         ASSERT(ctx->Light.ShadeModel == GL_FLAT);
389         span.interpMask |= SPAN_FLAT;
390         span.attrStepX[FRAG_ATTRIB_COL0][0] = span.attrStepY[FRAG_ATTRIB_COL0][0] = 0.0F;
391         span.attrStepX[FRAG_ATTRIB_COL0][1] = span.attrStepY[FRAG_ATTRIB_COL0][1] = 0.0F;
392         span.attrStepX[FRAG_ATTRIB_COL0][2] = span.attrStepY[FRAG_ATTRIB_COL0][2] = 0.0F;
393	 span.redStep   = 0;
394	 span.greenStep = 0;
395	 span.blueStep  = 0;
396#  ifdef INTERP_ALPHA
397         span.attrStepX[FRAG_ATTRIB_COL0][3] = span.attrStepY[FRAG_ATTRIB_COL0][3] = 0.0F;
398	 span.alphaStep = 0;
399#  endif
400      }
401#endif /* INTERP_RGB */
402#ifdef INTERP_INDEX
403      span.interpMask |= SPAN_INDEX;
404      if (ctx->Light.ShadeModel == GL_SMOOTH) {
405         GLfloat eMaj_di = vMax->attrib[FRAG_ATTRIB_CI][0] - vMin->attrib[FRAG_ATTRIB_CI][0];
406         GLfloat eBot_di = vMid->attrib[FRAG_ATTRIB_CI][0] - vMin->attrib[FRAG_ATTRIB_CI][0];
407         didx = oneOverArea * (eMaj_di * eBot.dy - eMaj.dy * eBot_di);
408         didy = oneOverArea * (eMaj.dx * eBot_di - eMaj_di * eBot.dx);
409         span.indexStep = SignedFloatToFixed(didx);
410      }
411      else {
412         span.interpMask |= SPAN_FLAT;
413         didx = didy = 0.0F;
414         span.indexStep = 0;
415      }
416#endif
417#ifdef INTERP_INT_TEX
418      {
419         GLfloat eMaj_ds = (vMax->attrib[FRAG_ATTRIB_TEX0][0] - vMin->attrib[FRAG_ATTRIB_TEX0][0]) * S_SCALE;
420         GLfloat eBot_ds = (vMid->attrib[FRAG_ATTRIB_TEX0][0] - vMin->attrib[FRAG_ATTRIB_TEX0][0]) * S_SCALE;
421         GLfloat eMaj_dt = (vMax->attrib[FRAG_ATTRIB_TEX0][1] - vMin->attrib[FRAG_ATTRIB_TEX0][1]) * T_SCALE;
422         GLfloat eBot_dt = (vMid->attrib[FRAG_ATTRIB_TEX0][1] - vMin->attrib[FRAG_ATTRIB_TEX0][1]) * T_SCALE;
423         span.attrStepX[FRAG_ATTRIB_TEX0][0] = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
424         span.attrStepY[FRAG_ATTRIB_TEX0][0] = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
425         span.attrStepX[FRAG_ATTRIB_TEX0][1] = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
426         span.attrStepY[FRAG_ATTRIB_TEX0][1] = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
427         span.intTexStep[0] = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_TEX0][0]);
428         span.intTexStep[1] = SignedFloatToFixed(span.attrStepX[FRAG_ATTRIB_TEX0][1]);
429      }
430#endif
431#ifdef INTERP_ATTRIBS
432      {
433         /* attrib[FRAG_ATTRIB_WPOS][3] is 1/W */
434         const GLfloat wMax = vMax->attrib[FRAG_ATTRIB_WPOS][3];
435         const GLfloat wMin = vMin->attrib[FRAG_ATTRIB_WPOS][3];
436         const GLfloat wMid = vMid->attrib[FRAG_ATTRIB_WPOS][3];
437         {
438            const GLfloat eMaj_dw = wMax - wMin;
439            const GLfloat eBot_dw = wMid - wMin;
440            span.attrStepX[FRAG_ATTRIB_WPOS][3] = oneOverArea * (eMaj_dw * eBot.dy - eMaj.dy * eBot_dw);
441            span.attrStepY[FRAG_ATTRIB_WPOS][3] = oneOverArea * (eMaj.dx * eBot_dw - eMaj_dw * eBot.dx);
442         }
443         ATTRIB_LOOP_BEGIN
444            if (swrast->_InterpMode[attr] == GL_FLAT) {
445               ASSIGN_4V(span.attrStepX[attr], 0.0, 0.0, 0.0, 0.0);
446               ASSIGN_4V(span.attrStepY[attr], 0.0, 0.0, 0.0, 0.0);
447            }
448            else {
449               GLuint c;
450               for (c = 0; c < 4; c++) {
451                  GLfloat eMaj_da = vMax->attrib[attr][c] * wMax - vMin->attrib[attr][c] * wMin;
452                  GLfloat eBot_da = vMid->attrib[attr][c] * wMid - vMin->attrib[attr][c] * wMin;
453                  span.attrStepX[attr][c] = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
454                  span.attrStepY[attr][c] = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
455               }
456            }
457         ATTRIB_LOOP_END
458      }
459#endif
460
461      /*
462       * We always sample at pixel centers.  However, we avoid
463       * explicit half-pixel offsets in this code by incorporating
464       * the proper offset in each of x and y during the
465       * transformation to window coordinates.
466       *
467       * We also apply the usual rasterization rules to prevent
468       * cracks and overlaps.  A pixel is considered inside a
469       * subtriangle if it meets all of four conditions: it is on or
470       * to the right of the left edge, strictly to the left of the
471       * right edge, on or below the top edge, and strictly above
472       * the bottom edge.  (Some edges may be degenerate.)
473       *
474       * The following discussion assumes left-to-right scanning
475       * (that is, the major edge is on the left); the right-to-left
476       * case is a straightforward variation.
477       *
478       * We start by finding the half-integral y coordinate that is
479       * at or below the top of the triangle.  This gives us the
480       * first scan line that could possibly contain pixels that are
481       * inside the triangle.
482       *
483       * Next we creep down the major edge until we reach that y,
484       * and compute the corresponding x coordinate on the edge.
485       * Then we find the half-integral x that lies on or just
486       * inside the edge.  This is the first pixel that might lie in
487       * the interior of the triangle.  (We won't know for sure
488       * until we check the other edges.)
489       *
490       * As we rasterize the triangle, we'll step down the major
491       * edge.  For each step in y, we'll move an integer number
492       * of steps in x.  There are two possible x step sizes, which
493       * we'll call the ``inner'' step (guaranteed to land on the
494       * edge or inside it) and the ``outer'' step (guaranteed to
495       * land on the edge or outside it).  The inner and outer steps
496       * differ by one.  During rasterization we maintain an error
497       * term that indicates our distance from the true edge, and
498       * select either the inner step or the outer step, whichever
499       * gets us to the first pixel that falls inside the triangle.
500       *
501       * All parameters (z, red, etc.) as well as the buffer
502       * addresses for color and z have inner and outer step values,
503       * so that we can increment them appropriately.  This method
504       * eliminates the need to adjust parameters by creeping a
505       * sub-pixel amount into the triangle at each scanline.
506       */
507
508      {
509         GLint subTriangle;
510         GLfixed fxLeftEdge = 0, fxRightEdge = 0;
511         GLfixed fdxLeftEdge = 0, fdxRightEdge = 0;
512         GLfixed fError = 0, fdError = 0;
513#ifdef PIXEL_ADDRESS
514         PIXEL_TYPE *pRow = NULL;
515         GLint dPRowOuter = 0, dPRowInner;  /* offset in bytes */
516#endif
517#ifdef INTERP_Z
518#  ifdef DEPTH_TYPE
519         struct gl_renderbuffer *zrb
520            = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
521         DEPTH_TYPE *zRow = NULL;
522         GLint dZRowOuter = 0, dZRowInner;  /* offset in bytes */
523#  endif
524         GLuint zLeft = 0;
525         GLfixed fdzOuter = 0, fdzInner;
526#endif
527#ifdef INTERP_RGB
528         GLint rLeft = 0, fdrOuter = 0, fdrInner;
529         GLint gLeft = 0, fdgOuter = 0, fdgInner;
530         GLint bLeft = 0, fdbOuter = 0, fdbInner;
531#endif
532#ifdef INTERP_ALPHA
533         GLint aLeft = 0, fdaOuter = 0, fdaInner;
534#endif
535#ifdef INTERP_INDEX
536         GLfixed iLeft=0, diOuter=0, diInner;
537#endif
538#ifdef INTERP_INT_TEX
539         GLfixed sLeft=0, dsOuter=0, dsInner;
540         GLfixed tLeft=0, dtOuter=0, dtInner;
541#endif
542#ifdef INTERP_ATTRIBS
543         GLfloat wLeft = 0, dwOuter = 0, dwInner;
544         GLfloat attrLeft[FRAG_ATTRIB_MAX][4];
545         GLfloat daOuter[FRAG_ATTRIB_MAX][4], daInner[FRAG_ATTRIB_MAX][4];
546#endif
547
548         for (subTriangle=0; subTriangle<=1; subTriangle++) {
549            EdgeT *eLeft, *eRight;
550            int setupLeft, setupRight;
551            int lines;
552
553            if (subTriangle==0) {
554               /* bottom half */
555               if (scan_from_left_to_right) {
556                  eLeft = &eMaj;
557                  eRight = &eBot;
558                  lines = eRight->lines;
559                  setupLeft = 1;
560                  setupRight = 1;
561               }
562               else {
563                  eLeft = &eBot;
564                  eRight = &eMaj;
565                  lines = eLeft->lines;
566                  setupLeft = 1;
567                  setupRight = 1;
568               }
569            }
570            else {
571               /* top half */
572               if (scan_from_left_to_right) {
573                  eLeft = &eMaj;
574                  eRight = &eTop;
575                  lines = eRight->lines;
576                  setupLeft = 0;
577                  setupRight = 1;
578               }
579               else {
580                  eLeft = &eTop;
581                  eRight = &eMaj;
582                  lines = eLeft->lines;
583                  setupLeft = 1;
584                  setupRight = 0;
585               }
586               if (lines == 0)
587                  return;
588            }
589
590            if (setupLeft && eLeft->lines > 0) {
591               const SWvertex *vLower = eLeft->v0;
592               const GLfixed fsy = eLeft->fsy;
593               const GLfixed fsx = eLeft->fsx;  /* no fractional part */
594               const GLfixed fx = FixedCeil(fsx);  /* no fractional part */
595               const GLfixed adjx = (GLfixed) (fx - eLeft->fx0); /* SCALED! */
596               const GLfixed adjy = (GLfixed) eLeft->adjy;      /* SCALED! */
597               GLint idxOuter;
598               GLfloat dxOuter;
599               GLfixed fdxOuter;
600
601               fError = fx - fsx - FIXED_ONE;
602               fxLeftEdge = fsx - FIXED_EPSILON;
603               fdxLeftEdge = eLeft->fdxdy;
604               fdxOuter = FixedFloor(fdxLeftEdge - FIXED_EPSILON);
605               fdError = fdxOuter - fdxLeftEdge + FIXED_ONE;
606               idxOuter = FixedToInt(fdxOuter);
607               dxOuter = (GLfloat) idxOuter;
608               span.y = FixedToInt(fsy);
609
610               /* silence warnings on some compilers */
611               (void) dxOuter;
612               (void) adjx;
613               (void) adjy;
614               (void) vLower;
615
616#ifdef PIXEL_ADDRESS
617               {
618                  pRow = (PIXEL_TYPE *) PIXEL_ADDRESS(FixedToInt(fxLeftEdge), span.y);
619                  dPRowOuter = -((int)BYTES_PER_ROW) + idxOuter * sizeof(PIXEL_TYPE);
620                  /* negative because Y=0 at bottom and increases upward */
621               }
622#endif
623               /*
624                * Now we need the set of parameter (z, color, etc.) values at
625                * the point (fx, fsy).  This gives us properly-sampled parameter
626                * values that we can step from pixel to pixel.  Furthermore,
627                * although we might have intermediate results that overflow
628                * the normal parameter range when we step temporarily outside
629                * the triangle, we shouldn't overflow or underflow for any
630                * pixel that's actually inside the triangle.
631                */
632
633#ifdef INTERP_Z
634               {
635                  GLfloat z0 = vLower->attrib[FRAG_ATTRIB_WPOS][2];
636                  if (depthBits <= 16) {
637                     /* interpolate fixed-pt values */
638                     GLfloat tmp = (z0 * FIXED_SCALE
639                                    + span.attrStepX[FRAG_ATTRIB_WPOS][2] * adjx
640                                    + span.attrStepY[FRAG_ATTRIB_WPOS][2] * adjy) + FIXED_HALF;
641                     if (tmp < MAX_GLUINT / 2)
642                        zLeft = (GLfixed) tmp;
643                     else
644                        zLeft = MAX_GLUINT / 2;
645                     fdzOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_WPOS][2] +
646                                                   dxOuter * span.attrStepX[FRAG_ATTRIB_WPOS][2]);
647                  }
648                  else {
649                     /* interpolate depth values w/out scaling */
650                     zLeft = (GLuint) (z0 + span.attrStepX[FRAG_ATTRIB_WPOS][2] * FixedToFloat(adjx)
651                                          + span.attrStepY[FRAG_ATTRIB_WPOS][2] * FixedToFloat(adjy));
652                     fdzOuter = (GLint) (span.attrStepY[FRAG_ATTRIB_WPOS][2] +
653                                         dxOuter * span.attrStepX[FRAG_ATTRIB_WPOS][2]);
654                  }
655#  ifdef DEPTH_TYPE
656                  zRow = (DEPTH_TYPE *)
657                    zrb->GetPointer(ctx, zrb, FixedToInt(fxLeftEdge), span.y);
658                  dZRowOuter = (ctx->DrawBuffer->Width + idxOuter) * sizeof(DEPTH_TYPE);
659#  endif
660               }
661#endif
662#ifdef INTERP_RGB
663               if (ctx->Light.ShadeModel == GL_SMOOTH) {
664                  rLeft = (GLint)(ChanToFixed(vLower->color[RCOMP])
665                                  + span.attrStepX[FRAG_ATTRIB_COL0][0] * adjx
666                                  + span.attrStepY[FRAG_ATTRIB_COL0][0] * adjy) + FIXED_HALF;
667                  gLeft = (GLint)(ChanToFixed(vLower->color[GCOMP])
668                                  + span.attrStepX[FRAG_ATTRIB_COL0][1] * adjx
669                                  + span.attrStepY[FRAG_ATTRIB_COL0][1] * adjy) + FIXED_HALF;
670                  bLeft = (GLint)(ChanToFixed(vLower->color[BCOMP])
671                                  + span.attrStepX[FRAG_ATTRIB_COL0][2] * adjx
672                                  + span.attrStepY[FRAG_ATTRIB_COL0][2] * adjy) + FIXED_HALF;
673                  fdrOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_COL0][0]
674                                                + dxOuter * span.attrStepX[FRAG_ATTRIB_COL0][0]);
675                  fdgOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_COL0][1]
676                                                + dxOuter * span.attrStepX[FRAG_ATTRIB_COL0][1]);
677                  fdbOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_COL0][2]
678                                                + dxOuter * span.attrStepX[FRAG_ATTRIB_COL0][2]);
679#  ifdef INTERP_ALPHA
680                  aLeft = (GLint)(ChanToFixed(vLower->color[ACOMP])
681                                  + span.attrStepX[FRAG_ATTRIB_COL0][3] * adjx
682                                  + span.attrStepY[FRAG_ATTRIB_COL0][3] * adjy) + FIXED_HALF;
683                  fdaOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_COL0][3]
684                                                + dxOuter * span.attrStepX[FRAG_ATTRIB_COL0][3]);
685#  endif
686               }
687               else {
688                  ASSERT(ctx->Light.ShadeModel == GL_FLAT);
689                  rLeft = ChanToFixed(v2->color[RCOMP]);
690                  gLeft = ChanToFixed(v2->color[GCOMP]);
691                  bLeft = ChanToFixed(v2->color[BCOMP]);
692                  fdrOuter = fdgOuter = fdbOuter = 0;
693#  ifdef INTERP_ALPHA
694                  aLeft = ChanToFixed(v2->color[ACOMP]);
695                  fdaOuter = 0;
696#  endif
697               }
698#endif /* INTERP_RGB */
699
700
701#ifdef INTERP_INDEX
702               if (ctx->Light.ShadeModel == GL_SMOOTH) {
703                  iLeft = (GLfixed)(vLower->attrib[FRAG_ATTRIB_CI][0] * FIXED_SCALE
704                                 + didx * adjx + didy * adjy) + FIXED_HALF;
705                  diOuter = SignedFloatToFixed(didy + dxOuter * didx);
706               }
707               else {
708                  ASSERT(ctx->Light.ShadeModel == GL_FLAT);
709                  iLeft = FloatToFixed(v2->attrib[FRAG_ATTRIB_CI][0]);
710                  diOuter = 0;
711               }
712#endif
713#ifdef INTERP_INT_TEX
714               {
715                  GLfloat s0, t0;
716                  s0 = vLower->attrib[FRAG_ATTRIB_TEX0][0] * S_SCALE;
717                  sLeft = (GLfixed)(s0 * FIXED_SCALE + span.attrStepX[FRAG_ATTRIB_TEX0][0] * adjx
718                                 + span.attrStepY[FRAG_ATTRIB_TEX0][0] * adjy) + FIXED_HALF;
719                  dsOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_TEX0][0]
720                                               + dxOuter * span.attrStepX[FRAG_ATTRIB_TEX0][0]);
721
722                  t0 = vLower->attrib[FRAG_ATTRIB_TEX0][1] * T_SCALE;
723                  tLeft = (GLfixed)(t0 * FIXED_SCALE + span.attrStepX[FRAG_ATTRIB_TEX0][1] * adjx
724                                 + span.attrStepY[FRAG_ATTRIB_TEX0][1] * adjy) + FIXED_HALF;
725                  dtOuter = SignedFloatToFixed(span.attrStepY[FRAG_ATTRIB_TEX0][1]
726                                               + dxOuter * span.attrStepX[FRAG_ATTRIB_TEX0][1]);
727               }
728#endif
729#ifdef INTERP_ATTRIBS
730               {
731                  const GLuint attr = FRAG_ATTRIB_WPOS;
732                  wLeft = vLower->attrib[FRAG_ATTRIB_WPOS][3]
733                        + (span.attrStepX[attr][3] * adjx
734                           + span.attrStepY[attr][3] * adjy) * (1.0F/FIXED_SCALE);
735                  dwOuter = span.attrStepY[attr][3] + dxOuter * span.attrStepX[attr][3];
736               }
737               ATTRIB_LOOP_BEGIN
738                  const GLfloat invW = vLower->attrib[FRAG_ATTRIB_WPOS][3];
739                  if (swrast->_InterpMode[attr] == GL_FLAT) {
740                     GLuint c;
741                     for (c = 0; c < 4; c++) {
742                        attrLeft[attr][c] = v2->attrib[attr][c] * invW;
743                        daOuter[attr][c] = 0.0;
744                     }
745                  }
746                  else {
747                     GLuint c;
748                     for (c = 0; c < 4; c++) {
749                        const GLfloat a = vLower->attrib[attr][c] * invW;
750                        attrLeft[attr][c] = a + (  span.attrStepX[attr][c] * adjx
751                                                 + span.attrStepY[attr][c] * adjy) * (1.0F/FIXED_SCALE);
752                        daOuter[attr][c] = span.attrStepY[attr][c] + dxOuter * span.attrStepX[attr][c];
753                     }
754                  }
755               ATTRIB_LOOP_END
756#endif
757            } /*if setupLeft*/
758
759
760            if (setupRight && eRight->lines>0) {
761               fxRightEdge = eRight->fsx - FIXED_EPSILON;
762               fdxRightEdge = eRight->fdxdy;
763            }
764
765            if (lines==0) {
766               continue;
767            }
768
769
770            /* Rasterize setup */
771#ifdef PIXEL_ADDRESS
772            dPRowInner = dPRowOuter + sizeof(PIXEL_TYPE);
773#endif
774#ifdef INTERP_Z
775#  ifdef DEPTH_TYPE
776            dZRowInner = dZRowOuter + sizeof(DEPTH_TYPE);
777#  endif
778            fdzInner = fdzOuter + span.zStep;
779#endif
780#ifdef INTERP_RGB
781            fdrInner = fdrOuter + span.redStep;
782            fdgInner = fdgOuter + span.greenStep;
783            fdbInner = fdbOuter + span.blueStep;
784#endif
785#ifdef INTERP_ALPHA
786            fdaInner = fdaOuter + span.alphaStep;
787#endif
788#ifdef INTERP_INDEX
789            diInner = diOuter + span.indexStep;
790#endif
791#ifdef INTERP_INT_TEX
792            dsInner = dsOuter + span.intTexStep[0];
793            dtInner = dtOuter + span.intTexStep[1];
794#endif
795#ifdef INTERP_ATTRIBS
796            dwInner = dwOuter + span.attrStepX[FRAG_ATTRIB_WPOS][3];
797            ATTRIB_LOOP_BEGIN
798               GLuint c;
799               for (c = 0; c < 4; c++) {
800                  daInner[attr][c] = daOuter[attr][c] + span.attrStepX[attr][c];
801               }
802            ATTRIB_LOOP_END
803#endif
804
805            while (lines > 0) {
806               /* initialize the span interpolants to the leftmost value */
807               /* ff = fixed-pt fragment */
808               const GLint right = FixedToInt(fxRightEdge);
809               span.x = FixedToInt(fxLeftEdge);
810               if (right <= span.x)
811                  span.end = 0;
812               else
813                  span.end = right - span.x;
814
815#ifdef INTERP_Z
816               span.z = zLeft;
817#endif
818#ifdef INTERP_RGB
819               span.red = rLeft;
820               span.green = gLeft;
821               span.blue = bLeft;
822#endif
823#ifdef INTERP_ALPHA
824               span.alpha = aLeft;
825#endif
826#ifdef INTERP_INDEX
827               span.index = iLeft;
828#endif
829#ifdef INTERP_INT_TEX
830               span.intTex[0] = sLeft;
831               span.intTex[1] = tLeft;
832#endif
833
834#ifdef INTERP_ATTRIBS
835               span.attrStart[FRAG_ATTRIB_WPOS][3] = wLeft;
836               ATTRIB_LOOP_BEGIN
837                  GLuint c;
838                  for (c = 0; c < 4; c++) {
839                     span.attrStart[attr][c] = attrLeft[attr][c];
840                  }
841               ATTRIB_LOOP_END
842#endif
843
844               /* This is where we actually generate fragments */
845               /* XXX the test for span.y > 0 _shouldn't_ be needed but
846                * it fixes a problem on 64-bit Opterons (bug 4842).
847                */
848               if (span.end > 0 && span.y >= 0) {
849                  const GLint len = span.end - 1;
850                  (void) len;
851#ifdef INTERP_RGB
852                  CLAMP_INTERPOLANT(red, redStep, len);
853                  CLAMP_INTERPOLANT(green, greenStep, len);
854                  CLAMP_INTERPOLANT(blue, blueStep, len);
855#endif
856#ifdef INTERP_ALPHA
857                  CLAMP_INTERPOLANT(alpha, alphaStep, len);
858#endif
859#ifdef INTERP_INDEX
860                  CLAMP_INTERPOLANT(index, indexStep, len);
861#endif
862                  {
863                     RENDER_SPAN( span );
864                  }
865               }
866
867               /*
868                * Advance to the next scan line.  Compute the
869                * new edge coordinates, and adjust the
870                * pixel-center x coordinate so that it stays
871                * on or inside the major edge.
872                */
873               span.y++;
874               lines--;
875
876               fxLeftEdge += fdxLeftEdge;
877               fxRightEdge += fdxRightEdge;
878
879               fError += fdError;
880               if (fError >= 0) {
881                  fError -= FIXED_ONE;
882
883#ifdef PIXEL_ADDRESS
884                  pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowOuter);
885#endif
886#ifdef INTERP_Z
887#  ifdef DEPTH_TYPE
888                  zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowOuter);
889#  endif
890                  zLeft += fdzOuter;
891#endif
892#ifdef INTERP_RGB
893                  rLeft += fdrOuter;
894                  gLeft += fdgOuter;
895                  bLeft += fdbOuter;
896#endif
897#ifdef INTERP_ALPHA
898                  aLeft += fdaOuter;
899#endif
900#ifdef INTERP_INDEX
901                  iLeft += diOuter;
902#endif
903#ifdef INTERP_INT_TEX
904                  sLeft += dsOuter;
905                  tLeft += dtOuter;
906#endif
907#ifdef INTERP_ATTRIBS
908                  wLeft += dwOuter;
909                  ATTRIB_LOOP_BEGIN
910                     GLuint c;
911                     for (c = 0; c < 4; c++) {
912                        attrLeft[attr][c] += daOuter[attr][c];
913                     }
914                  ATTRIB_LOOP_END
915#endif
916               }
917               else {
918#ifdef PIXEL_ADDRESS
919                  pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowInner);
920#endif
921#ifdef INTERP_Z
922#  ifdef DEPTH_TYPE
923                  zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowInner);
924#  endif
925                  zLeft += fdzInner;
926#endif
927#ifdef INTERP_RGB
928                  rLeft += fdrInner;
929                  gLeft += fdgInner;
930                  bLeft += fdbInner;
931#endif
932#ifdef INTERP_ALPHA
933                  aLeft += fdaInner;
934#endif
935#ifdef INTERP_INDEX
936                  iLeft += diInner;
937#endif
938#ifdef INTERP_INT_TEX
939                  sLeft += dsInner;
940                  tLeft += dtInner;
941#endif
942#ifdef INTERP_ATTRIBS
943                  wLeft += dwInner;
944                  ATTRIB_LOOP_BEGIN
945                     GLuint c;
946                     for (c = 0; c < 4; c++) {
947                        attrLeft[attr][c] += daInner[attr][c];
948                     }
949                  ATTRIB_LOOP_END
950#endif
951               }
952            } /*while lines>0*/
953
954         } /* for subTriangle */
955
956      }
957   }
958}
959
960#undef SETUP_CODE
961#undef RENDER_SPAN
962
963#undef PIXEL_TYPE
964#undef BYTES_PER_ROW
965#undef PIXEL_ADDRESS
966#undef DEPTH_TYPE
967
968#undef INTERP_Z
969#undef INTERP_RGB
970#undef INTERP_ALPHA
971#undef INTERP_INDEX
972#undef INTERP_INT_TEX
973#undef INTERP_ATTRIBS
974
975#undef S_SCALE
976#undef T_SCALE
977
978#undef FixedToDepth
979
980#undef NAME
981