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