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