1 /* $Id: tritemp.h,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2 
3 /*
4  * Mesa 3-D graphics library
5  * Version:  2.1
6  * Copyright (C) 1995-1996  Brian Paul
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 /*
24  * Triangle Rasterizer Template
25  *
26  * This file is #include'd to generate custom triangle rasterizers.
27  *
28  * The following macros may be defined to indicate what auxillary information
29  * must be interplated across the triangle:
30  *    INTERP_Z      - if defined, interpolate Z values
31  *    INTERP_RGB    - if defined, interpolate RGB values
32  *    INTERP_ALPHA  - if defined, interpolate Alpha values
33  *    INTERP_INDEX  - if defined, interpolate color index values
34  *    INTERP_ST     - if defined, interpolate integer ST texcoords
35  *                         (fast, simple 2-D texture mapping)
36  *    INTERP_STW    - if defined, interpolate float ST texcoords and W
37  *                         (2-D texture maps with perspective correction)
38  *    INTERP_UV     - if defined, interpolate float UV texcoords too
39  *                         (for 3-D, 4-D? texture maps)
40  *
41  * When one can directly address pixels in the color buffer the following
42  * macros can be defined and used to compute pixel addresses during
43  * rasterization (see pRow):
44  *    PIXEL_TYPE          - the datatype of a pixel (GLubyte, GLushort, GLuint)
45  *    BYTES_PER_ROW       - number of bytes per row in the color buffer
46  *    PIXEL_ADDRESS(X,Y)  - returns the address of pixel at (X,Y) where
47  *                          Y==0 at bottom of screen and increases upward.
48  *
49  * Optionally, one may provide one-time setup code per triangle:
50  *    SETUP_CODE    - code which is to be executed once per triangle
51  *
52  * The following macro MUST be defined:
53  *    INNER_LOOP(LEFT,RIGHT,Y) - code to write a span of pixels.
54  *        Something like:
55  *
56  *                    for (x=LEFT; x<RIGHT;x++) {
57  *                       put_pixel(x,Y);
58  *                       // increment fixed point interpolants
59  *                    }
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 
67 /*void triangle( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv )*/
68 {
69    typedef struct {
70         GLint v0, v1;     /* Y(v0) < Y(v1) */
71 	GLfloat dx;	/* X(v1) - X(v0) */
72 	GLfloat dy;	/* Y(v1) - Y(v0) */
73 	GLfixed fdxdy;	/* dx/dy in fixed-point */
74 	GLfixed fsx;	/* first sample point x coord */
75 	GLfixed fsy;
76 	GLfloat adjy;	/* adjust from v[0]->fy to fsy, scaled */
77 	GLint lines;	/* number of lines to be sampled on this edge */
78 	GLfixed fx0;	/* fixed pt X of lower endpoint */
79    } EdgeT;
80 
81    struct vertex_buffer *VB = ctx->VB;
82    EdgeT eMaj, eTop, eBot;
83    GLfloat oneOverArea;
84    int vMin, vMid, vMax;       /* vertex indexes:  Y(vMin)<=Y(vMid)<=Y(vMax) */
85 
86    /* find the order of the 3 vertices along the Y axis */
87    {
88       GLfloat y0 = VB->Win[v0][1];
89       GLfloat y1 = VB->Win[v1][1];
90       GLfloat y2 = VB->Win[v2][1];
91 
92       if (y0<=y1) {
93 	 if (y1<=y2) {
94 	    vMin = v0;   vMid = v1;   vMax = v2;   /* y0<=y1<=y2 */
95 	 }
96 	 else if (y2<=y0) {
97 	    vMin = v2;   vMid = v0;   vMax = v1;   /* y2<=y0<=y1 */
98 	 }
99 	 else {
100 	    vMin = v0;   vMid = v2;   vMax = v1;   /* y0<=y2<=y1 */
101 	 }
102       }
103       else {
104 	 if (y0<=y2) {
105 	    vMin = v1;   vMid = v0;   vMax = v2;   /* y1<=y0<=y2 */
106 	 }
107 	 else if (y2<=y1) {
108 	    vMin = v2;   vMid = v1;   vMax = v0;   /* y2<=y1<=y0 */
109 	 }
110 	 else {
111 	    vMin = v1;   vMid = v2;   vMax = v0;   /* y1<=y2<=y0 */
112 	 }
113       }
114    }
115 
116    /* vertex/edge relationship */
117    eMaj.v0 = vMin;   eMaj.v1 = vMax;   /*TODO: .v1's not needed */
118    eTop.v0 = vMid;   eTop.v1 = vMax;
119    eBot.v0 = vMin;   eBot.v1 = vMid;
120 
121    /* compute deltas for each edge:  vertex[v1] - vertex[v0] */
122    eMaj.dx = VB->Win[vMax][0] - VB->Win[vMin][0];
123    eMaj.dy = VB->Win[vMax][1] - VB->Win[vMin][1];
124    eTop.dx = VB->Win[vMax][0] - VB->Win[vMid][0];
125    eTop.dy = VB->Win[vMax][1] - VB->Win[vMid][1];
126    eBot.dx = VB->Win[vMid][0] - VB->Win[vMin][0];
127    eBot.dy = VB->Win[vMid][1] - VB->Win[vMin][1];
128 
129    /* compute oneOverArea */
130    {
131       GLfloat area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;
132       if (area>-0.05f && area<0.05f) {
133          return;  /* very small; CULLED */
134       }
135       oneOverArea = 1.0F / area;
136    }
137 
138    /* Edge setup.  For a triangle strip these could be reused... */
139    {
140       /* fixed point Y coordinates */
141       GLfixed vMin_fx = FloatToFixed(VB->Win[vMin][0] + 0.5F);
142       GLfixed vMin_fy = FloatToFixed(VB->Win[vMin][1] - 0.5F);
143       GLfixed vMid_fx = FloatToFixed(VB->Win[vMid][0] + 0.5F);
144       GLfixed vMid_fy = FloatToFixed(VB->Win[vMid][1] - 0.5F);
145       GLfixed vMax_fy = FloatToFixed(VB->Win[vMax][1] - 0.5F);
146 
147       eMaj.fsy = FixedCeil(vMin_fy);
148       eMaj.lines = FixedToInt(vMax_fy + FIXED_ONE - FIXED_EPSILON - eMaj.fsy);
149       if (eMaj.lines > 0) {
150          GLfloat dxdy = eMaj.dx / eMaj.dy;
151          eMaj.fdxdy = SignedFloatToFixed(dxdy);
152          eMaj.adjy = (GLfloat) (eMaj.fsy - vMin_fy);  /* SCALED! */
153          eMaj.fx0 = vMin_fx;
154          eMaj.fsx = eMaj.fx0 + (GLfixed) (eMaj.adjy * dxdy);
155       }
156       else {
157          return;  /*CULLED*/
158       }
159 
160       eTop.fsy = FixedCeil(vMid_fy);
161       eTop.lines = FixedToInt(vMax_fy + FIXED_ONE - FIXED_EPSILON - eTop.fsy);
162       if (eTop.lines > 0) {
163          GLfloat dxdy = eTop.dx / eTop.dy;
164          eTop.fdxdy = SignedFloatToFixed(dxdy);
165          eTop.adjy = (GLfloat) (eTop.fsy - vMid_fy); /* SCALED! */
166          eTop.fx0 = vMid_fx;
167          eTop.fsx = eTop.fx0 + (GLfixed) (eTop.adjy * dxdy);
168       }
169 
170       eBot.fsy = FixedCeil(vMin_fy);
171       eBot.lines = FixedToInt(vMid_fy + FIXED_ONE - FIXED_EPSILON - eBot.fsy);
172       if (eBot.lines > 0) {
173          GLfloat dxdy = eBot.dx / eBot.dy;
174          eBot.fdxdy = SignedFloatToFixed(dxdy);
175          eBot.adjy = (GLfloat) (eBot.fsy - vMin_fy);  /* SCALED! */
176          eBot.fx0 = vMin_fx;
177          eBot.fsx = eBot.fx0 + (GLfixed) (eBot.adjy * dxdy);
178       }
179    }
180 
181    /*
182     * Conceptually, we view a triangle as two subtriangles
183     * separated by a perfectly horizontal line.  The edge that is
184     * intersected by this line is one with maximal absolute dy; we
185     * call it a ``major'' edge.  The other two edges are the
186     * ``top'' edge (for the upper subtriangle) and the ``bottom''
187     * edge (for the lower subtriangle).  If either of these two
188     * edges is horizontal or very close to horizontal, the
189     * corresponding subtriangle might cover zero sample points;
190     * we take care to handle such cases, for performance as well
191     * as correctness.
192     *
193     * By stepping rasterization parameters along the major edge,
194     * we can avoid recomputing them at the discontinuity where
195     * the top and bottom edges meet.  However, this forces us to
196     * be able to scan both left-to-right and right-to-left.
197     * Also, we must determine whether the major edge is at the
198     * left or right side of the triangle.  We do this by
199     * computing the magnitude of the cross-product of the major
200     * and top edges.  Since this magnitude depends on the sine of
201     * the angle between the two edges, its sign tells us whether
202     * we turn to the left or to the right when travelling along
203     * the major edge to the top edge, and from this we infer
204     * whether the major edge is on the left or the right.
205     *
206     * Serendipitously, this cross-product magnitude is also a
207     * value we need to compute the iteration parameter
208     * derivatives for the triangle, and it can be used to perform
209     * backface culling because its sign tells us whether the
210     * triangle is clockwise or counterclockwise.  In this code we
211     * refer to it as ``area'' because it's also proportional to
212     * the pixel area of the triangle.
213     */
214 
215    {
216       GLint ltor;		/* true if scanning left-to-right */
217 #if INTERP_Z
218       GLfloat dzdx, dzdy;      GLfixed fdzdx;
219 #endif
220 #if INTERP_RGB
221       GLfloat drdx, drdy;      GLfixed fdrdx;
222       GLfloat dgdx, dgdy;      GLfixed fdgdx;
223       GLfloat dbdx, dbdy;      GLfixed fdbdx;
224 #endif
225 #if INTERP_ALPHA
226       GLfloat dadx, dady;      GLfixed fdadx;
227 #endif
228 #if INTERP_INDEX
229       GLfloat didx, didy;      GLfixed fdidx;
230 #endif
231 #if INTERP_ST
232       GLfloat dsdx, dsdy;      GLfixed fdsdx;
233       GLfloat dtdx, dtdy;      GLfixed fdtdx;
234 #endif
235 #if INTERP_STW
236       GLfloat dsdx, dsdy;
237       GLfloat dtdx, dtdy;
238       GLfloat dwdx, dwdy;
239 #endif
240 #if INTERP_UV
241       GLfloat dudx, dudy;
242       GLfloat dvdx, dvdy;
243 #endif
244 
245       /*
246        * Execute user-supplied setup code
247        */
248 #ifdef SETUP_CODE
249       SETUP_CODE
250 #endif
251 
252       ltor = (oneOverArea < 0.0F);
253 
254       /* compute d?/dx and d?/dy derivatives */
255 #if INTERP_Z
256       {
257          GLfloat eMaj_dz, eBot_dz;
258          eMaj_dz = VB->Win[vMax][2] - VB->Win[vMin][2];
259          eBot_dz = VB->Win[vMid][2] - VB->Win[vMin][2];
260          dzdx = oneOverArea * (eMaj_dz * eBot.dy - eMaj.dy * eBot_dz);
261          if (dzdx>DEPTH_SCALE || dzdx<-DEPTH_SCALE) {
262             /* probably a sliver triangle */
263             dzdx = 0.0;
264             dzdy = 0.0;
265          }
266          else {
267             dzdy = oneOverArea * (eMaj.dx * eBot_dz - eMaj_dz * eBot.dx);
268          }
269 #if DEPTH_BITS==16
270          fdzdx = SignedFloatToFixed(dzdx);
271 #else
272          fdzdx = (GLint) dzdx;
273 #endif
274       }
275 #endif
276 #if INTERP_RGB
277       {
278          GLfloat eMaj_dr, eBot_dr;
279          eMaj_dr = FixedToFloat( VB->Color[vMax][0] - VB->Color[vMin][0] );
280          eBot_dr = FixedToFloat( VB->Color[vMid][0] - VB->Color[vMin][0] );
281          drdx = oneOverArea * (eMaj_dr * eBot.dy - eMaj.dy * eBot_dr);
282          fdrdx = SignedFloatToFixed(drdx);
283          drdy = oneOverArea * (eMaj.dx * eBot_dr - eMaj_dr * eBot.dx);
284       }
285       {
286          GLfloat eMaj_dg, eBot_dg;
287          eMaj_dg = FixedToFloat( VB->Color[vMax][1] - VB->Color[vMin][1] );
288 	 eBot_dg = FixedToFloat( VB->Color[vMid][1] - VB->Color[vMin][1] );
289          dgdx = oneOverArea * (eMaj_dg * eBot.dy - eMaj.dy * eBot_dg);
290          fdgdx = SignedFloatToFixed(dgdx);
291          dgdy = oneOverArea * (eMaj.dx * eBot_dg - eMaj_dg * eBot.dx);
292       }
293       {
294          GLfloat eMaj_db, eBot_db;
295          eMaj_db = FixedToFloat( VB->Color[vMax][2] - VB->Color[vMin][2] );
296          eBot_db = FixedToFloat( VB->Color[vMid][2] - VB->Color[vMin][2] );
297          dbdx = oneOverArea * (eMaj_db * eBot.dy - eMaj.dy * eBot_db);
298          fdbdx = SignedFloatToFixed(dbdx);
299 	 dbdy = oneOverArea * (eMaj.dx * eBot_db - eMaj_db * eBot.dx);
300       }
301 #endif
302 #if INTERP_ALPHA
303       {
304          GLfloat eMaj_da, eBot_da;
305          eMaj_da = FixedToFloat( VB->Color[vMax][3] - VB->Color[vMin][3] );
306          eBot_da = FixedToFloat( VB->Color[vMid][3] - VB->Color[vMin][3] );
307          dadx = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
308          fdadx = SignedFloatToFixed(dadx);
309          dady = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
310       }
311 #endif
312 #if INTERP_INDEX
313       {
314          GLfloat eMaj_di, eBot_di;
315          eMaj_di = (GLint) VB->Index[vMax] - (GLint) VB->Index[vMin];
316          eBot_di = (GLint) VB->Index[vMid] - (GLint) VB->Index[vMin];
317          didx = oneOverArea * (eMaj_di * eBot.dy - eMaj.dy * eBot_di);
318          fdidx = SignedFloatToFixed(didx);
319          didy = oneOverArea * (eMaj.dx * eBot_di - eMaj_di * eBot.dx);
320       }
321 #endif
322 #if INTERP_ST
323       {
324          GLfloat eMaj_ds, eBot_ds;
325          eMaj_ds = (VB->TexCoord[vMax][0] - VB->TexCoord[vMin][0]) * S_SCALE;
326          eBot_ds = (VB->TexCoord[vMid][0] - VB->TexCoord[vMin][0]) * T_SCALE;
327          dsdx = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
328          fdsdx = SignedFloatToFixed(dsdx);
329          dsdy = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
330       }
331       {
332          GLfloat eMaj_dt, eBot_dt;
333          eMaj_dt = (VB->TexCoord[vMax][1] - VB->TexCoord[vMin][1]) * S_SCALE;
334          eBot_dt = (VB->TexCoord[vMid][1] - VB->TexCoord[vMin][1]) * T_SCALE;
335          dtdx = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
336          fdtdx = SignedFloatToFixed(dtdx);
337          dtdy = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
338       }
339 #endif
340 #if INTERP_STW
341       {
342          GLfloat wMax = 1.0F / VB->Clip[vMax][3];
343          GLfloat wMin = 1.0F / VB->Clip[vMin][3];
344          GLfloat wMid = 1.0F / VB->Clip[vMid][3];
345          GLfloat eMaj_dw, eBot_dw;
346          GLfloat eMaj_ds, eBot_ds;
347          GLfloat eMaj_dt, eBot_dt;
348 #if INTERP_UV
349          GLfloat eMaj_du, eBot_du;
350          GLfloat eMaj_dv, eBot_dv;
351 #endif
352          eMaj_dw = wMax - wMin;
353          eBot_dw = wMid - wMin;
354          dwdx = oneOverArea * (eMaj_dw * eBot.dy - eMaj.dy * eBot_dw);
355          dwdy = oneOverArea * (eMaj.dx * eBot_dw - eMaj_dw * eBot.dx);
356 
357          eMaj_ds = VB->TexCoord[vMax][0]*wMax - VB->TexCoord[vMin][0]*wMin;
358          eBot_ds = VB->TexCoord[vMid][0]*wMid - VB->TexCoord[vMin][0]*wMin;
359          dsdx = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
360          dsdy = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
361 
362          eMaj_dt = VB->TexCoord[vMax][1]*wMax - VB->TexCoord[vMin][1]*wMin;
363          eBot_dt = VB->TexCoord[vMid][1]*wMid - VB->TexCoord[vMin][1]*wMin;
364          dtdx = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
365          dtdy = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
366 #if INTERP_UV
367          eMaj_du = VB->TexCoord[vMax][2]*wMax - VB->TexCoord[vMin][2]*wMin;
368          eBot_du = VB->TexCoord[vMid][2]*wMid - VB->TexCoord[vMin][2]*wMin;
369          dudx = oneOverArea * (eMaj_du * eBot.dy - eMaj.dy * eBot_du);
370          dudy = oneOverArea * (eMaj.dx * eBot_du - eMaj_du * eBot.dx);
371 
372          /* Note: don't divide V component by W */
373          eMaj_dv = VB->TexCoord[vMax][3] - VB->TexCoord[vMin][3];
374          eBot_dv = VB->TexCoord[vMid][3] - VB->TexCoord[vMin][3];
375          dvdx = oneOverArea * (eMaj_dv * eBot.dy - eMaj.dy * eBot_dv);
376          dvdy = oneOverArea * (eMaj.dx * eBot_dv - eMaj_dv * eBot.dx);
377 #endif
378       }
379 #endif
380 
381       /*
382        * We always sample at pixel centers.  However, we avoid
383        * explicit half-pixel offsets in this code by incorporating
384        * the proper offset in each of x and y during the
385        * transformation to window coordinates.
386        *
387        * We also apply the usual rasterization rules to prevent
388        * cracks and overlaps.  A pixel is considered inside a
389        * subtriangle if it meets all of four conditions: it is on or
390        * to the right of the left edge, strictly to the left of the
391        * right edge, on or below the top edge, and strictly above
392        * the bottom edge.  (Some edges may be degenerate.)
393        *
394        * The following discussion assumes left-to-right scanning
395        * (that is, the major edge is on the left); the right-to-left
396        * case is a straightforward variation.
397        *
398        * We start by finding the half-integral y coordinate that is
399        * at or below the top of the triangle.  This gives us the
400        * first scan line that could possibly contain pixels that are
401        * inside the triangle.
402        *
403        * Next we creep down the major edge until we reach that y,
404        * and compute the corresponding x coordinate on the edge.
405        * Then we find the half-integral x that lies on or just
406        * inside the edge.  This is the first pixel that might lie in
407        * the interior of the triangle.  (We won't know for sure
408        * until we check the other edges.)
409        *
410        * As we rasterize the triangle, we'll step down the major
411        * edge.  For each step in y, we'll move an integer number
412        * of steps in x.  There are two possible x step sizes, which
413        * we'll call the ``inner'' step (guaranteed to land on the
414        * edge or inside it) and the ``outer'' step (guaranteed to
415        * land on the edge or outside it).  The inner and outer steps
416        * differ by one.  During rasterization we maintain an error
417        * term that indicates our distance from the true edge, and
418        * select either the inner step or the outer step, whichever
419        * gets us to the first pixel that falls inside the triangle.
420        *
421        * All parameters (z, red, etc.) as well as the buffer
422        * addresses for color and z have inner and outer step values,
423        * so that we can increment them appropriately.  This method
424        * eliminates the need to adjust parameters by creeping a
425        * sub-pixel amount into the triangle at each scanline.
426        */
427 
428       {
429          int subTriangle;
430          GLfixed fx, fxLeftEdge, fxRightEdge, fdxLeftEdge, fdxRightEdge;
431          GLfixed fdxOuter;
432          int idxOuter;
433          float dxOuter;
434          GLfixed fError, fdError;
435          float adjx, adjy;
436          GLfixed fy;
437          int iy;
438 #ifdef PIXEL_ADDRESS
439          PIXEL_TYPE *pRow;
440          int dPRowOuter, dPRowInner;  /* offset in bytes */
441 #endif
442 #if INTERP_Z
443          GLdepth *zRow;
444          int dZRowOuter, dZRowInner;  /* offset in bytes */
445          GLfixed fz, fdzOuter, fdzInner;
446 #endif
447 #if INTERP_RGB
448          GLfixed fr, fdrOuter, fdrInner;
449          GLfixed fg, fdgOuter, fdgInner;
450          GLfixed fb, fdbOuter, fdbInner;
451 #endif
452 #if INTERP_ALPHA
453          GLfixed fa, fdaOuter, fdaInner;
454 #endif
455 #if INTERP_INDEX
456          GLfixed fi, fdiOuter, fdiInner;
457 #endif
458 #if INTERP_ST
459          GLfixed fs, fdsOuter, fdsInner;
460          GLfixed ft, fdtOuter, fdtInner;
461 #endif
462 #if INTERP_STW
463          GLfloat sLeft, dsOuter, dsInner;
464          GLfloat tLeft, dtOuter, dtInner;
465          GLfloat wLeft, dwOuter, dwInner;
466 #endif
467 #if INTERP_UV
468          GLfloat uLeft, duOuter, duInner;
469          GLfloat vLeft, dvOuter, dvInner;
470 #endif
471 
472          for (subTriangle=0; subTriangle<=1; subTriangle++) {
473             EdgeT *eLeft, *eRight;
474             int setupLeft, setupRight;
475             int lines;
476 
477             if (subTriangle==0) {
478                /* bottom half */
479                if (ltor) {
480                   eLeft = &eMaj;
481                   eRight = &eBot;
482                   lines = eRight->lines;
483                   setupLeft = 1;
484                   setupRight = 1;
485                }
486                else {
487                   eLeft = &eBot;
488                   eRight = &eMaj;
489                   lines = eLeft->lines;
490                   setupLeft = 1;
491                   setupRight = 1;
492                }
493             }
494             else {
495                /* top half */
496                if (ltor) {
497                   eLeft = &eMaj;
498                   eRight = &eTop;
499                   lines = eRight->lines;
500                   setupLeft = 0;
501                   setupRight = 1;
502                }
503                else {
504                   eLeft = &eTop;
505                   eRight = &eMaj;
506                   lines = eLeft->lines;
507                   setupLeft = 1;
508                   setupRight = 0;
509                }
510                if (lines==0) return;
511             }
512 
513             if (setupLeft && eLeft->lines>0) {
514                GLint vLower;
515                GLfixed fsx = eLeft->fsx;
516                fx = FixedCeil(fsx);
517                fError = fx - fsx - FIXED_ONE;
518                fxLeftEdge = fsx - FIXED_EPSILON;
519                fdxLeftEdge = eLeft->fdxdy;
520                fdxOuter = FixedFloor(fdxLeftEdge - FIXED_EPSILON);
521                fdError = fdxOuter - fdxLeftEdge + FIXED_ONE;
522                idxOuter = FixedToInt(fdxOuter);
523                dxOuter = (float) idxOuter;
524 
525                fy = eLeft->fsy;
526                iy = FixedToInt(fy);
527 
528                adjx = (float)(fx - eLeft->fx0);  /* SCALED! */
529                adjy = eLeft->adjy;		 /* SCALED! */
530 
531                vLower = eLeft->v0;
532 
533 #ifdef PIXEL_ADDRESS
534                {
535                   pRow = PIXEL_ADDRESS( FixedToInt(fxLeftEdge), iy );
536                   dPRowOuter = -BYTES_PER_ROW + idxOuter * sizeof(PIXEL_TYPE);
537                   /* negative because Y=0 at bottom and increases upward */
538                }
539 #endif
540                /*
541                 * Now we need the set of parameter (z, color, etc.) values at
542                 * the point (fx, fy).  This gives us properly-sampled parameter
543                 * values that we can step from pixel to pixel.  Furthermore,
544                 * although we might have intermediate results that overflow
545                 * the normal parameter range when we step temporarily outside
546                 * the triangle, we shouldn't overflow or underflow for any
547                 * pixel that's actually inside the triangle.
548                 */
549 
550 #if INTERP_Z
551                {
552                   GLfloat z0;
553                   z0 = VB->Win[vLower][2] + ctx->PolygonZoffset;
554 #if DEPTH_BITS==16
555                   /* interpolate fixed-pt values */
556                   fz = (GLfixed)(z0 * FIXED_SCALE + dzdx * adjx + dzdy * adjy) + FIXED_HALF;
557                   fdzOuter = SignedFloatToFixed(dzdy + dxOuter * dzdx);
558 #else
559                   /* interpolate depth values exactly */
560                   fz = (GLint) (z0 + dzdx*FixedToFloat(adjx) + dzdy*FixedToFloat(adjy));
561                   fdzOuter = (GLint) (dzdy + dxOuter * dzdx);
562 #endif
563                   zRow = Z_ADDRESS( ctx, FixedToInt(fxLeftEdge), iy );
564                   dZRowOuter = (ctx->Buffer->Width + idxOuter) * sizeof(GLdepth);
565                }
566 #endif
567 #if INTERP_RGB
568                fr = (GLfixed)(VB->Color[vLower][0] + drdx * adjx + drdy * adjy)
569                     + FIXED_HALF;
570                fdrOuter = SignedFloatToFixed(drdy + dxOuter * drdx);
571 
572                fg = (GLfixed)(VB->Color[vLower][1] + dgdx * adjx + dgdy * adjy)
573                     + FIXED_HALF;
574                fdgOuter = SignedFloatToFixed(dgdy + dxOuter * dgdx);
575 
576                fb = (GLfixed)(VB->Color[vLower][2] + dbdx * adjx + dbdy * adjy)
577                     + FIXED_HALF;
578                fdbOuter = SignedFloatToFixed(dbdy + dxOuter * dbdx);
579 #endif
580 #if INTERP_ALPHA
581                fa = (GLfixed)(VB->Color[vLower][3] + dadx * adjx + dady * adjy)
582                     + FIXED_HALF;
583                fdaOuter = SignedFloatToFixed(dady + dxOuter * dadx);
584 #endif
585 #if INTERP_INDEX
586                fi = (GLfixed)(VB->Index[vLower] * FIXED_SCALE + didx * adjx
587                               + didy * adjy) + FIXED_HALF;
588                fdiOuter = SignedFloatToFixed(didy + dxOuter * didx);
589 #endif
590 #if INTERP_ST
591                {
592                   GLfloat s0, t0;
593                   s0 = VB->TexCoord[vLower][0] * S_SCALE;
594                   fs = (GLfixed)(s0 * FIXED_SCALE + dsdx * adjx + dsdy * adjy) + FIXED_HALF;
595                   fdsOuter = SignedFloatToFixed(dsdy + dxOuter * dsdx);
596                   t0 = VB->TexCoord[vLower][1] * T_SCALE;
597                   ft = (GLfixed)(t0 * FIXED_SCALE + dtdx * adjx + dtdy * adjy) + FIXED_HALF;
598                   fdtOuter = SignedFloatToFixed(dtdy + dxOuter * dtdx);
599                }
600 #endif
601 #if INTERP_STW
602                {
603                   GLfloat w0 = 1.0F / VB->Clip[vLower][3];
604                   GLfloat s0, t0, u0, v0;
605                   wLeft = w0 + (dwdx * adjx + dwdy * adjy) * (1.0F/FIXED_SCALE);
606 		  dwOuter = dwdy + dxOuter * dwdx;
607                   s0 = VB->TexCoord[vLower][0] * w0;
608                   sLeft = s0 + (dsdx * adjx + dsdy * adjy) * (1.0F/FIXED_SCALE);
609                   dsOuter = dsdy + dxOuter * dsdx;
610                   t0 = VB->TexCoord[vLower][1] * w0;
611                   tLeft = t0 + (dtdx * adjx + dtdy * adjy) * (1.0F/FIXED_SCALE);
612                   dtOuter = dtdy + dxOuter * dtdx;
613 #if INTERP_UV
614                   u0 = VB->TexCoord[vLower][2] * w0;
615                   uLeft = u0 + (dudx * adjx + dudy * adjy) * (1.0F/FIXED_SCALE);
616                   duOuter = dudy + dxOuter * dudx;
617                   /* Note: don't divide V component by W */
618                   v0 = VB->TexCoord[vLower][3];
619                   vLeft = v0 + (dvdx * adjx + dvdy * adjy) * (1.0F/FIXED_SCALE);
620                   dvOuter = dvdy + dxOuter * dvdx;
621 #endif
622                }
623 #endif
624 
625             } /*if setupLeft*/
626 
627 
628             if (setupRight && eRight->lines>0) {
629                fxRightEdge = eRight->fsx - FIXED_EPSILON;
630                fdxRightEdge = eRight->fdxdy;
631             }
632 
633             if (lines==0) {
634                continue;
635             }
636 
637 
638             /* Rasterize setup */
639 #ifdef PIXEL_ADDRESS
640             dPRowInner = dPRowOuter + sizeof(PIXEL_TYPE);
641 #endif
642 #if INTERP_Z
643             dZRowInner = dZRowOuter + sizeof(GLdepth);
644             fdzInner = fdzOuter + fdzdx;
645 #endif
646 #if INTERP_RGB
647             fdrInner = fdrOuter + fdrdx;
648             fdgInner = fdgOuter + fdgdx;
649             fdbInner = fdbOuter + fdbdx;
650 #endif
651 #if INTERP_ALPHA
652             fdaInner = fdaOuter + fdadx;
653 #endif
654 #if INTERP_INDEX
655             fdiInner = fdiOuter + fdidx;
656 #endif
657 #if INTERP_ST
658             fdsInner = fdsOuter + fdsdx;
659             fdtInner = fdtOuter + fdtdx;
660 #endif
661 #if INTERP_STW
662 	    dwInner = dwOuter + dwdx;
663 	    dsInner = dsOuter + dsdx;
664 	    dtInner = dtOuter + dtdx;
665 #if INTERP_UV
666 	    duInner = duOuter + dudx;
667 	    dvInner = dvOuter + dvdx;
668 #endif
669 #endif
670 
671             while (lines>0) {
672                /* initialize the span interpolants to the leftmost value */
673                /* ff = fixed-pt fragment */
674 #if INTERP_Z
675                GLfixed ffz = fz;
676                /*GLdepth *zp = zRow;*/
677 #endif
678 #if INTERP_RGB
679                GLfixed ffr = fr,  ffg = fg,  ffb = fb;
680 #endif
681 #if INTERP_ALPHA
682                GLfixed ffa = fa;
683 #endif
684 #if INTERP_INDEX
685                GLfixed ffi = fi;
686 #endif
687 #if INTERP_ST
688                GLfixed ffs = fs,  fft = ft;
689 #endif
690 #if INTERP_STW
691                GLfloat ss = sLeft,  tt = tLeft,  ww = wLeft;
692 #endif
693 #if INTERP_UV
694                GLfloat uu = uLeft,  vv = vLeft;
695 #endif
696                GLint left = FixedToInt(fxLeftEdge);
697                GLint right = FixedToInt(fxRightEdge);
698 
699 #if INTERP_RGB
700                /* need this to accomodate round-off errors */
701                if (ffr<0) ffr = 0;
702                if (ffg<0) ffg = 0;
703                if (ffb<0) ffb = 0;
704 #endif
705 #if INTERP_ALPHA
706                if (ffa<0) ffa = 0;
707 #endif
708 #if INTERP_INDEX
709                if (ffi<0) ffi = 0;
710 #endif
711 
712                INNER_LOOP( left, right, iy );
713 
714                /*
715                 * Advance to the next scan line.  Compute the
716                 * new edge coordinates, and adjust the
717                 * pixel-center x coordinate so that it stays
718                 * on or inside the major edge.
719                 */
720                iy++;
721                lines--;
722 
723                fxLeftEdge += fdxLeftEdge;
724                fxRightEdge += fdxRightEdge;
725 
726 
727                fError += fdError;
728                if (fError >= 0) {
729                   fError -= FIXED_ONE;
730 #ifdef PIXEL_ADDRESS
731                   pRow = (PIXEL_TYPE*) ((GLubyte*)pRow + dPRowOuter);
732 #endif
733 #if INTERP_Z
734                   zRow = (GLdepth*) ((GLubyte*)zRow + dZRowOuter);
735                   fz += fdzOuter;
736 #endif
737 #if INTERP_RGB
738                   fr += fdrOuter;   fg += fdgOuter;   fb += fdbOuter;
739 #endif
740 #if INTERP_ALPHA
741                   fa += fdaOuter;
742 #endif
743 #if INTERP_INDEX
744                   fi += fdiOuter;
745 #endif
746 #if INTERP_ST
747                   fs += fdsOuter;   ft += fdtOuter;
748 #endif
749 #if INTERP_STW
750 		  sLeft += dsOuter;
751 		  tLeft += dtOuter;
752 		  wLeft += dwOuter;
753 #endif
754 #if INTERP_UV
755 		  uLeft += duOuter;
756 		  vLeft += dvOuter;
757 #endif
758                }
759                else {
760 #ifdef PIXEL_ADDRESS
761                   pRow = (PIXEL_TYPE*) ((GLubyte*)pRow + dPRowInner);
762 #endif
763 #if INTERP_Z
764                   zRow = (GLdepth*) ((GLubyte*)zRow + dZRowInner);
765                   fz += fdzInner;
766 #endif
767 #if INTERP_RGB
768                   fr += fdrInner;   fg += fdgInner;   fb += fdbInner;
769 #endif
770 #if INTERP_ALPHA
771                   fa += fdaInner;
772 #endif
773 #if INTERP_INDEX
774                   fi += fdiInner;
775 #endif
776 #if INTERP_ST
777                   fs += fdsInner;   ft += fdtInner;
778 #endif
779 #if INTERP_STW
780 		  sLeft += dsInner;
781 		  tLeft += dtInner;
782 		  wLeft += dwInner;
783 #endif
784 #if INTERP_UV
785 		  uLeft += duInner;
786 		  vLeft += dvInner;
787 #endif
788                }
789             } /*while lines>0*/
790 
791          } /* for subTriangle */
792 
793       }
794    }
795 }
796 
797 #undef SETUP_CODE
798 #undef INNER_LOOP
799 
800 #undef PIXEL_TYPE
801 #undef BYTES_PER_ROW
802 #undef PIXEL_ADDRESS
803 
804 #undef INTERP_Z
805 #undef INTERP_RGB
806 #undef INTERP_ALPHA
807 #undef INTERP_INDEX
808 #undef INTERP_ST
809 #undef INTERP_STW
810 #undef INTERP_UV
811 
812 #undef S_SCALE
813 #undef T_SCALE
814