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