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