glsl: fix crash in loop analysis when some controls can't be determined
[mesa.git] / src / gallium / drivers / softpipe / sp_tex_sample.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008-2010 VMware, Inc. 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
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * Texture sampling
31 *
32 * Authors:
33 * Brian Paul
34 * Keith Whitwell
35 */
36
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "sp_quad.h" /* only for #define QUAD_* tokens */
43 #include "sp_tex_sample.h"
44 #include "sp_tex_tile_cache.h"
45
46
47 /** Set to one to help debug texture sampling */
48 #define DEBUG_TEX 0
49
50
51 /*
52 * Return fractional part of 'f'. Used for computing interpolation weights.
53 * Need to be careful with negative values.
54 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
55 * of improperly weighted linear-filtered textures.
56 * The tests/texwrap.c demo is a good test.
57 */
58 static INLINE float
59 frac(float f)
60 {
61 return f - floorf(f);
62 }
63
64
65
66 /**
67 * Linear interpolation macro
68 */
69 static INLINE float
70 lerp(float a, float v0, float v1)
71 {
72 return v0 + a * (v1 - v0);
73 }
74
75
76 /**
77 * Do 2D/bilinear interpolation of float values.
78 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
79 * a and b are the horizontal and vertical interpolants.
80 * It's important that this function is inlined when compiled with
81 * optimization! If we find that's not true on some systems, convert
82 * to a macro.
83 */
84 static INLINE float
85 lerp_2d(float a, float b,
86 float v00, float v10, float v01, float v11)
87 {
88 const float temp0 = lerp(a, v00, v10);
89 const float temp1 = lerp(a, v01, v11);
90 return lerp(b, temp0, temp1);
91 }
92
93
94 /**
95 * As above, but 3D interpolation of 8 values.
96 */
97 static INLINE float
98 lerp_3d(float a, float b, float c,
99 float v000, float v100, float v010, float v110,
100 float v001, float v101, float v011, float v111)
101 {
102 const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);
103 const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);
104 return lerp(c, temp0, temp1);
105 }
106
107
108
109 /**
110 * Compute coord % size for repeat wrap modes.
111 * Note that if coord is negative, coord % size doesn't give the right
112 * value. To avoid that problem we add a large multiple of the size
113 * (rather than using a conditional).
114 */
115 static INLINE int
116 repeat(int coord, unsigned size)
117 {
118 return (coord + size * 1024) % size;
119 }
120
121
122 /**
123 * Apply texture coord wrapping mode and return integer texture indexes
124 * for a vector of four texcoords (S or T or P).
125 * \param wrapMode PIPE_TEX_WRAP_x
126 * \param s the incoming texcoords
127 * \param size the texture image size
128 * \param icoord returns the integer texcoords
129 * \return integer texture index
130 */
131 static void
132 wrap_nearest_repeat(const float s[4], unsigned size, int icoord[4])
133 {
134 uint ch;
135 /* s limited to [0,1) */
136 /* i limited to [0,size-1] */
137 for (ch = 0; ch < 4; ch++) {
138 int i = util_ifloor(s[ch] * size);
139 icoord[ch] = repeat(i, size);
140 }
141 }
142
143
144 static void
145 wrap_nearest_clamp(const float s[4], unsigned size, int icoord[4])
146 {
147 uint ch;
148 /* s limited to [0,1] */
149 /* i limited to [0,size-1] */
150 for (ch = 0; ch < 4; ch++) {
151 if (s[ch] <= 0.0F)
152 icoord[ch] = 0;
153 else if (s[ch] >= 1.0F)
154 icoord[ch] = size - 1;
155 else
156 icoord[ch] = util_ifloor(s[ch] * size);
157 }
158 }
159
160
161 static void
162 wrap_nearest_clamp_to_edge(const float s[4], unsigned size, int icoord[4])
163 {
164 uint ch;
165 /* s limited to [min,max] */
166 /* i limited to [0, size-1] */
167 const float min = 1.0F / (2.0F * size);
168 const float max = 1.0F - min;
169 for (ch = 0; ch < 4; ch++) {
170 if (s[ch] < min)
171 icoord[ch] = 0;
172 else if (s[ch] > max)
173 icoord[ch] = size - 1;
174 else
175 icoord[ch] = util_ifloor(s[ch] * size);
176 }
177 }
178
179
180 static void
181 wrap_nearest_clamp_to_border(const float s[4], unsigned size, int icoord[4])
182 {
183 uint ch;
184 /* s limited to [min,max] */
185 /* i limited to [-1, size] */
186 const float min = -1.0F / (2.0F * size);
187 const float max = 1.0F - min;
188 for (ch = 0; ch < 4; ch++) {
189 if (s[ch] <= min)
190 icoord[ch] = -1;
191 else if (s[ch] >= max)
192 icoord[ch] = size;
193 else
194 icoord[ch] = util_ifloor(s[ch] * size);
195 }
196 }
197
198
199 static void
200 wrap_nearest_mirror_repeat(const float s[4], unsigned size, int icoord[4])
201 {
202 uint ch;
203 const float min = 1.0F / (2.0F * size);
204 const float max = 1.0F - min;
205 for (ch = 0; ch < 4; ch++) {
206 const int flr = util_ifloor(s[ch]);
207 float u = frac(s[ch]);
208 if (flr & 1)
209 u = 1.0F - u;
210 if (u < min)
211 icoord[ch] = 0;
212 else if (u > max)
213 icoord[ch] = size - 1;
214 else
215 icoord[ch] = util_ifloor(u * size);
216 }
217 }
218
219
220 static void
221 wrap_nearest_mirror_clamp(const float s[4], unsigned size, int icoord[4])
222 {
223 uint ch;
224 for (ch = 0; ch < 4; ch++) {
225 /* s limited to [0,1] */
226 /* i limited to [0,size-1] */
227 const float u = fabsf(s[ch]);
228 if (u <= 0.0F)
229 icoord[ch] = 0;
230 else if (u >= 1.0F)
231 icoord[ch] = size - 1;
232 else
233 icoord[ch] = util_ifloor(u * size);
234 }
235 }
236
237
238 static void
239 wrap_nearest_mirror_clamp_to_edge(const float s[4], unsigned size,
240 int icoord[4])
241 {
242 uint ch;
243 /* s limited to [min,max] */
244 /* i limited to [0, size-1] */
245 const float min = 1.0F / (2.0F * size);
246 const float max = 1.0F - min;
247 for (ch = 0; ch < 4; ch++) {
248 const float u = fabsf(s[ch]);
249 if (u < min)
250 icoord[ch] = 0;
251 else if (u > max)
252 icoord[ch] = size - 1;
253 else
254 icoord[ch] = util_ifloor(u * size);
255 }
256 }
257
258
259 static void
260 wrap_nearest_mirror_clamp_to_border(const float s[4], unsigned size,
261 int icoord[4])
262 {
263 uint ch;
264 /* s limited to [min,max] */
265 /* i limited to [0, size-1] */
266 const float min = -1.0F / (2.0F * size);
267 const float max = 1.0F - min;
268 for (ch = 0; ch < 4; ch++) {
269 const float u = fabsf(s[ch]);
270 if (u < min)
271 icoord[ch] = -1;
272 else if (u > max)
273 icoord[ch] = size;
274 else
275 icoord[ch] = util_ifloor(u * size);
276 }
277 }
278
279
280 /**
281 * Used to compute texel locations for linear sampling for four texcoords.
282 * \param wrapMode PIPE_TEX_WRAP_x
283 * \param s the texcoords
284 * \param size the texture image size
285 * \param icoord0 returns first texture indexes
286 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
287 * \param w returns blend factor/weight between texture indexes
288 * \param icoord returns the computed integer texture coords
289 */
290 static void
291 wrap_linear_repeat(const float s[4], unsigned size,
292 int icoord0[4], int icoord1[4], float w[4])
293 {
294 uint ch;
295 for (ch = 0; ch < 4; ch++) {
296 float u = s[ch] * size - 0.5F;
297 icoord0[ch] = repeat(util_ifloor(u), size);
298 icoord1[ch] = repeat(icoord0[ch] + 1, size);
299 w[ch] = frac(u);
300 }
301 }
302
303
304 static void
305 wrap_linear_clamp(const float s[4], unsigned size,
306 int icoord0[4], int icoord1[4], float w[4])
307 {
308 uint ch;
309 for (ch = 0; ch < 4; ch++) {
310 float u = CLAMP(s[ch], 0.0F, 1.0F);
311 u = u * size - 0.5f;
312 icoord0[ch] = util_ifloor(u);
313 icoord1[ch] = icoord0[ch] + 1;
314 w[ch] = frac(u);
315 }
316 }
317
318
319 static void
320 wrap_linear_clamp_to_edge(const float s[4], unsigned size,
321 int icoord0[4], int icoord1[4], float w[4])
322 {
323 uint ch;
324 for (ch = 0; ch < 4; ch++) {
325 float u = CLAMP(s[ch], 0.0F, 1.0F);
326 u = u * size - 0.5f;
327 icoord0[ch] = util_ifloor(u);
328 icoord1[ch] = icoord0[ch] + 1;
329 if (icoord0[ch] < 0)
330 icoord0[ch] = 0;
331 if (icoord1[ch] >= (int) size)
332 icoord1[ch] = size - 1;
333 w[ch] = frac(u);
334 }
335 }
336
337
338 static void
339 wrap_linear_clamp_to_border(const float s[4], unsigned size,
340 int icoord0[4], int icoord1[4], float w[4])
341 {
342 const float min = -1.0F / (2.0F * size);
343 const float max = 1.0F - min;
344 uint ch;
345 for (ch = 0; ch < 4; ch++) {
346 float u = CLAMP(s[ch], min, max);
347 u = u * size - 0.5f;
348 icoord0[ch] = util_ifloor(u);
349 icoord1[ch] = icoord0[ch] + 1;
350 w[ch] = frac(u);
351 }
352 }
353
354
355 static void
356 wrap_linear_mirror_repeat(const float s[4], unsigned size,
357 int icoord0[4], int icoord1[4], float w[4])
358 {
359 uint ch;
360 for (ch = 0; ch < 4; ch++) {
361 const int flr = util_ifloor(s[ch]);
362 float u = frac(s[ch]);
363 if (flr & 1)
364 u = 1.0F - u;
365 u = u * size - 0.5F;
366 icoord0[ch] = util_ifloor(u);
367 icoord1[ch] = icoord0[ch] + 1;
368 if (icoord0[ch] < 0)
369 icoord0[ch] = 0;
370 if (icoord1[ch] >= (int) size)
371 icoord1[ch] = size - 1;
372 w[ch] = frac(u);
373 }
374 }
375
376
377 static void
378 wrap_linear_mirror_clamp(const float s[4], unsigned size,
379 int icoord0[4], int icoord1[4], float w[4])
380 {
381 uint ch;
382 for (ch = 0; ch < 4; ch++) {
383 float u = fabsf(s[ch]);
384 if (u >= 1.0F)
385 u = (float) size;
386 else
387 u *= size;
388 u -= 0.5F;
389 icoord0[ch] = util_ifloor(u);
390 icoord1[ch] = icoord0[ch] + 1;
391 w[ch] = frac(u);
392 }
393 }
394
395
396 static void
397 wrap_linear_mirror_clamp_to_edge(const float s[4], unsigned size,
398 int icoord0[4], int icoord1[4], float w[4])
399 {
400 uint ch;
401 for (ch = 0; ch < 4; ch++) {
402 float u = fabsf(s[ch]);
403 if (u >= 1.0F)
404 u = (float) size;
405 else
406 u *= size;
407 u -= 0.5F;
408 icoord0[ch] = util_ifloor(u);
409 icoord1[ch] = icoord0[ch] + 1;
410 if (icoord0[ch] < 0)
411 icoord0[ch] = 0;
412 if (icoord1[ch] >= (int) size)
413 icoord1[ch] = size - 1;
414 w[ch] = frac(u);
415 }
416 }
417
418
419 static void
420 wrap_linear_mirror_clamp_to_border(const float s[4], unsigned size,
421 int icoord0[4], int icoord1[4], float w[4])
422 {
423 const float min = -1.0F / (2.0F * size);
424 const float max = 1.0F - min;
425 uint ch;
426 for (ch = 0; ch < 4; ch++) {
427 float u = fabsf(s[ch]);
428 if (u <= min)
429 u = min * size;
430 else if (u >= max)
431 u = max * size;
432 else
433 u *= size;
434 u -= 0.5F;
435 icoord0[ch] = util_ifloor(u);
436 icoord1[ch] = icoord0[ch] + 1;
437 w[ch] = frac(u);
438 }
439 }
440
441
442 /**
443 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
444 */
445 static void
446 wrap_nearest_unorm_clamp(const float s[4], unsigned size, int icoord[4])
447 {
448 uint ch;
449 for (ch = 0; ch < 4; ch++) {
450 int i = util_ifloor(s[ch]);
451 icoord[ch]= CLAMP(i, 0, (int) size-1);
452 }
453 }
454
455
456 /**
457 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
458 */
459 static void
460 wrap_nearest_unorm_clamp_to_border(const float s[4], unsigned size,
461 int icoord[4])
462 {
463 uint ch;
464 for (ch = 0; ch < 4; ch++) {
465 icoord[ch]= util_ifloor( CLAMP(s[ch], -0.5F, (float) size + 0.5F) );
466 }
467 }
468
469
470 /**
471 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
472 */
473 static void
474 wrap_nearest_unorm_clamp_to_edge(const float s[4], unsigned size,
475 int icoord[4])
476 {
477 uint ch;
478 for (ch = 0; ch < 4; ch++) {
479 icoord[ch]= util_ifloor( CLAMP(s[ch], 0.5F, (float) size - 0.5F) );
480 }
481 }
482
483
484 /**
485 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
486 */
487 static void
488 wrap_linear_unorm_clamp(const float s[4], unsigned size,
489 int icoord0[4], int icoord1[4], float w[4])
490 {
491 uint ch;
492 for (ch = 0; ch < 4; ch++) {
493 /* Not exactly what the spec says, but it matches NVIDIA output */
494 float u = CLAMP(s[ch] - 0.5F, 0.0f, (float) size - 1.0f);
495 icoord0[ch] = util_ifloor(u);
496 icoord1[ch] = icoord0[ch] + 1;
497 w[ch] = frac(u);
498 }
499 }
500
501
502 /**
503 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
504 */
505 static void
506 wrap_linear_unorm_clamp_to_border(const float s[4], unsigned size,
507 int icoord0[4], int icoord1[4], float w[4])
508 {
509 uint ch;
510 for (ch = 0; ch < 4; ch++) {
511 float u = CLAMP(s[ch], -0.5F, (float) size + 0.5F);
512 u -= 0.5F;
513 icoord0[ch] = util_ifloor(u);
514 icoord1[ch] = icoord0[ch] + 1;
515 if (icoord1[ch] > (int) size - 1)
516 icoord1[ch] = size - 1;
517 w[ch] = frac(u);
518 }
519 }
520
521
522 /**
523 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
524 */
525 static void
526 wrap_linear_unorm_clamp_to_edge(const float s[4], unsigned size,
527 int icoord0[4], int icoord1[4], float w[4])
528 {
529 uint ch;
530 for (ch = 0; ch < 4; ch++) {
531 float u = CLAMP(s[ch], +0.5F, (float) size - 0.5F);
532 u -= 0.5F;
533 icoord0[ch] = util_ifloor(u);
534 icoord1[ch] = icoord0[ch] + 1;
535 if (icoord1[ch] > (int) size - 1)
536 icoord1[ch] = size - 1;
537 w[ch] = frac(u);
538 }
539 }
540
541
542
543 /**
544 * Examine the quad's texture coordinates to compute the partial
545 * derivatives w.r.t X and Y, then compute lambda (level of detail).
546 */
547 static float
548 compute_lambda_1d(const struct sp_sampler_varient *samp,
549 const float s[QUAD_SIZE],
550 const float t[QUAD_SIZE],
551 const float p[QUAD_SIZE])
552 {
553 const struct pipe_resource *texture = samp->texture;
554 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
555 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
556 float rho = MAX2(dsdx, dsdy) * texture->width0;
557
558 return util_fast_log2(rho);
559 }
560
561
562 static float
563 compute_lambda_2d(const struct sp_sampler_varient *samp,
564 const float s[QUAD_SIZE],
565 const float t[QUAD_SIZE],
566 const float p[QUAD_SIZE])
567 {
568 const struct pipe_resource *texture = samp->texture;
569 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
570 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
571 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
572 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
573 float maxx = MAX2(dsdx, dsdy) * texture->width0;
574 float maxy = MAX2(dtdx, dtdy) * texture->height0;
575 float rho = MAX2(maxx, maxy);
576
577 return util_fast_log2(rho);
578 }
579
580
581 static float
582 compute_lambda_3d(const struct sp_sampler_varient *samp,
583 const float s[QUAD_SIZE],
584 const float t[QUAD_SIZE],
585 const float p[QUAD_SIZE])
586 {
587 const struct pipe_resource *texture = samp->texture;
588 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
589 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
590 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
591 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
592 float dpdx = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);
593 float dpdy = fabsf(p[QUAD_TOP_LEFT] - p[QUAD_BOTTOM_LEFT]);
594 float maxx = MAX2(dsdx, dsdy) * texture->width0;
595 float maxy = MAX2(dtdx, dtdy) * texture->height0;
596 float maxz = MAX2(dpdx, dpdy) * texture->depth0;
597 float rho;
598
599 rho = MAX2(maxx, maxy);
600 rho = MAX2(rho, maxz);
601
602 return util_fast_log2(rho);
603 }
604
605
606 /**
607 * Compute lambda for a vertex texture sampler.
608 * Since there aren't derivatives to use, just return 0.
609 */
610 static float
611 compute_lambda_vert(const struct sp_sampler_varient *samp,
612 const float s[QUAD_SIZE],
613 const float t[QUAD_SIZE],
614 const float p[QUAD_SIZE])
615 {
616 return 0.0f;
617 }
618
619
620
621 /**
622 * Get a texel from a texture, using the texture tile cache.
623 *
624 * \param addr the template tex address containing cube, z, face info.
625 * \param x the x coord of texel within 2D image
626 * \param y the y coord of texel within 2D image
627 * \param rgba the quad to put the texel/color into
628 *
629 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
630 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
631 */
632
633
634
635
636 static INLINE const float *
637 get_texel_2d_no_border(const struct sp_sampler_varient *samp,
638 union tex_tile_address addr, int x, int y)
639 {
640 const struct softpipe_tex_cached_tile *tile;
641
642 addr.bits.x = x / TILE_SIZE;
643 addr.bits.y = y / TILE_SIZE;
644 y %= TILE_SIZE;
645 x %= TILE_SIZE;
646
647 tile = sp_get_cached_tile_tex(samp->cache, addr);
648
649 return &tile->data.color[y][x][0];
650 }
651
652
653 static INLINE const float *
654 get_texel_2d(const struct sp_sampler_varient *samp,
655 union tex_tile_address addr, int x, int y)
656 {
657 const struct pipe_resource *texture = samp->texture;
658 unsigned level = addr.bits.level;
659
660 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
661 y < 0 || y >= (int) u_minify(texture->height0, level)) {
662 return sp_tex_tile_cache_border_color(samp->cache,
663 samp->sampler->border_color);
664 }
665 else {
666 return get_texel_2d_no_border( samp, addr, x, y );
667 }
668 }
669
670
671 /* Gather a quad of adjacent texels within a tile:
672 */
673 static INLINE void
674 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient *samp,
675 union tex_tile_address addr,
676 unsigned x, unsigned y,
677 const float *out[4])
678 {
679 const struct softpipe_tex_cached_tile *tile;
680
681 addr.bits.x = x / TILE_SIZE;
682 addr.bits.y = y / TILE_SIZE;
683 y %= TILE_SIZE;
684 x %= TILE_SIZE;
685
686 tile = sp_get_cached_tile_tex(samp->cache, addr);
687
688 out[0] = &tile->data.color[y ][x ][0];
689 out[1] = &tile->data.color[y ][x+1][0];
690 out[2] = &tile->data.color[y+1][x ][0];
691 out[3] = &tile->data.color[y+1][x+1][0];
692 }
693
694
695 /* Gather a quad of potentially non-adjacent texels:
696 */
697 static INLINE void
698 get_texel_quad_2d_no_border(const struct sp_sampler_varient *samp,
699 union tex_tile_address addr,
700 int x0, int y0,
701 int x1, int y1,
702 const float *out[4])
703 {
704 out[0] = get_texel_2d_no_border( samp, addr, x0, y0 );
705 out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
706 out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
707 out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
708 }
709
710 /* Can involve a lot of unnecessary checks for border color:
711 */
712 static INLINE void
713 get_texel_quad_2d(const struct sp_sampler_varient *samp,
714 union tex_tile_address addr,
715 int x0, int y0,
716 int x1, int y1,
717 const float *out[4])
718 {
719 out[0] = get_texel_2d( samp, addr, x0, y0 );
720 out[1] = get_texel_2d( samp, addr, x1, y0 );
721 out[3] = get_texel_2d( samp, addr, x1, y1 );
722 out[2] = get_texel_2d( samp, addr, x0, y1 );
723 }
724
725
726
727 /* 3d varients:
728 */
729 static INLINE const float *
730 get_texel_3d_no_border(const struct sp_sampler_varient *samp,
731 union tex_tile_address addr, int x, int y, int z)
732 {
733 const struct softpipe_tex_cached_tile *tile;
734
735 addr.bits.x = x / TILE_SIZE;
736 addr.bits.y = y / TILE_SIZE;
737 addr.bits.z = z;
738 y %= TILE_SIZE;
739 x %= TILE_SIZE;
740
741 tile = sp_get_cached_tile_tex(samp->cache, addr);
742
743 return &tile->data.color[y][x][0];
744 }
745
746
747 static INLINE const float *
748 get_texel_3d(const struct sp_sampler_varient *samp,
749 union tex_tile_address addr, int x, int y, int z)
750 {
751 const struct pipe_resource *texture = samp->texture;
752 unsigned level = addr.bits.level;
753
754 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
755 y < 0 || y >= (int) u_minify(texture->height0, level) ||
756 z < 0 || z >= (int) u_minify(texture->depth0, level)) {
757 return sp_tex_tile_cache_border_color(samp->cache,
758 samp->sampler->border_color);
759 }
760 else {
761 return get_texel_3d_no_border( samp, addr, x, y, z );
762 }
763 }
764
765
766 /**
767 * Given the logbase2 of a mipmap's base level size and a mipmap level,
768 * return the size (in texels) of that mipmap level.
769 * For example, if level[0].width = 256 then base_pot will be 8.
770 * If level = 2, then we'll return 64 (the width at level=2).
771 * Return 1 if level > base_pot.
772 */
773 static INLINE unsigned
774 pot_level_size(unsigned base_pot, unsigned level)
775 {
776 return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
777 }
778
779
780 static void
781 print_sample(const char *function, float rgba[NUM_CHANNELS][QUAD_SIZE])
782 {
783 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
784 function,
785 rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
786 rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
787 rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
788 rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
789 }
790
791
792 /* Some image-filter fastpaths:
793 */
794 static INLINE void
795 img_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
796 const float s[QUAD_SIZE],
797 const float t[QUAD_SIZE],
798 const float p[QUAD_SIZE],
799 const float c0[QUAD_SIZE],
800 enum tgsi_sampler_control control,
801 float rgba[NUM_CHANNELS][QUAD_SIZE])
802 {
803 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
804 unsigned j;
805 unsigned level = samp->level;
806 unsigned xpot = pot_level_size(samp->xpot, level);
807 unsigned ypot = pot_level_size(samp->ypot, level);
808 unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
809 unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
810 union tex_tile_address addr;
811
812 addr.value = 0;
813 addr.bits.level = samp->level;
814
815 for (j = 0; j < QUAD_SIZE; j++) {
816 int c;
817
818 float u = s[j] * xpot - 0.5F;
819 float v = t[j] * ypot - 0.5F;
820
821 int uflr = util_ifloor(u);
822 int vflr = util_ifloor(v);
823
824 float xw = u - (float)uflr;
825 float yw = v - (float)vflr;
826
827 int x0 = uflr & (xpot - 1);
828 int y0 = vflr & (ypot - 1);
829
830 const float *tx[4];
831
832 /* Can we fetch all four at once:
833 */
834 if (x0 < xmax && y0 < ymax) {
835 get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
836 }
837 else {
838 unsigned x1 = (x0 + 1) & (xpot - 1);
839 unsigned y1 = (y0 + 1) & (ypot - 1);
840 get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
841 }
842
843 /* interpolate R, G, B, A */
844 for (c = 0; c < 4; c++) {
845 rgba[c][j] = lerp_2d(xw, yw,
846 tx[0][c], tx[1][c],
847 tx[2][c], tx[3][c]);
848 }
849 }
850
851 if (DEBUG_TEX) {
852 print_sample(__FUNCTION__, rgba);
853 }
854 }
855
856
857 static INLINE void
858 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
859 const float s[QUAD_SIZE],
860 const float t[QUAD_SIZE],
861 const float p[QUAD_SIZE],
862 const float c0[QUAD_SIZE],
863 enum tgsi_sampler_control control,
864 float rgba[NUM_CHANNELS][QUAD_SIZE])
865 {
866 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
867 unsigned j;
868 unsigned level = samp->level;
869 unsigned xpot = pot_level_size(samp->xpot, level);
870 unsigned ypot = pot_level_size(samp->ypot, level);
871 union tex_tile_address addr;
872
873 addr.value = 0;
874 addr.bits.level = samp->level;
875
876 for (j = 0; j < QUAD_SIZE; j++) {
877 int c;
878
879 float u = s[j] * xpot;
880 float v = t[j] * ypot;
881
882 int uflr = util_ifloor(u);
883 int vflr = util_ifloor(v);
884
885 int x0 = uflr & (xpot - 1);
886 int y0 = vflr & (ypot - 1);
887
888 const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
889
890 for (c = 0; c < 4; c++) {
891 rgba[c][j] = out[c];
892 }
893 }
894
895 if (DEBUG_TEX) {
896 print_sample(__FUNCTION__, rgba);
897 }
898 }
899
900
901 static INLINE void
902 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
903 const float s[QUAD_SIZE],
904 const float t[QUAD_SIZE],
905 const float p[QUAD_SIZE],
906 const float c0[QUAD_SIZE],
907 enum tgsi_sampler_control control,
908 float rgba[NUM_CHANNELS][QUAD_SIZE])
909 {
910 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
911 unsigned j;
912 unsigned level = samp->level;
913 unsigned xpot = pot_level_size(samp->xpot, level);
914 unsigned ypot = pot_level_size(samp->ypot, level);
915 union tex_tile_address addr;
916
917 addr.value = 0;
918 addr.bits.level = samp->level;
919
920 for (j = 0; j < QUAD_SIZE; j++) {
921 int c;
922
923 float u = s[j] * xpot;
924 float v = t[j] * ypot;
925
926 int x0, y0;
927 const float *out;
928
929 x0 = util_ifloor(u);
930 if (x0 < 0)
931 x0 = 0;
932 else if (x0 > xpot - 1)
933 x0 = xpot - 1;
934
935 y0 = util_ifloor(v);
936 if (y0 < 0)
937 y0 = 0;
938 else if (y0 > ypot - 1)
939 y0 = ypot - 1;
940
941 out = get_texel_2d_no_border(samp, addr, x0, y0);
942
943 for (c = 0; c < 4; c++) {
944 rgba[c][j] = out[c];
945 }
946 }
947
948 if (DEBUG_TEX) {
949 print_sample(__FUNCTION__, rgba);
950 }
951 }
952
953
954 static void
955 img_filter_1d_nearest(struct tgsi_sampler *tgsi_sampler,
956 const float s[QUAD_SIZE],
957 const float t[QUAD_SIZE],
958 const float p[QUAD_SIZE],
959 const float c0[QUAD_SIZE],
960 enum tgsi_sampler_control control,
961 float rgba[NUM_CHANNELS][QUAD_SIZE])
962 {
963 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
964 const struct pipe_resource *texture = samp->texture;
965 unsigned level0, j;
966 int width;
967 int x[4];
968 union tex_tile_address addr;
969
970 level0 = samp->level;
971 width = u_minify(texture->width0, level0);
972
973 assert(width > 0);
974
975 addr.value = 0;
976 addr.bits.level = samp->level;
977
978 samp->nearest_texcoord_s(s, width, x);
979
980 for (j = 0; j < QUAD_SIZE; j++) {
981 const float *out = get_texel_2d(samp, addr, x[j], 0);
982 int c;
983 for (c = 0; c < 4; c++) {
984 rgba[c][j] = out[c];
985 }
986 }
987
988 if (DEBUG_TEX) {
989 print_sample(__FUNCTION__, rgba);
990 }
991 }
992
993
994 static void
995 img_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
996 const float s[QUAD_SIZE],
997 const float t[QUAD_SIZE],
998 const float p[QUAD_SIZE],
999 const float c0[QUAD_SIZE],
1000 enum tgsi_sampler_control control,
1001 float rgba[NUM_CHANNELS][QUAD_SIZE])
1002 {
1003 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1004 const struct pipe_resource *texture = samp->texture;
1005 unsigned level0, j;
1006 int width, height;
1007 int x[4], y[4];
1008 union tex_tile_address addr;
1009
1010
1011 level0 = samp->level;
1012 width = u_minify(texture->width0, level0);
1013 height = u_minify(texture->height0, level0);
1014
1015 assert(width > 0);
1016 assert(height > 0);
1017
1018 addr.value = 0;
1019 addr.bits.level = samp->level;
1020
1021 samp->nearest_texcoord_s(s, width, x);
1022 samp->nearest_texcoord_t(t, height, y);
1023
1024 for (j = 0; j < QUAD_SIZE; j++) {
1025 const float *out = get_texel_2d(samp, addr, x[j], y[j]);
1026 int c;
1027 for (c = 0; c < 4; c++) {
1028 rgba[c][j] = out[c];
1029 }
1030 }
1031
1032 if (DEBUG_TEX) {
1033 print_sample(__FUNCTION__, rgba);
1034 }
1035 }
1036
1037
1038 static INLINE union tex_tile_address
1039 face(union tex_tile_address addr, unsigned face )
1040 {
1041 addr.bits.face = face;
1042 return addr;
1043 }
1044
1045
1046 static void
1047 img_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
1048 const float s[QUAD_SIZE],
1049 const float t[QUAD_SIZE],
1050 const float p[QUAD_SIZE],
1051 const float c0[QUAD_SIZE],
1052 enum tgsi_sampler_control control,
1053 float rgba[NUM_CHANNELS][QUAD_SIZE])
1054 {
1055 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1056 const struct pipe_resource *texture = samp->texture;
1057 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1058 unsigned level0, j;
1059 int width, height;
1060 int x[4], y[4];
1061 union tex_tile_address addr;
1062
1063 level0 = samp->level;
1064 width = u_minify(texture->width0, level0);
1065 height = u_minify(texture->height0, level0);
1066
1067 assert(width > 0);
1068 assert(height > 0);
1069
1070 addr.value = 0;
1071 addr.bits.level = samp->level;
1072
1073 samp->nearest_texcoord_s(s, width, x);
1074 samp->nearest_texcoord_t(t, height, y);
1075
1076 for (j = 0; j < QUAD_SIZE; j++) {
1077 const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
1078 int c;
1079 for (c = 0; c < 4; c++) {
1080 rgba[c][j] = out[c];
1081 }
1082 }
1083
1084 if (DEBUG_TEX) {
1085 print_sample(__FUNCTION__, rgba);
1086 }
1087 }
1088
1089
1090 static void
1091 img_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
1092 const float s[QUAD_SIZE],
1093 const float t[QUAD_SIZE],
1094 const float p[QUAD_SIZE],
1095 const float c0[QUAD_SIZE],
1096 enum tgsi_sampler_control control,
1097 float rgba[NUM_CHANNELS][QUAD_SIZE])
1098 {
1099 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1100 const struct pipe_resource *texture = samp->texture;
1101 unsigned level0, j;
1102 int width, height, depth;
1103 int x[4], y[4], z[4];
1104 union tex_tile_address addr;
1105
1106 level0 = samp->level;
1107 width = u_minify(texture->width0, level0);
1108 height = u_minify(texture->height0, level0);
1109 depth = u_minify(texture->depth0, level0);
1110
1111 assert(width > 0);
1112 assert(height > 0);
1113 assert(depth > 0);
1114
1115 samp->nearest_texcoord_s(s, width, x);
1116 samp->nearest_texcoord_t(t, height, y);
1117 samp->nearest_texcoord_p(p, depth, z);
1118
1119 addr.value = 0;
1120 addr.bits.level = samp->level;
1121
1122 for (j = 0; j < QUAD_SIZE; j++) {
1123 const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
1124 int c;
1125 for (c = 0; c < 4; c++) {
1126 rgba[c][j] = out[c];
1127 }
1128 }
1129 }
1130
1131
1132 static void
1133 img_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
1134 const float s[QUAD_SIZE],
1135 const float t[QUAD_SIZE],
1136 const float p[QUAD_SIZE],
1137 const float c0[QUAD_SIZE],
1138 enum tgsi_sampler_control control,
1139 float rgba[NUM_CHANNELS][QUAD_SIZE])
1140 {
1141 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1142 const struct pipe_resource *texture = samp->texture;
1143 unsigned level0, j;
1144 int width;
1145 int x0[4], x1[4];
1146 float xw[4]; /* weights */
1147 union tex_tile_address addr;
1148
1149 level0 = samp->level;
1150 width = u_minify(texture->width0, level0);
1151
1152 assert(width > 0);
1153
1154 addr.value = 0;
1155 addr.bits.level = samp->level;
1156
1157 samp->linear_texcoord_s(s, width, x0, x1, xw);
1158
1159 for (j = 0; j < QUAD_SIZE; j++) {
1160 const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
1161 const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
1162 int c;
1163
1164 /* interpolate R, G, B, A */
1165 for (c = 0; c < 4; c++) {
1166 rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1167 }
1168 }
1169 }
1170
1171
1172 static void
1173 img_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
1174 const float s[QUAD_SIZE],
1175 const float t[QUAD_SIZE],
1176 const float p[QUAD_SIZE],
1177 const float c0[QUAD_SIZE],
1178 enum tgsi_sampler_control control,
1179 float rgba[NUM_CHANNELS][QUAD_SIZE])
1180 {
1181 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1182 const struct pipe_resource *texture = samp->texture;
1183 unsigned level0, j;
1184 int width, height;
1185 int x0[4], y0[4], x1[4], y1[4];
1186 float xw[4], yw[4]; /* weights */
1187 union tex_tile_address addr;
1188
1189 level0 = samp->level;
1190 width = u_minify(texture->width0, level0);
1191 height = u_minify(texture->height0, level0);
1192
1193 assert(width > 0);
1194 assert(height > 0);
1195
1196 addr.value = 0;
1197 addr.bits.level = samp->level;
1198
1199 samp->linear_texcoord_s(s, width, x0, x1, xw);
1200 samp->linear_texcoord_t(t, height, y0, y1, yw);
1201
1202 for (j = 0; j < QUAD_SIZE; j++) {
1203 const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
1204 const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
1205 const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
1206 const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
1207 int c;
1208
1209 /* interpolate R, G, B, A */
1210 for (c = 0; c < 4; c++) {
1211 rgba[c][j] = lerp_2d(xw[j], yw[j],
1212 tx0[c], tx1[c],
1213 tx2[c], tx3[c]);
1214 }
1215 }
1216 }
1217
1218
1219 static void
1220 img_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1221 const float s[QUAD_SIZE],
1222 const float t[QUAD_SIZE],
1223 const float p[QUAD_SIZE],
1224 const float c0[QUAD_SIZE],
1225 enum tgsi_sampler_control control,
1226 float rgba[NUM_CHANNELS][QUAD_SIZE])
1227 {
1228 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1229 const struct pipe_resource *texture = samp->texture;
1230 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1231 unsigned level0, j;
1232 int width, height;
1233 int x0[4], y0[4], x1[4], y1[4];
1234 float xw[4], yw[4]; /* weights */
1235 union tex_tile_address addr;
1236
1237 level0 = samp->level;
1238 width = u_minify(texture->width0, level0);
1239 height = u_minify(texture->height0, level0);
1240
1241 assert(width > 0);
1242 assert(height > 0);
1243
1244 addr.value = 0;
1245 addr.bits.level = samp->level;
1246
1247 samp->linear_texcoord_s(s, width, x0, x1, xw);
1248 samp->linear_texcoord_t(t, height, y0, y1, yw);
1249
1250 for (j = 0; j < QUAD_SIZE; j++) {
1251 union tex_tile_address addrj = face(addr, faces[j]);
1252 const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
1253 const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
1254 const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
1255 const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
1256 int c;
1257
1258 /* interpolate R, G, B, A */
1259 for (c = 0; c < 4; c++) {
1260 rgba[c][j] = lerp_2d(xw[j], yw[j],
1261 tx0[c], tx1[c],
1262 tx2[c], tx3[c]);
1263 }
1264 }
1265 }
1266
1267
1268 static void
1269 img_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
1270 const float s[QUAD_SIZE],
1271 const float t[QUAD_SIZE],
1272 const float p[QUAD_SIZE],
1273 const float c0[QUAD_SIZE],
1274 enum tgsi_sampler_control control,
1275 float rgba[NUM_CHANNELS][QUAD_SIZE])
1276 {
1277 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1278 const struct pipe_resource *texture = samp->texture;
1279 unsigned level0, j;
1280 int width, height, depth;
1281 int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
1282 float xw[4], yw[4], zw[4]; /* interpolation weights */
1283 union tex_tile_address addr;
1284
1285 level0 = samp->level;
1286 width = u_minify(texture->width0, level0);
1287 height = u_minify(texture->height0, level0);
1288 depth = u_minify(texture->depth0, level0);
1289
1290 addr.value = 0;
1291 addr.bits.level = level0;
1292
1293 assert(width > 0);
1294 assert(height > 0);
1295 assert(depth > 0);
1296
1297 samp->linear_texcoord_s(s, width, x0, x1, xw);
1298 samp->linear_texcoord_t(t, height, y0, y1, yw);
1299 samp->linear_texcoord_p(p, depth, z0, z1, zw);
1300
1301 for (j = 0; j < QUAD_SIZE; j++) {
1302 int c;
1303
1304 const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
1305 const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
1306 const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
1307 const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
1308
1309 const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
1310 const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
1311 const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
1312 const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
1313
1314 /* interpolate R, G, B, A */
1315 for (c = 0; c < 4; c++) {
1316 rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
1317 tx00[c], tx01[c],
1318 tx02[c], tx03[c],
1319 tx10[c], tx11[c],
1320 tx12[c], tx13[c]);
1321 }
1322 }
1323 }
1324
1325
1326 /* Calculate level of detail for every fragment.
1327 * Note that lambda has already been biased by global LOD bias.
1328 */
1329 static INLINE void
1330 compute_lod(const struct pipe_sampler_state *sampler,
1331 const float biased_lambda,
1332 const float lodbias[QUAD_SIZE],
1333 float lod[QUAD_SIZE])
1334 {
1335 uint i;
1336
1337 for (i = 0; i < QUAD_SIZE; i++) {
1338 lod[i] = biased_lambda + lodbias[i];
1339 lod[i] = CLAMP(lod[i], sampler->min_lod, sampler->max_lod);
1340 }
1341 }
1342
1343
1344 static void
1345 mip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1346 const float s[QUAD_SIZE],
1347 const float t[QUAD_SIZE],
1348 const float p[QUAD_SIZE],
1349 const float c0[QUAD_SIZE],
1350 enum tgsi_sampler_control control,
1351 float rgba[NUM_CHANNELS][QUAD_SIZE])
1352 {
1353 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1354 const struct pipe_resource *texture = samp->texture;
1355 int level0;
1356 float lambda;
1357 float lod[QUAD_SIZE];
1358
1359 if (control == tgsi_sampler_lod_bias) {
1360 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1361 compute_lod(samp->sampler, lambda, c0, lod);
1362 } else {
1363 assert(control == tgsi_sampler_lod_explicit);
1364
1365 memcpy(lod, c0, sizeof(lod));
1366 }
1367
1368 /* XXX: Take into account all lod values.
1369 */
1370 lambda = lod[0];
1371 level0 = (int)lambda;
1372
1373 if (lambda < 0.0) {
1374 samp->level = 0;
1375 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1376 }
1377 else if (level0 >= texture->last_level) {
1378 samp->level = texture->last_level;
1379 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1380 }
1381 else {
1382 float levelBlend = lambda - level0;
1383 float rgba0[4][4];
1384 float rgba1[4][4];
1385 int c,j;
1386
1387 samp->level = level0;
1388 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1389
1390 samp->level = level0+1;
1391 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1392
1393 for (j = 0; j < QUAD_SIZE; j++) {
1394 for (c = 0; c < 4; c++) {
1395 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1396 }
1397 }
1398 }
1399
1400 if (DEBUG_TEX) {
1401 print_sample(__FUNCTION__, rgba);
1402 }
1403 }
1404
1405
1406 /**
1407 * Compute nearest mipmap level from texcoords.
1408 * Then sample the texture level for four elements of a quad.
1409 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1410 */
1411 static void
1412 mip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
1413 const float s[QUAD_SIZE],
1414 const float t[QUAD_SIZE],
1415 const float p[QUAD_SIZE],
1416 const float c0[QUAD_SIZE],
1417 enum tgsi_sampler_control control,
1418 float rgba[NUM_CHANNELS][QUAD_SIZE])
1419 {
1420 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1421 const struct pipe_resource *texture = samp->texture;
1422 float lambda;
1423 float lod[QUAD_SIZE];
1424
1425 if (control == tgsi_sampler_lod_bias) {
1426 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1427 compute_lod(samp->sampler, lambda, c0, lod);
1428 } else {
1429 assert(control == tgsi_sampler_lod_explicit);
1430
1431 memcpy(lod, c0, sizeof(lod));
1432 }
1433
1434 /* XXX: Take into account all lod values.
1435 */
1436 lambda = lod[0];
1437
1438 if (lambda < 0.0) {
1439 samp->level = 0;
1440 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1441 }
1442 else {
1443 samp->level = (int)(lambda + 0.5) ;
1444 samp->level = MIN2(samp->level, (int)texture->last_level);
1445 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1446 }
1447
1448 if (DEBUG_TEX) {
1449 print_sample(__FUNCTION__, rgba);
1450 }
1451 }
1452
1453
1454 static void
1455 mip_filter_none(struct tgsi_sampler *tgsi_sampler,
1456 const float s[QUAD_SIZE],
1457 const float t[QUAD_SIZE],
1458 const float p[QUAD_SIZE],
1459 const float c0[QUAD_SIZE],
1460 enum tgsi_sampler_control control,
1461 float rgba[NUM_CHANNELS][QUAD_SIZE])
1462 {
1463 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1464 float lambda;
1465 float lod[QUAD_SIZE];
1466
1467 if (control == tgsi_sampler_lod_bias) {
1468 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1469 compute_lod(samp->sampler, lambda, c0, lod);
1470 } else {
1471 assert(control == tgsi_sampler_lod_explicit);
1472
1473 memcpy(lod, c0, sizeof(lod));
1474 }
1475
1476 /* XXX: Take into account all lod values.
1477 */
1478 lambda = lod[0];
1479
1480 if (lambda < 0.0) {
1481 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1482 }
1483 else {
1484 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1485 }
1486 }
1487
1488
1489
1490 /**
1491 * Specialized version of mip_filter_linear with hard-wired calls to
1492 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1493 */
1494 static void
1495 mip_filter_linear_2d_linear_repeat_POT(
1496 struct tgsi_sampler *tgsi_sampler,
1497 const float s[QUAD_SIZE],
1498 const float t[QUAD_SIZE],
1499 const float p[QUAD_SIZE],
1500 const float c0[QUAD_SIZE],
1501 enum tgsi_sampler_control control,
1502 float rgba[NUM_CHANNELS][QUAD_SIZE])
1503 {
1504 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1505 const struct pipe_resource *texture = samp->texture;
1506 int level0;
1507 float lambda;
1508 float lod[QUAD_SIZE];
1509
1510 if (control == tgsi_sampler_lod_bias) {
1511 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1512 compute_lod(samp->sampler, lambda, c0, lod);
1513 } else {
1514 assert(control == tgsi_sampler_lod_explicit);
1515
1516 memcpy(lod, c0, sizeof(lod));
1517 }
1518
1519 /* XXX: Take into account all lod values.
1520 */
1521 lambda = lod[0];
1522 level0 = (int)lambda;
1523
1524 /* Catches both negative and large values of level0:
1525 */
1526 if ((unsigned)level0 >= texture->last_level) {
1527 if (level0 < 0)
1528 samp->level = 0;
1529 else
1530 samp->level = texture->last_level;
1531
1532 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1533 }
1534 else {
1535 float levelBlend = lambda - level0;
1536 float rgba0[4][4];
1537 float rgba1[4][4];
1538 int c,j;
1539
1540 samp->level = level0;
1541 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1542
1543 samp->level = level0+1;
1544 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1545
1546 for (j = 0; j < QUAD_SIZE; j++) {
1547 for (c = 0; c < 4; c++) {
1548 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1549 }
1550 }
1551 }
1552
1553 if (DEBUG_TEX) {
1554 print_sample(__FUNCTION__, rgba);
1555 }
1556 }
1557
1558
1559
1560 /**
1561 * Do shadow/depth comparisons.
1562 */
1563 static void
1564 sample_compare(struct tgsi_sampler *tgsi_sampler,
1565 const float s[QUAD_SIZE],
1566 const float t[QUAD_SIZE],
1567 const float p[QUAD_SIZE],
1568 const float c0[QUAD_SIZE],
1569 enum tgsi_sampler_control control,
1570 float rgba[NUM_CHANNELS][QUAD_SIZE])
1571 {
1572 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1573 const struct pipe_sampler_state *sampler = samp->sampler;
1574 int j, k0, k1, k2, k3;
1575 float val;
1576
1577 samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
1578
1579 /**
1580 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1581 * When we sampled the depth texture, the depth value was put into all
1582 * RGBA channels. We look at the red channel here.
1583 */
1584
1585 /* compare four texcoords vs. four texture samples */
1586 switch (sampler->compare_func) {
1587 case PIPE_FUNC_LESS:
1588 k0 = p[0] < rgba[0][0];
1589 k1 = p[1] < rgba[0][1];
1590 k2 = p[2] < rgba[0][2];
1591 k3 = p[3] < rgba[0][3];
1592 break;
1593 case PIPE_FUNC_LEQUAL:
1594 k0 = p[0] <= rgba[0][0];
1595 k1 = p[1] <= rgba[0][1];
1596 k2 = p[2] <= rgba[0][2];
1597 k3 = p[3] <= rgba[0][3];
1598 break;
1599 case PIPE_FUNC_GREATER:
1600 k0 = p[0] > rgba[0][0];
1601 k1 = p[1] > rgba[0][1];
1602 k2 = p[2] > rgba[0][2];
1603 k3 = p[3] > rgba[0][3];
1604 break;
1605 case PIPE_FUNC_GEQUAL:
1606 k0 = p[0] >= rgba[0][0];
1607 k1 = p[1] >= rgba[0][1];
1608 k2 = p[2] >= rgba[0][2];
1609 k3 = p[3] >= rgba[0][3];
1610 break;
1611 case PIPE_FUNC_EQUAL:
1612 k0 = p[0] == rgba[0][0];
1613 k1 = p[1] == rgba[0][1];
1614 k2 = p[2] == rgba[0][2];
1615 k3 = p[3] == rgba[0][3];
1616 break;
1617 case PIPE_FUNC_NOTEQUAL:
1618 k0 = p[0] != rgba[0][0];
1619 k1 = p[1] != rgba[0][1];
1620 k2 = p[2] != rgba[0][2];
1621 k3 = p[3] != rgba[0][3];
1622 break;
1623 case PIPE_FUNC_ALWAYS:
1624 k0 = k1 = k2 = k3 = 1;
1625 break;
1626 case PIPE_FUNC_NEVER:
1627 k0 = k1 = k2 = k3 = 0;
1628 break;
1629 default:
1630 k0 = k1 = k2 = k3 = 0;
1631 assert(0);
1632 break;
1633 }
1634
1635 /* convert four pass/fail values to an intensity in [0,1] */
1636 val = 0.25F * (k0 + k1 + k2 + k3);
1637
1638 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1639 for (j = 0; j < 4; j++) {
1640 rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
1641 rgba[3][j] = 1.0F;
1642 }
1643 }
1644
1645
1646 /**
1647 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1648 * Put face info into the sampler faces[] array.
1649 */
1650 static void
1651 sample_cube(struct tgsi_sampler *tgsi_sampler,
1652 const float s[QUAD_SIZE],
1653 const float t[QUAD_SIZE],
1654 const float p[QUAD_SIZE],
1655 const float c0[QUAD_SIZE],
1656 enum tgsi_sampler_control control,
1657 float rgba[NUM_CHANNELS][QUAD_SIZE])
1658 {
1659 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1660 unsigned j;
1661 float ssss[4], tttt[4];
1662
1663 /*
1664 major axis
1665 direction target sc tc ma
1666 ---------- ------------------------------- --- --- ---
1667 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1668 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1669 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1670 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1671 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1672 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1673 */
1674
1675 /* Choose the cube face and compute new s/t coords for the 2D face.
1676 *
1677 * Use the same cube face for all four pixels in the quad.
1678 *
1679 * This isn't ideal, but if we want to use a different cube face
1680 * per pixel in the quad, we'd have to also compute the per-face
1681 * LOD here too. That's because the four post-face-selection
1682 * texcoords are no longer related to each other (they're
1683 * per-face!) so we can't use subtraction to compute the partial
1684 * deriviates to compute the LOD. Doing so (near cube edges
1685 * anyway) gives us pretty much random values.
1686 */
1687 {
1688 /* use the average of the four pixel's texcoords to choose the face */
1689 const float rx = 0.25 * (s[0] + s[1] + s[2] + s[3]);
1690 const float ry = 0.25 * (t[0] + t[1] + t[2] + t[3]);
1691 const float rz = 0.25 * (p[0] + p[1] + p[2] + p[3]);
1692 const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
1693
1694 if (arx >= ary && arx >= arz) {
1695 float sign = (rx >= 0.0F) ? 1.0F : -1.0F;
1696 uint face = (rx >= 0.0F) ? PIPE_TEX_FACE_POS_X : PIPE_TEX_FACE_NEG_X;
1697 for (j = 0; j < QUAD_SIZE; j++) {
1698 const float ima = -0.5F / fabsf(s[j]);
1699 ssss[j] = sign * p[j] * ima + 0.5F;
1700 tttt[j] = t[j] * ima + 0.5F;
1701 samp->faces[j] = face;
1702 }
1703 }
1704 else if (ary >= arx && ary >= arz) {
1705 float sign = (ry >= 0.0F) ? 1.0F : -1.0F;
1706 uint face = (ry >= 0.0F) ? PIPE_TEX_FACE_POS_Y : PIPE_TEX_FACE_NEG_Y;
1707 for (j = 0; j < QUAD_SIZE; j++) {
1708 const float ima = -0.5F / fabsf(t[j]);
1709 ssss[j] = -s[j] * ima + 0.5F;
1710 tttt[j] = sign * -p[j] * ima + 0.5F;
1711 samp->faces[j] = face;
1712 }
1713 }
1714 else {
1715 float sign = (rz >= 0.0F) ? 1.0F : -1.0F;
1716 uint face = (rz >= 0.0F) ? PIPE_TEX_FACE_POS_Z : PIPE_TEX_FACE_NEG_Z;
1717 for (j = 0; j < QUAD_SIZE; j++) {
1718 const float ima = -0.5 / fabsf(p[j]);
1719 ssss[j] = sign * -s[j] * ima + 0.5F;
1720 tttt[j] = t[j] * ima + 0.5F;
1721 samp->faces[j] = face;
1722 }
1723 }
1724 }
1725
1726 /* In our little pipeline, the compare stage is next. If compare
1727 * is not active, this will point somewhere deeper into the
1728 * pipeline, eg. to mip_filter or even img_filter.
1729 */
1730 samp->compare(tgsi_sampler, ssss, tttt, NULL, c0, control, rgba);
1731 }
1732
1733
1734
1735 static wrap_nearest_func
1736 get_nearest_unorm_wrap(unsigned mode)
1737 {
1738 switch (mode) {
1739 case PIPE_TEX_WRAP_CLAMP:
1740 return wrap_nearest_unorm_clamp;
1741 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1742 return wrap_nearest_unorm_clamp_to_edge;
1743 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1744 return wrap_nearest_unorm_clamp_to_border;
1745 default:
1746 assert(0);
1747 return wrap_nearest_unorm_clamp;
1748 }
1749 }
1750
1751
1752 static wrap_nearest_func
1753 get_nearest_wrap(unsigned mode)
1754 {
1755 switch (mode) {
1756 case PIPE_TEX_WRAP_REPEAT:
1757 return wrap_nearest_repeat;
1758 case PIPE_TEX_WRAP_CLAMP:
1759 return wrap_nearest_clamp;
1760 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1761 return wrap_nearest_clamp_to_edge;
1762 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1763 return wrap_nearest_clamp_to_border;
1764 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1765 return wrap_nearest_mirror_repeat;
1766 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1767 return wrap_nearest_mirror_clamp;
1768 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1769 return wrap_nearest_mirror_clamp_to_edge;
1770 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1771 return wrap_nearest_mirror_clamp_to_border;
1772 default:
1773 assert(0);
1774 return wrap_nearest_repeat;
1775 }
1776 }
1777
1778
1779 static wrap_linear_func
1780 get_linear_unorm_wrap(unsigned mode)
1781 {
1782 switch (mode) {
1783 case PIPE_TEX_WRAP_CLAMP:
1784 return wrap_linear_unorm_clamp;
1785 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1786 return wrap_linear_unorm_clamp_to_edge;
1787 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1788 return wrap_linear_unorm_clamp_to_border;
1789 default:
1790 assert(0);
1791 return wrap_linear_unorm_clamp;
1792 }
1793 }
1794
1795
1796 static wrap_linear_func
1797 get_linear_wrap(unsigned mode)
1798 {
1799 switch (mode) {
1800 case PIPE_TEX_WRAP_REPEAT:
1801 return wrap_linear_repeat;
1802 case PIPE_TEX_WRAP_CLAMP:
1803 return wrap_linear_clamp;
1804 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1805 return wrap_linear_clamp_to_edge;
1806 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1807 return wrap_linear_clamp_to_border;
1808 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1809 return wrap_linear_mirror_repeat;
1810 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1811 return wrap_linear_mirror_clamp;
1812 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1813 return wrap_linear_mirror_clamp_to_edge;
1814 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1815 return wrap_linear_mirror_clamp_to_border;
1816 default:
1817 assert(0);
1818 return wrap_linear_repeat;
1819 }
1820 }
1821
1822
1823 static compute_lambda_func
1824 get_lambda_func(const union sp_sampler_key key)
1825 {
1826 if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
1827 return compute_lambda_vert;
1828
1829 switch (key.bits.target) {
1830 case PIPE_TEXTURE_1D:
1831 return compute_lambda_1d;
1832 case PIPE_TEXTURE_2D:
1833 case PIPE_TEXTURE_RECT:
1834 case PIPE_TEXTURE_CUBE:
1835 return compute_lambda_2d;
1836 case PIPE_TEXTURE_3D:
1837 return compute_lambda_3d;
1838 default:
1839 assert(0);
1840 return compute_lambda_1d;
1841 }
1842 }
1843
1844
1845 static filter_func
1846 get_img_filter(const union sp_sampler_key key,
1847 unsigned filter,
1848 const struct pipe_sampler_state *sampler)
1849 {
1850 switch (key.bits.target) {
1851 case PIPE_TEXTURE_1D:
1852 if (filter == PIPE_TEX_FILTER_NEAREST)
1853 return img_filter_1d_nearest;
1854 else
1855 return img_filter_1d_linear;
1856 break;
1857 case PIPE_TEXTURE_2D:
1858 case PIPE_TEXTURE_RECT:
1859 /* Try for fast path:
1860 */
1861 if (key.bits.is_pot &&
1862 sampler->wrap_s == sampler->wrap_t &&
1863 sampler->normalized_coords)
1864 {
1865 switch (sampler->wrap_s) {
1866 case PIPE_TEX_WRAP_REPEAT:
1867 switch (filter) {
1868 case PIPE_TEX_FILTER_NEAREST:
1869 return img_filter_2d_nearest_repeat_POT;
1870 case PIPE_TEX_FILTER_LINEAR:
1871 return img_filter_2d_linear_repeat_POT;
1872 default:
1873 break;
1874 }
1875 break;
1876 case PIPE_TEX_WRAP_CLAMP:
1877 switch (filter) {
1878 case PIPE_TEX_FILTER_NEAREST:
1879 return img_filter_2d_nearest_clamp_POT;
1880 default:
1881 break;
1882 }
1883 }
1884 }
1885 /* Otherwise use default versions:
1886 */
1887 if (filter == PIPE_TEX_FILTER_NEAREST)
1888 return img_filter_2d_nearest;
1889 else
1890 return img_filter_2d_linear;
1891 break;
1892 case PIPE_TEXTURE_CUBE:
1893 if (filter == PIPE_TEX_FILTER_NEAREST)
1894 return img_filter_cube_nearest;
1895 else
1896 return img_filter_cube_linear;
1897 break;
1898 case PIPE_TEXTURE_3D:
1899 if (filter == PIPE_TEX_FILTER_NEAREST)
1900 return img_filter_3d_nearest;
1901 else
1902 return img_filter_3d_linear;
1903 break;
1904 default:
1905 assert(0);
1906 return img_filter_1d_nearest;
1907 }
1908 }
1909
1910
1911 /**
1912 * Bind the given texture object and texture cache to the sampler varient.
1913 */
1914 void
1915 sp_sampler_varient_bind_texture( struct sp_sampler_varient *samp,
1916 struct softpipe_tex_tile_cache *tex_cache,
1917 const struct pipe_resource *texture )
1918 {
1919 const struct pipe_sampler_state *sampler = samp->sampler;
1920
1921 samp->texture = texture;
1922 samp->cache = tex_cache;
1923 samp->xpot = util_unsigned_logbase2( texture->width0 );
1924 samp->ypot = util_unsigned_logbase2( texture->height0 );
1925 samp->level = CLAMP((int) sampler->min_lod, 0, (int) texture->last_level);
1926 }
1927
1928
1929 void
1930 sp_sampler_varient_destroy( struct sp_sampler_varient *samp )
1931 {
1932 FREE(samp);
1933 }
1934
1935
1936 /**
1937 * Create a sampler varient for a given set of non-orthogonal state.
1938 */
1939 struct sp_sampler_varient *
1940 sp_create_sampler_varient( const struct pipe_sampler_state *sampler,
1941 const union sp_sampler_key key )
1942 {
1943 struct sp_sampler_varient *samp = CALLOC_STRUCT(sp_sampler_varient);
1944 if (!samp)
1945 return NULL;
1946
1947 samp->sampler = sampler;
1948 samp->key = key;
1949
1950 /* Note that (for instance) linear_texcoord_s and
1951 * nearest_texcoord_s may be active at the same time, if the
1952 * sampler min_img_filter differs from its mag_img_filter.
1953 */
1954 if (sampler->normalized_coords) {
1955 samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
1956 samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
1957 samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
1958
1959 samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
1960 samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
1961 samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
1962 }
1963 else {
1964 samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
1965 samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
1966 samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
1967
1968 samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
1969 samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
1970 samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
1971 }
1972
1973 samp->compute_lambda = get_lambda_func( key );
1974
1975 samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
1976 samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
1977
1978 switch (sampler->min_mip_filter) {
1979 case PIPE_TEX_MIPFILTER_NONE:
1980 if (sampler->min_img_filter == sampler->mag_img_filter)
1981 samp->mip_filter = samp->min_img_filter;
1982 else
1983 samp->mip_filter = mip_filter_none;
1984 break;
1985
1986 case PIPE_TEX_MIPFILTER_NEAREST:
1987 samp->mip_filter = mip_filter_nearest;
1988 break;
1989
1990 case PIPE_TEX_MIPFILTER_LINEAR:
1991 if (key.bits.is_pot &&
1992 sampler->min_img_filter == sampler->mag_img_filter &&
1993 sampler->normalized_coords &&
1994 sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
1995 sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
1996 sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR)
1997 {
1998 samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
1999 }
2000 else
2001 {
2002 samp->mip_filter = mip_filter_linear;
2003 }
2004 break;
2005 }
2006
2007 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
2008 samp->compare = sample_compare;
2009 }
2010 else {
2011 /* Skip compare operation by promoting the mip_filter function
2012 * pointer:
2013 */
2014 samp->compare = samp->mip_filter;
2015 }
2016
2017 if (key.bits.target == PIPE_TEXTURE_CUBE) {
2018 samp->base.get_samples = sample_cube;
2019 }
2020 else {
2021 samp->faces[0] = 0;
2022 samp->faces[1] = 0;
2023 samp->faces[2] = 0;
2024 samp->faces[3] = 0;
2025
2026 /* Skip cube face determination by promoting the compare
2027 * function pointer:
2028 */
2029 samp->base.get_samples = samp->compare;
2030 }
2031
2032 return samp;
2033 }