s_aatritemp.h revision 880411c72aee7c0ec81366bdf6ab8cf25bebb9d5
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.0.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)->_BackfaceCullSign; 69 70 (void) swrast; 71 72 INIT_SPAN(span, GL_POLYGON); 73 span.arrayMask = SPAN_COVERAGE; 74 75 /* determine bottom to top order of vertices */ 76 { 77 GLfloat y0 = v0->attrib[FRAG_ATTRIB_WPOS][1]; 78 GLfloat y1 = v1->attrib[FRAG_ATTRIB_WPOS][1]; 79 GLfloat y2 = v2->attrib[FRAG_ATTRIB_WPOS][1]; 80 if (y0 <= y1) { 81 if (y1 <= y2) { 82 vMin = v0; vMid = v1; vMax = v2; /* y0<=y1<=y2 */ 83 } 84 else if (y2 <= y0) { 85 vMin = v2; vMid = v0; vMax = v1; /* y2<=y0<=y1 */ 86 } 87 else { 88 vMin = v0; vMid = v2; vMax = v1; bf = -bf; /* y0<=y2<=y1 */ 89 } 90 } 91 else { 92 if (y0 <= y2) { 93 vMin = v1; vMid = v0; vMax = v2; bf = -bf; /* y1<=y0<=y2 */ 94 } 95 else if (y2 <= y1) { 96 vMin = v2; vMid = v1; vMax = v0; bf = -bf; /* y2<=y1<=y0 */ 97 } 98 else { 99 vMin = v1; vMid = v2; vMax = v0; /* y1<=y2<=y0 */ 100 } 101 } 102 } 103 104 majDx = vMax->attrib[FRAG_ATTRIB_WPOS][0] - vMin->attrib[FRAG_ATTRIB_WPOS][0]; 105 majDy = vMax->attrib[FRAG_ATTRIB_WPOS][1] - vMin->attrib[FRAG_ATTRIB_WPOS][1]; 106 107 /* front/back-face determination and cullling */ 108 { 109 const GLfloat botDx = vMid->attrib[FRAG_ATTRIB_WPOS][0] - vMin->attrib[FRAG_ATTRIB_WPOS][0]; 110 const GLfloat botDy = vMid->attrib[FRAG_ATTRIB_WPOS][1] - vMin->attrib[FRAG_ATTRIB_WPOS][1]; 111 const GLfloat area = majDx * botDy - botDx * majDy; 112 /* Do backface culling */ 113 if (area * bf < 0 || area == 0 || IS_INF_OR_NAN(area)) 114 return; 115 ltor = (GLboolean) (area < 0.0F); 116 117 span.facing = area * swrast->_BackfaceSign > 0.0F; 118 } 119 120 /* Plane equation setup: 121 * We evaluate plane equations at window (x,y) coordinates in order 122 * to compute color, Z, fog, texcoords, etc. This isn't terribly 123 * efficient but it's easy and reliable. 124 */ 125#ifdef DO_Z 126 compute_plane(p0, p1, p2, p0[2], p1[2], p2[2], zPlane); 127 span.arrayMask |= SPAN_Z; 128#endif 129#ifdef DO_RGBA 130 if (ctx->Light.ShadeModel == GL_SMOOTH) { 131 compute_plane(p0, p1, p2, v0->color[RCOMP], v1->color[RCOMP], v2->color[RCOMP], rPlane); 132 compute_plane(p0, p1, p2, v0->color[GCOMP], v1->color[GCOMP], v2->color[GCOMP], gPlane); 133 compute_plane(p0, p1, p2, v0->color[BCOMP], v1->color[BCOMP], v2->color[BCOMP], bPlane); 134 compute_plane(p0, p1, p2, v0->color[ACOMP], v1->color[ACOMP], v2->color[ACOMP], aPlane); 135 } 136 else { 137 constant_plane(v2->color[RCOMP], rPlane); 138 constant_plane(v2->color[GCOMP], gPlane); 139 constant_plane(v2->color[BCOMP], bPlane); 140 constant_plane(v2->color[ACOMP], aPlane); 141 } 142 span.arrayMask |= SPAN_RGBA; 143#endif 144#ifdef DO_INDEX 145 if (ctx->Light.ShadeModel == GL_SMOOTH) { 146 compute_plane(p0, p1, p2, (GLfloat) v0->attrib[FRAG_ATTRIB_CI][0], 147 v1->attrib[FRAG_ATTRIB_CI][0], v2->attrib[FRAG_ATTRIB_CI][0], iPlane); 148 } 149 else { 150 constant_plane(v2->attrib[FRAG_ATTRIB_CI][0], iPlane); 151 } 152 span.arrayMask |= SPAN_INDEX; 153#endif 154#if defined(DO_ATTRIBS) 155 { 156 const GLfloat invW0 = v0->attrib[FRAG_ATTRIB_WPOS][3]; 157 const GLfloat invW1 = v1->attrib[FRAG_ATTRIB_WPOS][3]; 158 const GLfloat invW2 = v2->attrib[FRAG_ATTRIB_WPOS][3]; 159 compute_plane(p0, p1, p2, invW0, invW1, invW2, wPlane); 160 span.attrStepX[FRAG_ATTRIB_WPOS][3] = plane_dx(wPlane); 161 span.attrStepY[FRAG_ATTRIB_WPOS][3] = plane_dy(wPlane); 162 ATTRIB_LOOP_BEGIN 163 GLuint c; 164 if (swrast->_InterpMode[attr] == GL_FLAT) { 165 for (c = 0; c < 4; c++) { 166 constant_plane(v2->attrib[attr][c] * invW2, attrPlane[attr][c]); 167 } 168 } 169 else { 170 for (c = 0; c < 4; c++) { 171 const GLfloat a0 = v0->attrib[attr][c] * invW0; 172 const GLfloat a1 = v1->attrib[attr][c] * invW1; 173 const GLfloat a2 = v2->attrib[attr][c] * invW2; 174 compute_plane(p0, p1, p2, a0, a1, a2, attrPlane[attr][c]); 175 } 176 } 177 for (c = 0; c < 4; c++) { 178 span.attrStepX[attr][c] = plane_dx(attrPlane[attr][c]); 179 span.attrStepY[attr][c] = plane_dy(attrPlane[attr][c]); 180 } 181 ATTRIB_LOOP_END 182 } 183#endif 184 185 /* Begin bottom-to-top scan over the triangle. 186 * The long edge will either be on the left or right side of the 187 * triangle. We always scan from the long edge toward the shorter 188 * edges, stopping when we find that coverage = 0. If the long edge 189 * is on the left we scan left-to-right. Else, we scan right-to-left. 190 */ 191 yMin = vMin->attrib[FRAG_ATTRIB_WPOS][1]; 192 yMax = vMax->attrib[FRAG_ATTRIB_WPOS][1]; 193 iyMin = (GLint) yMin; 194 iyMax = (GLint) yMax + 1; 195 196 if (ltor) { 197 /* scan left to right */ 198 const GLfloat *pMin = vMin->attrib[FRAG_ATTRIB_WPOS]; 199 const GLfloat *pMid = vMid->attrib[FRAG_ATTRIB_WPOS]; 200 const GLfloat *pMax = vMax->attrib[FRAG_ATTRIB_WPOS]; 201 const GLfloat dxdy = majDx / majDy; 202 const GLfloat xAdj = dxdy < 0.0F ? -dxdy : 0.0F; 203 GLfloat x = pMin[0] - (yMin - iyMin) * dxdy; 204 GLint iy; 205 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) { 206 GLint ix, startX = (GLint) (x - xAdj); 207 GLuint count; 208 GLfloat coverage = 0.0F; 209 210 /* skip over fragments with zero coverage */ 211 while (startX < MAX_WIDTH) { 212 coverage = compute_coveragef(pMin, pMid, pMax, startX, iy); 213 if (coverage > 0.0F) 214 break; 215 startX++; 216 } 217 218 /* enter interior of triangle */ 219 ix = startX; 220 221#if defined(DO_ATTRIBS) 222 /* compute attributes at left-most fragment */ 223 span.attrStart[FRAG_ATTRIB_WPOS][3] = solve_plane(ix + 0.5F, iy + 0.5F, wPlane); 224 ATTRIB_LOOP_BEGIN 225 GLuint c; 226 for (c = 0; c < 4; c++) { 227 span.attrStart[attr][c] = solve_plane(ix + 0.5F, iy + 0.5F, attrPlane[attr][c]); 228 } 229 ATTRIB_LOOP_END 230#endif 231 232 count = 0; 233 while (coverage > 0.0F) { 234 /* (cx,cy) = center of fragment */ 235 const GLfloat cx = ix + 0.5F, cy = iy + 0.5F; 236 SWspanarrays *array = span.array; 237#ifdef DO_INDEX 238 array->coverage[count] = (GLfloat) compute_coveragei(pMin, pMid, pMax, ix, iy); 239#else 240 array->coverage[count] = coverage; 241#endif 242#ifdef DO_Z 243 array->z[count] = (GLuint) solve_plane(cx, cy, zPlane); 244#endif 245#ifdef DO_RGBA 246 array->rgba[count][RCOMP] = solve_plane_chan(cx, cy, rPlane); 247 array->rgba[count][GCOMP] = solve_plane_chan(cx, cy, gPlane); 248 array->rgba[count][BCOMP] = solve_plane_chan(cx, cy, bPlane); 249 array->rgba[count][ACOMP] = solve_plane_chan(cx, cy, aPlane); 250#endif 251#ifdef DO_INDEX 252 array->index[count] = (GLint) solve_plane(cx, cy, iPlane); 253#endif 254 ix++; 255 count++; 256 coverage = compute_coveragef(pMin, pMid, pMax, ix, iy); 257 } 258 259 if (ix <= startX) 260 continue; 261 262 span.x = startX; 263 span.y = iy; 264 span.end = (GLuint) ix - (GLuint) startX; 265#if defined(DO_RGBA) 266 _swrast_write_rgba_span(ctx, &span); 267#else 268 _swrast_write_index_span(ctx, &span); 269#endif 270 } 271 } 272 else { 273 /* scan right to left */ 274 const GLfloat *pMin = vMin->attrib[FRAG_ATTRIB_WPOS]; 275 const GLfloat *pMid = vMid->attrib[FRAG_ATTRIB_WPOS]; 276 const GLfloat *pMax = vMax->attrib[FRAG_ATTRIB_WPOS]; 277 const GLfloat dxdy = majDx / majDy; 278 const GLfloat xAdj = dxdy > 0 ? dxdy : 0.0F; 279 GLfloat x = pMin[0] - (yMin - iyMin) * dxdy; 280 GLint iy; 281 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) { 282 GLint ix, left, startX = (GLint) (x + xAdj); 283 GLuint count, n; 284 GLfloat coverage = 0.0F; 285 286 /* make sure we're not past the window edge */ 287 if (startX >= ctx->DrawBuffer->_Xmax) { 288 startX = ctx->DrawBuffer->_Xmax - 1; 289 } 290 291 /* skip fragments with zero coverage */ 292 while (startX > 0) { 293 coverage = compute_coveragef(pMin, pMax, pMid, startX, iy); 294 if (coverage > 0.0F) 295 break; 296 startX--; 297 } 298 299 /* enter interior of triangle */ 300 ix = startX; 301 count = 0; 302 while (coverage > 0.0F) { 303 /* (cx,cy) = center of fragment */ 304 const GLfloat cx = ix + 0.5F, cy = iy + 0.5F; 305 SWspanarrays *array = span.array; 306 ASSERT(ix >= 0); 307#ifdef DO_INDEX 308 array->coverage[ix] = (GLfloat) compute_coveragei(pMin, pMax, pMid, ix, iy); 309#else 310 array->coverage[ix] = coverage; 311#endif 312#ifdef DO_Z 313 array->z[ix] = (GLuint) solve_plane(cx, cy, zPlane); 314#endif 315#ifdef DO_RGBA 316 array->rgba[ix][RCOMP] = solve_plane_chan(cx, cy, rPlane); 317 array->rgba[ix][GCOMP] = solve_plane_chan(cx, cy, gPlane); 318 array->rgba[ix][BCOMP] = solve_plane_chan(cx, cy, bPlane); 319 array->rgba[ix][ACOMP] = solve_plane_chan(cx, cy, aPlane); 320#endif 321#ifdef DO_INDEX 322 array->index[ix] = (GLint) solve_plane(cx, cy, iPlane); 323#endif 324 ix--; 325 count++; 326 coverage = compute_coveragef(pMin, pMax, pMid, ix, iy); 327 } 328 329#if defined(DO_ATTRIBS) 330 /* compute attributes at left-most fragment */ 331 span.attrStart[FRAG_ATTRIB_WPOS][3] = solve_plane(ix + 1.5, iy + 0.5F, wPlane); 332 ATTRIB_LOOP_BEGIN 333 GLuint c; 334 for (c = 0; c < 4; c++) { 335 span.attrStart[attr][c] = solve_plane(ix + 1.5, iy + 0.5F, attrPlane[attr][c]); 336 } 337 ATTRIB_LOOP_END 338#endif 339 340 if (startX <= ix) 341 continue; 342 343 n = (GLuint) startX - (GLuint) ix; 344 345 left = ix + 1; 346 347 /* shift all values to the left */ 348 /* XXX this is temporary */ 349 { 350 SWspanarrays *array = span.array; 351 GLint j; 352 for (j = 0; j < (GLint) n; j++) { 353 array->coverage[j] = array->coverage[j + left]; 354#ifdef DO_RGBA 355 COPY_CHAN4(array->rgba[j], array->rgba[j + left]); 356#endif 357#ifdef DO_INDEX 358 array->index[j] = array->index[j + left]; 359#endif 360#ifdef DO_Z 361 array->z[j] = array->z[j + left]; 362#endif 363 } 364 } 365 366 span.x = left; 367 span.y = iy; 368 span.end = n; 369#if defined(DO_RGBA) 370 _swrast_write_rgba_span(ctx, &span); 371#else 372 _swrast_write_index_span(ctx, &span); 373#endif 374 } 375 } 376} 377 378 379#undef DO_Z 380#undef DO_RGBA 381#undef DO_INDEX 382#undef DO_ATTRIBS 383#undef DO_OCCLUSION_TEST 384