1 /* $Id: s_tritemp.h,v 1.3 2000/11/13 20:02:57 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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_TEX - if defined, interpolate set 0 float STRQ texcoords
43 * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red)
44 * INTERP_MULTITEX - if defined, interpolate N units of STRQ texcoords
46 * When one can directly address pixels in the color buffer the following
47 * macros can be defined and used to compute pixel addresses during
48 * rasterization (see pRow):
49 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
50 * BYTES_PER_ROW - number of bytes per row in the color buffer
51 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
52 * Y==0 at bottom of screen and increases upward.
54 * Similarly, for direct depth buffer access, this type is used for depth
56 * DEPTH_TYPE - either GLushort or GLuint
58 * Optionally, one may provide one-time setup code per triangle:
59 * SETUP_CODE - code which is to be executed once per triangle
61 * The following macro MUST be defined:
62 * INNER_LOOP(LEFT,RIGHT,Y) - code to write a span of pixels.
65 * for (x=LEFT; x<RIGHT;x++) {
67 * // increment fixed point interpolants
70 * This code was designed for the origin to be in the lower-left corner.
72 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
76 /*void triangle( GLcontext *ctx, SWvertex *v0, SWvertex *v1, SWvertex *v2 )*/
79 SWvertex
*v0
, *v1
; /* Y(v0) < Y(v1) */
80 GLfloat dx
; /* X(v1) - X(v0) */
81 GLfloat dy
; /* Y(v1) - Y(v0) */
82 GLfixed fdxdy
; /* dx/dy in fixed-point */
83 GLfixed fsx
; /* first sample point x coord */
85 GLfloat adjy
; /* adjust from v[0]->fy to fsy, scaled */
86 GLint lines
; /* number of lines to be sampled on this edge */
87 GLfixed fx0
; /* fixed pt X of lower endpoint */
91 const GLint depthBits
= ctx
->Visual
.DepthBits
;
92 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
93 const GLfloat maxDepth
= ctx
->Visual
.DepthMaxF
;
94 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
96 EdgeT eMaj
, eTop
, eBot
;
98 SWvertex
*vMin
, *vMid
, *vMax
; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
99 float bf
= SWRAST_CONTEXT(ctx
)->_backface_sign
;
101 /* find the order of the 3 vertices along the Y axis */
103 GLfloat y0
= v0
->win
[1];
104 GLfloat y1
= v1
->win
[1];
105 GLfloat y2
= v2
->win
[1];
109 vMin
= v0
; vMid
= v1
; vMax
= v2
; /* y0<=y1<=y2 */
112 vMin
= v2
; vMid
= v0
; vMax
= v1
; /* y2<=y0<=y1 */
115 vMin
= v0
; vMid
= v2
; vMax
= v1
; bf
= -bf
; /* y0<=y2<=y1 */
120 vMin
= v1
; vMid
= v0
; vMax
= v2
; bf
= -bf
; /* y1<=y0<=y2 */
123 vMin
= v2
; vMid
= v1
; vMax
= v0
; bf
= -bf
; /* y2<=y1<=y0 */
126 vMin
= v1
; vMid
= v2
; vMax
= v0
; /* y1<=y2<=y0 */
131 /* vertex/edge relationship */
132 eMaj
.v0
= vMin
; eMaj
.v1
= vMax
; /*TODO: .v1's not needed */
133 eTop
.v0
= vMid
; eTop
.v1
= vMax
;
134 eBot
.v0
= vMin
; eBot
.v1
= vMid
;
136 /* compute deltas for each edge: vertex[v1] - vertex[v0] */
137 eMaj
.dx
= vMax
->win
[0] - vMin
->win
[0];
138 eMaj
.dy
= vMax
->win
[1] - vMin
->win
[1];
139 eTop
.dx
= vMax
->win
[0] - vMid
->win
[0];
140 eTop
.dy
= vMax
->win
[1] - vMid
->win
[1];
141 eBot
.dx
= vMid
->win
[0] - vMin
->win
[0];
142 eBot
.dy
= vMid
->win
[1] - vMin
->win
[1];
144 /* compute oneOverArea */
146 const GLfloat area
= eMaj
.dx
* eBot
.dy
- eBot
.dx
* eMaj
.dy
;
148 /* Do backface culling */
155 /* check for very tiny triangle */
156 if (area
* area
< 0.0025F
) /* square it to ensure positive value */
157 oneOverArea
= 1.0F
/ 0.0025F
; /* a close-enough value */
159 oneOverArea
= 1.0F
/ area
;
162 #ifndef DO_OCCLUSION_TEST
163 ctx
->OcclusionResult
= GL_TRUE
;
166 /* Edge setup. For a triangle strip these could be reused... */
168 /* fixed point Y coordinates */
169 GLfixed vMin_fx
= FloatToFixed(vMin
->win
[0] + 0.5F
);
170 GLfixed vMin_fy
= FloatToFixed(vMin
->win
[1] - 0.5F
);
171 GLfixed vMid_fx
= FloatToFixed(vMid
->win
[0] + 0.5F
);
172 GLfixed vMid_fy
= FloatToFixed(vMid
->win
[1] - 0.5F
);
173 GLfixed vMax_fy
= FloatToFixed(vMax
->win
[1] - 0.5F
);
175 eMaj
.fsy
= FixedCeil(vMin_fy
);
176 eMaj
.lines
= FixedToInt(vMax_fy
+ FIXED_ONE
- FIXED_EPSILON
- eMaj
.fsy
);
177 if (eMaj
.lines
> 0) {
178 GLfloat dxdy
= eMaj
.dx
/ eMaj
.dy
;
179 eMaj
.fdxdy
= SignedFloatToFixed(dxdy
);
180 eMaj
.adjy
= (GLfloat
) (eMaj
.fsy
- vMin_fy
); /* SCALED! */
182 eMaj
.fsx
= eMaj
.fx0
+ (GLfixed
) (eMaj
.adjy
* dxdy
);
188 eTop
.fsy
= FixedCeil(vMid_fy
);
189 eTop
.lines
= FixedToInt(vMax_fy
+ FIXED_ONE
- FIXED_EPSILON
- eTop
.fsy
);
190 if (eTop
.lines
> 0) {
191 GLfloat dxdy
= eTop
.dx
/ eTop
.dy
;
192 eTop
.fdxdy
= SignedFloatToFixed(dxdy
);
193 eTop
.adjy
= (GLfloat
) (eTop
.fsy
- vMid_fy
); /* SCALED! */
195 eTop
.fsx
= eTop
.fx0
+ (GLfixed
) (eTop
.adjy
* dxdy
);
198 eBot
.fsy
= FixedCeil(vMin_fy
);
199 eBot
.lines
= FixedToInt(vMid_fy
+ FIXED_ONE
- FIXED_EPSILON
- eBot
.fsy
);
200 if (eBot
.lines
> 0) {
201 GLfloat dxdy
= eBot
.dx
/ eBot
.dy
;
202 eBot
.fdxdy
= SignedFloatToFixed(dxdy
);
203 eBot
.adjy
= (GLfloat
) (eBot
.fsy
- vMin_fy
); /* SCALED! */
205 eBot
.fsx
= eBot
.fx0
+ (GLfixed
) (eBot
.adjy
* dxdy
);
210 * Conceptually, we view a triangle as two subtriangles
211 * separated by a perfectly horizontal line. The edge that is
212 * intersected by this line is one with maximal absolute dy; we
213 * call it a ``major'' edge. The other two edges are the
214 * ``top'' edge (for the upper subtriangle) and the ``bottom''
215 * edge (for the lower subtriangle). If either of these two
216 * edges is horizontal or very close to horizontal, the
217 * corresponding subtriangle might cover zero sample points;
218 * we take care to handle such cases, for performance as well
221 * By stepping rasterization parameters along the major edge,
222 * we can avoid recomputing them at the discontinuity where
223 * the top and bottom edges meet. However, this forces us to
224 * be able to scan both left-to-right and right-to-left.
225 * Also, we must determine whether the major edge is at the
226 * left or right side of the triangle. We do this by
227 * computing the magnitude of the cross-product of the major
228 * and top edges. Since this magnitude depends on the sine of
229 * the angle between the two edges, its sign tells us whether
230 * we turn to the left or to the right when travelling along
231 * the major edge to the top edge, and from this we infer
232 * whether the major edge is on the left or the right.
234 * Serendipitously, this cross-product magnitude is also a
235 * value we need to compute the iteration parameter
236 * derivatives for the triangle, and it can be used to perform
237 * backface culling because its sign tells us whether the
238 * triangle is clockwise or counterclockwise. In this code we
239 * refer to it as ``area'' because it's also proportional to
240 * the pixel area of the triangle.
244 GLint ltor
; /* true if scanning left-to-right */
246 GLfloat dzdx
, dzdy
; GLfixed fdzdx
;
247 GLfloat dfogdx
, dfogdy
; GLfixed fdfogdx
;
250 GLfloat drdx
, drdy
; GLfixed fdrdx
;
251 GLfloat dgdx
, dgdy
; GLfixed fdgdx
;
252 GLfloat dbdx
, dbdy
; GLfixed fdbdx
;
255 GLfloat dsrdx
, dsrdy
; GLfixed fdsrdx
;
256 GLfloat dsgdx
, dsgdy
; GLfixed fdsgdx
;
257 GLfloat dsbdx
, dsbdy
; GLfixed fdsbdx
;
260 GLfloat dadx
, dady
; GLfixed fdadx
;
263 GLfloat didx
, didy
; GLfixed fdidx
;
265 #ifdef INTERP_INT_TEX
266 GLfloat dsdx
, dsdy
; GLfixed fdsdx
;
267 GLfloat dtdx
, dtdy
; GLfixed fdtdx
;
275 #ifdef INTERP_MULTITEX
276 GLfloat dsdx
[MAX_TEXTURE_UNITS
], dsdy
[MAX_TEXTURE_UNITS
];
277 GLfloat dtdx
[MAX_TEXTURE_UNITS
], dtdy
[MAX_TEXTURE_UNITS
];
278 GLfloat dudx
[MAX_TEXTURE_UNITS
], dudy
[MAX_TEXTURE_UNITS
];
279 GLfloat dvdx
[MAX_TEXTURE_UNITS
], dvdy
[MAX_TEXTURE_UNITS
];
283 * Execute user-supplied setup code
289 ltor
= (oneOverArea
< 0.0F
);
291 /* compute d?/dx and d?/dy derivatives */
294 GLfloat eMaj_dz
, eBot_dz
;
295 eMaj_dz
= vMax
->win
[2] - vMin
->win
[2];
296 eBot_dz
= vMid
->win
[2] - vMin
->win
[2];
297 dzdx
= oneOverArea
* (eMaj_dz
* eBot
.dy
- eMaj
.dy
* eBot_dz
);
298 if (dzdx
> maxDepth
|| dzdx
< -maxDepth
) {
299 /* probably a sliver triangle */
304 dzdy
= oneOverArea
* (eMaj
.dx
* eBot_dz
- eMaj_dz
* eBot
.dx
);
307 fdzdx
= SignedFloatToFixed(dzdx
);
309 fdzdx
= (GLint
) dzdx
;
312 GLfloat eMaj_dfog
, eBot_dfog
;
313 eMaj_dfog
= (vMax
->fog
- vMin
->fog
) * 256;
314 eBot_dfog
= (vMid
->fog
- vMin
->fog
) * 256;
315 dfogdx
= oneOverArea
* (eMaj_dfog
* eBot
.dy
- eMaj
.dy
* eBot_dfog
);
316 fdfogdx
= SignedFloatToFixed(dfogdx
);
317 dfogdy
= oneOverArea
* (eMaj
.dx
* eBot_dfog
- eMaj_dfog
* eBot
.dx
);
322 GLfloat eMaj_dr
, eBot_dr
;
323 eMaj_dr
= (GLint
) vMax
->color
[0]
324 - (GLint
) vMin
->color
[0];
325 eBot_dr
= (GLint
) vMid
->color
[0]
326 - (GLint
) vMin
->color
[0];
327 drdx
= oneOverArea
* (eMaj_dr
* eBot
.dy
- eMaj
.dy
* eBot_dr
);
328 fdrdx
= SignedFloatToFixed(drdx
);
329 drdy
= oneOverArea
* (eMaj
.dx
* eBot_dr
- eMaj_dr
* eBot
.dx
);
332 GLfloat eMaj_dg
, eBot_dg
;
333 eMaj_dg
= (GLint
) vMax
->color
[1]
334 - (GLint
) vMin
->color
[1];
335 eBot_dg
= (GLint
) vMid
->color
[1]
336 - (GLint
) vMin
->color
[1];
337 dgdx
= oneOverArea
* (eMaj_dg
* eBot
.dy
- eMaj
.dy
* eBot_dg
);
338 fdgdx
= SignedFloatToFixed(dgdx
);
339 dgdy
= oneOverArea
* (eMaj
.dx
* eBot_dg
- eMaj_dg
* eBot
.dx
);
342 GLfloat eMaj_db
, eBot_db
;
343 eMaj_db
= (GLint
) vMax
->color
[2]
344 - (GLint
) vMin
->color
[2];
345 eBot_db
= (GLint
) vMid
->color
[2]
346 - (GLint
) vMin
->color
[2];
347 dbdx
= oneOverArea
* (eMaj_db
* eBot
.dy
- eMaj
.dy
* eBot_db
);
348 fdbdx
= SignedFloatToFixed(dbdx
);
349 dbdy
= oneOverArea
* (eMaj
.dx
* eBot_db
- eMaj_db
* eBot
.dx
);
354 GLfloat eMaj_dsr
, eBot_dsr
;
355 eMaj_dsr
= (GLint
) vMax
->specular
[0]
356 - (GLint
) vMin
->specular
[0];
357 eBot_dsr
= (GLint
) vMid
->specular
[0]
358 - (GLint
) vMin
->specular
[0];
359 dsrdx
= oneOverArea
* (eMaj_dsr
* eBot
.dy
- eMaj
.dy
* eBot_dsr
);
360 fdsrdx
= SignedFloatToFixed(dsrdx
);
361 dsrdy
= oneOverArea
* (eMaj
.dx
* eBot_dsr
- eMaj_dsr
* eBot
.dx
);
364 GLfloat eMaj_dsg
, eBot_dsg
;
365 eMaj_dsg
= (GLint
) vMax
->specular
[1]
366 - (GLint
) vMin
->specular
[1];
367 eBot_dsg
= (GLint
) vMid
->specular
[1]
368 - (GLint
) vMin
->specular
[1];
369 dsgdx
= oneOverArea
* (eMaj_dsg
* eBot
.dy
- eMaj
.dy
* eBot_dsg
);
370 fdsgdx
= SignedFloatToFixed(dsgdx
);
371 dsgdy
= oneOverArea
* (eMaj
.dx
* eBot_dsg
- eMaj_dsg
* eBot
.dx
);
374 GLfloat eMaj_dsb
, eBot_dsb
;
375 eMaj_dsb
= (GLint
) vMax
->specular
[2]
376 - (GLint
) vMin
->specular
[2];
377 eBot_dsb
= (GLint
) vMid
->specular
[2]
378 - (GLint
) vMin
->specular
[2];
379 dsbdx
= oneOverArea
* (eMaj_dsb
* eBot
.dy
- eMaj
.dy
* eBot_dsb
);
380 fdsbdx
= SignedFloatToFixed(dsbdx
);
381 dsbdy
= oneOverArea
* (eMaj
.dx
* eBot_dsb
- eMaj_dsb
* eBot
.dx
);
386 GLfloat eMaj_da
, eBot_da
;
387 eMaj_da
= (GLint
) vMax
->color
[3]
388 - (GLint
) vMin
->color
[3];
389 eBot_da
= (GLint
) vMid
->color
[3]
390 - (GLint
) vMin
->color
[3];
391 dadx
= oneOverArea
* (eMaj_da
* eBot
.dy
- eMaj
.dy
* eBot_da
);
392 fdadx
= SignedFloatToFixed(dadx
);
393 dady
= oneOverArea
* (eMaj
.dx
* eBot_da
- eMaj_da
* eBot
.dx
);
398 GLfloat eMaj_di
, eBot_di
;
399 eMaj_di
= (GLint
) vMax
->index
400 - (GLint
) vMin
->index
;
401 eBot_di
= (GLint
) vMid
->index
402 - (GLint
) vMin
->index
;
403 didx
= oneOverArea
* (eMaj_di
* eBot
.dy
- eMaj
.dy
* eBot_di
);
404 fdidx
= SignedFloatToFixed(didx
);
405 didy
= oneOverArea
* (eMaj
.dx
* eBot_di
- eMaj_di
* eBot
.dx
);
408 #ifdef INTERP_INT_TEX
410 GLfloat eMaj_ds
, eBot_ds
;
411 eMaj_ds
= (vMax
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
412 eBot_ds
= (vMid
->texcoord
[0][0] - vMin
->texcoord
[0][0]) * S_SCALE
;
413 dsdx
= oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
414 fdsdx
= SignedFloatToFixed(dsdx
);
415 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
418 GLfloat eMaj_dt
, eBot_dt
;
419 eMaj_dt
= (vMax
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
420 eBot_dt
= (vMid
->texcoord
[0][1] - vMin
->texcoord
[0][1]) * T_SCALE
;
421 dtdx
= oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
422 fdtdx
= SignedFloatToFixed(dtdx
);
423 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
429 GLfloat wMax
= vMax
->win
[3];
430 GLfloat wMin
= vMin
->win
[3];
431 GLfloat wMid
= vMid
->win
[3];
432 GLfloat eMaj_ds
, eBot_ds
;
433 GLfloat eMaj_dt
, eBot_dt
;
434 GLfloat eMaj_du
, eBot_du
;
435 GLfloat eMaj_dv
, eBot_dv
;
437 eMaj_ds
= vMax
->texcoord
[0][0] * wMax
- vMin
->texcoord
[0][0] * wMin
;
438 eBot_ds
= vMid
->texcoord
[0][0] * wMid
- vMin
->texcoord
[0][0] * wMin
;
439 dsdx
= oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
440 dsdy
= oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
443 eMaj_dt
= vMax
->texcoord
[0][1] * wMax
- vMin
->texcoord
[0][1] * wMin
;
444 eBot_dt
= vMid
->texcoord
[0][1] * wMid
- vMin
->texcoord
[0][1] * wMin
;
445 dtdx
= oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
446 dtdy
= oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
448 eMaj_du
= vMax
->texcoord
[0][2] * wMax
- vMin
->texcoord
[0][2] * wMin
;
449 eBot_du
= vMid
->texcoord
[0][2] * wMid
- vMin
->texcoord
[0][2] * wMin
;
450 dudx
= oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
451 dudy
= oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
454 eMaj_dv
= vMax
->texcoord
[0][3] * wMax
- vMin
->texcoord
[0][3] * wMin
;
455 eBot_dv
= vMid
->texcoord
[0][3] * wMid
- vMin
->texcoord
[0][3] * wMin
;
456 dvdx
= oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
457 dvdy
= oneOverArea
* (eMaj
.dx
* eBot_dv
- eMaj_dv
* eBot
.dx
);
460 #ifdef INTERP_MULTITEX
462 GLfloat wMax
= vMax
->win
[3];
463 GLfloat wMin
= vMin
->win
[3];
464 GLfloat wMid
= vMid
->win
[3];
466 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
467 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
468 GLfloat eMaj_ds
, eBot_ds
;
469 GLfloat eMaj_dt
, eBot_dt
;
470 GLfloat eMaj_du
, eBot_du
;
471 GLfloat eMaj_dv
, eBot_dv
;
472 eMaj_ds
= vMax
->texcoord
[u
][0] * wMax
473 - vMin
->texcoord
[u
][0] * wMin
;
474 eBot_ds
= vMid
->texcoord
[u
][0] * wMid
475 - vMin
->texcoord
[u
][0] * wMin
;
476 dsdx
[u
] = oneOverArea
* (eMaj_ds
* eBot
.dy
- eMaj
.dy
* eBot_ds
);
477 dsdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_ds
- eMaj_ds
* eBot
.dx
);
479 eMaj_dt
= vMax
->texcoord
[u
][1] * wMax
480 - vMin
->texcoord
[u
][1] * wMin
;
481 eBot_dt
= vMid
->texcoord
[u
][1] * wMid
482 - vMin
->texcoord
[u
][1] * wMin
;
483 dtdx
[u
] = oneOverArea
* (eMaj_dt
* eBot
.dy
- eMaj
.dy
* eBot_dt
);
484 dtdy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_dt
- eMaj_dt
* eBot
.dx
);
486 eMaj_du
= vMax
->texcoord
[u
][2] * wMax
487 - vMin
->texcoord
[u
][2] * wMin
;
488 eBot_du
= vMid
->texcoord
[u
][2] * wMid
489 - vMin
->texcoord
[u
][2] * wMin
;
490 dudx
[u
] = oneOverArea
* (eMaj_du
* eBot
.dy
- eMaj
.dy
* eBot_du
);
491 dudy
[u
] = oneOverArea
* (eMaj
.dx
* eBot_du
- eMaj_du
* eBot
.dx
);
493 eMaj_dv
= vMax
->texcoord
[u
][3] * wMax
494 - vMin
->texcoord
[u
][3] * wMin
;
495 eBot_dv
= vMid
->texcoord
[u
][3] * wMid
496 - vMin
->texcoord
[u
][3] * wMin
;
497 dvdx
[u
] = oneOverArea
* (eMaj_dv
* eBot
.dy
- eMaj
.dy
* eBot_dv
);
498 dvdy
[u
] = 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.
553 GLfixed fx
, fxLeftEdge
, fxRightEdge
, fdxLeftEdge
, fdxRightEdge
;
557 GLfixed fError
, fdError
;
563 int dPRowOuter
, dPRowInner
; /* offset in bytes */
568 int dZRowOuter
, dZRowInner
; /* offset in bytes */
570 GLfixed fz
, fdzOuter
, fdzInner
;
571 GLfixed ffog
, fdfogOuter
, fdfogInner
;
574 GLfixed fr
, fdrOuter
, fdrInner
;
575 GLfixed fg
, fdgOuter
, fdgInner
;
576 GLfixed fb
, fdbOuter
, fdbInner
;
579 GLfixed fsr
, fdsrOuter
, fdsrInner
;
580 GLfixed fsg
, fdsgOuter
, fdsgInner
;
581 GLfixed fsb
, fdsbOuter
, fdsbInner
;
584 GLfixed fa
, fdaOuter
, fdaInner
;
587 GLfixed fi
, fdiOuter
, fdiInner
;
589 #ifdef INTERP_INT_TEX
590 GLfixed fs
, fdsOuter
, fdsInner
;
591 GLfixed ft
, fdtOuter
, fdtInner
;
594 GLfloat sLeft
, dsOuter
, dsInner
;
595 GLfloat tLeft
, dtOuter
, dtInner
;
596 GLfloat uLeft
, duOuter
, duInner
;
597 GLfloat vLeft
, dvOuter
, dvInner
;
599 #ifdef INTERP_MULTITEX
600 GLfloat sLeft
[MAX_TEXTURE_UNITS
];
601 GLfloat tLeft
[MAX_TEXTURE_UNITS
];
602 GLfloat uLeft
[MAX_TEXTURE_UNITS
];
603 GLfloat vLeft
[MAX_TEXTURE_UNITS
];
604 GLfloat dsOuter
[MAX_TEXTURE_UNITS
], dsInner
[MAX_TEXTURE_UNITS
];
605 GLfloat dtOuter
[MAX_TEXTURE_UNITS
], dtInner
[MAX_TEXTURE_UNITS
];
606 GLfloat duOuter
[MAX_TEXTURE_UNITS
], duInner
[MAX_TEXTURE_UNITS
];
607 GLfloat dvOuter
[MAX_TEXTURE_UNITS
], dvInner
[MAX_TEXTURE_UNITS
];
610 for (subTriangle
=0; subTriangle
<=1; subTriangle
++) {
611 EdgeT
*eLeft
, *eRight
;
612 int setupLeft
, setupRight
;
615 if (subTriangle
==0) {
620 lines
= eRight
->lines
;
627 lines
= eLeft
->lines
;
637 lines
= eRight
->lines
;
644 lines
= eLeft
->lines
;
652 if (setupLeft
&& eLeft
->lines
> 0) {
654 GLfixed fsx
= eLeft
->fsx
;
656 fError
= fx
- fsx
- FIXED_ONE
;
657 fxLeftEdge
= fsx
- FIXED_EPSILON
;
658 fdxLeftEdge
= eLeft
->fdxdy
;
659 fdxOuter
= FixedFloor(fdxLeftEdge
- FIXED_EPSILON
);
660 fdError
= fdxOuter
- fdxLeftEdge
+ FIXED_ONE
;
661 idxOuter
= FixedToInt(fdxOuter
);
662 dxOuter
= (float) idxOuter
;
668 adjx
= (float)(fx
- eLeft
->fx0
); /* SCALED! */
669 adjy
= eLeft
->adjy
; /* SCALED! */
670 (void) adjx
; /* silence compiler warnings */
671 (void) adjy
; /* silence compiler warnings */
674 (void) vLower
; /* silence compiler warnings */
678 pRow
= PIXEL_ADDRESS( FixedToInt(fxLeftEdge
), iy
);
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
+
699 dzdx
* adjx
+ dzdy
* adjy
) + FIXED_HALF
;
700 if (tmp
< MAX_GLUINT
/ 2)
704 fdzOuter
= SignedFloatToFixed(dzdy
+ dxOuter
* dzdx
);
707 /* interpolate depth values exactly */
708 fz
= (GLint
) (z0
+ dzdx
*FixedToFloat(adjx
) + dzdy
*FixedToFloat(adjy
));
709 fdzOuter
= (GLint
) (dzdy
+ dxOuter
* dzdx
);
712 zRow
= (DEPTH_TYPE
*) _mesa_zbuffer_address(ctx
, FixedToInt(fxLeftEdge
), iy
);
713 dZRowOuter
= (ctx
->DrawBuffer
->Width
+ idxOuter
) * sizeof(DEPTH_TYPE
);
717 ffog
= FloatToFixed(vLower
->fog
) * 256 + dfogdx
* adjx
+ dfogdy
* adjy
+ FIXED_HALF
;
718 fdfogOuter
= SignedFloatToFixed(dfogdy
+ dxOuter
* dfogdx
);
722 fr
= (GLfixed
)(IntToFixed(vLower
->color
[0])
723 + drdx
* adjx
+ drdy
* adjy
) + FIXED_HALF
;
724 fdrOuter
= SignedFloatToFixed(drdy
+ dxOuter
* drdx
);
726 fg
= (GLfixed
)(IntToFixed(vLower
->color
[1])
727 + dgdx
* adjx
+ dgdy
* adjy
) + FIXED_HALF
;
728 fdgOuter
= SignedFloatToFixed(dgdy
+ dxOuter
* dgdx
);
730 fb
= (GLfixed
)(IntToFixed(vLower
->color
[2])
731 + dbdx
* adjx
+ dbdy
* adjy
) + FIXED_HALF
;
732 fdbOuter
= SignedFloatToFixed(dbdy
+ dxOuter
* dbdx
);
735 fsr
= (GLfixed
)(IntToFixed(vLower
->specular
[0])
736 + dsrdx
* adjx
+ dsrdy
* adjy
) + FIXED_HALF
;
737 fdsrOuter
= SignedFloatToFixed(dsrdy
+ dxOuter
* dsrdx
);
739 fsg
= (GLfixed
)(IntToFixed(vLower
->specular
[1])
740 + dsgdx
* adjx
+ dsgdy
* adjy
) + FIXED_HALF
;
741 fdsgOuter
= SignedFloatToFixed(dsgdy
+ dxOuter
* dsgdx
);
743 fsb
= (GLfixed
)(IntToFixed(vLower
->specular
[2])
744 + dsbdx
* adjx
+ dsbdy
* adjy
) + FIXED_HALF
;
745 fdsbOuter
= SignedFloatToFixed(dsbdy
+ dxOuter
* dsbdx
);
748 fa
= (GLfixed
)(IntToFixed(vLower
->color
[3])
749 + dadx
* adjx
+ dady
* adjy
) + FIXED_HALF
;
750 fdaOuter
= SignedFloatToFixed(dady
+ dxOuter
* dadx
);
753 fi
= (GLfixed
)(vLower
->index
* FIXED_SCALE
754 + didx
* adjx
+ didy
* adjy
) + FIXED_HALF
;
755 fdiOuter
= SignedFloatToFixed(didy
+ dxOuter
* didx
);
757 #ifdef INTERP_INT_TEX
760 s0
= vLower
->texcoord
[0][0] * S_SCALE
;
761 fs
= (GLfixed
)(s0
* FIXED_SCALE
+ dsdx
* adjx
+ dsdy
* adjy
) + FIXED_HALF
;
762 fdsOuter
= SignedFloatToFixed(dsdy
+ dxOuter
* dsdx
);
764 t0
= vLower
->texcoord
[0][1] * T_SCALE
;
765 ft
= (GLfixed
)(t0
* FIXED_SCALE
+ dtdx
* adjx
+ dtdy
* adjy
) + FIXED_HALF
;
766 fdtOuter
= SignedFloatToFixed(dtdy
+ dxOuter
* dtdx
);
771 GLfloat invW
= vLower
->win
[3];
772 GLfloat s0
, t0
, u0
, v0
;
773 s0
= vLower
->texcoord
[0][0] * invW
;
774 sLeft
= s0
+ (dsdx
* adjx
+ dsdy
* adjy
) * (1.0F
/FIXED_SCALE
);
775 dsOuter
= dsdy
+ dxOuter
* dsdx
;
776 t0
= vLower
->texcoord
[0][1] * invW
;
777 tLeft
= t0
+ (dtdx
* adjx
+ dtdy
* adjy
) * (1.0F
/FIXED_SCALE
);
778 dtOuter
= dtdy
+ dxOuter
* dtdx
;
779 u0
= vLower
->texcoord
[0][2] * invW
;
780 uLeft
= u0
+ (dudx
* adjx
+ dudy
* adjy
) * (1.0F
/FIXED_SCALE
);
781 duOuter
= dudy
+ dxOuter
* dudx
;
782 v0
= vLower
->texcoord
[0][3] * invW
;
783 vLeft
= v0
+ (dvdx
* adjx
+ dvdy
* adjy
) * (1.0F
/FIXED_SCALE
);
784 dvOuter
= dvdy
+ dxOuter
* dvdx
;
787 #ifdef INTERP_MULTITEX
790 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
791 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
792 GLfloat invW
= vLower
->win
[3];
793 GLfloat s0
, t0
, u0
, v0
;
794 s0
= vLower
->texcoord
[u
][0] * invW
;
795 sLeft
[u
] = s0
+ (dsdx
[u
] * adjx
+ dsdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
796 dsOuter
[u
] = dsdy
[u
] + dxOuter
* dsdx
[u
];
797 t0
= vLower
->texcoord
[u
][1] * invW
;
798 tLeft
[u
] = t0
+ (dtdx
[u
] * adjx
+ dtdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
799 dtOuter
[u
] = dtdy
[u
] + dxOuter
* dtdx
[u
];
800 u0
= vLower
->texcoord
[u
][2] * invW
;
801 uLeft
[u
] = u0
+ (dudx
[u
] * adjx
+ dudy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
802 duOuter
[u
] = dudy
[u
] + dxOuter
* dudx
[u
];
803 v0
= vLower
->texcoord
[u
][3] * invW
;
804 vLeft
[u
] = v0
+ (dvdx
[u
] * adjx
+ dvdy
[u
] * adjy
) * (1.0F
/FIXED_SCALE
);
805 dvOuter
[u
] = dvdy
[u
] + dxOuter
* dvdx
[u
];
814 if (setupRight
&& eRight
->lines
>0) {
815 fxRightEdge
= eRight
->fsx
- FIXED_EPSILON
;
816 fdxRightEdge
= eRight
->fdxdy
;
824 /* Rasterize setup */
826 dPRowInner
= dPRowOuter
+ sizeof(PIXEL_TYPE
);
830 dZRowInner
= dZRowOuter
+ sizeof(DEPTH_TYPE
);
832 fdzInner
= fdzOuter
+ fdzdx
;
833 fdfogInner
= fdfogOuter
+ fdfogdx
;
836 fdrInner
= fdrOuter
+ fdrdx
;
837 fdgInner
= fdgOuter
+ fdgdx
;
838 fdbInner
= fdbOuter
+ fdbdx
;
841 fdsrInner
= fdsrOuter
+ fdsrdx
;
842 fdsgInner
= fdsgOuter
+ fdsgdx
;
843 fdsbInner
= fdsbOuter
+ fdsbdx
;
846 fdaInner
= fdaOuter
+ fdadx
;
849 fdiInner
= fdiOuter
+ fdidx
;
851 #ifdef INTERP_INT_TEX
852 fdsInner
= fdsOuter
+ fdsdx
;
853 fdtInner
= fdtOuter
+ fdtdx
;
856 dsInner
= dsOuter
+ dsdx
;
857 dtInner
= dtOuter
+ dtdx
;
858 duInner
= duOuter
+ dudx
;
859 dvInner
= dvOuter
+ dvdx
;
861 #ifdef INTERP_MULTITEX
864 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
865 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
866 dsInner
[u
] = dsOuter
[u
] + dsdx
[u
];
867 dtInner
[u
] = dtOuter
[u
] + dtdx
[u
];
868 duInner
[u
] = duOuter
[u
] + dudx
[u
];
869 dvInner
[u
] = dvOuter
[u
] + dvdx
[u
];
876 /* initialize the span interpolants to the leftmost value */
877 /* ff = fixed-pt fragment */
878 GLint left
= FixedToInt(fxLeftEdge
);
879 GLint right
= FixedToInt(fxRightEdge
);
882 GLfixed fffog
= ffog
;
885 GLfixed ffr
= fr
, ffg
= fg
, ffb
= fb
;
888 GLfixed ffsr
= fsr
, ffsg
= fsg
, ffsb
= fsb
;
896 #ifdef INTERP_INT_TEX
897 GLfixed ffs
= fs
, fft
= ft
;
900 GLfloat ss
= sLeft
, tt
= tLeft
, uu
= uLeft
, vv
= vLeft
;
902 #ifdef INTERP_MULTITEX
903 GLfloat ss
[MAX_TEXTURE_UNITS
];
904 GLfloat tt
[MAX_TEXTURE_UNITS
];
905 GLfloat uu
[MAX_TEXTURE_UNITS
];
906 GLfloat vv
[MAX_TEXTURE_UNITS
];
909 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
910 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
922 /* need this to accomodate round-off errors */
923 GLfixed ffrend
= ffr
+(right
-left
-1)*fdrdx
;
924 GLfixed ffgend
= ffg
+(right
-left
-1)*fdgdx
;
925 GLfixed ffbend
= ffb
+(right
-left
-1)*fdbdx
;
926 if (ffrend
<0) ffr
-= ffrend
;
927 if (ffgend
<0) ffg
-= ffgend
;
928 if (ffbend
<0) ffb
-= ffbend
;
936 /* need this to accomodate round-off errors */
937 GLfixed ffsrend
= ffsr
+(right
-left
-1)*fdsrdx
;
938 GLfixed ffsgend
= ffsg
+(right
-left
-1)*fdsgdx
;
939 GLfixed ffsbend
= ffsb
+(right
-left
-1)*fdsbdx
;
940 if (ffsrend
<0) ffsr
-= ffsrend
;
941 if (ffsgend
<0) ffsg
-= ffsgend
;
942 if (ffsbend
<0) ffsb
-= ffsbend
;
943 if (ffsr
<0) ffsr
= 0;
944 if (ffsg
<0) ffsg
= 0;
945 if (ffsb
<0) ffsb
= 0;
950 GLfixed ffaend
= ffa
+(right
-left
-1)*fdadx
;
951 if (ffaend
<0) ffa
-= ffaend
;
959 INNER_LOOP( left
, right
, iy
);
962 * Advance to the next scan line. Compute the
963 * new edge coordinates, and adjust the
964 * pixel-center x coordinate so that it stays
965 * on or inside the major edge.
970 fxLeftEdge
+= fdxLeftEdge
;
971 fxRightEdge
+= fdxRightEdge
;
978 pRow
= (PIXEL_TYPE
*) ((GLubyte
*)pRow
+ dPRowOuter
);
982 zRow
= (DEPTH_TYPE
*) ((GLubyte
*)zRow
+ dZRowOuter
);
988 fr
+= fdrOuter
; fg
+= fdgOuter
; fb
+= fdbOuter
;
991 fsr
+= fdsrOuter
; fsg
+= fdsgOuter
; fsb
+= fdsbOuter
;
999 #ifdef INTERP_INT_TEX
1000 fs
+= fdsOuter
; ft
+= fdtOuter
;
1008 #ifdef INTERP_MULTITEX
1011 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1012 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1013 sLeft
[u
] += dsOuter
[u
];
1014 tLeft
[u
] += dtOuter
[u
];
1015 uLeft
[u
] += duOuter
[u
];
1016 vLeft
[u
] += dvOuter
[u
];
1023 #ifdef PIXEL_ADDRESS
1024 pRow
= (PIXEL_TYPE
*) ((GLubyte
*)pRow
+ dPRowInner
);
1028 zRow
= (DEPTH_TYPE
*) ((GLubyte
*)zRow
+ dZRowInner
);
1034 fr
+= fdrInner
; fg
+= fdgInner
; fb
+= fdbInner
;
1037 fsr
+= fdsrInner
; fsg
+= fdsgInner
; fsb
+= fdsbInner
;
1045 #ifdef INTERP_INT_TEX
1046 fs
+= fdsInner
; ft
+= fdtInner
;
1054 #ifdef INTERP_MULTITEX
1057 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
1058 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1059 sLeft
[u
] += dsInner
[u
];
1060 tLeft
[u
] += dtInner
[u
];
1061 uLeft
[u
] += duInner
[u
];
1062 vLeft
[u
] += dvInner
[u
];
1070 } /* for subTriangle */
1080 #undef BYTES_PER_ROW
1081 #undef PIXEL_ADDRESS
1088 #undef INTERP_INT_TEX
1090 #undef INTERP_MULTITEX
1097 #undef DO_OCCLUSION_TEST