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