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