Merge branch 'master' of git+ssh://joukj@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / swrast / s_aatritemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * Antialiased Triangle Rasterizer Template
28 *
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
31 * works correctly.
32 *
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_RGBA - if defined, compute RGBA values
37 * DO_INDEX - if defined, compute color index values
38 * DO_ATTRIBS - if defined, compute texcoords, varying, etc.
39 */
40
41 /*void triangle( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv )*/
42 {
43 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
44 const GLfloat *p0 = v0->attrib[FRAG_ATTRIB_WPOS];
45 const GLfloat *p1 = v1->attrib[FRAG_ATTRIB_WPOS];
46 const GLfloat *p2 = v2->attrib[FRAG_ATTRIB_WPOS];
47 const SWvertex *vMin, *vMid, *vMax;
48 GLint iyMin, iyMax;
49 GLfloat yMin, yMax;
50 GLboolean ltor;
51 GLfloat majDx, majDy; /* major (i.e. long) edge dx and dy */
52
53 SWspan span;
54
55 #ifdef DO_Z
56 GLfloat zPlane[4];
57 #endif
58 #ifdef DO_RGBA
59 GLfloat rPlane[4], gPlane[4], bPlane[4], aPlane[4];
60 #endif
61 #ifdef DO_INDEX
62 GLfloat iPlane[4];
63 #endif
64 #if defined(DO_ATTRIBS)
65 GLfloat attrPlane[FRAG_ATTRIB_MAX][4][4];
66 GLfloat wPlane[4]; /* win[3] */
67 #endif
68 GLfloat bf = SWRAST_CONTEXT(ctx)->_BackfaceSign;
69
70 (void) swrast;
71
72 INIT_SPAN(span, GL_POLYGON, 0, 0, SPAN_COVERAGE);
73
74 /* determine bottom to top order of vertices */
75 {
76 GLfloat y0 = v0->attrib[FRAG_ATTRIB_WPOS][1];
77 GLfloat y1 = v1->attrib[FRAG_ATTRIB_WPOS][1];
78 GLfloat y2 = v2->attrib[FRAG_ATTRIB_WPOS][1];
79 if (y0 <= y1) {
80 if (y1 <= y2) {
81 vMin = v0; vMid = v1; vMax = v2; /* y0<=y1<=y2 */
82 }
83 else if (y2 <= y0) {
84 vMin = v2; vMid = v0; vMax = v1; /* y2<=y0<=y1 */
85 }
86 else {
87 vMin = v0; vMid = v2; vMax = v1; bf = -bf; /* y0<=y2<=y1 */
88 }
89 }
90 else {
91 if (y0 <= y2) {
92 vMin = v1; vMid = v0; vMax = v2; bf = -bf; /* y1<=y0<=y2 */
93 }
94 else if (y2 <= y1) {
95 vMin = v2; vMid = v1; vMax = v0; bf = -bf; /* y2<=y1<=y0 */
96 }
97 else {
98 vMin = v1; vMid = v2; vMax = v0; /* y1<=y2<=y0 */
99 }
100 }
101 }
102
103 majDx = vMax->attrib[FRAG_ATTRIB_WPOS][0] - vMin->attrib[FRAG_ATTRIB_WPOS][0];
104 majDy = vMax->attrib[FRAG_ATTRIB_WPOS][1] - vMin->attrib[FRAG_ATTRIB_WPOS][1];
105
106 {
107 const GLfloat botDx = vMid->attrib[FRAG_ATTRIB_WPOS][0] - vMin->attrib[FRAG_ATTRIB_WPOS][0];
108 const GLfloat botDy = vMid->attrib[FRAG_ATTRIB_WPOS][1] - vMin->attrib[FRAG_ATTRIB_WPOS][1];
109 const GLfloat area = majDx * botDy - botDx * majDy;
110 /* Do backface culling */
111 if (area * bf < 0 || area == 0 || IS_INF_OR_NAN(area))
112 return;
113 ltor = (GLboolean) (area < 0.0F);
114 }
115
116 /* Plane equation setup:
117 * We evaluate plane equations at window (x,y) coordinates in order
118 * to compute color, Z, fog, texcoords, etc. This isn't terribly
119 * efficient but it's easy and reliable.
120 */
121 #ifdef DO_Z
122 compute_plane(p0, p1, p2, p0[2], p1[2], p2[2], zPlane);
123 span.arrayMask |= SPAN_Z;
124 #endif
125 #ifdef DO_RGBA
126 if (ctx->Light.ShadeModel == GL_SMOOTH) {
127 compute_plane(p0, p1, p2, v0->color[RCOMP], v1->color[RCOMP], v2->color[RCOMP], rPlane);
128 compute_plane(p0, p1, p2, v0->color[GCOMP], v1->color[GCOMP], v2->color[GCOMP], gPlane);
129 compute_plane(p0, p1, p2, v0->color[BCOMP], v1->color[BCOMP], v2->color[BCOMP], bPlane);
130 compute_plane(p0, p1, p2, v0->color[ACOMP], v1->color[ACOMP], v2->color[ACOMP], aPlane);
131 }
132 else {
133 constant_plane(v2->color[RCOMP], rPlane);
134 constant_plane(v2->color[GCOMP], gPlane);
135 constant_plane(v2->color[BCOMP], bPlane);
136 constant_plane(v2->color[ACOMP], aPlane);
137 }
138 span.arrayMask |= SPAN_RGBA;
139 #endif
140 #ifdef DO_INDEX
141 if (ctx->Light.ShadeModel == GL_SMOOTH) {
142 compute_plane(p0, p1, p2, (GLfloat) v0->attrib[FRAG_ATTRIB_CI][0],
143 v1->attrib[FRAG_ATTRIB_CI][0], v2->attrib[FRAG_ATTRIB_CI][0], iPlane);
144 }
145 else {
146 constant_plane(v2->attrib[FRAG_ATTRIB_CI][0], iPlane);
147 }
148 span.arrayMask |= SPAN_INDEX;
149 #endif
150 #if defined(DO_ATTRIBS)
151 {
152 const GLfloat invW0 = v0->attrib[FRAG_ATTRIB_WPOS][3];
153 const GLfloat invW1 = v1->attrib[FRAG_ATTRIB_WPOS][3];
154 const GLfloat invW2 = v2->attrib[FRAG_ATTRIB_WPOS][3];
155 compute_plane(p0, p1, p2, invW0, invW1, invW2, wPlane);
156 span.attrStepX[FRAG_ATTRIB_WPOS][3] = plane_dx(wPlane);
157 span.attrStepY[FRAG_ATTRIB_WPOS][3] = plane_dy(wPlane);
158 ATTRIB_LOOP_BEGIN
159 GLuint c;
160 if (swrast->_InterpMode[attr] == GL_FLAT) {
161 for (c = 0; c < 4; c++) {
162 constant_plane(v2->attrib[attr][c] * invW2, attrPlane[attr][c]);
163 }
164 }
165 else {
166 for (c = 0; c < 4; c++) {
167 const GLfloat a0 = v0->attrib[attr][c] * invW0;
168 const GLfloat a1 = v1->attrib[attr][c] * invW1;
169 const GLfloat a2 = v2->attrib[attr][c] * invW2;
170 compute_plane(p0, p1, p2, a0, a1, a2, attrPlane[attr][c]);
171 }
172 }
173 for (c = 0; c < 4; c++) {
174 span.attrStepX[attr][c] = plane_dx(attrPlane[attr][c]);
175 span.attrStepY[attr][c] = plane_dy(attrPlane[attr][c]);
176 }
177 ATTRIB_LOOP_END
178 }
179 #endif
180
181 /* Begin bottom-to-top scan over the triangle.
182 * The long edge will either be on the left or right side of the
183 * triangle. We always scan from the long edge toward the shorter
184 * edges, stopping when we find that coverage = 0. If the long edge
185 * is on the left we scan left-to-right. Else, we scan right-to-left.
186 */
187 yMin = vMin->attrib[FRAG_ATTRIB_WPOS][1];
188 yMax = vMax->attrib[FRAG_ATTRIB_WPOS][1];
189 iyMin = (GLint) yMin;
190 iyMax = (GLint) yMax + 1;
191
192 if (ltor) {
193 /* scan left to right */
194 const GLfloat *pMin = vMin->attrib[FRAG_ATTRIB_WPOS];
195 const GLfloat *pMid = vMid->attrib[FRAG_ATTRIB_WPOS];
196 const GLfloat *pMax = vMax->attrib[FRAG_ATTRIB_WPOS];
197 const GLfloat dxdy = majDx / majDy;
198 const GLfloat xAdj = dxdy < 0.0F ? -dxdy : 0.0F;
199 GLfloat x = pMin[0] - (yMin - iyMin) * dxdy;
200 GLint iy;
201 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) {
202 GLint ix, startX = (GLint) (x - xAdj);
203 GLuint count;
204 GLfloat coverage = 0.0F;
205
206 /* skip over fragments with zero coverage */
207 while (startX < MAX_WIDTH) {
208 coverage = compute_coveragef(pMin, pMid, pMax, startX, iy);
209 if (coverage > 0.0F)
210 break;
211 startX++;
212 }
213
214 /* enter interior of triangle */
215 ix = startX;
216
217 #if defined(DO_ATTRIBS)
218 /* compute attributes at left-most fragment */
219 span.attrStart[FRAG_ATTRIB_WPOS][3] = solve_plane(ix + 0.5, iy + 0.5, wPlane);
220 ATTRIB_LOOP_BEGIN
221 GLuint c;
222 for (c = 0; c < 4; c++) {
223 span.attrStart[attr][c] = solve_plane(ix + 0.5, iy + 0.5, attrPlane[attr][c]);
224 }
225 ATTRIB_LOOP_END
226 #endif
227
228 count = 0;
229 while (coverage > 0.0F) {
230 /* (cx,cy) = center of fragment */
231 const GLfloat cx = ix + 0.5F, cy = iy + 0.5F;
232 SWspanarrays *array = span.array;
233 #ifdef DO_INDEX
234 array->coverage[count] = (GLfloat) compute_coveragei(pMin, pMid, pMax, ix, iy);
235 #else
236 array->coverage[count] = coverage;
237 #endif
238 #ifdef DO_Z
239 array->z[count] = (GLuint) solve_plane(cx, cy, zPlane);
240 #endif
241 #ifdef DO_RGBA
242 array->rgba[count][RCOMP] = solve_plane_chan(cx, cy, rPlane);
243 array->rgba[count][GCOMP] = solve_plane_chan(cx, cy, gPlane);
244 array->rgba[count][BCOMP] = solve_plane_chan(cx, cy, bPlane);
245 array->rgba[count][ACOMP] = solve_plane_chan(cx, cy, aPlane);
246 #endif
247 #ifdef DO_INDEX
248 array->index[count] = (GLint) solve_plane(cx, cy, iPlane);
249 #endif
250 ix++;
251 count++;
252 coverage = compute_coveragef(pMin, pMid, pMax, ix, iy);
253 }
254
255 if (ix <= startX)
256 continue;
257
258 span.x = startX;
259 span.y = iy;
260 span.end = (GLuint) ix - (GLuint) startX;
261 #if defined(DO_RGBA)
262 _swrast_write_rgba_span(ctx, &span);
263 #else
264 _swrast_write_index_span(ctx, &span);
265 #endif
266 }
267 }
268 else {
269 /* scan right to left */
270 const GLfloat *pMin = vMin->attrib[FRAG_ATTRIB_WPOS];
271 const GLfloat *pMid = vMid->attrib[FRAG_ATTRIB_WPOS];
272 const GLfloat *pMax = vMax->attrib[FRAG_ATTRIB_WPOS];
273 const GLfloat dxdy = majDx / majDy;
274 const GLfloat xAdj = dxdy > 0 ? dxdy : 0.0F;
275 GLfloat x = pMin[0] - (yMin - iyMin) * dxdy;
276 GLint iy;
277 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) {
278 GLint ix, left, startX = (GLint) (x + xAdj);
279 GLuint count, n;
280 GLfloat coverage = 0.0F;
281
282 /* make sure we're not past the window edge */
283 if (startX >= ctx->DrawBuffer->_Xmax) {
284 startX = ctx->DrawBuffer->_Xmax - 1;
285 }
286
287 /* skip fragments with zero coverage */
288 while (startX >= 0) {
289 coverage = compute_coveragef(pMin, pMax, pMid, startX, iy);
290 if (coverage > 0.0F)
291 break;
292 startX--;
293 }
294
295 /* enter interior of triangle */
296 ix = startX;
297 count = 0;
298 while (coverage > 0.0F) {
299 /* (cx,cy) = center of fragment */
300 const GLfloat cx = ix + 0.5F, cy = iy + 0.5F;
301 SWspanarrays *array = span.array;
302 #ifdef DO_INDEX
303 array->coverage[ix] = (GLfloat) compute_coveragei(pMin, pMax, pMid, ix, iy);
304 #else
305 array->coverage[ix] = coverage;
306 #endif
307 #ifdef DO_Z
308 array->z[ix] = (GLuint) solve_plane(cx, cy, zPlane);
309 #endif
310 #ifdef DO_RGBA
311 array->rgba[ix][RCOMP] = solve_plane_chan(cx, cy, rPlane);
312 array->rgba[ix][GCOMP] = solve_plane_chan(cx, cy, gPlane);
313 array->rgba[ix][BCOMP] = solve_plane_chan(cx, cy, bPlane);
314 array->rgba[ix][ACOMP] = solve_plane_chan(cx, cy, aPlane);
315 #endif
316 #ifdef DO_INDEX
317 array->index[ix] = (GLint) solve_plane(cx, cy, iPlane);
318 #endif
319 ix--;
320 count++;
321 coverage = compute_coveragef(pMin, pMax, pMid, ix, iy);
322 }
323
324 #if defined(DO_ATTRIBS)
325 /* compute attributes at left-most fragment */
326 span.attrStart[FRAG_ATTRIB_WPOS][3] = solve_plane(ix + 1.5, iy + 0.5, wPlane);
327 ATTRIB_LOOP_BEGIN
328 GLuint c;
329 for (c = 0; c < 4; c++) {
330 span.attrStart[attr][c] = solve_plane(ix + 1.5, iy + 0.5, attrPlane[attr][c]);
331 }
332 ATTRIB_LOOP_END
333 #endif
334
335 if (startX <= ix)
336 continue;
337
338 n = (GLuint) startX - (GLuint) ix;
339
340 left = ix + 1;
341
342 /* shift all values to the left */
343 /* XXX this is temporary */
344 {
345 SWspanarrays *array = span.array;
346 GLint j;
347 for (j = 0; j < (GLint) n; j++) {
348 array->coverage[j] = array->coverage[j + left];
349 #ifdef DO_RGBA
350 COPY_CHAN4(array->rgba[j], array->rgba[j + left]);
351 #endif
352 #ifdef DO_INDEX
353 array->index[j] = array->index[j + left];
354 #endif
355 #ifdef DO_Z
356 array->z[j] = array->z[j + left];
357 #endif
358 }
359 }
360
361 span.x = left;
362 span.y = iy;
363 span.end = n;
364 #if defined(DO_RGBA)
365 _swrast_write_rgba_span(ctx, &span);
366 #else
367 _swrast_write_index_span(ctx, &span);
368 #endif
369 }
370 }
371 }
372
373
374 #undef DO_Z
375 #undef DO_RGBA
376 #undef DO_INDEX
377 #undef DO_ATTRIBS
378 #undef DO_OCCLUSION_TEST