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