1 /* $Id: s_tritemp.h,v 1.17 2001/05/16 20:27:12 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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.
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
39 * INTERP_SPEC - if defined, interpolate specular RGB values
40 * INTERP_INDEX - if defined, interpolate color index values
41 * INTERP_INT_TEX - if defined, interpolate integer ST texcoords
42 * (fast, simple 2-D texture mapping)
43 * INTERP_TEX - if defined, interpolate set 0 float STRQ texcoords
44 * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red)
45 * INTERP_MULTITEX - if defined, interpolate N units of STRQ texcoords
46 * INTERP_LAMBDA - if defined, compute lambda value (for mipmapping)
47 * a lambda value for every texture unit
49 * When one can directly address pixels in the color buffer the following
50 * macros can be defined and used to compute pixel addresses during
51 * rasterization (see pRow):
52 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
53 * BYTES_PER_ROW - number of bytes per row in the color buffer
54 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
55 * Y==0 at bottom of screen and increases upward.
57 * Similarly, for direct depth buffer access, this type is used for depth
59 * DEPTH_TYPE - either GLushort or GLuint
61 * Optionally, one may provide one-time setup code per triangle:
62 * SETUP_CODE - code which is to be executed once per triangle
63 * CLEANUP_CODE - code to execute at end of triangle
65 * The following macro MUST be defined:
66 * RENDER_SPAN(span) - code to write a span of pixels.
68 * This code was designed for the origin to be in the lower-left corner.
70 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
73 /*void triangle( GLcontext *ctx, SWvertex *v0, SWvertex *v1, SWvertex *v2 )*/
76 const SWvertex
*v0
, *v1
; /* Y(v0) < Y(v1) */
77 GLfloat dx
; /* X(v1) - X(v0) */
78 GLfloat dy
; /* Y(v1) - Y(v0) */
79 GLfixed fdxdy
; /* dx/dy in fixed-point */
80 GLfixed fsx
; /* first sample point x coord */
82 GLfloat adjy
; /* adjust from v[0]->fy to fsy, scaled */
83 GLint lines
; /* number of lines to be sampled on this edge */
84 GLfixed fx0
; /* fixed pt X of lower endpoint */
88 const GLint depthBits
= ctx
->Visual
.depthBits
;
89 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
90 const GLfloat maxDepth
= ctx
->DepthMaxF
;
91 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
93 EdgeT eMaj
, eTop
, eBot
;
95 const SWvertex
*vMin
, *vMid
, *vMax
; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
96 float bf
= SWRAST_CONTEXT(ctx
)->_backface_sign
;
99 struct triangle_span span
;
102 (void) fixedToDepthShift
;
105 /* find the order of the 3 vertices along the Y axis */
107 GLfloat y0
= v0
->win
[1];
108 GLfloat y1
= v1
->win
[1];
109 GLfloat y2
= v2
->win
[1];
113 vMin
= v0
; vMid
= v1
; vMax
= v2
; /* y0<=y1<=y2 */
116 vMin
= v2
; vMid
= v0
; vMax
= v1
; /* y2<=y0<=y1 */
119 vMin
= v0
; vMid
= v2
; vMax
= v1
; bf
= -bf
; /* y0<=y2<=y1 */
124 vMin
= v1
; vMid
= v0
; vMax
= v2
; bf
= -bf
; /* y1<=y0<=y2 */
127 vMin
= v2
; vMid
= v1
; vMax
= v0
; bf
= -bf
; /* y2<=y1<=y0 */
130 vMin
= v1
; vMid
= v2
; vMax
= v0
; /* y1<=y2<=y0 */
135 /* vertex/edge relationship */
136 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
137 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
138 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
140 /* compute deltas for each edge: vertex[v1] - vertex[v0] */
141 eMaj
.dx
= vMax
->win
[0] - vMin
->win
[0];
142 eMaj
.dy
= vMax
->win
[1] - vMin
->win
[1];
143 eTop
.dx
= vMax
->win
[0] - vMid
->win
[0];
144 eTop
.dy
= vMax
->win
[1] - vMid
->win
[1];
145 eBot
.dx
= vMid
->win
[0] - vMin
->win
[0];
146 eBot
.dy
= vMid
->win
[1] - vMin
->win
[1];
148 /* compute oneOverArea */
150 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
152 /* Do backface culling */
159 /* check for very tiny triangle */
160 if (area
* area
< (0.05F
* 0.05F
)) { /* square to ensure positive value */
161 oneOverArea
= 1.0F
/ 0.05F
; /* a close-enough value */
165 oneOverArea
= 1.0F
/ area
;
170 #ifndef DO_OCCLUSION_TEST
171 ctx
->OcclusionResult
= GL_TRUE
;
174 /* Edge setup. For a triangle strip these could be reused... */
176 /* fixed point Y coordinates */
177 GLfixed vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
);
178 GLfixed vMin_fy
= FloatToFixed(vMin
->win
[1] - 0.5F
);
179 GLfixed vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
);
180 GLfixed vMid_fy
= FloatToFixed(vMid
->win
[1] - 0.5F
);
181 GLfixed vMax_fy
= FloatToFixed(vMax
->win
[1] - 0.5F
);
183 eMaj
.fsy
= FixedCeil(vMin_fy
);
184 eMaj
.lines
= FixedToInt(FixedCeil(vMax_fy
- eMaj
.fsy
));
185 if (eMaj
.lines
> 0) {
186 GLfloat dxdy
= eMaj
.dx
/ eMaj
.dy
;
187 eMaj
.fdxdy
= SignedFloatToFixed(dxdy
);
188 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
190 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* dxdy
);
196 eTop
.fsy
= FixedCeil(vMid_fy
);
197 eTop
.lines
= FixedToInt(FixedCeil(vMax_fy
- eTop
.fsy
));
198 if (eTop
.lines
> 0) {
199 GLfloat dxdy
= eTop
.dx
/ eTop
.dy
;
200 eTop
.fdxdy
= SignedFloatToFixed(dxdy
);
201 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
203 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* dxdy
);
206 eBot
.fsy
= FixedCeil(vMin_fy
);
207 eBot
.lines
= FixedToInt(FixedCeil(vMid_fy
- eBot
.fsy
));
208 if (eBot
.lines
> 0) {
209 GLfloat dxdy
= eBot
.dx
/ eBot
.dy
;
210 eBot
.fdxdy
= SignedFloatToFixed(dxdy
);
211 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
213 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* dxdy
);
218 * Conceptually, we view a triangle as two subtriangles
219 * separated by a perfectly horizontal line. The edge that is
220 * intersected by this line is one with maximal absolute dy; we
221 * call it a ``major'' edge. The other two edges are the
222 * ``top'' edge (for the upper subtriangle) and the ``bottom''
223 * edge (for the lower subtriangle). If either of these two
224 * edges is horizontal or very close to horizontal, the
225 * corresponding subtriangle might cover zero sample points;
226 * we take care to handle such cases, for performance as well
229 * By stepping rasterization parameters along the major edge,
230 * we can avoid recomputing them at the discontinuity where
231 * the top and bottom edges meet. However, this forces us to
232 * be able to scan both left-to-right and right-to-left.
233 * Also, we must determine whether the major edge is at the
234 * left or right side of the triangle. We do this by
235 * computing the magnitude of the cross-product of the major
236 * and top edges. Since this magnitude depends on the sine of
237 * the angle between the two edges, its sign tells us whether
238 * we turn to the left or to the right when travelling along
239 * the major edge to the top edge, and from this we infer
240 * whether the major edge is on the left or the right.
242 * Serendipitously, this cross-product magnitude is also a
243 * value we need to compute the iteration parameter
244 * derivatives for the triangle, and it can be used to perform
245 * backface culling because its sign tells us whether the
246 * triangle is clockwise or counterclockwise. In this code we
247 * refer to it as ``area'' because it's also proportional to
248 * the pixel area of the triangle.
252 GLint ltor
; /* true if scanning left-to-right */
268 GLfloat dsrdx
, dsrdy
;
269 GLfloat dsgdx
, dsgdy
;
270 GLfloat dsbdx
, dsbdy
;
275 #ifdef INTERP_INT_TEX
285 #ifdef INTERP_MULTITEX
286 GLfloat dsdy
[MAX_TEXTURE_UNITS
];
287 GLfloat dtdy
[MAX_TEXTURE_UNITS
];
288 GLfloat dudy
[MAX_TEXTURE_UNITS
];
289 GLfloat dvdy
[MAX_TEXTURE_UNITS
];
292 #if defined(INTERP_LAMBDA) && !defined(INTERP_TEX) && !defined(INTERP_MULTITEX)
293 #error "Mipmapping without texturing doesn't make sense."
297 * Execute user-supplied setup code
303 ltor
= (oneOverArea
< 0.0F
);
307 /* compute d?/dx and d?/dy derivatives */
309 span
.activeMask
|= SPAN_Z
;
311 GLfloat eMaj_dz
, eBot_dz
;
312 eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
313 eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
314 dzdx
= oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
315 if (dzdx
> maxDepth
|| dzdx
< -maxDepth
) {
316 /* probably a sliver triangle */
321 dzdy
= oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
324 span
.zStep
= SignedFloatToFixed(dzdx
);
326 span
.zStep
= (GLint
) dzdx
;
330 span
.activeMask
|= SPAN_FOG
;
332 const GLfloat eMaj_dfog
= vMax
->fog
- vMin
->fog
;
333 const GLfloat eBot_dfog
= vMid
->fog
- vMin
->fog
;
334 span
.fogStep
= oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
335 dfogdy
= oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
339 span
.activeMask
|= SPAN_RGBA
;
341 /* This is kind of a hack to eliminate RGB color over/underflow
342 * problems when rendering very tiny triangles. We're not doing
343 * anything with alpha or specular color at this time.
345 drdx
= drdy
= 0.0; span
.redStep
= 0;
346 dgdx
= dgdy
= 0.0; span
.greenStep
= 0;
347 dbdx
= dbdy
= 0.0; span
.blueStep
= 0;
350 GLfloat eMaj_dr
, eBot_dr
;
351 GLfloat eMaj_dg
, eBot_dg
;
352 GLfloat eMaj_db
, eBot_db
;
353 eMaj_dr
= (GLint
) vMax
->color
[0] - (GLint
) vMin
->color
[0];
354 eBot_dr
= (GLint
) vMid
->color
[0] - (GLint
) vMin
->color
[0];
355 drdx
= oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
356 span
.redStep
= SignedFloatToFixed(drdx
);
357 drdy
= oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
358 eMaj_dg
= (GLint
) vMax
->color
[1] - (GLint
) vMin
->color
[1];
359 eBot_dg
= (GLint
) vMid
->color
[1] - (GLint
) vMin
->color
[1];
360 dgdx
= oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
361 span
.greenStep
= SignedFloatToFixed(dgdx
);
362 dgdy
= oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
363 eMaj_db
= (GLint
) vMax
->color
[2] - (GLint
) vMin
->color
[2];
364 eBot_db
= (GLint
) vMid
->color
[2] - (GLint
) vMin
->color
[2];
365 dbdx
= oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
366 span
.blueStep
= SignedFloatToFixed(dbdx
);
367 dbdy
= oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
372 GLfloat eMaj_da
, eBot_da
;
373 eMaj_da
= (GLint
) vMax
->color
[3] - (GLint
) vMin
->color
[3];
374 eBot_da
= (GLint
) vMid
->color
[3] - (GLint
) vMin
->color
[3];
375 dadx
= oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
376 span
.alphaStep
= SignedFloatToFixed(dadx
);
377 dady
= oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
381 span
.activeMask
|= SPAN_SPEC
;
383 GLfloat eMaj_dsr
, eBot_dsr
;
384 eMaj_dsr
= (GLint
) vMax
->specular
[0] - (GLint
) vMin
->specular
[0];
385 eBot_dsr
= (GLint
) vMid
->specular
[0] - (GLint
) vMin
->specular
[0];
386 dsrdx
= oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
387 span
.specRedStep
= SignedFloatToFixed(dsrdx
);
388 dsrdy
= oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
391 GLfloat eMaj_dsg
, eBot_dsg
;
392 eMaj_dsg
= (GLint
) vMax
->specular
[1] - (GLint
) vMin
->specular
[1];
393 eBot_dsg
= (GLint
) vMid
->specular
[1] - (GLint
) vMin
->specular
[1];
394 dsgdx
= oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
395 span
.specGreenStep
= SignedFloatToFixed(dsgdx
);
396 dsgdy
= oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
399 GLfloat eMaj_dsb
, eBot_dsb
;
400 eMaj_dsb
= (GLint
) vMax
->specular
[2] - (GLint
) vMin
->specular
[2];
401 eBot_dsb
= (GLint
) vMid
->specular
[2] - (GLint
) vMin
->specular
[2];
402 dsbdx
= oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
403 span
.specBlueStep
= SignedFloatToFixed(dsbdx
);
404 dsbdy
= oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
408 span
.activeMask
|= SPAN_INDEX
;
410 GLfloat eMaj_di
, eBot_di
;
411 eMaj_di
= (GLint
) vMax
->index
- (GLint
) vMin
->index
;
412 eBot_di
= (GLint
) vMid
->index
- (GLint
) vMin
->index
;
413 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
414 span
.indexStep
= SignedFloatToFixed(didx
);
415 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
418 #ifdef INTERP_INT_TEX
419 span
.activeMask
|= SPAN_INT_TEXTURE
;
421 GLfloat eMaj_ds
, eBot_ds
;
422 eMaj_ds
= (vMax
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
423 eBot_ds
= (vMid
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
424 dsdx
= oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
425 span
.intTexStep
[0] = SignedFloatToFixed(dsdx
);
426 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
429 GLfloat eMaj_dt
, eBot_dt
;
430 eMaj_dt
= (vMax
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
431 eBot_dt
= (vMid
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
432 dtdx
= oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
433 span
.intTexStep
[1] = SignedFloatToFixed(dtdx
);
434 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
440 span
.activeMask
|= SPAN_TEXTURE
;
442 GLfloat wMax
= vMax
->win
[3];
443 GLfloat wMin
= vMin
->win
[3];
444 GLfloat wMid
= vMid
->win
[3];
445 GLfloat eMaj_ds
, eBot_ds
;
446 GLfloat eMaj_dt
, eBot_dt
;
447 GLfloat eMaj_du
, eBot_du
;
448 GLfloat eMaj_dv
, eBot_dv
;
450 eMaj_ds
= vMax
->texcoord
[0][0] * wMax
- vMin
->texcoord
[0][0] * wMin
;
451 eBot_ds
= vMid
->texcoord
[0][0] * wMid
- vMin
->texcoord
[0][0] * wMin
;
452 span
.texStep
[0][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
453 - eMaj
.dy
* eBot_ds
);
454 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
456 eMaj_dt
= vMax
->texcoord
[0][1] * wMax
- vMin
->texcoord
[0][1] * wMin
;
457 eBot_dt
= vMid
->texcoord
[0][1] * wMid
- vMin
->texcoord
[0][1] * wMin
;
458 span
.texStep
[0][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
459 - eMaj
.dy
* eBot_dt
);
460 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
462 eMaj_du
= vMax
->texcoord
[0][2] * wMax
- vMin
->texcoord
[0][2] * wMin
;
463 eBot_du
= vMid
->texcoord
[0][2] * wMid
- vMin
->texcoord
[0][2] * wMin
;
464 span
.texStep
[0][2] = oneOverArea
* (eMaj_du
* eBot
.dy
465 - eMaj
.dy
* eBot_du
);
466 dudy
= oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
468 eMaj_dv
= vMax
->texcoord
[0][3] * wMax
- vMin
->texcoord
[0][3] * wMin
;
469 eBot_dv
= vMid
->texcoord
[0][3] * wMid
- vMin
->texcoord
[0][3] * wMin
;
470 span
.texStep
[0][3] = oneOverArea
* (eMaj_dv
* eBot
.dy
471 - eMaj
.dy
* eBot_dv
);
472 dvdy
= oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
474 # ifdef INTERP_LAMBDA
476 GLfloat dudx
= span
.texStep
[0][0] * span
.texWidth
[0];
477 GLfloat dudy
= dsdy
* span
.texWidth
[0];
478 GLfloat dvdx
= span
.texStep
[0][1] * span
.texHeight
[0];
479 GLfloat dvdy
= dtdy
* span
.texHeight
[0];
480 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
481 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
482 span
.rho
[0] = r1
+ r2
; /* was rho2 = MAX2(r1,r2) */
483 span
.activeMask
|= SPAN_LAMBDA
;
488 #ifdef INTERP_MULTITEX
489 span
.activeMask
|= SPAN_TEXTURE
;
490 # ifdef INTERP_LAMBDA
491 span
.activeMask
|= SPAN_LAMBDA
;
494 GLfloat wMax
= vMax
->win
[3];
495 GLfloat wMin
= vMin
->win
[3];
496 GLfloat wMid
= vMid
->win
[3];
498 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
499 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
500 GLfloat eMaj_ds
, eBot_ds
;
501 GLfloat eMaj_dt
, eBot_dt
;
502 GLfloat eMaj_du
, eBot_du
;
503 GLfloat eMaj_dv
, eBot_dv
;
504 eMaj_ds
= vMax
->texcoord
[u
][0] * wMax
505 - vMin
->texcoord
[u
][0] * wMin
;
506 eBot_ds
= vMid
->texcoord
[u
][0] * wMid
507 - vMin
->texcoord
[u
][0] * wMin
;
508 span
.texStep
[u
][0] = oneOverArea
* (eMaj_ds
* eBot
.dy
509 - eMaj
.dy
* eBot_ds
);
510 dsdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
512 eMaj_dt
= vMax
->texcoord
[u
][1] * wMax
513 - vMin
->texcoord
[u
][1] * wMin
;
514 eBot_dt
= vMid
->texcoord
[u
][1] * wMid
515 - vMin
->texcoord
[u
][1] * wMin
;
516 span
.texStep
[u
][1] = oneOverArea
* (eMaj_dt
* eBot
.dy
517 - eMaj
.dy
* eBot_dt
);
518 dtdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
520 eMaj_du
= vMax
->texcoord
[u
][2] * wMax
521 - vMin
->texcoord
[u
][2] * wMin
;
522 eBot_du
= vMid
->texcoord
[u
][2] * wMid
523 - vMin
->texcoord
[u
][2] * wMin
;
524 span
.texStep
[u
][2] = oneOverArea
* (eMaj_du
* eBot
.dy
525 - eMaj
.dy
* eBot_du
);
526 dudy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
528 eMaj_dv
= vMax
->texcoord
[u
][3] * wMax
529 - vMin
->texcoord
[u
][3] * wMin
;
530 eBot_dv
= vMid
->texcoord
[u
][3] * wMid
531 - vMin
->texcoord
[u
][3] * wMin
;
532 span
.texStep
[u
][3] = oneOverArea
* (eMaj_dv
* eBot
.dy
533 - eMaj
.dy
* eBot_dv
);
534 dvdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
535 # ifdef INTERP_LAMBDA
537 GLfloat dudx
= span
.texStep
[u
][0] * span
.texWidth
[u
];
538 GLfloat dudy
= dsdy
[u
] * span
.texWidth
[u
];
539 GLfloat dvdx
= span
.texStep
[u
][1] * span
.texHeight
[u
];
540 GLfloat dvdy
= dtdy
[u
] * span
.texHeight
[u
];
541 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
542 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
543 span
.rho
[u
] = r1
+ r2
; /* was rho2 = MAX2(r1,r2) */
552 * We always sample at pixel centers. However, we avoid
553 * explicit half-pixel offsets in this code by incorporating
554 * the proper offset in each of x and y during the
555 * transformation to window coordinates.
557 * We also apply the usual rasterization rules to prevent
558 * cracks and overlaps. A pixel is considered inside a
559 * subtriangle if it meets all of four conditions: it is on or
560 * to the right of the left edge, strictly to the left of the
561 * right edge, on or below the top edge, and strictly above
562 * the bottom edge. (Some edges may be degenerate.)
564 * The following discussion assumes left-to-right scanning
565 * (that is, the major edge is on the left); the right-to-left
566 * case is a straightforward variation.
568 * We start by finding the half-integral y coordinate that is
569 * at or below the top of the triangle. This gives us the
570 * first scan line that could possibly contain pixels that are
571 * inside the triangle.
573 * Next we creep down the major edge until we reach that y,
574 * and compute the corresponding x coordinate on the edge.
575 * Then we find the half-integral x that lies on or just
576 * inside the edge. This is the first pixel that might lie in
577 * the interior of the triangle. (We won't know for sure
578 * until we check the other edges.)
580 * As we rasterize the triangle, we'll step down the major
581 * edge. For each step in y, we'll move an integer number
582 * of steps in x. There are two possible x step sizes, which
583 * we'll call the ``inner'' step (guaranteed to land on the
584 * edge or inside it) and the ``outer'' step (guaranteed to
585 * land on the edge or outside it). The inner and outer steps
586 * differ by one. During rasterization we maintain an error
587 * term that indicates our distance from the true edge, and
588 * select either the inner step or the outer step, whichever
589 * gets us to the first pixel that falls inside the triangle.
591 * All parameters (z, red, etc.) as well as the buffer
592 * addresses for color and z have inner and outer step values,
593 * so that we can increment them appropriately. This method
594 * eliminates the need to adjust parameters by creeping a
595 * sub-pixel amount into the triangle at each scanline.
601 GLfixed fxLeftEdge
, fxRightEdge
, fdxLeftEdge
, fdxRightEdge
;
605 GLfixed fError
, fdError
;
610 int dPRowOuter
, dPRowInner
; /* offset in bytes */
615 int dZRowOuter
, dZRowInner
; /* offset in bytes */
617 GLfixed fz
, fdzOuter
, fdzInner
;
620 GLfloat fogLeft
, dfogOuter
, dfogInner
;
623 GLfixed fr
, fdrOuter
, fdrInner
;
624 GLfixed fg
, fdgOuter
, fdgInner
;
625 GLfixed fb
, fdbOuter
, fdbInner
;
628 GLfixed fa
=0, fdaOuter
=0, fdaInner
;
631 GLfixed fsr
=0, fdsrOuter
=0, fdsrInner
;
632 GLfixed fsg
=0, fdsgOuter
=0, fdsgInner
;
633 GLfixed fsb
=0, fdsbOuter
=0, fdsbInner
;
636 GLfixed fi
=0, fdiOuter
=0, fdiInner
;
638 #ifdef INTERP_INT_TEX
639 GLfixed fs
=0, fdsOuter
=0, fdsInner
;
640 GLfixed ft
=0, fdtOuter
=0, fdtInner
;
643 GLfloat sLeft
=0, dsOuter
=0, dsInner
;
644 GLfloat tLeft
=0, dtOuter
=0, dtInner
;
645 GLfloat uLeft
=0, duOuter
=0, duInner
;
646 GLfloat vLeft
=0, dvOuter
=0, dvInner
;
648 #ifdef INTERP_MULTITEX
649 GLfloat sLeft
[MAX_TEXTURE_UNITS
];
650 GLfloat tLeft
[MAX_TEXTURE_UNITS
];
651 GLfloat uLeft
[MAX_TEXTURE_UNITS
];
652 GLfloat vLeft
[MAX_TEXTURE_UNITS
];
653 GLfloat dsOuter
[MAX_TEXTURE_UNITS
], dsInner
[MAX_TEXTURE_UNITS
];
654 GLfloat dtOuter
[MAX_TEXTURE_UNITS
], dtInner
[MAX_TEXTURE_UNITS
];
655 GLfloat duOuter
[MAX_TEXTURE_UNITS
], duInner
[MAX_TEXTURE_UNITS
];
656 GLfloat dvOuter
[MAX_TEXTURE_UNITS
], dvInner
[MAX_TEXTURE_UNITS
];
659 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
660 EdgeT
*eLeft
, *eRight
;
661 int setupLeft
, setupRight
;
664 if (subTriangle
==0) {
669 lines
= eRight
->lines
;
676 lines
= eLeft
->lines
;
686 lines
= eRight
->lines
;
693 lines
= eLeft
->lines
;
701 if (setupLeft
&& eLeft
->lines
> 0) {
702 const SWvertex
*vLower
;
703 GLfixed fsx
= eLeft
->fsx
;
705 fError
= fx
- fsx
- FIXED_ONE
;
706 fxLeftEdge
= fsx
- FIXED_EPSILON
;
707 fdxLeftEdge
= eLeft
->fdxdy
;
708 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
709 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
710 idxOuter
= FixedToInt(fdxOuter
);
711 dxOuter
= (float) idxOuter
;
715 span
.y
= FixedToInt(fy
);
717 adjx
= (float)(fx
- eLeft
->fx0
); /* SCALED! */
718 adjy
= eLeft
->adjy
; /* SCALED! */
719 (void) adjx
; /* silence compiler warnings */
720 (void) adjy
; /* silence compiler warnings */
723 (void) vLower
; /* silence compiler warnings */
727 pRow
= (PIXEL_TYPE
*) PIXEL_ADDRESS(FixedToInt(fxLeftEdge
), span
.y
);
728 dPRowOuter
= -((int)BYTES_PER_ROW
) + idxOuter
* sizeof(PIXEL_TYPE
);
729 /* negative because Y=0 at bottom and increases upward */
733 * Now we need the set of parameter (z, color, etc.) values at
734 * the point (fx, fy). This gives us properly-sampled parameter
735 * values that we can step from pixel to pixel. Furthermore,
736 * although we might have intermediate results that overflow
737 * the normal parameter range when we step temporarily outside
738 * the triangle, we shouldn't overflow or underflow for any
739 * pixel that's actually inside the triangle.
744 GLfloat z0
= vLower
->win
[2];
745 if (depthBits
<= 16) {
746 /* interpolate fixed-pt values */
747 GLfloat tmp
= (z0
* FIXED_SCALE
+
748 dzdx
* adjx
+ dzdy
* adjy
) + FIXED_HALF
;
749 if (tmp
< MAX_GLUINT
/ 2)
753 fdzOuter
= SignedFloatToFixed(dzdy
+ dxOuter
* dzdx
);
756 /* interpolate depth values exactly */
757 fz
= (GLint
) (z0
+ dzdx
* FixedToFloat(adjx
)
758 + dzdy
* FixedToFloat(adjy
));
759 fdzOuter
= (GLint
) (dzdy
+ dxOuter
* dzdx
);
762 zRow
= (DEPTH_TYPE
*)
763 _mesa_zbuffer_address(ctx
, FixedToInt(fxLeftEdge
), span
.y
);
764 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
769 fogLeft
= vLower
->fog
+ (span
.fogStep
* adjx
+ dfogdy
* adjy
)
770 * (1.0F
/FIXED_SCALE
);
771 dfogOuter
= dfogdy
+ dxOuter
* span
.fogStep
;
774 fr
= (GLfixed
)(IntToFixed(vLower
->color
[0])
775 + drdx
* adjx
+ drdy
* adjy
) + FIXED_HALF
;
776 fdrOuter
= SignedFloatToFixed(drdy
+ dxOuter
* drdx
);
778 fg
= (GLfixed
)(IntToFixed(vLower
->color
[1])
779 + dgdx
* adjx
+ dgdy
* adjy
) + FIXED_HALF
;
780 fdgOuter
= SignedFloatToFixed(dgdy
+ dxOuter
* dgdx
);
782 fb
= (GLfixed
)(IntToFixed(vLower
->color
[2])
783 + dbdx
* adjx
+ dbdy
* adjy
) + FIXED_HALF
;
784 fdbOuter
= SignedFloatToFixed(dbdy
+ dxOuter
* dbdx
);
787 fa
= (GLfixed
)(IntToFixed(vLower
->color
[3])
788 + dadx
* adjx
+ dady
* adjy
) + FIXED_HALF
;
789 fdaOuter
= SignedFloatToFixed(dady
+ dxOuter
* dadx
);
792 fsr
= (GLfixed
)(IntToFixed(vLower
->specular
[0])
793 + dsrdx
* adjx
+ dsrdy
* adjy
) + FIXED_HALF
;
794 fdsrOuter
= SignedFloatToFixed(dsrdy
+ dxOuter
* dsrdx
);
796 fsg
= (GLfixed
)(IntToFixed(vLower
->specular
[1])
797 + dsgdx
* adjx
+ dsgdy
* adjy
) + FIXED_HALF
;
798 fdsgOuter
= SignedFloatToFixed(dsgdy
+ dxOuter
* dsgdx
);
800 fsb
= (GLfixed
)(IntToFixed(vLower
->specular
[2])
801 + dsbdx
* adjx
+ dsbdy
* adjy
) + FIXED_HALF
;
802 fdsbOuter
= SignedFloatToFixed(dsbdy
+ dxOuter
* dsbdx
);
805 fi
= (GLfixed
)(vLower
->index
* FIXED_SCALE
806 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
807 fdiOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
809 #ifdef INTERP_INT_TEX
812 s0
= vLower
->texcoord
[0][0] * S_SCALE
;
813 fs
= (GLfixed
)(s0
* FIXED_SCALE
+ dsdx
* adjx
814 + dsdy
* adjy
) + FIXED_HALF
;
815 fdsOuter
= SignedFloatToFixed(dsdy
+ dxOuter
* dsdx
);
817 t0
= vLower
->texcoord
[0][1] * T_SCALE
;
818 ft
= (GLfixed
)(t0
* FIXED_SCALE
+ dtdx
* adjx
819 + dtdy
* adjy
) + FIXED_HALF
;
820 fdtOuter
= SignedFloatToFixed(dtdy
+ dxOuter
* dtdx
);
825 GLfloat invW
= vLower
->win
[3];
826 GLfloat s0
, t0
, u0
, v0
;
827 s0
= vLower
->texcoord
[0][0] * invW
;
828 sLeft
= s0
+ (span
.texStep
[0][0] * adjx
+ dsdy
* adjy
)
829 * (1.0F
/FIXED_SCALE
);
830 dsOuter
= dsdy
+ dxOuter
* span
.texStep
[0][0];
831 t0
= vLower
->texcoord
[0][1] * invW
;
832 tLeft
= t0
+ (span
.texStep
[0][1] * adjx
+ dtdy
* adjy
)
833 * (1.0F
/FIXED_SCALE
);
834 dtOuter
= dtdy
+ dxOuter
* span
.texStep
[0][1];
835 u0
= vLower
->texcoord
[0][2] * invW
;
836 uLeft
= u0
+ (span
.texStep
[0][2] * adjx
+ dudy
* adjy
)
837 * (1.0F
/FIXED_SCALE
);
838 duOuter
= dudy
+ dxOuter
* span
.texStep
[0][2];
839 v0
= vLower
->texcoord
[0][3] * invW
;
840 vLeft
= v0
+ (span
.texStep
[0][3] * adjx
+ dvdy
* adjy
)
841 * (1.0F
/FIXED_SCALE
);
842 dvOuter
= dvdy
+ dxOuter
* span
.texStep
[0][3];
845 #ifdef INTERP_MULTITEX
848 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
849 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
850 GLfloat invW
= vLower
->win
[3];
851 GLfloat s0
, t0
, u0
, v0
;
852 s0
= vLower
->texcoord
[u
][0] * invW
;
853 sLeft
[u
] = s0
+ (span
.texStep
[u
][0] * adjx
+ dsdy
[u
]
854 * adjy
) * (1.0F
/FIXED_SCALE
);
855 dsOuter
[u
] = dsdy
[u
] + dxOuter
* span
.texStep
[u
][0];
856 t0
= vLower
->texcoord
[u
][1] * invW
;
857 tLeft
[u
] = t0
+ (span
.texStep
[u
][1] * adjx
+ dtdy
[u
]
858 * adjy
) * (1.0F
/FIXED_SCALE
);
859 dtOuter
[u
] = dtdy
[u
] + dxOuter
* span
.texStep
[u
][1];
860 u0
= vLower
->texcoord
[u
][2] * invW
;
861 uLeft
[u
] = u0
+ (span
.texStep
[u
][2] * adjx
+ dudy
[u
]
862 * adjy
) * (1.0F
/FIXED_SCALE
);
863 duOuter
[u
] = dudy
[u
] + dxOuter
* span
.texStep
[u
][2];
864 v0
= vLower
->texcoord
[u
][3] * invW
;
865 vLeft
[u
] = v0
+ (span
.texStep
[u
][3] * adjx
+ dvdy
[u
]
866 * adjy
) * (1.0F
/FIXED_SCALE
);
867 dvOuter
[u
] = dvdy
[u
] + dxOuter
* span
.texStep
[u
][3];
876 if (setupRight
&& eRight
->lines
>0) {
877 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
878 fdxRightEdge
= eRight
->fdxdy
;
886 /* Rasterize setup */
888 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
892 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
894 fdzInner
= fdzOuter
+ span
.zStep
;
897 dfogInner
= dfogOuter
+ span
.fogStep
;
900 fdrInner
= fdrOuter
+ span
.redStep
;
901 fdgInner
= fdgOuter
+ span
.greenStep
;
902 fdbInner
= fdbOuter
+ span
.blueStep
;
905 fdaInner
= fdaOuter
+ span
.alphaStep
;
908 fdsrInner
= fdsrOuter
+ span
.specRedStep
;
909 fdsgInner
= fdsgOuter
+ span
.specGreenStep
;
910 fdsbInner
= fdsbOuter
+ span
.specBlueStep
;
913 fdiInner
= fdiOuter
+ span
.indexStep
;
915 #ifdef INTERP_INT_TEX
916 fdsInner
= fdsOuter
+ span
.intTexStep
[0];
917 fdtInner
= fdtOuter
+ span
.intTexStep
[1];
920 dsInner
= dsOuter
+ span
.texStep
[0][0];
921 dtInner
= dtOuter
+ span
.texStep
[0][1];
922 duInner
= duOuter
+ span
.texStep
[0][2];
923 dvInner
= dvOuter
+ span
.texStep
[0][3];
925 #ifdef INTERP_MULTITEX
928 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
929 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
930 dsInner
[u
] = dsOuter
[u
] + span
.texStep
[u
][0];
931 dtInner
[u
] = dtOuter
[u
] + span
.texStep
[u
][1];
932 duInner
[u
] = duOuter
[u
] + span
.texStep
[u
][2];
933 dvInner
[u
] = dvOuter
[u
] + span
.texStep
[u
][3];
940 /* initialize the span interpolants to the leftmost value */
941 /* ff = fixed-pt fragment */
942 const GLint right
= FixedToInt(fxRightEdge
);
943 span
.x
= FixedToInt(fxLeftEdge
);
947 span
.count
= right
- span
.x
;
965 span
.specGreen
= fsg
;
971 #ifdef INTERP_INT_TEX
977 span
.tex
[0][0] = sLeft
;
978 span
.tex
[0][1] = tLeft
;
979 span
.tex
[0][2] = uLeft
;
980 span
.tex
[0][3] = vLeft
;
983 #ifdef INTERP_MULTITEX
986 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
987 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
988 span
.tex
[u
][0] = sLeft
[u
];
989 span
.tex
[u
][1] = tLeft
[u
];
990 span
.tex
[u
][2] = uLeft
[u
];
991 span
.tex
[u
][3] = vLeft
[u
];
999 /* need this to accomodate round-off errors */
1000 const GLint len
= right
- span
.x
- 1;
1001 GLfixed ffrend
= span
.red
+ len
* span
.redStep
;
1002 GLfixed ffgend
= span
.green
+ len
* span
.greenStep
;
1003 GLfixed ffbend
= span
.blue
+ len
* span
.blueStep
;
1010 span
.green
-= ffgend
;
1015 span
.blue
-= ffbend
;
1023 const GLint len
= right
- span
.x
- 1;
1024 GLfixed ffaend
= span
.alpha
+ len
* span
.alphaStep
;
1026 span
.alpha
-= ffaend
;
1034 /* need this to accomodate round-off errors */
1035 const GLint len
= right
- span
.x
- 1;
1036 GLfixed ffsrend
= span
.specRed
+ len
* span
.specRedStep
;
1037 GLfixed ffsgend
= span
.specGreen
+ len
* span
.specGreenStep
;
1038 GLfixed ffsbend
= span
.specBlue
+ len
* span
.specBlueStep
;
1040 span
.specRed
-= ffsrend
;
1041 if (span
.specRed
< 0)
1045 span
.specGreen
-= ffsgend
;
1046 if (span
.specGreen
< 0)
1050 span
.specBlue
-= ffsbend
;
1051 if (span
.specBlue
< 0)
1057 if (span
.index
< 0) span
.index
= 0;
1060 /* This is where we actually generate fragments */
1061 if (span
.count
> 0) {
1062 RENDER_SPAN( span
);
1066 * Advance to the next scan line. Compute the
1067 * new edge coordinates, and adjust the
1068 * pixel-center x coordinate so that it stays
1069 * on or inside the major edge.
1074 fxLeftEdge
+= fdxLeftEdge
;
1075 fxRightEdge
+= fdxRightEdge
;
1080 fError
-= FIXED_ONE
;
1081 #ifdef PIXEL_ADDRESS
1082 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowOuter
);
1086 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowOuter
);
1091 fogLeft
+= dfogOuter
;
1109 #ifdef INTERP_INT_TEX
1119 #ifdef INTERP_MULTITEX
1122 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1123 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1124 sLeft
[u
] += dsOuter
[u
];
1125 tLeft
[u
] += dtOuter
[u
];
1126 uLeft
[u
] += duOuter
[u
];
1127 vLeft
[u
] += dvOuter
[u
];
1134 #ifdef PIXEL_ADDRESS
1135 pRow
= (PIXEL_TYPE
*) ((GLubyte
*) pRow
+ dPRowInner
);
1139 zRow
= (DEPTH_TYPE
*) ((GLubyte
*) zRow
+ dZRowInner
);
1144 fogLeft
+= dfogInner
;
1162 #ifdef INTERP_INT_TEX
1172 #ifdef INTERP_MULTITEX
1175 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1176 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1177 sLeft
[u
] += dsInner
[u
];
1178 tLeft
[u
] += dtInner
[u
];
1179 uLeft
[u
] += duInner
[u
];
1180 vLeft
[u
] += dvInner
[u
];
1188 } /* for subTriangle */
1202 #undef BYTES_PER_ROW
1203 #undef PIXEL_ADDRESS
1211 #undef INTERP_INT_TEX
1213 #undef INTERP_MULTITEX
1214 #undef INTERP_LAMBDA
1221 #undef DO_OCCLUSION_TEST