softpipe: Bind samplers to views instead of the underlying resource.
[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_variant *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->view->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_variant *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->view->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_variant *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->view->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_variant *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_variant *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_variant *samp,
655 union tex_tile_address addr, int x, int y)
656 {
657 const struct pipe_resource *texture = samp->view->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 samp->sampler->border_color;
663 }
664 else {
665 return get_texel_2d_no_border( samp, addr, x, y );
666 }
667 }
668
669
670 /* Gather a quad of adjacent texels within a tile:
671 */
672 static INLINE void
673 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant *samp,
674 union tex_tile_address addr,
675 unsigned x, unsigned y,
676 const float *out[4])
677 {
678 const struct softpipe_tex_cached_tile *tile;
679
680 addr.bits.x = x / TILE_SIZE;
681 addr.bits.y = y / TILE_SIZE;
682 y %= TILE_SIZE;
683 x %= TILE_SIZE;
684
685 tile = sp_get_cached_tile_tex(samp->cache, addr);
686
687 out[0] = &tile->data.color[y ][x ][0];
688 out[1] = &tile->data.color[y ][x+1][0];
689 out[2] = &tile->data.color[y+1][x ][0];
690 out[3] = &tile->data.color[y+1][x+1][0];
691 }
692
693
694 /* Gather a quad of potentially non-adjacent texels:
695 */
696 static INLINE void
697 get_texel_quad_2d_no_border(const struct sp_sampler_variant *samp,
698 union tex_tile_address addr,
699 int x0, int y0,
700 int x1, int y1,
701 const float *out[4])
702 {
703 out[0] = get_texel_2d_no_border( samp, addr, x0, y0 );
704 out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
705 out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
706 out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
707 }
708
709 /* Can involve a lot of unnecessary checks for border color:
710 */
711 static INLINE void
712 get_texel_quad_2d(const struct sp_sampler_variant *samp,
713 union tex_tile_address addr,
714 int x0, int y0,
715 int x1, int y1,
716 const float *out[4])
717 {
718 out[0] = get_texel_2d( samp, addr, x0, y0 );
719 out[1] = get_texel_2d( samp, addr, x1, y0 );
720 out[3] = get_texel_2d( samp, addr, x1, y1 );
721 out[2] = get_texel_2d( samp, addr, x0, y1 );
722 }
723
724
725
726 /* 3d variants:
727 */
728 static INLINE const float *
729 get_texel_3d_no_border(const struct sp_sampler_variant *samp,
730 union tex_tile_address addr, int x, int y, int z)
731 {
732 const struct softpipe_tex_cached_tile *tile;
733
734 addr.bits.x = x / TILE_SIZE;
735 addr.bits.y = y / TILE_SIZE;
736 addr.bits.z = z;
737 y %= TILE_SIZE;
738 x %= TILE_SIZE;
739
740 tile = sp_get_cached_tile_tex(samp->cache, addr);
741
742 return &tile->data.color[y][x][0];
743 }
744
745
746 static INLINE const float *
747 get_texel_3d(const struct sp_sampler_variant *samp,
748 union tex_tile_address addr, int x, int y, int z)
749 {
750 const struct pipe_resource *texture = samp->view->texture;
751 unsigned level = addr.bits.level;
752
753 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
754 y < 0 || y >= (int) u_minify(texture->height0, level) ||
755 z < 0 || z >= (int) u_minify(texture->depth0, level)) {
756 return samp->sampler->border_color;
757 }
758 else {
759 return get_texel_3d_no_border( samp, addr, x, y, z );
760 }
761 }
762
763
764 /**
765 * Given the logbase2 of a mipmap's base level size and a mipmap level,
766 * return the size (in texels) of that mipmap level.
767 * For example, if level[0].width = 256 then base_pot will be 8.
768 * If level = 2, then we'll return 64 (the width at level=2).
769 * Return 1 if level > base_pot.
770 */
771 static INLINE unsigned
772 pot_level_size(unsigned base_pot, unsigned level)
773 {
774 return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
775 }
776
777
778 static void
779 print_sample(const char *function, float rgba[NUM_CHANNELS][QUAD_SIZE])
780 {
781 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
782 function,
783 rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
784 rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
785 rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
786 rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
787 }
788
789
790 /* Some image-filter fastpaths:
791 */
792 static INLINE void
793 img_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
794 const float s[QUAD_SIZE],
795 const float t[QUAD_SIZE],
796 const float p[QUAD_SIZE],
797 const float c0[QUAD_SIZE],
798 enum tgsi_sampler_control control,
799 float rgba[NUM_CHANNELS][QUAD_SIZE])
800 {
801 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
802 unsigned j;
803 unsigned level = samp->level;
804 unsigned xpot = pot_level_size(samp->xpot, level);
805 unsigned ypot = pot_level_size(samp->ypot, level);
806 unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
807 unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
808 union tex_tile_address addr;
809
810 addr.value = 0;
811 addr.bits.level = samp->level;
812
813 for (j = 0; j < QUAD_SIZE; j++) {
814 int c;
815
816 float u = s[j] * xpot - 0.5F;
817 float v = t[j] * ypot - 0.5F;
818
819 int uflr = util_ifloor(u);
820 int vflr = util_ifloor(v);
821
822 float xw = u - (float)uflr;
823 float yw = v - (float)vflr;
824
825 int x0 = uflr & (xpot - 1);
826 int y0 = vflr & (ypot - 1);
827
828 const float *tx[4];
829
830 /* Can we fetch all four at once:
831 */
832 if (x0 < xmax && y0 < ymax) {
833 get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
834 }
835 else {
836 unsigned x1 = (x0 + 1) & (xpot - 1);
837 unsigned y1 = (y0 + 1) & (ypot - 1);
838 get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
839 }
840
841 /* interpolate R, G, B, A */
842 for (c = 0; c < 4; c++) {
843 rgba[c][j] = lerp_2d(xw, yw,
844 tx[0][c], tx[1][c],
845 tx[2][c], tx[3][c]);
846 }
847 }
848
849 if (DEBUG_TEX) {
850 print_sample(__FUNCTION__, rgba);
851 }
852 }
853
854
855 static INLINE void
856 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
857 const float s[QUAD_SIZE],
858 const float t[QUAD_SIZE],
859 const float p[QUAD_SIZE],
860 const float c0[QUAD_SIZE],
861 enum tgsi_sampler_control control,
862 float rgba[NUM_CHANNELS][QUAD_SIZE])
863 {
864 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
865 unsigned j;
866 unsigned level = samp->level;
867 unsigned xpot = pot_level_size(samp->xpot, level);
868 unsigned ypot = pot_level_size(samp->ypot, level);
869 union tex_tile_address addr;
870
871 addr.value = 0;
872 addr.bits.level = samp->level;
873
874 for (j = 0; j < QUAD_SIZE; j++) {
875 int c;
876
877 float u = s[j] * xpot;
878 float v = t[j] * ypot;
879
880 int uflr = util_ifloor(u);
881 int vflr = util_ifloor(v);
882
883 int x0 = uflr & (xpot - 1);
884 int y0 = vflr & (ypot - 1);
885
886 const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
887
888 for (c = 0; c < 4; c++) {
889 rgba[c][j] = out[c];
890 }
891 }
892
893 if (DEBUG_TEX) {
894 print_sample(__FUNCTION__, rgba);
895 }
896 }
897
898
899 static INLINE void
900 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
901 const float s[QUAD_SIZE],
902 const float t[QUAD_SIZE],
903 const float p[QUAD_SIZE],
904 const float c0[QUAD_SIZE],
905 enum tgsi_sampler_control control,
906 float rgba[NUM_CHANNELS][QUAD_SIZE])
907 {
908 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
909 unsigned j;
910 unsigned level = samp->level;
911 unsigned xpot = pot_level_size(samp->xpot, level);
912 unsigned ypot = pot_level_size(samp->ypot, level);
913 union tex_tile_address addr;
914
915 addr.value = 0;
916 addr.bits.level = samp->level;
917
918 for (j = 0; j < QUAD_SIZE; j++) {
919 int c;
920
921 float u = s[j] * xpot;
922 float v = t[j] * ypot;
923
924 int x0, y0;
925 const float *out;
926
927 x0 = util_ifloor(u);
928 if (x0 < 0)
929 x0 = 0;
930 else if (x0 > xpot - 1)
931 x0 = xpot - 1;
932
933 y0 = util_ifloor(v);
934 if (y0 < 0)
935 y0 = 0;
936 else if (y0 > ypot - 1)
937 y0 = ypot - 1;
938
939 out = get_texel_2d_no_border(samp, addr, x0, y0);
940
941 for (c = 0; c < 4; c++) {
942 rgba[c][j] = out[c];
943 }
944 }
945
946 if (DEBUG_TEX) {
947 print_sample(__FUNCTION__, rgba);
948 }
949 }
950
951
952 static void
953 img_filter_1d_nearest(struct tgsi_sampler *tgsi_sampler,
954 const float s[QUAD_SIZE],
955 const float t[QUAD_SIZE],
956 const float p[QUAD_SIZE],
957 const float c0[QUAD_SIZE],
958 enum tgsi_sampler_control control,
959 float rgba[NUM_CHANNELS][QUAD_SIZE])
960 {
961 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
962 const struct pipe_resource *texture = samp->view->texture;
963 unsigned level0, j;
964 int width;
965 int x[4];
966 union tex_tile_address addr;
967
968 level0 = samp->level;
969 width = u_minify(texture->width0, level0);
970
971 assert(width > 0);
972
973 addr.value = 0;
974 addr.bits.level = samp->level;
975
976 samp->nearest_texcoord_s(s, width, x);
977
978 for (j = 0; j < QUAD_SIZE; j++) {
979 const float *out = get_texel_2d(samp, addr, x[j], 0);
980 int c;
981 for (c = 0; c < 4; c++) {
982 rgba[c][j] = out[c];
983 }
984 }
985
986 if (DEBUG_TEX) {
987 print_sample(__FUNCTION__, rgba);
988 }
989 }
990
991
992 static void
993 img_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
994 const float s[QUAD_SIZE],
995 const float t[QUAD_SIZE],
996 const float p[QUAD_SIZE],
997 const float c0[QUAD_SIZE],
998 enum tgsi_sampler_control control,
999 float rgba[NUM_CHANNELS][QUAD_SIZE])
1000 {
1001 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1002 const struct pipe_resource *texture = samp->view->texture;
1003 unsigned level0, j;
1004 int width, height;
1005 int x[4], y[4];
1006 union tex_tile_address addr;
1007
1008
1009 level0 = samp->level;
1010 width = u_minify(texture->width0, level0);
1011 height = u_minify(texture->height0, level0);
1012
1013 assert(width > 0);
1014 assert(height > 0);
1015
1016 addr.value = 0;
1017 addr.bits.level = samp->level;
1018
1019 samp->nearest_texcoord_s(s, width, x);
1020 samp->nearest_texcoord_t(t, height, y);
1021
1022 for (j = 0; j < QUAD_SIZE; j++) {
1023 const float *out = get_texel_2d(samp, addr, x[j], y[j]);
1024 int c;
1025 for (c = 0; c < 4; c++) {
1026 rgba[c][j] = out[c];
1027 }
1028 }
1029
1030 if (DEBUG_TEX) {
1031 print_sample(__FUNCTION__, rgba);
1032 }
1033 }
1034
1035
1036 static INLINE union tex_tile_address
1037 face(union tex_tile_address addr, unsigned face )
1038 {
1039 addr.bits.face = face;
1040 return addr;
1041 }
1042
1043
1044 static void
1045 img_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
1046 const float s[QUAD_SIZE],
1047 const float t[QUAD_SIZE],
1048 const float p[QUAD_SIZE],
1049 const float c0[QUAD_SIZE],
1050 enum tgsi_sampler_control control,
1051 float rgba[NUM_CHANNELS][QUAD_SIZE])
1052 {
1053 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1054 const struct pipe_resource *texture = samp->view->texture;
1055 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1056 unsigned level0, j;
1057 int width, height;
1058 int x[4], y[4];
1059 union tex_tile_address addr;
1060
1061 level0 = samp->level;
1062 width = u_minify(texture->width0, level0);
1063 height = u_minify(texture->height0, level0);
1064
1065 assert(width > 0);
1066 assert(height > 0);
1067
1068 addr.value = 0;
1069 addr.bits.level = samp->level;
1070
1071 samp->nearest_texcoord_s(s, width, x);
1072 samp->nearest_texcoord_t(t, height, y);
1073
1074 for (j = 0; j < QUAD_SIZE; j++) {
1075 const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
1076 int c;
1077 for (c = 0; c < 4; c++) {
1078 rgba[c][j] = out[c];
1079 }
1080 }
1081
1082 if (DEBUG_TEX) {
1083 print_sample(__FUNCTION__, rgba);
1084 }
1085 }
1086
1087
1088 static void
1089 img_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
1090 const float s[QUAD_SIZE],
1091 const float t[QUAD_SIZE],
1092 const float p[QUAD_SIZE],
1093 const float c0[QUAD_SIZE],
1094 enum tgsi_sampler_control control,
1095 float rgba[NUM_CHANNELS][QUAD_SIZE])
1096 {
1097 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1098 const struct pipe_resource *texture = samp->view->texture;
1099 unsigned level0, j;
1100 int width, height, depth;
1101 int x[4], y[4], z[4];
1102 union tex_tile_address addr;
1103
1104 level0 = samp->level;
1105 width = u_minify(texture->width0, level0);
1106 height = u_minify(texture->height0, level0);
1107 depth = u_minify(texture->depth0, level0);
1108
1109 assert(width > 0);
1110 assert(height > 0);
1111 assert(depth > 0);
1112
1113 samp->nearest_texcoord_s(s, width, x);
1114 samp->nearest_texcoord_t(t, height, y);
1115 samp->nearest_texcoord_p(p, depth, z);
1116
1117 addr.value = 0;
1118 addr.bits.level = samp->level;
1119
1120 for (j = 0; j < QUAD_SIZE; j++) {
1121 const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
1122 int c;
1123 for (c = 0; c < 4; c++) {
1124 rgba[c][j] = out[c];
1125 }
1126 }
1127 }
1128
1129
1130 static void
1131 img_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
1132 const float s[QUAD_SIZE],
1133 const float t[QUAD_SIZE],
1134 const float p[QUAD_SIZE],
1135 const float c0[QUAD_SIZE],
1136 enum tgsi_sampler_control control,
1137 float rgba[NUM_CHANNELS][QUAD_SIZE])
1138 {
1139 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1140 const struct pipe_resource *texture = samp->view->texture;
1141 unsigned level0, j;
1142 int width;
1143 int x0[4], x1[4];
1144 float xw[4]; /* weights */
1145 union tex_tile_address addr;
1146
1147 level0 = samp->level;
1148 width = u_minify(texture->width0, level0);
1149
1150 assert(width > 0);
1151
1152 addr.value = 0;
1153 addr.bits.level = samp->level;
1154
1155 samp->linear_texcoord_s(s, width, x0, x1, xw);
1156
1157 for (j = 0; j < QUAD_SIZE; j++) {
1158 const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
1159 const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
1160 int c;
1161
1162 /* interpolate R, G, B, A */
1163 for (c = 0; c < 4; c++) {
1164 rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1165 }
1166 }
1167 }
1168
1169
1170 static void
1171 img_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
1172 const float s[QUAD_SIZE],
1173 const float t[QUAD_SIZE],
1174 const float p[QUAD_SIZE],
1175 const float c0[QUAD_SIZE],
1176 enum tgsi_sampler_control control,
1177 float rgba[NUM_CHANNELS][QUAD_SIZE])
1178 {
1179 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1180 const struct pipe_resource *texture = samp->view->texture;
1181 unsigned level0, j;
1182 int width, height;
1183 int x0[4], y0[4], x1[4], y1[4];
1184 float xw[4], yw[4]; /* weights */
1185 union tex_tile_address addr;
1186
1187 level0 = samp->level;
1188 width = u_minify(texture->width0, level0);
1189 height = u_minify(texture->height0, level0);
1190
1191 assert(width > 0);
1192 assert(height > 0);
1193
1194 addr.value = 0;
1195 addr.bits.level = samp->level;
1196
1197 samp->linear_texcoord_s(s, width, x0, x1, xw);
1198 samp->linear_texcoord_t(t, height, y0, y1, yw);
1199
1200 for (j = 0; j < QUAD_SIZE; j++) {
1201 const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
1202 const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
1203 const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
1204 const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
1205 int c;
1206
1207 /* interpolate R, G, B, A */
1208 for (c = 0; c < 4; c++) {
1209 rgba[c][j] = lerp_2d(xw[j], yw[j],
1210 tx0[c], tx1[c],
1211 tx2[c], tx3[c]);
1212 }
1213 }
1214 }
1215
1216
1217 static void
1218 img_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1219 const float s[QUAD_SIZE],
1220 const float t[QUAD_SIZE],
1221 const float p[QUAD_SIZE],
1222 const float c0[QUAD_SIZE],
1223 enum tgsi_sampler_control control,
1224 float rgba[NUM_CHANNELS][QUAD_SIZE])
1225 {
1226 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1227 const struct pipe_resource *texture = samp->view->texture;
1228 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1229 unsigned level0, j;
1230 int width, height;
1231 int x0[4], y0[4], x1[4], y1[4];
1232 float xw[4], yw[4]; /* weights */
1233 union tex_tile_address addr;
1234
1235 level0 = samp->level;
1236 width = u_minify(texture->width0, level0);
1237 height = u_minify(texture->height0, level0);
1238
1239 assert(width > 0);
1240 assert(height > 0);
1241
1242 addr.value = 0;
1243 addr.bits.level = samp->level;
1244
1245 samp->linear_texcoord_s(s, width, x0, x1, xw);
1246 samp->linear_texcoord_t(t, height, y0, y1, yw);
1247
1248 for (j = 0; j < QUAD_SIZE; j++) {
1249 union tex_tile_address addrj = face(addr, faces[j]);
1250 const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
1251 const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
1252 const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
1253 const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
1254 int c;
1255
1256 /* interpolate R, G, B, A */
1257 for (c = 0; c < 4; c++) {
1258 rgba[c][j] = lerp_2d(xw[j], yw[j],
1259 tx0[c], tx1[c],
1260 tx2[c], tx3[c]);
1261 }
1262 }
1263 }
1264
1265
1266 static void
1267 img_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
1268 const float s[QUAD_SIZE],
1269 const float t[QUAD_SIZE],
1270 const float p[QUAD_SIZE],
1271 const float c0[QUAD_SIZE],
1272 enum tgsi_sampler_control control,
1273 float rgba[NUM_CHANNELS][QUAD_SIZE])
1274 {
1275 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1276 const struct pipe_resource *texture = samp->view->texture;
1277 unsigned level0, j;
1278 int width, height, depth;
1279 int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
1280 float xw[4], yw[4], zw[4]; /* interpolation weights */
1281 union tex_tile_address addr;
1282
1283 level0 = samp->level;
1284 width = u_minify(texture->width0, level0);
1285 height = u_minify(texture->height0, level0);
1286 depth = u_minify(texture->depth0, level0);
1287
1288 addr.value = 0;
1289 addr.bits.level = level0;
1290
1291 assert(width > 0);
1292 assert(height > 0);
1293 assert(depth > 0);
1294
1295 samp->linear_texcoord_s(s, width, x0, x1, xw);
1296 samp->linear_texcoord_t(t, height, y0, y1, yw);
1297 samp->linear_texcoord_p(p, depth, z0, z1, zw);
1298
1299 for (j = 0; j < QUAD_SIZE; j++) {
1300 int c;
1301
1302 const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
1303 const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
1304 const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
1305 const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
1306
1307 const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
1308 const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
1309 const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
1310 const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
1311
1312 /* interpolate R, G, B, A */
1313 for (c = 0; c < 4; c++) {
1314 rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
1315 tx00[c], tx01[c],
1316 tx02[c], tx03[c],
1317 tx10[c], tx11[c],
1318 tx12[c], tx13[c]);
1319 }
1320 }
1321 }
1322
1323
1324 /* Calculate level of detail for every fragment.
1325 * Note that lambda has already been biased by global LOD bias.
1326 */
1327 static INLINE void
1328 compute_lod(const struct pipe_sampler_state *sampler,
1329 const float biased_lambda,
1330 const float lodbias[QUAD_SIZE],
1331 float lod[QUAD_SIZE])
1332 {
1333 uint i;
1334
1335 for (i = 0; i < QUAD_SIZE; i++) {
1336 lod[i] = biased_lambda + lodbias[i];
1337 lod[i] = CLAMP(lod[i], sampler->min_lod, sampler->max_lod);
1338 }
1339 }
1340
1341
1342 static void
1343 mip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1344 const float s[QUAD_SIZE],
1345 const float t[QUAD_SIZE],
1346 const float p[QUAD_SIZE],
1347 const float c0[QUAD_SIZE],
1348 enum tgsi_sampler_control control,
1349 float rgba[NUM_CHANNELS][QUAD_SIZE])
1350 {
1351 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1352 const struct pipe_resource *texture = samp->view->texture;
1353 int level0;
1354 float lambda;
1355 float lod[QUAD_SIZE];
1356
1357 if (control == tgsi_sampler_lod_bias) {
1358 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1359 compute_lod(samp->sampler, lambda, c0, lod);
1360 } else {
1361 assert(control == tgsi_sampler_lod_explicit);
1362
1363 memcpy(lod, c0, sizeof(lod));
1364 }
1365
1366 /* XXX: Take into account all lod values.
1367 */
1368 lambda = lod[0];
1369 level0 = (int)lambda;
1370
1371 if (lambda < 0.0) {
1372 samp->level = 0;
1373 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1374 }
1375 else if (level0 >= texture->last_level) {
1376 samp->level = texture->last_level;
1377 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1378 }
1379 else {
1380 float levelBlend = lambda - level0;
1381 float rgba0[4][4];
1382 float rgba1[4][4];
1383 int c,j;
1384
1385 samp->level = level0;
1386 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1387
1388 samp->level = level0+1;
1389 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1390
1391 for (j = 0; j < QUAD_SIZE; j++) {
1392 for (c = 0; c < 4; c++) {
1393 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1394 }
1395 }
1396 }
1397
1398 if (DEBUG_TEX) {
1399 print_sample(__FUNCTION__, rgba);
1400 }
1401 }
1402
1403
1404 /**
1405 * Compute nearest mipmap level from texcoords.
1406 * Then sample the texture level for four elements of a quad.
1407 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1408 */
1409 static void
1410 mip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
1411 const float s[QUAD_SIZE],
1412 const float t[QUAD_SIZE],
1413 const float p[QUAD_SIZE],
1414 const float c0[QUAD_SIZE],
1415 enum tgsi_sampler_control control,
1416 float rgba[NUM_CHANNELS][QUAD_SIZE])
1417 {
1418 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1419 const struct pipe_resource *texture = samp->view->texture;
1420 float lambda;
1421 float lod[QUAD_SIZE];
1422
1423 if (control == tgsi_sampler_lod_bias) {
1424 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1425 compute_lod(samp->sampler, lambda, c0, lod);
1426 } else {
1427 assert(control == tgsi_sampler_lod_explicit);
1428
1429 memcpy(lod, c0, sizeof(lod));
1430 }
1431
1432 /* XXX: Take into account all lod values.
1433 */
1434 lambda = lod[0];
1435
1436 if (lambda < 0.0) {
1437 samp->level = 0;
1438 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1439 }
1440 else {
1441 samp->level = (int)(lambda + 0.5) ;
1442 samp->level = MIN2(samp->level, (int)texture->last_level);
1443 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1444 }
1445
1446 if (DEBUG_TEX) {
1447 print_sample(__FUNCTION__, rgba);
1448 }
1449 }
1450
1451
1452 static void
1453 mip_filter_none(struct tgsi_sampler *tgsi_sampler,
1454 const float s[QUAD_SIZE],
1455 const float t[QUAD_SIZE],
1456 const float p[QUAD_SIZE],
1457 const float c0[QUAD_SIZE],
1458 enum tgsi_sampler_control control,
1459 float rgba[NUM_CHANNELS][QUAD_SIZE])
1460 {
1461 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1462 float lambda;
1463 float lod[QUAD_SIZE];
1464
1465 if (control == tgsi_sampler_lod_bias) {
1466 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1467 compute_lod(samp->sampler, lambda, c0, lod);
1468 } else {
1469 assert(control == tgsi_sampler_lod_explicit);
1470
1471 memcpy(lod, c0, sizeof(lod));
1472 }
1473
1474 /* XXX: Take into account all lod values.
1475 */
1476 lambda = lod[0];
1477
1478 if (lambda < 0.0) {
1479 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1480 }
1481 else {
1482 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1483 }
1484 }
1485
1486
1487
1488 /**
1489 * Specialized version of mip_filter_linear with hard-wired calls to
1490 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1491 */
1492 static void
1493 mip_filter_linear_2d_linear_repeat_POT(
1494 struct tgsi_sampler *tgsi_sampler,
1495 const float s[QUAD_SIZE],
1496 const float t[QUAD_SIZE],
1497 const float p[QUAD_SIZE],
1498 const float c0[QUAD_SIZE],
1499 enum tgsi_sampler_control control,
1500 float rgba[NUM_CHANNELS][QUAD_SIZE])
1501 {
1502 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1503 const struct pipe_resource *texture = samp->view->texture;
1504 int level0;
1505 float lambda;
1506 float lod[QUAD_SIZE];
1507
1508 if (control == tgsi_sampler_lod_bias) {
1509 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1510 compute_lod(samp->sampler, lambda, c0, lod);
1511 } else {
1512 assert(control == tgsi_sampler_lod_explicit);
1513
1514 memcpy(lod, c0, sizeof(lod));
1515 }
1516
1517 /* XXX: Take into account all lod values.
1518 */
1519 lambda = lod[0];
1520 level0 = (int)lambda;
1521
1522 /* Catches both negative and large values of level0:
1523 */
1524 if ((unsigned)level0 >= texture->last_level) {
1525 if (level0 < 0)
1526 samp->level = 0;
1527 else
1528 samp->level = texture->last_level;
1529
1530 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1531 }
1532 else {
1533 float levelBlend = lambda - level0;
1534 float rgba0[4][4];
1535 float rgba1[4][4];
1536 int c,j;
1537
1538 samp->level = level0;
1539 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1540
1541 samp->level = level0+1;
1542 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1543
1544 for (j = 0; j < QUAD_SIZE; j++) {
1545 for (c = 0; c < 4; c++) {
1546 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1547 }
1548 }
1549 }
1550
1551 if (DEBUG_TEX) {
1552 print_sample(__FUNCTION__, rgba);
1553 }
1554 }
1555
1556
1557
1558 /**
1559 * Do shadow/depth comparisons.
1560 */
1561 static void
1562 sample_compare(struct tgsi_sampler *tgsi_sampler,
1563 const float s[QUAD_SIZE],
1564 const float t[QUAD_SIZE],
1565 const float p[QUAD_SIZE],
1566 const float c0[QUAD_SIZE],
1567 enum tgsi_sampler_control control,
1568 float rgba[NUM_CHANNELS][QUAD_SIZE])
1569 {
1570 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1571 const struct pipe_sampler_state *sampler = samp->sampler;
1572 int j, k0, k1, k2, k3;
1573 float val;
1574
1575 samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
1576
1577 /**
1578 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1579 * When we sampled the depth texture, the depth value was put into all
1580 * RGBA channels. We look at the red channel here.
1581 */
1582
1583 /* compare four texcoords vs. four texture samples */
1584 switch (sampler->compare_func) {
1585 case PIPE_FUNC_LESS:
1586 k0 = p[0] < rgba[0][0];
1587 k1 = p[1] < rgba[0][1];
1588 k2 = p[2] < rgba[0][2];
1589 k3 = p[3] < rgba[0][3];
1590 break;
1591 case PIPE_FUNC_LEQUAL:
1592 k0 = p[0] <= rgba[0][0];
1593 k1 = p[1] <= rgba[0][1];
1594 k2 = p[2] <= rgba[0][2];
1595 k3 = p[3] <= rgba[0][3];
1596 break;
1597 case PIPE_FUNC_GREATER:
1598 k0 = p[0] > rgba[0][0];
1599 k1 = p[1] > rgba[0][1];
1600 k2 = p[2] > rgba[0][2];
1601 k3 = p[3] > rgba[0][3];
1602 break;
1603 case PIPE_FUNC_GEQUAL:
1604 k0 = p[0] >= rgba[0][0];
1605 k1 = p[1] >= rgba[0][1];
1606 k2 = p[2] >= rgba[0][2];
1607 k3 = p[3] >= rgba[0][3];
1608 break;
1609 case PIPE_FUNC_EQUAL:
1610 k0 = p[0] == rgba[0][0];
1611 k1 = p[1] == rgba[0][1];
1612 k2 = p[2] == rgba[0][2];
1613 k3 = p[3] == rgba[0][3];
1614 break;
1615 case PIPE_FUNC_NOTEQUAL:
1616 k0 = p[0] != rgba[0][0];
1617 k1 = p[1] != rgba[0][1];
1618 k2 = p[2] != rgba[0][2];
1619 k3 = p[3] != rgba[0][3];
1620 break;
1621 case PIPE_FUNC_ALWAYS:
1622 k0 = k1 = k2 = k3 = 1;
1623 break;
1624 case PIPE_FUNC_NEVER:
1625 k0 = k1 = k2 = k3 = 0;
1626 break;
1627 default:
1628 k0 = k1 = k2 = k3 = 0;
1629 assert(0);
1630 break;
1631 }
1632
1633 /* convert four pass/fail values to an intensity in [0,1] */
1634 val = 0.25F * (k0 + k1 + k2 + k3);
1635
1636 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1637 for (j = 0; j < 4; j++) {
1638 rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
1639 rgba[3][j] = 1.0F;
1640 }
1641 }
1642
1643
1644 /**
1645 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1646 * Put face info into the sampler faces[] array.
1647 */
1648 static void
1649 sample_cube(struct tgsi_sampler *tgsi_sampler,
1650 const float s[QUAD_SIZE],
1651 const float t[QUAD_SIZE],
1652 const float p[QUAD_SIZE],
1653 const float c0[QUAD_SIZE],
1654 enum tgsi_sampler_control control,
1655 float rgba[NUM_CHANNELS][QUAD_SIZE])
1656 {
1657 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1658 unsigned j;
1659 float ssss[4], tttt[4];
1660
1661 /*
1662 major axis
1663 direction target sc tc ma
1664 ---------- ------------------------------- --- --- ---
1665 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1666 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1667 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1668 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1669 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1670 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1671 */
1672
1673 /* Choose the cube face and compute new s/t coords for the 2D face.
1674 *
1675 * Use the same cube face for all four pixels in the quad.
1676 *
1677 * This isn't ideal, but if we want to use a different cube face
1678 * per pixel in the quad, we'd have to also compute the per-face
1679 * LOD here too. That's because the four post-face-selection
1680 * texcoords are no longer related to each other (they're
1681 * per-face!) so we can't use subtraction to compute the partial
1682 * deriviates to compute the LOD. Doing so (near cube edges
1683 * anyway) gives us pretty much random values.
1684 */
1685 {
1686 /* use the average of the four pixel's texcoords to choose the face */
1687 const float rx = 0.25 * (s[0] + s[1] + s[2] + s[3]);
1688 const float ry = 0.25 * (t[0] + t[1] + t[2] + t[3]);
1689 const float rz = 0.25 * (p[0] + p[1] + p[2] + p[3]);
1690 const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
1691
1692 if (arx >= ary && arx >= arz) {
1693 float sign = (rx >= 0.0F) ? 1.0F : -1.0F;
1694 uint face = (rx >= 0.0F) ? PIPE_TEX_FACE_POS_X : PIPE_TEX_FACE_NEG_X;
1695 for (j = 0; j < QUAD_SIZE; j++) {
1696 const float ima = -0.5F / fabsf(s[j]);
1697 ssss[j] = sign * p[j] * ima + 0.5F;
1698 tttt[j] = t[j] * ima + 0.5F;
1699 samp->faces[j] = face;
1700 }
1701 }
1702 else if (ary >= arx && ary >= arz) {
1703 float sign = (ry >= 0.0F) ? 1.0F : -1.0F;
1704 uint face = (ry >= 0.0F) ? PIPE_TEX_FACE_POS_Y : PIPE_TEX_FACE_NEG_Y;
1705 for (j = 0; j < QUAD_SIZE; j++) {
1706 const float ima = -0.5F / fabsf(t[j]);
1707 ssss[j] = -s[j] * ima + 0.5F;
1708 tttt[j] = sign * -p[j] * ima + 0.5F;
1709 samp->faces[j] = face;
1710 }
1711 }
1712 else {
1713 float sign = (rz >= 0.0F) ? 1.0F : -1.0F;
1714 uint face = (rz >= 0.0F) ? PIPE_TEX_FACE_POS_Z : PIPE_TEX_FACE_NEG_Z;
1715 for (j = 0; j < QUAD_SIZE; j++) {
1716 const float ima = -0.5 / fabsf(p[j]);
1717 ssss[j] = sign * -s[j] * ima + 0.5F;
1718 tttt[j] = t[j] * ima + 0.5F;
1719 samp->faces[j] = face;
1720 }
1721 }
1722 }
1723
1724 /* In our little pipeline, the compare stage is next. If compare
1725 * is not active, this will point somewhere deeper into the
1726 * pipeline, eg. to mip_filter or even img_filter.
1727 */
1728 samp->compare(tgsi_sampler, ssss, tttt, NULL, c0, control, rgba);
1729 }
1730
1731
1732 static void
1733 sample_swizzle(struct tgsi_sampler *tgsi_sampler,
1734 const float s[QUAD_SIZE],
1735 const float t[QUAD_SIZE],
1736 const float p[QUAD_SIZE],
1737 const float c0[QUAD_SIZE],
1738 enum tgsi_sampler_control control,
1739 float rgba[NUM_CHANNELS][QUAD_SIZE])
1740 {
1741 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1742 float rgba_temp[NUM_CHANNELS][QUAD_SIZE];
1743 const unsigned swizzle_r = samp->key.bits.swizzle_r;
1744 const unsigned swizzle_g = samp->key.bits.swizzle_g;
1745 const unsigned swizzle_b = samp->key.bits.swizzle_b;
1746 const unsigned swizzle_a = samp->key.bits.swizzle_a;
1747 unsigned j;
1748
1749 samp->sample_target(tgsi_sampler, s, t, p, c0, control, rgba_temp);
1750
1751 switch (swizzle_r) {
1752 case PIPE_SWIZZLE_ZERO:
1753 for (j = 0; j < 4; j++)
1754 rgba[0][j] = 0.0f;
1755 break;
1756 case PIPE_SWIZZLE_ONE:
1757 for (j = 0; j < 4; j++)
1758 rgba[0][j] = 1.0f;
1759 break;
1760 default:
1761 assert(swizzle_r < 4);
1762 for (j = 0; j < 4; j++)
1763 rgba[0][j] = rgba_temp[swizzle_r][j];
1764 }
1765
1766 switch (swizzle_g) {
1767 case PIPE_SWIZZLE_ZERO:
1768 for (j = 0; j < 4; j++)
1769 rgba[1][j] = 0.0f;
1770 break;
1771 case PIPE_SWIZZLE_ONE:
1772 for (j = 0; j < 4; j++)
1773 rgba[1][j] = 1.0f;
1774 break;
1775 default:
1776 assert(swizzle_g < 4);
1777 for (j = 0; j < 4; j++)
1778 rgba[1][j] = rgba_temp[swizzle_g][j];
1779 }
1780
1781 switch (swizzle_b) {
1782 case PIPE_SWIZZLE_ZERO:
1783 for (j = 0; j < 4; j++)
1784 rgba[2][j] = 0.0f;
1785 break;
1786 case PIPE_SWIZZLE_ONE:
1787 for (j = 0; j < 4; j++)
1788 rgba[2][j] = 1.0f;
1789 break;
1790 default:
1791 assert(swizzle_b < 4);
1792 for (j = 0; j < 4; j++)
1793 rgba[2][j] = rgba_temp[swizzle_b][j];
1794 }
1795
1796 switch (swizzle_a) {
1797 case PIPE_SWIZZLE_ZERO:
1798 for (j = 0; j < 4; j++)
1799 rgba[3][j] = 0.0f;
1800 break;
1801 case PIPE_SWIZZLE_ONE:
1802 for (j = 0; j < 4; j++)
1803 rgba[3][j] = 1.0f;
1804 break;
1805 default:
1806 assert(swizzle_a < 4);
1807 for (j = 0; j < 4; j++)
1808 rgba[3][j] = rgba_temp[swizzle_a][j];
1809 }
1810 }
1811
1812
1813 static wrap_nearest_func
1814 get_nearest_unorm_wrap(unsigned mode)
1815 {
1816 switch (mode) {
1817 case PIPE_TEX_WRAP_CLAMP:
1818 return wrap_nearest_unorm_clamp;
1819 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1820 return wrap_nearest_unorm_clamp_to_edge;
1821 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1822 return wrap_nearest_unorm_clamp_to_border;
1823 default:
1824 assert(0);
1825 return wrap_nearest_unorm_clamp;
1826 }
1827 }
1828
1829
1830 static wrap_nearest_func
1831 get_nearest_wrap(unsigned mode)
1832 {
1833 switch (mode) {
1834 case PIPE_TEX_WRAP_REPEAT:
1835 return wrap_nearest_repeat;
1836 case PIPE_TEX_WRAP_CLAMP:
1837 return wrap_nearest_clamp;
1838 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1839 return wrap_nearest_clamp_to_edge;
1840 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1841 return wrap_nearest_clamp_to_border;
1842 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1843 return wrap_nearest_mirror_repeat;
1844 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1845 return wrap_nearest_mirror_clamp;
1846 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1847 return wrap_nearest_mirror_clamp_to_edge;
1848 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1849 return wrap_nearest_mirror_clamp_to_border;
1850 default:
1851 assert(0);
1852 return wrap_nearest_repeat;
1853 }
1854 }
1855
1856
1857 static wrap_linear_func
1858 get_linear_unorm_wrap(unsigned mode)
1859 {
1860 switch (mode) {
1861 case PIPE_TEX_WRAP_CLAMP:
1862 return wrap_linear_unorm_clamp;
1863 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1864 return wrap_linear_unorm_clamp_to_edge;
1865 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1866 return wrap_linear_unorm_clamp_to_border;
1867 default:
1868 assert(0);
1869 return wrap_linear_unorm_clamp;
1870 }
1871 }
1872
1873
1874 static wrap_linear_func
1875 get_linear_wrap(unsigned mode)
1876 {
1877 switch (mode) {
1878 case PIPE_TEX_WRAP_REPEAT:
1879 return wrap_linear_repeat;
1880 case PIPE_TEX_WRAP_CLAMP:
1881 return wrap_linear_clamp;
1882 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1883 return wrap_linear_clamp_to_edge;
1884 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1885 return wrap_linear_clamp_to_border;
1886 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1887 return wrap_linear_mirror_repeat;
1888 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1889 return wrap_linear_mirror_clamp;
1890 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1891 return wrap_linear_mirror_clamp_to_edge;
1892 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1893 return wrap_linear_mirror_clamp_to_border;
1894 default:
1895 assert(0);
1896 return wrap_linear_repeat;
1897 }
1898 }
1899
1900
1901 static compute_lambda_func
1902 get_lambda_func(const union sp_sampler_key key)
1903 {
1904 if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
1905 return compute_lambda_vert;
1906
1907 switch (key.bits.target) {
1908 case PIPE_TEXTURE_1D:
1909 return compute_lambda_1d;
1910 case PIPE_TEXTURE_2D:
1911 case PIPE_TEXTURE_RECT:
1912 case PIPE_TEXTURE_CUBE:
1913 return compute_lambda_2d;
1914 case PIPE_TEXTURE_3D:
1915 return compute_lambda_3d;
1916 default:
1917 assert(0);
1918 return compute_lambda_1d;
1919 }
1920 }
1921
1922
1923 static filter_func
1924 get_img_filter(const union sp_sampler_key key,
1925 unsigned filter,
1926 const struct pipe_sampler_state *sampler)
1927 {
1928 switch (key.bits.target) {
1929 case PIPE_TEXTURE_1D:
1930 if (filter == PIPE_TEX_FILTER_NEAREST)
1931 return img_filter_1d_nearest;
1932 else
1933 return img_filter_1d_linear;
1934 break;
1935 case PIPE_TEXTURE_2D:
1936 case PIPE_TEXTURE_RECT:
1937 /* Try for fast path:
1938 */
1939 if (key.bits.is_pot &&
1940 sampler->wrap_s == sampler->wrap_t &&
1941 sampler->normalized_coords)
1942 {
1943 switch (sampler->wrap_s) {
1944 case PIPE_TEX_WRAP_REPEAT:
1945 switch (filter) {
1946 case PIPE_TEX_FILTER_NEAREST:
1947 return img_filter_2d_nearest_repeat_POT;
1948 case PIPE_TEX_FILTER_LINEAR:
1949 return img_filter_2d_linear_repeat_POT;
1950 default:
1951 break;
1952 }
1953 break;
1954 case PIPE_TEX_WRAP_CLAMP:
1955 switch (filter) {
1956 case PIPE_TEX_FILTER_NEAREST:
1957 return img_filter_2d_nearest_clamp_POT;
1958 default:
1959 break;
1960 }
1961 }
1962 }
1963 /* Otherwise use default versions:
1964 */
1965 if (filter == PIPE_TEX_FILTER_NEAREST)
1966 return img_filter_2d_nearest;
1967 else
1968 return img_filter_2d_linear;
1969 break;
1970 case PIPE_TEXTURE_CUBE:
1971 if (filter == PIPE_TEX_FILTER_NEAREST)
1972 return img_filter_cube_nearest;
1973 else
1974 return img_filter_cube_linear;
1975 break;
1976 case PIPE_TEXTURE_3D:
1977 if (filter == PIPE_TEX_FILTER_NEAREST)
1978 return img_filter_3d_nearest;
1979 else
1980 return img_filter_3d_linear;
1981 break;
1982 default:
1983 assert(0);
1984 return img_filter_1d_nearest;
1985 }
1986 }
1987
1988
1989 /**
1990 * Bind the given texture object and texture cache to the sampler variant.
1991 */
1992 void
1993 sp_sampler_variant_bind_view( struct sp_sampler_variant *samp,
1994 struct softpipe_tex_tile_cache *tex_cache,
1995 const struct pipe_sampler_view *view )
1996 {
1997 const struct pipe_sampler_state *sampler = samp->sampler;
1998 const struct pipe_resource *texture = view->texture;
1999
2000 samp->view = view;
2001 samp->cache = tex_cache;
2002 samp->xpot = util_unsigned_logbase2( texture->width0 );
2003 samp->ypot = util_unsigned_logbase2( texture->height0 );
2004 samp->level = CLAMP((int) sampler->min_lod, 0, (int) texture->last_level);
2005 }
2006
2007
2008 void
2009 sp_sampler_variant_destroy( struct sp_sampler_variant *samp )
2010 {
2011 FREE(samp);
2012 }
2013
2014
2015 /**
2016 * Create a sampler variant for a given set of non-orthogonal state.
2017 */
2018 struct sp_sampler_variant *
2019 sp_create_sampler_variant( const struct pipe_sampler_state *sampler,
2020 const union sp_sampler_key key )
2021 {
2022 struct sp_sampler_variant *samp = CALLOC_STRUCT(sp_sampler_variant);
2023 if (!samp)
2024 return NULL;
2025
2026 samp->sampler = sampler;
2027 samp->key = key;
2028
2029 /* Note that (for instance) linear_texcoord_s and
2030 * nearest_texcoord_s may be active at the same time, if the
2031 * sampler min_img_filter differs from its mag_img_filter.
2032 */
2033 if (sampler->normalized_coords) {
2034 samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
2035 samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
2036 samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
2037
2038 samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
2039 samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
2040 samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
2041 }
2042 else {
2043 samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
2044 samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
2045 samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
2046
2047 samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
2048 samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
2049 samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
2050 }
2051
2052 samp->compute_lambda = get_lambda_func( key );
2053
2054 samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
2055 samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
2056
2057 switch (sampler->min_mip_filter) {
2058 case PIPE_TEX_MIPFILTER_NONE:
2059 if (sampler->min_img_filter == sampler->mag_img_filter)
2060 samp->mip_filter = samp->min_img_filter;
2061 else
2062 samp->mip_filter = mip_filter_none;
2063 break;
2064
2065 case PIPE_TEX_MIPFILTER_NEAREST:
2066 samp->mip_filter = mip_filter_nearest;
2067 break;
2068
2069 case PIPE_TEX_MIPFILTER_LINEAR:
2070 if (key.bits.is_pot &&
2071 sampler->min_img_filter == sampler->mag_img_filter &&
2072 sampler->normalized_coords &&
2073 sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
2074 sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
2075 sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR)
2076 {
2077 samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
2078 }
2079 else
2080 {
2081 samp->mip_filter = mip_filter_linear;
2082 }
2083 break;
2084 }
2085
2086 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
2087 samp->compare = sample_compare;
2088 }
2089 else {
2090 /* Skip compare operation by promoting the mip_filter function
2091 * pointer:
2092 */
2093 samp->compare = samp->mip_filter;
2094 }
2095
2096 if (key.bits.target == PIPE_TEXTURE_CUBE) {
2097 samp->sample_target = sample_cube;
2098 }
2099 else {
2100 samp->faces[0] = 0;
2101 samp->faces[1] = 0;
2102 samp->faces[2] = 0;
2103 samp->faces[3] = 0;
2104
2105 /* Skip cube face determination by promoting the compare
2106 * function pointer:
2107 */
2108 samp->sample_target = samp->compare;
2109 }
2110
2111 if (key.bits.swizzle_r != PIPE_SWIZZLE_RED ||
2112 key.bits.swizzle_g != PIPE_SWIZZLE_GREEN ||
2113 key.bits.swizzle_b != PIPE_SWIZZLE_BLUE ||
2114 key.bits.swizzle_a != PIPE_SWIZZLE_ALPHA) {
2115 samp->base.get_samples = sample_swizzle;
2116 }
2117 else {
2118 samp->base.get_samples = samp->sample_target;
2119 }
2120
2121 return samp;
2122 }