sp_tex_sample.c revision 3b2a291888d8e62787de03f8529806fb562bd186
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * Texture sampling 30 * 31 * Authors: 32 * Brian Paul 33 */ 34 35#include "sp_context.h" 36#include "sp_headers.h" 37#include "sp_surface.h" 38#include "sp_tex_sample.h" 39#include "sp_tile_cache.h" 40#include "pipe/p_context.h" 41#include "pipe/p_defines.h" 42#include "pipe/p_util.h" 43#include "tgsi/exec/tgsi_exec.h" 44 45 46/* 47 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes 48 * see 1-pixel bands of improperly weighted linear-filtered textures. 49 * The tests/texwrap.c demo is a good test. 50 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0. 51 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x). 52 */ 53#define FRAC(f) ((f) - ifloor(f)) 54 55 56/** 57 * Linear interpolation macro 58 */ 59#define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) ) 60 61 62/** 63 * Do 2D/biliner interpolation of float values. 64 * v00, v10, v01 and v11 are typically four texture samples in a square/box. 65 * a and b are the horizontal and vertical interpolants. 66 * It's important that this function is inlined when compiled with 67 * optimization! If we find that's not true on some systems, convert 68 * to a macro. 69 */ 70static INLINE float 71lerp_2d(float a, float b, 72 float v00, float v10, float v01, float v11) 73{ 74 const float temp0 = LERP(a, v00, v10); 75 const float temp1 = LERP(a, v01, v11); 76 return LERP(b, temp0, temp1); 77} 78 79 80/** 81 * If A is a signed integer, A % B doesn't give the right value for A < 0 82 * (in terms of texture repeat). Just casting to unsigned fixes that. 83 */ 84#define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B)) 85 86 87/** 88 * Apply texture coord wrapping mode and return integer texture index. 89 * \param wrapMode PIPE_TEX_WRAP_x 90 * \param s the texcoord 91 * \param size the texture image size 92 * \return integer texture index 93 */ 94static INLINE int 95nearest_texcoord(unsigned wrapMode, float s, unsigned size) 96{ 97 int i; 98 switch (wrapMode) { 99 case PIPE_TEX_WRAP_REPEAT: 100 /* s limited to [0,1) */ 101 /* i limited to [0,size-1] */ 102 i = ifloor(s * size); 103 i = REMAINDER(i, size); 104 return i; 105 case PIPE_TEX_WRAP_CLAMP: 106 /* s limited to [0,1] */ 107 /* i limited to [0,size-1] */ 108 if (s <= 0.0F) 109 i = 0; 110 else if (s >= 1.0F) 111 i = size - 1; 112 else 113 i = ifloor(s * size); 114 return i; 115 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 116 { 117 /* s limited to [min,max] */ 118 /* i limited to [0, size-1] */ 119 const float min = 1.0F / (2.0F * size); 120 const float max = 1.0F - min; 121 if (s < min) 122 i = 0; 123 else if (s > max) 124 i = size - 1; 125 else 126 i = ifloor(s * size); 127 } 128 return i; 129 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 130 { 131 /* s limited to [min,max] */ 132 /* i limited to [-1, size] */ 133 const float min = -1.0F / (2.0F * size); 134 const float max = 1.0F - min; 135 if (s <= min) 136 i = -1; 137 else if (s >= max) 138 i = size; 139 else 140 i = ifloor(s * size); 141 } 142 return i; 143 case PIPE_TEX_WRAP_MIRROR_REPEAT: 144 { 145 const float min = 1.0F / (2.0F * size); 146 const float max = 1.0F - min; 147 const int flr = ifloor(s); 148 float u; 149 if (flr & 1) 150 u = 1.0F - (s - (float) flr); 151 else 152 u = s - (float) flr; 153 if (u < min) 154 i = 0; 155 else if (u > max) 156 i = size - 1; 157 else 158 i = ifloor(u * size); 159 } 160 return i; 161 case PIPE_TEX_WRAP_MIRROR_CLAMP: 162 { 163 /* s limited to [0,1] */ 164 /* i limited to [0,size-1] */ 165 const float u = FABSF(s); 166 if (u <= 0.0F) 167 i = 0; 168 else if (u >= 1.0F) 169 i = size - 1; 170 else 171 i = ifloor(u * size); 172 } 173 return i; 174 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE: 175 { 176 /* s limited to [min,max] */ 177 /* i limited to [0, size-1] */ 178 const float min = 1.0F / (2.0F * size); 179 const float max = 1.0F - min; 180 const float u = FABSF(s); 181 if (u < min) 182 i = 0; 183 else if (u > max) 184 i = size - 1; 185 else 186 i = ifloor(u * size); 187 } 188 return i; 189 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER: 190 { 191 /* s limited to [min,max] */ 192 /* i limited to [0, size-1] */ 193 const float min = -1.0F / (2.0F * size); 194 const float max = 1.0F - min; 195 const float u = FABSF(s); 196 if (u < min) 197 i = -1; 198 else if (u > max) 199 i = size; 200 else 201 i = ifloor(u * size); 202 } 203 return i; 204 default: 205 assert(0); 206 return 0; 207 } 208} 209 210 211/** 212 * Used to compute texel locations for linear sampling. 213 * \param wrapMode PIPE_TEX_WRAP_x 214 * \param s the texcoord 215 * \param size the texture image size 216 * \param i0 returns first texture index 217 * \param i1 returns second texture index (usually *i0 + 1) 218 * \param a returns blend factor/weight between texture indexes 219 */ 220static INLINE void 221linear_texcoord(unsigned wrapMode, float s, unsigned size, 222 int *i0, int *i1, float *a) 223{ 224 float u; 225 switch (wrapMode) { 226 case PIPE_TEX_WRAP_REPEAT: 227 u = s * size - 0.5F; 228 *i0 = REMAINDER(ifloor(u), size); 229 *i1 = REMAINDER(*i0 + 1, size); 230 break; 231 case PIPE_TEX_WRAP_CLAMP: 232 if (s <= 0.0F) 233 u = 0.0F; 234 else if (s >= 1.0F) 235 u = (float) size; 236 else 237 u = s * size; 238 u -= 0.5F; 239 *i0 = ifloor(u); 240 *i1 = *i0 + 1; 241 break; 242 case PIPE_TEX_WRAP_CLAMP_TO_EDGE: 243 if (s <= 0.0F) 244 u = 0.0F; 245 else if (s >= 1.0F) 246 u = (float) size; 247 else 248 u = s * size; 249 u -= 0.5F; 250 *i0 = ifloor(u); 251 *i1 = *i0 + 1; 252 if (*i0 < 0) 253 *i0 = 0; 254 if (*i1 >= (int) size) 255 *i1 = size - 1; 256 break; 257 case PIPE_TEX_WRAP_CLAMP_TO_BORDER: 258 { 259 const float min = -1.0F / (2.0F * size); 260 const float max = 1.0F - min; 261 if (s <= min) 262 u = min * size; 263 else if (s >= max) 264 u = max * size; 265 else 266 u = s * size; 267 u -= 0.5F; 268 *i0 = ifloor(u); 269 *i1 = *i0 + 1; 270 } 271 break; 272 case PIPE_TEX_WRAP_MIRROR_REPEAT: 273 { 274 const int flr = ifloor(s); 275 if (flr & 1) 276 u = 1.0F - (s - (float) flr); 277 else 278 u = s - (float) flr; 279 u = (u * size) - 0.5F; 280 *i0 = ifloor(u); 281 *i1 = *i0 + 1; 282 if (*i0 < 0) 283 *i0 = 0; 284 if (*i1 >= (int) size) 285 *i1 = size - 1; 286 } 287 break; 288 case PIPE_TEX_WRAP_MIRROR_CLAMP: 289 u = FABSF(s); 290 if (u >= 1.0F) 291 u = (float) size; 292 else 293 u *= size; 294 u -= 0.5F; 295 *i0 = ifloor(u); 296 *i1 = *i0 + 1; 297 break; 298 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE: 299 u = FABSF(s); 300 if (u >= 1.0F) 301 u = (float) size; 302 else 303 u *= size; 304 u -= 0.5F; 305 *i0 = ifloor(u); 306 *i1 = *i0 + 1; 307 if (*i0 < 0) 308 *i0 = 0; 309 if (*i1 >= (int) size) 310 *i1 = size - 1; 311 break; 312 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER: 313 { 314 const float min = -1.0F / (2.0F * size); 315 const float max = 1.0F - min; 316 u = FABSF(s); 317 if (u <= min) 318 u = min * size; 319 else if (u >= max) 320 u = max * size; 321 else 322 u *= size; 323 u -= 0.5F; 324 *i0 = ifloor(u); 325 *i1 = *i0 + 1; 326 } 327 break; 328 default: 329 assert(0); 330 } 331 *a = FRAC(u); 332} 333 334 335static unsigned 336choose_cube_face(float rx, float ry, float rz, float *newS, float *newT) 337{ 338 /* 339 major axis 340 direction target sc tc ma 341 ---------- ------------------------------- --- --- --- 342 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx 343 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx 344 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry 345 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry 346 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz 347 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz 348 */ 349 const float arx = FABSF(rx), ary = FABSF(ry), arz = FABSF(rz); 350 unsigned face; 351 float sc, tc, ma; 352 353 if (arx > ary && arx > arz) { 354 if (rx >= 0.0F) { 355 face = PIPE_TEX_FACE_POS_X; 356 sc = -rz; 357 tc = -ry; 358 ma = arx; 359 } 360 else { 361 face = PIPE_TEX_FACE_NEG_X; 362 sc = rz; 363 tc = -ry; 364 ma = arx; 365 } 366 } 367 else if (ary > arx && ary > arz) { 368 if (ry >= 0.0F) { 369 face = PIPE_TEX_FACE_POS_Y; 370 sc = rx; 371 tc = rz; 372 ma = ary; 373 } 374 else { 375 face = PIPE_TEX_FACE_NEG_Y; 376 sc = rx; 377 tc = -rz; 378 ma = ary; 379 } 380 } 381 else { 382 if (rz > 0.0F) { 383 face = PIPE_TEX_FACE_POS_Z; 384 sc = rx; 385 tc = -ry; 386 ma = arz; 387 } 388 else { 389 face = PIPE_TEX_FACE_NEG_Z; 390 sc = -rx; 391 tc = -ry; 392 ma = arz; 393 } 394 } 395 396 *newS = ( sc / ma + 1.0F ) * 0.5F; 397 *newT = ( tc / ma + 1.0F ) * 0.5F; 398 399 return face; 400} 401 402 403/** 404 * Examine the quad's texture coordinates to compute the partial 405 * derivatives w.r.t X and Y, then compute lambda (level of detail). 406 * 407 * This is only done for fragment shaders, not vertex shaders. 408 */ 409static float 410compute_lambda(struct tgsi_sampler *sampler, 411 const float s[QUAD_SIZE], 412 const float t[QUAD_SIZE], 413 const float p[QUAD_SIZE], 414 float lodbias) 415{ 416 float rho, lambda; 417 418 assert(s); 419 { 420 float dsdx = s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]; 421 float dsdy = s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]; 422 dsdx = FABSF(dsdx); 423 dsdy = FABSF(dsdy); 424 rho = MAX2(dsdx, dsdy); 425 if (sampler->state->normalized_coords) 426 rho *= sampler->texture->width[0]; 427 } 428 if (t) { 429 float dtdx = t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]; 430 float dtdy = t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]; 431 float max; 432 dtdx = FABSF(dtdx); 433 dtdy = FABSF(dtdy); 434 max = MAX2(dtdx, dtdy); 435 if (sampler->state->normalized_coords) 436 max *= sampler->texture->height[0]; 437 rho = MAX2(rho, max); 438 } 439 if (p) { 440 float dpdx = p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]; 441 float dpdy = p[QUAD_TOP_LEFT] - p[QUAD_BOTTOM_LEFT]; 442 float max; 443 dpdx = FABSF(dpdx); 444 dpdy = FABSF(dpdy); 445 max = MAX2(dpdx, dpdy); 446 if (sampler->state->normalized_coords) 447 max *= sampler->texture->depth[0]; 448 rho = MAX2(rho, max); 449 } 450 451 lambda = LOG2(rho); 452 lambda += lodbias + sampler->state->lod_bias; 453 lambda = CLAMP(lambda, sampler->state->min_lod, sampler->state->max_lod); 454 455 return lambda; 456} 457 458 459/** 460 * Do several things here: 461 * 1. Compute lambda from the texcoords, if needed 462 * 2. Determine if we're minifying or magnifying 463 * 3. If minifying, choose mipmap levels 464 * 4. Return image filter to use within mipmap images 465 */ 466static void 467choose_mipmap_levels(struct tgsi_sampler *sampler, 468 const float s[QUAD_SIZE], 469 const float t[QUAD_SIZE], 470 const float p[QUAD_SIZE], 471 float lodbias, 472 unsigned *level0, unsigned *level1, float *levelBlend, 473 unsigned *imgFilter) 474{ 475 if (sampler->state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) { 476 /* no mipmap selection needed */ 477 *level0 = *level1 = (int) sampler->state->min_lod; 478 479 if (sampler->state->min_img_filter != sampler->state->mag_img_filter) { 480 /* non-mipmapped texture, but still need to determine if doing 481 * minification or magnification. 482 */ 483 float lambda = compute_lambda(sampler, s, t, p, lodbias); 484 if (lambda <= 0.0) { 485 *imgFilter = sampler->state->mag_img_filter; 486 } 487 else { 488 *imgFilter = sampler->state->min_img_filter; 489 } 490 } 491 else { 492 *imgFilter = sampler->state->mag_img_filter; 493 } 494 } 495 else { 496 float lambda; 497 498 if (1) 499 /* fragment shader */ 500 lambda = compute_lambda(sampler, s, t, p, lodbias); 501 else 502 /* vertex shader */ 503 lambda = lodbias; /* not really a bias, but absolute LOD */ 504 505 if (lambda <= 0.0) { /* XXX threshold depends on the filter */ 506 /* magnifying */ 507 *imgFilter = sampler->state->mag_img_filter; 508 *level0 = *level1 = 0; 509 } 510 else { 511 /* minifying */ 512 *imgFilter = sampler->state->min_img_filter; 513 514 /* choose mipmap level(s) and compute the blend factor between them */ 515 if (sampler->state->min_mip_filter == PIPE_TEX_MIPFILTER_NEAREST) { 516 /* Nearest mipmap level */ 517 const int lvl = (int) (lambda + 0.5); 518 *level0 = 519 *level1 = CLAMP(lvl, 0, (int) sampler->texture->last_level); 520 } 521 else { 522 /* Linear interpolation between mipmap levels */ 523 const int lvl = (int) lambda; 524 *level0 = CLAMP(lvl, 0, (int) sampler->texture->last_level); 525 *level1 = CLAMP(lvl + 1, 0, (int) sampler->texture->last_level); 526 *levelBlend = FRAC(lambda); /* blending weight between levels */ 527 } 528 } 529 } 530} 531 532 533/** 534 * Get a texel from a texture, using the texture tile cache. 535 * 536 * \param face the cube face in 0..5 537 * \param level the mipmap level 538 * \param x the x coord of texel within 2D image 539 * \param y the y coord of texel within 2D image 540 * \param z which slice of a 3D texture 541 * \param rgba the quad to put the texel/color into 542 * \param j which element of the rgba quad to write to 543 * 544 * XXX maybe move this into sp_tile_cache.c and merge with the 545 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1... 546 */ 547static void 548get_texel(struct tgsi_sampler *sampler, 549 unsigned face, unsigned level, int x, int y, int z, 550 float rgba[NUM_CHANNELS][QUAD_SIZE], unsigned j) 551{ 552 const int tx = x % TILE_SIZE; 553 const int ty = y % TILE_SIZE; 554 const struct softpipe_cached_tile *tile 555 = sp_get_cached_tile_tex(sampler->pipe, sampler->cache, 556 x, y, z, face, level); 557 rgba[0][j] = tile->data.color[ty][tx][0]; 558 rgba[1][j] = tile->data.color[ty][tx][1]; 559 rgba[2][j] = tile->data.color[ty][tx][2]; 560 rgba[3][j] = tile->data.color[ty][tx][3]; 561} 562 563 564/** 565 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]' 566 * When we sampled the depth texture, the depth value was put into all 567 * RGBA channels. We look at the red channel here. 568 */ 569static INLINE void 570shadow_compare(uint compare_func, 571 float rgba[NUM_CHANNELS][QUAD_SIZE], 572 const float p[QUAD_SIZE], 573 uint j) 574{ 575 int k; 576 switch (compare_func) { 577 case PIPE_FUNC_LESS: 578 k = p[j] < rgba[0][j]; 579 break; 580 case PIPE_FUNC_LEQUAL: 581 k = p[j] <= rgba[0][j]; 582 break; 583 case PIPE_FUNC_GREATER: 584 k = p[j] > rgba[0][j]; 585 break; 586 case PIPE_FUNC_GEQUAL: 587 k = p[j] >= rgba[0][j]; 588 break; 589 case PIPE_FUNC_EQUAL: 590 k = p[j] == rgba[0][j]; 591 break; 592 case PIPE_FUNC_NOTEQUAL: 593 k = p[j] != rgba[0][j]; 594 break; 595 case PIPE_FUNC_ALWAYS: 596 k = 1; 597 break; 598 case PIPE_FUNC_NEVER: 599 k = 0; 600 break; 601 default: 602 assert(0); 603 } 604 605 rgba[0][j] = rgba[1][j] = rgba[2][j] = (float) k; 606} 607 608 609/** 610 * Common code for sampling 1D/2D/cube textures. 611 * Could probably extend for 3D... 612 */ 613static void 614sp_get_samples_2d_common(struct tgsi_sampler *sampler, 615 const float s[QUAD_SIZE], 616 const float t[QUAD_SIZE], 617 const float p[QUAD_SIZE], 618 float lodbias, 619 float rgba[NUM_CHANNELS][QUAD_SIZE], 620 const unsigned faces[4]) 621{ 622 const uint compare_func = sampler->state->compare_func; 623 unsigned level0, level1, j, imgFilter; 624 int width, height; 625 float levelBlend; 626 627 choose_mipmap_levels(sampler, s, t, p, lodbias, 628 &level0, &level1, &levelBlend, &imgFilter); 629 630 if (sampler->state->normalized_coords) { 631 width = sampler->texture->width[level0]; 632 height = sampler->texture->height[level0]; 633 } 634 else { 635 width = height = 1; 636 } 637 638 assert(width > 0); 639 640 switch (imgFilter) { 641 case PIPE_TEX_FILTER_NEAREST: 642 for (j = 0; j < QUAD_SIZE; j++) { 643 int x = nearest_texcoord(sampler->state->wrap_s, s[j], width); 644 int y = nearest_texcoord(sampler->state->wrap_t, t[j], height); 645 get_texel(sampler, faces[j], level0, x, y, 0, rgba, j); 646 if (sampler->state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) { 647 shadow_compare(compare_func, rgba, p, j); 648 } 649 650 if (level0 != level1) { 651 /* get texels from second mipmap level and blend */ 652 float rgba2[4][4]; 653 unsigned c; 654 x = x / 2; 655 y = y / 2; 656 get_texel(sampler, faces[j], level1, x, y, 0, rgba2, j); 657 if (sampler->state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){ 658 shadow_compare(compare_func, rgba2, p, j); 659 } 660 661 for (c = 0; c < NUM_CHANNELS; c++) { 662 rgba[c][j] = LERP(levelBlend, rgba[c][j], rgba2[c][j]); 663 } 664 } 665 } 666 break; 667 case PIPE_TEX_FILTER_LINEAR: 668 for (j = 0; j < QUAD_SIZE; j++) { 669 float tx[4][4], a, b; 670 int x0, y0, x1, y1, c; 671 linear_texcoord(sampler->state->wrap_s, s[j], width, &x0, &x1, &a); 672 linear_texcoord(sampler->state->wrap_t, t[j], height, &y0, &y1, &b); 673 get_texel(sampler, faces[j], level0, x0, y0, 0, tx, 0); 674 get_texel(sampler, faces[j], level0, x1, y0, 0, tx, 1); 675 get_texel(sampler, faces[j], level0, x0, y1, 0, tx, 2); 676 get_texel(sampler, faces[j], level0, x1, y1, 0, tx, 3); 677 if (sampler->state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) { 678 shadow_compare(compare_func, tx, p, 0); 679 shadow_compare(compare_func, tx, p, 1); 680 shadow_compare(compare_func, tx, p, 2); 681 shadow_compare(compare_func, tx, p, 3); 682 } 683 684 for (c = 0; c < 4; c++) { 685 rgba[c][j] = lerp_2d(a, b, tx[c][0], tx[c][1], tx[c][2], tx[c][3]); 686 } 687 688 if (level0 != level1) { 689 /* get texels from second mipmap level and blend */ 690 float rgba2[4][4]; 691 x0 = x0 / 2; 692 y0 = y0 / 2; 693 x1 = x1 / 2; 694 y1 = y1 / 2; 695 get_texel(sampler, faces[j], level1, x0, y0, 0, tx, 0); 696 get_texel(sampler, faces[j], level1, x1, y0, 0, tx, 1); 697 get_texel(sampler, faces[j], level1, x0, y1, 0, tx, 2); 698 get_texel(sampler, faces[j], level1, x1, y1, 0, tx, 3); 699 if (sampler->state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){ 700 shadow_compare(compare_func, tx, p, 0); 701 shadow_compare(compare_func, tx, p, 1); 702 shadow_compare(compare_func, tx, p, 2); 703 shadow_compare(compare_func, tx, p, 3); 704 } 705 706 for (c = 0; c < 4; c++) { 707 rgba2[c][j] = lerp_2d(a, b, 708 tx[c][0], tx[c][1], tx[c][2], tx[c][3]); 709 } 710 711 for (c = 0; c < NUM_CHANNELS; c++) { 712 rgba[c][j] = LERP(levelBlend, rgba[c][j], rgba2[c][j]); 713 } 714 } 715 } 716 break; 717 default: 718 assert(0); 719 } 720} 721 722 723static void 724sp_get_samples_1d(struct tgsi_sampler *sampler, 725 const float s[QUAD_SIZE], 726 const float t[QUAD_SIZE], 727 const float p[QUAD_SIZE], 728 float lodbias, 729 float rgba[NUM_CHANNELS][QUAD_SIZE]) 730{ 731 static const unsigned faces[4] = {0, 0, 0, 0}; 732 static const float tzero[4] = {0, 0, 0, 0}; 733 sp_get_samples_2d_common(sampler, s, tzero, NULL, lodbias, rgba, faces); 734} 735 736 737static void 738sp_get_samples_2d(struct tgsi_sampler *sampler, 739 const float s[QUAD_SIZE], 740 const float t[QUAD_SIZE], 741 const float p[QUAD_SIZE], 742 float lodbias, 743 float rgba[NUM_CHANNELS][QUAD_SIZE]) 744{ 745 static const unsigned faces[4] = {0, 0, 0, 0}; 746 sp_get_samples_2d_common(sampler, s, t, p, lodbias, rgba, faces); 747} 748 749 750static void 751sp_get_samples_3d(struct tgsi_sampler *sampler, 752 const float s[QUAD_SIZE], 753 const float t[QUAD_SIZE], 754 const float p[QUAD_SIZE], 755 float lodbias, 756 float rgba[NUM_CHANNELS][QUAD_SIZE]) 757{ 758 /* get/map pipe_surfaces corresponding to 3D tex slices */ 759 unsigned level0, level1, j, imgFilter; 760 int width, height, depth; 761 float levelBlend; 762 const uint face = 0; 763 764 choose_mipmap_levels(sampler, s, t, p, lodbias, 765 &level0, &level1, &levelBlend, &imgFilter); 766 767 if (sampler->state->normalized_coords) { 768 width = sampler->texture->width[level0]; 769 height = sampler->texture->height[level0]; 770 depth = sampler->texture->depth[level0]; 771 } 772 else { 773 width = height = depth = 1; 774 } 775 776 assert(width > 0); 777 assert(height > 0); 778 assert(depth > 0); 779 780 switch (imgFilter) { 781 case PIPE_TEX_FILTER_NEAREST: 782 for (j = 0; j < QUAD_SIZE; j++) { 783 int x = nearest_texcoord(sampler->state->wrap_s, s[j], width); 784 int y = nearest_texcoord(sampler->state->wrap_t, t[j], height); 785 int z = nearest_texcoord(sampler->state->wrap_r, p[j], depth); 786 get_texel(sampler, face, level0, x, y, z, rgba, j); 787 788 if (level0 != level1) { 789 /* get texels from second mipmap level and blend */ 790 float rgba2[4][4]; 791 unsigned c; 792 x /= 2; 793 y /= 2; 794 z /= 2; 795 get_texel(sampler, face, level1, x, y, z, rgba2, j); 796 for (c = 0; c < NUM_CHANNELS; c++) { 797 rgba[c][j] = LERP(levelBlend, rgba2[c][j], rgba[c][j]); 798 } 799 } 800 } 801 break; 802 case PIPE_TEX_FILTER_LINEAR: 803 for (j = 0; j < QUAD_SIZE; j++) { 804 float texel0[4][4], texel1[4][4]; 805 float xw, yw, zw; /* interpolation weights */ 806 int x0, x1, y0, y1, z0, z1, c; 807 linear_texcoord(sampler->state->wrap_s, s[j], width, &x0, &x1, &xw); 808 linear_texcoord(sampler->state->wrap_t, t[j], height, &y0, &y1, &yw); 809 linear_texcoord(sampler->state->wrap_r, p[j], depth, &z0, &z1, &zw); 810 get_texel(sampler, face, level0, x0, y0, z0, texel0, 0); 811 get_texel(sampler, face, level0, x1, y0, z0, texel0, 1); 812 get_texel(sampler, face, level0, x0, y1, z0, texel0, 2); 813 get_texel(sampler, face, level0, x1, y1, z0, texel0, 3); 814 get_texel(sampler, face, level0, x0, y0, z1, texel1, 0); 815 get_texel(sampler, face, level0, x1, y0, z1, texel1, 1); 816 get_texel(sampler, face, level0, x0, y1, z1, texel1, 2); 817 get_texel(sampler, face, level0, x1, y1, z1, texel1, 3); 818 819 /* 3D lerp */ 820 for (c = 0; c < 4; c++) { 821 float ctemp0[4][4], ctemp1[4][4]; 822 ctemp0[c][j] = lerp_2d(xw, yw, 823 texel0[c][0], texel0[c][1], 824 texel0[c][2], texel0[c][3]); 825 ctemp1[c][j] = lerp_2d(xw, yw, 826 texel1[c][0], texel1[c][1], 827 texel1[c][2], texel1[c][3]); 828 rgba[c][j] = LERP(zw, ctemp0[c][j], ctemp1[c][j]); 829 } 830 831 if (level0 != level1) { 832 /* get texels from second mipmap level and blend */ 833 float rgba2[4][4]; 834 x0 /= 2; 835 y0 /= 2; 836 z0 /= 2; 837 x1 /= 2; 838 y1 /= 2; 839 z1 /= 2; 840 get_texel(sampler, face, level1, x0, y0, z0, texel0, 0); 841 get_texel(sampler, face, level1, x1, y0, z0, texel0, 1); 842 get_texel(sampler, face, level1, x0, y1, z0, texel0, 2); 843 get_texel(sampler, face, level1, x1, y1, z0, texel0, 3); 844 get_texel(sampler, face, level1, x0, y0, z1, texel1, 0); 845 get_texel(sampler, face, level1, x1, y0, z1, texel1, 1); 846 get_texel(sampler, face, level1, x0, y1, z1, texel1, 2); 847 get_texel(sampler, face, level1, x1, y1, z1, texel1, 3); 848 849 /* 3D lerp */ 850 for (c = 0; c < 4; c++) { 851 float ctemp0[4][4], ctemp1[4][4]; 852 ctemp0[c][j] = lerp_2d(xw, yw, 853 texel0[c][0], texel0[c][1], 854 texel0[c][2], texel0[c][3]); 855 ctemp1[c][j] = lerp_2d(xw, yw, 856 texel1[c][0], texel1[c][1], 857 texel1[c][2], texel1[c][3]); 858 rgba2[c][j] = LERP(zw, ctemp0[c][j], ctemp1[c][j]); 859 } 860 861 /* blend mipmap levels */ 862 for (c = 0; c < NUM_CHANNELS; c++) { 863 rgba[c][j] = LERP(levelBlend, rgba[c][j], rgba2[c][j]); 864 } 865 } 866 } 867 break; 868 default: 869 assert(0); 870 } 871} 872 873 874static void 875sp_get_samples_cube(struct tgsi_sampler *sampler, 876 const float s[QUAD_SIZE], 877 const float t[QUAD_SIZE], 878 const float p[QUAD_SIZE], 879 float lodbias, 880 float rgba[NUM_CHANNELS][QUAD_SIZE]) 881{ 882 unsigned faces[QUAD_SIZE], j; 883 float ssss[4], tttt[4]; 884 for (j = 0; j < QUAD_SIZE; j++) { 885 faces[j] = choose_cube_face(s[j], t[j], p[j], ssss + j, tttt + j); 886 } 887 sp_get_samples_2d_common(sampler, ssss, tttt, NULL, lodbias, rgba, faces); 888} 889 890 891/** 892 * Called via tgsi_sampler::get_samples() 893 * Use the sampler's state setting to get a filtered RGBA value 894 * from the sampler's texture. 895 * 896 * XXX we can implement many versions of this function, each 897 * tightly coded for a specific combination of sampler state 898 * (nearest + repeat), (bilinear mipmap + clamp), etc. 899 * 900 * The update_samplers() function in st_atom_sampler.c could create 901 * a new tgsi_sampler object for each state combo it finds.... 902 */ 903void 904sp_get_samples(struct tgsi_sampler *sampler, 905 const float s[QUAD_SIZE], 906 const float t[QUAD_SIZE], 907 const float p[QUAD_SIZE], 908 float lodbias, 909 float rgba[NUM_CHANNELS][QUAD_SIZE]) 910{ 911 if (!sampler->texture) 912 return; 913 914 switch (sampler->texture->target) { 915 case PIPE_TEXTURE_1D: 916 sp_get_samples_1d(sampler, s, t, p, lodbias, rgba); 917 break; 918 case PIPE_TEXTURE_2D: 919 sp_get_samples_2d(sampler, s, t, p, lodbias, rgba); 920 break; 921 case PIPE_TEXTURE_3D: 922 sp_get_samples_3d(sampler, s, t, p, lodbias, rgba); 923 break; 924 case PIPE_TEXTURE_CUBE: 925 sp_get_samples_cube(sampler, s, t, p, lodbias, rgba); 926 break; 927 default: 928 assert(0); 929 } 930} 931 932