2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Antialiased Triangle Rasterizer Template
29 * This file is #include'd to generate custom AA triangle rasterizers.
30 * NOTE: this code hasn't been optimized yet. That'll come after it
33 * The following macros may be defined to indicate what auxillary information
34 * must be copmuted across the triangle:
35 * DO_Z - if defined, compute Z values
36 * DO_ATTRIBS - if defined, compute texcoords, varying, etc.
39 /*void triangle( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv )*/
41 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
42 const GLfloat
*p0
= v0
->attrib
[VARYING_SLOT_POS
];
43 const GLfloat
*p1
= v1
->attrib
[VARYING_SLOT_POS
];
44 const GLfloat
*p2
= v2
->attrib
[VARYING_SLOT_POS
];
45 const SWvertex
*vMin
, *vMid
, *vMax
;
49 GLfloat majDx
, majDy
; /* major (i.e. long) edge dx and dy */
56 GLfloat rPlane
[4], gPlane
[4], bPlane
[4], aPlane
[4];
57 #if defined(DO_ATTRIBS)
58 GLfloat attrPlane
[VARYING_SLOT_MAX
][4][4];
59 GLfloat wPlane
[4]; /* win[3] */
61 GLfloat bf
= SWRAST_CONTEXT(ctx
)->_BackfaceCullSign
;
65 INIT_SPAN(span
, GL_POLYGON
);
66 span
.arrayMask
= SPAN_COVERAGE
;
68 /* determine bottom to top order of vertices */
70 GLfloat y0
= v0
->attrib
[VARYING_SLOT_POS
][1];
71 GLfloat y1
= v1
->attrib
[VARYING_SLOT_POS
][1];
72 GLfloat y2
= v2
->attrib
[VARYING_SLOT_POS
][1];
75 vMin
= v0
; vMid
= v1
; vMax
= v2
; /* y0<=y1<=y2 */
78 vMin
= v2
; vMid
= v0
; vMax
= v1
; /* y2<=y0<=y1 */
81 vMin
= v0
; vMid
= v2
; vMax
= v1
; bf
= -bf
; /* y0<=y2<=y1 */
86 vMin
= v1
; vMid
= v0
; vMax
= v2
; bf
= -bf
; /* y1<=y0<=y2 */
89 vMin
= v2
; vMid
= v1
; vMax
= v0
; bf
= -bf
; /* y2<=y1<=y0 */
92 vMin
= v1
; vMid
= v2
; vMax
= v0
; /* y1<=y2<=y0 */
97 majDx
= vMax
->attrib
[VARYING_SLOT_POS
][0] - vMin
->attrib
[VARYING_SLOT_POS
][0];
98 majDy
= vMax
->attrib
[VARYING_SLOT_POS
][1] - vMin
->attrib
[VARYING_SLOT_POS
][1];
100 /* front/back-face determination and cullling */
102 const GLfloat botDx
= vMid
->attrib
[VARYING_SLOT_POS
][0] - vMin
->attrib
[VARYING_SLOT_POS
][0];
103 const GLfloat botDy
= vMid
->attrib
[VARYING_SLOT_POS
][1] - vMin
->attrib
[VARYING_SLOT_POS
][1];
104 const GLfloat area
= majDx
* botDy
- botDx
* majDy
;
105 /* Do backface culling */
106 if (area
* bf
< 0 || area
== 0 || IS_INF_OR_NAN(area
))
108 ltor
= (GLboolean
) (area
< 0.0F
);
110 span
.facing
= area
* swrast
->_BackfaceSign
> 0.0F
;
113 /* Plane equation setup:
114 * We evaluate plane equations at window (x,y) coordinates in order
115 * to compute color, Z, fog, texcoords, etc. This isn't terribly
116 * efficient but it's easy and reliable.
119 compute_plane(p0
, p1
, p2
, p0
[2], p1
[2], p2
[2], zPlane
);
120 span
.arrayMask
|= SPAN_Z
;
122 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
123 compute_plane(p0
, p1
, p2
, v0
->color
[RCOMP
], v1
->color
[RCOMP
], v2
->color
[RCOMP
], rPlane
);
124 compute_plane(p0
, p1
, p2
, v0
->color
[GCOMP
], v1
->color
[GCOMP
], v2
->color
[GCOMP
], gPlane
);
125 compute_plane(p0
, p1
, p2
, v0
->color
[BCOMP
], v1
->color
[BCOMP
], v2
->color
[BCOMP
], bPlane
);
126 compute_plane(p0
, p1
, p2
, v0
->color
[ACOMP
], v1
->color
[ACOMP
], v2
->color
[ACOMP
], aPlane
);
129 constant_plane(v2
->color
[RCOMP
], rPlane
);
130 constant_plane(v2
->color
[GCOMP
], gPlane
);
131 constant_plane(v2
->color
[BCOMP
], bPlane
);
132 constant_plane(v2
->color
[ACOMP
], aPlane
);
134 span
.arrayMask
|= SPAN_RGBA
;
135 #if defined(DO_ATTRIBS)
137 const GLfloat invW0
= v0
->attrib
[VARYING_SLOT_POS
][3];
138 const GLfloat invW1
= v1
->attrib
[VARYING_SLOT_POS
][3];
139 const GLfloat invW2
= v2
->attrib
[VARYING_SLOT_POS
][3];
140 compute_plane(p0
, p1
, p2
, invW0
, invW1
, invW2
, wPlane
);
141 span
.attrStepX
[VARYING_SLOT_POS
][3] = plane_dx(wPlane
);
142 span
.attrStepY
[VARYING_SLOT_POS
][3] = plane_dy(wPlane
);
145 if (swrast
->_InterpMode
[attr
] == GL_FLAT
) {
146 for (c
= 0; c
< 4; c
++) {
147 constant_plane(v2
->attrib
[attr
][c
] * invW2
, attrPlane
[attr
][c
]);
151 for (c
= 0; c
< 4; c
++) {
152 const GLfloat a0
= v0
->attrib
[attr
][c
] * invW0
;
153 const GLfloat a1
= v1
->attrib
[attr
][c
] * invW1
;
154 const GLfloat a2
= v2
->attrib
[attr
][c
] * invW2
;
155 compute_plane(p0
, p1
, p2
, a0
, a1
, a2
, attrPlane
[attr
][c
]);
158 for (c
= 0; c
< 4; c
++) {
159 span
.attrStepX
[attr
][c
] = plane_dx(attrPlane
[attr
][c
]);
160 span
.attrStepY
[attr
][c
] = plane_dy(attrPlane
[attr
][c
]);
166 /* Begin bottom-to-top scan over the triangle.
167 * The long edge will either be on the left or right side of the
168 * triangle. We always scan from the long edge toward the shorter
169 * edges, stopping when we find that coverage = 0. If the long edge
170 * is on the left we scan left-to-right. Else, we scan right-to-left.
172 yMin
= vMin
->attrib
[VARYING_SLOT_POS
][1];
173 yMax
= vMax
->attrib
[VARYING_SLOT_POS
][1];
174 iyMin
= (GLint
) yMin
;
175 iyMax
= (GLint
) yMax
+ 1;
178 /* scan left to right */
179 const GLfloat
*pMin
= vMin
->attrib
[VARYING_SLOT_POS
];
180 const GLfloat
*pMid
= vMid
->attrib
[VARYING_SLOT_POS
];
181 const GLfloat
*pMax
= vMax
->attrib
[VARYING_SLOT_POS
];
182 const GLfloat dxdy
= majDx
/ majDy
;
183 const GLfloat xAdj
= dxdy
< 0.0F
? -dxdy
: 0.0F
;
186 #pragma omp parallel for schedule(dynamic) private(iy) firstprivate(span)
188 for (iy
= iyMin
; iy
< iyMax
; iy
++) {
189 GLfloat x
= pMin
[0] - (yMin
- iy
) * dxdy
;
190 GLint ix
, startX
= (GLint
) (x
- xAdj
);
192 GLfloat coverage
= 0.0F
;
195 /* each thread needs to use a different (global) SpanArrays variable */
196 span
.array
= SWRAST_CONTEXT(ctx
)->SpanArrays
+ omp_get_thread_num();
198 /* skip over fragments with zero coverage */
199 while (startX
< SWRAST_MAX_WIDTH
) {
200 coverage
= compute_coveragef(pMin
, pMid
, pMax
, startX
, iy
);
206 /* enter interior of triangle */
209 #if defined(DO_ATTRIBS)
210 /* compute attributes at left-most fragment */
211 span
.attrStart
[VARYING_SLOT_POS
][3] = solve_plane(ix
+ 0.5F
, iy
+ 0.5F
, wPlane
);
214 for (c
= 0; c
< 4; c
++) {
215 span
.attrStart
[attr
][c
] = solve_plane(ix
+ 0.5F
, iy
+ 0.5F
, attrPlane
[attr
][c
]);
221 while (coverage
> 0.0F
) {
222 /* (cx,cy) = center of fragment */
223 const GLfloat cx
= ix
+ 0.5F
, cy
= iy
+ 0.5F
;
224 SWspanarrays
*array
= span
.array
;
225 array
->coverage
[count
] = coverage
;
227 array
->z
[count
] = (GLuint
) solve_plane(cx
, cy
, zPlane
);
229 array
->rgba
[count
][RCOMP
] = solve_plane_chan(cx
, cy
, rPlane
);
230 array
->rgba
[count
][GCOMP
] = solve_plane_chan(cx
, cy
, gPlane
);
231 array
->rgba
[count
][BCOMP
] = solve_plane_chan(cx
, cy
, bPlane
);
232 array
->rgba
[count
][ACOMP
] = solve_plane_chan(cx
, cy
, aPlane
);
235 coverage
= compute_coveragef(pMin
, pMid
, pMax
, ix
, iy
);
241 span
.end
= (GLuint
) ix
- (GLuint
) startX
;
242 _swrast_write_rgba_span(ctx
, &span
);
247 /* scan right to left */
248 const GLfloat
*pMin
= vMin
->attrib
[VARYING_SLOT_POS
];
249 const GLfloat
*pMid
= vMid
->attrib
[VARYING_SLOT_POS
];
250 const GLfloat
*pMax
= vMax
->attrib
[VARYING_SLOT_POS
];
251 const GLfloat dxdy
= majDx
/ majDy
;
252 const GLfloat xAdj
= dxdy
> 0 ? dxdy
: 0.0F
;
255 #pragma omp parallel for schedule(dynamic) private(iy) firstprivate(span)
257 for (iy
= iyMin
; iy
< iyMax
; iy
++) {
258 GLfloat x
= pMin
[0] - (yMin
- iy
) * dxdy
;
259 GLint ix
, left
, startX
= (GLint
) (x
+ xAdj
);
261 GLfloat coverage
= 0.0F
;
264 /* each thread needs to use a different (global) SpanArrays variable */
265 span
.array
= SWRAST_CONTEXT(ctx
)->SpanArrays
+ omp_get_thread_num();
267 /* make sure we're not past the window edge */
268 if (startX
>= ctx
->DrawBuffer
->_Xmax
) {
269 startX
= ctx
->DrawBuffer
->_Xmax
- 1;
272 /* skip fragments with zero coverage */
274 coverage
= compute_coveragef(pMin
, pMax
, pMid
, startX
, iy
);
280 /* enter interior of triangle */
283 while (coverage
> 0.0F
) {
284 /* (cx,cy) = center of fragment */
285 const GLfloat cx
= ix
+ 0.5F
, cy
= iy
+ 0.5F
;
286 SWspanarrays
*array
= span
.array
;
288 array
->coverage
[ix
] = coverage
;
290 array
->z
[ix
] = (GLuint
) solve_plane(cx
, cy
, zPlane
);
292 array
->rgba
[ix
][RCOMP
] = solve_plane_chan(cx
, cy
, rPlane
);
293 array
->rgba
[ix
][GCOMP
] = solve_plane_chan(cx
, cy
, gPlane
);
294 array
->rgba
[ix
][BCOMP
] = solve_plane_chan(cx
, cy
, bPlane
);
295 array
->rgba
[ix
][ACOMP
] = solve_plane_chan(cx
, cy
, aPlane
);
298 coverage
= compute_coveragef(pMin
, pMax
, pMid
, ix
, iy
);
301 #if defined(DO_ATTRIBS)
302 /* compute attributes at left-most fragment */
303 span
.attrStart
[VARYING_SLOT_POS
][3] = solve_plane(ix
+ 1.5F
, iy
+ 0.5F
, wPlane
);
306 for (c
= 0; c
< 4; c
++) {
307 span
.attrStart
[attr
][c
] = solve_plane(ix
+ 1.5F
, iy
+ 0.5F
, attrPlane
[attr
][c
]);
313 n
= (GLuint
) startX
- (GLuint
) ix
;
317 /* shift all values to the left */
318 /* XXX this is temporary */
320 SWspanarrays
*array
= span
.array
;
322 for (j
= 0; j
< (GLint
) n
; j
++) {
323 array
->coverage
[j
] = array
->coverage
[j
+ left
];
324 COPY_CHAN4(array
->rgba
[j
], array
->rgba
[j
+ left
]);
326 array
->z
[j
] = array
->z
[j
+ left
];
334 _swrast_write_rgba_span(ctx
, &span
);
343 #undef DO_OCCLUSION_TEST