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