1 /* $Id: s_tritemp.h,v 1.10 2001/02/12 17:02:00 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_RGB - if defined, interpolate RGB values
37 * INTERP_SPEC - if defined, interpolate specular RGB values
38 * INTERP_ALPHA - if defined, interpolate Alpha values
39 * INTERP_INDEX - if defined, interpolate color index values
40 * INTERP_INT_TEX - if defined, interpolate integer ST texcoords
41 * (fast, simple 2-D texture mapping)
42 * INTERP_LAMBDA - if defined, the lambda value is computed at every
43 * pixel, to apply MIPMAPPING, and min/maxification
44 * INTERP_TEX - if defined, interpolate set 0 float STRQ texcoords
45 * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red)
46 * INTERP_MULTITEX - if defined, interpolate N units of STRQ texcoords
48 * When one can directly address pixels in the color buffer the following
49 * macros can be defined and used to compute pixel addresses during
50 * rasterization (see pRow):
51 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
52 * BYTES_PER_ROW - number of bytes per row in the color buffer
53 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
54 * Y==0 at bottom of screen and increases upward.
56 * Similarly, for direct depth buffer access, this type is used for depth
58 * DEPTH_TYPE - either GLushort or GLuint
60 * Optionally, one may provide one-time setup code per triangle:
61 * SETUP_CODE - code which is to be executed once per triangle
63 * The following macro MUST be defined:
64 * INNER_LOOP(LEFT,RIGHT,Y) - code to write a span of pixels.
67 * for (x=LEFT; x<RIGHT;x++) {
69 * // increment fixed point interpolants
72 * This code was designed for the origin to be in the lower-left corner.
74 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
78 /*void triangle( GLcontext *ctx, SWvertex *v0, SWvertex *v1, SWvertex *v2 )*/
81 const SWvertex
*v0
, *v1
; /* Y(v0) < Y(v1) */
82 GLfloat dx
; /* X(v1) - X(v0) */
83 GLfloat dy
; /* Y(v1) - Y(v0) */
84 GLfixed fdxdy
; /* dx/dy in fixed-point */
85 GLfixed fsx
; /* first sample point x coord */
87 GLfloat adjy
; /* adjust from v[0]->fy to fsy, scaled */
88 GLint lines
; /* number of lines to be sampled on this edge */
89 GLfixed fx0
; /* fixed pt X of lower endpoint */
93 const GLint depthBits
= ctx
->Visual
.depthBits
;
94 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
95 const GLfloat maxDepth
= ctx
->DepthMaxF
;
96 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
98 EdgeT eMaj
, eTop
, eBot
;
100 const SWvertex
*vMin
, *vMid
, *vMax
; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
101 float bf
= SWRAST_CONTEXT(ctx
)->_backface_sign
;
104 /* find the order of the 3 vertices along the Y axis */
106 GLfloat y0
= v0
->win
[1];
107 GLfloat y1
= v1
->win
[1];
108 GLfloat y2
= v2
->win
[1];
112 vMin
= v0
; vMid
= v1
; vMax
= v2
; /* y0<=y1<=y2 */
115 vMin
= v2
; vMid
= v0
; vMax
= v1
; /* y2<=y0<=y1 */
118 vMin
= v0
; vMid
= v2
; vMax
= v1
; bf
= -bf
; /* y0<=y2<=y1 */
123 vMin
= v1
; vMid
= v0
; vMax
= v2
; bf
= -bf
; /* y1<=y0<=y2 */
126 vMin
= v2
; vMid
= v1
; vMax
= v0
; bf
= -bf
; /* y2<=y1<=y0 */
129 vMin
= v1
; vMid
= v2
; vMax
= v0
; /* y1<=y2<=y0 */
134 /* vertex/edge relationship */
135 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
136 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
137 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
139 /* compute deltas for each edge: vertex[v1] - vertex[v0] */
140 eMaj
.dx
= vMax
->win
[0] - vMin
->win
[0];
141 eMaj
.dy
= vMax
->win
[1] - vMin
->win
[1];
142 eTop
.dx
= vMax
->win
[0] - vMid
->win
[0];
143 eTop
.dy
= vMax
->win
[1] - vMid
->win
[1];
144 eBot
.dx
= vMid
->win
[0] - vMin
->win
[0];
145 eBot
.dy
= vMid
->win
[1] - vMin
->win
[1];
147 /* compute oneOverArea */
149 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
151 /* Do backface culling */
158 /* check for very tiny triangle */
159 if (area
* area
< (0.05F
* 0.05F
)) { /* square to ensure positive value */
160 oneOverArea
= 1.0F
/ 0.05F
; /* a close-enough value */
164 oneOverArea
= 1.0F
/ area
;
169 #ifndef DO_OCCLUSION_TEST
170 ctx
->OcclusionResult
= GL_TRUE
;
173 /* Edge setup. For a triangle strip these could be reused... */
175 /* fixed point Y coordinates */
176 GLfixed vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
);
177 GLfixed vMin_fy
= FloatToFixed(vMin
->win
[1] - 0.5F
);
178 GLfixed vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
);
179 GLfixed vMid_fy
= FloatToFixed(vMid
->win
[1] - 0.5F
);
180 GLfixed vMax_fy
= FloatToFixed(vMax
->win
[1] - 0.5F
);
182 eMaj
.fsy
= FixedCeil(vMin_fy
);
183 eMaj
.lines
= FixedToInt(FixedCeil(vMax_fy
- eMaj
.fsy
));
184 if (eMaj
.lines
> 0) {
185 GLfloat dxdy
= eMaj
.dx
/ eMaj
.dy
;
186 eMaj
.fdxdy
= SignedFloatToFixed(dxdy
);
187 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
189 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* dxdy
);
195 eTop
.fsy
= FixedCeil(vMid_fy
);
196 eTop
.lines
= FixedToInt(FixedCeil(vMax_fy
- eTop
.fsy
));
197 if (eTop
.lines
> 0) {
198 GLfloat dxdy
= eTop
.dx
/ eTop
.dy
;
199 eTop
.fdxdy
= SignedFloatToFixed(dxdy
);
200 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
202 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* dxdy
);
205 eBot
.fsy
= FixedCeil(vMin_fy
);
206 eBot
.lines
= FixedToInt(FixedCeil(vMid_fy
- eBot
.fsy
));
207 if (eBot
.lines
> 0) {
208 GLfloat dxdy
= eBot
.dx
/ eBot
.dy
;
209 eBot
.fdxdy
= SignedFloatToFixed(dxdy
);
210 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
212 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* dxdy
);
217 * Conceptually, we view a triangle as two subtriangles
218 * separated by a perfectly horizontal line. The edge that is
219 * intersected by this line is one with maximal absolute dy; we
220 * call it a ``major'' edge. The other two edges are the
221 * ``top'' edge (for the upper subtriangle) and the ``bottom''
222 * edge (for the lower subtriangle). If either of these two
223 * edges is horizontal or very close to horizontal, the
224 * corresponding subtriangle might cover zero sample points;
225 * we take care to handle such cases, for performance as well
228 * By stepping rasterization parameters along the major edge,
229 * we can avoid recomputing them at the discontinuity where
230 * the top and bottom edges meet. However, this forces us to
231 * be able to scan both left-to-right and right-to-left.
232 * Also, we must determine whether the major edge is at the
233 * left or right side of the triangle. We do this by
234 * computing the magnitude of the cross-product of the major
235 * and top edges. Since this magnitude depends on the sine of
236 * the angle between the two edges, its sign tells us whether
237 * we turn to the left or to the right when travelling along
238 * the major edge to the top edge, and from this we infer
239 * whether the major edge is on the left or the right.
241 * Serendipitously, this cross-product magnitude is also a
242 * value we need to compute the iteration parameter
243 * derivatives for the triangle, and it can be used to perform
244 * backface culling because its sign tells us whether the
245 * triangle is clockwise or counterclockwise. In this code we
246 * refer to it as ``area'' because it's also proportional to
247 * the pixel area of the triangle.
251 GLint ltor
; /* true if scanning left-to-right */
253 GLfloat dzdx
, dzdy
; GLfixed fdzdx
;
254 GLfloat dfogdx
, dfogdy
; GLfixed fdfogdx
;
257 GLfloat drdx
, drdy
; GLfixed fdrdx
;
258 GLfloat dgdx
, dgdy
; GLfixed fdgdx
;
259 GLfloat dbdx
, dbdy
; GLfixed fdbdx
;
262 GLfloat dsrdx
, dsrdy
; GLfixed fdsrdx
;
263 GLfloat dsgdx
, dsgdy
; GLfixed fdsgdx
;
264 GLfloat dsbdx
, dsbdy
; GLfixed fdsbdx
;
267 GLfloat dadx
, dady
; GLfixed fdadx
;
270 GLfloat didx
, didy
; GLfixed fdidx
;
272 #ifdef INTERP_INT_TEX
273 GLfloat dsdx
, dsdy
; GLfixed fdsdx
;
274 GLfloat dtdx
, dtdy
; GLfixed fdtdx
;
282 #ifdef INTERP_MULTITEX
283 GLfloat dsdx
[MAX_TEXTURE_UNITS
], dsdy
[MAX_TEXTURE_UNITS
];
284 GLfloat dtdx
[MAX_TEXTURE_UNITS
], dtdy
[MAX_TEXTURE_UNITS
];
285 GLfloat dudx
[MAX_TEXTURE_UNITS
], dudy
[MAX_TEXTURE_UNITS
];
286 GLfloat dvdx
[MAX_TEXTURE_UNITS
], dvdy
[MAX_TEXTURE_UNITS
];
291 #error "Mipmapping without texturing doesn't make sense."
293 GLfloat lambda_nominator
;
294 #endif /* INTERP_LAMBDA */
298 * Execute user-supplied setup code
304 ltor
= (oneOverArea
< 0.0F
);
306 /* compute d?/dx and d?/dy derivatives */
309 GLfloat eMaj_dz
, eBot_dz
;
310 eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
311 eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
312 dzdx
= oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
313 if (dzdx
> maxDepth
|| dzdx
< -maxDepth
) {
314 /* probably a sliver triangle */
319 dzdy
= oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
322 fdzdx
= SignedFloatToFixed(dzdx
);
324 fdzdx
= (GLint
) dzdx
;
327 GLfloat eMaj_dfog
, eBot_dfog
;
328 eMaj_dfog
= (vMax
->fog
- vMin
->fog
) * 256;
329 eBot_dfog
= (vMid
->fog
- vMin
->fog
) * 256;
330 dfogdx
= oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
331 fdfogdx
= SignedFloatToFixed(dfogdx
);
332 dfogdy
= oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
337 /* This is kind of a hack to eliminate RGB color over/underflow
338 * problems when rendering very tiny triangles. We're not doing
339 * anything with alpha or specular color at this time.
341 drdx
= drdy
= 0.0; fdrdx
= 0;
342 dgdx
= dgdy
= 0.0; fdgdx
= 0;
343 dbdx
= dbdy
= 0.0; fdbdx
= 0;
346 GLfloat eMaj_dr
, eBot_dr
;
347 GLfloat eMaj_dg
, eBot_dg
;
348 GLfloat eMaj_db
, eBot_db
;
349 eMaj_dr
= (GLint
) vMax
->color
[0] - (GLint
) vMin
->color
[0];
350 eBot_dr
= (GLint
) vMid
->color
[0] - (GLint
) vMin
->color
[0];
351 drdx
= oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
352 fdrdx
= SignedFloatToFixed(drdx
);
353 drdy
= oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
354 eMaj_dg
= (GLint
) vMax
->color
[1] - (GLint
) vMin
->color
[1];
355 eBot_dg
= (GLint
) vMid
->color
[1] - (GLint
) vMin
->color
[1];
356 dgdx
= oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
357 fdgdx
= SignedFloatToFixed(dgdx
);
358 dgdy
= oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
359 eMaj_db
= (GLint
) vMax
->color
[2] - (GLint
) vMin
->color
[2];
360 eBot_db
= (GLint
) vMid
->color
[2] - (GLint
) vMin
->color
[2];
361 dbdx
= oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
362 fdbdx
= SignedFloatToFixed(dbdx
);
363 dbdy
= oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
368 GLfloat eMaj_dsr
, eBot_dsr
;
369 eMaj_dsr
= (GLint
) vMax
->specular
[0] - (GLint
) vMin
->specular
[0];
370 eBot_dsr
= (GLint
) vMid
->specular
[0] - (GLint
) vMin
->specular
[0];
371 dsrdx
= oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
372 fdsrdx
= SignedFloatToFixed(dsrdx
);
373 dsrdy
= oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
376 GLfloat eMaj_dsg
, eBot_dsg
;
377 eMaj_dsg
= (GLint
) vMax
->specular
[1] - (GLint
) vMin
->specular
[1];
378 eBot_dsg
= (GLint
) vMid
->specular
[1] - (GLint
) vMin
->specular
[1];
379 dsgdx
= oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
380 fdsgdx
= SignedFloatToFixed(dsgdx
);
381 dsgdy
= oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
384 GLfloat eMaj_dsb
, eBot_dsb
;
385 eMaj_dsb
= (GLint
) vMax
->specular
[2] - (GLint
) vMin
->specular
[2];
386 eBot_dsb
= (GLint
) vMid
->specular
[2] - (GLint
) vMin
->specular
[2];
387 dsbdx
= oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
388 fdsbdx
= SignedFloatToFixed(dsbdx
);
389 dsbdy
= oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
394 GLfloat eMaj_da
, eBot_da
;
395 eMaj_da
= (GLint
) vMax
->color
[3] - (GLint
) vMin
->color
[3];
396 eBot_da
= (GLint
) vMid
->color
[3] - (GLint
) vMin
->color
[3];
397 dadx
= oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
398 fdadx
= SignedFloatToFixed(dadx
);
399 dady
= oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
404 GLfloat eMaj_di
, eBot_di
;
405 eMaj_di
= (GLint
) vMax
->index
- (GLint
) vMin
->index
;
406 eBot_di
= (GLint
) vMid
->index
- (GLint
) vMin
->index
;
407 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
408 fdidx
= SignedFloatToFixed(didx
);
409 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
412 #ifdef INTERP_INT_TEX
414 GLfloat eMaj_ds
, eBot_ds
;
415 eMaj_ds
= (vMax
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
416 eBot_ds
= (vMid
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
417 dsdx
= oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
418 fdsdx
= SignedFloatToFixed(dsdx
);
419 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
422 GLfloat eMaj_dt
, eBot_dt
;
423 eMaj_dt
= (vMax
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
424 eBot_dt
= (vMid
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
425 dtdx
= oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
426 fdtdx
= SignedFloatToFixed(dtdx
);
427 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
433 GLfloat wMax
= vMax
->win
[3];
434 GLfloat wMin
= vMin
->win
[3];
435 GLfloat wMid
= vMid
->win
[3];
436 GLfloat eMaj_ds
, eBot_ds
;
437 GLfloat eMaj_dt
, eBot_dt
;
438 GLfloat eMaj_du
, eBot_du
;
439 GLfloat eMaj_dv
, eBot_dv
;
441 eMaj_ds
= vMax
->texcoord
[0][0] * wMax
- vMin
->texcoord
[0][0] * wMin
;
442 eBot_ds
= vMid
->texcoord
[0][0] * wMid
- vMin
->texcoord
[0][0] * wMin
;
443 dsdx
= oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
444 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
446 eMaj_dt
= vMax
->texcoord
[0][1] * wMax
- vMin
->texcoord
[0][1] * wMin
;
447 eBot_dt
= vMid
->texcoord
[0][1] * wMid
- vMin
->texcoord
[0][1] * wMin
;
448 dtdx
= oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
449 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
451 eMaj_du
= vMax
->texcoord
[0][2] * wMax
- vMin
->texcoord
[0][2] * wMin
;
452 eBot_du
= vMid
->texcoord
[0][2] * wMid
- vMin
->texcoord
[0][2] * wMin
;
453 dudx
= oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
454 dudy
= oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
456 eMaj_dv
= vMax
->texcoord
[0][3] * wMax
- vMin
->texcoord
[0][3] * wMin
;
457 eBot_dv
= vMid
->texcoord
[0][3] * wMid
- vMin
->texcoord
[0][3] * wMin
;
458 dvdx
= oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
459 dvdy
= oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
462 #ifdef INTERP_MULTITEX
464 GLfloat wMax
= vMax
->win
[3];
465 GLfloat wMin
= vMin
->win
[3];
466 GLfloat wMid
= vMid
->win
[3];
468 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
469 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
470 GLfloat eMaj_ds
, eBot_ds
;
471 GLfloat eMaj_dt
, eBot_dt
;
472 GLfloat eMaj_du
, eBot_du
;
473 GLfloat eMaj_dv
, eBot_dv
;
474 eMaj_ds
= vMax
->texcoord
[u
][0] * wMax
475 - vMin
->texcoord
[u
][0] * wMin
;
476 eBot_ds
= vMid
->texcoord
[u
][0] * wMid
477 - vMin
->texcoord
[u
][0] * wMin
;
478 dsdx
[u
] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
479 dsdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
481 eMaj_dt
= vMax
->texcoord
[u
][1] * wMax
482 - vMin
->texcoord
[u
][1] * wMin
;
483 eBot_dt
= vMid
->texcoord
[u
][1] * wMid
484 - vMin
->texcoord
[u
][1] * wMin
;
485 dtdx
[u
] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
486 dtdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
488 eMaj_du
= vMax
->texcoord
[u
][2] * wMax
489 - vMin
->texcoord
[u
][2] * wMin
;
490 eBot_du
= vMid
->texcoord
[u
][2] * wMid
491 - vMin
->texcoord
[u
][2] * wMin
;
492 dudx
[u
] = oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
493 dudy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
495 eMaj_dv
= vMax
->texcoord
[u
][3] * wMax
496 - vMin
->texcoord
[u
][3] * wMin
;
497 eBot_dv
= vMid
->texcoord
[u
][3] * wMid
498 - vMin
->texcoord
[u
][3] * wMin
;
499 dvdx
[u
] = oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
500 dvdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
507 * We always sample at pixel centers. However, we avoid
508 * explicit half-pixel offsets in this code by incorporating
509 * the proper offset in each of x and y during the
510 * transformation to window coordinates.
512 * We also apply the usual rasterization rules to prevent
513 * cracks and overlaps. A pixel is considered inside a
514 * subtriangle if it meets all of four conditions: it is on or
515 * to the right of the left edge, strictly to the left of the
516 * right edge, on or below the top edge, and strictly above
517 * the bottom edge. (Some edges may be degenerate.)
519 * The following discussion assumes left-to-right scanning
520 * (that is, the major edge is on the left); the right-to-left
521 * case is a straightforward variation.
523 * We start by finding the half-integral y coordinate that is
524 * at or below the top of the triangle. This gives us the
525 * first scan line that could possibly contain pixels that are
526 * inside the triangle.
528 * Next we creep down the major edge until we reach that y,
529 * and compute the corresponding x coordinate on the edge.
530 * Then we find the half-integral x that lies on or just
531 * inside the edge. This is the first pixel that might lie in
532 * the interior of the triangle. (We won't know for sure
533 * until we check the other edges.)
535 * As we rasterize the triangle, we'll step down the major
536 * edge. For each step in y, we'll move an integer number
537 * of steps in x. There are two possible x step sizes, which
538 * we'll call the ``inner'' step (guaranteed to land on the
539 * edge or inside it) and the ``outer'' step (guaranteed to
540 * land on the edge or outside it). The inner and outer steps
541 * differ by one. During rasterization we maintain an error
542 * term that indicates our distance from the true edge, and
543 * select either the inner step or the outer step, whichever
544 * gets us to the first pixel that falls inside the triangle.
546 * All parameters (z, red, etc.) as well as the buffer
547 * addresses for color and z have inner and outer step values,
548 * so that we can increment them appropriately. This method
549 * eliminates the need to adjust parameters by creeping a
550 * sub-pixel amount into the triangle at each scanline.
555 GLfixed fx
, fxLeftEdge
, fxRightEdge
, fdxLeftEdge
, fdxRightEdge
;
559 GLfixed fError
, fdError
;
565 int dPRowOuter
, dPRowInner
; /* offset in bytes */
570 int dZRowOuter
, dZRowInner
; /* offset in bytes */
572 GLfixed fz
, fdzOuter
, fdzInner
;
573 GLfixed ffog
, fdfogOuter
, fdfogInner
;
576 GLfixed fr
, fdrOuter
, fdrInner
;
577 GLfixed fg
, fdgOuter
, fdgInner
;
578 GLfixed fb
, fdbOuter
, fdbInner
;
581 GLfixed fsr
, fdsrOuter
, fdsrInner
;
582 GLfixed fsg
, fdsgOuter
, fdsgInner
;
583 GLfixed fsb
, fdsbOuter
, fdsbInner
;
586 GLfixed fa
, fdaOuter
, fdaInner
;
589 GLfixed fi
, fdiOuter
, fdiInner
;
591 #ifdef INTERP_INT_TEX
592 GLfixed fs
, fdsOuter
, fdsInner
;
593 GLfixed ft
, fdtOuter
, fdtInner
;
596 GLfloat sLeft
, dsOuter
, dsInner
;
597 GLfloat tLeft
, dtOuter
, dtInner
;
598 GLfloat uLeft
, duOuter
, duInner
;
599 GLfloat vLeft
, dvOuter
, dvInner
;
601 #ifdef INTERP_MULTITEX
602 GLfloat sLeft
[MAX_TEXTURE_UNITS
];
603 GLfloat tLeft
[MAX_TEXTURE_UNITS
];
604 GLfloat uLeft
[MAX_TEXTURE_UNITS
];
605 GLfloat vLeft
[MAX_TEXTURE_UNITS
];
606 GLfloat dsOuter
[MAX_TEXTURE_UNITS
], dsInner
[MAX_TEXTURE_UNITS
];
607 GLfloat dtOuter
[MAX_TEXTURE_UNITS
], dtInner
[MAX_TEXTURE_UNITS
];
608 GLfloat duOuter
[MAX_TEXTURE_UNITS
], duInner
[MAX_TEXTURE_UNITS
];
609 GLfloat dvOuter
[MAX_TEXTURE_UNITS
], dvInner
[MAX_TEXTURE_UNITS
];
612 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
613 EdgeT
*eLeft
, *eRight
;
614 int setupLeft
, setupRight
;
617 if (subTriangle
==0) {
622 lines
= eRight
->lines
;
629 lines
= eLeft
->lines
;
639 lines
= eRight
->lines
;
646 lines
= eLeft
->lines
;
654 if (setupLeft
&& eLeft
->lines
> 0) {
655 const SWvertex
*vLower
;
656 GLfixed fsx
= eLeft
->fsx
;
658 fError
= fx
- fsx
- FIXED_ONE
;
659 fxLeftEdge
= fsx
- FIXED_EPSILON
;
660 fdxLeftEdge
= eLeft
->fdxdy
;
661 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
662 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
663 idxOuter
= FixedToInt(fdxOuter
);
664 dxOuter
= (float) idxOuter
;
670 adjx
= (float)(fx
- eLeft
->fx0
); /* SCALED! */
671 adjy
= eLeft
->adjy
; /* SCALED! */
672 (void) adjx
; /* silence compiler warnings */
673 (void) adjy
; /* silence compiler warnings */
676 (void) vLower
; /* silence compiler warnings */
680 pRow
= PIXEL_ADDRESS( FixedToInt(fxLeftEdge
), iy
);
681 dPRowOuter
= -((int)BYTES_PER_ROW
) + idxOuter
* sizeof(PIXEL_TYPE
);
682 /* negative because Y=0 at bottom and increases upward */
686 * Now we need the set of parameter (z, color, etc.) values at
687 * the point (fx, fy). This gives us properly-sampled parameter
688 * values that we can step from pixel to pixel. Furthermore,
689 * although we might have intermediate results that overflow
690 * the normal parameter range when we step temporarily outside
691 * the triangle, we shouldn't overflow or underflow for any
692 * pixel that's actually inside the triangle.
697 GLfloat z0
= vLower
->win
[2];
698 if (depthBits
<= 16) {
699 /* interpolate fixed-pt values */
700 GLfloat tmp
= (z0
* FIXED_SCALE
+
701 dzdx
* adjx
+ dzdy
* adjy
) + FIXED_HALF
;
702 if (tmp
< MAX_GLUINT
/ 2)
706 fdzOuter
= SignedFloatToFixed(dzdy
+ dxOuter
* dzdx
);
709 /* interpolate depth values exactly */
710 fz
= (GLint
) (z0
+ dzdx
*FixedToFloat(adjx
) + dzdy
*FixedToFloat(adjy
));
711 fdzOuter
= (GLint
) (dzdy
+ dxOuter
* dzdx
);
714 zRow
= (DEPTH_TYPE
*) _mesa_zbuffer_address(ctx
, FixedToInt(fxLeftEdge
), iy
);
715 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
719 ffog
= FloatToFixed(vLower
->fog
) * 256 + dfogdx
* adjx
+ dfogdy
* adjy
+ FIXED_HALF
;
720 fdfogOuter
= SignedFloatToFixed(dfogdy
+ dxOuter
* dfogdx
);
724 fr
= (GLfixed
)(IntToFixed(vLower
->color
[0])
725 + drdx
* adjx
+ drdy
* adjy
) + FIXED_HALF
;
726 fdrOuter
= SignedFloatToFixed(drdy
+ dxOuter
* drdx
);
728 fg
= (GLfixed
)(IntToFixed(vLower
->color
[1])
729 + dgdx
* adjx
+ dgdy
* adjy
) + FIXED_HALF
;
730 fdgOuter
= SignedFloatToFixed(dgdy
+ dxOuter
* dgdx
);
732 fb
= (GLfixed
)(IntToFixed(vLower
->color
[2])
733 + dbdx
* adjx
+ dbdy
* adjy
) + FIXED_HALF
;
734 fdbOuter
= SignedFloatToFixed(dbdy
+ dxOuter
* dbdx
);
737 fsr
= (GLfixed
)(IntToFixed(vLower
->specular
[0])
738 + dsrdx
* adjx
+ dsrdy
* adjy
) + FIXED_HALF
;
739 fdsrOuter
= SignedFloatToFixed(dsrdy
+ dxOuter
* dsrdx
);
741 fsg
= (GLfixed
)(IntToFixed(vLower
->specular
[1])
742 + dsgdx
* adjx
+ dsgdy
* adjy
) + FIXED_HALF
;
743 fdsgOuter
= SignedFloatToFixed(dsgdy
+ dxOuter
* dsgdx
);
745 fsb
= (GLfixed
)(IntToFixed(vLower
->specular
[2])
746 + dsbdx
* adjx
+ dsbdy
* adjy
) + FIXED_HALF
;
747 fdsbOuter
= SignedFloatToFixed(dsbdy
+ dxOuter
* dsbdx
);
750 fa
= (GLfixed
)(IntToFixed(vLower
->color
[3])
751 + dadx
* adjx
+ dady
* adjy
) + FIXED_HALF
;
752 fdaOuter
= SignedFloatToFixed(dady
+ dxOuter
* dadx
);
755 fi
= (GLfixed
)(vLower
->index
* FIXED_SCALE
756 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
757 fdiOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
759 #ifdef INTERP_INT_TEX
762 s0
= vLower
->texcoord
[0][0] * S_SCALE
;
763 fs
= (GLfixed
)(s0
* FIXED_SCALE
+ dsdx
* adjx
+ dsdy
* adjy
) + FIXED_HALF
;
764 fdsOuter
= SignedFloatToFixed(dsdy
+ dxOuter
* dsdx
);
766 t0
= vLower
->texcoord
[0][1] * T_SCALE
;
767 ft
= (GLfixed
)(t0
* FIXED_SCALE
+ dtdx
* adjx
+ dtdy
* adjy
) + FIXED_HALF
;
768 fdtOuter
= SignedFloatToFixed(dtdy
+ dxOuter
* dtdx
);
773 GLfloat invW
= vLower
->win
[3];
774 GLfloat s0
, t0
, u0
, v0
;
775 s0
= vLower
->texcoord
[0][0] * invW
;
776 sLeft
= s0
+ (dsdx
* adjx
+ dsdy
* adjy
) * (1.0F
/FIXED_SCALE
);
777 dsOuter
= dsdy
+ dxOuter
* dsdx
;
778 t0
= vLower
->texcoord
[0][1] * invW
;
779 tLeft
= t0
+ (dtdx
* adjx
+ dtdy
* adjy
) * (1.0F
/FIXED_SCALE
);
780 dtOuter
= dtdy
+ dxOuter
* dtdx
;
781 u0
= vLower
->texcoord
[0][2] * invW
;
782 uLeft
= u0
+ (dudx
* adjx
+ dudy
* adjy
) * (1.0F
/FIXED_SCALE
);
783 duOuter
= dudy
+ dxOuter
* dudx
;
784 v0
= vLower
->texcoord
[0][3] * invW
;
785 vLeft
= v0
+ (dvdx
* adjx
+ dvdy
* adjy
) * (1.0F
/FIXED_SCALE
);
786 dvOuter
= dvdy
+ dxOuter
* dvdx
;
789 #ifdef INTERP_MULTITEX
792 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
793 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
794 GLfloat invW
= vLower
->win
[3];
795 GLfloat s0
, t0
, u0
, v0
;
796 s0
= vLower
->texcoord
[u
][0] * invW
;
797 sLeft
[u
] = s0
+ (dsdx
[u
] * adjx
+ dsdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
798 dsOuter
[u
] = dsdy
[u
] + dxOuter
* dsdx
[u
];
799 t0
= vLower
->texcoord
[u
][1] * invW
;
800 tLeft
[u
] = t0
+ (dtdx
[u
] * adjx
+ dtdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
801 dtOuter
[u
] = dtdy
[u
] + dxOuter
* dtdx
[u
];
802 u0
= vLower
->texcoord
[u
][2] * invW
;
803 uLeft
[u
] = u0
+ (dudx
[u
] * adjx
+ dudy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
804 duOuter
[u
] = dudy
[u
] + dxOuter
* dudx
[u
];
805 v0
= vLower
->texcoord
[u
][3] * invW
;
806 vLeft
[u
] = v0
+ (dvdx
[u
] * adjx
+ dvdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
807 dvOuter
[u
] = dvdy
[u
] + dxOuter
* dvdx
[u
];
816 if (setupRight
&& eRight
->lines
>0) {
817 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
818 fdxRightEdge
= eRight
->fdxdy
;
826 /* Rasterize setup */
828 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
832 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
834 fdzInner
= fdzOuter
+ fdzdx
;
835 fdfogInner
= fdfogOuter
+ fdfogdx
;
838 fdrInner
= fdrOuter
+ fdrdx
;
839 fdgInner
= fdgOuter
+ fdgdx
;
840 fdbInner
= fdbOuter
+ fdbdx
;
843 fdsrInner
= fdsrOuter
+ fdsrdx
;
844 fdsgInner
= fdsgOuter
+ fdsgdx
;
845 fdsbInner
= fdsbOuter
+ fdsbdx
;
848 fdaInner
= fdaOuter
+ fdadx
;
851 fdiInner
= fdiOuter
+ fdidx
;
853 #ifdef INTERP_INT_TEX
854 fdsInner
= fdsOuter
+ fdsdx
;
855 fdtInner
= fdtOuter
+ fdtdx
;
858 dsInner
= dsOuter
+ dsdx
;
859 dtInner
= dtOuter
+ dtdx
;
860 duInner
= duOuter
+ dudx
;
861 dvInner
= dvOuter
+ dvdx
;
863 #ifdef INTERP_MULTITEX
866 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
867 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
868 dsInner
[u
] = dsOuter
[u
] + dsdx
[u
];
869 dtInner
[u
] = dtOuter
[u
] + dtdx
[u
];
870 duInner
[u
] = duOuter
[u
] + dudx
[u
];
871 dvInner
[u
] = dvOuter
[u
] + dvdx
[u
];
878 /* initialize the span interpolants to the leftmost value */
879 /* ff = fixed-pt fragment */
880 GLint left
= FixedToInt(fxLeftEdge
);
881 GLint right
= FixedToInt(fxRightEdge
);
884 GLfixed fffog
= ffog
;
887 GLfixed ffr
= fr
, ffg
= fg
, ffb
= fb
;
890 GLfixed ffsr
= fsr
, ffsg
= fsg
, ffsb
= fsb
;
898 #ifdef INTERP_INT_TEX
899 GLfixed ffs
= fs
, fft
= ft
;
902 GLfloat ss
= sLeft
, tt
= tLeft
, uu
= uLeft
, vv
= vLeft
;
904 #ifdef INTERP_MULTITEX
905 GLfloat ss
[MAX_TEXTURE_UNITS
];
906 GLfloat tt
[MAX_TEXTURE_UNITS
];
907 GLfloat uu
[MAX_TEXTURE_UNITS
];
908 GLfloat vv
[MAX_TEXTURE_UNITS
];
911 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
912 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
924 /* need this to accomodate round-off errors */
925 GLfixed ffrend
= ffr
+(right
-left
-1)*fdrdx
;
926 GLfixed ffgend
= ffg
+(right
-left
-1)*fdgdx
;
927 GLfixed ffbend
= ffb
+(right
-left
-1)*fdbdx
;
928 if (ffrend
<0) ffr
-= ffrend
;
929 if (ffgend
<0) ffg
-= ffgend
;
930 if (ffbend
<0) ffb
-= ffbend
;
938 /* need this to accomodate round-off errors */
939 GLfixed ffsrend
= ffsr
+(right
-left
-1)*fdsrdx
;
940 GLfixed ffsgend
= ffsg
+(right
-left
-1)*fdsgdx
;
941 GLfixed ffsbend
= ffsb
+(right
-left
-1)*fdsbdx
;
942 if (ffsrend
<0) ffsr
-= ffsrend
;
943 if (ffsgend
<0) ffsg
-= ffsgend
;
944 if (ffsbend
<0) ffsb
-= ffsbend
;
945 if (ffsr
<0) ffsr
= 0;
946 if (ffsg
<0) ffsg
= 0;
947 if (ffsb
<0) ffsb
= 0;
952 GLfixed ffaend
= ffa
+(right
-left
-1)*fdadx
;
953 if (ffaend
<0) ffa
-= ffaend
;
963 * The lambda value is:
964 * log_2(sqrt(f(n))) = 1/2*log_2(f(n)), where f(n) is a function
966 * f(n):= dudx * dudx + dudy * dudy + dvdx * dvdx + dvdy * dvdy;
967 * and each of this terms is resp.
968 * dudx = dsdx * invQ(n) * tex_width;
969 * dudy = dsdy * invQ(n) * tex_width;
970 * dvdx = dtdx * invQ(n) * tex_height;
971 * dvdy = dtdy * invQ(n) * tex_height;
972 * Therefore the function lambda can be represented (by factoring out) as:
973 * f(n) = lambda_nominator * invQ(n) * invQ(n),
974 * which saves some computation time.
977 GLfloat dudx
= dsdx
/* * invQ*/ * twidth
;
978 GLfloat dudy
= dsdy
/* * invQ*/ * twidth
;
979 GLfloat dvdx
= dtdx
/* * invQ*/ * theight
;
980 GLfloat dvdy
= dtdy
/* * invQ*/ * theight
;
981 GLfloat r1
= dudx
* dudx
+ dudy
* dudy
;
982 GLfloat r2
= dvdx
* dvdx
+ dvdy
* dvdy
;
983 GLfloat rho2
= r1
+ r2
; /* used to be: rho2 = MAX2(r1,r2); */
984 lambda_nominator
= rho2
;
987 /* return log base 2 of sqrt(rho) */
988 #define COMPUTE_LAMBDA(X) log( lambda_nominator * (X)*(X) ) * 1.442695F * 0.5F /* 1.442695 = 1/log(2) */
991 INNER_LOOP( left
, right
, iy
);
994 * Advance to the next scan line. Compute the
995 * new edge coordinates, and adjust the
996 * pixel-center x coordinate so that it stays
997 * on or inside the major edge.
1002 fxLeftEdge
+= fdxLeftEdge
;
1003 fxRightEdge
+= fdxRightEdge
;
1008 fError
-= FIXED_ONE
;
1009 #ifdef PIXEL_ADDRESS
1010 pRow
= (PIXEL_TYPE
*) ((GLubyte
*)pRow
+ dPRowOuter
);
1014 zRow
= (DEPTH_TYPE
*) ((GLubyte
*)zRow
+ dZRowOuter
);
1020 fr
+= fdrOuter
; fg
+= fdgOuter
; fb
+= fdbOuter
;
1023 fsr
+= fdsrOuter
; fsg
+= fdsgOuter
; fsb
+= fdsbOuter
;
1031 #ifdef INTERP_INT_TEX
1032 fs
+= fdsOuter
; ft
+= fdtOuter
;
1040 #ifdef INTERP_MULTITEX
1043 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1044 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1045 sLeft
[u
] += dsOuter
[u
];
1046 tLeft
[u
] += dtOuter
[u
];
1047 uLeft
[u
] += duOuter
[u
];
1048 vLeft
[u
] += dvOuter
[u
];
1055 #ifdef PIXEL_ADDRESS
1056 pRow
= (PIXEL_TYPE
*) ((GLubyte
*)pRow
+ dPRowInner
);
1060 zRow
= (DEPTH_TYPE
*) ((GLubyte
*)zRow
+ dZRowInner
);
1066 fr
+= fdrInner
; fg
+= fdgInner
; fb
+= fdbInner
;
1069 fsr
+= fdsrInner
; fsg
+= fdsgInner
; fsb
+= fdsbInner
;
1077 #ifdef INTERP_INT_TEX
1078 fs
+= fdsInner
; ft
+= fdtInner
;
1086 #ifdef INTERP_MULTITEX
1089 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1090 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1091 sLeft
[u
] += dsInner
[u
];
1092 tLeft
[u
] += dtInner
[u
];
1093 uLeft
[u
] += duInner
[u
];
1094 vLeft
[u
] += dvInner
[u
];
1102 } /* for subTriangle */
1112 #undef BYTES_PER_ROW
1113 #undef PIXEL_ADDRESS
1120 #undef INTERP_LAMBDA
1121 #undef COMPUTE_LAMBDA
1122 #undef INTERP_INT_TEX
1124 #undef INTERP_MULTITEX
1131 #undef DO_OCCLUSION_TEST