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