s_aatritemp.h revision 08836341788a9f9d638d9dc8328510ccd18ddeb5
1/* $Id: s_aatritemp.h,v 1.6 2001/03/03 20:33:30 brianp Exp $ */ 2 3/* 4 * Mesa 3-D graphics library 5 * Version: 3.5 6 * 7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. 8 * 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: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 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. 25 */ 26 27 28/* 29 * Antialiased Triangle Rasterizer Template 30 * 31 * This file is #include'd to generate custom AA triangle rasterizers. 32 * NOTE: this code hasn't been optimized yet. That'll come after it 33 * works correctly. 34 * 35 * The following macros may be defined to indicate what auxillary information 36 * must be copmuted across the triangle: 37 * DO_Z - if defined, compute Z values 38 * DO_RGBA - if defined, compute RGBA values 39 * DO_INDEX - if defined, compute color index values 40 * DO_SPEC - if defined, compute specular RGB values 41 * DO_TEX - if defined, compute unit 0 STRQ texcoords 42 * DO_MULTITEX - if defined, compute all unit's STRQ texcoords 43 */ 44 45/*void triangle( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv )*/ 46{ 47 const GLfloat *p0 = v0->win; 48 const GLfloat *p1 = v1->win; 49 const GLfloat *p2 = v2->win; 50 const SWvertex *vMin, *vMid, *vMax; 51 GLint iyMin, iyMax; 52 GLfloat yMin, yMax; 53 GLboolean ltor; 54 GLfloat majDx, majDy; 55#ifdef DO_Z 56 GLfloat zPlane[4]; /* Z (depth) */ 57 GLdepth z[MAX_WIDTH]; 58 GLfloat fogPlane[4]; 59 GLfixed fog[MAX_WIDTH]; 60#endif 61#ifdef DO_RGBA 62 GLfloat rPlane[4], gPlane[4], bPlane[4], aPlane[4]; /* color */ 63 GLchan rgba[MAX_WIDTH][4]; 64#endif 65#ifdef DO_INDEX 66 GLfloat iPlane[4]; /* color index */ 67 GLuint index[MAX_WIDTH]; 68#endif 69#ifdef DO_SPEC 70 GLfloat srPlane[4], sgPlane[4], sbPlane[4]; /* spec color */ 71 GLchan spec[MAX_WIDTH][4]; 72#endif 73#ifdef DO_TEX 74 GLfloat sPlane[4], tPlane[4], uPlane[4], vPlane[4]; 75 GLfloat texWidth, texHeight; 76 GLfloat s[MAX_WIDTH], t[MAX_WIDTH], u[MAX_WIDTH]; 77 GLfloat lambda[MAX_WIDTH]; 78#elif defined(DO_MULTITEX) 79 GLfloat sPlane[MAX_TEXTURE_UNITS][4]; 80 GLfloat tPlane[MAX_TEXTURE_UNITS][4]; 81 GLfloat uPlane[MAX_TEXTURE_UNITS][4]; 82 GLfloat vPlane[MAX_TEXTURE_UNITS][4]; 83 GLfloat texWidth[MAX_TEXTURE_UNITS], texHeight[MAX_TEXTURE_UNITS]; 84 GLfloat s[MAX_TEXTURE_UNITS][MAX_WIDTH]; 85 GLfloat t[MAX_TEXTURE_UNITS][MAX_WIDTH]; 86 GLfloat u[MAX_TEXTURE_UNITS][MAX_WIDTH]; 87 GLfloat lambda[MAX_TEXTURE_UNITS][MAX_WIDTH]; 88#endif 89 GLfloat bf = SWRAST_CONTEXT(ctx)->_backface_sign; 90 91 /* determine bottom to top order of vertices */ 92 { 93 GLfloat y0 = v0->win[1]; 94 GLfloat y1 = v1->win[1]; 95 GLfloat y2 = v2->win[1]; 96 if (y0 <= y1) { 97 if (y1 <= y2) { 98 vMin = v0; vMid = v1; vMax = v2; /* y0<=y1<=y2 */ 99 } 100 else if (y2 <= y0) { 101 vMin = v2; vMid = v0; vMax = v1; /* y2<=y0<=y1 */ 102 } 103 else { 104 vMin = v0; vMid = v2; vMax = v1; bf = -bf; /* y0<=y2<=y1 */ 105 } 106 } 107 else { 108 if (y0 <= y2) { 109 vMin = v1; vMid = v0; vMax = v2; bf = -bf; /* y1<=y0<=y2 */ 110 } 111 else if (y2 <= y1) { 112 vMin = v2; vMid = v1; vMax = v0; bf = -bf; /* y2<=y1<=y0 */ 113 } 114 else { 115 vMin = v1; vMid = v2; vMax = v0; /* y1<=y2<=y0 */ 116 } 117 } 118 } 119 120 majDx = vMax->win[0] - vMin->win[0]; 121 majDy = vMax->win[1] - vMin->win[1]; 122 123 { 124 const GLfloat botDx = vMid->win[0] - vMin->win[0]; 125 const GLfloat botDy = vMid->win[1] - vMin->win[1]; 126 const GLfloat area = majDx * botDy - botDx * majDy; 127 ltor = (GLboolean) (area < 0.0F); 128 /* Do backface culling */ 129 if (area * bf < 0 || area * area < .0025) 130 return; 131 } 132 133#ifndef DO_OCCLUSION_TEST 134 ctx->OcclusionResult = GL_TRUE; 135#endif 136 137 /* plane setup */ 138#ifdef DO_Z 139 compute_plane(p0, p1, p2, p0[2], p1[2], p2[2], zPlane); 140 compute_plane(p0, p1, p2, 141 v0->fog, 142 v1->fog, 143 v2->fog, 144 fogPlane); 145#endif 146#ifdef DO_RGBA 147 if (ctx->Light.ShadeModel == GL_SMOOTH) { 148 compute_plane(p0, p1, p2, v0->color[0], v1->color[0], v2->color[0], rPlane); 149 compute_plane(p0, p1, p2, v0->color[1], v1->color[1], v2->color[1], gPlane); 150 compute_plane(p0, p1, p2, v0->color[2], v1->color[2], v2->color[2], bPlane); 151 compute_plane(p0, p1, p2, v0->color[3], v1->color[3], v2->color[3], aPlane); 152 } 153 else { 154 constant_plane(v2->color[RCOMP], rPlane); 155 constant_plane(v2->color[GCOMP], gPlane); 156 constant_plane(v2->color[BCOMP], bPlane); 157 constant_plane(v2->color[ACOMP], aPlane); 158 } 159#endif 160#ifdef DO_INDEX 161 if (ctx->Light.ShadeModel == GL_SMOOTH) { 162 compute_plane(p0, p1, p2, v0->index, 163 v1->index, v2->index, iPlane); 164 } 165 else { 166 constant_plane(v2->index, iPlane); 167 } 168#endif 169#ifdef DO_SPEC 170 if (ctx->Light.ShadeModel == GL_SMOOTH) { 171 compute_plane(p0, p1, p2, v0->specular[0], v1->specular[0], v2->specular[0],srPlane); 172 compute_plane(p0, p1, p2, v0->specular[1], v1->specular[1], v2->specular[1],sgPlane); 173 compute_plane(p0, p1, p2, v0->specular[2], v1->specular[2], v2->specular[2],sbPlane); 174 } 175 else { 176 /* KW: added this */ 177 constant_plane(v2->specular[RCOMP], srPlane); 178 constant_plane(v2->specular[GCOMP], sgPlane); 179 constant_plane(v2->specular[BCOMP], sbPlane); 180 } 181#endif 182#ifdef DO_TEX 183 { 184 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; 185 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel]; 186 const GLfloat invW0 = v0->win[3]; 187 const GLfloat invW1 = v1->win[3]; 188 const GLfloat invW2 = v2->win[3]; 189 const GLfloat s0 = v0->texcoord[0][0] * invW0; 190 const GLfloat s1 = v1->texcoord[0][0] * invW1; 191 const GLfloat s2 = v2->texcoord[0][0] * invW2; 192 const GLfloat t0 = v0->texcoord[0][1] * invW0; 193 const GLfloat t1 = v1->texcoord[0][1] * invW1; 194 const GLfloat t2 = v2->texcoord[0][1] * invW2; 195 const GLfloat r0 = v0->texcoord[0][2] * invW0; 196 const GLfloat r1 = v1->texcoord[0][2] * invW1; 197 const GLfloat r2 = v2->texcoord[0][2] * invW2; 198 const GLfloat q0 = v0->texcoord[0][3] * invW0; 199 const GLfloat q1 = v1->texcoord[0][3] * invW1; 200 const GLfloat q2 = v2->texcoord[0][3] * invW2; 201 compute_plane(p0, p1, p2, s0, s1, s2, sPlane); 202 compute_plane(p0, p1, p2, t0, t1, t2, tPlane); 203 compute_plane(p0, p1, p2, r0, r1, r2, uPlane); 204 compute_plane(p0, p1, p2, q0, q1, q2, vPlane); 205 texWidth = (GLfloat) texImage->Width; 206 texHeight = (GLfloat) texImage->Height; 207 } 208#elif defined(DO_MULTITEX) 209 { 210 GLuint u; 211 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { 212 if (ctx->Texture.Unit[u]._ReallyEnabled) { 213 const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current; 214 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel]; 215 const GLfloat invW0 = v0->win[3]; 216 const GLfloat invW1 = v1->win[3]; 217 const GLfloat invW2 = v2->win[3]; 218 const GLfloat s0 = v0->texcoord[u][0] * invW0; 219 const GLfloat s1 = v1->texcoord[u][0] * invW1; 220 const GLfloat s2 = v2->texcoord[u][0] * invW2; 221 const GLfloat t0 = v0->texcoord[u][1] * invW0; 222 const GLfloat t1 = v1->texcoord[u][1] * invW1; 223 const GLfloat t2 = v2->texcoord[u][1] * invW2; 224 const GLfloat r0 = v0->texcoord[u][2] * invW0; 225 const GLfloat r1 = v1->texcoord[u][2] * invW1; 226 const GLfloat r2 = v2->texcoord[u][2] * invW2; 227 const GLfloat q0 = v0->texcoord[u][3] * invW0; 228 const GLfloat q1 = v1->texcoord[u][3] * invW1; 229 const GLfloat q2 = v2->texcoord[u][3] * invW2; 230 compute_plane(p0, p1, p2, s0, s1, s2, sPlane[u]); 231 compute_plane(p0, p1, p2, t0, t1, t2, tPlane[u]); 232 compute_plane(p0, p1, p2, r0, r1, r2, uPlane[u]); 233 compute_plane(p0, p1, p2, q0, q1, q2, vPlane[u]); 234 texWidth[u] = (GLfloat) texImage->Width; 235 texHeight[u] = (GLfloat) texImage->Height; 236 } 237 } 238 } 239#endif 240 241 yMin = vMin->win[1]; 242 yMax = vMax->win[1]; 243 iyMin = (int) yMin; 244 iyMax = (int) yMax + 1; 245 246 if (ltor) { 247 /* scan left to right */ 248 const float *pMin = vMin->win; 249 const float *pMid = vMid->win; 250 const float *pMax = vMax->win; 251 const float dxdy = majDx / majDy; 252 const float xAdj = dxdy < 0.0F ? -dxdy : 0.0F; 253 float x = vMin->win[0] - (yMin - iyMin) * dxdy; 254 int iy; 255 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) { 256 GLint ix, startX = (GLint) (x - xAdj); 257 GLuint count, n; 258 GLfloat coverage = 0.0F; 259 /* skip over fragments with zero coverage */ 260 while (startX < MAX_WIDTH) { 261 coverage = compute_coveragef(pMin, pMid, pMax, startX, iy); 262 if (coverage > 0.0F) 263 break; 264 startX++; 265 } 266 267 /* enter interior of triangle */ 268 ix = startX; 269 count = 0; 270 while (coverage > 0.0F) { 271#ifdef DO_Z 272 z[count] = (GLdepth) solve_plane(ix, iy, zPlane); 273 fog[count] = FloatToFixed(solve_plane(ix, iy, fogPlane)); 274#endif 275#ifdef DO_RGBA 276 rgba[count][RCOMP] = solve_plane_chan(ix, iy, rPlane); 277 rgba[count][GCOMP] = solve_plane_chan(ix, iy, gPlane); 278 rgba[count][BCOMP] = solve_plane_chan(ix, iy, bPlane); 279 rgba[count][ACOMP] = (GLchan) (solve_plane_chan(ix, iy, aPlane) * coverage); 280#endif 281#ifdef DO_INDEX 282 { 283 GLint frac = compute_coveragei(pMin, pMid, pMax, ix, iy); 284 GLint indx = (GLint) solve_plane(ix, iy, iPlane); 285 index[count] = (indx & ~0xf) | frac; 286 } 287#endif 288#ifdef DO_SPEC 289 spec[count][RCOMP] = solve_plane_chan(ix, iy, srPlane); 290 spec[count][GCOMP] = solve_plane_chan(ix, iy, sgPlane); 291 spec[count][BCOMP] = solve_plane_chan(ix, iy, sbPlane); 292#endif 293#ifdef DO_TEX 294 { 295 GLfloat invQ = solve_plane_recip(ix, iy, vPlane); 296 s[count] = solve_plane(ix, iy, sPlane) * invQ; 297 t[count] = solve_plane(ix, iy, tPlane) * invQ; 298 u[count] = solve_plane(ix, iy, uPlane) * invQ; 299 lambda[count] = compute_lambda(sPlane, tPlane, invQ, 300 texWidth, texHeight); 301 } 302#elif defined(DO_MULTITEX) 303 { 304 GLuint unit; 305 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) { 306 if (ctx->Texture.Unit[unit]._ReallyEnabled) { 307 GLfloat invQ = solve_plane_recip(ix, iy, vPlane[unit]); 308 s[unit][count] = solve_plane(ix, iy, sPlane[unit]) * invQ; 309 t[unit][count] = solve_plane(ix, iy, tPlane[unit]) * invQ; 310 u[unit][count] = solve_plane(ix, iy, uPlane[unit]) * invQ; 311 lambda[unit][count] = compute_lambda(sPlane[unit], 312 tPlane[unit], invQ, texWidth[unit], texHeight[unit]); 313 } 314 } 315 } 316#endif 317 ix++; 318 count++; 319 coverage = compute_coveragef(pMin, pMid, pMax, ix, iy); 320 } 321 322 n = (GLuint) ix - (GLuint) startX; 323#ifdef DO_MULTITEX 324# ifdef DO_SPEC 325 _mesa_write_multitexture_span(ctx, n, startX, iy, z, fog, 326 (const GLfloat (*)[MAX_WIDTH]) s, 327 (const GLfloat (*)[MAX_WIDTH]) t, 328 (const GLfloat (*)[MAX_WIDTH]) u, 329 (GLfloat (*)[MAX_WIDTH]) lambda, 330 rgba, (const GLchan (*)[4]) spec, 331 GL_POLYGON); 332# else 333 _mesa_write_multitexture_span(ctx, n, startX, iy, z, fog, 334 (const GLfloat (*)[MAX_WIDTH]) s, 335 (const GLfloat (*)[MAX_WIDTH]) t, 336 (const GLfloat (*)[MAX_WIDTH]) u, 337 lambda, rgba, NULL, GL_POLYGON); 338# endif 339#elif defined(DO_TEX) 340# ifdef DO_SPEC 341 _mesa_write_texture_span(ctx, n, startX, iy, z, fog, 342 s, t, u, lambda, rgba, 343 (const GLchan (*)[4]) spec, GL_POLYGON); 344# else 345 _mesa_write_texture_span(ctx, n, startX, iy, z, fog, 346 s, t, u, lambda, 347 rgba, NULL, GL_POLYGON); 348# endif 349#elif defined(DO_RGBA) 350 _mesa_write_rgba_span(ctx, n, startX, iy, z, fog, rgba, GL_POLYGON); 351#elif defined(DO_INDEX) 352 _mesa_write_index_span(ctx, n, startX, iy, z, fog, index, GL_POLYGON); 353#endif 354 } 355 } 356 else { 357 /* scan right to left */ 358 const GLfloat *pMin = vMin->win; 359 const GLfloat *pMid = vMid->win; 360 const GLfloat *pMax = vMax->win; 361 const GLfloat dxdy = majDx / majDy; 362 const GLfloat xAdj = dxdy > 0 ? dxdy : 0.0F; 363 GLfloat x = vMin->win[0] - (yMin - iyMin) * dxdy; 364 GLint iy; 365 for (iy = iyMin; iy < iyMax; iy++, x += dxdy) { 366 GLint ix, left, startX = (GLint) (x + xAdj); 367 GLuint count, n; 368 GLfloat coverage = 0.0F; 369 /* skip fragments with zero coverage */ 370 while (startX >= 0) { 371 coverage = compute_coveragef(pMin, pMax, pMid, startX, iy); 372 if (coverage > 0.0F) 373 break; 374 startX--; 375 } 376 377 /* enter interior of triangle */ 378 ix = startX; 379 count = 0; 380 while (coverage > 0.0F) { 381#ifdef DO_Z 382 z[ix] = (GLdepth) solve_plane(ix, iy, zPlane); 383 fog[ix] = FloatToFixed(solve_plane(ix, iy, fogPlane)); 384#endif 385#ifdef DO_RGBA 386 rgba[ix][RCOMP] = solve_plane_chan(ix, iy, rPlane); 387 rgba[ix][GCOMP] = solve_plane_chan(ix, iy, gPlane); 388 rgba[ix][BCOMP] = solve_plane_chan(ix, iy, bPlane); 389 rgba[ix][ACOMP] = (GLchan) (solve_plane_chan(ix, iy, aPlane) * coverage); 390#endif 391#ifdef DO_INDEX 392 { 393 GLint frac = compute_coveragei(pMin, pMax, pMid, ix, iy); 394 GLint indx = (GLint) solve_plane(ix, iy, iPlane); 395 index[ix] = (indx & ~0xf) | frac; 396 } 397#endif 398#ifdef DO_SPEC 399 spec[ix][RCOMP] = solve_plane_chan(ix, iy, srPlane); 400 spec[ix][GCOMP] = solve_plane_chan(ix, iy, sgPlane); 401 spec[ix][BCOMP] = solve_plane_chan(ix, iy, sbPlane); 402#endif 403#ifdef DO_TEX 404 { 405 GLfloat invQ = solve_plane_recip(ix, iy, vPlane); 406 s[ix] = solve_plane(ix, iy, sPlane) * invQ; 407 t[ix] = solve_plane(ix, iy, tPlane) * invQ; 408 u[ix] = solve_plane(ix, iy, uPlane) * invQ; 409 lambda[ix] = compute_lambda(sPlane, tPlane, invQ, 410 texWidth, texHeight); 411 } 412#elif defined(DO_MULTITEX) 413 { 414 GLuint unit; 415 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) { 416 if (ctx->Texture.Unit[unit]._ReallyEnabled) { 417 GLfloat invQ = solve_plane_recip(ix, iy, vPlane[unit]); 418 s[unit][ix] = solve_plane(ix, iy, sPlane[unit]) * invQ; 419 t[unit][ix] = solve_plane(ix, iy, tPlane[unit]) * invQ; 420 u[unit][ix] = solve_plane(ix, iy, uPlane[unit]) * invQ; 421 lambda[unit][ix] = compute_lambda(sPlane[unit], 422 tPlane[unit], invQ, texWidth[unit], texHeight[unit]); 423 } 424 } 425 } 426#endif 427 ix--; 428 count++; 429 coverage = compute_coveragef(pMin, pMax, pMid, ix, iy); 430 } 431 432 n = (GLuint) startX - (GLuint) ix; 433 left = ix + 1; 434#ifdef DO_MULTITEX 435 { 436 GLuint unit; 437 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) { 438 if (ctx->Texture.Unit[unit]._ReallyEnabled) { 439 GLint j; 440 for (j = 0; j < n; j++) { 441 s[unit][j] = s[unit][j + left]; 442 t[unit][j] = t[unit][j + left]; 443 u[unit][j] = u[unit][j + left]; 444 lambda[unit][j] = lambda[unit][j + left]; 445 } 446 } 447 } 448 } 449# ifdef DO_SPEC 450 _mesa_write_multitexture_span(ctx, n, left, iy, z + left, fog + left, 451 (const GLfloat (*)[MAX_WIDTH]) s, 452 (const GLfloat (*)[MAX_WIDTH]) t, 453 (const GLfloat (*)[MAX_WIDTH]) u, 454 lambda, rgba + left, 455 (const GLchan (*)[4]) (spec + left), 456 GL_POLYGON); 457# else 458 _mesa_write_multitexture_span(ctx, n, left, iy, z + left, fog + left, 459 (const GLfloat (*)[MAX_WIDTH]) s, 460 (const GLfloat (*)[MAX_WIDTH]) t, 461 (const GLfloat (*)[MAX_WIDTH]) u, 462 lambda, 463 rgba + left, NULL, GL_POLYGON); 464# endif 465#elif defined(DO_TEX) 466# ifdef DO_SPEC 467 _mesa_write_texture_span(ctx, n, left, iy, z + left, fog + left, 468 s + left, t + left, u + left, 469 lambda + left, rgba + left, 470 (const GLchan (*)[4]) (spec + left), 471 GL_POLYGON); 472# else 473 _mesa_write_texture_span(ctx, n, left, iy, z + left, fog + left, 474 s + left, t + left, 475 u + left, lambda + left, 476 rgba + left, NULL, GL_POLYGON); 477# endif 478#elif defined(DO_RGBA) 479 _mesa_write_rgba_span(ctx, n, left, iy, z + left, fog + left, 480 rgba + left, GL_POLYGON); 481#elif defined(DO_INDEX) 482 _mesa_write_index_span(ctx, n, left, iy, z + left, fog + left, 483 index + left, GL_POLYGON); 484#endif 485 } 486 } 487} 488 489 490#ifdef DO_Z 491#undef DO_Z 492#endif 493 494#ifdef DO_RGBA 495#undef DO_RGBA 496#endif 497 498#ifdef DO_INDEX 499#undef DO_INDEX 500#endif 501 502#ifdef DO_SPEC 503#undef DO_SPEC 504#endif 505 506#ifdef DO_TEX 507#undef DO_TEX 508#endif 509 510#ifdef DO_MULTITEX 511#undef DO_MULTITEX 512#endif 513 514#ifdef DO_OCCLUSION_TEST 515#undef DO_OCCLUSION_TEST 516#endif 517