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