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