some code clean-ups
[mesa.git] / src / mesa / swrast / s_tritemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 interplated across the triangle:
32 * INTERP_Z - if defined, interpolate Z values
33 * INTERP_FOG - if defined, interpolate fog values
34 * INTERP_RGB - if defined, interpolate RGB values
35 * INTERP_ALPHA - if defined, interpolate Alpha values (req's INTERP_RGB)
36 * INTERP_SPEC - if defined, interpolate specular RGB values
37 * INTERP_INDEX - if defined, interpolate color index values
38 * INTERP_INT_TEX - if defined, interpolate integer ST texcoords
39 * (fast, simple 2-D texture mapping)
40 * INTERP_TEX - if defined, interpolate set 0 float STRQ texcoords
41 * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red)
42 * INTERP_MULTITEX - if defined, interpolate N units of STRQ texcoords
43 *
44 * When one can directly address pixels in the color buffer the following
45 * macros can be defined and used to compute pixel addresses during
46 * rasterization (see pRow):
47 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
48 * BYTES_PER_ROW - number of bytes per row in the color buffer
49 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
50 * Y==0 at bottom of screen and increases upward.
51 *
52 * Similarly, for direct depth buffer access, this type is used for depth
53 * buffer addressing:
54 * DEPTH_TYPE - either GLushort or GLuint
55 *
56 * Optionally, one may provide one-time setup code per triangle:
57 * SETUP_CODE - code which is to be executed once per triangle
58 * CLEANUP_CODE - code to execute at end of triangle
59 *
60 * The following macro MUST be defined:
61 * RENDER_SPAN(span) - code to write a span of pixels.
62 *
63 * This code was designed for the origin to be in the lower-left corner.
64 *
65 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
66 *
67 *
68 * Some notes on rasterization accuracy:
69 *
70 * This code uses fixed point arithmetic (the GLfixed type) to iterate
71 * over the triangle edges and interpolate ancillary data (such as Z,
72 * color, secondary color, etc). The number of fractional bits in
73 * GLfixed and the value of SUB_PIXEL_BITS has a direct bearing on the
74 * accuracy of rasterization.
75 *
76 * If SUB_PIXEL_BITS=4 then we'll snap the vertices to the nearest
77 * 1/16 of a pixel. If we're walking up a long, nearly vertical edge
78 * (dx=1/16, dy=1024) we'll need 4 + 10 = 14 fractional bits in
79 * GLfixed to walk the edge without error. If the maximum viewport
80 * height is 4K pixels, then we'll need 4 + 12 = 16 fractional bits.
81 *
82 * Historically, Mesa has used 11 fractional bits in GLfixed, snaps
83 * vertices to 1/16 pixel and allowed a maximum viewport height of 2K
84 * pixels. 11 fractional bits is actually insufficient for accurately
85 * rasterizing some triangles. More recently, the maximum viewport
86 * height was increased to 4K pixels. Thus, Mesa should be using 16
87 * fractional bits in GLfixed. Unfortunately, there may be some issues
88 * with setting FIXED_FRAC_BITS=16, such as multiplication overflow.
89 * This will have to be examined in some detail...
90 *
91 * For now, if you find rasterization errors, particularly with tall,
92 * sliver triangles, try increasing FIXED_FRAC_BITS and/or decreasing
93 * SUB_PIXEL_BITS.
94 */
95
96
97 /*
98 * ColorTemp is used for intermediate color values.
99 */
100 #if CHAN_TYPE == GL_FLOAT
101 #define ColorTemp GLfloat
102 #else
103 #define ColorTemp GLint /* same as GLfixed */
104 #endif
105
106 /*
107 * Either loop over all texture units, or just use unit zero.
108 */
109 #ifdef INTERP_MULTITEX
110 #define TEX_UNIT_LOOP(CODE) \
111 { \
112 GLuint u; \
113 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
114 if (ctx->Texture._EnabledCoordUnits & (1 << u)) { \
115 CODE \
116 } \
117 } \
118 }
119 #define INTERP_TEX
120 #elif defined(INTERP_TEX)
121 #define TEX_UNIT_LOOP(CODE) \
122 { \
123 const GLuint u = 0; \
124 CODE \
125 }
126 #endif
127
128
129 static void NAME(GLcontext *ctx, const SWvertex *v0,
130 const SWvertex *v1,
131 const SWvertex *v2 )
132 {
133 typedef struct {
134 const SWvertex *v0, *v1; /* Y(v0) < Y(v1) */
135 GLfloat dx; /* X(v1) - X(v0) */
136 GLfloat dy; /* Y(v1) - Y(v0) */
137 GLfixed fdxdy; /* dx/dy in fixed-point */
138 GLfixed fsx; /* first sample point x coord */
139 GLfixed fsy;
140 GLfloat adjy; /* adjust from v[0]->fy to fsy, scaled */
141 GLint lines; /* number of lines to be sampled on this edge */
142 GLfixed fx0; /* fixed pt X of lower endpoint */
143 } EdgeT;
144
145 #ifdef INTERP_Z
146 const GLint depthBits = ctx->Visual.depthBits;
147 const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
148 const GLfloat maxDepth = ctx->DepthMaxF;
149 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
150 #endif
151 EdgeT eMaj, eTop, eBot;
152 GLfloat oneOverArea;
153 const SWvertex *vMin, *vMid, *vMax; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
154 GLfloat bf = SWRAST_CONTEXT(ctx)->_BackfaceSign;
155 const GLint snapMask = ~((FIXED_ONE / (1 << SUB_PIXEL_BITS)) - 1); /* for x/y coord snapping */
156 GLfixed vMin_fx, vMin_fy, vMid_fx, vMid_fy, vMax_fx, vMax_fy;
157
158 struct sw_span span;
159
160 INIT_SPAN(span, GL_POLYGON, 0, 0, 0);
161
162 #ifdef INTERP_Z
163 (void) fixedToDepthShift;
164 #endif
165
166 /*
167 printf("%s()\n", __FUNCTION__);
168 printf(" %g, %g, %g\n", v0->win[0], v0->win[1], v0->win[2]);
169 printf(" %g, %g, %g\n", v1->win[0], v1->win[1], v1->win[2]);
170 printf(" %g, %g, %g\n", v2->win[0], v2->win[1], v2->win[2]);
171 */
172
173 /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
174 * And find the order of the 3 vertices along the Y axis.
175 */
176 {
177 const GLfixed fy0 = FloatToFixed(v0->win[1] - 0.5F) & snapMask;
178 const GLfixed fy1 = FloatToFixed(v1->win[1] - 0.5F) & snapMask;
179 const GLfixed fy2 = FloatToFixed(v2->win[1] - 0.5F) & snapMask;
180
181 if (fy0 <= fy1) {
182 if (fy1 <= fy2) {
183 /* y0 <= y1 <= y2 */
184 vMin = v0; vMid = v1; vMax = v2;
185 vMin_fy = fy0; vMid_fy = fy1; vMax_fy = fy2;
186 }
187 else if (fy2 <= fy0) {
188 /* y2 <= y0 <= y1 */
189 vMin = v2; vMid = v0; vMax = v1;
190 vMin_fy = fy2; vMid_fy = fy0; vMax_fy = fy1;
191 }
192 else {
193 /* y0 <= y2 <= y1 */
194 vMin = v0; vMid = v2; vMax = v1;
195 vMin_fy = fy0; vMid_fy = fy2; vMax_fy = fy1;
196 bf = -bf;
197 }
198 }
199 else {
200 if (fy0 <= fy2) {
201 /* y1 <= y0 <= y2 */
202 vMin = v1; vMid = v0; vMax = v2;
203 vMin_fy = fy1; vMid_fy = fy0; vMax_fy = fy2;
204 bf = -bf;
205 }
206 else if (fy2 <= fy1) {
207 /* y2 <= y1 <= y0 */
208 vMin = v2; vMid = v1; vMax = v0;
209 vMin_fy = fy2; vMid_fy = fy1; vMax_fy = fy0;
210 bf = -bf;
211 }
212 else {
213 /* y1 <= y2 <= y0 */
214 vMin = v1; vMid = v2; vMax = v0;
215 vMin_fy = fy1; vMid_fy = fy2; vMax_fy = fy0;
216 }
217 }
218
219 /* fixed point X coords */
220 vMin_fx = FloatToFixed(vMin->win[0] + 0.5F) & snapMask;
221 vMid_fx = FloatToFixed(vMid->win[0] + 0.5F) & snapMask;
222 vMax_fx = FloatToFixed(vMax->win[0] + 0.5F) & snapMask;
223 }
224
225 /* vertex/edge relationship */
226 eMaj.v0 = vMin; eMaj.v1 = vMax; /*TODO: .v1's not needed */
227 eTop.v0 = vMid; eTop.v1 = vMax;
228 eBot.v0 = vMin; eBot.v1 = vMid;
229
230 /* compute deltas for each edge: vertex[upper] - vertex[lower] */
231 eMaj.dx = FixedToFloat(vMax_fx - vMin_fx);
232 eMaj.dy = FixedToFloat(vMax_fy - vMin_fy);
233 eTop.dx = FixedToFloat(vMax_fx - vMid_fx);
234 eTop.dy = FixedToFloat(vMax_fy - vMid_fy);
235 eBot.dx = FixedToFloat(vMid_fx - vMin_fx);
236 eBot.dy = FixedToFloat(vMid_fy - vMin_fy);
237
238 /* compute area, oneOverArea and perform backface culling */
239 {
240 const GLfloat area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;
241
242 /* Do backface culling */
243 if (area * bf < 0.0)
244 return;
245
246 if (IS_INF_OR_NAN(area) || area == 0.0F)
247 return;
248
249 oneOverArea = 1.0F / area;
250 }
251
252 #ifndef DO_OCCLUSION_TEST
253 ctx->OcclusionResult = GL_TRUE;
254 #endif
255 span.facing = ctx->_Facing; /* for 2-sided stencil test */
256
257 /* Edge setup. For a triangle strip these could be reused... */
258 {
259 eMaj.fsy = FixedCeil(vMin_fy);
260 eMaj.lines = FixedToInt(FixedCeil(vMax_fy - eMaj.fsy));
261 if (eMaj.lines > 0) {
262 GLfloat dxdy = eMaj.dx / eMaj.dy;
263 eMaj.fdxdy = SignedFloatToFixed(dxdy);
264 eMaj.adjy = (GLfloat) (eMaj.fsy - vMin_fy); /* SCALED! */
265 eMaj.fx0 = vMin_fx;
266 eMaj.fsx = eMaj.fx0 + (GLfixed) (eMaj.adjy * dxdy);
267 }
268 else {
269 return; /*CULLED*/
270 }
271
272 eTop.fsy = FixedCeil(vMid_fy);
273 eTop.lines = FixedToInt(FixedCeil(vMax_fy - eTop.fsy));
274 if (eTop.lines > 0) {
275 GLfloat dxdy = eTop.dx / eTop.dy;
276 eTop.fdxdy = SignedFloatToFixed(dxdy);
277 eTop.adjy = (GLfloat) (eTop.fsy - vMid_fy); /* SCALED! */
278 eTop.fx0 = vMid_fx;
279 eTop.fsx = eTop.fx0 + (GLfixed) (eTop.adjy * dxdy);
280 }
281
282 eBot.fsy = FixedCeil(vMin_fy);
283 eBot.lines = FixedToInt(FixedCeil(vMid_fy - eBot.fsy));
284 if (eBot.lines > 0) {
285 GLfloat dxdy = eBot.dx / eBot.dy;
286 eBot.fdxdy = SignedFloatToFixed(dxdy);
287 eBot.adjy = (GLfloat) (eBot.fsy - vMin_fy); /* SCALED! */
288 eBot.fx0 = vMin_fx;
289 eBot.fsx = eBot.fx0 + (GLfixed) (eBot.adjy * dxdy);
290 }
291 }
292
293 /*
294 * Conceptually, we view a triangle as two subtriangles
295 * separated by a perfectly horizontal line. The edge that is
296 * intersected by this line is one with maximal absolute dy; we
297 * call it a ``major'' edge. The other two edges are the
298 * ``top'' edge (for the upper subtriangle) and the ``bottom''
299 * edge (for the lower subtriangle). If either of these two
300 * edges is horizontal or very close to horizontal, the
301 * corresponding subtriangle might cover zero sample points;
302 * we take care to handle such cases, for performance as well
303 * as correctness.
304 *
305 * By stepping rasterization parameters along the major edge,
306 * we can avoid recomputing them at the discontinuity where
307 * the top and bottom edges meet. However, this forces us to
308 * be able to scan both left-to-right and right-to-left.
309 * Also, we must determine whether the major edge is at the
310 * left or right side of the triangle. We do this by
311 * computing the magnitude of the cross-product of the major
312 * and top edges. Since this magnitude depends on the sine of
313 * the angle between the two edges, its sign tells us whether
314 * we turn to the left or to the right when travelling along
315 * the major edge to the top edge, and from this we infer
316 * whether the major edge is on the left or the right.
317 *
318 * Serendipitously, this cross-product magnitude is also a
319 * value we need to compute the iteration parameter
320 * derivatives for the triangle, and it can be used to perform
321 * backface culling because its sign tells us whether the
322 * triangle is clockwise or counterclockwise. In this code we
323 * refer to it as ``area'' because it's also proportional to
324 * the pixel area of the triangle.
325 */
326
327 {
328 GLint scan_from_left_to_right; /* true if scanning left-to-right */
329 #ifdef INTERP_INDEX
330 GLfloat didx, didy;
331 #endif
332
333 /*
334 * Execute user-supplied setup code
335 */
336 #ifdef SETUP_CODE
337 SETUP_CODE
338 #endif
339
340 scan_from_left_to_right = (oneOverArea < 0.0F);
341
342
343 /* compute d?/dx and d?/dy derivatives */
344 #ifdef INTERP_Z
345 span.interpMask |= SPAN_Z;
346 {
347 GLfloat eMaj_dz = vMax->win[2] - vMin->win[2];
348 GLfloat eBot_dz = vMid->win[2] - vMin->win[2];
349 span.dzdx = oneOverArea * (eMaj_dz * eBot.dy - eMaj.dy * eBot_dz);
350 if (span.dzdx > maxDepth || span.dzdx < -maxDepth) {
351 /* probably a sliver triangle */
352 span.dzdx = 0.0;
353 span.dzdy = 0.0;
354 }
355 else {
356 span.dzdy = oneOverArea * (eMaj.dx * eBot_dz - eMaj_dz * eBot.dx);
357 }
358 if (depthBits <= 16)
359 span.zStep = SignedFloatToFixed(span.dzdx);
360 else
361 span.zStep = (GLint) span.dzdx;
362 }
363 #endif
364 #ifdef INTERP_W
365 {
366 const GLfloat eMaj_dw = vMax->win[3] - vMin->win[3];
367 const GLfloat eBot_dw = vMid->win[3] - vMin->win[3];
368 span.dwdx = oneOverArea * (eMaj_dw * eBot.dy - eMaj.dy * eBot_dw);
369 span.dwdy = oneOverArea * (eMaj.dx * eBot_dw - eMaj_dw * eBot.dx);
370 }
371 #endif
372 #ifdef INTERP_FOG
373 span.interpMask |= SPAN_FOG;
374 {
375 const GLfloat eMaj_dfog = vMax->fog - vMin->fog;
376 const GLfloat eBot_dfog = vMid->fog - vMin->fog;
377 span.dfogdx = oneOverArea * (eMaj_dfog * eBot.dy - eMaj.dy * eBot_dfog);
378 span.dfogdy = oneOverArea * (eMaj.dx * eBot_dfog - eMaj_dfog * eBot.dx);
379 span.fogStep = span.dfogdx;
380 }
381 #endif
382 #ifdef INTERP_RGB
383 span.interpMask |= SPAN_RGBA;
384 if (ctx->Light.ShadeModel == GL_SMOOTH) {
385 GLfloat eMaj_dr = (GLfloat) ((ColorTemp) vMax->color[RCOMP] - vMin->color[RCOMP]);
386 GLfloat eBot_dr = (GLfloat) ((ColorTemp) vMid->color[RCOMP] - vMin->color[RCOMP]);
387 GLfloat eMaj_dg = (GLfloat) ((ColorTemp) vMax->color[GCOMP] - vMin->color[GCOMP]);
388 GLfloat eBot_dg = (GLfloat) ((ColorTemp) vMid->color[GCOMP] - vMin->color[GCOMP]);
389 GLfloat eMaj_db = (GLfloat) ((ColorTemp) vMax->color[BCOMP] - vMin->color[BCOMP]);
390 GLfloat eBot_db = (GLfloat) ((ColorTemp) vMid->color[BCOMP] - vMin->color[BCOMP]);
391 # ifdef INTERP_ALPHA
392 GLfloat eMaj_da = (GLfloat) ((ColorTemp) vMax->color[ACOMP] - vMin->color[ACOMP]);
393 GLfloat eBot_da = (GLfloat) ((ColorTemp) vMid->color[ACOMP] - vMin->color[ACOMP]);
394 # endif
395 span.drdx = oneOverArea * (eMaj_dr * eBot.dy - eMaj.dy * eBot_dr);
396 span.drdy = oneOverArea * (eMaj.dx * eBot_dr - eMaj_dr * eBot.dx);
397 span.dgdx = oneOverArea * (eMaj_dg * eBot.dy - eMaj.dy * eBot_dg);
398 span.dgdy = oneOverArea * (eMaj.dx * eBot_dg - eMaj_dg * eBot.dx);
399 span.dbdx = oneOverArea * (eMaj_db * eBot.dy - eMaj.dy * eBot_db);
400 span.dbdy = oneOverArea * (eMaj.dx * eBot_db - eMaj_db * eBot.dx);
401 # if CHAN_TYPE == GL_FLOAT
402 span.redStep = span.drdx;
403 span.greenStep = span.dgdx;
404 span.blueStep = span.dbdx;
405 # else
406 span.redStep = SignedFloatToFixed(span.drdx);
407 span.greenStep = SignedFloatToFixed(span.dgdx);
408 span.blueStep = SignedFloatToFixed(span.dbdx);
409 # endif /* GL_FLOAT */
410 # ifdef INTERP_ALPHA
411 span.dadx = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
412 span.dady = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
413 # if CHAN_TYPE == GL_FLOAT
414 span.alphaStep = span.dadx;
415 # else
416 span.alphaStep = SignedFloatToFixed(span.dadx);
417 # endif /* GL_FLOAT */
418 # endif /* INTERP_ALPHA */
419 }
420 else {
421 ASSERT (ctx->Light.ShadeModel == GL_FLAT);
422 span.interpMask |= SPAN_FLAT;
423 span.drdx = span.drdy = 0.0F;
424 span.dgdx = span.dgdy = 0.0F;
425 span.dbdx = span.dbdy = 0.0F;
426 # if CHAN_TYPE == GL_FLOAT
427 span.redStep = 0.0F;
428 span.greenStep = 0.0F;
429 span.blueStep = 0.0F;
430 # else
431 span.redStep = 0;
432 span.greenStep = 0;
433 span.blueStep = 0;
434 # endif /* GL_FLOAT */
435 # ifdef INTERP_ALPHA
436 span.dadx = span.dady = 0.0F;
437 # if CHAN_TYPE == GL_FLOAT
438 span.alphaStep = 0.0F;
439 # else
440 span.alphaStep = 0;
441 # endif /* GL_FLOAT */
442 # endif
443 }
444 #endif /* INTERP_RGB */
445 #ifdef INTERP_SPEC
446 span.interpMask |= SPAN_SPEC;
447 if (ctx->Light.ShadeModel == GL_SMOOTH) {
448 GLfloat eMaj_dsr = (GLfloat) ((ColorTemp) vMax->specular[RCOMP] - vMin->specular[RCOMP]);
449 GLfloat eBot_dsr = (GLfloat) ((ColorTemp) vMid->specular[RCOMP] - vMin->specular[RCOMP]);
450 GLfloat eMaj_dsg = (GLfloat) ((ColorTemp) vMax->specular[GCOMP] - vMin->specular[GCOMP]);
451 GLfloat eBot_dsg = (GLfloat) ((ColorTemp) vMid->specular[GCOMP] - vMin->specular[GCOMP]);
452 GLfloat eMaj_dsb = (GLfloat) ((ColorTemp) vMax->specular[BCOMP] - vMin->specular[BCOMP]);
453 GLfloat eBot_dsb = (GLfloat) ((ColorTemp) vMid->specular[BCOMP] - vMin->specular[BCOMP]);
454 span.dsrdx = oneOverArea * (eMaj_dsr * eBot.dy - eMaj.dy * eBot_dsr);
455 span.dsrdy = oneOverArea * (eMaj.dx * eBot_dsr - eMaj_dsr * eBot.dx);
456 span.dsgdx = oneOverArea * (eMaj_dsg * eBot.dy - eMaj.dy * eBot_dsg);
457 span.dsgdy = oneOverArea * (eMaj.dx * eBot_dsg - eMaj_dsg * eBot.dx);
458 span.dsbdx = oneOverArea * (eMaj_dsb * eBot.dy - eMaj.dy * eBot_dsb);
459 span.dsbdy = oneOverArea * (eMaj.dx * eBot_dsb - eMaj_dsb * eBot.dx);
460 # if CHAN_TYPE == GL_FLOAT
461 span.specRedStep = span.dsrdx;
462 span.specGreenStep = span.dsgdx;
463 span.specBlueStep = span.dsbdx;
464 # else
465 span.specRedStep = SignedFloatToFixed(span.dsrdx);
466 span.specGreenStep = SignedFloatToFixed(span.dsgdx);
467 span.specBlueStep = SignedFloatToFixed(span.dsbdx);
468 # endif
469 }
470 else {
471 span.dsrdx = span.dsrdy = 0.0F;
472 span.dsgdx = span.dsgdy = 0.0F;
473 span.dsbdx = span.dsbdy = 0.0F;
474 # if CHAN_TYPE == GL_FLOAT
475 span.specRedStep = 0.0F;
476 span.specGreenStep = 0.0F;
477 span.specBlueStep = 0.0F;
478 # else
479 span.specRedStep = 0;
480 span.specGreenStep = 0;
481 span.specBlueStep = 0;
482 # endif
483 }
484 #endif /* INTERP_SPEC */
485 #ifdef INTERP_INDEX
486 span.interpMask |= SPAN_INDEX;
487 if (ctx->Light.ShadeModel == GL_SMOOTH) {
488 GLfloat eMaj_di = (GLfloat) ((GLint) vMax->index - (GLint) vMin->index);
489 GLfloat eBot_di = (GLfloat) ((GLint) vMid->index - (GLint) vMin->index);
490 didx = oneOverArea * (eMaj_di * eBot.dy - eMaj.dy * eBot_di);
491 didy = oneOverArea * (eMaj.dx * eBot_di - eMaj_di * eBot.dx);
492 span.indexStep = SignedFloatToFixed(didx);
493 }
494 else {
495 span.interpMask |= SPAN_FLAT;
496 didx = didy = 0.0F;
497 span.indexStep = 0;
498 }
499 #endif
500 #ifdef INTERP_INT_TEX
501 span.interpMask |= SPAN_INT_TEXTURE;
502 {
503 GLfloat eMaj_ds = (vMax->texcoord[0][0] - vMin->texcoord[0][0]) * S_SCALE;
504 GLfloat eBot_ds = (vMid->texcoord[0][0] - vMin->texcoord[0][0]) * S_SCALE;
505 GLfloat eMaj_dt = (vMax->texcoord[0][1] - vMin->texcoord[0][1]) * T_SCALE;
506 GLfloat eBot_dt = (vMid->texcoord[0][1] - vMin->texcoord[0][1]) * T_SCALE;
507 span.texStepX[0][0] = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
508 span.texStepY[0][0] = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
509 span.texStepX[0][1] = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
510 span.texStepY[0][1] = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
511 span.intTexStep[0] = SignedFloatToFixed(span.texStepX[0][0]);
512 span.intTexStep[1] = SignedFloatToFixed(span.texStepX[0][1]);
513 }
514 #endif
515 #ifdef INTERP_TEX
516 span.interpMask |= SPAN_TEXTURE;
517 {
518 /* win[3] is 1/W */
519 const GLfloat wMax = vMax->win[3], wMin = vMin->win[3], wMid = vMid->win[3];
520 TEX_UNIT_LOOP(
521 GLfloat eMaj_ds = vMax->texcoord[u][0] * wMax - vMin->texcoord[u][0] * wMin;
522 GLfloat eBot_ds = vMid->texcoord[u][0] * wMid - vMin->texcoord[u][0] * wMin;
523 GLfloat eMaj_dt = vMax->texcoord[u][1] * wMax - vMin->texcoord[u][1] * wMin;
524 GLfloat eBot_dt = vMid->texcoord[u][1] * wMid - vMin->texcoord[u][1] * wMin;
525 GLfloat eMaj_du = vMax->texcoord[u][2] * wMax - vMin->texcoord[u][2] * wMin;
526 GLfloat eBot_du = vMid->texcoord[u][2] * wMid - vMin->texcoord[u][2] * wMin;
527 GLfloat eMaj_dv = vMax->texcoord[u][3] * wMax - vMin->texcoord[u][3] * wMin;
528 GLfloat eBot_dv = vMid->texcoord[u][3] * wMid - vMin->texcoord[u][3] * wMin;
529 span.texStepX[u][0] = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
530 span.texStepY[u][0] = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
531 span.texStepX[u][1] = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
532 span.texStepY[u][1] = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
533 span.texStepX[u][2] = oneOverArea * (eMaj_du * eBot.dy - eMaj.dy * eBot_du);
534 span.texStepY[u][2] = oneOverArea * (eMaj.dx * eBot_du - eMaj_du * eBot.dx);
535 span.texStepX[u][3] = oneOverArea * (eMaj_dv * eBot.dy - eMaj.dy * eBot_dv);
536 span.texStepY[u][3] = oneOverArea * (eMaj.dx * eBot_dv - eMaj_dv * eBot.dx);
537 )
538 }
539 #endif
540
541 /*
542 * We always sample at pixel centers. However, we avoid
543 * explicit half-pixel offsets in this code by incorporating
544 * the proper offset in each of x and y during the
545 * transformation to window coordinates.
546 *
547 * We also apply the usual rasterization rules to prevent
548 * cracks and overlaps. A pixel is considered inside a
549 * subtriangle if it meets all of four conditions: it is on or
550 * to the right of the left edge, strictly to the left of the
551 * right edge, on or below the top edge, and strictly above
552 * the bottom edge. (Some edges may be degenerate.)
553 *
554 * The following discussion assumes left-to-right scanning
555 * (that is, the major edge is on the left); the right-to-left
556 * case is a straightforward variation.
557 *
558 * We start by finding the half-integral y coordinate that is
559 * at or below the top of the triangle. This gives us the
560 * first scan line that could possibly contain pixels that are
561 * inside the triangle.
562 *
563 * Next we creep down the major edge until we reach that y,
564 * and compute the corresponding x coordinate on the edge.
565 * Then we find the half-integral x that lies on or just
566 * inside the edge. This is the first pixel that might lie in
567 * the interior of the triangle. (We won't know for sure
568 * until we check the other edges.)
569 *
570 * As we rasterize the triangle, we'll step down the major
571 * edge. For each step in y, we'll move an integer number
572 * of steps in x. There are two possible x step sizes, which
573 * we'll call the ``inner'' step (guaranteed to land on the
574 * edge or inside it) and the ``outer'' step (guaranteed to
575 * land on the edge or outside it). The inner and outer steps
576 * differ by one. During rasterization we maintain an error
577 * term that indicates our distance from the true edge, and
578 * select either the inner step or the outer step, whichever
579 * gets us to the first pixel that falls inside the triangle.
580 *
581 * All parameters (z, red, etc.) as well as the buffer
582 * addresses for color and z have inner and outer step values,
583 * so that we can increment them appropriately. This method
584 * eliminates the need to adjust parameters by creeping a
585 * sub-pixel amount into the triangle at each scanline.
586 */
587
588 {
589 GLint subTriangle;
590 GLfixed fxLeftEdge = 0, fxRightEdge = 0;
591 GLfixed fdxLeftEdge = 0, fdxRightEdge = 0;
592 GLfixed fError = 0, fdError = 0;
593 #ifdef PIXEL_ADDRESS
594 PIXEL_TYPE *pRow = NULL;
595 GLint dPRowOuter = 0, dPRowInner; /* offset in bytes */
596 #endif
597 #ifdef INTERP_Z
598 # ifdef DEPTH_TYPE
599 DEPTH_TYPE *zRow = NULL;
600 GLint dZRowOuter = 0, dZRowInner; /* offset in bytes */
601 # endif
602 GLfixed zLeft = 0, fdzOuter = 0, fdzInner;
603 #endif
604 #ifdef INTERP_W
605 GLfloat wLeft = 0, dwOuter = 0, dwInner;
606 #endif
607 #ifdef INTERP_FOG
608 GLfloat fogLeft = 0, dfogOuter = 0, dfogInner;
609 #endif
610 #ifdef INTERP_RGB
611 ColorTemp rLeft = 0, fdrOuter = 0, fdrInner;
612 ColorTemp gLeft = 0, fdgOuter = 0, fdgInner;
613 ColorTemp bLeft = 0, fdbOuter = 0, fdbInner;
614 #endif
615 #ifdef INTERP_ALPHA
616 ColorTemp aLeft = 0, fdaOuter = 0, fdaInner;
617 #endif
618 #ifdef INTERP_SPEC
619 ColorTemp srLeft=0, dsrOuter=0, dsrInner;
620 ColorTemp sgLeft=0, dsgOuter=0, dsgInner;
621 ColorTemp sbLeft=0, dsbOuter=0, dsbInner;
622 #endif
623 #ifdef INTERP_INDEX
624 GLfixed iLeft=0, diOuter=0, diInner;
625 #endif
626 #ifdef INTERP_INT_TEX
627 GLfixed sLeft=0, dsOuter=0, dsInner;
628 GLfixed tLeft=0, dtOuter=0, dtInner;
629 #endif
630 #ifdef INTERP_TEX
631 GLfloat sLeft[MAX_TEXTURE_COORD_UNITS];
632 GLfloat tLeft[MAX_TEXTURE_COORD_UNITS];
633 GLfloat uLeft[MAX_TEXTURE_COORD_UNITS];
634 GLfloat vLeft[MAX_TEXTURE_COORD_UNITS];
635 GLfloat dsOuter[MAX_TEXTURE_COORD_UNITS], dsInner[MAX_TEXTURE_COORD_UNITS];
636 GLfloat dtOuter[MAX_TEXTURE_COORD_UNITS], dtInner[MAX_TEXTURE_COORD_UNITS];
637 GLfloat duOuter[MAX_TEXTURE_COORD_UNITS], duInner[MAX_TEXTURE_COORD_UNITS];
638 GLfloat dvOuter[MAX_TEXTURE_COORD_UNITS], dvInner[MAX_TEXTURE_COORD_UNITS];
639 #endif
640
641 for (subTriangle=0; subTriangle<=1; subTriangle++) {
642 EdgeT *eLeft, *eRight;
643 int setupLeft, setupRight;
644 int lines;
645
646 if (subTriangle==0) {
647 /* bottom half */
648 if (scan_from_left_to_right) {
649 eLeft = &eMaj;
650 eRight = &eBot;
651 lines = eRight->lines;
652 setupLeft = 1;
653 setupRight = 1;
654 }
655 else {
656 eLeft = &eBot;
657 eRight = &eMaj;
658 lines = eLeft->lines;
659 setupLeft = 1;
660 setupRight = 1;
661 }
662 }
663 else {
664 /* top half */
665 if (scan_from_left_to_right) {
666 eLeft = &eMaj;
667 eRight = &eTop;
668 lines = eRight->lines;
669 setupLeft = 0;
670 setupRight = 1;
671 }
672 else {
673 eLeft = &eTop;
674 eRight = &eMaj;
675 lines = eLeft->lines;
676 setupLeft = 1;
677 setupRight = 0;
678 }
679 if (lines == 0)
680 return;
681 }
682
683 if (setupLeft && eLeft->lines > 0) {
684 const SWvertex *vLower = eLeft->v0;
685 const GLfixed fsx = eLeft->fsx; /* no fractional part */
686 const GLfixed fsy = eLeft->fsy;
687 const GLfixed fx = FixedCeil(fsx); /* no fractional part */
688 const GLfloat adjx = (GLfloat) (fx - eLeft->fx0); /* SCALED! */
689 const GLfloat adjy = eLeft->adjy; /* SCALED! */
690 GLfixed fdxOuter;
691 GLint idxOuter;
692 GLfloat dxOuter;
693
694 fError = fx - fsx - FIXED_ONE;
695 fxLeftEdge = fsx - FIXED_EPSILON;
696 fdxLeftEdge = eLeft->fdxdy;
697 fdxOuter = FixedFloor(fdxLeftEdge - FIXED_EPSILON);
698 fdError = fdxOuter - fdxLeftEdge + FIXED_ONE;
699 idxOuter = FixedToInt(fdxOuter);
700 dxOuter = (GLfloat) idxOuter;
701
702 span.y = FixedToInt(fsy);
703
704 /* silence warnings on some compilers */
705 (void) dxOuter;
706 (void) adjx;
707 (void) adjy;
708 (void) vLower;
709
710 #ifdef PIXEL_ADDRESS
711 {
712 pRow = (PIXEL_TYPE *) PIXEL_ADDRESS(FixedToInt(fxLeftEdge), span.y);
713 dPRowOuter = -((int)BYTES_PER_ROW) + idxOuter * sizeof(PIXEL_TYPE);
714 /* negative because Y=0 at bottom and increases upward */
715 }
716 #endif
717 /*
718 * Now we need the set of parameter (z, color, etc.) values at
719 * the point (fx, fsy). This gives us properly-sampled parameter
720 * values that we can step from pixel to pixel. Furthermore,
721 * although we might have intermediate results that overflow
722 * the normal parameter range when we step temporarily outside
723 * the triangle, we shouldn't overflow or underflow for any
724 * pixel that's actually inside the triangle.
725 */
726
727 #ifdef INTERP_Z
728 {
729 GLfloat z0 = vLower->win[2];
730 if (depthBits <= 16) {
731 /* interpolate fixed-pt values */
732 GLfloat tmp = (z0 * FIXED_SCALE + span.dzdx * adjx + span.dzdy * adjy) + FIXED_HALF;
733 if (tmp < MAX_GLUINT / 2)
734 zLeft = (GLfixed) tmp;
735 else
736 zLeft = MAX_GLUINT / 2;
737 fdzOuter = SignedFloatToFixed(span.dzdy + dxOuter * span.dzdx);
738 }
739 else {
740 /* interpolate depth values exactly */
741 zLeft = (GLint) (z0 + span.dzdx * FixedToFloat(adjx) + span.dzdy * FixedToFloat(adjy));
742 fdzOuter = (GLint) (span.dzdy + dxOuter * span.dzdx);
743 }
744 # ifdef DEPTH_TYPE
745 zRow = (DEPTH_TYPE *)
746 _swrast_zbuffer_address(ctx, FixedToInt(fxLeftEdge), span.y);
747 dZRowOuter = (ctx->DrawBuffer->Width + idxOuter) * sizeof(DEPTH_TYPE);
748 # endif
749 }
750 #endif
751 #ifdef INTERP_W
752 wLeft = vLower->win[3] + (span.dwdx * adjx + span.dwdy * adjy) * (1.0F/FIXED_SCALE);
753 dwOuter = span.dwdy + dxOuter * span.dwdx;
754 #endif
755 #ifdef INTERP_FOG
756 fogLeft = vLower->fog + (span.dfogdx * adjx + span.dfogdy * adjy) * (1.0F/FIXED_SCALE);
757 dfogOuter = span.dfogdy + dxOuter * span.dfogdx;
758 #endif
759 #ifdef INTERP_RGB
760 if (ctx->Light.ShadeModel == GL_SMOOTH) {
761 # if CHAN_TYPE == GL_FLOAT
762 rLeft = vLower->color[RCOMP] + (span.drdx * adjx + span.drdy * adjy) * (1.0F / FIXED_SCALE);
763 gLeft = vLower->color[GCOMP] + (span.dgdx * adjx + span.dgdy * adjy) * (1.0F / FIXED_SCALE);
764 bLeft = vLower->color[BCOMP] + (span.dbdx * adjx + span.dbdy * adjy) * (1.0F / FIXED_SCALE);
765 fdrOuter = span.drdy + dxOuter * span.drdx;
766 fdgOuter = span.dgdy + dxOuter * span.dgdx;
767 fdbOuter = span.dbdy + dxOuter * span.dbdx;
768 # else
769 rLeft = (GLint)(ChanToFixed(vLower->color[RCOMP]) + span.drdx * adjx + span.drdy * adjy) + FIXED_HALF;
770 gLeft = (GLint)(ChanToFixed(vLower->color[GCOMP]) + span.dgdx * adjx + span.dgdy * adjy) + FIXED_HALF;
771 bLeft = (GLint)(ChanToFixed(vLower->color[BCOMP]) + span.dbdx * adjx + span.dbdy * adjy) + FIXED_HALF;
772 fdrOuter = SignedFloatToFixed(span.drdy + dxOuter * span.drdx);
773 fdgOuter = SignedFloatToFixed(span.dgdy + dxOuter * span.dgdx);
774 fdbOuter = SignedFloatToFixed(span.dbdy + dxOuter * span.dbdx);
775 # endif
776 # ifdef INTERP_ALPHA
777 # if CHAN_TYPE == GL_FLOAT
778 aLeft = vLower->color[ACOMP] + (span.dadx * adjx + span.dady * adjy) * (1.0F / FIXED_SCALE);
779 fdaOuter = span.dady + dxOuter * span.dadx;
780 # else
781 aLeft = (GLint)(ChanToFixed(vLower->color[ACOMP]) + span.dadx * adjx + span.dady * adjy) + FIXED_HALF;
782 fdaOuter = SignedFloatToFixed(span.dady + dxOuter * span.dadx);
783 # endif
784 # endif
785 }
786 else {
787 ASSERT (ctx->Light.ShadeModel == GL_FLAT);
788 # if CHAN_TYPE == GL_FLOAT
789 rLeft = v2->color[RCOMP];
790 gLeft = v2->color[GCOMP];
791 bLeft = v2->color[BCOMP];
792 fdrOuter = fdgOuter = fdbOuter = 0.0F;
793 # else
794 rLeft = ChanToFixed(v2->color[RCOMP]);
795 gLeft = ChanToFixed(v2->color[GCOMP]);
796 bLeft = ChanToFixed(v2->color[BCOMP]);
797 fdrOuter = fdgOuter = fdbOuter = 0;
798 # endif
799 # ifdef INTERP_ALPHA
800 # if CHAN_TYPE == GL_FLOAT
801 aLeft = v2->color[ACOMP];
802 fdaOuter = 0.0F;
803 # else
804 aLeft = ChanToFixed(v2->color[ACOMP]);
805 fdaOuter = 0;
806 # endif
807 # endif
808 }
809 #endif
810
811 #ifdef INTERP_SPEC
812 if (ctx->Light.ShadeModel == GL_SMOOTH) {
813 # if CHAN_TYPE == GL_FLOAT
814 srLeft = vLower->specular[RCOMP] + (span.dsrdx * adjx + span.dsrdy * adjy) * (1.0F / FIXED_SCALE);
815 sgLeft = vLower->specular[GCOMP] + (span.dsgdx * adjx + span.dsgdy * adjy) * (1.0F / FIXED_SCALE);
816 sbLeft = vLower->specular[BCOMP] + (span.dsbdx * adjx + span.dsbdy * adjy) * (1.0F / FIXED_SCALE);
817 dsrOuter = span.dsrdy + dxOuter * span.dsrdx;
818 dsgOuter = span.dsgdy + dxOuter * span.dsgdx;
819 dsbOuter = span.dsbdy + dxOuter * span.dsbdx;
820 # else
821 srLeft = (GLfixed) (ChanToFixed(vLower->specular[RCOMP]) + span.dsrdx * adjx + span.dsrdy * adjy) + FIXED_HALF;
822 sgLeft = (GLfixed) (ChanToFixed(vLower->specular[GCOMP]) + span.dsgdx * adjx + span.dsgdy * adjy) + FIXED_HALF;
823 sbLeft = (GLfixed) (ChanToFixed(vLower->specular[BCOMP]) + span.dsbdx * adjx + span.dsbdy * adjy) + FIXED_HALF;
824 dsrOuter = SignedFloatToFixed(span.dsrdy + dxOuter * span.dsrdx);
825 dsgOuter = SignedFloatToFixed(span.dsgdy + dxOuter * span.dsgdx);
826 dsbOuter = SignedFloatToFixed(span.dsbdy + dxOuter * span.dsbdx);
827 # endif
828 }
829 else {
830 #if CHAN_TYPE == GL_FLOAT
831 srLeft = v2->specular[RCOMP];
832 sgLeft = v2->specular[GCOMP];
833 sbLeft = v2->specular[BCOMP];
834 dsrOuter = dsgOuter = dsbOuter = 0.0F;
835 # else
836 srLeft = ChanToFixed(v2->specular[RCOMP]);
837 sgLeft = ChanToFixed(v2->specular[GCOMP]);
838 sbLeft = ChanToFixed(v2->specular[BCOMP]);
839 dsrOuter = dsgOuter = dsbOuter = 0;
840 # endif
841 }
842 #endif
843
844 #ifdef INTERP_INDEX
845 if (ctx->Light.ShadeModel == GL_SMOOTH) {
846 iLeft = (GLfixed)(vLower->index * FIXED_SCALE
847 + didx * adjx + didy * adjy) + FIXED_HALF;
848 diOuter = SignedFloatToFixed(didy + dxOuter * didx);
849 }
850 else {
851 iLeft = (GLfixed) (v2->index * FIXED_SCALE);
852 diOuter = 0;
853 }
854 #endif
855 #ifdef INTERP_INT_TEX
856 {
857 GLfloat s0, t0;
858 s0 = vLower->texcoord[0][0] * S_SCALE;
859 sLeft = (GLfixed)(s0 * FIXED_SCALE + span.texStepX[0][0] * adjx
860 + span.texStepY[0][0] * adjy) + FIXED_HALF;
861 dsOuter = SignedFloatToFixed(span.texStepY[0][0] + dxOuter * span.texStepX[0][0]);
862
863 t0 = vLower->texcoord[0][1] * T_SCALE;
864 tLeft = (GLfixed)(t0 * FIXED_SCALE + span.texStepX[0][1] * adjx
865 + span.texStepY[0][1] * adjy) + FIXED_HALF;
866 dtOuter = SignedFloatToFixed(span.texStepY[0][1] + dxOuter * span.texStepX[0][1]);
867 }
868 #endif
869 #ifdef INTERP_TEX
870 TEX_UNIT_LOOP(
871 const GLfloat invW = vLower->win[3];
872 const GLfloat s0 = vLower->texcoord[u][0] * invW;
873 const GLfloat t0 = vLower->texcoord[u][1] * invW;
874 const GLfloat u0 = vLower->texcoord[u][2] * invW;
875 const GLfloat v0 = vLower->texcoord[u][3] * invW;
876 sLeft[u] = s0 + (span.texStepX[u][0] * adjx + span.texStepY[u][0] * adjy) * (1.0F/FIXED_SCALE);
877 tLeft[u] = t0 + (span.texStepX[u][1] * adjx + span.texStepY[u][1] * adjy) * (1.0F/FIXED_SCALE);
878 uLeft[u] = u0 + (span.texStepX[u][2] * adjx + span.texStepY[u][2] * adjy) * (1.0F/FIXED_SCALE);
879 vLeft[u] = v0 + (span.texStepX[u][3] * adjx + span.texStepY[u][3] * adjy) * (1.0F/FIXED_SCALE);
880 dsOuter[u] = span.texStepY[u][0] + dxOuter * span.texStepX[u][0];
881 dtOuter[u] = span.texStepY[u][1] + dxOuter * span.texStepX[u][1];
882 duOuter[u] = span.texStepY[u][2] + dxOuter * span.texStepX[u][2];
883 dvOuter[u] = span.texStepY[u][3] + dxOuter * span.texStepX[u][3];
884 )
885 #endif
886 } /*if setupLeft*/
887
888
889 if (setupRight && eRight->lines>0) {
890 fxRightEdge = eRight->fsx - FIXED_EPSILON;
891 fdxRightEdge = eRight->fdxdy;
892 }
893
894 if (lines==0) {
895 continue;
896 }
897
898
899 /* Rasterize setup */
900 #ifdef PIXEL_ADDRESS
901 dPRowInner = dPRowOuter + sizeof(PIXEL_TYPE);
902 #endif
903 #ifdef INTERP_Z
904 # ifdef DEPTH_TYPE
905 dZRowInner = dZRowOuter + sizeof(DEPTH_TYPE);
906 # endif
907 fdzInner = fdzOuter + span.zStep;
908 #endif
909 #ifdef INTERP_W
910 dwInner = dwOuter + span.dwdx;
911 #endif
912 #ifdef INTERP_FOG
913 dfogInner = dfogOuter + span.dfogdx;
914 #endif
915 #ifdef INTERP_RGB
916 fdrInner = fdrOuter + span.redStep;
917 fdgInner = fdgOuter + span.greenStep;
918 fdbInner = fdbOuter + span.blueStep;
919 #endif
920 #ifdef INTERP_ALPHA
921 fdaInner = fdaOuter + span.alphaStep;
922 #endif
923 #ifdef INTERP_SPEC
924 dsrInner = dsrOuter + span.specRedStep;
925 dsgInner = dsgOuter + span.specGreenStep;
926 dsbInner = dsbOuter + span.specBlueStep;
927 #endif
928 #ifdef INTERP_INDEX
929 diInner = diOuter + span.indexStep;
930 #endif
931 #ifdef INTERP_INT_TEX
932 dsInner = dsOuter + span.intTexStep[0];
933 dtInner = dtOuter + span.intTexStep[1];
934 #endif
935 #ifdef INTERP_TEX
936 TEX_UNIT_LOOP(
937 dsInner[u] = dsOuter[u] + span.texStepX[u][0];
938 dtInner[u] = dtOuter[u] + span.texStepX[u][1];
939 duInner[u] = duOuter[u] + span.texStepX[u][2];
940 dvInner[u] = dvOuter[u] + span.texStepX[u][3];
941 )
942 #endif
943
944 while (lines > 0) {
945 /* initialize the span interpolants to the leftmost value */
946 /* ff = fixed-pt fragment */
947 const GLint right = FixedToInt(fxRightEdge);
948
949 span.x = FixedToInt(fxLeftEdge);
950
951 if (right <= span.x)
952 span.end = 0;
953 else
954 span.end = right - span.x;
955
956 #ifdef INTERP_Z
957 span.z = zLeft;
958 #endif
959 #ifdef INTERP_W
960 span.w = wLeft;
961 #endif
962 #ifdef INTERP_FOG
963 span.fog = fogLeft;
964 #endif
965 #ifdef INTERP_RGB
966 span.red = rLeft;
967 span.green = gLeft;
968 span.blue = bLeft;
969 #endif
970 #ifdef INTERP_ALPHA
971 span.alpha = aLeft;
972 #endif
973 #ifdef INTERP_SPEC
974 span.specRed = srLeft;
975 span.specGreen = sgLeft;
976 span.specBlue = sbLeft;
977 #endif
978 #ifdef INTERP_INDEX
979 span.index = iLeft;
980 #endif
981 #ifdef INTERP_INT_TEX
982 span.intTex[0] = sLeft;
983 span.intTex[1] = tLeft;
984 #endif
985
986 #ifdef INTERP_TEX
987 TEX_UNIT_LOOP(
988 span.tex[u][0] = sLeft[u];
989 span.tex[u][1] = tLeft[u];
990 span.tex[u][2] = uLeft[u];
991 span.tex[u][3] = vLeft[u];
992 )
993 #endif
994
995 if (span.end > 1) {
996 /* Under rare circumstances, we might have to fudge the
997 * colors. XXX does this really happen anymore???
998 */
999 const GLint len = span.end - 1;
1000 (void) len;
1001 #ifdef INTERP_RGB
1002 {
1003 GLfixed ffrend = span.red + len * span.redStep;
1004 GLfixed ffgend = span.green + len * span.greenStep;
1005 GLfixed ffbend = span.blue + len * span.blueStep;
1006 if (ffrend < 0) {
1007 span.red -= ffrend;
1008 if (span.red < 0)
1009 span.red = 0;
1010 }
1011 if (ffgend < 0) {
1012 span.green -= ffgend;
1013 if (span.green < 0)
1014 span.green = 0;
1015 }
1016 if (ffbend < 0) {
1017 span.blue -= ffbend;
1018 if (span.blue < 0)
1019 span.blue = 0;
1020 }
1021 }
1022 #endif
1023 #ifdef INTERP_ALPHA
1024 {
1025 GLfixed ffaend = span.alpha + len * span.alphaStep;
1026 if (ffaend < 0) {
1027 span.alpha -= ffaend;
1028 if (span.alpha < 0)
1029 span.alpha = 0;
1030 }
1031 }
1032 #endif
1033 #ifdef INTERP_SPEC
1034 {
1035 GLfixed ffsrend = span.specRed + len * span.specRedStep;
1036 GLfixed ffsgend = span.specGreen + len * span.specGreenStep;
1037 GLfixed ffsbend = span.specBlue + len * span.specBlueStep;
1038 if (ffsrend < 0) {
1039 span.specRed -= ffsrend;
1040 if (span.specRed < 0)
1041 span.specRed = 0;
1042 }
1043 if (ffsgend < 0) {
1044 span.specGreen -= ffsgend;
1045 if (span.specGreen < 0)
1046 span.specGreen = 0;
1047 }
1048 if (ffsbend < 0) {
1049 span.specBlue -= ffsbend;
1050 if (span.specBlue < 0)
1051 span.specBlue = 0;
1052 }
1053 }
1054 #endif
1055 #ifdef INTERP_INDEX
1056 if (span.index < 0)
1057 span.index = 0;
1058 #endif
1059 } /* span.end > 1 */
1060
1061 /* This is where we actually generate fragments */
1062 if (span.end > 0) {
1063 RENDER_SPAN( span );
1064 }
1065
1066 /*
1067 * Advance to the next scan line. Compute the
1068 * new edge coordinates, and adjust the
1069 * pixel-center x coordinate so that it stays
1070 * on or inside the major edge.
1071 */
1072 span.y++;
1073 lines--;
1074
1075 fxLeftEdge += fdxLeftEdge;
1076 fxRightEdge += fdxRightEdge;
1077
1078
1079 fError += fdError;
1080 if (fError >= 0) {
1081 fError -= FIXED_ONE;
1082 #ifdef PIXEL_ADDRESS
1083 pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowOuter);
1084 #endif
1085 #ifdef INTERP_Z
1086 # ifdef DEPTH_TYPE
1087 zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowOuter);
1088 # endif
1089 zLeft += fdzOuter;
1090 #endif
1091 #ifdef INTERP_W
1092 wLeft += dwOuter;
1093 #endif
1094 #ifdef INTERP_FOG
1095 fogLeft += dfogOuter;
1096 #endif
1097 #ifdef INTERP_RGB
1098 rLeft += fdrOuter;
1099 gLeft += fdgOuter;
1100 bLeft += fdbOuter;
1101 #endif
1102 #ifdef INTERP_ALPHA
1103 aLeft += fdaOuter;
1104 #endif
1105 #ifdef INTERP_SPEC
1106 srLeft += dsrOuter;
1107 sgLeft += dsgOuter;
1108 sbLeft += dsbOuter;
1109 #endif
1110 #ifdef INTERP_INDEX
1111 iLeft += diOuter;
1112 #endif
1113 #ifdef INTERP_INT_TEX
1114 sLeft += dsOuter;
1115 tLeft += dtOuter;
1116 #endif
1117 #ifdef INTERP_TEX
1118 TEX_UNIT_LOOP(
1119 sLeft[u] += dsOuter[u];
1120 tLeft[u] += dtOuter[u];
1121 uLeft[u] += duOuter[u];
1122 vLeft[u] += dvOuter[u];
1123 )
1124 #endif
1125 }
1126 else {
1127 #ifdef PIXEL_ADDRESS
1128 pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowInner);
1129 #endif
1130 #ifdef INTERP_Z
1131 # ifdef DEPTH_TYPE
1132 zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowInner);
1133 # endif
1134 zLeft += fdzInner;
1135 #endif
1136 #ifdef INTERP_W
1137 wLeft += dwInner;
1138 #endif
1139 #ifdef INTERP_FOG
1140 fogLeft += dfogInner;
1141 #endif
1142 #ifdef INTERP_RGB
1143 rLeft += fdrInner;
1144 gLeft += fdgInner;
1145 bLeft += fdbInner;
1146 #endif
1147 #ifdef INTERP_ALPHA
1148 aLeft += fdaInner;
1149 #endif
1150 #ifdef INTERP_SPEC
1151 srLeft += dsrInner;
1152 sgLeft += dsgInner;
1153 sbLeft += dsbInner;
1154 #endif
1155 #ifdef INTERP_INDEX
1156 iLeft += diInner;
1157 #endif
1158 #ifdef INTERP_INT_TEX
1159 sLeft += dsInner;
1160 tLeft += dtInner;
1161 #endif
1162 #ifdef INTERP_TEX
1163 TEX_UNIT_LOOP(
1164 sLeft[u] += dsInner[u];
1165 tLeft[u] += dtInner[u];
1166 uLeft[u] += duInner[u];
1167 vLeft[u] += dvInner[u];
1168 )
1169 #endif
1170 }
1171 } /*while lines>0*/
1172
1173 } /* for subTriangle */
1174
1175 }
1176 #ifdef CLEANUP_CODE
1177 CLEANUP_CODE
1178 #endif
1179 }
1180 }
1181
1182 #undef SETUP_CODE
1183 #undef CLEANUP_CODE
1184 #undef RENDER_SPAN
1185
1186 #undef PIXEL_TYPE
1187 #undef BYTES_PER_ROW
1188 #undef PIXEL_ADDRESS
1189
1190 #undef INTERP_Z
1191 #undef INTERP_W
1192 #undef INTERP_FOG
1193 #undef INTERP_RGB
1194 #undef INTERP_ALPHA
1195 #undef INTERP_SPEC
1196 #undef INTERP_INDEX
1197 #undef INTERP_INT_TEX
1198 #undef INTERP_TEX
1199 #undef INTERP_MULTITEX
1200 #undef TEX_UNIT_LOOP
1201
1202 #undef S_SCALE
1203 #undef T_SCALE
1204
1205 #undef FixedToDepth
1206
1207 #undef DO_OCCLUSION_TEST
1208 #undef NAME