s_triangle.c revision 36a0a3252e1e20df69b53f70ba93bc74c4a4bf0e
1/* 2 * Mesa 3-D graphics library 3 * Version: 5.1 4 * 5 * Copyright (C) 1999-2003 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 _swrast_culltriangle( GLcontext *ctx, 53 const SWvertex *v0, 54 const SWvertex *v1, 55 const SWvertex *v2 ) 56{ 57 GLfloat ex = v1->win[0] - v0->win[0]; 58 GLfloat ey = v1->win[1] - v0->win[1]; 59 GLfloat fx = v2->win[0] - v0->win[0]; 60 GLfloat fy = v2->win[1] - v0->win[1]; 61 GLfloat c = ex*fy-ey*fx; 62 63 if (c * SWRAST_CONTEXT(ctx)->_backface_sign > 0) 64 return 0; 65 66 return 1; 67} 68 69 70 71/* 72 * Render a flat-shaded color index triangle. 73 */ 74#define NAME flat_ci_triangle 75#define INTERP_Z 1 76#define INTERP_FOG 1 77#define SETUP_CODE \ 78 span.interpMask |= SPAN_INDEX; \ 79 span.index = IntToFixed(v2->index); \ 80 span.indexStep = 0; 81#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span); 82#include "s_tritemp.h" 83 84 85 86/* 87 * Render a smooth-shaded color index triangle. 88 */ 89#define NAME smooth_ci_triangle 90#define INTERP_Z 1 91#define INTERP_FOG 1 92#define INTERP_INDEX 1 93#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span); 94#include "s_tritemp.h" 95 96 97 98/* 99 * Render a flat-shaded RGBA triangle. 100 */ 101#define NAME flat_rgba_triangle 102#define INTERP_Z 1 103#define INTERP_FOG 1 104#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 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 DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 129#define INTERP_RGB 1 130#define INTERP_ALPHA 1 131#define SETUP_CODE \ 132 { \ 133 /* texturing must be off */ \ 134 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \ 135 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \ 136 } 137#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span); 138#include "s_tritemp.h" 139 140 141 142/* 143 * Render an RGB, GL_DECAL, textured triangle. 144 * Interpolate S,T only w/out mipmapping or perspective correction. 145 * 146 * No fog. 147 */ 148#define NAME simple_textured_triangle 149#define INTERP_INT_TEX 1 150#define S_SCALE twidth 151#define T_SCALE theight 152 153#define SETUP_CODE \ 154 SWcontext *swrast = SWRAST_CONTEXT(ctx); \ 155 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \ 156 const GLint b = obj->BaseLevel; \ 157 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \ 158 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \ 159 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \ 160 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \ 161 const GLint smask = obj->Image[b]->Width - 1; \ 162 const GLint tmask = obj->Image[b]->Height - 1; \ 163 if (!texture) { \ 164 /* this shouldn't happen */ \ 165 return; \ 166 } 167 168#define RENDER_SPAN( span ) \ 169 GLuint i; \ 170 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \ 171 span.intTex[1] -= FIXED_HALF; \ 172 for (i = 0; i < span.end; i++) { \ 173 GLint s = FixedToInt(span.intTex[0]) & smask; \ 174 GLint t = FixedToInt(span.intTex[1]) & tmask; \ 175 GLint pos = (t << twidth_log2) + s; \ 176 pos = pos + pos + pos; /* multiply by 3 */ \ 177 span.array->rgb[i][RCOMP] = texture[pos]; \ 178 span.array->rgb[i][GCOMP] = texture[pos+1]; \ 179 span.array->rgb[i][BCOMP] = texture[pos+2]; \ 180 span.intTex[0] += span.intTexStep[0]; \ 181 span.intTex[1] += span.intTexStep[1]; \ 182 } \ 183 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \ 184 (CONST GLchan (*)[3]) span.array->rgb,\ 185 NULL ); 186#include "s_tritemp.h" 187 188 189 190/* 191 * Render an RGB, GL_DECAL, textured triangle. 192 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or 193 * perspective correction. 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[b]->Width; \ 209 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \ 210 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \ 211 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \ 212 const GLint smask = obj->Image[b]->Width - 1; \ 213 const GLint tmask = obj->Image[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 DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 530#define INTERP_RGB 1 531#define INTERP_ALPHA 1 532#define INTERP_INT_TEX 1 533#define S_SCALE twidth 534#define T_SCALE theight 535 536#define SETUP_CODE \ 537 struct affine_info info; \ 538 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \ 539 struct gl_texture_object *obj = unit->Current2D; \ 540 const GLint b = obj->BaseLevel; \ 541 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \ 542 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \ 543 info.texture = (const GLchan *) obj->Image[b]->Data; \ 544 info.twidth_log2 = obj->Image[b]->WidthLog2; \ 545 info.smask = obj->Image[b]->Width - 1; \ 546 info.tmask = obj->Image[b]->Height - 1; \ 547 info.format = obj->Image[b]->Format; \ 548 info.filter = obj->MinFilter; \ 549 info.envmode = unit->EnvMode; \ 550 span.arrayMask |= SPAN_RGBA; \ 551 \ 552 if (info.envmode == GL_BLEND) { \ 553 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \ 554 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \ 555 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \ 556 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \ 557 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \ 558 } \ 559 if (!info.texture) { \ 560 /* this shouldn't happen */ \ 561 return; \ 562 } \ 563 \ 564 switch (info.format) { \ 565 case GL_ALPHA: \ 566 case GL_LUMINANCE: \ 567 case GL_INTENSITY: \ 568 info.tbytesline = obj->Image[b]->Width; \ 569 break; \ 570 case GL_LUMINANCE_ALPHA: \ 571 info.tbytesline = obj->Image[b]->Width * 2; \ 572 break; \ 573 case GL_RGB: \ 574 info.tbytesline = obj->Image[b]->Width * 3; \ 575 break; \ 576 case GL_RGBA: \ 577 info.tbytesline = obj->Image[b]->Width * 4; \ 578 break; \ 579 default: \ 580 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\ 581 return; \ 582 } \ 583 info.tsize = obj->Image[b]->Height * info.tbytesline; 584 585#define RENDER_SPAN( span ) affine_span(ctx, &span, &info); 586 587#include "s_tritemp.h" 588 589 590 591struct persp_info 592{ 593 GLenum filter; 594 GLenum format; 595 GLenum envmode; 596 GLint smask, tmask; 597 GLint twidth_log2; 598 const GLchan *texture; 599 GLfixed er, eg, eb, ea; /* texture env color */ 600 GLint tbytesline, tsize; 601}; 602 603 604static INLINE void 605fast_persp_span(GLcontext *ctx, struct sw_span *span, 606 struct persp_info *info) 607{ 608 GLchan sample[4]; /* the filtered texture sample */ 609 610 /* Instead of defining a function for each mode, a test is done 611 * between the outer and inner loops. This is to reduce code size 612 * and complexity. Observe that an optimizing compiler kills 613 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST). 614 */ 615#define SPAN_NEAREST(DO_TEX,COMP) \ 616 for (i = 0; i < span->end; i++) { \ 617 GLdouble invQ = tex_coord[2] ? \ 618 (1.0 / tex_coord[2]) : 1.0; \ 619 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \ 620 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \ 621 GLint s = IFLOOR(s_tmp) & info->smask; \ 622 GLint t = IFLOOR(t_tmp) & info->tmask; \ 623 GLint pos = (t << info->twidth_log2) + s; \ 624 const GLchan *tex00 = info->texture + COMP * pos; \ 625 DO_TEX; \ 626 span->red += span->redStep; \ 627 span->green += span->greenStep; \ 628 span->blue += span->blueStep; \ 629 span->alpha += span->alphaStep; \ 630 tex_coord[0] += tex_step[0]; \ 631 tex_coord[1] += tex_step[1]; \ 632 tex_coord[2] += tex_step[2]; \ 633 dest += 4; \ 634 } 635 636#define SPAN_LINEAR(DO_TEX,COMP) \ 637 for (i = 0; i < span->end; i++) { \ 638 GLdouble invQ = tex_coord[2] ? \ 639 (1.0 / tex_coord[2]) : 1.0; \ 640 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \ 641 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \ 642 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \ 643 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \ 644 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \ 645 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \ 646 GLfixed sf = s_fix & FIXED_FRAC_MASK; \ 647 GLfixed tf = t_fix & FIXED_FRAC_MASK; \ 648 GLfixed si = FIXED_FRAC_MASK - sf; \ 649 GLfixed ti = FIXED_FRAC_MASK - tf; \ 650 GLint pos = (t << info->twidth_log2) + s; \ 651 const GLchan *tex00 = info->texture + COMP * pos; \ 652 const GLchan *tex10 = tex00 + info->tbytesline; \ 653 const GLchan *tex01 = tex00 + COMP; \ 654 const GLchan *tex11 = tex10 + COMP; \ 655 (void) ti; \ 656 (void) si; \ 657 if (t == info->tmask) { \ 658 tex10 -= info->tsize; \ 659 tex11 -= info->tsize; \ 660 } \ 661 if (s == info->smask) { \ 662 tex01 -= info->tbytesline; \ 663 tex11 -= info->tbytesline; \ 664 } \ 665 DO_TEX; \ 666 span->red += span->redStep; \ 667 span->green += span->greenStep; \ 668 span->blue += span->blueStep; \ 669 span->alpha += span->alphaStep; \ 670 tex_coord[0] += tex_step[0]; \ 671 tex_coord[1] += tex_step[1]; \ 672 tex_coord[2] += tex_step[2]; \ 673 dest += 4; \ 674 } 675 676 GLuint i; 677 GLfloat tex_coord[3], tex_step[3]; 678 GLchan *dest = span->array->rgba[0]; 679 680 tex_coord[0] = span->tex[0][0] * (info->smask + 1); 681 tex_step[0] = span->texStepX[0][0] * (info->smask + 1); 682 tex_coord[1] = span->tex[0][1] * (info->tmask + 1); 683 tex_step[1] = span->texStepX[0][1] * (info->tmask + 1); 684 /* span->tex[0][2] only if 3D-texturing, here only 2D */ 685 tex_coord[2] = span->tex[0][3]; 686 tex_step[2] = span->texStepX[0][3]; 687 688 switch (info->filter) { 689 case GL_NEAREST: 690 switch (info->format) { 691 case GL_RGB: 692 switch (info->envmode) { 693 case GL_MODULATE: 694 SPAN_NEAREST(NEAREST_RGB;MODULATE,3); 695 break; 696 case GL_DECAL: 697 case GL_REPLACE: 698 SPAN_NEAREST(NEAREST_RGB_REPLACE,3); 699 break; 700 case GL_BLEND: 701 SPAN_NEAREST(NEAREST_RGB;BLEND,3); 702 break; 703 case GL_ADD: 704 SPAN_NEAREST(NEAREST_RGB;ADD,3); 705 break; 706 default: 707 _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR"); 708 return; 709 } 710 break; 711 case GL_RGBA: 712 switch(info->envmode) { 713 case GL_MODULATE: 714 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4); 715 break; 716 case GL_DECAL: 717 SPAN_NEAREST(NEAREST_RGBA;DECAL,4); 718 break; 719 case GL_BLEND: 720 SPAN_NEAREST(NEAREST_RGBA;BLEND,4); 721 break; 722 case GL_ADD: 723 SPAN_NEAREST(NEAREST_RGBA;ADD,4); 724 break; 725 case GL_REPLACE: 726 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4); 727 break; 728 default: 729 _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR"); 730 return; 731 } 732 break; 733 } 734 break; 735 736 case GL_LINEAR: 737 switch (info->format) { 738 case GL_RGB: 739 switch (info->envmode) { 740 case GL_MODULATE: 741 SPAN_LINEAR(LINEAR_RGB;MODULATE,3); 742 break; 743 case GL_DECAL: 744 case GL_REPLACE: 745 SPAN_LINEAR(LINEAR_RGB;REPLACE,3); 746 break; 747 case GL_BLEND: 748 SPAN_LINEAR(LINEAR_RGB;BLEND,3); 749 break; 750 case GL_ADD: 751 SPAN_LINEAR(LINEAR_RGB;ADD,3); 752 break; 753 default: 754 _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR"); 755 return; 756 } 757 break; 758 case GL_RGBA: 759 switch (info->envmode) { 760 case GL_MODULATE: 761 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4); 762 break; 763 case GL_DECAL: 764 SPAN_LINEAR(LINEAR_RGBA;DECAL,4); 765 break; 766 case GL_BLEND: 767 SPAN_LINEAR(LINEAR_RGBA;BLEND,4); 768 break; 769 case GL_ADD: 770 SPAN_LINEAR(LINEAR_RGBA;ADD,4); 771 break; 772 case GL_REPLACE: 773 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4); 774 break; 775 default: 776 _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR"); 777 return; 778 } 779 break; 780 } 781 break; 782 } 783 784 ASSERT(span->arrayMask & SPAN_RGBA); 785 _swrast_write_rgba_span(ctx, span); 786 787#undef SPAN_NEAREST 788#undef SPAN_LINEAR 789} 790 791 792/* 793 * Render an perspective corrected RGB/RGBA textured triangle. 794 * The Q (aka V in Mesa) coordinate must be zero such that the divide 795 * by interpolated Q/W comes out right. 796 * 797 */ 798#define NAME persp_textured_triangle 799#define INTERP_Z 1 800#define INTERP_FOG 1 801#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 802#define INTERP_RGB 1 803#define INTERP_ALPHA 1 804#define INTERP_TEX 1 805 806#define SETUP_CODE \ 807 struct persp_info info; \ 808 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \ 809 const struct gl_texture_object *obj = unit->Current2D; \ 810 const GLint b = obj->BaseLevel; \ 811 info.texture = (const GLchan *) obj->Image[b]->Data; \ 812 info.twidth_log2 = obj->Image[b]->WidthLog2; \ 813 info.smask = obj->Image[b]->Width - 1; \ 814 info.tmask = obj->Image[b]->Height - 1; \ 815 info.format = obj->Image[b]->Format; \ 816 info.filter = obj->MinFilter; \ 817 info.envmode = unit->EnvMode; \ 818 \ 819 if (info.envmode == GL_BLEND) { \ 820 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \ 821 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \ 822 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \ 823 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \ 824 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \ 825 } \ 826 if (!info.texture) { \ 827 /* this shouldn't happen */ \ 828 return; \ 829 } \ 830 \ 831 switch (info.format) { \ 832 case GL_ALPHA: \ 833 case GL_LUMINANCE: \ 834 case GL_INTENSITY: \ 835 info.tbytesline = obj->Image[b]->Width; \ 836 break; \ 837 case GL_LUMINANCE_ALPHA: \ 838 info.tbytesline = obj->Image[b]->Width * 2; \ 839 break; \ 840 case GL_RGB: \ 841 info.tbytesline = obj->Image[b]->Width * 3; \ 842 break; \ 843 case GL_RGBA: \ 844 info.tbytesline = obj->Image[b]->Width * 4; \ 845 break; \ 846 default: \ 847 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\ 848 return; \ 849 } \ 850 info.tsize = obj->Image[b]->Height * info.tbytesline; 851 852#define RENDER_SPAN( span ) \ 853 span.interpMask &= ~SPAN_RGBA; \ 854 span.arrayMask |= SPAN_RGBA; \ 855 fast_persp_span(ctx, &span, &info); 856 857#include "s_tritemp.h" 858 859 860#endif /* CHAN_BITS != GL_FLOAT */ 861 862 863 864 865/* 866 * Render a smooth-shaded, textured, RGBA triangle. 867 * Interpolate S,T,R with perspective correction, w/out mipmapping. 868 */ 869#define NAME general_textured_triangle 870#define INTERP_Z 1 871#define INTERP_W 1 872#define INTERP_FOG 1 873#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 874#define INTERP_RGB 1 875#define INTERP_SPEC 1 876#define INTERP_ALPHA 1 877#define INTERP_TEX 1 878#define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span); 879#include "s_tritemp.h" 880 881 882 883/* 884 * This is the big one! 885 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates. 886 * Yup, it's slow. 887 */ 888#define NAME multitextured_triangle 889#define INTERP_Z 1 890#define INTERP_W 1 891#define INTERP_FOG 1 892#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 893#define INTERP_RGB 1 894#define INTERP_ALPHA 1 895#define INTERP_SPEC 1 896#define INTERP_MULTITEX 1 897#define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span); 898#include "s_tritemp.h" 899 900 901 902#define NAME occlusion_zless_triangle 903#define DO_OCCLUSION_TEST 904#define INTERP_Z 1 905#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE 906#define SETUP_CODE \ 907 if (ctx->OcclusionResult) { \ 908 return; \ 909 } 910#define RENDER_SPAN( span ) \ 911 GLuint i; \ 912 for (i = 0; i < span.end; i++) { \ 913 GLdepth z = FixedToDepth(span.z); \ 914 if (z < zRow[i]) { \ 915 ctx->OcclusionResult = GL_TRUE; \ 916 return; \ 917 } \ 918 span.z += span.zStep; \ 919 } 920#include "s_tritemp.h" 921 922 923 924static void 925nodraw_triangle( GLcontext *ctx, 926 const SWvertex *v0, 927 const SWvertex *v1, 928 const SWvertex *v2 ) 929{ 930 (void) (ctx && v0 && v1 && v2); 931} 932 933 934/* 935 * This is used when separate specular color is enabled, but not 936 * texturing. We add the specular color to the primary color, 937 * draw the triangle, then restore the original primary color. 938 * Inefficient, but seldom needed. 939 */ 940void _swrast_add_spec_terms_triangle( GLcontext *ctx, 941 const SWvertex *v0, 942 const SWvertex *v1, 943 const SWvertex *v2 ) 944{ 945 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */ 946 SWvertex *ncv1 = (SWvertex *)v1; 947 SWvertex *ncv2 = (SWvertex *)v2; 948#if CHAN_TYPE == GL_FLOAT 949 GLfloat rSum, gSum, bSum; 950#else 951 GLint rSum, gSum, bSum; 952#endif 953 GLchan c[3][4]; 954 /* save original colors */ 955 COPY_CHAN4( c[0], ncv0->color ); 956 COPY_CHAN4( c[1], ncv1->color ); 957 COPY_CHAN4( c[2], ncv2->color ); 958 /* sum v0 */ 959 rSum = ncv0->color[0] + ncv0->specular[0]; 960 gSum = ncv0->color[1] + ncv0->specular[1]; 961 bSum = ncv0->color[2] + ncv0->specular[2]; 962 ncv0->color[0] = MIN2(rSum, CHAN_MAX); 963 ncv0->color[1] = MIN2(gSum, CHAN_MAX); 964 ncv0->color[2] = MIN2(bSum, CHAN_MAX); 965 /* sum v1 */ 966 rSum = ncv1->color[0] + ncv1->specular[0]; 967 gSum = ncv1->color[1] + ncv1->specular[1]; 968 bSum = ncv1->color[2] + ncv1->specular[2]; 969 ncv1->color[0] = MIN2(rSum, CHAN_MAX); 970 ncv1->color[1] = MIN2(gSum, CHAN_MAX); 971 ncv1->color[2] = MIN2(bSum, CHAN_MAX); 972 /* sum v2 */ 973 rSum = ncv2->color[0] + ncv2->specular[0]; 974 gSum = ncv2->color[1] + ncv2->specular[1]; 975 bSum = ncv2->color[2] + ncv2->specular[2]; 976 ncv2->color[0] = MIN2(rSum, CHAN_MAX); 977 ncv2->color[1] = MIN2(gSum, CHAN_MAX); 978 ncv2->color[2] = MIN2(bSum, CHAN_MAX); 979 /* draw */ 980 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 ); 981 /* restore original colors */ 982 COPY_CHAN4( ncv0->color, c[0] ); 983 COPY_CHAN4( ncv1->color, c[1] ); 984 COPY_CHAN4( ncv2->color, c[2] ); 985} 986 987 988 989#ifdef DEBUG 990 991/* record the current triangle function name */ 992const char *_mesa_triFuncName = NULL; 993 994#define USE(triFunc) \ 995do { \ 996 _mesa_triFuncName = #triFunc; \ 997 /*printf("%s\n", _mesa_triFuncName);*/ \ 998 swrast->Triangle = triFunc; \ 999} while (0) 1000 1001#else 1002 1003#define USE(triFunc) swrast->Triangle = triFunc; 1004 1005#endif 1006 1007 1008 1009 1010/* 1011 * Determine which triangle rendering function to use given the current 1012 * rendering context. 1013 * 1014 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or 1015 * remove tests to this code. 1016 */ 1017void 1018_swrast_choose_triangle( GLcontext *ctx ) 1019{ 1020 SWcontext *swrast = SWRAST_CONTEXT(ctx); 1021 const GLboolean rgbmode = ctx->Visual.rgbMode; 1022 1023 if (ctx->Polygon.CullFlag && 1024 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) { 1025 USE(nodraw_triangle); 1026 return; 1027 } 1028 1029 if (ctx->RenderMode==GL_RENDER) { 1030 1031 if (ctx->Polygon.SmoothFlag) { 1032 _swrast_set_aa_triangle_function(ctx); 1033 ASSERT(swrast->Triangle); 1034 return; 1035 } 1036 1037 if (ctx->Depth.OcclusionTest && 1038 ctx->Depth.Test && 1039 ctx->Depth.Mask == GL_FALSE && 1040 ctx->Depth.Func == GL_LESS && 1041 !ctx->Stencil.Enabled) { 1042 if ((rgbmode && 1043 ctx->Color.ColorMask[0] == 0 && 1044 ctx->Color.ColorMask[1] == 0 && 1045 ctx->Color.ColorMask[2] == 0 && 1046 ctx->Color.ColorMask[3] == 0) 1047 || 1048 (!rgbmode && ctx->Color.IndexMask == 0)) { 1049 USE(occlusion_zless_triangle); 1050 return; 1051 } 1052 } 1053 1054 if (ctx->Texture._EnabledCoordUnits || ctx->FragmentProgram.Enabled) { 1055 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */ 1056 const struct gl_texture_object *texObj2D; 1057 const struct gl_texture_image *texImg; 1058 GLenum minFilter, magFilter, envMode; 1059 GLint format; 1060 texObj2D = ctx->Texture.Unit[0].Current2D; 1061 texImg = texObj2D ? texObj2D->Image[texObj2D->BaseLevel] : NULL; 1062 format = texImg ? texImg->TexFormat->MesaFormat : -1; 1063 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0; 1064 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0; 1065 envMode = ctx->Texture.Unit[0].EnvMode; 1066 1067 /* First see if we can use an optimized 2-D texture function */ 1068 if (ctx->Texture._EnabledCoordUnits == 1 1069 && !ctx->FragmentProgram.Enabled 1070 && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT 1071 && texObj2D->WrapS==GL_REPEAT 1072 && texObj2D->WrapT==GL_REPEAT 1073 && texImg->Border==0 1074 && texImg->Width == texImg->RowStride 1075 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA) 1076 && minFilter == magFilter 1077 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR 1078 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) { 1079 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) { 1080 if (minFilter == GL_NEAREST 1081 && format == MESA_FORMAT_RGB 1082 && (envMode == GL_REPLACE || envMode == GL_DECAL) 1083 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT) 1084 && ctx->Depth.Func == GL_LESS 1085 && ctx->Depth.Mask == GL_TRUE) 1086 || swrast->_RasterMask == TEXTURE_BIT) 1087 && ctx->Polygon.StippleFlag == GL_FALSE) { 1088 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) { 1089 USE(simple_z_textured_triangle); 1090 } 1091 else { 1092 USE(simple_textured_triangle); 1093 } 1094 } 1095 else { 1096#if (CHAN_BITS == 16 || CHAN_BITS == 32) 1097 USE(general_textured_triangle); 1098#else 1099 USE(affine_textured_triangle); 1100#endif 1101 } 1102 } 1103 else { 1104#if (CHAN_BITS == 16 || CHAN_BITS == 32) 1105 USE(general_textured_triangle); 1106#else 1107 USE(persp_textured_triangle); 1108#endif 1109 } 1110 } 1111 else { 1112 /* general case textured triangles */ 1113 if (ctx->Texture._EnabledCoordUnits > 1) { 1114 USE(multitextured_triangle); 1115 } 1116 else { 1117 USE(general_textured_triangle); 1118 } 1119 } 1120 } 1121 else { 1122 ASSERT(!ctx->Texture._EnabledCoordUnits); 1123 if (ctx->Light.ShadeModel==GL_SMOOTH) { 1124 /* smooth shaded, no texturing, stippled or some raster ops */ 1125 if (rgbmode) { 1126 USE(smooth_rgba_triangle); 1127 } 1128 else { 1129 USE(smooth_ci_triangle); 1130 } 1131 } 1132 else { 1133 /* flat shaded, no texturing, stippled or some raster ops */ 1134 if (rgbmode) { 1135 USE(flat_rgba_triangle); 1136 } 1137 else { 1138 USE(flat_ci_triangle); 1139 } 1140 } 1141 } 1142 } 1143 else if (ctx->RenderMode==GL_FEEDBACK) { 1144 USE(_swrast_feedback_triangle); 1145 } 1146 else { 1147 /* GL_SELECT mode */ 1148 USE(_swrast_select_triangle); 1149 } 1150} 1151