1 /* $Id: s_tritemp.h,v 1.46 2003/03/16 20:10:01 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
26 /* $XFree86: xc/extras/Mesa/src/swrast/s_tritemp.h,v 1.2 2002/02/27 21:07:54 tsi Exp $ */
29 * Triangle Rasterizer Template
31 * This file is #include'd to generate custom triangle rasterizers.
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
47 * When one can directly address pixels in the color buffer the following
48 * macros can be defined and used to compute pixel addresses during
49 * rasterization (see pRow):
50 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
51 * BYTES_PER_ROW - number of bytes per row in the color buffer
52 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
53 * Y==0 at bottom of screen and increases upward.
55 * Similarly, for direct depth buffer access, this type is used for depth
57 * DEPTH_TYPE - either GLushort or GLuint
59 * Optionally, one may provide one-time setup code per triangle:
60 * SETUP_CODE - code which is to be executed once per triangle
61 * CLEANUP_CODE - code to execute at end of triangle
63 * The following macro MUST be defined:
64 * RENDER_SPAN(span) - code to write a span of pixels.
66 * This code was designed for the origin to be in the lower-left corner.
68 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
73 * ColorTemp is used for intermediate color values.
75 #if CHAN_TYPE == GL_FLOAT
76 #define ColorTemp GLfloat
78 #define ColorTemp GLint /* same as GLfixed */
82 * Either loop over all texture units, or just use unit zero.
84 #ifdef INTERP_MULTITEX
85 #define TEX_UNIT_LOOP(CODE) \
88 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
89 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
95 #elif defined(INTERP_TEX)
96 #define TEX_UNIT_LOOP(CODE) \
104 static void NAME(GLcontext
*ctx
, const SWvertex
*v0
,
109 const SWvertex
*v0
, *v1
; /* Y(v0) < Y(v1) */
110 GLfloat dx
; /* X(v1) - X(v0) */
111 GLfloat dy
; /* Y(v1) - Y(v0) */
112 GLfixed fdxdy
; /* dx/dy in fixed-point */
113 GLfixed fsx
; /* first sample point x coord */
115 GLfloat adjy
; /* adjust from v[0]->fy to fsy, scaled */
116 GLint lines
; /* number of lines to be sampled on this edge */
117 GLfixed fx0
; /* fixed pt X of lower endpoint */
121 const GLint depthBits
= ctx
->Visual
.depthBits
;
122 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
123 const GLfloat maxDepth
= ctx
->DepthMaxF
;
124 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
126 EdgeT eMaj
, eTop
, eBot
;
128 const SWvertex
*vMin
, *vMid
, *vMax
; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
129 float bf
= SWRAST_CONTEXT(ctx
)->_backface_sign
;
130 const GLint snapMask
= ~((FIXED_ONE
/ (1 << SUB_PIXEL_BITS
)) - 1); /* for x/y coord snapping */
131 GLfixed vMin_fx
, vMin_fy
, vMid_fx
, vMid_fy
, vMax_fx
, vMax_fy
;
135 INIT_SPAN(span
, GL_POLYGON
, 0, 0, 0);
138 (void) fixedToDepthShift
;
142 printf("%s()\n", __FUNCTION__);
143 printf(" %g, %g, %g\n", v0->win[0], v0->win[1], v0->win[2]);
144 printf(" %g, %g, %g\n", v1->win[0], v1->win[1], v1->win[2]);
145 printf(" %g, %g, %g\n", v2->win[0], v2->win[1], v2->win[2]);
148 /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
149 * And find the order of the 3 vertices along the Y axis.
152 const GLfixed fy0
= FloatToFixed(v0
->win
[1] - 0.5F
) & snapMask
;
153 const GLfixed fy1
= FloatToFixed(v1
->win
[1] - 0.5F
) & snapMask
;
154 const GLfixed fy2
= FloatToFixed(v2
->win
[1] - 0.5F
) & snapMask
;
159 vMin
= v0
; vMid
= v1
; vMax
= v2
;
160 vMin_fy
= fy0
; vMid_fy
= fy1
; vMax_fy
= fy2
;
162 else if (fy2
<= fy0
) {
164 vMin
= v2
; vMid
= v0
; vMax
= v1
;
165 vMin_fy
= fy2
; vMid_fy
= fy0
; vMax_fy
= fy1
;
169 vMin
= v0
; vMid
= v2
; vMax
= v1
;
170 vMin_fy
= fy0
; vMid_fy
= fy2
; vMax_fy
= fy1
;
177 vMin
= v1
; vMid
= v0
; vMax
= v2
;
178 vMin_fy
= fy1
; vMid_fy
= fy0
; vMax_fy
= fy2
;
181 else if (fy2
<= fy1
) {
183 vMin
= v2
; vMid
= v1
; vMax
= v0
;
184 vMin_fy
= fy2
; vMid_fy
= fy1
; vMax_fy
= fy0
;
189 vMin
= v1
; vMid
= v2
; vMax
= v0
;
190 vMin_fy
= fy1
; vMid_fy
= fy2
; vMax_fy
= fy0
;
194 /* fixed point X coords */
195 vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
) & snapMask
;
196 vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
) & snapMask
;
197 vMax_fx
= FloatToFixed(vMax
->win
[0] + 0.5F
) & snapMask
;
200 /* vertex/edge relationship */
201 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
202 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
203 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
205 /* compute deltas for each edge: vertex[upper] - vertex[lower] */
206 eMaj
.dx
= FixedToFloat(vMax_fx
- vMin_fx
);
207 eMaj
.dy
= FixedToFloat(vMax_fy
- vMin_fy
);
208 eTop
.dx
= FixedToFloat(vMax_fx
- vMid_fx
);
209 eTop
.dy
= FixedToFloat(vMax_fy
- vMid_fy
);
210 eBot
.dx
= FixedToFloat(vMid_fx
- vMin_fx
);
211 eBot
.dy
= FixedToFloat(vMid_fy
- vMin_fy
);
213 /* compute area, oneOverArea and perform backface culling */
215 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
217 /* Do backface culling */
221 if (IS_INF_OR_NAN(area
) || area
== 0.0F
)
224 oneOverArea
= 1.0F
/ area
;
227 #ifndef DO_OCCLUSION_TEST
228 ctx
->OcclusionResult
= GL_TRUE
;
230 span
.facing
= ctx
->_Facing
; /* for 2-sided stencil test */
232 /* Edge setup. For a triangle strip these could be reused... */
234 eMaj
.fsy
= FixedCeil(vMin_fy
);
235 eMaj
.lines
= FixedToInt(FixedCeil(vMax_fy
- eMaj
.fsy
));
236 if (eMaj
.lines
> 0) {
237 GLfloat dxdy
= eMaj
.dx
/ eMaj
.dy
;
238 eMaj
.fdxdy
= SignedFloatToFixed(dxdy
);
239 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
241 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* dxdy
);
247 eTop
.fsy
= FixedCeil(vMid_fy
);
248 eTop
.lines
= FixedToInt(FixedCeil(vMax_fy
- eTop
.fsy
));
249 if (eTop
.lines
> 0) {
250 GLfloat dxdy
= eTop
.dx
/ eTop
.dy
;
251 eTop
.fdxdy
= SignedFloatToFixed(dxdy
);
252 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
254 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* dxdy
);
257 eBot
.fsy
= FixedCeil(vMin_fy
);
258 eBot
.lines
= FixedToInt(FixedCeil(vMid_fy
- eBot
.fsy
));
259 if (eBot
.lines
> 0) {
260 GLfloat dxdy
= eBot
.dx
/ eBot
.dy
;
261 eBot
.fdxdy
= SignedFloatToFixed(dxdy
);
262 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
264 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* dxdy
);
269 * Conceptually, we view a triangle as two subtriangles
270 * separated by a perfectly horizontal line. The edge that is
271 * intersected by this line is one with maximal absolute dy; we
272 * call it a ``major'' edge. The other two edges are the
273 * ``top'' edge (for the upper subtriangle) and the ``bottom''
274 * edge (for the lower subtriangle). If either of these two
275 * edges is horizontal or very close to horizontal, the
276 * corresponding subtriangle might cover zero sample points;
277 * we take care to handle such cases, for performance as well
280 * By stepping rasterization parameters along the major edge,
281 * we can avoid recomputing them at the discontinuity where
282 * the top and bottom edges meet. However, this forces us to
283 * be able to scan both left-to-right and right-to-left.
284 * Also, we must determine whether the major edge is at the
285 * left or right side of the triangle. We do this by
286 * computing the magnitude of the cross-product of the major
287 * and top edges. Since this magnitude depends on the sine of
288 * the angle between the two edges, its sign tells us whether
289 * we turn to the left or to the right when travelling along
290 * the major edge to the top edge, and from this we infer
291 * whether the major edge is on the left or the right.
293 * Serendipitously, this cross-product magnitude is also a
294 * value we need to compute the iteration parameter
295 * derivatives for the triangle, and it can be used to perform
296 * backface culling because its sign tells us whether the
297 * triangle is clockwise or counterclockwise. In this code we
298 * refer to it as ``area'' because it's also proportional to
299 * the pixel area of the triangle.
303 GLint scan_from_left_to_right
; /* true if scanning left-to-right */
310 #if defined(INTERP_RGB)
315 #if defined(INTERP_ALPHA)
318 #if defined(INTERP_SPEC)
319 GLfloat dsrdx
, dsrdy
;
320 GLfloat dsgdx
, dsgdy
;
321 GLfloat dsbdx
, dsbdy
;
328 * Execute user-supplied setup code
334 scan_from_left_to_right
= (oneOverArea
< 0.0F
);
337 /* compute d?/dx and d?/dy derivatives */
339 span
.interpMask
|= SPAN_Z
;
341 GLfloat eMaj_dz
, eBot_dz
;
342 eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
343 eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
344 dzdx
= oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
345 if (dzdx
> maxDepth
|| dzdx
< -maxDepth
) {
346 /* probably a sliver triangle */
351 dzdy
= oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
354 span
.zStep
= SignedFloatToFixed(dzdx
);
356 span
.zStep
= (GLint
) dzdx
;
360 span
.interpMask
|= SPAN_FOG
;
362 const GLfloat eMaj_dfog
= vMax
->fog
- vMin
->fog
;
363 const GLfloat eBot_dfog
= vMid
->fog
- vMin
->fog
;
364 span
.fogStep
= oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
365 dfogdy
= oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
369 span
.interpMask
|= SPAN_RGBA
;
370 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
371 GLfloat eMaj_dr
= (GLfloat
) ((ColorTemp
) vMax
->color
[RCOMP
] - vMin
->color
[RCOMP
]);
372 GLfloat eBot_dr
= (GLfloat
) ((ColorTemp
) vMid
->color
[RCOMP
] - vMin
->color
[RCOMP
]);
373 GLfloat eMaj_dg
= (GLfloat
) ((ColorTemp
) vMax
->color
[GCOMP
] - vMin
->color
[GCOMP
]);
374 GLfloat eBot_dg
= (GLfloat
) ((ColorTemp
) vMid
->color
[GCOMP
] - vMin
->color
[GCOMP
]);
375 GLfloat eMaj_db
= (GLfloat
) ((ColorTemp
) vMax
->color
[BCOMP
] - vMin
->color
[BCOMP
]);
376 GLfloat eBot_db
= (GLfloat
) ((ColorTemp
) vMid
->color
[BCOMP
] - vMin
->color
[BCOMP
]);
378 GLfloat eMaj_da
= (GLfloat
) ((ColorTemp
) vMax
->color
[ACOMP
] - vMin
->color
[ACOMP
]);
379 GLfloat eBot_da
= (GLfloat
) ((ColorTemp
) vMid
->color
[ACOMP
] - vMin
->color
[ACOMP
]);
381 drdx
= oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
382 drdy
= oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
383 dgdx
= oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
384 dgdy
= oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
385 dbdx
= oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
386 dbdy
= oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
387 # if CHAN_TYPE == GL_FLOAT
389 span
.greenStep
= dgdx
;
390 span
.blueStep
= dbdx
;
392 span
.redStep
= SignedFloatToFixed(drdx
);
393 span
.greenStep
= SignedFloatToFixed(dgdx
);
394 span
.blueStep
= SignedFloatToFixed(dbdx
);
395 # endif /* GL_FLOAT */
397 dadx
= oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
398 dady
= oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
399 # if CHAN_TYPE == GL_FLOAT
400 span
.alphaStep
= dadx
;
402 span
.alphaStep
= SignedFloatToFixed(dadx
);
403 # endif /* GL_FLOAT */
404 # endif /* INTERP_ALPHA */
407 ASSERT (ctx
->Light
.ShadeModel
== GL_FLAT
);
408 span
.interpMask
|= SPAN_FLAT
;
409 drdx
= drdy
= span
.redStep
= 0;
410 dgdx
= dgdy
= span
.greenStep
= 0;
411 dbdx
= dbdy
= span
.blueStep
= 0;
413 dadx
= dady
= span
.alphaStep
= 0;
416 #endif /* INTERP_RGB */
418 span
.interpMask
|= SPAN_SPEC
;
419 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
420 GLfloat eMaj_dsr
= (GLfloat
) ((ColorTemp
) vMax
->specular
[RCOMP
] - vMin
->specular
[RCOMP
]);
421 GLfloat eBot_dsr
= (GLfloat
) ((ColorTemp
) vMid
->specular
[RCOMP
] - vMin
->specular
[RCOMP
]);
422 GLfloat eMaj_dsg
= (GLfloat
) ((ColorTemp
) vMax
->specular
[GCOMP
] - vMin
->specular
[GCOMP
]);
423 GLfloat eBot_dsg
= (GLfloat
) ((ColorTemp
) vMid
->specular
[GCOMP
] - vMin
->specular
[GCOMP
]);
424 GLfloat eMaj_dsb
= (GLfloat
) ((ColorTemp
) vMax
->specular
[BCOMP
] - vMin
->specular
[BCOMP
]);
425 GLfloat eBot_dsb
= (GLfloat
) ((ColorTemp
) vMid
->specular
[BCOMP
] - vMin
->specular
[BCOMP
]);
426 dsrdx
= oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
427 dsrdy
= oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
428 dsgdx
= oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
429 dsgdy
= oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
430 dsbdx
= oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
431 dsbdy
= oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
432 # if CHAN_TYPE == GL_FLOAT
433 span
.specRedStep
= dsrdx
;
434 span
.specGreenStep
= dsgdx
;
435 span
.specBlueStep
= dsbdx
;
437 span
.specRedStep
= SignedFloatToFixed(dsrdx
);
438 span
.specGreenStep
= SignedFloatToFixed(dsgdx
);
439 span
.specBlueStep
= SignedFloatToFixed(dsbdx
);
443 dsrdx
= dsrdy
= span
.specRedStep
= 0;
444 dsgdx
= dsgdy
= span
.specGreenStep
= 0;
445 dsbdx
= dsbdy
= span
.specBlueStep
= 0;
447 #endif /* INTERP_SPEC */
449 span
.interpMask
|= SPAN_INDEX
;
450 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
451 GLfloat eMaj_di
= (GLfloat
) ((GLint
) vMax
->index
- (GLint
) vMin
->index
);
452 GLfloat eBot_di
= (GLfloat
) ((GLint
) vMid
->index
- (GLint
) vMin
->index
);
453 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
454 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
455 span
.indexStep
= SignedFloatToFixed(didx
);
458 span
.interpMask
|= SPAN_FLAT
;
463 #ifdef INTERP_INT_TEX
464 span
.interpMask
|= SPAN_INT_TEXTURE
;
466 GLfloat eMaj_ds
= (vMax
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
467 GLfloat eBot_ds
= (vMid
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
468 GLfloat eMaj_dt
= (vMax
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
469 GLfloat eBot_dt
= (vMid
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
470 span
.texStepX
[0][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
471 span
.texStepY
[0][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
472 span
.texStepX
[0][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
473 span
.texStepY
[0][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
474 span
.intTexStep
[0] = SignedFloatToFixed(span
.texStepX
[0][0]);
475 span
.intTexStep
[1] = SignedFloatToFixed(span
.texStepX
[0][1]);
479 span
.interpMask
|= SPAN_TEXTURE
;
482 const GLfloat wMax
= vMax
->win
[3], wMin
= vMin
->win
[3], wMid
= vMid
->win
[3];
484 GLfloat eMaj_ds
= vMax
->texcoord
[u
][0] * wMax
- vMin
->texcoord
[u
][0] * wMin
;
485 GLfloat eBot_ds
= vMid
->texcoord
[u
][0] * wMid
- vMin
->texcoord
[u
][0] * wMin
;
486 GLfloat eMaj_dt
= vMax
->texcoord
[u
][1] * wMax
- vMin
->texcoord
[u
][1] * wMin
;
487 GLfloat eBot_dt
= vMid
->texcoord
[u
][1] * wMid
- vMin
->texcoord
[u
][1] * wMin
;
488 GLfloat eMaj_du
= vMax
->texcoord
[u
][2] * wMax
- vMin
->texcoord
[u
][2] * wMin
;
489 GLfloat eBot_du
= vMid
->texcoord
[u
][2] * wMid
- vMin
->texcoord
[u
][2] * wMin
;
490 GLfloat eMaj_dv
= vMax
->texcoord
[u
][3] * wMax
- vMin
->texcoord
[u
][3] * wMin
;
491 GLfloat eBot_dv
= vMid
->texcoord
[u
][3] * wMid
- vMin
->texcoord
[u
][3] * wMin
;
492 span
.texStepX
[u
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
493 span
.texStepY
[u
][0] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
494 span
.texStepX
[u
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
495 span
.texStepY
[u
][1] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
496 span
.texStepX
[u
][2] = oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
497 span
.texStepY
[u
][2] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
498 span
.texStepX
[u
][3] = oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
499 span
.texStepY
[u
][3] = oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
505 * We always sample at pixel centers. However, we avoid
506 * explicit half-pixel offsets in this code by incorporating
507 * the proper offset in each of x and y during the
508 * transformation to window coordinates.
510 * We also apply the usual rasterization rules to prevent
511 * cracks and overlaps. A pixel is considered inside a
512 * subtriangle if it meets all of four conditions: it is on or
513 * to the right of the left edge, strictly to the left of the
514 * right edge, on or below the top edge, and strictly above
515 * the bottom edge. (Some edges may be degenerate.)
517 * The following discussion assumes left-to-right scanning
518 * (that is, the major edge is on the left); the right-to-left
519 * case is a straightforward variation.
521 * We start by finding the half-integral y coordinate that is
522 * at or below the top of the triangle. This gives us the
523 * first scan line that could possibly contain pixels that are
524 * inside the triangle.
526 * Next we creep down the major edge until we reach that y,
527 * and compute the corresponding x coordinate on the edge.
528 * Then we find the half-integral x that lies on or just
529 * inside the edge. This is the first pixel that might lie in
530 * the interior of the triangle. (We won't know for sure
531 * until we check the other edges.)
533 * As we rasterize the triangle, we'll step down the major
534 * edge. For each step in y, we'll move an integer number
535 * of steps in x. There are two possible x step sizes, which
536 * we'll call the ``inner'' step (guaranteed to land on the
537 * edge or inside it) and the ``outer'' step (guaranteed to
538 * land on the edge or outside it). The inner and outer steps
539 * differ by one. During rasterization we maintain an error
540 * term that indicates our distance from the true edge, and
541 * select either the inner step or the outer step, whichever
542 * gets us to the first pixel that falls inside the triangle.
544 * All parameters (z, red, etc.) as well as the buffer
545 * addresses for color and z have inner and outer step values,
546 * so that we can increment them appropriately. This method
547 * eliminates the need to adjust parameters by creeping a
548 * sub-pixel amount into the triangle at each scanline.
554 GLfixed fxLeftEdge
= 0, fxRightEdge
= 0;
555 GLfixed fdxLeftEdge
= 0, fdxRightEdge
= 0;
559 GLfixed fError
= 0, fdError
= 0;
563 PIXEL_TYPE
*pRow
= NULL
;
564 int dPRowOuter
= 0, dPRowInner
; /* offset in bytes */
568 DEPTH_TYPE
*zRow
= NULL
;
569 int dZRowOuter
= 0, dZRowInner
; /* offset in bytes */
571 GLfixed fz
= 0, fdzOuter
= 0, fdzInner
;
574 GLfloat fogLeft
= 0, dfogOuter
= 0, dfogInner
;
577 ColorTemp fr
= 0, fdrOuter
= 0, fdrInner
;
578 ColorTemp fg
= 0, fdgOuter
= 0, fdgInner
;
579 ColorTemp fb
= 0, fdbOuter
= 0, fdbInner
;
582 ColorTemp fa
= 0, fdaOuter
= 0, fdaInner
;
585 ColorTemp fsr
=0, fdsrOuter
=0, fdsrInner
;
586 ColorTemp fsg
=0, fdsgOuter
=0, fdsgInner
;
587 ColorTemp fsb
=0, fdsbOuter
=0, fdsbInner
;
590 GLfixed fi
=0, fdiOuter
=0, fdiInner
;
592 #ifdef INTERP_INT_TEX
593 GLfixed fs
=0, fdsOuter
=0, fdsInner
;
594 GLfixed ft
=0, fdtOuter
=0, fdtInner
;
597 GLfloat sLeft
[MAX_TEXTURE_COORD_UNITS
];
598 GLfloat tLeft
[MAX_TEXTURE_COORD_UNITS
];
599 GLfloat uLeft
[MAX_TEXTURE_COORD_UNITS
];
600 GLfloat vLeft
[MAX_TEXTURE_COORD_UNITS
];
601 GLfloat dsOuter
[MAX_TEXTURE_COORD_UNITS
], dsInner
[MAX_TEXTURE_COORD_UNITS
];
602 GLfloat dtOuter
[MAX_TEXTURE_COORD_UNITS
], dtInner
[MAX_TEXTURE_COORD_UNITS
];
603 GLfloat duOuter
[MAX_TEXTURE_COORD_UNITS
], duInner
[MAX_TEXTURE_COORD_UNITS
];
604 GLfloat dvOuter
[MAX_TEXTURE_COORD_UNITS
], dvInner
[MAX_TEXTURE_COORD_UNITS
];
607 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
608 EdgeT
*eLeft
, *eRight
;
609 int setupLeft
, setupRight
;
612 if (subTriangle
==0) {
614 if (scan_from_left_to_right
) {
617 lines
= eRight
->lines
;
624 lines
= eLeft
->lines
;
631 if (scan_from_left_to_right
) {
634 lines
= eRight
->lines
;
641 lines
= eLeft
->lines
;
649 if (setupLeft
&& eLeft
->lines
> 0) {
650 const SWvertex
*vLower
;
651 GLfixed fsx
= eLeft
->fsx
;
653 fError
= fx
- fsx
- FIXED_ONE
;
654 fxLeftEdge
= fsx
- FIXED_EPSILON
;
655 fdxLeftEdge
= eLeft
->fdxdy
;
656 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
657 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
658 idxOuter
= FixedToInt(fdxOuter
);
659 dxOuter
= (float) idxOuter
;
663 span
.y
= FixedToInt(fy
);
665 adjx
= (float)(fx
- eLeft
->fx0
); /* SCALED! */
666 adjy
= eLeft
->adjy
; /* SCALED! */
668 (void) adjx
; /* silence compiler warnings */
669 (void) adjy
; /* silence compiler warnings */
673 (void) vLower
; /* silence compiler warnings */
678 pRow
= (PIXEL_TYPE
*) PIXEL_ADDRESS(FixedToInt(fxLeftEdge
), span
.y
);
679 dPRowOuter
= -((int)BYTES_PER_ROW
) + idxOuter
* sizeof(PIXEL_TYPE
);
680 /* negative because Y=0 at bottom and increases upward */
684 * Now we need the set of parameter (z, color, etc.) values at
685 * the point (fx, fy). This gives us properly-sampled parameter
686 * values that we can step from pixel to pixel. Furthermore,
687 * although we might have intermediate results that overflow
688 * the normal parameter range when we step temporarily outside
689 * the triangle, we shouldn't overflow or underflow for any
690 * pixel that's actually inside the triangle.
695 GLfloat z0
= vLower
->win
[2];
696 if (depthBits
<= 16) {
697 /* interpolate fixed-pt values */
698 GLfloat tmp
= (z0
* FIXED_SCALE
+ dzdx
* adjx
+ dzdy
* adjy
) + FIXED_HALF
;
699 if (tmp
< MAX_GLUINT
/ 2)
703 fdzOuter
= SignedFloatToFixed(dzdy
+ dxOuter
* dzdx
);
706 /* interpolate depth values exactly */
707 fz
= (GLint
) (z0
+ dzdx
* FixedToFloat(adjx
) + dzdy
* FixedToFloat(adjy
));
708 fdzOuter
= (GLint
) (dzdy
+ dxOuter
* dzdx
);
711 zRow
= (DEPTH_TYPE
*)
712 _mesa_zbuffer_address(ctx
, FixedToInt(fxLeftEdge
), span
.y
);
713 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
718 fogLeft
= vLower
->fog
+ (span
.fogStep
* adjx
+ dfogdy
* adjy
)
719 * (1.0F
/FIXED_SCALE
);
720 dfogOuter
= dfogdy
+ dxOuter
* span
.fogStep
;
723 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
724 # if CHAN_TYPE == GL_FLOAT
725 fr
= vLower
->color
[RCOMP
] + (drdx
* adjx
+ drdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
726 fg
= vLower
->color
[GCOMP
] + (dgdx
* adjx
+ dgdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
727 fb
= vLower
->color
[BCOMP
] + (dbdx
* adjx
+ dbdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
728 fdrOuter
= drdy
+ dxOuter
* drdx
;
729 fdgOuter
= dgdy
+ dxOuter
* dgdx
;
730 fdbOuter
= dbdy
+ dxOuter
* dbdx
;
732 fr
= (ChanToFixed(vLower
->color
[RCOMP
]) + drdx
* adjx
+ drdy
* adjy
) + FIXED_HALF
;
733 fg
= (ChanToFixed(vLower
->color
[GCOMP
]) + dgdx
* adjx
+ dgdy
* adjy
) + FIXED_HALF
;
734 fb
= (ChanToFixed(vLower
->color
[BCOMP
]) + dbdx
* adjx
+ dbdy
* adjy
) + FIXED_HALF
;
735 fdrOuter
= SignedFloatToFixed(drdy
+ dxOuter
* drdx
);
736 fdgOuter
= SignedFloatToFixed(dgdy
+ dxOuter
* dgdx
);
737 fdbOuter
= SignedFloatToFixed(dbdy
+ dxOuter
* dbdx
);
740 # if CHAN_TYPE == GL_FLOAT
741 fa
= vLower
->color
[ACOMP
] + (dadx
* adjx
+ dady
* adjy
) * (1.0F
/ FIXED_SCALE
);
742 fdaOuter
= dady
+ dxOuter
* dadx
;
744 fa
= (ChanToFixed(vLower
->color
[ACOMP
]) + dadx
* adjx
+ dady
* adjy
) + FIXED_HALF
;
745 fdaOuter
= SignedFloatToFixed(dady
+ dxOuter
* dadx
);
750 ASSERT (ctx
->Light
.ShadeModel
== GL_FLAT
);
751 # if CHAN_TYPE == GL_FLOAT
752 fr
= v2
->color
[RCOMP
];
753 fg
= v2
->color
[GCOMP
];
754 fb
= v2
->color
[BCOMP
];
755 fdrOuter
= fdgOuter
= fdbOuter
= 0.0F
;
757 fr
= ChanToFixed(v2
->color
[RCOMP
]);
758 fg
= ChanToFixed(v2
->color
[GCOMP
]);
759 fb
= ChanToFixed(v2
->color
[BCOMP
]);
760 fdrOuter
= fdgOuter
= fdbOuter
= 0;
763 # if CHAN_TYPE == GL_FLOAT
764 fa
= v2
->color
[ACOMP
];
767 fa
= ChanToFixed(v2
->color
[ACOMP
]);
775 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
776 # if CHAN_TYPE == GL_FLOAT
777 fsr
= vLower
->specular
[RCOMP
] + (dsrdx
* adjx
+ dsrdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
778 fsg
= vLower
->specular
[GCOMP
] + (dsgdx
* adjx
+ dsgdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
779 fsb
= vLower
->specular
[BCOMP
] + (dsbdx
* adjx
+ dsbdy
* adjy
) * (1.0F
/ FIXED_SCALE
);
780 fdsrOuter
= dsrdy
+ dxOuter
* dsrdx
;
781 fdsgOuter
= dsgdy
+ dxOuter
* dsgdx
;
782 fdsbOuter
= dsbdy
+ dxOuter
* dsbdx
;
784 fsr
= (GLfixed
) (ChanToFixed(vLower
->specular
[RCOMP
]) + dsrdx
* adjx
+ dsrdy
* adjy
) + FIXED_HALF
;
785 fsg
= (GLfixed
) (ChanToFixed(vLower
->specular
[GCOMP
]) + dsgdx
* adjx
+ dsgdy
* adjy
) + FIXED_HALF
;
786 fsb
= (GLfixed
) (ChanToFixed(vLower
->specular
[BCOMP
]) + dsbdx
* adjx
+ dsbdy
* adjy
) + FIXED_HALF
;
787 fdsrOuter
= SignedFloatToFixed(dsrdy
+ dxOuter
* dsrdx
);
788 fdsgOuter
= SignedFloatToFixed(dsgdy
+ dxOuter
* dsgdx
);
789 fdsbOuter
= SignedFloatToFixed(dsbdy
+ dxOuter
* dsbdx
);
793 #if CHAN_TYPE == GL_FLOAT
794 fsr
= v2
->specular
[RCOMP
];
795 fsg
= v2
->specular
[GCOMP
];
796 fsb
= v2
->specular
[BCOMP
];
797 fdsrOuter
= fdsgOuter
= fdsbOuter
= 0.0F
;
799 fsr
= ChanToFixed(v2
->specular
[RCOMP
]);
800 fsg
= ChanToFixed(v2
->specular
[GCOMP
]);
801 fsb
= ChanToFixed(v2
->specular
[BCOMP
]);
802 fdsrOuter
= fdsgOuter
= fdsbOuter
= 0;
808 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
809 fi
= (GLfixed
)(vLower
->index
* FIXED_SCALE
810 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
811 fdiOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
814 fi
= (GLfixed
) (v2
->index
* FIXED_SCALE
);
818 #ifdef INTERP_INT_TEX
821 s0
= vLower
->texcoord
[0][0] * S_SCALE
;
822 fs
= (GLfixed
)(s0
* FIXED_SCALE
+ span
.texStepX
[0][0] * adjx
823 + span
.texStepY
[0][0] * adjy
) + FIXED_HALF
;
824 fdsOuter
= SignedFloatToFixed(span
.texStepY
[0][0] + dxOuter
* span
.texStepX
[0][0]);
826 t0
= vLower
->texcoord
[0][1] * T_SCALE
;
827 ft
= (GLfixed
)(t0
* FIXED_SCALE
+ span
.texStepX
[0][1] * adjx
828 + span
.texStepY
[0][1] * adjy
) + FIXED_HALF
;
829 fdtOuter
= SignedFloatToFixed(span
.texStepY
[0][1] + dxOuter
* span
.texStepX
[0][1]);
834 const GLfloat invW
= vLower
->win
[3];
835 const GLfloat s0
= vLower
->texcoord
[u
][0] * invW
;
836 const GLfloat t0
= vLower
->texcoord
[u
][1] * invW
;
837 const GLfloat u0
= vLower
->texcoord
[u
][2] * invW
;
838 const GLfloat v0
= vLower
->texcoord
[u
][3] * invW
;
839 sLeft
[u
] = s0
+ (span
.texStepX
[u
][0] * adjx
+ span
.texStepY
[u
][0] * adjy
) * (1.0F
/FIXED_SCALE
);
840 tLeft
[u
] = t0
+ (span
.texStepX
[u
][1] * adjx
+ span
.texStepY
[u
][1] * adjy
) * (1.0F
/FIXED_SCALE
);
841 uLeft
[u
] = u0
+ (span
.texStepX
[u
][2] * adjx
+ span
.texStepY
[u
][2] * adjy
) * (1.0F
/FIXED_SCALE
);
842 vLeft
[u
] = v0
+ (span
.texStepX
[u
][3] * adjx
+ span
.texStepY
[u
][3] * adjy
) * (1.0F
/FIXED_SCALE
);
843 dsOuter
[u
] = span
.texStepY
[u
][0] + dxOuter
* span
.texStepX
[u
][0];
844 dtOuter
[u
] = span
.texStepY
[u
][1] + dxOuter
* span
.texStepX
[u
][1];
845 duOuter
[u
] = span
.texStepY
[u
][2] + dxOuter
* span
.texStepX
[u
][2];
846 dvOuter
[u
] = span
.texStepY
[u
][3] + dxOuter
* span
.texStepX
[u
][3];
852 if (setupRight
&& eRight
->lines
>0) {
853 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
854 fdxRightEdge
= eRight
->fdxdy
;
862 /* Rasterize setup */
864 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
868 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
870 fdzInner
= fdzOuter
+ span
.zStep
;
873 dfogInner
= dfogOuter
+ span
.fogStep
;
875 #if defined(INTERP_RGB)
876 fdrInner
= fdrOuter
+ span
.redStep
;
877 fdgInner
= fdgOuter
+ span
.greenStep
;
878 fdbInner
= fdbOuter
+ span
.blueStep
;
880 #if defined(INTERP_ALPHA)
881 fdaInner
= fdaOuter
+ span
.alphaStep
;
883 #if defined(INTERP_SPEC)
884 fdsrInner
= fdsrOuter
+ span
.specRedStep
;
885 fdsgInner
= fdsgOuter
+ span
.specGreenStep
;
886 fdsbInner
= fdsbOuter
+ span
.specBlueStep
;
889 fdiInner
= fdiOuter
+ span
.indexStep
;
891 #ifdef INTERP_INT_TEX
892 fdsInner
= fdsOuter
+ span
.intTexStep
[0];
893 fdtInner
= fdtOuter
+ span
.intTexStep
[1];
897 dsInner
[u
] = dsOuter
[u
] + span
.texStepX
[u
][0];
898 dtInner
[u
] = dtOuter
[u
] + span
.texStepX
[u
][1];
899 duInner
[u
] = duOuter
[u
] + span
.texStepX
[u
][2];
900 dvInner
[u
] = dvOuter
[u
] + span
.texStepX
[u
][3];
905 /* initialize the span interpolants to the leftmost value */
906 /* ff = fixed-pt fragment */
907 const GLint right
= FixedToInt(fxRightEdge
);
909 span
.x
= FixedToInt(fxLeftEdge
);
914 span
.end
= right
- span
.x
;
922 #if defined(INTERP_RGB)
927 #if defined(INTERP_ALPHA)
930 #if defined(INTERP_SPEC)
932 span
.specGreen
= fsg
;
938 #ifdef INTERP_INT_TEX
945 span
.tex
[u
][0] = sLeft
[u
];
946 span
.tex
[u
][1] = tLeft
[u
];
947 span
.tex
[u
][2] = uLeft
[u
];
948 span
.tex
[u
][3] = vLeft
[u
];
954 /* need this to accomodate round-off errors */
955 const GLint len
= right
- span
.x
- 1;
956 GLfixed ffrend
= span
.red
+ len
* span
.redStep
;
957 GLfixed ffgend
= span
.green
+ len
* span
.greenStep
;
958 GLfixed ffbend
= span
.blue
+ len
* span
.blueStep
;
965 span
.green
-= ffgend
;
978 const GLint len
= right
- span
.x
- 1;
979 GLfixed ffaend
= span
.alpha
+ len
* span
.alphaStep
;
981 span
.alpha
-= ffaend
;
989 /* need this to accomodate round-off errors */
990 const GLint len
= right
- span
.x
- 1;
991 GLfixed ffsrend
= span
.specRed
+ len
* span
.specRedStep
;
992 GLfixed ffsgend
= span
.specGreen
+ len
* span
.specGreenStep
;
993 GLfixed ffsbend
= span
.specBlue
+ len
* span
.specBlueStep
;
995 span
.specRed
-= ffsrend
;
996 if (span
.specRed
< 0)
1000 span
.specGreen
-= ffsgend
;
1001 if (span
.specGreen
< 0)
1005 span
.specBlue
-= ffsbend
;
1006 if (span
.specBlue
< 0)
1012 if (span
.index
< 0) span
.index
= 0;
1015 /* This is where we actually generate fragments */
1017 RENDER_SPAN( span
);
1021 * Advance to the next scan line. Compute the
1022 * new edge coordinates, and adjust the
1023 * pixel-center x coordinate so that it stays
1024 * on or inside the major edge.
1029 fxLeftEdge
+= fdxLeftEdge
;
1030 fxRightEdge
+= fdxRightEdge
;
1035 fError
-= FIXED_ONE
;
1036 #ifdef PIXEL_ADDRESS
1037 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowOuter
);
1041 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowOuter
);
1046 fogLeft
+= dfogOuter
;
1048 #if defined(INTERP_RGB)
1053 #if defined(INTERP_ALPHA)
1056 #if defined(INTERP_SPEC)
1064 #ifdef INTERP_INT_TEX
1070 sLeft
[u
] += dsOuter
[u
];
1071 tLeft
[u
] += dtOuter
[u
];
1072 uLeft
[u
] += duOuter
[u
];
1073 vLeft
[u
] += dvOuter
[u
];
1078 #ifdef PIXEL_ADDRESS
1079 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowInner
);
1083 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowInner
);
1088 fogLeft
+= dfogInner
;
1090 #if defined(INTERP_RGB)
1095 #if defined(INTERP_ALPHA)
1098 #if defined(INTERP_SPEC)
1106 #ifdef INTERP_INT_TEX
1112 sLeft
[u
] += dsInner
[u
];
1113 tLeft
[u
] += dtInner
[u
];
1114 uLeft
[u
] += duInner
[u
];
1115 vLeft
[u
] += dvInner
[u
];
1121 } /* for subTriangle */
1135 #undef BYTES_PER_ROW
1136 #undef PIXEL_ADDRESS
1144 #undef INTERP_INT_TEX
1146 #undef INTERP_MULTITEX
1147 #undef TEX_UNIT_LOOP
1154 #undef DO_OCCLUSION_TEST