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