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