2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Triangle Rasterizer Template
28 * This file is #include'd to generate custom triangle rasterizers.
30 * The following macros may be defined to indicate what auxillary information
31 * must be interpolated across the triangle:
32 * INTERP_Z - if defined, interpolate vertex Z values
33 * INTERP_W - if defined, interpolate vertex W values
34 * INTERP_FOG - if defined, interpolate fog values
35 * INTERP_RGB - if defined, interpolate RGB values
36 * INTERP_ALPHA - if defined, interpolate Alpha values (req's INTERP_RGB)
37 * INTERP_SPEC - if defined, interpolate specular RGB values
38 * INTERP_INDEX - if defined, interpolate color index values
39 * INTERP_INT_TEX - if defined, interpolate integer ST texcoords
40 * (fast, simple 2-D texture mapping)
41 * INTERP_TEX - if defined, interpolate texcoords and varying vars
42 * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red)
44 * When one can directly address pixels in the color buffer the following
45 * macros can be defined and used to compute pixel addresses during
46 * rasterization (see pRow):
47 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
48 * BYTES_PER_ROW - number of bytes per row in the color buffer
49 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
50 * Y==0 at bottom of screen and increases upward.
52 * Similarly, for direct depth buffer access, this type is used for depth
54 * DEPTH_TYPE - either GLushort or GLuint
56 * Optionally, one may provide one-time setup code per triangle:
57 * SETUP_CODE - code which is to be executed once per triangle
58 * CLEANUP_CODE - code to execute at end of triangle
60 * The following macro MUST be defined:
61 * RENDER_SPAN(span) - code to write a span of pixels.
63 * This code was designed for the origin to be in the lower-left corner.
65 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
68 * Some notes on rasterization accuracy:
70 * This code uses fixed point arithmetic (the GLfixed type) to iterate
71 * over the triangle edges and interpolate ancillary data (such as Z,
72 * color, secondary color, etc). The number of fractional bits in
73 * GLfixed and the value of SUB_PIXEL_BITS has a direct bearing on the
74 * accuracy of rasterization.
76 * If SUB_PIXEL_BITS=4 then we'll snap the vertices to the nearest
77 * 1/16 of a pixel. If we're walking up a long, nearly vertical edge
78 * (dx=1/16, dy=1024) we'll need 4 + 10 = 14 fractional bits in
79 * GLfixed to walk the edge without error. If the maximum viewport
80 * height is 4K pixels, then we'll need 4 + 12 = 16 fractional bits.
82 * Historically, Mesa has used 11 fractional bits in GLfixed, snaps
83 * vertices to 1/16 pixel and allowed a maximum viewport height of 2K
84 * pixels. 11 fractional bits is actually insufficient for accurately
85 * rasterizing some triangles. More recently, the maximum viewport
86 * height was increased to 4K pixels. Thus, Mesa should be using 16
87 * fractional bits in GLfixed. Unfortunately, there may be some issues
88 * with setting FIXED_FRAC_BITS=16, such as multiplication overflow.
89 * This will have to be examined in some detail...
91 * For now, if you find rasterization errors, particularly with tall,
92 * sliver triangles, try increasing FIXED_FRAC_BITS and/or decreasing
97 * ColorTemp is used for intermediate color values.
99 #if CHAN_TYPE == GL_FLOAT
100 #define ColorTemp GLfloat
102 #define ColorTemp GLint /* same as GLfixed */
107 * Walk triangle edges with GLfixed or GLdouble
109 #if TRIANGLE_WALK_DOUBLE
110 #define GLinterp GLdouble
111 #define InterpToInt(X) ((GLint) (X))
112 #define INTERP_ONE 1.0
114 #define GLinterp GLfixed
115 #define InterpToInt(X) FixedToInt(X)
116 #define INTERP_ONE FIXED_ONE
120 #define TEXVAR_LOOP(CODE) \
123 for (attr = swrast->_MinFragmentAttrib; \
124 attr < swrast->_MaxFragmentAttrib; attr++) { \
125 if (swrast->_FragmentAttribs & (1 << attr)) { \
135 * Some code we unfortunately need to prevent negative interpolated colors.
137 #ifndef CLAMP_INTERPOLANT
138 #define CLAMP_INTERPOLANT(CHANNEL, CHANNELSTEP, LEN) \
140 GLfixed endVal = span.CHANNEL + (LEN) * span.CHANNELSTEP; \
142 span.CHANNEL -= endVal; \
144 if (span.CHANNEL < 0) { \
151 static void NAME(GLcontext
*ctx
, const SWvertex
*v0
,
156 const SWvertex
*v0
, *v1
; /* Y(v0) < Y(v1) */
157 #if TRIANGLE_WALK_DOUBLE
158 GLdouble dx
; /* X(v1) - X(v0) */
159 GLdouble dy
; /* Y(v1) - Y(v0) */
160 GLdouble dxdy
; /* dx/dy */
161 GLdouble adjy
; /* adjust from v[0]->fy to fsy, scaled */
162 GLdouble fsx
; /* first sample point x coord */
164 GLdouble fx0
; /*X of lower endpoint */
166 GLfloat dx
; /* X(v1) - X(v0) */
167 GLfloat dy
; /* Y(v1) - Y(v0) */
168 GLfloat dxdy
; /* dx/dy */
169 GLfixed fdxdy
; /* dx/dy in fixed-point */
170 GLfloat adjy
; /* adjust from v[0]->fy to fsy, scaled */
171 GLfixed fsx
; /* first sample point x coord */
173 GLfixed fx0
; /* fixed pt X of lower endpoint */
175 GLint lines
; /* number of lines to be sampled on this edge */
178 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
180 const GLint depthBits
= ctx
->DrawBuffer
->Visual
.depthBits
;
181 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
182 const GLfloat maxDepth
= ctx
->DrawBuffer
->_DepthMaxF
;
183 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
185 EdgeT eMaj
, eTop
, eBot
;
187 const SWvertex
*vMin
, *vMid
, *vMax
; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
188 GLfloat bf
= SWRAST_CONTEXT(ctx
)->_BackfaceSign
;
189 #if !TRIANGLE_WALK_DOUBLE
190 const GLint snapMask
= ~((FIXED_ONE
/ (1 << SUB_PIXEL_BITS
)) - 1); /* for x/y coord snapping */
192 GLinterp vMin_fx
, vMin_fy
, vMid_fx
, vMid_fy
, vMax_fx
, vMax_fy
;
198 INIT_SPAN(span
, GL_POLYGON
, 0, 0, 0);
199 span
.y
= 0; /* silence warnings */
202 (void) fixedToDepthShift
;
206 printf("%s()\n", __FUNCTION__);
207 printf(" %g, %g, %g\n", v0->win[0], v0->win[1], v0->win[2]);
208 printf(" %g, %g, %g\n", v1->win[0], v1->win[1], v1->win[2]);
209 printf(" %g, %g, %g\n", v2->win[0], v2->win[1], v2->win[2]);
212 ASSERT(v0->win[2] >= 0.0);
213 ASSERT(v1->win[2] >= 0.0);
214 ASSERT(v2->win[2] >= 0.0);
216 /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
217 * And find the order of the 3 vertices along the Y axis.
220 #if TRIANGLE_WALK_DOUBLE
221 const GLdouble fy0
= v0
->win
[1] - 0.5;
222 const GLdouble fy1
= v1
->win
[1] - 0.5;
223 const GLdouble fy2
= v2
->win
[1] - 0.5;
225 const GLfixed fy0
= FloatToFixed(v0
->win
[1] - 0.5F
) & snapMask
;
226 const GLfixed fy1
= FloatToFixed(v1
->win
[1] - 0.5F
) & snapMask
;
227 const GLfixed fy2
= FloatToFixed(v2
->win
[1] - 0.5F
) & snapMask
;
232 vMin
= v0
; vMid
= v1
; vMax
= v2
;
233 vMin_fy
= fy0
; vMid_fy
= fy1
; vMax_fy
= fy2
;
235 else if (fy2
<= fy0
) {
237 vMin
= v2
; vMid
= v0
; vMax
= v1
;
238 vMin_fy
= fy2
; vMid_fy
= fy0
; vMax_fy
= fy1
;
242 vMin
= v0
; vMid
= v2
; vMax
= v1
;
243 vMin_fy
= fy0
; vMid_fy
= fy2
; vMax_fy
= fy1
;
250 vMin
= v1
; vMid
= v0
; vMax
= v2
;
251 vMin_fy
= fy1
; vMid_fy
= fy0
; vMax_fy
= fy2
;
254 else if (fy2
<= fy1
) {
256 vMin
= v2
; vMid
= v1
; vMax
= v0
;
257 vMin_fy
= fy2
; vMid_fy
= fy1
; vMax_fy
= fy0
;
262 vMin
= v1
; vMid
= v2
; vMax
= v0
;
263 vMin_fy
= fy1
; vMid_fy
= fy2
; vMax_fy
= fy0
;
267 /* fixed point X coords */
268 #if TRIANGLE_WALK_DOUBLE
269 vMin_fx
= vMin
->win
[0] + 0.5;
270 vMid_fx
= vMid
->win
[0] + 0.5;
271 vMax_fx
= vMax
->win
[0] + 0.5;
273 vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
) & snapMask
;
274 vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
) & snapMask
;
275 vMax_fx
= FloatToFixed(vMax
->win
[0] + 0.5F
) & snapMask
;
279 /* vertex/edge relationship */
280 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
281 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
282 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
284 /* compute deltas for each edge: vertex[upper] - vertex[lower] */
285 #if TRIANGLE_WALK_DOUBLE
286 eMaj
.dx
= vMax_fx
- vMin_fx
;
287 eMaj
.dy
= vMax_fy
- vMin_fy
;
288 eTop
.dx
= vMax_fx
- vMid_fx
;
289 eTop
.dy
= vMax_fy
- vMid_fy
;
290 eBot
.dx
= vMid_fx
- vMin_fx
;
291 eBot
.dy
= vMid_fy
- vMin_fy
;
293 eMaj
.dx
= FixedToFloat(vMax_fx
- vMin_fx
);
294 eMaj
.dy
= FixedToFloat(vMax_fy
- vMin_fy
);
295 eTop
.dx
= FixedToFloat(vMax_fx
- vMid_fx
);
296 eTop
.dy
= FixedToFloat(vMax_fy
- vMid_fy
);
297 eBot
.dx
= FixedToFloat(vMid_fx
- vMin_fx
);
298 eBot
.dy
= FixedToFloat(vMid_fy
- vMin_fy
);
301 /* compute area, oneOverArea and perform backface culling */
303 #if TRIANGLE_WALK_DOUBLE
304 const GLdouble area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
306 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
308 /* Do backface culling */
312 if (IS_INF_OR_NAN(area
) || area
== 0.0F
)
315 oneOverArea
= 1.0F
/ area
;
319 span
.facing
= ctx
->_Facing
; /* for 2-sided stencil test */
321 /* Edge setup. For a triangle strip these could be reused... */
323 #if TRIANGLE_WALK_DOUBLE
324 eMaj
.fsy
= CEILF(vMin_fy
);
325 eMaj
.lines
= (GLint
) CEILF(vMax_fy
- eMaj
.fsy
);
327 eMaj
.fsy
= FixedCeil(vMin_fy
);
328 eMaj
.lines
= FixedToInt(FixedCeil(vMax_fy
- eMaj
.fsy
));
330 if (eMaj
.lines
> 0) {
331 eMaj
.dxdy
= eMaj
.dx
/ eMaj
.dy
;
332 #if TRIANGLE_WALK_DOUBLE
333 eMaj
.adjy
= (eMaj
.fsy
- vMin_fy
) * FIXED_SCALE
; /* SCALED! */
335 eMaj
.fsx
= eMaj
.fx0
+ (eMaj
.adjy
* eMaj
.dxdy
) / (GLdouble
) FIXED_SCALE
;
337 eMaj
.fdxdy
= SignedFloatToFixed(eMaj
.dxdy
);
338 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
340 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* eMaj
.dxdy
);
347 #if TRIANGLE_WALK_DOUBLE
348 eTop
.fsy
= CEILF(vMid_fy
);
349 eTop
.lines
= (GLint
) CEILF(vMax_fy
- eTop
.fsy
);
351 eTop
.fsy
= FixedCeil(vMid_fy
);
352 eTop
.lines
= FixedToInt(FixedCeil(vMax_fy
- eTop
.fsy
));
354 if (eTop
.lines
> 0) {
355 eTop
.dxdy
= eTop
.dx
/ eTop
.dy
;
356 #if TRIANGLE_WALK_DOUBLE
357 eTop
.adjy
= (eTop
.fsy
- vMid_fy
) * FIXED_SCALE
; /* SCALED! */
359 eTop
.fsx
= eTop
.fx0
+ (eTop
.adjy
* eTop
.dxdy
) / (GLdouble
) FIXED_SCALE
;
361 eTop
.fdxdy
= SignedFloatToFixed(eTop
.dxdy
);
362 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
364 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* eTop
.dxdy
);
368 #if TRIANGLE_WALK_DOUBLE
369 eBot
.fsy
= CEILF(vMin_fy
);
370 eBot
.lines
= (GLint
) CEILF(vMid_fy
- eBot
.fsy
);
372 eBot
.fsy
= FixedCeil(vMin_fy
);
373 eBot
.lines
= FixedToInt(FixedCeil(vMid_fy
- eBot
.fsy
));
375 if (eBot
.lines
> 0) {
376 eBot
.dxdy
= eBot
.dx
/ eBot
.dy
;
377 #if TRIANGLE_WALK_DOUBLE
378 eBot
.adjy
= (eBot
.fsy
- vMin_fy
) * FIXED_SCALE
; /* SCALED! */
380 eBot
.fsx
= eBot
.fx0
+ (eBot
.adjy
* eBot
.dxdy
) / (GLdouble
) FIXED_SCALE
;
382 eBot
.fdxdy
= SignedFloatToFixed(eBot
.dxdy
);
383 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
385 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* eBot
.dxdy
);
391 * Conceptually, we view a triangle as two subtriangles
392 * separated by a perfectly horizontal line. The edge that is
393 * intersected by this line is one with maximal absolute dy; we
394 * call it a ``major'' edge. The other two edges are the
395 * ``top'' edge (for the upper subtriangle) and the ``bottom''
396 * edge (for the lower subtriangle). If either of these two
397 * edges is horizontal or very close to horizontal, the
398 * corresponding subtriangle might cover zero sample points;
399 * we take care to handle such cases, for performance as well
402 * By stepping rasterization parameters along the major edge,
403 * we can avoid recomputing them at the discontinuity where
404 * the top and bottom edges meet. However, this forces us to
405 * be able to scan both left-to-right and right-to-left.
406 * Also, we must determine whether the major edge is at the
407 * left or right side of the triangle. We do this by
408 * computing the magnitude of the cross-product of the major
409 * and top edges. Since this magnitude depends on the sine of
410 * the angle between the two edges, its sign tells us whether
411 * we turn to the left or to the right when travelling along
412 * the major edge to the top edge, and from this we infer
413 * whether the major edge is on the left or the right.
415 * Serendipitously, this cross-product magnitude is also a
416 * value we need to compute the iteration parameter
417 * derivatives for the triangle, and it can be used to perform
418 * backface culling because its sign tells us whether the
419 * triangle is clockwise or counterclockwise. In this code we
420 * refer to it as ``area'' because it's also proportional to
421 * the pixel area of the triangle.
425 GLint scan_from_left_to_right
; /* true if scanning left-to-right */
431 * Execute user-supplied setup code
437 scan_from_left_to_right
= (oneOverArea
< 0.0F
);
440 /* compute d?/dx and d?/dy derivatives */
442 span
.interpMask
|= SPAN_Z
;
444 GLfloat eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
445 GLfloat eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
446 span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] = oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
447 if (span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] > maxDepth
|| span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] < -maxDepth
) {
448 /* probably a sliver triangle */
449 span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] = 0.0;
450 span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] = 0.0;
453 span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] = oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
456 span
.zStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
458 span
.zStep
= (GLint
) span
.attrStepX
[FRAG_ATTRIB_WPOS
][2];
462 span
.interpMask
|= SPAN_W
;
464 const GLfloat eMaj_dw
= vMax
->win
[3] - vMin
->win
[3];
465 const GLfloat eBot_dw
= vMid
->win
[3] - vMin
->win
[3];
466 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = oneOverArea
* (eMaj_dw
* eBot
.dy
- eMaj
.dy
* eBot_dw
);
467 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = oneOverArea
* (eMaj
.dx
* eBot_dw
- eMaj_dw
* eBot
.dx
);
471 span
.interpMask
|= SPAN_FOG
;
474 const GLfloat wMax
= vMax
->win
[3], wMin
= vMin
->win
[3], wMid
= vMid
->win
[3];
475 const GLfloat eMaj_dfog
= vMax
->fog
* wMax
- vMin
->fog
* wMin
;
476 const GLfloat eBot_dfog
= vMid
->fog
* wMid
- vMin
->fog
* wMin
;
478 const GLfloat eMaj_dfog
= vMax
->fog
- vMin
->fog
;
479 const GLfloat eBot_dfog
= vMid
->fog
- vMin
->fog
;
481 span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] = oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
482 span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] = oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
486 span
.interpMask
|= SPAN_RGBA
;
487 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
488 GLfloat eMaj_dr
= (GLfloat
) ((ColorTemp
) vMax
->color
[RCOMP
] - (ColorTemp
) vMin
->color
[RCOMP
]);
489 GLfloat eBot_dr
= (GLfloat
) ((ColorTemp
) vMid
->color
[RCOMP
] - (ColorTemp
) vMin
->color
[RCOMP
]);
490 GLfloat eMaj_dg
= (GLfloat
) ((ColorTemp
) vMax
->color
[GCOMP
] - (ColorTemp
) vMin
->color
[GCOMP
]);
491 GLfloat eBot_dg
= (GLfloat
) ((ColorTemp
) vMid
->color
[GCOMP
] - (ColorTemp
) vMin
->color
[GCOMP
]);
492 GLfloat eMaj_db
= (GLfloat
) ((ColorTemp
) vMax
->color
[BCOMP
] - (ColorTemp
) vMin
->color
[BCOMP
]);
493 GLfloat eBot_db
= (GLfloat
) ((ColorTemp
) vMid
->color
[BCOMP
] - (ColorTemp
) vMin
->color
[BCOMP
]);
495 GLfloat eMaj_da
= (GLfloat
) ((ColorTemp
) vMax
->color
[ACOMP
] - (ColorTemp
) vMin
->color
[ACOMP
]);
496 GLfloat eBot_da
= (GLfloat
) ((ColorTemp
) vMid
->color
[ACOMP
] - (ColorTemp
) vMin
->color
[ACOMP
]);
498 span
.attrStepX
[FRAG_ATTRIB_COL0
][0] = oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
499 span
.attrStepY
[FRAG_ATTRIB_COL0
][0] = oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
500 span
.attrStepX
[FRAG_ATTRIB_COL0
][1] = oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
501 span
.attrStepY
[FRAG_ATTRIB_COL0
][1] = oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
502 span
.attrStepX
[FRAG_ATTRIB_COL0
][2] = oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
503 span
.attrStepY
[FRAG_ATTRIB_COL0
][2] = oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
504 # if CHAN_TYPE == GL_FLOAT
505 span
.redStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][0];
506 span
.greenStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][1];
507 span
.blueStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][2];
509 span
.redStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][0]);
510 span
.greenStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][1]);
511 span
.blueStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][2]);
512 # endif /* GL_FLOAT */
514 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
515 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
516 # if CHAN_TYPE == GL_FLOAT
517 span
.alphaStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][3];
519 span
.alphaStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][3]);
520 # endif /* GL_FLOAT */
521 # endif /* INTERP_ALPHA */
524 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
525 span
.interpMask
|= SPAN_FLAT
;
526 span
.attrStepX
[FRAG_ATTRIB_COL0
][0] = span
.attrStepY
[FRAG_ATTRIB_COL0
][0] = 0.0F
;
527 span
.attrStepX
[FRAG_ATTRIB_COL0
][1] = span
.attrStepY
[FRAG_ATTRIB_COL0
][1] = 0.0F
;
528 span
.attrStepX
[FRAG_ATTRIB_COL0
][2] = span
.attrStepY
[FRAG_ATTRIB_COL0
][2] = 0.0F
;
529 # if CHAN_TYPE == GL_FLOAT
531 span
.greenStep
= 0.0F
;
532 span
.blueStep
= 0.0F
;
537 # endif /* GL_FLOAT */
539 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = 0.0F
;
540 # if CHAN_TYPE == GL_FLOAT
541 span
.alphaStep
= 0.0F
;
544 # endif /* GL_FLOAT */
547 #endif /* INTERP_RGB */
549 span
.interpMask
|= SPAN_SPEC
;
550 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
551 GLfloat eMaj_dsr
= (GLfloat
) ((ColorTemp
) vMax
->specular
[RCOMP
] - (ColorTemp
) vMin
->specular
[RCOMP
]);
552 GLfloat eBot_dsr
= (GLfloat
) ((ColorTemp
) vMid
->specular
[RCOMP
] - (ColorTemp
) vMin
->specular
[RCOMP
]);
553 GLfloat eMaj_dsg
= (GLfloat
) ((ColorTemp
) vMax
->specular
[GCOMP
] - (ColorTemp
) vMin
->specular
[GCOMP
]);
554 GLfloat eBot_dsg
= (GLfloat
) ((ColorTemp
) vMid
->specular
[GCOMP
] - (ColorTemp
) vMin
->specular
[GCOMP
]);
555 GLfloat eMaj_dsb
= (GLfloat
) ((ColorTemp
) vMax
->specular
[BCOMP
] - (ColorTemp
) vMin
->specular
[BCOMP
]);
556 GLfloat eBot_dsb
= (GLfloat
) ((ColorTemp
) vMid
->specular
[BCOMP
] - (ColorTemp
) vMin
->specular
[BCOMP
]);
557 span
.attrStepX
[FRAG_ATTRIB_COL1
][0] = oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
558 span
.attrStepY
[FRAG_ATTRIB_COL1
][0] = oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
559 span
.attrStepX
[FRAG_ATTRIB_COL1
][1] = oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
560 span
.attrStepY
[FRAG_ATTRIB_COL1
][1] = oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
561 span
.attrStepX
[FRAG_ATTRIB_COL1
][2] = oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
562 span
.attrStepY
[FRAG_ATTRIB_COL1
][2] = oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
563 # if CHAN_TYPE == GL_FLOAT
564 span
.specRedStep
= span
.attrStepX
[FRAG_ATTRIB_COL1
][0];
565 span
.specGreenStep
= span
.attrStepX
[FRAG_ATTRIB_COL1
][1];
566 span
.specBlueStep
= span
.attrStepX
[FRAG_ATTRIB_COL1
][2];
568 span
.specRedStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][0]);
569 span
.specGreenStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][1]);
570 span
.specBlueStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][2]);
574 span
.attrStepX
[FRAG_ATTRIB_COL1
][0] = span
.attrStepY
[FRAG_ATTRIB_COL1
][0] = 0.0F
;
575 span
.attrStepX
[FRAG_ATTRIB_COL1
][1] = span
.attrStepY
[FRAG_ATTRIB_COL1
][1] = 0.0F
;
576 span
.attrStepX
[FRAG_ATTRIB_COL1
][2] = span
.attrStepY
[FRAG_ATTRIB_COL1
][2] = 0.0F
;
577 # if CHAN_TYPE == GL_FLOAT
578 span
.specRedStep
= 0.0F
;
579 span
.specGreenStep
= 0.0F
;
580 span
.specBlueStep
= 0.0F
;
582 span
.specRedStep
= 0;
583 span
.specGreenStep
= 0;
584 span
.specBlueStep
= 0;
587 #endif /* INTERP_SPEC */
589 span
.interpMask
|= SPAN_INDEX
;
590 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
591 GLfloat eMaj_di
= vMax
->index
- vMin
->index
;
592 GLfloat eBot_di
= vMid
->index
- vMin
->index
;
593 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
594 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
595 span
.indexStep
= SignedFloatToFixed(didx
);
598 span
.interpMask
|= SPAN_FLAT
;
603 #ifdef INTERP_INT_TEX
604 span
.interpMask
|= SPAN_INT_TEXTURE
;
606 GLfloat eMaj_ds
= (vMax
->attrib
[FRAG_ATTRIB_TEX0
][0] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][0]) * S_SCALE
;
607 GLfloat eBot_ds
= (vMid
->attrib
[FRAG_ATTRIB_TEX0
][0] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][0]) * S_SCALE
;
608 GLfloat eMaj_dt
= (vMax
->attrib
[FRAG_ATTRIB_TEX0
][1] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][1]) * T_SCALE
;
609 GLfloat eBot_dt
= (vMid
->attrib
[FRAG_ATTRIB_TEX0
][1] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][1]) * T_SCALE
;
610 span
.attrStepX
[FRAG_ATTRIB_TEX0
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
611 span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
612 span
.attrStepX
[FRAG_ATTRIB_TEX0
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
613 span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
614 span
.intTexStep
[0] = SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_TEX0
][0]);
615 span
.intTexStep
[1] = SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_TEX0
][1]);
619 span
.interpMask
|= (SPAN_TEXTURE
| SPAN_VARYING
);
622 const GLfloat wMax
= vMax
->win
[3], wMin
= vMin
->win
[3], wMid
= vMid
->win
[3];
624 GLfloat eMaj_ds
= vMax
->attrib
[attr
][0] * wMax
- vMin
->attrib
[attr
][0] * wMin
;
625 GLfloat eBot_ds
= vMid
->attrib
[attr
][0] * wMid
- vMin
->attrib
[attr
][0] * wMin
;
626 GLfloat eMaj_dt
= vMax
->attrib
[attr
][1] * wMax
- vMin
->attrib
[attr
][1] * wMin
;
627 GLfloat eBot_dt
= vMid
->attrib
[attr
][1] * wMid
- vMin
->attrib
[attr
][1] * wMin
;
628 GLfloat eMaj_du
= vMax
->attrib
[attr
][2] * wMax
- vMin
->attrib
[attr
][2] * wMin
;
629 GLfloat eBot_du
= vMid
->attrib
[attr
][2] * wMid
- vMin
->attrib
[attr
][2] * wMin
;
630 GLfloat eMaj_dv
= vMax
->attrib
[attr
][3] * wMax
- vMin
->attrib
[attr
][3] * wMin
;
631 GLfloat eBot_dv
= vMid
->attrib
[attr
][3] * wMid
- vMin
->attrib
[attr
][3] * wMin
;
632 span
.attrStepX
[attr
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
633 span
.attrStepY
[attr
][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
634 span
.attrStepX
[attr
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
635 span
.attrStepY
[attr
][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
636 span
.attrStepX
[attr
][2] = oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
637 span
.attrStepY
[attr
][2] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
638 span
.attrStepX
[attr
][3] = oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
639 span
.attrStepY
[attr
][3] = oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
645 * We always sample at pixel centers. However, we avoid
646 * explicit half-pixel offsets in this code by incorporating
647 * the proper offset in each of x and y during the
648 * transformation to window coordinates.
650 * We also apply the usual rasterization rules to prevent
651 * cracks and overlaps. A pixel is considered inside a
652 * subtriangle if it meets all of four conditions: it is on or
653 * to the right of the left edge, strictly to the left of the
654 * right edge, on or below the top edge, and strictly above
655 * the bottom edge. (Some edges may be degenerate.)
657 * The following discussion assumes left-to-right scanning
658 * (that is, the major edge is on the left); the right-to-left
659 * case is a straightforward variation.
661 * We start by finding the half-integral y coordinate that is
662 * at or below the top of the triangle. This gives us the
663 * first scan line that could possibly contain pixels that are
664 * inside the triangle.
666 * Next we creep down the major edge until we reach that y,
667 * and compute the corresponding x coordinate on the edge.
668 * Then we find the half-integral x that lies on or just
669 * inside the edge. This is the first pixel that might lie in
670 * the interior of the triangle. (We won't know for sure
671 * until we check the other edges.)
673 * As we rasterize the triangle, we'll step down the major
674 * edge. For each step in y, we'll move an integer number
675 * of steps in x. There are two possible x step sizes, which
676 * we'll call the ``inner'' step (guaranteed to land on the
677 * edge or inside it) and the ``outer'' step (guaranteed to
678 * land on the edge or outside it). The inner and outer steps
679 * differ by one. During rasterization we maintain an error
680 * term that indicates our distance from the true edge, and
681 * select either the inner step or the outer step, whichever
682 * gets us to the first pixel that falls inside the triangle.
684 * All parameters (z, red, etc.) as well as the buffer
685 * addresses for color and z have inner and outer step values,
686 * so that we can increment them appropriately. This method
687 * eliminates the need to adjust parameters by creeping a
688 * sub-pixel amount into the triangle at each scanline.
693 GLinterp fxLeftEdge
= 0, fxRightEdge
= 0;
694 GLinterp fdxLeftEdge
= 0, fdxRightEdge
= 0;
695 GLinterp fError
= 0, fdError
= 0;
697 PIXEL_TYPE
*pRow
= NULL
;
698 GLint dPRowOuter
= 0, dPRowInner
; /* offset in bytes */
702 struct gl_renderbuffer
*zrb
703 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
704 DEPTH_TYPE
*zRow
= NULL
;
705 GLint dZRowOuter
= 0, dZRowInner
; /* offset in bytes */
708 GLfixed fdzOuter
= 0, fdzInner
;
711 GLfloat wLeft
= 0, dwOuter
= 0, dwInner
;
714 GLfloat fogLeft
= 0, dfogOuter
= 0, dfogInner
;
717 ColorTemp rLeft
= 0, fdrOuter
= 0, fdrInner
;
718 ColorTemp gLeft
= 0, fdgOuter
= 0, fdgInner
;
719 ColorTemp bLeft
= 0, fdbOuter
= 0, fdbInner
;
722 ColorTemp aLeft
= 0, fdaOuter
= 0, fdaInner
;
725 ColorTemp srLeft
=0, dsrOuter
=0, dsrInner
;
726 ColorTemp sgLeft
=0, dsgOuter
=0, dsgInner
;
727 ColorTemp sbLeft
=0, dsbOuter
=0, dsbInner
;
730 GLfixed iLeft
=0, diOuter
=0, diInner
;
732 #ifdef INTERP_INT_TEX
733 GLfixed sLeft
=0, dsOuter
=0, dsInner
;
734 GLfixed tLeft
=0, dtOuter
=0, dtInner
;
737 GLfloat sLeft
[FRAG_ATTRIB_MAX
];
738 GLfloat tLeft
[FRAG_ATTRIB_MAX
];
739 GLfloat uLeft
[FRAG_ATTRIB_MAX
];
740 GLfloat vLeft
[FRAG_ATTRIB_MAX
];
741 GLfloat dsOuter
[FRAG_ATTRIB_MAX
], dsInner
[FRAG_ATTRIB_MAX
];
742 GLfloat dtOuter
[FRAG_ATTRIB_MAX
], dtInner
[FRAG_ATTRIB_MAX
];
743 GLfloat duOuter
[FRAG_ATTRIB_MAX
], duInner
[FRAG_ATTRIB_MAX
];
744 GLfloat dvOuter
[FRAG_ATTRIB_MAX
], dvInner
[FRAG_ATTRIB_MAX
];
747 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
748 EdgeT
*eLeft
, *eRight
;
749 int setupLeft
, setupRight
;
752 if (subTriangle
==0) {
754 if (scan_from_left_to_right
) {
757 lines
= eRight
->lines
;
764 lines
= eLeft
->lines
;
771 if (scan_from_left_to_right
) {
774 lines
= eRight
->lines
;
781 lines
= eLeft
->lines
;
789 if (setupLeft
&& eLeft
->lines
> 0) {
790 const SWvertex
*vLower
= eLeft
->v0
;
791 #if TRIANGLE_WALK_DOUBLE
792 const GLdouble fsy
= eLeft
->fsy
;
793 const GLdouble fsx
= eLeft
->fsx
;
794 const GLdouble fx
= CEILF(fsx
);
795 const GLdouble adjx
= (fx
- eLeft
->fx0
) * FIXED_SCALE
; /* SCALED! */
797 const GLfixed fsy
= eLeft
->fsy
;
798 const GLfixed fsx
= eLeft
->fsx
; /* no fractional part */
799 const GLfixed fx
= FixedCeil(fsx
); /* no fractional part */
800 const GLfixed adjx
= (GLinterp
) (fx
- eLeft
->fx0
); /* SCALED! */
802 const GLinterp adjy
= (GLinterp
) eLeft
->adjy
; /* SCALED! */
804 #if TRIANGLE_WALK_DOUBLE
807 fError
= fx
- fsx
- 1.0;
809 fdxLeftEdge
= eLeft
->dxdy
;
810 dxOuter
= FLOORF(fdxLeftEdge
);
811 fdError
= dxOuter
- fdxLeftEdge
+ 1.0;
812 idxOuter
= (GLint
) dxOuter
;
813 span
.y
= (GLint
) fsy
;
818 fError
= fx
- fsx
- FIXED_ONE
;
819 fxLeftEdge
= fsx
- FIXED_EPSILON
;
820 fdxLeftEdge
= eLeft
->fdxdy
;
821 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
822 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
823 idxOuter
= FixedToInt(fdxOuter
);
824 dxOuter
= (GLfloat
) idxOuter
;
825 span
.y
= FixedToInt(fsy
);
828 /* silence warnings on some compilers */
836 pRow
= (PIXEL_TYPE
*) PIXEL_ADDRESS(InterpToInt(fxLeftEdge
), span
.y
);
837 dPRowOuter
= -((int)BYTES_PER_ROW
) + idxOuter
* sizeof(PIXEL_TYPE
);
838 /* negative because Y=0 at bottom and increases upward */
842 * Now we need the set of parameter (z, color, etc.) values at
843 * the point (fx, fsy). This gives us properly-sampled parameter
844 * values that we can step from pixel to pixel. Furthermore,
845 * although we might have intermediate results that overflow
846 * the normal parameter range when we step temporarily outside
847 * the triangle, we shouldn't overflow or underflow for any
848 * pixel that's actually inside the triangle.
853 GLfloat z0
= vLower
->win
[2];
854 if (depthBits
<= 16) {
855 /* interpolate fixed-pt values */
856 GLfloat tmp
= (z0
* FIXED_SCALE
857 + span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] * adjx
858 + span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] * adjy
) + FIXED_HALF
;
859 if (tmp
< MAX_GLUINT
/ 2)
860 zLeft
= (GLfixed
) tmp
;
862 zLeft
= MAX_GLUINT
/ 2;
863 fdzOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
866 /* interpolate depth values w/out scaling */
867 zLeft
= (GLuint
) (z0
+ span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] * FixedToFloat(adjx
)
868 + span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] * FixedToFloat(adjy
));
869 fdzOuter
= (GLint
) (span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
872 zRow
= (DEPTH_TYPE
*)
873 zrb
->GetPointer(ctx
, zrb
, InterpToInt(fxLeftEdge
), span
.y
);
874 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
879 wLeft
= vLower
->win
[3] + (span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] * adjy
) * (1.0F
/FIXED_SCALE
);
880 dwOuter
= span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][3];
884 fogLeft
= vLower
->fog
* vLower
->win
[3] + (span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
886 fogLeft
= vLower
->fog
+ (span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
888 dfogOuter
= span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_FOGC
][0];
891 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
892 # if CHAN_TYPE == GL_FLOAT
893 rLeft
= vLower
->color
[RCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][0] * adjy
) * (1.0F
/ FIXED_SCALE
);
894 gLeft
= vLower
->color
[GCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][1] * adjy
) * (1.0F
/ FIXED_SCALE
);
895 bLeft
= vLower
->color
[BCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][2] * adjy
) * (1.0F
/ FIXED_SCALE
);
896 fdrOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][0];
897 fdgOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][1];
898 fdbOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][2];
900 rLeft
= (GLint
)(ChanToFixed(vLower
->color
[RCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][0] * adjy
) + FIXED_HALF
;
901 gLeft
= (GLint
)(ChanToFixed(vLower
->color
[GCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][1] * adjy
) + FIXED_HALF
;
902 bLeft
= (GLint
)(ChanToFixed(vLower
->color
[BCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][2] * adjy
) + FIXED_HALF
;
903 fdrOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][0]);
904 fdgOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][1]);
905 fdbOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][2]);
908 # if CHAN_TYPE == GL_FLOAT
909 aLeft
= vLower
->color
[ACOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjx
+ span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjy
) * (1.0F
/ FIXED_SCALE
);
910 fdaOuter
= span
.attrStepX
[FRAG_ATTRIB_COL0
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][3];
912 aLeft
= (GLint
)(ChanToFixed(vLower
->color
[ACOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjx
+ span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjy
) + FIXED_HALF
;
913 fdaOuter
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][3]);
918 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
919 # if CHAN_TYPE == GL_FLOAT
920 rLeft
= v2
->color
[RCOMP
];
921 gLeft
= v2
->color
[GCOMP
];
922 bLeft
= v2
->color
[BCOMP
];
923 fdrOuter
= fdgOuter
= fdbOuter
= 0.0F
;
925 rLeft
= ChanToFixed(v2
->color
[RCOMP
]);
926 gLeft
= ChanToFixed(v2
->color
[GCOMP
]);
927 bLeft
= ChanToFixed(v2
->color
[BCOMP
]);
928 fdrOuter
= fdgOuter
= fdbOuter
= 0;
931 # if CHAN_TYPE == GL_FLOAT
932 aLeft
= v2
->color
[ACOMP
];
935 aLeft
= ChanToFixed(v2
->color
[ACOMP
]);
940 #endif /* INTERP_RGB */
944 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
945 # if CHAN_TYPE == GL_FLOAT
946 srLeft
= vLower
->specular
[RCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][0] * adjy
) * (1.0F
/ FIXED_SCALE
);
947 sgLeft
= vLower
->specular
[GCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][1] * adjy
) * (1.0F
/ FIXED_SCALE
);
948 sbLeft
= vLower
->specular
[BCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][2] * adjy
) * (1.0F
/ FIXED_SCALE
);
949 dsrOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][0];
950 dsgOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][1];
951 dsbOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][2];
953 srLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[RCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][0] * adjy
) + FIXED_HALF
;
954 sgLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[GCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][1] * adjy
) + FIXED_HALF
;
955 sbLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[BCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][2] * adjy
) + FIXED_HALF
;
956 dsrOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][0]);
957 dsgOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][1]);
958 dsbOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][2]);
962 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
963 #if CHAN_TYPE == GL_FLOAT
964 srLeft
= v2
->specular
[RCOMP
];
965 sgLeft
= v2
->specular
[GCOMP
];
966 sbLeft
= v2
->specular
[BCOMP
];
967 dsrOuter
= dsgOuter
= dsbOuter
= 0.0F
;
969 srLeft
= ChanToFixed(v2
->specular
[RCOMP
]);
970 sgLeft
= ChanToFixed(v2
->specular
[GCOMP
]);
971 sbLeft
= ChanToFixed(v2
->specular
[BCOMP
]);
972 dsrOuter
= dsgOuter
= dsbOuter
= 0;
978 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
979 iLeft
= (GLfixed
)(vLower
->index
* FIXED_SCALE
980 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
981 diOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
984 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
985 iLeft
= FloatToFixed(v2
->index
);
989 #ifdef INTERP_INT_TEX
992 s0
= vLower
->attrib
[FRAG_ATTRIB_TEX0
][0] * S_SCALE
;
993 sLeft
= (GLfixed
)(s0
* FIXED_SCALE
+ span
.attrStepX
[FRAG_ATTRIB_TEX0
][0] * adjx
994 + span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] * adjy
) + FIXED_HALF
;
995 dsOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_TEX0
][0]);
997 t0
= vLower
->attrib
[FRAG_ATTRIB_TEX0
][1] * T_SCALE
;
998 tLeft
= (GLfixed
)(t0
* FIXED_SCALE
+ span
.attrStepX
[FRAG_ATTRIB_TEX0
][1] * adjx
999 + span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] * adjy
) + FIXED_HALF
;
1000 dtOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_TEX0
][1]);
1005 const GLfloat invW
= vLower
->win
[3];
1006 const GLfloat s0
= vLower
->attrib
[attr
][0] * invW
;
1007 const GLfloat t0
= vLower
->attrib
[attr
][1] * invW
;
1008 const GLfloat u0
= vLower
->attrib
[attr
][2] * invW
;
1009 const GLfloat v0
= vLower
->attrib
[attr
][3] * invW
;
1010 sLeft
[attr
] = s0
+ (span
.attrStepX
[attr
][0] * adjx
+ span
.attrStepY
[attr
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
1011 tLeft
[attr
] = t0
+ (span
.attrStepX
[attr
][1] * adjx
+ span
.attrStepY
[attr
][1] * adjy
) * (1.0F
/FIXED_SCALE
);
1012 uLeft
[attr
] = u0
+ (span
.attrStepX
[attr
][2] * adjx
+ span
.attrStepY
[attr
][2] * adjy
) * (1.0F
/FIXED_SCALE
);
1013 vLeft
[attr
] = v0
+ (span
.attrStepX
[attr
][3] * adjx
+ span
.attrStepY
[attr
][3] * adjy
) * (1.0F
/FIXED_SCALE
);
1014 dsOuter
[attr
] = span
.attrStepY
[attr
][0] + dxOuter
* span
.attrStepX
[attr
][0];
1015 dtOuter
[attr
] = span
.attrStepY
[attr
][1] + dxOuter
* span
.attrStepX
[attr
][1];
1016 duOuter
[attr
] = span
.attrStepY
[attr
][2] + dxOuter
* span
.attrStepX
[attr
][2];
1017 dvOuter
[attr
] = span
.attrStepY
[attr
][3] + dxOuter
* span
.attrStepX
[attr
][3];
1023 if (setupRight
&& eRight
->lines
>0) {
1024 #if TRIANGLE_WALK_DOUBLE
1025 fxRightEdge
= eRight
->fsx
;
1026 fdxRightEdge
= eRight
->dxdy
;
1028 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
1029 fdxRightEdge
= eRight
->fdxdy
;
1038 /* Rasterize setup */
1039 #ifdef PIXEL_ADDRESS
1040 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
1044 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
1046 fdzInner
= fdzOuter
+ span
.zStep
;
1049 dwInner
= dwOuter
+ span
.attrStepX
[FRAG_ATTRIB_WPOS
][3];
1052 dfogInner
= dfogOuter
+ span
.attrStepX
[FRAG_ATTRIB_FOGC
][0];
1055 fdrInner
= fdrOuter
+ span
.redStep
;
1056 fdgInner
= fdgOuter
+ span
.greenStep
;
1057 fdbInner
= fdbOuter
+ span
.blueStep
;
1060 fdaInner
= fdaOuter
+ span
.alphaStep
;
1063 dsrInner
= dsrOuter
+ span
.specRedStep
;
1064 dsgInner
= dsgOuter
+ span
.specGreenStep
;
1065 dsbInner
= dsbOuter
+ span
.specBlueStep
;
1068 diInner
= diOuter
+ span
.indexStep
;
1070 #ifdef INTERP_INT_TEX
1071 dsInner
= dsOuter
+ span
.intTexStep
[0];
1072 dtInner
= dtOuter
+ span
.intTexStep
[1];
1076 dsInner
[attr
] = dsOuter
[attr
] + span
.attrStepX
[attr
][0];
1077 dtInner
[attr
] = dtOuter
[attr
] + span
.attrStepX
[attr
][1];
1078 duInner
[attr
] = duOuter
[attr
] + span
.attrStepX
[attr
][2];
1079 dvInner
[attr
] = dvOuter
[attr
] + span
.attrStepX
[attr
][3];
1084 /* initialize the span interpolants to the leftmost value */
1085 /* ff = fixed-pt fragment */
1086 const GLint right
= InterpToInt(fxRightEdge
);
1087 span
.x
= InterpToInt(fxLeftEdge
);
1088 if (right
<= span
.x
)
1091 span
.end
= right
- span
.x
;
1097 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = wLeft
;
1100 span
.attrStart
[FRAG_ATTRIB_FOGC
][0] = fogLeft
;
1111 span
.specRed
= srLeft
;
1112 span
.specGreen
= sgLeft
;
1113 span
.specBlue
= sbLeft
;
1118 #ifdef INTERP_INT_TEX
1119 span
.intTex
[0] = sLeft
;
1120 span
.intTex
[1] = tLeft
;
1125 span
.attrStart
[attr
][0] = sLeft
[attr
];
1126 span
.attrStart
[attr
][1] = tLeft
[attr
];
1127 span
.attrStart
[attr
][2] = uLeft
[attr
];
1128 span
.attrStart
[attr
][3] = vLeft
[attr
];
1132 /* This is where we actually generate fragments */
1133 /* XXX the test for span.y > 0 _shouldn't_ be needed but
1134 * it fixes a problem on 64-bit Opterons (bug 4842).
1136 if (span
.end
> 0 && span
.y
>= 0) {
1137 const GLint len
= span
.end
- 1;
1140 CLAMP_INTERPOLANT(red
, redStep
, len
);
1141 CLAMP_INTERPOLANT(green
, greenStep
, len
);
1142 CLAMP_INTERPOLANT(blue
, blueStep
, len
);
1145 CLAMP_INTERPOLANT(alpha
, alphaStep
, len
);
1148 CLAMP_INTERPOLANT(specRed
, specRedStep
, len
);
1149 CLAMP_INTERPOLANT(specGreen
, specGreenStep
, len
);
1150 CLAMP_INTERPOLANT(specBlue
, specBlueStep
, len
);
1153 CLAMP_INTERPOLANT(index
, indexStep
, len
);
1156 RENDER_SPAN( span
);
1161 * Advance to the next scan line. Compute the
1162 * new edge coordinates, and adjust the
1163 * pixel-center x coordinate so that it stays
1164 * on or inside the major edge.
1169 fxLeftEdge
+= fdxLeftEdge
;
1170 fxRightEdge
+= fdxRightEdge
;
1174 fError
-= INTERP_ONE
;
1176 #ifdef PIXEL_ADDRESS
1177 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowOuter
);
1181 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowOuter
);
1189 fogLeft
+= dfogOuter
;
1207 #ifdef INTERP_INT_TEX
1213 sLeft
[attr
] += dsOuter
[attr
];
1214 tLeft
[attr
] += dtOuter
[attr
];
1215 uLeft
[attr
] += duOuter
[attr
];
1216 vLeft
[attr
] += dvOuter
[attr
];
1221 #ifdef PIXEL_ADDRESS
1222 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowInner
);
1226 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowInner
);
1234 fogLeft
+= dfogInner
;
1252 #ifdef INTERP_INT_TEX
1258 sLeft
[attr
] += dsInner
[attr
];
1259 tLeft
[attr
] += dtInner
[attr
];
1260 uLeft
[attr
] += duInner
[attr
];
1261 vLeft
[attr
] += dvInner
[attr
];
1267 } /* for subTriangle */
1281 #undef BYTES_PER_ROW
1282 #undef PIXEL_ADDRESS
1292 #undef INTERP_INT_TEX
1294 #undef TEX_UNIT_LOOP