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);
201 (void) fixedToDepthShift
;
205 printf("%s()\n", __FUNCTION__);
206 printf(" %g, %g, %g\n", v0->win[0], v0->win[1], v0->win[2]);
207 printf(" %g, %g, %g\n", v1->win[0], v1->win[1], v1->win[2]);
208 printf(" %g, %g, %g\n", v2->win[0], v2->win[1], v2->win[2]);
211 ASSERT(v0->win[2] >= 0.0);
212 ASSERT(v1->win[2] >= 0.0);
213 ASSERT(v2->win[2] >= 0.0);
215 /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
216 * And find the order of the 3 vertices along the Y axis.
219 #if TRIANGLE_WALK_DOUBLE
220 const GLdouble fy0
= v0
->win
[1] - 0.5;
221 const GLdouble fy1
= v1
->win
[1] - 0.5;
222 const GLdouble fy2
= v2
->win
[1] - 0.5;
224 const GLfixed fy0
= FloatToFixed(v0
->win
[1] - 0.5F
) & snapMask
;
225 const GLfixed fy1
= FloatToFixed(v1
->win
[1] - 0.5F
) & snapMask
;
226 const GLfixed fy2
= FloatToFixed(v2
->win
[1] - 0.5F
) & snapMask
;
231 vMin
= v0
; vMid
= v1
; vMax
= v2
;
232 vMin_fy
= fy0
; vMid_fy
= fy1
; vMax_fy
= fy2
;
234 else if (fy2
<= fy0
) {
236 vMin
= v2
; vMid
= v0
; vMax
= v1
;
237 vMin_fy
= fy2
; vMid_fy
= fy0
; vMax_fy
= fy1
;
241 vMin
= v0
; vMid
= v2
; vMax
= v1
;
242 vMin_fy
= fy0
; vMid_fy
= fy2
; vMax_fy
= fy1
;
249 vMin
= v1
; vMid
= v0
; vMax
= v2
;
250 vMin_fy
= fy1
; vMid_fy
= fy0
; vMax_fy
= fy2
;
253 else if (fy2
<= fy1
) {
255 vMin
= v2
; vMid
= v1
; vMax
= v0
;
256 vMin_fy
= fy2
; vMid_fy
= fy1
; vMax_fy
= fy0
;
261 vMin
= v1
; vMid
= v2
; vMax
= v0
;
262 vMin_fy
= fy1
; vMid_fy
= fy2
; vMax_fy
= fy0
;
266 /* fixed point X coords */
267 #if TRIANGLE_WALK_DOUBLE
268 vMin_fx
= vMin
->win
[0] + 0.5;
269 vMid_fx
= vMid
->win
[0] + 0.5;
270 vMax_fx
= vMax
->win
[0] + 0.5;
272 vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
) & snapMask
;
273 vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
) & snapMask
;
274 vMax_fx
= FloatToFixed(vMax
->win
[0] + 0.5F
) & snapMask
;
278 /* vertex/edge relationship */
279 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
280 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
281 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
283 /* compute deltas for each edge: vertex[upper] - vertex[lower] */
284 #if TRIANGLE_WALK_DOUBLE
285 eMaj
.dx
= vMax_fx
- vMin_fx
;
286 eMaj
.dy
= vMax_fy
- vMin_fy
;
287 eTop
.dx
= vMax_fx
- vMid_fx
;
288 eTop
.dy
= vMax_fy
- vMid_fy
;
289 eBot
.dx
= vMid_fx
- vMin_fx
;
290 eBot
.dy
= vMid_fy
- vMin_fy
;
292 eMaj
.dx
= FixedToFloat(vMax_fx
- vMin_fx
);
293 eMaj
.dy
= FixedToFloat(vMax_fy
- vMin_fy
);
294 eTop
.dx
= FixedToFloat(vMax_fx
- vMid_fx
);
295 eTop
.dy
= FixedToFloat(vMax_fy
- vMid_fy
);
296 eBot
.dx
= FixedToFloat(vMid_fx
- vMin_fx
);
297 eBot
.dy
= FixedToFloat(vMid_fy
- vMin_fy
);
300 /* compute area, oneOverArea and perform backface culling */
302 #if TRIANGLE_WALK_DOUBLE
303 const GLdouble area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
305 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
307 /* Do backface culling */
311 if (IS_INF_OR_NAN(area
) || area
== 0.0F
)
314 oneOverArea
= 1.0F
/ area
;
318 span
.facing
= ctx
->_Facing
; /* for 2-sided stencil test */
320 /* Edge setup. For a triangle strip these could be reused... */
322 #if TRIANGLE_WALK_DOUBLE
323 eMaj
.fsy
= CEILF(vMin_fy
);
324 eMaj
.lines
= (GLint
) CEILF(vMax_fy
- eMaj
.fsy
);
326 eMaj
.fsy
= FixedCeil(vMin_fy
);
327 eMaj
.lines
= FixedToInt(FixedCeil(vMax_fy
- eMaj
.fsy
));
329 if (eMaj
.lines
> 0) {
330 eMaj
.dxdy
= eMaj
.dx
/ eMaj
.dy
;
331 #if TRIANGLE_WALK_DOUBLE
332 eMaj
.adjy
= (eMaj
.fsy
- vMin_fy
) * FIXED_SCALE
; /* SCALED! */
334 eMaj
.fsx
= eMaj
.fx0
+ (eMaj
.adjy
* eMaj
.dxdy
) / (GLdouble
) FIXED_SCALE
;
336 eMaj
.fdxdy
= SignedFloatToFixed(eMaj
.dxdy
);
337 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
339 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* eMaj
.dxdy
);
346 #if TRIANGLE_WALK_DOUBLE
347 eTop
.fsy
= CEILF(vMid_fy
);
348 eTop
.lines
= (GLint
) CEILF(vMax_fy
- eTop
.fsy
);
350 eTop
.fsy
= FixedCeil(vMid_fy
);
351 eTop
.lines
= FixedToInt(FixedCeil(vMax_fy
- eTop
.fsy
));
353 if (eTop
.lines
> 0) {
354 eTop
.dxdy
= eTop
.dx
/ eTop
.dy
;
355 #if TRIANGLE_WALK_DOUBLE
356 eTop
.adjy
= (eTop
.fsy
- vMid_fy
) * FIXED_SCALE
; /* SCALED! */
358 eTop
.fsx
= eTop
.fx0
+ (eTop
.adjy
* eTop
.dxdy
) / (GLdouble
) FIXED_SCALE
;
360 eTop
.fdxdy
= SignedFloatToFixed(eTop
.dxdy
);
361 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
363 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* eTop
.dxdy
);
367 #if TRIANGLE_WALK_DOUBLE
368 eBot
.fsy
= CEILF(vMin_fy
);
369 eBot
.lines
= (GLint
) CEILF(vMid_fy
- eBot
.fsy
);
371 eBot
.fsy
= FixedCeil(vMin_fy
);
372 eBot
.lines
= FixedToInt(FixedCeil(vMid_fy
- eBot
.fsy
));
374 if (eBot
.lines
> 0) {
375 eBot
.dxdy
= eBot
.dx
/ eBot
.dy
;
376 #if TRIANGLE_WALK_DOUBLE
377 eBot
.adjy
= (eBot
.fsy
- vMin_fy
) * FIXED_SCALE
; /* SCALED! */
379 eBot
.fsx
= eBot
.fx0
+ (eBot
.adjy
* eBot
.dxdy
) / (GLdouble
) FIXED_SCALE
;
381 eBot
.fdxdy
= SignedFloatToFixed(eBot
.dxdy
);
382 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
384 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* eBot
.dxdy
);
390 * Conceptually, we view a triangle as two subtriangles
391 * separated by a perfectly horizontal line. The edge that is
392 * intersected by this line is one with maximal absolute dy; we
393 * call it a ``major'' edge. The other two edges are the
394 * ``top'' edge (for the upper subtriangle) and the ``bottom''
395 * edge (for the lower subtriangle). If either of these two
396 * edges is horizontal or very close to horizontal, the
397 * corresponding subtriangle might cover zero sample points;
398 * we take care to handle such cases, for performance as well
401 * By stepping rasterization parameters along the major edge,
402 * we can avoid recomputing them at the discontinuity where
403 * the top and bottom edges meet. However, this forces us to
404 * be able to scan both left-to-right and right-to-left.
405 * Also, we must determine whether the major edge is at the
406 * left or right side of the triangle. We do this by
407 * computing the magnitude of the cross-product of the major
408 * and top edges. Since this magnitude depends on the sine of
409 * the angle between the two edges, its sign tells us whether
410 * we turn to the left or to the right when travelling along
411 * the major edge to the top edge, and from this we infer
412 * whether the major edge is on the left or the right.
414 * Serendipitously, this cross-product magnitude is also a
415 * value we need to compute the iteration parameter
416 * derivatives for the triangle, and it can be used to perform
417 * backface culling because its sign tells us whether the
418 * triangle is clockwise or counterclockwise. In this code we
419 * refer to it as ``area'' because it's also proportional to
420 * the pixel area of the triangle.
424 GLint scan_from_left_to_right
; /* true if scanning left-to-right */
430 * Execute user-supplied setup code
436 scan_from_left_to_right
= (oneOverArea
< 0.0F
);
439 /* compute d?/dx and d?/dy derivatives */
441 span
.interpMask
|= SPAN_Z
;
443 GLfloat eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
444 GLfloat eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
445 span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] = oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
446 if (span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] > maxDepth
|| span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] < -maxDepth
) {
447 /* probably a sliver triangle */
448 span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] = 0.0;
449 span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] = 0.0;
452 span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] = oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
455 span
.zStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
457 span
.zStep
= (GLint
) span
.attrStepX
[FRAG_ATTRIB_WPOS
][2];
461 span
.interpMask
|= SPAN_W
;
463 const GLfloat eMaj_dw
= vMax
->win
[3] - vMin
->win
[3];
464 const GLfloat eBot_dw
= vMid
->win
[3] - vMin
->win
[3];
465 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = oneOverArea
* (eMaj_dw
* eBot
.dy
- eMaj
.dy
* eBot_dw
);
466 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = oneOverArea
* (eMaj
.dx
* eBot_dw
- eMaj_dw
* eBot
.dx
);
470 span
.interpMask
|= SPAN_FOG
;
473 const GLfloat wMax
= vMax
->win
[3], wMin
= vMin
->win
[3], wMid
= vMid
->win
[3];
474 const GLfloat eMaj_dfog
= vMax
->fog
* wMax
- vMin
->fog
* wMin
;
475 const GLfloat eBot_dfog
= vMid
->fog
* wMid
- vMin
->fog
* wMin
;
477 const GLfloat eMaj_dfog
= vMax
->fog
- vMin
->fog
;
478 const GLfloat eBot_dfog
= vMid
->fog
- vMin
->fog
;
480 span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] = oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
481 span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] = oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
485 span
.interpMask
|= SPAN_RGBA
;
486 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
487 GLfloat eMaj_dr
= (GLfloat
) ((ColorTemp
) vMax
->color
[RCOMP
] - (ColorTemp
) vMin
->color
[RCOMP
]);
488 GLfloat eBot_dr
= (GLfloat
) ((ColorTemp
) vMid
->color
[RCOMP
] - (ColorTemp
) vMin
->color
[RCOMP
]);
489 GLfloat eMaj_dg
= (GLfloat
) ((ColorTemp
) vMax
->color
[GCOMP
] - (ColorTemp
) vMin
->color
[GCOMP
]);
490 GLfloat eBot_dg
= (GLfloat
) ((ColorTemp
) vMid
->color
[GCOMP
] - (ColorTemp
) vMin
->color
[GCOMP
]);
491 GLfloat eMaj_db
= (GLfloat
) ((ColorTemp
) vMax
->color
[BCOMP
] - (ColorTemp
) vMin
->color
[BCOMP
]);
492 GLfloat eBot_db
= (GLfloat
) ((ColorTemp
) vMid
->color
[BCOMP
] - (ColorTemp
) vMin
->color
[BCOMP
]);
494 GLfloat eMaj_da
= (GLfloat
) ((ColorTemp
) vMax
->color
[ACOMP
] - (ColorTemp
) vMin
->color
[ACOMP
]);
495 GLfloat eBot_da
= (GLfloat
) ((ColorTemp
) vMid
->color
[ACOMP
] - (ColorTemp
) vMin
->color
[ACOMP
]);
497 span
.attrStepX
[FRAG_ATTRIB_COL0
][0] = oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
498 span
.attrStepY
[FRAG_ATTRIB_COL0
][0] = oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
499 span
.attrStepX
[FRAG_ATTRIB_COL0
][1] = oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
500 span
.attrStepY
[FRAG_ATTRIB_COL0
][1] = oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
501 span
.attrStepX
[FRAG_ATTRIB_COL0
][2] = oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
502 span
.attrStepY
[FRAG_ATTRIB_COL0
][2] = oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
503 # if CHAN_TYPE == GL_FLOAT
504 span
.redStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][0];
505 span
.greenStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][1];
506 span
.blueStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][2];
508 span
.redStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][0]);
509 span
.greenStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][1]);
510 span
.blueStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][2]);
511 # endif /* GL_FLOAT */
513 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
514 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
515 # if CHAN_TYPE == GL_FLOAT
516 span
.alphaStep
= span
.attrStepX
[FRAG_ATTRIB_COL0
][3];
518 span
.alphaStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][3]);
519 # endif /* GL_FLOAT */
520 # endif /* INTERP_ALPHA */
523 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
524 span
.interpMask
|= SPAN_FLAT
;
525 span
.attrStepX
[FRAG_ATTRIB_COL0
][0] = span
.attrStepY
[FRAG_ATTRIB_COL0
][0] = 0.0F
;
526 span
.attrStepX
[FRAG_ATTRIB_COL0
][1] = span
.attrStepY
[FRAG_ATTRIB_COL0
][1] = 0.0F
;
527 span
.attrStepX
[FRAG_ATTRIB_COL0
][2] = span
.attrStepY
[FRAG_ATTRIB_COL0
][2] = 0.0F
;
528 # if CHAN_TYPE == GL_FLOAT
530 span
.greenStep
= 0.0F
;
531 span
.blueStep
= 0.0F
;
536 # endif /* GL_FLOAT */
538 span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = span
.attrStepX
[FRAG_ATTRIB_COL0
][3] = 0.0F
;
539 # if CHAN_TYPE == GL_FLOAT
540 span
.alphaStep
= 0.0F
;
543 # endif /* GL_FLOAT */
546 #endif /* INTERP_RGB */
548 span
.interpMask
|= SPAN_SPEC
;
549 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
550 GLfloat eMaj_dsr
= (GLfloat
) ((ColorTemp
) vMax
->specular
[RCOMP
] - (ColorTemp
) vMin
->specular
[RCOMP
]);
551 GLfloat eBot_dsr
= (GLfloat
) ((ColorTemp
) vMid
->specular
[RCOMP
] - (ColorTemp
) vMin
->specular
[RCOMP
]);
552 GLfloat eMaj_dsg
= (GLfloat
) ((ColorTemp
) vMax
->specular
[GCOMP
] - (ColorTemp
) vMin
->specular
[GCOMP
]);
553 GLfloat eBot_dsg
= (GLfloat
) ((ColorTemp
) vMid
->specular
[GCOMP
] - (ColorTemp
) vMin
->specular
[GCOMP
]);
554 GLfloat eMaj_dsb
= (GLfloat
) ((ColorTemp
) vMax
->specular
[BCOMP
] - (ColorTemp
) vMin
->specular
[BCOMP
]);
555 GLfloat eBot_dsb
= (GLfloat
) ((ColorTemp
) vMid
->specular
[BCOMP
] - (ColorTemp
) vMin
->specular
[BCOMP
]);
556 span
.attrStepX
[FRAG_ATTRIB_COL1
][0] = oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
557 span
.attrStepY
[FRAG_ATTRIB_COL1
][0] = oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
558 span
.attrStepX
[FRAG_ATTRIB_COL1
][1] = oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
559 span
.attrStepY
[FRAG_ATTRIB_COL1
][1] = oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
560 span
.attrStepX
[FRAG_ATTRIB_COL1
][2] = oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
561 span
.attrStepY
[FRAG_ATTRIB_COL1
][2] = oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
562 # if CHAN_TYPE == GL_FLOAT
563 span
.specRedStep
= span
.attrStep
[FRAG_ATTRIB_COL1
][0];
564 span
.specGreenStep
= span
.dsgdx
;
565 span
.specBlueStep
= span
.dsbdx
;
567 span
.specRedStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][0]);
568 span
.specGreenStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][1]);
569 span
.specBlueStep
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL1
][2]);
573 span
.attrStepX
[FRAG_ATTRIB_COL1
][0] = span
.attrStepY
[FRAG_ATTRIB_COL1
][0] = 0.0F
;
574 span
.attrStepX
[FRAG_ATTRIB_COL1
][1] = span
.attrStepY
[FRAG_ATTRIB_COL1
][1] = 0.0F
;
575 span
.attrStepX
[FRAG_ATTRIB_COL1
][2] = span
.attrStepY
[FRAG_ATTRIB_COL1
][2] = 0.0F
;
576 # if CHAN_TYPE == GL_FLOAT
577 span
.specRedStep
= 0.0F
;
578 span
.specGreenStep
= 0.0F
;
579 span
.specBlueStep
= 0.0F
;
581 span
.specRedStep
= 0;
582 span
.specGreenStep
= 0;
583 span
.specBlueStep
= 0;
586 #endif /* INTERP_SPEC */
588 span
.interpMask
|= SPAN_INDEX
;
589 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
590 GLfloat eMaj_di
= vMax
->index
- vMin
->index
;
591 GLfloat eBot_di
= vMid
->index
- vMin
->index
;
592 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
593 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
594 span
.indexStep
= SignedFloatToFixed(didx
);
597 span
.interpMask
|= SPAN_FLAT
;
602 #ifdef INTERP_INT_TEX
603 span
.interpMask
|= SPAN_INT_TEXTURE
;
605 GLfloat eMaj_ds
= (vMax
->attrib
[FRAG_ATTRIB_TEX0
][0] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][0]) * S_SCALE
;
606 GLfloat eBot_ds
= (vMid
->attrib
[FRAG_ATTRIB_TEX0
][0] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][0]) * S_SCALE
;
607 GLfloat eMaj_dt
= (vMax
->attrib
[FRAG_ATTRIB_TEX0
][1] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][1]) * T_SCALE
;
608 GLfloat eBot_dt
= (vMid
->attrib
[FRAG_ATTRIB_TEX0
][1] - vMin
->attrib
[FRAG_ATTRIB_TEX0
][1]) * T_SCALE
;
609 span
.attrStepX
[FRAG_ATTRIB_TEX0
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
610 span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
611 span
.attrStepX
[FRAG_ATTRIB_TEX0
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
612 span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
613 span
.intTexStep
[0] = SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_TEX0
][0]);
614 span
.intTexStep
[1] = SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_TEX0
][1]);
618 span
.interpMask
|= (SPAN_TEXTURE
| SPAN_VARYING
);
621 const GLfloat wMax
= vMax
->win
[3], wMin
= vMin
->win
[3], wMid
= vMid
->win
[3];
623 GLfloat eMaj_ds
= vMax
->attrib
[attr
][0] * wMax
- vMin
->attrib
[attr
][0] * wMin
;
624 GLfloat eBot_ds
= vMid
->attrib
[attr
][0] * wMid
- vMin
->attrib
[attr
][0] * wMin
;
625 GLfloat eMaj_dt
= vMax
->attrib
[attr
][1] * wMax
- vMin
->attrib
[attr
][1] * wMin
;
626 GLfloat eBot_dt
= vMid
->attrib
[attr
][1] * wMid
- vMin
->attrib
[attr
][1] * wMin
;
627 GLfloat eMaj_du
= vMax
->attrib
[attr
][2] * wMax
- vMin
->attrib
[attr
][2] * wMin
;
628 GLfloat eBot_du
= vMid
->attrib
[attr
][2] * wMid
- vMin
->attrib
[attr
][2] * wMin
;
629 GLfloat eMaj_dv
= vMax
->attrib
[attr
][3] * wMax
- vMin
->attrib
[attr
][3] * wMin
;
630 GLfloat eBot_dv
= vMid
->attrib
[attr
][3] * wMid
- vMin
->attrib
[attr
][3] * wMin
;
631 span
.attrStepX
[attr
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
632 span
.attrStepY
[attr
][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
633 span
.attrStepX
[attr
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
634 span
.attrStepY
[attr
][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
635 span
.attrStepX
[attr
][2] = oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
636 span
.attrStepY
[attr
][2] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
637 span
.attrStepX
[attr
][3] = oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
638 span
.attrStepY
[attr
][3] = oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
644 * We always sample at pixel centers. However, we avoid
645 * explicit half-pixel offsets in this code by incorporating
646 * the proper offset in each of x and y during the
647 * transformation to window coordinates.
649 * We also apply the usual rasterization rules to prevent
650 * cracks and overlaps. A pixel is considered inside a
651 * subtriangle if it meets all of four conditions: it is on or
652 * to the right of the left edge, strictly to the left of the
653 * right edge, on or below the top edge, and strictly above
654 * the bottom edge. (Some edges may be degenerate.)
656 * The following discussion assumes left-to-right scanning
657 * (that is, the major edge is on the left); the right-to-left
658 * case is a straightforward variation.
660 * We start by finding the half-integral y coordinate that is
661 * at or below the top of the triangle. This gives us the
662 * first scan line that could possibly contain pixels that are
663 * inside the triangle.
665 * Next we creep down the major edge until we reach that y,
666 * and compute the corresponding x coordinate on the edge.
667 * Then we find the half-integral x that lies on or just
668 * inside the edge. This is the first pixel that might lie in
669 * the interior of the triangle. (We won't know for sure
670 * until we check the other edges.)
672 * As we rasterize the triangle, we'll step down the major
673 * edge. For each step in y, we'll move an integer number
674 * of steps in x. There are two possible x step sizes, which
675 * we'll call the ``inner'' step (guaranteed to land on the
676 * edge or inside it) and the ``outer'' step (guaranteed to
677 * land on the edge or outside it). The inner and outer steps
678 * differ by one. During rasterization we maintain an error
679 * term that indicates our distance from the true edge, and
680 * select either the inner step or the outer step, whichever
681 * gets us to the first pixel that falls inside the triangle.
683 * All parameters (z, red, etc.) as well as the buffer
684 * addresses for color and z have inner and outer step values,
685 * so that we can increment them appropriately. This method
686 * eliminates the need to adjust parameters by creeping a
687 * sub-pixel amount into the triangle at each scanline.
692 GLinterp fxLeftEdge
= 0, fxRightEdge
= 0;
693 GLinterp fdxLeftEdge
= 0, fdxRightEdge
= 0;
694 GLinterp fError
= 0, fdError
= 0;
696 PIXEL_TYPE
*pRow
= NULL
;
697 GLint dPRowOuter
= 0, dPRowInner
; /* offset in bytes */
701 struct gl_renderbuffer
*zrb
702 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
703 DEPTH_TYPE
*zRow
= NULL
;
704 GLint dZRowOuter
= 0, dZRowInner
; /* offset in bytes */
707 GLfixed fdzOuter
= 0, fdzInner
;
710 GLfloat wLeft
= 0, dwOuter
= 0, dwInner
;
713 GLfloat fogLeft
= 0, dfogOuter
= 0, dfogInner
;
716 ColorTemp rLeft
= 0, fdrOuter
= 0, fdrInner
;
717 ColorTemp gLeft
= 0, fdgOuter
= 0, fdgInner
;
718 ColorTemp bLeft
= 0, fdbOuter
= 0, fdbInner
;
721 ColorTemp aLeft
= 0, fdaOuter
= 0, fdaInner
;
724 ColorTemp srLeft
=0, dsrOuter
=0, dsrInner
;
725 ColorTemp sgLeft
=0, dsgOuter
=0, dsgInner
;
726 ColorTemp sbLeft
=0, dsbOuter
=0, dsbInner
;
729 GLfixed iLeft
=0, diOuter
=0, diInner
;
731 #ifdef INTERP_INT_TEX
732 GLfixed sLeft
=0, dsOuter
=0, dsInner
;
733 GLfixed tLeft
=0, dtOuter
=0, dtInner
;
736 GLfloat sLeft
[FRAG_ATTRIB_MAX
];
737 GLfloat tLeft
[FRAG_ATTRIB_MAX
];
738 GLfloat uLeft
[FRAG_ATTRIB_MAX
];
739 GLfloat vLeft
[FRAG_ATTRIB_MAX
];
740 GLfloat dsOuter
[FRAG_ATTRIB_MAX
], dsInner
[FRAG_ATTRIB_MAX
];
741 GLfloat dtOuter
[FRAG_ATTRIB_MAX
], dtInner
[FRAG_ATTRIB_MAX
];
742 GLfloat duOuter
[FRAG_ATTRIB_MAX
], duInner
[FRAG_ATTRIB_MAX
];
743 GLfloat dvOuter
[FRAG_ATTRIB_MAX
], dvInner
[FRAG_ATTRIB_MAX
];
746 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
747 EdgeT
*eLeft
, *eRight
;
748 int setupLeft
, setupRight
;
751 if (subTriangle
==0) {
753 if (scan_from_left_to_right
) {
756 lines
= eRight
->lines
;
763 lines
= eLeft
->lines
;
770 if (scan_from_left_to_right
) {
773 lines
= eRight
->lines
;
780 lines
= eLeft
->lines
;
788 if (setupLeft
&& eLeft
->lines
> 0) {
789 const SWvertex
*vLower
= eLeft
->v0
;
790 #if TRIANGLE_WALK_DOUBLE
791 const GLdouble fsy
= eLeft
->fsy
;
792 const GLdouble fsx
= eLeft
->fsx
;
793 const GLdouble fx
= CEILF(fsx
);
794 const GLdouble adjx
= (fx
- eLeft
->fx0
) * FIXED_SCALE
; /* SCALED! */
796 const GLfixed fsy
= eLeft
->fsy
;
797 const GLfixed fsx
= eLeft
->fsx
; /* no fractional part */
798 const GLfixed fx
= FixedCeil(fsx
); /* no fractional part */
799 const GLfixed adjx
= (GLinterp
) (fx
- eLeft
->fx0
); /* SCALED! */
801 const GLinterp adjy
= (GLinterp
) eLeft
->adjy
; /* SCALED! */
803 #if TRIANGLE_WALK_DOUBLE
806 fError
= fx
- fsx
- 1.0;
808 fdxLeftEdge
= eLeft
->dxdy
;
809 dxOuter
= FLOORF(fdxLeftEdge
);
810 fdError
= dxOuter
- fdxLeftEdge
+ 1.0;
811 idxOuter
= (GLint
) dxOuter
;
812 span
.y
= (GLint
) fsy
;
817 fError
= fx
- fsx
- FIXED_ONE
;
818 fxLeftEdge
= fsx
- FIXED_EPSILON
;
819 fdxLeftEdge
= eLeft
->fdxdy
;
820 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
821 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
822 idxOuter
= FixedToInt(fdxOuter
);
823 dxOuter
= (GLfloat
) idxOuter
;
824 span
.y
= FixedToInt(fsy
);
827 /* silence warnings on some compilers */
835 pRow
= (PIXEL_TYPE
*) PIXEL_ADDRESS(InterpToInt(fxLeftEdge
), span
.y
);
836 dPRowOuter
= -((int)BYTES_PER_ROW
) + idxOuter
* sizeof(PIXEL_TYPE
);
837 /* negative because Y=0 at bottom and increases upward */
841 * Now we need the set of parameter (z, color, etc.) values at
842 * the point (fx, fsy). This gives us properly-sampled parameter
843 * values that we can step from pixel to pixel. Furthermore,
844 * although we might have intermediate results that overflow
845 * the normal parameter range when we step temporarily outside
846 * the triangle, we shouldn't overflow or underflow for any
847 * pixel that's actually inside the triangle.
852 GLfloat z0
= vLower
->win
[2];
853 if (depthBits
<= 16) {
854 /* interpolate fixed-pt values */
855 GLfloat tmp
= (z0
* FIXED_SCALE
856 + span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] * adjx
857 + span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] * adjy
) + FIXED_HALF
;
858 if (tmp
< MAX_GLUINT
/ 2)
859 zLeft
= (GLfixed
) tmp
;
861 zLeft
= MAX_GLUINT
/ 2;
862 fdzOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
865 /* interpolate depth values w/out scaling */
866 zLeft
= (GLuint
) (z0
+ span
.attrStepX
[FRAG_ATTRIB_WPOS
][2] * FixedToFloat(adjx
)
867 + span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] * FixedToFloat(adjy
));
868 fdzOuter
= (GLint
) (span
.attrStepY
[FRAG_ATTRIB_WPOS
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][2]);
871 zRow
= (DEPTH_TYPE
*)
872 zrb
->GetPointer(ctx
, zrb
, InterpToInt(fxLeftEdge
), span
.y
);
873 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
878 wLeft
= vLower
->win
[3] + (span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] * adjy
) * (1.0F
/FIXED_SCALE
);
879 dwOuter
= span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_WPOS
][3];
883 fogLeft
= vLower
->fog
* vLower
->win
[3] + (span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
885 fogLeft
= vLower
->fog
+ (span
.attrStepX
[FRAG_ATTRIB_FOGC
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
887 dfogOuter
= span
.attrStepY
[FRAG_ATTRIB_FOGC
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_FOGC
][0];
890 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
891 # if CHAN_TYPE == GL_FLOAT
892 rLeft
= vLower
->color
[RCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][0] * adjy
) * (1.0F
/ FIXED_SCALE
);
893 gLeft
= vLower
->color
[GCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][1] * adjy
) * (1.0F
/ FIXED_SCALE
);
894 bLeft
= vLower
->color
[BCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][2] * adjy
) * (1.0F
/ FIXED_SCALE
);
895 fdrOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][0];
896 fdgOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][1];
897 fdbOuter
= span
.attrStepY
[FRAG_ATTRIB_COL0
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][2];
899 rLeft
= (GLint
)(ChanToFixed(vLower
->color
[RCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][0] * adjy
) + FIXED_HALF
;
900 gLeft
= (GLint
)(ChanToFixed(vLower
->color
[GCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][1] * adjy
) + FIXED_HALF
;
901 bLeft
= (GLint
)(ChanToFixed(vLower
->color
[BCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL0
][2] * adjy
) + FIXED_HALF
;
902 fdrOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][0]);
903 fdgOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][1]);
904 fdbOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL0
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][2]);
907 # if CHAN_TYPE == GL_FLOAT
908 aLeft
= vLower
->color
[ACOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjx
+ span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjy
) * (1.0F
/ FIXED_SCALE
);
909 fdaOuter
= span
.attrStepX
[FRAG_ATTRIB_COL0
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][3];
911 aLeft
= (GLint
)(ChanToFixed(vLower
->color
[ACOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjx
+ span
.attrStepX
[FRAG_ATTRIB_COL0
][3] * adjy
) + FIXED_HALF
;
912 fdaOuter
= SignedFloatToFixed(span
.attrStepX
[FRAG_ATTRIB_COL0
][3] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL0
][3]);
917 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
918 # if CHAN_TYPE == GL_FLOAT
919 rLeft
= v2
->color
[RCOMP
];
920 gLeft
= v2
->color
[GCOMP
];
921 bLeft
= v2
->color
[BCOMP
];
922 fdrOuter
= fdgOuter
= fdbOuter
= 0.0F
;
924 rLeft
= ChanToFixed(v2
->color
[RCOMP
]);
925 gLeft
= ChanToFixed(v2
->color
[GCOMP
]);
926 bLeft
= ChanToFixed(v2
->color
[BCOMP
]);
927 fdrOuter
= fdgOuter
= fdbOuter
= 0;
930 # if CHAN_TYPE == GL_FLOAT
931 aLeft
= v2
->color
[ACOMP
];
934 aLeft
= ChanToFixed(v2
->color
[ACOMP
]);
939 #endif /* INTERP_RGB */
943 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
944 # if CHAN_TYPE == GL_FLOAT
945 srLeft
= vLower
->specular
[RCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][0] * adjy
) * (1.0F
/ FIXED_SCALE
);
946 sgLeft
= vLower
->specular
[GCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][1] * adjy
) * (1.0F
/ FIXED_SCALE
);
947 sbLeft
= vLower
->specular
[BCOMP
] + (span
.attrStepX
[FRAG_ATTRIB_COL1
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][2] * adjy
) * (1.0F
/ FIXED_SCALE
);
948 dsrOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][0];
949 dsgOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][1];
950 dsbOuter
= span
.attrStepY
[FRAG_ATTRIB_COL1
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][2];
952 srLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[RCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][0] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][0] * adjy
) + FIXED_HALF
;
953 sgLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[GCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][1] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][1] * adjy
) + FIXED_HALF
;
954 sbLeft
= (GLfixed
) (ChanToFixed(vLower
->specular
[BCOMP
]) + span
.attrStepX
[FRAG_ATTRIB_COL1
][2] * adjx
+ span
.attrStepY
[FRAG_ATTRIB_COL1
][2] * adjy
) + FIXED_HALF
;
955 dsrOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][0]);
956 dsgOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][1]);
957 dsbOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_COL1
][2] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_COL1
][2]);
961 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
962 #if CHAN_TYPE == GL_FLOAT
963 srLeft
= v2
->specular
[RCOMP
];
964 sgLeft
= v2
->specular
[GCOMP
];
965 sbLeft
= v2
->specular
[BCOMP
];
966 dsrOuter
= dsgOuter
= dsbOuter
= 0.0F
;
968 srLeft
= ChanToFixed(v2
->specular
[RCOMP
]);
969 sgLeft
= ChanToFixed(v2
->specular
[GCOMP
]);
970 sbLeft
= ChanToFixed(v2
->specular
[BCOMP
]);
971 dsrOuter
= dsgOuter
= dsbOuter
= 0;
977 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
978 iLeft
= (GLfixed
)(vLower
->index
* FIXED_SCALE
979 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
980 diOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
983 ASSERT(ctx
->Light
.ShadeModel
== GL_FLAT
);
984 iLeft
= FloatToFixed(v2
->index
);
988 #ifdef INTERP_INT_TEX
991 s0
= vLower
->attrib
[FRAG_ATTRIB_TEX0
][0] * S_SCALE
;
992 sLeft
= (GLfixed
)(s0
* FIXED_SCALE
+ span
.attrStepX
[FRAG_ATTRIB_TEX0
][0] * adjx
993 + span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] * adjy
) + FIXED_HALF
;
994 dsOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_TEX0
][0] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_TEX0
][0]);
996 t0
= vLower
->attrib
[FRAG_ATTRIB_TEX0
][1] * T_SCALE
;
997 tLeft
= (GLfixed
)(t0
* FIXED_SCALE
+ span
.attrStepX
[FRAG_ATTRIB_TEX0
][1] * adjx
998 + span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] * adjy
) + FIXED_HALF
;
999 dtOuter
= SignedFloatToFixed(span
.attrStepY
[FRAG_ATTRIB_TEX0
][1] + dxOuter
* span
.attrStepX
[FRAG_ATTRIB_TEX0
][1]);
1004 const GLfloat invW
= vLower
->win
[3];
1005 const GLfloat s0
= vLower
->attrib
[attr
][0] * invW
;
1006 const GLfloat t0
= vLower
->attrib
[attr
][1] * invW
;
1007 const GLfloat u0
= vLower
->attrib
[attr
][2] * invW
;
1008 const GLfloat v0
= vLower
->attrib
[attr
][3] * invW
;
1009 sLeft
[attr
] = s0
+ (span
.attrStepX
[attr
][0] * adjx
+ span
.attrStepY
[attr
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
1010 tLeft
[attr
] = t0
+ (span
.attrStepX
[attr
][1] * adjx
+ span
.attrStepY
[attr
][1] * adjy
) * (1.0F
/FIXED_SCALE
);
1011 uLeft
[attr
] = u0
+ (span
.attrStepX
[attr
][2] * adjx
+ span
.attrStepY
[attr
][2] * adjy
) * (1.0F
/FIXED_SCALE
);
1012 vLeft
[attr
] = v0
+ (span
.attrStepX
[attr
][3] * adjx
+ span
.attrStepY
[attr
][3] * adjy
) * (1.0F
/FIXED_SCALE
);
1013 dsOuter
[attr
] = span
.attrStepY
[attr
][0] + dxOuter
* span
.attrStepX
[attr
][0];
1014 dtOuter
[attr
] = span
.attrStepY
[attr
][1] + dxOuter
* span
.attrStepX
[attr
][1];
1015 duOuter
[attr
] = span
.attrStepY
[attr
][2] + dxOuter
* span
.attrStepX
[attr
][2];
1016 dvOuter
[attr
] = span
.attrStepY
[attr
][3] + dxOuter
* span
.attrStepX
[attr
][3];
1022 if (setupRight
&& eRight
->lines
>0) {
1023 #if TRIANGLE_WALK_DOUBLE
1024 fxRightEdge
= eRight
->fsx
;
1025 fdxRightEdge
= eRight
->dxdy
;
1027 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
1028 fdxRightEdge
= eRight
->fdxdy
;
1037 /* Rasterize setup */
1038 #ifdef PIXEL_ADDRESS
1039 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
1043 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
1045 fdzInner
= fdzOuter
+ span
.zStep
;
1048 dwInner
= dwOuter
+ span
.attrStepX
[FRAG_ATTRIB_WPOS
][3];
1051 dfogInner
= dfogOuter
+ span
.attrStepX
[FRAG_ATTRIB_FOGC
][0];
1054 fdrInner
= fdrOuter
+ span
.redStep
;
1055 fdgInner
= fdgOuter
+ span
.greenStep
;
1056 fdbInner
= fdbOuter
+ span
.blueStep
;
1059 fdaInner
= fdaOuter
+ span
.alphaStep
;
1062 dsrInner
= dsrOuter
+ span
.specRedStep
;
1063 dsgInner
= dsgOuter
+ span
.specGreenStep
;
1064 dsbInner
= dsbOuter
+ span
.specBlueStep
;
1067 diInner
= diOuter
+ span
.indexStep
;
1069 #ifdef INTERP_INT_TEX
1070 dsInner
= dsOuter
+ span
.intTexStep
[0];
1071 dtInner
= dtOuter
+ span
.intTexStep
[1];
1075 dsInner
[attr
] = dsOuter
[attr
] + span
.attrStepX
[attr
][0];
1076 dtInner
[attr
] = dtOuter
[attr
] + span
.attrStepX
[attr
][1];
1077 duInner
[attr
] = duOuter
[attr
] + span
.attrStepX
[attr
][2];
1078 dvInner
[attr
] = dvOuter
[attr
] + span
.attrStepX
[attr
][3];
1083 /* initialize the span interpolants to the leftmost value */
1084 /* ff = fixed-pt fragment */
1085 const GLint right
= InterpToInt(fxRightEdge
);
1086 span
.x
= InterpToInt(fxLeftEdge
);
1087 if (right
<= span
.x
)
1090 span
.end
= right
- span
.x
;
1096 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = wLeft
;
1099 span
.attrStart
[FRAG_ATTRIB_FOGC
][0] = fogLeft
;
1110 span
.specRed
= srLeft
;
1111 span
.specGreen
= sgLeft
;
1112 span
.specBlue
= sbLeft
;
1117 #ifdef INTERP_INT_TEX
1118 span
.intTex
[0] = sLeft
;
1119 span
.intTex
[1] = tLeft
;
1124 span
.attrStart
[attr
][0] = sLeft
[attr
];
1125 span
.attrStart
[attr
][1] = tLeft
[attr
];
1126 span
.attrStart
[attr
][2] = uLeft
[attr
];
1127 span
.attrStart
[attr
][3] = vLeft
[attr
];
1131 /* This is where we actually generate fragments */
1132 /* XXX the test for span.y > 0 _shouldn't_ be needed but
1133 * it fixes a problem on 64-bit Opterons (bug 4842).
1135 if (span
.end
> 0 && span
.y
>= 0) {
1136 const GLint len
= span
.end
- 1;
1139 CLAMP_INTERPOLANT(red
, redStep
, len
);
1140 CLAMP_INTERPOLANT(green
, greenStep
, len
);
1141 CLAMP_INTERPOLANT(blue
, blueStep
, len
);
1144 CLAMP_INTERPOLANT(alpha
, alphaStep
, len
);
1147 CLAMP_INTERPOLANT(specRed
, specRedStep
, len
);
1148 CLAMP_INTERPOLANT(specGreen
, specGreenStep
, len
);
1149 CLAMP_INTERPOLANT(specBlue
, specBlueStep
, len
);
1152 CLAMP_INTERPOLANT(index
, indexStep
, len
);
1155 RENDER_SPAN( span
);
1160 * Advance to the next scan line. Compute the
1161 * new edge coordinates, and adjust the
1162 * pixel-center x coordinate so that it stays
1163 * on or inside the major edge.
1168 fxLeftEdge
+= fdxLeftEdge
;
1169 fxRightEdge
+= fdxRightEdge
;
1173 fError
-= INTERP_ONE
;
1175 #ifdef PIXEL_ADDRESS
1176 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowOuter
);
1180 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowOuter
);
1188 fogLeft
+= dfogOuter
;
1206 #ifdef INTERP_INT_TEX
1212 sLeft
[attr
] += dsOuter
[attr
];
1213 tLeft
[attr
] += dtOuter
[attr
];
1214 uLeft
[attr
] += duOuter
[attr
];
1215 vLeft
[attr
] += dvOuter
[attr
];
1220 #ifdef PIXEL_ADDRESS
1221 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowInner
);
1225 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowInner
);
1233 fogLeft
+= dfogInner
;
1251 #ifdef INTERP_INT_TEX
1257 sLeft
[attr
] += dsInner
[attr
];
1258 tLeft
[attr
] += dtInner
[attr
];
1259 uLeft
[attr
] += duInner
[attr
];
1260 vLeft
[attr
] += dvInner
[attr
];
1266 } /* for subTriangle */
1280 #undef BYTES_PER_ROW
1281 #undef PIXEL_ADDRESS
1291 #undef INTERP_INT_TEX
1293 #undef TEX_UNIT_LOOP