s_triangle.c revision 47b29f511a8e917c65536fde90397d54d2ad23d3
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.3 4 * 5 * Copyright (C) 1999-2004 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 * When the device driver doesn't implement triangle rasterization it 28 * can hook in _swrast_Triangle, which eventually calls one of these 29 * functions to draw triangles. 30 */ 31 32#include "glheader.h" 33#include "context.h" 34#include "colormac.h" 35#include "imports.h" 36#include "macros.h" 37#include "texformat.h" 38#include "teximage.h" 39#include "texstate.h" 40 41#include "s_aatriangle.h" 42#include "s_context.h" 43#include "s_depth.h" 44#include "s_feedback.h" 45#include "s_span.h" 46#include "s_triangle.h" 47 48 49/* 50 * Just used for feedback mode. 51 */ 52GLboolean 53_swrast_culltriangle( GLcontext *ctx, 54 const SWvertex *v0, 55 const SWvertex *v1, 56 const SWvertex *v2 ) 57{ 58 GLfloat ex = v1->win[0] - v0->win[0]; 59 GLfloat ey = v1->win[1] - v0->win[1]; 60 GLfloat fx = v2->win[0] - v0->win[0]; 61 GLfloat fy = v2->win[1] - v0->win[1]; 62 GLfloat c = ex*fy-ey*fx; 63 64 if (c * SWRAST_CONTEXT(ctx)->_BackfaceSign > 0) 65 return 0; 66 67 return 1; 68} 69 70 71 72/* 73 * Render a flat-shaded color index triangle. 74 */ 75#define NAME flat_ci_triangle 76#define INTERP_Z 1 77#define INTERP_FOG 1 78#define SETUP_CODE \ 79 span.interpMask |= SPAN_INDEX; \ 80 span.index = FloatToFixed(v2->index);\ 81 span.indexStep = 0; 82#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span); 83#include "s_tritemp.h" 84 85 86 87/* 88 * Render a smooth-shaded color index triangle. 89 */ 90#define NAME smooth_ci_triangle 91#define INTERP_Z 1 92#define INTERP_FOG 1 93#define INTERP_INDEX 1 94#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span); 95#include "s_tritemp.h" 96 97 98 99/* 100 * Render a flat-shaded RGBA triangle. 101 */ 102#define NAME flat_rgba_triangle 103#define INTERP_Z 1 104#define INTERP_FOG 1 105#define SETUP_CODE \ 106 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\ 107 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \ 108 span.interpMask |= SPAN_RGBA; \ 109 span.red = ChanToFixed(v2->color[0]); \ 110 span.green = ChanToFixed(v2->color[1]); \ 111 span.blue = ChanToFixed(v2->color[2]); \ 112 span.alpha = ChanToFixed(v2->color[3]); \ 113 span.redStep = 0; \ 114 span.greenStep = 0; \ 115 span.blueStep = 0; \ 116 span.alphaStep = 0; 117#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span); 118#include "s_tritemp.h" 119 120 121 122/* 123 * Render a smooth-shaded RGBA triangle. 124 */ 125#define NAME smooth_rgba_triangle 126#define INTERP_Z 1 127#define INTERP_FOG 1 128#define INTERP_RGB 1 129#define INTERP_ALPHA 1 130#define SETUP_CODE \ 131 { \ 132 /* texturing must be off */ \ 133 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \ 134 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \ 135 } 136#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span); 137#include "s_tritemp.h" 138 139 140 141/* 142 * Render an RGB, GL_DECAL, textured triangle. 143 * Interpolate S,T only w/out mipmapping or perspective correction. 144 * 145 * No fog. 146 */ 147#define NAME simple_textured_triangle 148#define INTERP_INT_TEX 1 149#define S_SCALE twidth 150#define T_SCALE theight 151 152#define SETUP_CODE \ 153 SWcontext *swrast = SWRAST_CONTEXT(ctx); \ 154 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \ 155 const GLint b = obj->BaseLevel; \ 156 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \ 157 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \ 158 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \ 159 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \ 160 const GLint smask = obj->Image[0][b]->Width - 1; \ 161 const GLint tmask = obj->Image[0][b]->Height - 1; \ 162 if (!texture) { \ 163 /* this shouldn't happen */ \ 164 return; \ 165 } 166 167#define RENDER_SPAN( span ) \ 168 GLuint i; \ 169 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \ 170 span.intTex[1] -= FIXED_HALF; \ 171 for (i = 0; i < span.end; i++) { \ 172 GLint s = FixedToInt(span.intTex[0]) & smask; \ 173 GLint t = FixedToInt(span.intTex[1]) & tmask; \ 174 GLint pos = (t << twidth_log2) + s; \ 175 pos = pos + pos + pos; /* multiply by 3 */ \ 176 span.array->rgb[i][RCOMP] = texture[pos]; \ 177 span.array->rgb[i][GCOMP] = texture[pos+1]; \ 178 span.array->rgb[i][BCOMP] = texture[pos+2]; \ 179 span.intTex[0] += span.intTexStep[0]; \ 180 span.intTex[1] += span.intTexStep[1]; \ 181 } \ 182 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \ 183 (CONST GLchan (*)[3]) span.array->rgb,\ 184 NULL ); 185#include "s_tritemp.h" 186 187 188 189/* 190 * Render an RGB, GL_DECAL, textured triangle. 191 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or 192 * perspective correction. 193 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE) 194 * 195 * No fog. 196 */ 197#define NAME simple_z_textured_triangle 198#define INTERP_Z 1 199#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 200#define INTERP_INT_TEX 1 201#define S_SCALE twidth 202#define T_SCALE theight 203 204#define SETUP_CODE \ 205 SWcontext *swrast = SWRAST_CONTEXT(ctx); \ 206 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \ 207 const GLint b = obj->BaseLevel; \ 208 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \ 209 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \ 210 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \ 211 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \ 212 const GLint smask = obj->Image[0][b]->Width - 1; \ 213 const GLint tmask = obj->Image[0][b]->Height - 1; \ 214 if (!texture) { \ 215 /* this shouldn't happen */ \ 216 return; \ 217 } 218 219#define RENDER_SPAN( span ) \ 220 GLuint i; \ 221 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \ 222 span.intTex[1] -= FIXED_HALF; \ 223 for (i = 0; i < span.end; i++) { \ 224 const GLdepth z = FixedToDepth(span.z); \ 225 if (z < zRow[i]) { \ 226 GLint s = FixedToInt(span.intTex[0]) & smask; \ 227 GLint t = FixedToInt(span.intTex[1]) & tmask; \ 228 GLint pos = (t << twidth_log2) + s; \ 229 pos = pos + pos + pos; /* multiply by 3 */ \ 230 span.array->rgb[i][RCOMP] = texture[pos]; \ 231 span.array->rgb[i][GCOMP] = texture[pos+1]; \ 232 span.array->rgb[i][BCOMP] = texture[pos+2]; \ 233 zRow[i] = z; \ 234 span.array->mask[i] = 1; \ 235 } \ 236 else { \ 237 span.array->mask[i] = 0; \ 238 } \ 239 span.intTex[0] += span.intTexStep[0]; \ 240 span.intTex[1] += span.intTexStep[1]; \ 241 span.z += span.zStep; \ 242 } \ 243 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \ 244 (CONST GLchan (*)[3]) span.array->rgb,\ 245 span.array->mask ); 246#include "s_tritemp.h" 247 248 249 250#if CHAN_TYPE != GL_FLOAT 251 252struct affine_info 253{ 254 GLenum filter; 255 GLenum format; 256 GLenum envmode; 257 GLint smask, tmask; 258 GLint twidth_log2; 259 const GLchan *texture; 260 GLfixed er, eg, eb, ea; 261 GLint tbytesline, tsize; 262}; 263 264 265/* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA 266 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD 267 * texture env modes. 268 */ 269static INLINE void 270affine_span(GLcontext *ctx, struct sw_span *span, 271 struct affine_info *info) 272{ 273 GLchan sample[4]; /* the filtered texture sample */ 274 275 /* Instead of defining a function for each mode, a test is done 276 * between the outer and inner loops. This is to reduce code size 277 * and complexity. Observe that an optimizing compiler kills 278 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST). 279 */ 280 281#define NEAREST_RGB \ 282 sample[RCOMP] = tex00[RCOMP]; \ 283 sample[GCOMP] = tex00[GCOMP]; \ 284 sample[BCOMP] = tex00[BCOMP]; \ 285 sample[ACOMP] = CHAN_MAX 286 287#define LINEAR_RGB \ 288 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \ 289 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \ 290 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \ 291 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \ 292 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \ 293 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \ 294 sample[ACOMP] = CHAN_MAX 295 296#define NEAREST_RGBA COPY_CHAN4(sample, tex00) 297 298#define LINEAR_RGBA \ 299 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \ 300 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\ 301 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \ 302 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\ 303 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \ 304 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\ 305 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \ 306 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT 307 308#define MODULATE \ 309 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \ 310 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \ 311 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \ 312 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8) 313 314#define DECAL \ 315 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \ 316 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \ 317 >> (FIXED_SHIFT + 8); \ 318 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \ 319 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \ 320 >> (FIXED_SHIFT + 8); \ 321 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \ 322 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \ 323 >> (FIXED_SHIFT + 8); \ 324 dest[ACOMP] = FixedToInt(span->alpha) 325 326#define BLEND \ 327 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \ 328 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \ 329 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \ 330 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \ 331 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \ 332 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \ 333 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8) 334 335#define REPLACE COPY_CHAN4(dest, sample) 336 337#define ADD \ 338 { \ 339 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \ 340 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \ 341 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \ 342 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \ 343 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \ 344 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \ 345 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \ 346 } 347 348/* shortcuts */ 349 350#define NEAREST_RGB_REPLACE \ 351 NEAREST_RGB; \ 352 dest[0] = sample[0]; \ 353 dest[1] = sample[1]; \ 354 dest[2] = sample[2]; \ 355 dest[3] = FixedToInt(span->alpha); 356 357#define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00) 358 359#define SPAN_NEAREST(DO_TEX,COMP) \ 360 for (i = 0; i < span->end; i++) { \ 361 /* Isn't it necessary to use FixedFloor below?? */ \ 362 GLint s = FixedToInt(span->intTex[0]) & info->smask; \ 363 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \ 364 GLint pos = (t << info->twidth_log2) + s; \ 365 const GLchan *tex00 = info->texture + COMP * pos; \ 366 DO_TEX; \ 367 span->red += span->redStep; \ 368 span->green += span->greenStep; \ 369 span->blue += span->blueStep; \ 370 span->alpha += span->alphaStep; \ 371 span->intTex[0] += span->intTexStep[0]; \ 372 span->intTex[1] += span->intTexStep[1]; \ 373 dest += 4; \ 374 } 375 376#define SPAN_LINEAR(DO_TEX,COMP) \ 377 for (i = 0; i < span->end; i++) { \ 378 /* Isn't it necessary to use FixedFloor below?? */ \ 379 GLint s = FixedToInt(span->intTex[0]) & info->smask; \ 380 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \ 381 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \ 382 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \ 383 GLfixed si = FIXED_FRAC_MASK - sf; \ 384 GLfixed ti = FIXED_FRAC_MASK - tf; \ 385 GLint pos = (t << info->twidth_log2) + s; \ 386 const GLchan *tex00 = info->texture + COMP * pos; \ 387 const GLchan *tex10 = tex00 + info->tbytesline; \ 388 const GLchan *tex01 = tex00 + COMP; \ 389 const GLchan *tex11 = tex10 + COMP; \ 390 (void) ti; \ 391 (void) si; \ 392 if (t == info->tmask) { \ 393 tex10 -= info->tsize; \ 394 tex11 -= info->tsize; \ 395 } \ 396 if (s == info->smask) { \ 397 tex01 -= info->tbytesline; \ 398 tex11 -= info->tbytesline; \ 399 } \ 400 DO_TEX; \ 401 span->red += span->redStep; \ 402 span->green += span->greenStep; \ 403 span->blue += span->blueStep; \ 404 span->alpha += span->alphaStep; \ 405 span->intTex[0] += span->intTexStep[0]; \ 406 span->intTex[1] += span->intTexStep[1]; \ 407 dest += 4; \ 408 } 409 410 411 GLuint i; 412 GLchan *dest = span->array->rgba[0]; 413 414 span->intTex[0] -= FIXED_HALF; 415 span->intTex[1] -= FIXED_HALF; 416 switch (info->filter) { 417 case GL_NEAREST: 418 switch (info->format) { 419 case GL_RGB: 420 switch (info->envmode) { 421 case GL_MODULATE: 422 SPAN_NEAREST(NEAREST_RGB;MODULATE,3); 423 break; 424 case GL_DECAL: 425 case GL_REPLACE: 426 SPAN_NEAREST(NEAREST_RGB_REPLACE,3); 427 break; 428 case GL_BLEND: 429 SPAN_NEAREST(NEAREST_RGB;BLEND,3); 430 break; 431 case GL_ADD: 432 SPAN_NEAREST(NEAREST_RGB;ADD,3); 433 break; 434 default: 435 _mesa_problem(ctx, "bad tex env mode in SPAN_LINEAR"); 436 return; 437 } 438 break; 439 case GL_RGBA: 440 switch(info->envmode) { 441 case GL_MODULATE: 442 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4); 443 break; 444 case GL_DECAL: 445 SPAN_NEAREST(NEAREST_RGBA;DECAL,4); 446 break; 447 case GL_BLEND: 448 SPAN_NEAREST(NEAREST_RGBA;BLEND,4); 449 break; 450 case GL_ADD: 451 SPAN_NEAREST(NEAREST_RGBA;ADD,4); 452 break; 453 case GL_REPLACE: 454 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4); 455 break; 456 default: 457 _mesa_problem(ctx, "bad tex env mode (2) in SPAN_LINEAR"); 458 return; 459 } 460 break; 461 } 462 break; 463 464 case GL_LINEAR: 465 span->intTex[0] -= FIXED_HALF; 466 span->intTex[1] -= FIXED_HALF; 467 switch (info->format) { 468 case GL_RGB: 469 switch (info->envmode) { 470 case GL_MODULATE: 471 SPAN_LINEAR(LINEAR_RGB;MODULATE,3); 472 break; 473 case GL_DECAL: 474 case GL_REPLACE: 475 SPAN_LINEAR(LINEAR_RGB;REPLACE,3); 476 break; 477 case GL_BLEND: 478 SPAN_LINEAR(LINEAR_RGB;BLEND,3); 479 break; 480 case GL_ADD: 481 SPAN_LINEAR(LINEAR_RGB;ADD,3); 482 break; 483 default: 484 _mesa_problem(ctx, "bad tex env mode (3) in SPAN_LINEAR"); 485 return; 486 } 487 break; 488 case GL_RGBA: 489 switch (info->envmode) { 490 case GL_MODULATE: 491 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4); 492 break; 493 case GL_DECAL: 494 SPAN_LINEAR(LINEAR_RGBA;DECAL,4); 495 break; 496 case GL_BLEND: 497 SPAN_LINEAR(LINEAR_RGBA;BLEND,4); 498 break; 499 case GL_ADD: 500 SPAN_LINEAR(LINEAR_RGBA;ADD,4); 501 break; 502 case GL_REPLACE: 503 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4); 504 break; 505 default: 506 _mesa_problem(ctx, "bad tex env mode (4) in SPAN_LINEAR"); 507 return; 508 } 509 break; 510 } 511 break; 512 } 513 span->interpMask &= ~SPAN_RGBA; 514 ASSERT(span->arrayMask & SPAN_RGBA); 515 _swrast_write_rgba_span(ctx, span); 516 517#undef SPAN_NEAREST 518#undef SPAN_LINEAR 519} 520 521 522 523/* 524 * Render an RGB/RGBA textured triangle without perspective correction. 525 */ 526#define NAME affine_textured_triangle 527#define INTERP_Z 1 528#define INTERP_FOG 1 529#define INTERP_RGB 1 530#define INTERP_ALPHA 1 531#define INTERP_INT_TEX 1 532#define S_SCALE twidth 533#define T_SCALE theight 534 535#define SETUP_CODE \ 536 struct affine_info info; \ 537 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \ 538 struct gl_texture_object *obj = unit->Current2D; \ 539 const GLint b = obj->BaseLevel; \ 540 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \ 541 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \ 542 info.texture = (const GLchan *) obj->Image[0][b]->Data; \ 543 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \ 544 info.smask = obj->Image[0][b]->Width - 1; \ 545 info.tmask = obj->Image[0][b]->Height - 1; \ 546 info.format = obj->Image[0][b]->Format; \ 547 info.filter = obj->MinFilter; \ 548 info.envmode = unit->EnvMode; \ 549 span.arrayMask |= SPAN_RGBA; \ 550 \ 551 if (info.envmode == GL_BLEND) { \ 552 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \ 553 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \ 554 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \ 555 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \ 556 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \ 557 } \ 558 if (!info.texture) { \ 559 /* this shouldn't happen */ \ 560 return; \ 561 } \ 562 \ 563 switch (info.format) { \ 564 case GL_ALPHA: \ 565 case GL_LUMINANCE: \ 566 case GL_INTENSITY: \ 567 info.tbytesline = obj->Image[0][b]->Width; \ 568 break; \ 569 case GL_LUMINANCE_ALPHA: \ 570 info.tbytesline = obj->Image[0][b]->Width * 2; \ 571 break; \ 572 case GL_RGB: \ 573 info.tbytesline = obj->Image[0][b]->Width * 3; \ 574 break; \ 575 case GL_RGBA: \ 576 info.tbytesline = obj->Image[0][b]->Width * 4; \ 577 break; \ 578 default: \ 579 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\ 580 return; \ 581 } \ 582 info.tsize = obj->Image[0][b]->Height * info.tbytesline; 583 584#define RENDER_SPAN( span ) affine_span(ctx, &span, &info); 585 586#include "s_tritemp.h" 587 588 589 590struct persp_info 591{ 592 GLenum filter; 593 GLenum format; 594 GLenum envmode; 595 GLint smask, tmask; 596 GLint twidth_log2; 597 const GLchan *texture; 598 GLfixed er, eg, eb, ea; /* texture env color */ 599 GLint tbytesline, tsize; 600}; 601 602 603static INLINE void 604fast_persp_span(GLcontext *ctx, struct sw_span *span, 605 struct persp_info *info) 606{ 607 GLchan sample[4]; /* the filtered texture sample */ 608 609 /* Instead of defining a function for each mode, a test is done 610 * between the outer and inner loops. This is to reduce code size 611 * and complexity. Observe that an optimizing compiler kills 612 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST). 613 */ 614#define SPAN_NEAREST(DO_TEX,COMP) \ 615 for (i = 0; i < span->end; i++) { \ 616 GLdouble invQ = tex_coord[2] ? \ 617 (1.0 / tex_coord[2]) : 1.0; \ 618 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \ 619 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \ 620 GLint s = IFLOOR(s_tmp) & info->smask; \ 621 GLint t = IFLOOR(t_tmp) & info->tmask; \ 622 GLint pos = (t << info->twidth_log2) + s; \ 623 const GLchan *tex00 = info->texture + COMP * pos; \ 624 DO_TEX; \ 625 span->red += span->redStep; \ 626 span->green += span->greenStep; \ 627 span->blue += span->blueStep; \ 628 span->alpha += span->alphaStep; \ 629 tex_coord[0] += tex_step[0]; \ 630 tex_coord[1] += tex_step[1]; \ 631 tex_coord[2] += tex_step[2]; \ 632 dest += 4; \ 633 } 634 635#define SPAN_LINEAR(DO_TEX,COMP) \ 636 for (i = 0; i < span->end; i++) { \ 637 GLdouble invQ = tex_coord[2] ? \ 638 (1.0 / tex_coord[2]) : 1.0; \ 639 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \ 640 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \ 641 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \ 642 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \ 643 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \ 644 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \ 645 GLfixed sf = s_fix & FIXED_FRAC_MASK; \ 646 GLfixed tf = t_fix & FIXED_FRAC_MASK; \ 647 GLfixed si = FIXED_FRAC_MASK - sf; \ 648 GLfixed ti = FIXED_FRAC_MASK - tf; \ 649 GLint pos = (t << info->twidth_log2) + s; \ 650 const GLchan *tex00 = info->texture + COMP * pos; \ 651 const GLchan *tex10 = tex00 + info->tbytesline; \ 652 const GLchan *tex01 = tex00 + COMP; \ 653 const GLchan *tex11 = tex10 + COMP; \ 654 (void) ti; \ 655 (void) si; \ 656 if (t == info->tmask) { \ 657 tex10 -= info->tsize; \ 658 tex11 -= info->tsize; \ 659 } \ 660 if (s == info->smask) { \ 661 tex01 -= info->tbytesline; \ 662 tex11 -= info->tbytesline; \ 663 } \ 664 DO_TEX; \ 665 span->red += span->redStep; \ 666 span->green += span->greenStep; \ 667 span->blue += span->blueStep; \ 668 span->alpha += span->alphaStep; \ 669 tex_coord[0] += tex_step[0]; \ 670 tex_coord[1] += tex_step[1]; \ 671 tex_coord[2] += tex_step[2]; \ 672 dest += 4; \ 673 } 674 675 GLuint i; 676 GLfloat tex_coord[3], tex_step[3]; 677 GLchan *dest = span->array->rgba[0]; 678 679 const GLuint savedTexEnable = ctx->Texture._EnabledUnits; 680 ctx->Texture._EnabledUnits = 0; 681 682 tex_coord[0] = span->tex[0][0] * (info->smask + 1); 683 tex_step[0] = span->texStepX[0][0] * (info->smask + 1); 684 tex_coord[1] = span->tex[0][1] * (info->tmask + 1); 685 tex_step[1] = span->texStepX[0][1] * (info->tmask + 1); 686 /* span->tex[0][2] only if 3D-texturing, here only 2D */ 687 tex_coord[2] = span->tex[0][3]; 688 tex_step[2] = span->texStepX[0][3]; 689 690 switch (info->filter) { 691 case GL_NEAREST: 692 switch (info->format) { 693 case GL_RGB: 694 switch (info->envmode) { 695 case GL_MODULATE: 696 SPAN_NEAREST(NEAREST_RGB;MODULATE,3); 697 break; 698 case GL_DECAL: 699 case GL_REPLACE: 700 SPAN_NEAREST(NEAREST_RGB_REPLACE,3); 701 break; 702 case GL_BLEND: 703 SPAN_NEAREST(NEAREST_RGB;BLEND,3); 704 break; 705 case GL_ADD: 706 SPAN_NEAREST(NEAREST_RGB;ADD,3); 707 break; 708 default: 709 _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR"); 710 return; 711 } 712 break; 713 case GL_RGBA: 714 switch(info->envmode) { 715 case GL_MODULATE: 716 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4); 717 break; 718 case GL_DECAL: 719 SPAN_NEAREST(NEAREST_RGBA;DECAL,4); 720 break; 721 case GL_BLEND: 722 SPAN_NEAREST(NEAREST_RGBA;BLEND,4); 723 break; 724 case GL_ADD: 725 SPAN_NEAREST(NEAREST_RGBA;ADD,4); 726 break; 727 case GL_REPLACE: 728 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4); 729 break; 730 default: 731 _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR"); 732 return; 733 } 734 break; 735 } 736 break; 737 738 case GL_LINEAR: 739 switch (info->format) { 740 case GL_RGB: 741 switch (info->envmode) { 742 case GL_MODULATE: 743 SPAN_LINEAR(LINEAR_RGB;MODULATE,3); 744 break; 745 case GL_DECAL: 746 case GL_REPLACE: 747 SPAN_LINEAR(LINEAR_RGB;REPLACE,3); 748 break; 749 case GL_BLEND: 750 SPAN_LINEAR(LINEAR_RGB;BLEND,3); 751 break; 752 case GL_ADD: 753 SPAN_LINEAR(LINEAR_RGB;ADD,3); 754 break; 755 default: 756 _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR"); 757 return; 758 } 759 break; 760 case GL_RGBA: 761 switch (info->envmode) { 762 case GL_MODULATE: 763 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4); 764 break; 765 case GL_DECAL: 766 SPAN_LINEAR(LINEAR_RGBA;DECAL,4); 767 break; 768 case GL_BLEND: 769 SPAN_LINEAR(LINEAR_RGBA;BLEND,4); 770 break; 771 case GL_ADD: 772 SPAN_LINEAR(LINEAR_RGBA;ADD,4); 773 break; 774 case GL_REPLACE: 775 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4); 776 break; 777 default: 778 _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR"); 779 return; 780 } 781 break; 782 } 783 break; 784 } 785 786 ASSERT(span->arrayMask & SPAN_RGBA); 787 _swrast_write_rgba_span(ctx, span); 788 789#undef SPAN_NEAREST 790#undef SPAN_LINEAR 791 792 /* restore state */ 793 ctx->Texture._EnabledUnits = savedTexEnable; 794} 795 796 797/* 798 * Render an perspective corrected RGB/RGBA textured triangle. 799 * The Q (aka V in Mesa) coordinate must be zero such that the divide 800 * by interpolated Q/W comes out right. 801 * 802 */ 803#define NAME persp_textured_triangle 804#define INTERP_Z 1 805#define INTERP_W 1 806#define INTERP_FOG 1 807#define INTERP_RGB 1 808#define INTERP_ALPHA 1 809#define INTERP_TEX 1 810 811#define SETUP_CODE \ 812 struct persp_info info; \ 813 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \ 814 const struct gl_texture_object *obj = unit->Current2D; \ 815 const GLint b = obj->BaseLevel; \ 816 info.texture = (const GLchan *) obj->Image[0][b]->Data; \ 817 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \ 818 info.smask = obj->Image[0][b]->Width - 1; \ 819 info.tmask = obj->Image[0][b]->Height - 1; \ 820 info.format = obj->Image[0][b]->Format; \ 821 info.filter = obj->MinFilter; \ 822 info.envmode = unit->EnvMode; \ 823 \ 824 if (info.envmode == GL_BLEND) { \ 825 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \ 826 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \ 827 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \ 828 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \ 829 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \ 830 } \ 831 if (!info.texture) { \ 832 /* this shouldn't happen */ \ 833 return; \ 834 } \ 835 \ 836 switch (info.format) { \ 837 case GL_ALPHA: \ 838 case GL_LUMINANCE: \ 839 case GL_INTENSITY: \ 840 info.tbytesline = obj->Image[0][b]->Width; \ 841 break; \ 842 case GL_LUMINANCE_ALPHA: \ 843 info.tbytesline = obj->Image[0][b]->Width * 2; \ 844 break; \ 845 case GL_RGB: \ 846 info.tbytesline = obj->Image[0][b]->Width * 3; \ 847 break; \ 848 case GL_RGBA: \ 849 info.tbytesline = obj->Image[0][b]->Width * 4; \ 850 break; \ 851 default: \ 852 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\ 853 return; \ 854 } \ 855 info.tsize = obj->Image[0][b]->Height * info.tbytesline; 856 857#define RENDER_SPAN( span ) \ 858 span.interpMask &= ~SPAN_RGBA; \ 859 span.arrayMask |= SPAN_RGBA; \ 860 fast_persp_span(ctx, &span, &info); 861 862#include "s_tritemp.h" 863 864 865#endif /* CHAN_BITS != GL_FLOAT */ 866 867 868 869 870/* 871 * Render a smooth-shaded, textured, RGBA triangle. 872 * Interpolate S,T,R with perspective correction, w/out mipmapping. 873 */ 874#define NAME general_textured_triangle 875#define INTERP_Z 1 876#define INTERP_W 1 877#define INTERP_FOG 1 878#define INTERP_RGB 1 879#define INTERP_SPEC 1 880#define INTERP_ALPHA 1 881#define INTERP_TEX 1 882#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span); 883#include "s_tritemp.h" 884 885 886 887/* 888 * This is the big one! 889 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates. 890 * Yup, it's slow. 891 */ 892#define NAME multitextured_triangle 893#define INTERP_Z 1 894#define INTERP_W 1 895#define INTERP_FOG 1 896#define INTERP_RGB 1 897#define INTERP_ALPHA 1 898#define INTERP_SPEC 1 899#define INTERP_MULTITEX 1 900#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span); 901#include "s_tritemp.h" 902 903 904 905/* 906 * Special tri function for occlusion testing 907 */ 908#define NAME occlusion_zless_triangle 909#define INTERP_Z 1 910#define SETUP_CODE \ 911 ASSERT(ctx->Depth.Test); \ 912 ASSERT(!ctx->Depth.Mask); \ 913 ASSERT(ctx->Depth.Func == GL_LESS); \ 914 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \ 915 return; \ 916 } 917#define RENDER_SPAN( span ) \ 918 if (ctx->Visual.depthBits <= 16) { \ 919 GLuint i; \ 920 const GLushort *zRow = (const GLushort *) \ 921 _swrast_zbuffer_address(ctx, span.x, span.y); \ 922 for (i = 0; i < span.end; i++) { \ 923 GLdepth z = FixedToDepth(span.z); \ 924 if (z < zRow[i]) { \ 925 ctx->OcclusionResult = GL_TRUE; \ 926 ctx->Occlusion.PassedCounter++; \ 927 } \ 928 span.z += span.zStep; \ 929 } \ 930 } \ 931 else { \ 932 GLuint i; \ 933 const GLuint *zRow = (const GLuint *) \ 934 _swrast_zbuffer_address(ctx, span.x, span.y); \ 935 for (i = 0; i < span.end; i++) { \ 936 if ((GLuint)span.z < zRow[i]) { \ 937 ctx->OcclusionResult = GL_TRUE; \ 938 ctx->Occlusion.PassedCounter++; \ 939 } \ 940 span.z += span.zStep; \ 941 } \ 942 } 943#include "s_tritemp.h" 944 945 946 947static void 948nodraw_triangle( GLcontext *ctx, 949 const SWvertex *v0, 950 const SWvertex *v1, 951 const SWvertex *v2 ) 952{ 953 (void) (ctx && v0 && v1 && v2); 954} 955 956 957/* 958 * This is used when separate specular color is enabled, but not 959 * texturing. We add the specular color to the primary color, 960 * draw the triangle, then restore the original primary color. 961 * Inefficient, but seldom needed. 962 */ 963void _swrast_add_spec_terms_triangle( GLcontext *ctx, 964 const SWvertex *v0, 965 const SWvertex *v1, 966 const SWvertex *v2 ) 967{ 968 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */ 969 SWvertex *ncv1 = (SWvertex *)v1; 970 SWvertex *ncv2 = (SWvertex *)v2; 971#if CHAN_TYPE == GL_FLOAT 972 GLfloat rSum, gSum, bSum; 973#else 974 GLint rSum, gSum, bSum; 975#endif 976 GLchan c[3][4]; 977 /* save original colors */ 978 COPY_CHAN4( c[0], ncv0->color ); 979 COPY_CHAN4( c[1], ncv1->color ); 980 COPY_CHAN4( c[2], ncv2->color ); 981 /* sum v0 */ 982 rSum = ncv0->color[0] + ncv0->specular[0]; 983 gSum = ncv0->color[1] + ncv0->specular[1]; 984 bSum = ncv0->color[2] + ncv0->specular[2]; 985 ncv0->color[0] = MIN2(rSum, CHAN_MAX); 986 ncv0->color[1] = MIN2(gSum, CHAN_MAX); 987 ncv0->color[2] = MIN2(bSum, CHAN_MAX); 988 /* sum v1 */ 989 rSum = ncv1->color[0] + ncv1->specular[0]; 990 gSum = ncv1->color[1] + ncv1->specular[1]; 991 bSum = ncv1->color[2] + ncv1->specular[2]; 992 ncv1->color[0] = MIN2(rSum, CHAN_MAX); 993 ncv1->color[1] = MIN2(gSum, CHAN_MAX); 994 ncv1->color[2] = MIN2(bSum, CHAN_MAX); 995 /* sum v2 */ 996 rSum = ncv2->color[0] + ncv2->specular[0]; 997 gSum = ncv2->color[1] + ncv2->specular[1]; 998 bSum = ncv2->color[2] + ncv2->specular[2]; 999 ncv2->color[0] = MIN2(rSum, CHAN_MAX); 1000 ncv2->color[1] = MIN2(gSum, CHAN_MAX); 1001 ncv2->color[2] = MIN2(bSum, CHAN_MAX); 1002 /* draw */ 1003 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 ); 1004 /* restore original colors */ 1005 COPY_CHAN4( ncv0->color, c[0] ); 1006 COPY_CHAN4( ncv1->color, c[1] ); 1007 COPY_CHAN4( ncv2->color, c[2] ); 1008} 1009 1010 1011 1012#ifdef DEBUG 1013 1014/* record the current triangle function name */ 1015const char *_mesa_triFuncName = NULL; 1016 1017#define USE(triFunc) \ 1018do { \ 1019 _mesa_triFuncName = #triFunc; \ 1020 /*printf("%s\n", _mesa_triFuncName);*/ \ 1021 swrast->Triangle = triFunc; \ 1022} while (0) 1023 1024#else 1025 1026#define USE(triFunc) swrast->Triangle = triFunc; 1027 1028#endif 1029 1030 1031 1032 1033/* 1034 * Determine which triangle rendering function to use given the current 1035 * rendering context. 1036 * 1037 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or 1038 * remove tests to this code. 1039 */ 1040void 1041_swrast_choose_triangle( GLcontext *ctx ) 1042{ 1043 SWcontext *swrast = SWRAST_CONTEXT(ctx); 1044 const GLboolean rgbmode = ctx->Visual.rgbMode; 1045 1046 if (ctx->Polygon.CullFlag && 1047 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) { 1048 USE(nodraw_triangle); 1049 return; 1050 } 1051 1052 if (ctx->RenderMode==GL_RENDER) { 1053 1054 if (ctx->Polygon.SmoothFlag) { 1055 _swrast_set_aa_triangle_function(ctx); 1056 ASSERT(swrast->Triangle); 1057 return; 1058 } 1059 1060 /* special case for occlusion testing */ 1061 if ((ctx->Depth.OcclusionTest || ctx->Occlusion.Active) && 1062 ctx->Depth.Test && 1063 ctx->Depth.Mask == GL_FALSE && 1064 ctx->Depth.Func == GL_LESS && 1065 !ctx->Stencil.Enabled) { 1066 if ((rgbmode && 1067 ctx->Color.ColorMask[0] == 0 && 1068 ctx->Color.ColorMask[1] == 0 && 1069 ctx->Color.ColorMask[2] == 0 && 1070 ctx->Color.ColorMask[3] == 0) 1071 || 1072 (!rgbmode && ctx->Color.IndexMask == 0)) { 1073 USE(occlusion_zless_triangle); 1074 return; 1075 } 1076 } 1077 1078 if (ctx->Texture._EnabledCoordUnits || ctx->FragmentProgram._Active) { 1079 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */ 1080 const struct gl_texture_object *texObj2D; 1081 const struct gl_texture_image *texImg; 1082 GLenum minFilter, magFilter, envMode; 1083 GLint format; 1084 texObj2D = ctx->Texture.Unit[0].Current2D; 1085 texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL; 1086 format = texImg ? texImg->TexFormat->MesaFormat : -1; 1087 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0; 1088 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0; 1089 envMode = ctx->Texture.Unit[0].EnvMode; 1090 1091 /* First see if we can use an optimized 2-D texture function */ 1092 if (ctx->Texture._EnabledCoordUnits == 0x1 1093 && !ctx->FragmentProgram._Active 1094 && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT 1095 && texObj2D->WrapS == GL_REPEAT 1096 && texObj2D->WrapT == GL_REPEAT 1097 && texObj2D->_IsPowerOfTwo 1098 && texImg->Border == 0 1099 && texImg->Width == texImg->RowStride 1100 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA) 1101 && minFilter == magFilter 1102 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR 1103 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) { 1104 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) { 1105 if (minFilter == GL_NEAREST 1106 && format == MESA_FORMAT_RGB 1107 && (envMode == GL_REPLACE || envMode == GL_DECAL) 1108 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT) 1109 && ctx->Depth.Func == GL_LESS 1110 && ctx->Depth.Mask == GL_TRUE) 1111 || swrast->_RasterMask == TEXTURE_BIT) 1112 && ctx->Polygon.StippleFlag == GL_FALSE 1113 && ctx->Visual.depthBits <= 16) { 1114 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) { 1115 USE(simple_z_textured_triangle); 1116 } 1117 else { 1118 USE(simple_textured_triangle); 1119 } 1120 } 1121 else { 1122#if (CHAN_BITS == 16 || CHAN_BITS == 32) 1123 USE(general_textured_triangle); 1124#else 1125 USE(affine_textured_triangle); 1126#endif 1127 } 1128 } 1129 else { 1130#if (CHAN_BITS == 16 || CHAN_BITS == 32) 1131 USE(general_textured_triangle); 1132#else 1133 USE(persp_textured_triangle); 1134#endif 1135 } 1136 } 1137 else { 1138 /* general case textured triangles */ 1139 if (ctx->Texture._EnabledCoordUnits > 1) { 1140 USE(multitextured_triangle); 1141 } 1142 else { 1143 USE(general_textured_triangle); 1144 } 1145 } 1146 } 1147 else { 1148 ASSERT(!ctx->Texture._EnabledCoordUnits); 1149 if (ctx->Light.ShadeModel==GL_SMOOTH) { 1150 /* smooth shaded, no texturing, stippled or some raster ops */ 1151 if (rgbmode) { 1152 USE(smooth_rgba_triangle); 1153 } 1154 else { 1155 USE(smooth_ci_triangle); 1156 } 1157 } 1158 else { 1159 /* flat shaded, no texturing, stippled or some raster ops */ 1160 if (rgbmode) { 1161 USE(flat_rgba_triangle); 1162 } 1163 else { 1164 USE(flat_ci_triangle); 1165 } 1166 } 1167 } 1168 } 1169 else if (ctx->RenderMode==GL_FEEDBACK) { 1170 USE(_swrast_feedback_triangle); 1171 } 1172 else { 1173 /* GL_SELECT mode */ 1174 USE(_swrast_select_triangle); 1175 } 1176} 1177