softpipe: implement coord clamping for texel fetches (TXF)
[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 * Do coordinate to array index conversion. For array textures.
544 */
545 static INLINE void
546 wrap_array_layer(const float coord[4], unsigned size, int layer[4])
547 {
548 uint ch;
549 for (ch = 0; ch < 4; ch++) {
550 int c = util_ifloor(coord[ch] + 0.5F);
551 layer[ch] = CLAMP(c, 0, size - 1);
552 }
553 }
554
555
556 /**
557 * Examine the quad's texture coordinates to compute the partial
558 * derivatives w.r.t X and Y, then compute lambda (level of detail).
559 */
560 static float
561 compute_lambda_1d(const struct sp_sampler_variant *samp,
562 const float s[TGSI_QUAD_SIZE],
563 const float t[TGSI_QUAD_SIZE],
564 const float p[TGSI_QUAD_SIZE])
565 {
566 const struct pipe_resource *texture = samp->view->texture;
567 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
568 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
569 float rho = MAX2(dsdx, dsdy) * u_minify(texture->width0, samp->view->u.tex.first_level);
570
571 return util_fast_log2(rho);
572 }
573
574
575 static float
576 compute_lambda_2d(const struct sp_sampler_variant *samp,
577 const float s[TGSI_QUAD_SIZE],
578 const float t[TGSI_QUAD_SIZE],
579 const float p[TGSI_QUAD_SIZE])
580 {
581 const struct pipe_resource *texture = samp->view->texture;
582 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
583 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
584 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
585 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
586 float maxx = MAX2(dsdx, dsdy) * u_minify(texture->width0, samp->view->u.tex.first_level);
587 float maxy = MAX2(dtdx, dtdy) * u_minify(texture->height0, samp->view->u.tex.first_level);
588 float rho = MAX2(maxx, maxy);
589
590 return util_fast_log2(rho);
591 }
592
593
594 static float
595 compute_lambda_3d(const struct sp_sampler_variant *samp,
596 const float s[TGSI_QUAD_SIZE],
597 const float t[TGSI_QUAD_SIZE],
598 const float p[TGSI_QUAD_SIZE])
599 {
600 const struct pipe_resource *texture = samp->view->texture;
601 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
602 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
603 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
604 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
605 float dpdx = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);
606 float dpdy = fabsf(p[QUAD_TOP_LEFT] - p[QUAD_BOTTOM_LEFT]);
607 float maxx = MAX2(dsdx, dsdy) * u_minify(texture->width0, samp->view->u.tex.first_level);
608 float maxy = MAX2(dtdx, dtdy) * u_minify(texture->height0, samp->view->u.tex.first_level);
609 float maxz = MAX2(dpdx, dpdy) * u_minify(texture->depth0, samp->view->u.tex.first_level);
610 float rho;
611
612 rho = MAX2(maxx, maxy);
613 rho = MAX2(rho, maxz);
614
615 return util_fast_log2(rho);
616 }
617
618
619 /**
620 * Compute lambda for a vertex texture sampler.
621 * Since there aren't derivatives to use, just return 0.
622 */
623 static float
624 compute_lambda_vert(const struct sp_sampler_variant *samp,
625 const float s[TGSI_QUAD_SIZE],
626 const float t[TGSI_QUAD_SIZE],
627 const float p[TGSI_QUAD_SIZE])
628 {
629 return 0.0f;
630 }
631
632
633
634 /**
635 * Get a texel from a texture, using the texture tile cache.
636 *
637 * \param addr the template tex address containing cube, z, face info.
638 * \param x the x coord of texel within 2D image
639 * \param y the y coord of texel within 2D image
640 * \param rgba the quad to put the texel/color into
641 *
642 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
643 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
644 */
645
646
647
648
649 static INLINE const float *
650 get_texel_2d_no_border(const struct sp_sampler_variant *samp,
651 union tex_tile_address addr, int x, int y)
652 {
653 const struct softpipe_tex_cached_tile *tile;
654
655 addr.bits.x = x / TILE_SIZE;
656 addr.bits.y = y / TILE_SIZE;
657 y %= TILE_SIZE;
658 x %= TILE_SIZE;
659
660 tile = sp_get_cached_tile_tex(samp->cache, addr);
661
662 return &tile->data.color[y][x][0];
663 }
664
665
666 static INLINE const float *
667 get_texel_2d(const struct sp_sampler_variant *samp,
668 union tex_tile_address addr, int x, int y)
669 {
670 const struct pipe_resource *texture = samp->view->texture;
671 unsigned level = addr.bits.level;
672
673 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
674 y < 0 || y >= (int) u_minify(texture->height0, level)) {
675 return samp->sampler->border_color.f;
676 }
677 else {
678 return get_texel_2d_no_border( samp, addr, x, y );
679 }
680 }
681
682
683 /* Gather a quad of adjacent texels within a tile:
684 */
685 static INLINE void
686 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant *samp,
687 union tex_tile_address addr,
688 unsigned x, unsigned y,
689 const float *out[4])
690 {
691 const struct softpipe_tex_cached_tile *tile;
692
693 addr.bits.x = x / TILE_SIZE;
694 addr.bits.y = y / TILE_SIZE;
695 y %= TILE_SIZE;
696 x %= TILE_SIZE;
697
698 tile = sp_get_cached_tile_tex(samp->cache, addr);
699
700 out[0] = &tile->data.color[y ][x ][0];
701 out[1] = &tile->data.color[y ][x+1][0];
702 out[2] = &tile->data.color[y+1][x ][0];
703 out[3] = &tile->data.color[y+1][x+1][0];
704 }
705
706
707 /* Gather a quad of potentially non-adjacent texels:
708 */
709 static INLINE void
710 get_texel_quad_2d_no_border(const struct sp_sampler_variant *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_no_border( samp, addr, x0, y0 );
717 out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
718 out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
719 out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
720 }
721
722 /* Can involve a lot of unnecessary checks for border color:
723 */
724 static INLINE void
725 get_texel_quad_2d(const struct sp_sampler_variant *samp,
726 union tex_tile_address addr,
727 int x0, int y0,
728 int x1, int y1,
729 const float *out[4])
730 {
731 out[0] = get_texel_2d( samp, addr, x0, y0 );
732 out[1] = get_texel_2d( samp, addr, x1, y0 );
733 out[3] = get_texel_2d( samp, addr, x1, y1 );
734 out[2] = get_texel_2d( samp, addr, x0, y1 );
735 }
736
737
738
739 /* 3d variants:
740 */
741 static INLINE const float *
742 get_texel_3d_no_border(const struct sp_sampler_variant *samp,
743 union tex_tile_address addr, int x, int y, int z)
744 {
745 const struct softpipe_tex_cached_tile *tile;
746
747 addr.bits.x = x / TILE_SIZE;
748 addr.bits.y = y / TILE_SIZE;
749 addr.bits.z = z;
750 y %= TILE_SIZE;
751 x %= TILE_SIZE;
752
753 tile = sp_get_cached_tile_tex(samp->cache, addr);
754
755 return &tile->data.color[y][x][0];
756 }
757
758
759 static INLINE const float *
760 get_texel_3d(const struct sp_sampler_variant *samp,
761 union tex_tile_address addr, int x, int y, int z)
762 {
763 const struct pipe_resource *texture = samp->view->texture;
764 unsigned level = addr.bits.level;
765
766 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
767 y < 0 || y >= (int) u_minify(texture->height0, level) ||
768 z < 0 || z >= (int) u_minify(texture->depth0, level)) {
769 return samp->sampler->border_color.f;
770 }
771 else {
772 return get_texel_3d_no_border( samp, addr, x, y, z );
773 }
774 }
775
776
777 /* Get texel pointer for 1D array texture */
778 static INLINE const float *
779 get_texel_1d_array(const struct sp_sampler_variant *samp,
780 union tex_tile_address addr, int x, int y)
781 {
782 const struct pipe_resource *texture = samp->view->texture;
783 unsigned level = addr.bits.level;
784
785 if (x < 0 || x >= (int) u_minify(texture->width0, level)) {
786 return samp->sampler->border_color.f;
787 }
788 else {
789 return get_texel_2d_no_border(samp, addr, x, y);
790 }
791 }
792
793
794 /* Get texel pointer for 2D array texture */
795 static INLINE const float *
796 get_texel_2d_array(const struct sp_sampler_variant *samp,
797 union tex_tile_address addr, int x, int y, int layer)
798 {
799 const struct pipe_resource *texture = samp->view->texture;
800 unsigned level = addr.bits.level;
801
802 assert(layer < (int) texture->array_size);
803 assert(layer >= 0);
804
805 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
806 y < 0 || y >= (int) u_minify(texture->height0, level)) {
807 return samp->sampler->border_color.f;
808 }
809 else {
810 return get_texel_3d_no_border(samp, addr, x, y, layer);
811 }
812 }
813
814
815 /**
816 * Given the logbase2 of a mipmap's base level size and a mipmap level,
817 * return the size (in texels) of that mipmap level.
818 * For example, if level[0].width = 256 then base_pot will be 8.
819 * If level = 2, then we'll return 64 (the width at level=2).
820 * Return 1 if level > base_pot.
821 */
822 static INLINE unsigned
823 pot_level_size(unsigned base_pot, unsigned level)
824 {
825 return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
826 }
827
828
829 static void
830 print_sample(const char *function, float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
831 {
832 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
833 function,
834 rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
835 rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
836 rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
837 rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
838 }
839
840
841 /* Some image-filter fastpaths:
842 */
843 static INLINE void
844 img_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
845 const float s[TGSI_QUAD_SIZE],
846 const float t[TGSI_QUAD_SIZE],
847 const float p[TGSI_QUAD_SIZE],
848 const float c0[TGSI_QUAD_SIZE],
849 enum tgsi_sampler_control control,
850 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
851 {
852 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
853 unsigned j;
854 unsigned level = samp->level;
855 unsigned xpot = pot_level_size(samp->xpot, level);
856 unsigned ypot = pot_level_size(samp->ypot, level);
857 unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
858 unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
859 union tex_tile_address addr;
860
861 addr.value = 0;
862 addr.bits.level = samp->level;
863
864 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
865 int c;
866
867 float u = s[j] * xpot - 0.5F;
868 float v = t[j] * ypot - 0.5F;
869
870 int uflr = util_ifloor(u);
871 int vflr = util_ifloor(v);
872
873 float xw = u - (float)uflr;
874 float yw = v - (float)vflr;
875
876 int x0 = uflr & (xpot - 1);
877 int y0 = vflr & (ypot - 1);
878
879 const float *tx[4];
880
881 /* Can we fetch all four at once:
882 */
883 if (x0 < xmax && y0 < ymax) {
884 get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
885 }
886 else {
887 unsigned x1 = (x0 + 1) & (xpot - 1);
888 unsigned y1 = (y0 + 1) & (ypot - 1);
889 get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
890 }
891
892 /* interpolate R, G, B, A */
893 for (c = 0; c < 4; c++) {
894 rgba[c][j] = lerp_2d(xw, yw,
895 tx[0][c], tx[1][c],
896 tx[2][c], tx[3][c]);
897 }
898 }
899
900 if (DEBUG_TEX) {
901 print_sample(__FUNCTION__, rgba);
902 }
903 }
904
905
906 static INLINE void
907 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
908 const float s[TGSI_QUAD_SIZE],
909 const float t[TGSI_QUAD_SIZE],
910 const float p[TGSI_QUAD_SIZE],
911 const float c0[TGSI_QUAD_SIZE],
912 enum tgsi_sampler_control control,
913 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
914 {
915 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
916 unsigned j;
917 unsigned level = samp->level;
918 unsigned xpot = pot_level_size(samp->xpot, level);
919 unsigned ypot = pot_level_size(samp->ypot, level);
920 union tex_tile_address addr;
921
922 addr.value = 0;
923 addr.bits.level = samp->level;
924
925 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
926 int c;
927
928 float u = s[j] * xpot;
929 float v = t[j] * ypot;
930
931 int uflr = util_ifloor(u);
932 int vflr = util_ifloor(v);
933
934 int x0 = uflr & (xpot - 1);
935 int y0 = vflr & (ypot - 1);
936
937 const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
938
939 for (c = 0; c < 4; c++) {
940 rgba[c][j] = out[c];
941 }
942 }
943
944 if (DEBUG_TEX) {
945 print_sample(__FUNCTION__, rgba);
946 }
947 }
948
949
950 static INLINE void
951 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
952 const float s[TGSI_QUAD_SIZE],
953 const float t[TGSI_QUAD_SIZE],
954 const float p[TGSI_QUAD_SIZE],
955 const float c0[TGSI_QUAD_SIZE],
956 enum tgsi_sampler_control control,
957 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
958 {
959 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
960 unsigned j;
961 unsigned level = samp->level;
962 unsigned xpot = pot_level_size(samp->xpot, level);
963 unsigned ypot = pot_level_size(samp->ypot, level);
964 union tex_tile_address addr;
965
966 addr.value = 0;
967 addr.bits.level = samp->level;
968
969 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
970 int c;
971
972 float u = s[j] * xpot;
973 float v = t[j] * ypot;
974
975 int x0, y0;
976 const float *out;
977
978 x0 = util_ifloor(u);
979 if (x0 < 0)
980 x0 = 0;
981 else if (x0 > xpot - 1)
982 x0 = xpot - 1;
983
984 y0 = util_ifloor(v);
985 if (y0 < 0)
986 y0 = 0;
987 else if (y0 > ypot - 1)
988 y0 = ypot - 1;
989
990 out = get_texel_2d_no_border(samp, addr, x0, y0);
991
992 for (c = 0; c < 4; c++) {
993 rgba[c][j] = out[c];
994 }
995 }
996
997 if (DEBUG_TEX) {
998 print_sample(__FUNCTION__, rgba);
999 }
1000 }
1001
1002
1003 static void
1004 img_filter_1d_nearest(struct tgsi_sampler *tgsi_sampler,
1005 const float s[TGSI_QUAD_SIZE],
1006 const float t[TGSI_QUAD_SIZE],
1007 const float p[TGSI_QUAD_SIZE],
1008 const float c0[TGSI_QUAD_SIZE],
1009 enum tgsi_sampler_control control,
1010 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1011 {
1012 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1013 const struct pipe_resource *texture = samp->view->texture;
1014 unsigned level0, j;
1015 int width;
1016 int x[4];
1017 union tex_tile_address addr;
1018
1019 level0 = samp->level;
1020 width = u_minify(texture->width0, level0);
1021
1022 assert(width > 0);
1023
1024 addr.value = 0;
1025 addr.bits.level = samp->level;
1026
1027 samp->nearest_texcoord_s(s, width, x);
1028
1029 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1030 const float *out = get_texel_2d(samp, addr, x[j], 0);
1031 int c;
1032 for (c = 0; c < 4; c++) {
1033 rgba[c][j] = out[c];
1034 }
1035 }
1036
1037 if (DEBUG_TEX) {
1038 print_sample(__FUNCTION__, rgba);
1039 }
1040 }
1041
1042
1043 static void
1044 img_filter_1d_array_nearest(struct tgsi_sampler *tgsi_sampler,
1045 const float s[TGSI_QUAD_SIZE],
1046 const float t[TGSI_QUAD_SIZE],
1047 const float p[TGSI_QUAD_SIZE],
1048 const float c0[TGSI_QUAD_SIZE],
1049 enum tgsi_sampler_control control,
1050 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1051 {
1052 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1053 const struct pipe_resource *texture = samp->view->texture;
1054 unsigned level0, j;
1055 int width;
1056 int x[4], layer[4];
1057 union tex_tile_address addr;
1058
1059 level0 = samp->level;
1060 width = u_minify(texture->width0, level0);
1061
1062 assert(width > 0);
1063
1064 addr.value = 0;
1065 addr.bits.level = samp->level;
1066
1067 samp->nearest_texcoord_s(s, width, x);
1068 wrap_array_layer(t, texture->array_size, layer);
1069
1070 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1071 const float *out = get_texel_1d_array(samp, addr, x[j], layer[j]);
1072 int c;
1073 for (c = 0; c < 4; c++) {
1074 rgba[c][j] = out[c];
1075 }
1076 }
1077
1078 if (DEBUG_TEX) {
1079 print_sample(__FUNCTION__, rgba);
1080 }
1081 }
1082
1083
1084 static void
1085 img_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
1086 const float s[TGSI_QUAD_SIZE],
1087 const float t[TGSI_QUAD_SIZE],
1088 const float p[TGSI_QUAD_SIZE],
1089 const float c0[TGSI_QUAD_SIZE],
1090 enum tgsi_sampler_control control,
1091 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1092 {
1093 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1094 const struct pipe_resource *texture = samp->view->texture;
1095 unsigned level0, j;
1096 int width, height;
1097 int x[4], y[4];
1098 union tex_tile_address addr;
1099
1100
1101 level0 = samp->level;
1102 width = u_minify(texture->width0, level0);
1103 height = u_minify(texture->height0, level0);
1104
1105 assert(width > 0);
1106 assert(height > 0);
1107
1108 addr.value = 0;
1109 addr.bits.level = samp->level;
1110
1111 samp->nearest_texcoord_s(s, width, x);
1112 samp->nearest_texcoord_t(t, height, y);
1113
1114 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1115 const float *out = get_texel_2d(samp, addr, x[j], y[j]);
1116 int c;
1117 for (c = 0; c < 4; c++) {
1118 rgba[c][j] = out[c];
1119 }
1120 }
1121
1122 if (DEBUG_TEX) {
1123 print_sample(__FUNCTION__, rgba);
1124 }
1125 }
1126
1127
1128 static void
1129 img_filter_2d_array_nearest(struct tgsi_sampler *tgsi_sampler,
1130 const float s[TGSI_QUAD_SIZE],
1131 const float t[TGSI_QUAD_SIZE],
1132 const float p[TGSI_QUAD_SIZE],
1133 const float c0[TGSI_QUAD_SIZE],
1134 enum tgsi_sampler_control control,
1135 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1136 {
1137 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1138 const struct pipe_resource *texture = samp->view->texture;
1139 unsigned level0, j;
1140 int width, height;
1141 int x[4], y[4], layer[4];
1142 union tex_tile_address addr;
1143
1144 level0 = samp->level;
1145 width = u_minify(texture->width0, level0);
1146 height = u_minify(texture->height0, level0);
1147
1148 assert(width > 0);
1149 assert(height > 0);
1150
1151 addr.value = 0;
1152 addr.bits.level = samp->level;
1153
1154 samp->nearest_texcoord_s(s, width, x);
1155 samp->nearest_texcoord_t(t, height, y);
1156 wrap_array_layer(p, texture->array_size, layer);
1157
1158 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1159 const float *out = get_texel_2d_array(samp, addr, x[j], y[j], layer[j]);
1160 int c;
1161 for (c = 0; c < 4; c++) {
1162 rgba[c][j] = out[c];
1163 }
1164 }
1165
1166 if (DEBUG_TEX) {
1167 print_sample(__FUNCTION__, rgba);
1168 }
1169 }
1170
1171
1172 static INLINE union tex_tile_address
1173 face(union tex_tile_address addr, unsigned face )
1174 {
1175 addr.bits.face = face;
1176 return addr;
1177 }
1178
1179
1180 static void
1181 img_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
1182 const float s[TGSI_QUAD_SIZE],
1183 const float t[TGSI_QUAD_SIZE],
1184 const float p[TGSI_QUAD_SIZE],
1185 const float c0[TGSI_QUAD_SIZE],
1186 enum tgsi_sampler_control control,
1187 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1188 {
1189 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1190 const struct pipe_resource *texture = samp->view->texture;
1191 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1192 unsigned level0, j;
1193 int width, height;
1194 int x[4], y[4];
1195 union tex_tile_address addr;
1196
1197 level0 = samp->level;
1198 width = u_minify(texture->width0, level0);
1199 height = u_minify(texture->height0, level0);
1200
1201 assert(width > 0);
1202 assert(height > 0);
1203
1204 addr.value = 0;
1205 addr.bits.level = samp->level;
1206
1207 samp->nearest_texcoord_s(s, width, x);
1208 samp->nearest_texcoord_t(t, height, y);
1209
1210 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1211 const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
1212 int c;
1213 for (c = 0; c < 4; c++) {
1214 rgba[c][j] = out[c];
1215 }
1216 }
1217
1218 if (DEBUG_TEX) {
1219 print_sample(__FUNCTION__, rgba);
1220 }
1221 }
1222
1223
1224 static void
1225 img_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
1226 const float s[TGSI_QUAD_SIZE],
1227 const float t[TGSI_QUAD_SIZE],
1228 const float p[TGSI_QUAD_SIZE],
1229 const float c0[TGSI_QUAD_SIZE],
1230 enum tgsi_sampler_control control,
1231 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1232 {
1233 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1234 const struct pipe_resource *texture = samp->view->texture;
1235 unsigned level0, j;
1236 int width, height, depth;
1237 int x[4], y[4], z[4];
1238 union tex_tile_address addr;
1239
1240 level0 = samp->level;
1241 width = u_minify(texture->width0, level0);
1242 height = u_minify(texture->height0, level0);
1243 depth = u_minify(texture->depth0, level0);
1244
1245 assert(width > 0);
1246 assert(height > 0);
1247 assert(depth > 0);
1248
1249 samp->nearest_texcoord_s(s, width, x);
1250 samp->nearest_texcoord_t(t, height, y);
1251 samp->nearest_texcoord_p(p, depth, z);
1252
1253 addr.value = 0;
1254 addr.bits.level = samp->level;
1255
1256 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1257 const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
1258 int c;
1259 for (c = 0; c < 4; c++) {
1260 rgba[c][j] = out[c];
1261 }
1262 }
1263 }
1264
1265
1266 static void
1267 img_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
1268 const float s[TGSI_QUAD_SIZE],
1269 const float t[TGSI_QUAD_SIZE],
1270 const float p[TGSI_QUAD_SIZE],
1271 const float c0[TGSI_QUAD_SIZE],
1272 enum tgsi_sampler_control control,
1273 float rgba[TGSI_NUM_CHANNELS][TGSI_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;
1279 int x0[4], x1[4];
1280 float xw[4]; /* weights */
1281 union tex_tile_address addr;
1282
1283 level0 = samp->level;
1284 width = u_minify(texture->width0, level0);
1285
1286 assert(width > 0);
1287
1288 addr.value = 0;
1289 addr.bits.level = samp->level;
1290
1291 samp->linear_texcoord_s(s, width, x0, x1, xw);
1292
1293 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1294 const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
1295 const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
1296 int c;
1297
1298 /* interpolate R, G, B, A */
1299 for (c = 0; c < 4; c++) {
1300 rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1301 }
1302 }
1303 }
1304
1305
1306 static void
1307 img_filter_1d_array_linear(struct tgsi_sampler *tgsi_sampler,
1308 const float s[TGSI_QUAD_SIZE],
1309 const float t[TGSI_QUAD_SIZE],
1310 const float p[TGSI_QUAD_SIZE],
1311 const float c0[TGSI_QUAD_SIZE],
1312 enum tgsi_sampler_control control,
1313 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1314 {
1315 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1316 const struct pipe_resource *texture = samp->view->texture;
1317 unsigned level0, j;
1318 int width;
1319 int x0[4], x1[4], layer[4];
1320 float xw[4]; /* weights */
1321 union tex_tile_address addr;
1322
1323 level0 = samp->level;
1324 width = u_minify(texture->width0, level0);
1325
1326 assert(width > 0);
1327
1328 addr.value = 0;
1329 addr.bits.level = samp->level;
1330
1331 samp->linear_texcoord_s(s, width, x0, x1, xw);
1332 wrap_array_layer(t, texture->array_size, layer);
1333
1334 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1335 const float *tx0 = get_texel_1d_array(samp, addr, x0[j], layer[j]);
1336 const float *tx1 = get_texel_1d_array(samp, addr, x1[j], layer[j]);
1337 int c;
1338
1339 /* interpolate R, G, B, A */
1340 for (c = 0; c < 4; c++) {
1341 rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1342 }
1343 }
1344 }
1345
1346
1347 static void
1348 img_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
1349 const float s[TGSI_QUAD_SIZE],
1350 const float t[TGSI_QUAD_SIZE],
1351 const float p[TGSI_QUAD_SIZE],
1352 const float c0[TGSI_QUAD_SIZE],
1353 enum tgsi_sampler_control control,
1354 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1355 {
1356 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1357 const struct pipe_resource *texture = samp->view->texture;
1358 unsigned level0, j;
1359 int width, height;
1360 int x0[4], y0[4], x1[4], y1[4];
1361 float xw[4], yw[4]; /* weights */
1362 union tex_tile_address addr;
1363
1364 level0 = samp->level;
1365 width = u_minify(texture->width0, level0);
1366 height = u_minify(texture->height0, level0);
1367
1368 assert(width > 0);
1369 assert(height > 0);
1370
1371 addr.value = 0;
1372 addr.bits.level = samp->level;
1373
1374 samp->linear_texcoord_s(s, width, x0, x1, xw);
1375 samp->linear_texcoord_t(t, height, y0, y1, yw);
1376
1377 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1378 const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
1379 const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
1380 const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
1381 const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
1382 int c;
1383
1384 /* interpolate R, G, B, A */
1385 for (c = 0; c < 4; c++) {
1386 rgba[c][j] = lerp_2d(xw[j], yw[j],
1387 tx0[c], tx1[c],
1388 tx2[c], tx3[c]);
1389 }
1390 }
1391 }
1392
1393
1394 static void
1395 img_filter_2d_array_linear(struct tgsi_sampler *tgsi_sampler,
1396 const float s[TGSI_QUAD_SIZE],
1397 const float t[TGSI_QUAD_SIZE],
1398 const float p[TGSI_QUAD_SIZE],
1399 const float c0[TGSI_QUAD_SIZE],
1400 enum tgsi_sampler_control control,
1401 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1402 {
1403 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1404 const struct pipe_resource *texture = samp->view->texture;
1405 unsigned level0, j;
1406 int width, height;
1407 int x0[4], y0[4], x1[4], y1[4], layer[4];
1408 float xw[4], yw[4]; /* weights */
1409 union tex_tile_address addr;
1410
1411 level0 = samp->level;
1412 width = u_minify(texture->width0, level0);
1413 height = u_minify(texture->height0, level0);
1414
1415 assert(width > 0);
1416 assert(height > 0);
1417
1418 addr.value = 0;
1419 addr.bits.level = samp->level;
1420
1421 samp->linear_texcoord_s(s, width, x0, x1, xw);
1422 samp->linear_texcoord_t(t, height, y0, y1, yw);
1423 wrap_array_layer(p, texture->array_size, layer);
1424
1425 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1426 const float *tx0 = get_texel_2d_array(samp, addr, x0[j], y0[j], layer[j]);
1427 const float *tx1 = get_texel_2d_array(samp, addr, x1[j], y0[j], layer[j]);
1428 const float *tx2 = get_texel_2d_array(samp, addr, x0[j], y1[j], layer[j]);
1429 const float *tx3 = get_texel_2d_array(samp, addr, x1[j], y1[j], layer[j]);
1430 int c;
1431
1432 /* interpolate R, G, B, A */
1433 for (c = 0; c < 4; c++) {
1434 rgba[c][j] = lerp_2d(xw[j], yw[j],
1435 tx0[c], tx1[c],
1436 tx2[c], tx3[c]);
1437 }
1438 }
1439 }
1440
1441
1442 static void
1443 img_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1444 const float s[TGSI_QUAD_SIZE],
1445 const float t[TGSI_QUAD_SIZE],
1446 const float p[TGSI_QUAD_SIZE],
1447 const float c0[TGSI_QUAD_SIZE],
1448 enum tgsi_sampler_control control,
1449 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1450 {
1451 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1452 const struct pipe_resource *texture = samp->view->texture;
1453 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1454 unsigned level0, j;
1455 int width, height;
1456 int x0[4], y0[4], x1[4], y1[4];
1457 float xw[4], yw[4]; /* weights */
1458 union tex_tile_address addr;
1459
1460 level0 = samp->level;
1461 width = u_minify(texture->width0, level0);
1462 height = u_minify(texture->height0, level0);
1463
1464 assert(width > 0);
1465 assert(height > 0);
1466
1467 addr.value = 0;
1468 addr.bits.level = samp->level;
1469
1470 samp->linear_texcoord_s(s, width, x0, x1, xw);
1471 samp->linear_texcoord_t(t, height, y0, y1, yw);
1472
1473 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1474 union tex_tile_address addrj = face(addr, faces[j]);
1475 const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
1476 const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
1477 const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
1478 const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
1479 int c;
1480
1481 /* interpolate R, G, B, A */
1482 for (c = 0; c < 4; c++) {
1483 rgba[c][j] = lerp_2d(xw[j], yw[j],
1484 tx0[c], tx1[c],
1485 tx2[c], tx3[c]);
1486 }
1487 }
1488 }
1489
1490
1491 static void
1492 img_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
1493 const float s[TGSI_QUAD_SIZE],
1494 const float t[TGSI_QUAD_SIZE],
1495 const float p[TGSI_QUAD_SIZE],
1496 const float c0[TGSI_QUAD_SIZE],
1497 enum tgsi_sampler_control control,
1498 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1499 {
1500 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1501 const struct pipe_resource *texture = samp->view->texture;
1502 unsigned level0, j;
1503 int width, height, depth;
1504 int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
1505 float xw[4], yw[4], zw[4]; /* interpolation weights */
1506 union tex_tile_address addr;
1507
1508 level0 = samp->level;
1509 width = u_minify(texture->width0, level0);
1510 height = u_minify(texture->height0, level0);
1511 depth = u_minify(texture->depth0, level0);
1512
1513 addr.value = 0;
1514 addr.bits.level = level0;
1515
1516 assert(width > 0);
1517 assert(height > 0);
1518 assert(depth > 0);
1519
1520 samp->linear_texcoord_s(s, width, x0, x1, xw);
1521 samp->linear_texcoord_t(t, height, y0, y1, yw);
1522 samp->linear_texcoord_p(p, depth, z0, z1, zw);
1523
1524 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1525 int c;
1526
1527 const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
1528 const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
1529 const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
1530 const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
1531
1532 const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
1533 const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
1534 const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
1535 const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
1536
1537 /* interpolate R, G, B, A */
1538 for (c = 0; c < 4; c++) {
1539 rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
1540 tx00[c], tx01[c],
1541 tx02[c], tx03[c],
1542 tx10[c], tx11[c],
1543 tx12[c], tx13[c]);
1544 }
1545 }
1546 }
1547
1548
1549 /* Calculate level of detail for every fragment.
1550 * Note that lambda has already been biased by global LOD bias.
1551 */
1552 static INLINE void
1553 compute_lod(const struct pipe_sampler_state *sampler,
1554 const float biased_lambda,
1555 const float lodbias[TGSI_QUAD_SIZE],
1556 float lod[TGSI_QUAD_SIZE])
1557 {
1558 uint i;
1559
1560 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1561 lod[i] = biased_lambda + lodbias[i];
1562 lod[i] = CLAMP(lod[i], sampler->min_lod, sampler->max_lod);
1563 }
1564 }
1565
1566
1567 static void
1568 mip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1569 const float s[TGSI_QUAD_SIZE],
1570 const float t[TGSI_QUAD_SIZE],
1571 const float p[TGSI_QUAD_SIZE],
1572 const float c0[TGSI_QUAD_SIZE],
1573 enum tgsi_sampler_control control,
1574 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1575 {
1576 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1577 const struct pipe_resource *texture = samp->view->texture;
1578 int level0;
1579 float lambda;
1580 float lod[TGSI_QUAD_SIZE];
1581
1582 if (control == tgsi_sampler_lod_bias) {
1583 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1584 compute_lod(samp->sampler, lambda, c0, lod);
1585 } else {
1586 assert(control == tgsi_sampler_lod_explicit);
1587
1588 memcpy(lod, c0, sizeof(lod));
1589 }
1590
1591 /* XXX: Take into account all lod values.
1592 */
1593 lambda = lod[0];
1594 level0 = samp->view->u.tex.first_level + (int)lambda;
1595
1596 if (lambda < 0.0) {
1597 samp->level = samp->view->u.tex.first_level;
1598 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1599 }
1600 else if (level0 >= texture->last_level) {
1601 samp->level = texture->last_level;
1602 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1603 }
1604 else {
1605 float levelBlend = frac(lambda);
1606 float rgba0[4][4];
1607 float rgba1[4][4];
1608 int c,j;
1609
1610 samp->level = level0;
1611 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1612
1613 samp->level = level0+1;
1614 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1615
1616 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1617 for (c = 0; c < 4; c++) {
1618 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1619 }
1620 }
1621 }
1622
1623 if (DEBUG_TEX) {
1624 print_sample(__FUNCTION__, rgba);
1625 }
1626 }
1627
1628
1629 /**
1630 * Compute nearest mipmap level from texcoords.
1631 * Then sample the texture level for four elements of a quad.
1632 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1633 */
1634 static void
1635 mip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
1636 const float s[TGSI_QUAD_SIZE],
1637 const float t[TGSI_QUAD_SIZE],
1638 const float p[TGSI_QUAD_SIZE],
1639 const float c0[TGSI_QUAD_SIZE],
1640 enum tgsi_sampler_control control,
1641 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1642 {
1643 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1644 const struct pipe_resource *texture = samp->view->texture;
1645 float lambda;
1646 float lod[TGSI_QUAD_SIZE];
1647
1648 if (control == tgsi_sampler_lod_bias) {
1649 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1650 compute_lod(samp->sampler, lambda, c0, lod);
1651 } else {
1652 assert(control == tgsi_sampler_lod_explicit);
1653
1654 memcpy(lod, c0, sizeof(lod));
1655 }
1656
1657 /* XXX: Take into account all lod values.
1658 */
1659 lambda = lod[0];
1660
1661 if (lambda < 0.0) {
1662 samp->level = samp->view->u.tex.first_level;
1663 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1664 }
1665 else {
1666 samp->level = samp->view->u.tex.first_level + (int)(lambda + 0.5F) ;
1667 samp->level = MIN2(samp->level, (int)texture->last_level);
1668 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1669 }
1670
1671 if (DEBUG_TEX) {
1672 print_sample(__FUNCTION__, rgba);
1673 }
1674 }
1675
1676
1677 static void
1678 mip_filter_none(struct tgsi_sampler *tgsi_sampler,
1679 const float s[TGSI_QUAD_SIZE],
1680 const float t[TGSI_QUAD_SIZE],
1681 const float p[TGSI_QUAD_SIZE],
1682 const float c0[TGSI_QUAD_SIZE],
1683 enum tgsi_sampler_control control,
1684 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1685 {
1686 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1687 float lambda;
1688 float lod[TGSI_QUAD_SIZE];
1689
1690 if (control == tgsi_sampler_lod_bias) {
1691 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1692 compute_lod(samp->sampler, lambda, c0, lod);
1693 } else {
1694 assert(control == tgsi_sampler_lod_explicit);
1695
1696 memcpy(lod, c0, sizeof(lod));
1697 }
1698
1699 /* XXX: Take into account all lod values.
1700 */
1701 lambda = lod[0];
1702
1703 samp->level = samp->view->u.tex.first_level;
1704 if (lambda < 0.0) {
1705 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1706 }
1707 else {
1708 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1709 }
1710 }
1711
1712
1713 /* For anisotropic filtering */
1714 #define WEIGHT_LUT_SIZE 1024
1715
1716 static float *weightLut = NULL;
1717
1718 /**
1719 * Creates the look-up table used to speed-up EWA sampling
1720 */
1721 static void
1722 create_filter_table(void)
1723 {
1724 unsigned i;
1725 if (!weightLut) {
1726 weightLut = (float *) MALLOC(WEIGHT_LUT_SIZE * sizeof(float));
1727
1728 for (i = 0; i < WEIGHT_LUT_SIZE; ++i) {
1729 float alpha = 2;
1730 float r2 = (float) i / (float) (WEIGHT_LUT_SIZE - 1);
1731 float weight = (float) exp(-alpha * r2);
1732 weightLut[i] = weight;
1733 }
1734 }
1735 }
1736
1737
1738 /**
1739 * Elliptical weighted average (EWA) filter for producing high quality
1740 * anisotropic filtered results.
1741 * Based on the Higher Quality Elliptical Weighted Avarage Filter
1742 * published by Paul S. Heckbert in his Master's Thesis
1743 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1744 */
1745 static void
1746 img_filter_2d_ewa(struct tgsi_sampler *tgsi_sampler,
1747 const float s[TGSI_QUAD_SIZE],
1748 const float t[TGSI_QUAD_SIZE],
1749 const float p[TGSI_QUAD_SIZE],
1750 const float c0[TGSI_QUAD_SIZE],
1751 enum tgsi_sampler_control control,
1752 const float dudx, const float dvdx,
1753 const float dudy, const float dvdy,
1754 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1755 {
1756 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1757 const struct pipe_resource *texture = samp->view->texture;
1758
1759 unsigned level0 = samp->level > 0 ? samp->level : 0;
1760 float scaling = 1.0 / (1 << level0);
1761 int width = u_minify(texture->width0, level0);
1762 int height = u_minify(texture->height0, level0);
1763
1764 float ux = dudx * scaling;
1765 float vx = dvdx * scaling;
1766 float uy = dudy * scaling;
1767 float vy = dvdy * scaling;
1768
1769 /* compute ellipse coefficients to bound the region:
1770 * A*x*x + B*x*y + C*y*y = F.
1771 */
1772 float A = vx*vx+vy*vy+1;
1773 float B = -2*(ux*vx+uy*vy);
1774 float C = ux*ux+uy*uy+1;
1775 float F = A*C-B*B/4.0;
1776
1777 /* check if it is an ellipse */
1778 /* ASSERT(F > 0.0); */
1779
1780 /* Compute the ellipse's (u,v) bounding box in texture space */
1781 float d = -B*B+4.0*C*A;
1782 float box_u = 2.0 / d * sqrt(d*C*F); /* box_u -> half of bbox with */
1783 float box_v = 2.0 / d * sqrt(A*d*F); /* box_v -> half of bbox height */
1784
1785 float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
1786 float s_buffer[TGSI_QUAD_SIZE];
1787 float t_buffer[TGSI_QUAD_SIZE];
1788 float weight_buffer[TGSI_QUAD_SIZE];
1789 unsigned buffer_next;
1790 int j;
1791 float den;// = 0.0F;
1792 float ddq;
1793 float U;// = u0 - tex_u;
1794 int v;
1795
1796 /* Scale ellipse formula to directly index the Filter Lookup Table.
1797 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1798 */
1799 double formScale = (double) (WEIGHT_LUT_SIZE - 1) / F;
1800 A *= formScale;
1801 B *= formScale;
1802 C *= formScale;
1803 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1804
1805 /* For each quad, the du and dx values are the same and so the ellipse is
1806 * also the same. Note that texel/image access can only be performed using
1807 * a quad, i.e. it is not possible to get the pixel value for a single
1808 * tex coord. In order to have a better performance, the access is buffered
1809 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is full,
1810 * then the pixel values are read from the image.
1811 */
1812 ddq = 2 * A;
1813
1814 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
1815 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1816 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1817 * value, q, is less than F, we're inside the ellipse
1818 */
1819 float tex_u = -0.5F + s[j] * texture->width0 * scaling;
1820 float tex_v = -0.5F + t[j] * texture->height0 * scaling;
1821
1822 int u0 = (int) floorf(tex_u - box_u);
1823 int u1 = (int) ceilf(tex_u + box_u);
1824 int v0 = (int) floorf(tex_v - box_v);
1825 int v1 = (int) ceilf(tex_v + box_v);
1826
1827 float num[4] = {0.0F, 0.0F, 0.0F, 0.0F};
1828 buffer_next = 0;
1829 den = 0;
1830 U = u0 - tex_u;
1831 for (v = v0; v <= v1; ++v) {
1832 float V = v - tex_v;
1833 float dq = A * (2 * U + 1) + B * V;
1834 float q = (C * V + B * U) * V + A * U * U;
1835
1836 int u;
1837 for (u = u0; u <= u1; ++u) {
1838 /* Note that the ellipse has been pre-scaled so F = WEIGHT_LUT_SIZE - 1 */
1839 if (q < WEIGHT_LUT_SIZE) {
1840 /* as a LUT is used, q must never be negative;
1841 * should not happen, though
1842 */
1843 const int qClamped = q >= 0.0F ? q : 0;
1844 float weight = weightLut[qClamped];
1845
1846 weight_buffer[buffer_next] = weight;
1847 s_buffer[buffer_next] = u / ((float) width);
1848 t_buffer[buffer_next] = v / ((float) height);
1849
1850 buffer_next++;
1851 if (buffer_next == TGSI_QUAD_SIZE) {
1852 /* 4 texel coords are in the buffer -> read it now */
1853 unsigned jj;
1854 /* it is assumed that samp->min_img_filter is set to
1855 * img_filter_2d_nearest or one of the
1856 * accelerated img_filter_2d_nearest_XXX functions.
1857 */
1858 samp->min_img_filter(tgsi_sampler, s_buffer, t_buffer, p, NULL,
1859 tgsi_sampler_lod_bias, rgba_temp);
1860 for (jj = 0; jj < buffer_next; jj++) {
1861 num[0] += weight_buffer[jj] * rgba_temp[0][jj];
1862 num[1] += weight_buffer[jj] * rgba_temp[1][jj];
1863 num[2] += weight_buffer[jj] * rgba_temp[2][jj];
1864 num[3] += weight_buffer[jj] * rgba_temp[3][jj];
1865 }
1866
1867 buffer_next = 0;
1868 }
1869
1870 den += weight;
1871 }
1872 q += dq;
1873 dq += ddq;
1874 }
1875 }
1876
1877 /* if the tex coord buffer contains unread values, we will read them now.
1878 * Note that in most cases we have to read more pixel values than required,
1879 * however, as the img_filter_2d_nearest function(s) does not have a count
1880 * parameter, we need to read the whole quad and ignore the unused values
1881 */
1882 if (buffer_next > 0) {
1883 unsigned jj;
1884 /* it is assumed that samp->min_img_filter is set to
1885 * img_filter_2d_nearest or one of the
1886 * accelerated img_filter_2d_nearest_XXX functions.
1887 */
1888 samp->min_img_filter(tgsi_sampler, s_buffer, t_buffer, p, NULL,
1889 tgsi_sampler_lod_bias, rgba_temp);
1890 for (jj = 0; jj < buffer_next; jj++) {
1891 num[0] += weight_buffer[jj] * rgba_temp[0][jj];
1892 num[1] += weight_buffer[jj] * rgba_temp[1][jj];
1893 num[2] += weight_buffer[jj] * rgba_temp[2][jj];
1894 num[3] += weight_buffer[jj] * rgba_temp[3][jj];
1895 }
1896 }
1897
1898 if (den <= 0.0F) {
1899 /* Reaching this place would mean
1900 * that no pixels intersected the ellipse.
1901 * This should never happen because
1902 * the filter we use always
1903 * intersects at least one pixel.
1904 */
1905
1906 /*rgba[0]=0;
1907 rgba[1]=0;
1908 rgba[2]=0;
1909 rgba[3]=0;*/
1910 /* not enough pixels in resampling, resort to direct interpolation */
1911 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba_temp);
1912 den = 1;
1913 num[0] = rgba_temp[0][j];
1914 num[1] = rgba_temp[1][j];
1915 num[2] = rgba_temp[2][j];
1916 num[3] = rgba_temp[3][j];
1917 }
1918
1919 rgba[0][j] = num[0] / den;
1920 rgba[1][j] = num[1] / den;
1921 rgba[2][j] = num[2] / den;
1922 rgba[3][j] = num[3] / den;
1923 }
1924 }
1925
1926
1927 /**
1928 * Sample 2D texture using an anisotropic filter.
1929 */
1930 static void
1931 mip_filter_linear_aniso(struct tgsi_sampler *tgsi_sampler,
1932 const float s[TGSI_QUAD_SIZE],
1933 const float t[TGSI_QUAD_SIZE],
1934 const float p[TGSI_QUAD_SIZE],
1935 const float c0[TGSI_QUAD_SIZE],
1936 enum tgsi_sampler_control control,
1937 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
1938 {
1939 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1940 const struct pipe_resource *texture = samp->view->texture;
1941 int level0;
1942 float lambda;
1943 float lod[TGSI_QUAD_SIZE];
1944
1945 float s_to_u = u_minify(texture->width0, samp->view->u.tex.first_level);
1946 float t_to_v = u_minify(texture->height0, samp->view->u.tex.first_level);
1947 float dudx = (s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]) * s_to_u;
1948 float dudy = (s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]) * s_to_u;
1949 float dvdx = (t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]) * t_to_v;
1950 float dvdy = (t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]) * t_to_v;
1951
1952 if (control == tgsi_sampler_lod_bias) {
1953 /* note: instead of working with Px and Py, we will use the
1954 * squared length instead, to avoid sqrt.
1955 */
1956 float Px2 = dudx * dudx + dvdx * dvdx;
1957 float Py2 = dudy * dudy + dvdy * dvdy;
1958
1959 float Pmax2;
1960 float Pmin2;
1961 float e;
1962 const float maxEccentricity = samp->sampler->max_anisotropy * samp->sampler->max_anisotropy;
1963
1964 if (Px2 < Py2) {
1965 Pmax2 = Py2;
1966 Pmin2 = Px2;
1967 }
1968 else {
1969 Pmax2 = Px2;
1970 Pmin2 = Py2;
1971 }
1972
1973 /* if the eccentricity of the ellipse is too big, scale up the shorter
1974 * of the two vectors to limit the maximum amount of work per pixel
1975 */
1976 e = Pmax2 / Pmin2;
1977 if (e > maxEccentricity) {
1978 /* float s=e / maxEccentricity;
1979 minor[0] *= s;
1980 minor[1] *= s;
1981 Pmin2 *= s; */
1982 Pmin2 = Pmax2 / maxEccentricity;
1983 }
1984
1985 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1986 * this since 0.5*log(x) = log(sqrt(x))
1987 */
1988 lambda = 0.5F * util_fast_log2(Pmin2) + samp->sampler->lod_bias;
1989 compute_lod(samp->sampler, lambda, c0, lod);
1990 }
1991 else {
1992 assert(control == tgsi_sampler_lod_explicit);
1993
1994 memcpy(lod, c0, sizeof(lod));
1995 }
1996
1997 /* XXX: Take into account all lod values.
1998 */
1999 lambda = lod[0];
2000 level0 = samp->view->u.tex.first_level + (int)lambda;
2001
2002 /* If the ellipse covers the whole image, we can
2003 * simply return the average of the whole image.
2004 */
2005 if (level0 >= (int) texture->last_level) {
2006 samp->level = texture->last_level;
2007 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
2008 }
2009 else {
2010 /* don't bother interpolating between multiple LODs; it doesn't
2011 * seem to be worth the extra running time.
2012 */
2013 samp->level = level0;
2014 img_filter_2d_ewa(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias,
2015 dudx, dvdx, dudy, dvdy, rgba);
2016 }
2017
2018 if (DEBUG_TEX) {
2019 print_sample(__FUNCTION__, rgba);
2020 }
2021 }
2022
2023
2024
2025 /**
2026 * Specialized version of mip_filter_linear with hard-wired calls to
2027 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2028 */
2029 static void
2030 mip_filter_linear_2d_linear_repeat_POT(
2031 struct tgsi_sampler *tgsi_sampler,
2032 const float s[TGSI_QUAD_SIZE],
2033 const float t[TGSI_QUAD_SIZE],
2034 const float p[TGSI_QUAD_SIZE],
2035 const float c0[TGSI_QUAD_SIZE],
2036 enum tgsi_sampler_control control,
2037 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2038 {
2039 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2040 const struct pipe_resource *texture = samp->view->texture;
2041 int level0;
2042 float lambda;
2043 float lod[TGSI_QUAD_SIZE];
2044
2045 if (control == tgsi_sampler_lod_bias) {
2046 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
2047 compute_lod(samp->sampler, lambda, c0, lod);
2048 } else {
2049 assert(control == tgsi_sampler_lod_explicit);
2050
2051 memcpy(lod, c0, sizeof(lod));
2052 }
2053
2054 /* XXX: Take into account all lod values.
2055 */
2056 lambda = lod[0];
2057 level0 = samp->view->u.tex.first_level + (int)lambda;
2058
2059 /* Catches both negative and large values of level0:
2060 */
2061 if ((unsigned)level0 >= texture->last_level) {
2062 if (level0 < 0)
2063 samp->level = samp->view->u.tex.first_level;
2064 else
2065 samp->level = texture->last_level;
2066
2067 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
2068 }
2069 else {
2070 float levelBlend = frac(lambda);
2071 float rgba0[4][4];
2072 float rgba1[4][4];
2073 int c,j;
2074
2075 samp->level = level0;
2076 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
2077
2078 samp->level = level0+1;
2079 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
2080
2081 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2082 for (c = 0; c < 4; c++) {
2083 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
2084 }
2085 }
2086 }
2087
2088 if (DEBUG_TEX) {
2089 print_sample(__FUNCTION__, rgba);
2090 }
2091 }
2092
2093
2094
2095 /**
2096 * Do shadow/depth comparisons.
2097 */
2098 static void
2099 sample_compare(struct tgsi_sampler *tgsi_sampler,
2100 const float s[TGSI_QUAD_SIZE],
2101 const float t[TGSI_QUAD_SIZE],
2102 const float p[TGSI_QUAD_SIZE],
2103 const float c0[TGSI_QUAD_SIZE],
2104 enum tgsi_sampler_control control,
2105 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2106 {
2107 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2108 const struct pipe_sampler_state *sampler = samp->sampler;
2109 int j, k0, k1, k2, k3;
2110 float val;
2111 float pc0, pc1, pc2, pc3;
2112
2113 samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
2114
2115 /**
2116 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2117 * for 2D Array texture we need to use the 'c0' (aka Q).
2118 * When we sampled the depth texture, the depth value was put into all
2119 * RGBA channels. We look at the red channel here.
2120 */
2121
2122 if (samp->view->texture->target == PIPE_TEXTURE_2D_ARRAY ||
2123 samp->view->texture->target == PIPE_TEXTURE_CUBE) {
2124 pc0 = CLAMP(c0[0], 0.0F, 1.0F);
2125 pc1 = CLAMP(c0[1], 0.0F, 1.0F);
2126 pc2 = CLAMP(c0[2], 0.0F, 1.0F);
2127 pc3 = CLAMP(c0[3], 0.0F, 1.0F);
2128 } else {
2129 pc0 = CLAMP(p[0], 0.0F, 1.0F);
2130 pc1 = CLAMP(p[1], 0.0F, 1.0F);
2131 pc2 = CLAMP(p[2], 0.0F, 1.0F);
2132 pc3 = CLAMP(p[3], 0.0F, 1.0F);
2133 }
2134 /* compare four texcoords vs. four texture samples */
2135 switch (sampler->compare_func) {
2136 case PIPE_FUNC_LESS:
2137 k0 = pc0 < rgba[0][0];
2138 k1 = pc1 < rgba[0][1];
2139 k2 = pc2 < rgba[0][2];
2140 k3 = pc3 < rgba[0][3];
2141 break;
2142 case PIPE_FUNC_LEQUAL:
2143 k0 = pc0 <= rgba[0][0];
2144 k1 = pc1 <= rgba[0][1];
2145 k2 = pc2 <= rgba[0][2];
2146 k3 = pc3 <= rgba[0][3];
2147 break;
2148 case PIPE_FUNC_GREATER:
2149 k0 = pc0 > rgba[0][0];
2150 k1 = pc1 > rgba[0][1];
2151 k2 = pc2 > rgba[0][2];
2152 k3 = pc3 > rgba[0][3];
2153 break;
2154 case PIPE_FUNC_GEQUAL:
2155 k0 = pc0 >= rgba[0][0];
2156 k1 = pc1 >= rgba[0][1];
2157 k2 = pc2 >= rgba[0][2];
2158 k3 = pc3 >= rgba[0][3];
2159 break;
2160 case PIPE_FUNC_EQUAL:
2161 k0 = pc0 == rgba[0][0];
2162 k1 = pc1 == rgba[0][1];
2163 k2 = pc2 == rgba[0][2];
2164 k3 = pc3 == rgba[0][3];
2165 break;
2166 case PIPE_FUNC_NOTEQUAL:
2167 k0 = pc0 != rgba[0][0];
2168 k1 = pc1 != rgba[0][1];
2169 k2 = pc2 != rgba[0][2];
2170 k3 = pc3 != rgba[0][3];
2171 break;
2172 case PIPE_FUNC_ALWAYS:
2173 k0 = k1 = k2 = k3 = 1;
2174 break;
2175 case PIPE_FUNC_NEVER:
2176 k0 = k1 = k2 = k3 = 0;
2177 break;
2178 default:
2179 k0 = k1 = k2 = k3 = 0;
2180 assert(0);
2181 break;
2182 }
2183
2184 if (sampler->mag_img_filter == PIPE_TEX_FILTER_LINEAR) {
2185 /* convert four pass/fail values to an intensity in [0,1] */
2186 val = 0.25F * (k0 + k1 + k2 + k3);
2187
2188 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2189 for (j = 0; j < 4; j++) {
2190 rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
2191 rgba[3][j] = 1.0F;
2192 }
2193 } else {
2194 for (j = 0; j < 4; j++) {
2195 rgba[0][j] = k0;
2196 rgba[1][j] = k1;
2197 rgba[2][j] = k2;
2198 rgba[3][j] = 1.0F;
2199 }
2200 }
2201 }
2202
2203
2204 /**
2205 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
2206 * Put face info into the sampler faces[] array.
2207 */
2208 static void
2209 sample_cube(struct tgsi_sampler *tgsi_sampler,
2210 const float s[TGSI_QUAD_SIZE],
2211 const float t[TGSI_QUAD_SIZE],
2212 const float p[TGSI_QUAD_SIZE],
2213 const float c0[TGSI_QUAD_SIZE],
2214 enum tgsi_sampler_control control,
2215 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2216 {
2217 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2218 unsigned j;
2219 float ssss[4], tttt[4];
2220
2221 /*
2222 major axis
2223 direction target sc tc ma
2224 ---------- ------------------------------- --- --- ---
2225 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2226 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2227 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2228 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2229 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2230 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2231 */
2232
2233 /* Choose the cube face and compute new s/t coords for the 2D face.
2234 *
2235 * Use the same cube face for all four pixels in the quad.
2236 *
2237 * This isn't ideal, but if we want to use a different cube face
2238 * per pixel in the quad, we'd have to also compute the per-face
2239 * LOD here too. That's because the four post-face-selection
2240 * texcoords are no longer related to each other (they're
2241 * per-face!) so we can't use subtraction to compute the partial
2242 * deriviates to compute the LOD. Doing so (near cube edges
2243 * anyway) gives us pretty much random values.
2244 */
2245 {
2246 /* use the average of the four pixel's texcoords to choose the face */
2247 const float rx = 0.25F * (s[0] + s[1] + s[2] + s[3]);
2248 const float ry = 0.25F * (t[0] + t[1] + t[2] + t[3]);
2249 const float rz = 0.25F * (p[0] + p[1] + p[2] + p[3]);
2250 const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
2251
2252 if (arx >= ary && arx >= arz) {
2253 float sign = (rx >= 0.0F) ? 1.0F : -1.0F;
2254 uint face = (rx >= 0.0F) ? PIPE_TEX_FACE_POS_X : PIPE_TEX_FACE_NEG_X;
2255 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2256 const float ima = -0.5F / fabsf(s[j]);
2257 ssss[j] = sign * p[j] * ima + 0.5F;
2258 tttt[j] = t[j] * ima + 0.5F;
2259 samp->faces[j] = face;
2260 }
2261 }
2262 else if (ary >= arx && ary >= arz) {
2263 float sign = (ry >= 0.0F) ? 1.0F : -1.0F;
2264 uint face = (ry >= 0.0F) ? PIPE_TEX_FACE_POS_Y : PIPE_TEX_FACE_NEG_Y;
2265 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2266 const float ima = -0.5F / fabsf(t[j]);
2267 ssss[j] = -s[j] * ima + 0.5F;
2268 tttt[j] = sign * -p[j] * ima + 0.5F;
2269 samp->faces[j] = face;
2270 }
2271 }
2272 else {
2273 float sign = (rz >= 0.0F) ? 1.0F : -1.0F;
2274 uint face = (rz >= 0.0F) ? PIPE_TEX_FACE_POS_Z : PIPE_TEX_FACE_NEG_Z;
2275 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2276 const float ima = -0.5F / fabsf(p[j]);
2277 ssss[j] = sign * -s[j] * ima + 0.5F;
2278 tttt[j] = t[j] * ima + 0.5F;
2279 samp->faces[j] = face;
2280 }
2281 }
2282 }
2283
2284 /* In our little pipeline, the compare stage is next. If compare
2285 * is not active, this will point somewhere deeper into the
2286 * pipeline, eg. to mip_filter or even img_filter.
2287 */
2288 samp->compare(tgsi_sampler, ssss, tttt, NULL, c0, control, rgba);
2289 }
2290
2291 static void do_swizzling(const struct sp_sampler_variant *samp,
2292 float in[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE],
2293 float out[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2294 {
2295 int j;
2296 const unsigned swizzle_r = samp->key.bits.swizzle_r;
2297 const unsigned swizzle_g = samp->key.bits.swizzle_g;
2298 const unsigned swizzle_b = samp->key.bits.swizzle_b;
2299 const unsigned swizzle_a = samp->key.bits.swizzle_a;
2300
2301 switch (swizzle_r) {
2302 case PIPE_SWIZZLE_ZERO:
2303 for (j = 0; j < 4; j++)
2304 out[0][j] = 0.0f;
2305 break;
2306 case PIPE_SWIZZLE_ONE:
2307 for (j = 0; j < 4; j++)
2308 out[0][j] = 1.0f;
2309 break;
2310 default:
2311 assert(swizzle_r < 4);
2312 for (j = 0; j < 4; j++)
2313 out[0][j] = in[swizzle_r][j];
2314 }
2315
2316 switch (swizzle_g) {
2317 case PIPE_SWIZZLE_ZERO:
2318 for (j = 0; j < 4; j++)
2319 out[1][j] = 0.0f;
2320 break;
2321 case PIPE_SWIZZLE_ONE:
2322 for (j = 0; j < 4; j++)
2323 out[1][j] = 1.0f;
2324 break;
2325 default:
2326 assert(swizzle_g < 4);
2327 for (j = 0; j < 4; j++)
2328 out[1][j] = in[swizzle_g][j];
2329 }
2330
2331 switch (swizzle_b) {
2332 case PIPE_SWIZZLE_ZERO:
2333 for (j = 0; j < 4; j++)
2334 out[2][j] = 0.0f;
2335 break;
2336 case PIPE_SWIZZLE_ONE:
2337 for (j = 0; j < 4; j++)
2338 out[2][j] = 1.0f;
2339 break;
2340 default:
2341 assert(swizzle_b < 4);
2342 for (j = 0; j < 4; j++)
2343 out[2][j] = in[swizzle_b][j];
2344 }
2345
2346 switch (swizzle_a) {
2347 case PIPE_SWIZZLE_ZERO:
2348 for (j = 0; j < 4; j++)
2349 out[3][j] = 0.0f;
2350 break;
2351 case PIPE_SWIZZLE_ONE:
2352 for (j = 0; j < 4; j++)
2353 out[3][j] = 1.0f;
2354 break;
2355 default:
2356 assert(swizzle_a < 4);
2357 for (j = 0; j < 4; j++)
2358 out[3][j] = in[swizzle_a][j];
2359 }
2360 }
2361
2362 static void
2363 sample_swizzle(struct tgsi_sampler *tgsi_sampler,
2364 const float s[TGSI_QUAD_SIZE],
2365 const float t[TGSI_QUAD_SIZE],
2366 const float p[TGSI_QUAD_SIZE],
2367 const float c0[TGSI_QUAD_SIZE],
2368 enum tgsi_sampler_control control,
2369 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2370 {
2371 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2372 float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
2373
2374 samp->sample_target(tgsi_sampler, s, t, p, c0, control, rgba_temp);
2375
2376 do_swizzling(samp, rgba_temp, rgba);
2377 }
2378
2379
2380 static wrap_nearest_func
2381 get_nearest_unorm_wrap(unsigned mode)
2382 {
2383 switch (mode) {
2384 case PIPE_TEX_WRAP_CLAMP:
2385 return wrap_nearest_unorm_clamp;
2386 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2387 return wrap_nearest_unorm_clamp_to_edge;
2388 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
2389 return wrap_nearest_unorm_clamp_to_border;
2390 default:
2391 assert(0);
2392 return wrap_nearest_unorm_clamp;
2393 }
2394 }
2395
2396
2397 static wrap_nearest_func
2398 get_nearest_wrap(unsigned mode)
2399 {
2400 switch (mode) {
2401 case PIPE_TEX_WRAP_REPEAT:
2402 return wrap_nearest_repeat;
2403 case PIPE_TEX_WRAP_CLAMP:
2404 return wrap_nearest_clamp;
2405 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2406 return wrap_nearest_clamp_to_edge;
2407 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
2408 return wrap_nearest_clamp_to_border;
2409 case PIPE_TEX_WRAP_MIRROR_REPEAT:
2410 return wrap_nearest_mirror_repeat;
2411 case PIPE_TEX_WRAP_MIRROR_CLAMP:
2412 return wrap_nearest_mirror_clamp;
2413 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
2414 return wrap_nearest_mirror_clamp_to_edge;
2415 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
2416 return wrap_nearest_mirror_clamp_to_border;
2417 default:
2418 assert(0);
2419 return wrap_nearest_repeat;
2420 }
2421 }
2422
2423
2424 static wrap_linear_func
2425 get_linear_unorm_wrap(unsigned mode)
2426 {
2427 switch (mode) {
2428 case PIPE_TEX_WRAP_CLAMP:
2429 return wrap_linear_unorm_clamp;
2430 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2431 return wrap_linear_unorm_clamp_to_edge;
2432 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
2433 return wrap_linear_unorm_clamp_to_border;
2434 default:
2435 assert(0);
2436 return wrap_linear_unorm_clamp;
2437 }
2438 }
2439
2440
2441 static wrap_linear_func
2442 get_linear_wrap(unsigned mode)
2443 {
2444 switch (mode) {
2445 case PIPE_TEX_WRAP_REPEAT:
2446 return wrap_linear_repeat;
2447 case PIPE_TEX_WRAP_CLAMP:
2448 return wrap_linear_clamp;
2449 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2450 return wrap_linear_clamp_to_edge;
2451 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
2452 return wrap_linear_clamp_to_border;
2453 case PIPE_TEX_WRAP_MIRROR_REPEAT:
2454 return wrap_linear_mirror_repeat;
2455 case PIPE_TEX_WRAP_MIRROR_CLAMP:
2456 return wrap_linear_mirror_clamp;
2457 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
2458 return wrap_linear_mirror_clamp_to_edge;
2459 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
2460 return wrap_linear_mirror_clamp_to_border;
2461 default:
2462 assert(0);
2463 return wrap_linear_repeat;
2464 }
2465 }
2466
2467
2468 static compute_lambda_func
2469 get_lambda_func(const union sp_sampler_key key)
2470 {
2471 if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
2472 return compute_lambda_vert;
2473
2474 switch (key.bits.target) {
2475 case PIPE_TEXTURE_1D:
2476 case PIPE_TEXTURE_1D_ARRAY:
2477 return compute_lambda_1d;
2478 case PIPE_TEXTURE_2D:
2479 case PIPE_TEXTURE_2D_ARRAY:
2480 case PIPE_TEXTURE_RECT:
2481 case PIPE_TEXTURE_CUBE:
2482 return compute_lambda_2d;
2483 case PIPE_TEXTURE_3D:
2484 return compute_lambda_3d;
2485 default:
2486 assert(0);
2487 return compute_lambda_1d;
2488 }
2489 }
2490
2491
2492 static filter_func
2493 get_img_filter(const union sp_sampler_key key,
2494 unsigned filter,
2495 const struct pipe_sampler_state *sampler)
2496 {
2497 switch (key.bits.target) {
2498 case PIPE_TEXTURE_1D:
2499 if (filter == PIPE_TEX_FILTER_NEAREST)
2500 return img_filter_1d_nearest;
2501 else
2502 return img_filter_1d_linear;
2503 break;
2504 case PIPE_TEXTURE_1D_ARRAY:
2505 if (filter == PIPE_TEX_FILTER_NEAREST)
2506 return img_filter_1d_array_nearest;
2507 else
2508 return img_filter_1d_array_linear;
2509 break;
2510 case PIPE_TEXTURE_2D:
2511 case PIPE_TEXTURE_RECT:
2512 /* Try for fast path:
2513 */
2514 if (key.bits.is_pot &&
2515 sampler->wrap_s == sampler->wrap_t &&
2516 sampler->normalized_coords)
2517 {
2518 switch (sampler->wrap_s) {
2519 case PIPE_TEX_WRAP_REPEAT:
2520 switch (filter) {
2521 case PIPE_TEX_FILTER_NEAREST:
2522 return img_filter_2d_nearest_repeat_POT;
2523 case PIPE_TEX_FILTER_LINEAR:
2524 return img_filter_2d_linear_repeat_POT;
2525 default:
2526 break;
2527 }
2528 break;
2529 case PIPE_TEX_WRAP_CLAMP:
2530 switch (filter) {
2531 case PIPE_TEX_FILTER_NEAREST:
2532 return img_filter_2d_nearest_clamp_POT;
2533 default:
2534 break;
2535 }
2536 }
2537 }
2538 /* Otherwise use default versions:
2539 */
2540 if (filter == PIPE_TEX_FILTER_NEAREST)
2541 return img_filter_2d_nearest;
2542 else
2543 return img_filter_2d_linear;
2544 break;
2545 case PIPE_TEXTURE_2D_ARRAY:
2546 if (filter == PIPE_TEX_FILTER_NEAREST)
2547 return img_filter_2d_array_nearest;
2548 else
2549 return img_filter_2d_array_linear;
2550 break;
2551 case PIPE_TEXTURE_CUBE:
2552 if (filter == PIPE_TEX_FILTER_NEAREST)
2553 return img_filter_cube_nearest;
2554 else
2555 return img_filter_cube_linear;
2556 break;
2557 case PIPE_TEXTURE_3D:
2558 if (filter == PIPE_TEX_FILTER_NEAREST)
2559 return img_filter_3d_nearest;
2560 else
2561 return img_filter_3d_linear;
2562 break;
2563 default:
2564 assert(0);
2565 return img_filter_1d_nearest;
2566 }
2567 }
2568
2569
2570 /**
2571 * Bind the given texture object and texture cache to the sampler variant.
2572 */
2573 void
2574 sp_sampler_variant_bind_view( struct sp_sampler_variant *samp,
2575 struct softpipe_tex_tile_cache *tex_cache,
2576 const struct pipe_sampler_view *view )
2577 {
2578 const struct pipe_resource *texture = view->texture;
2579
2580 samp->view = view;
2581 samp->cache = tex_cache;
2582 samp->xpot = util_logbase2( texture->width0 );
2583 samp->ypot = util_logbase2( texture->height0 );
2584 samp->level = view->u.tex.first_level;
2585 }
2586
2587
2588 void
2589 sp_sampler_variant_destroy( struct sp_sampler_variant *samp )
2590 {
2591 FREE(samp);
2592 }
2593
2594 static void
2595 sample_get_dims(struct tgsi_sampler *tgsi_sampler, int level,
2596 int dims[4])
2597 {
2598 struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2599 const struct pipe_sampler_view *view = samp->view;
2600 const struct pipe_resource *texture = view->texture;
2601
2602 /* undefined according to EXT_gpu_program */
2603 level += view->u.tex.first_level;
2604 if (level > view->u.tex.last_level)
2605 return;
2606
2607 dims[0] = u_minify(texture->width0, level);
2608
2609 switch(texture->target) {
2610 case PIPE_TEXTURE_1D_ARRAY:
2611 dims[1] = texture->array_size;
2612 /* fallthrough */
2613 case PIPE_TEXTURE_1D:
2614 case PIPE_BUFFER:
2615 return;
2616 case PIPE_TEXTURE_2D_ARRAY:
2617 dims[2] = texture->array_size;
2618 /* fallthrough */
2619 case PIPE_TEXTURE_2D:
2620 case PIPE_TEXTURE_CUBE:
2621 case PIPE_TEXTURE_RECT:
2622 dims[1] = u_minify(texture->height0, level);
2623 return;
2624 case PIPE_TEXTURE_3D:
2625 dims[1] = u_minify(texture->height0, level);
2626 dims[2] = u_minify(texture->depth0, level);
2627 return;
2628 default:
2629 assert(!"unexpected texture target in sample_get_dims()");
2630 return;
2631 }
2632 }
2633
2634 /**
2635 * This function is only used for getting unfiltered texels via the
2636 * TXF opcode. The GL spec says that out-of-bounds texel fetches
2637 * produce undefined results. Instead of crashing, lets just clamp
2638 * coords to the texture image size.
2639 */
2640 static void
2641 sample_get_texels(struct tgsi_sampler *tgsi_sampler,
2642 const int v_i[TGSI_QUAD_SIZE],
2643 const int v_j[TGSI_QUAD_SIZE],
2644 const int v_k[TGSI_QUAD_SIZE],
2645 const int lod[TGSI_QUAD_SIZE],
2646 const int8_t offset[3],
2647 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])
2648 {
2649 const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
2650 union tex_tile_address addr;
2651 const struct pipe_resource *texture = samp->view->texture;
2652 int j, c;
2653 const float *tx;
2654 bool need_swizzle = (samp->key.bits.swizzle_r != PIPE_SWIZZLE_RED ||
2655 samp->key.bits.swizzle_g != PIPE_SWIZZLE_GREEN ||
2656 samp->key.bits.swizzle_b != PIPE_SWIZZLE_BLUE ||
2657 samp->key.bits.swizzle_a != PIPE_SWIZZLE_ALPHA);
2658 int width, height, depth, layers;
2659
2660 addr.value = 0;
2661 /* TODO write a better test for LOD */
2662 addr.bits.level = lod[0];
2663
2664 width = u_minify(texture->width0, addr.bits.level);
2665 height = u_minify(texture->height0, addr.bits.level);
2666 depth = u_minify(texture->depth0, addr.bits.level);
2667 layers = texture->array_size;
2668
2669 switch(texture->target) {
2670 case PIPE_TEXTURE_1D:
2671 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2672 int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
2673 tx = get_texel_2d(samp, addr, x, 0);
2674 for (c = 0; c < 4; c++) {
2675 rgba[c][j] = tx[c];
2676 }
2677 }
2678 break;
2679 case PIPE_TEXTURE_1D_ARRAY:
2680 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2681 int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
2682 int y = CLAMP(v_j[j] + offset[1], 0, layers - 1);
2683 tx = get_texel_1d_array(samp, addr, x, y);
2684 for (c = 0; c < 4; c++) {
2685 rgba[c][j] = tx[c];
2686 }
2687 }
2688 break;
2689 case PIPE_TEXTURE_2D:
2690 case PIPE_TEXTURE_RECT:
2691 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2692 int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
2693 int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
2694 tx = get_texel_2d(samp, addr, x, y);
2695 for (c = 0; c < 4; c++) {
2696 rgba[c][j] = tx[c];
2697 }
2698 }
2699 break;
2700 case PIPE_TEXTURE_2D_ARRAY:
2701 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2702 int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
2703 int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
2704 int layer = CLAMP(v_k[j] + offset[2], 0, layers - 1);
2705 tx = get_texel_2d_array(samp, addr, x, y, layer);
2706 for (c = 0; c < 4; c++) {
2707 rgba[c][j] = tx[c];
2708 }
2709 }
2710 break;
2711 case PIPE_TEXTURE_3D:
2712 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2713 int x = CLAMP(v_i[j] + offset[0], 0, width - 1);
2714 int y = CLAMP(v_j[j] + offset[1], 0, height - 1);
2715 int z = CLAMP(v_k[j] + offset[2], 0, depth - 1);
2716
2717 tx = get_texel_3d(samp, addr, x, y, z);
2718 for (c = 0; c < 4; c++) {
2719 rgba[c][j] = tx[c];
2720 }
2721 }
2722 break;
2723 case PIPE_TEXTURE_CUBE: /* TXF can't work on CUBE according to spec */
2724 default:
2725 assert(!"Unknown or CUBE texture type in TXF processing\n");
2726 break;
2727 }
2728
2729 if (need_swizzle) {
2730 float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
2731 memcpy(rgba_temp, rgba, sizeof(rgba_temp));
2732 do_swizzling(samp, rgba_temp, rgba);
2733 }
2734 }
2735 /**
2736 * Create a sampler variant for a given set of non-orthogonal state.
2737 */
2738 struct sp_sampler_variant *
2739 sp_create_sampler_variant( const struct pipe_sampler_state *sampler,
2740 const union sp_sampler_key key )
2741 {
2742 struct sp_sampler_variant *samp = CALLOC_STRUCT(sp_sampler_variant);
2743 if (!samp)
2744 return NULL;
2745
2746 samp->sampler = sampler;
2747 samp->key = key;
2748
2749 /* Note that (for instance) linear_texcoord_s and
2750 * nearest_texcoord_s may be active at the same time, if the
2751 * sampler min_img_filter differs from its mag_img_filter.
2752 */
2753 if (sampler->normalized_coords) {
2754 samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
2755 samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
2756 samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
2757
2758 samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
2759 samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
2760 samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
2761 }
2762 else {
2763 samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
2764 samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
2765 samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
2766
2767 samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
2768 samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
2769 samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
2770 }
2771
2772 samp->compute_lambda = get_lambda_func( key );
2773
2774 samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
2775 samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
2776
2777 switch (sampler->min_mip_filter) {
2778 case PIPE_TEX_MIPFILTER_NONE:
2779 if (sampler->min_img_filter == sampler->mag_img_filter)
2780 samp->mip_filter = samp->min_img_filter;
2781 else
2782 samp->mip_filter = mip_filter_none;
2783 break;
2784
2785 case PIPE_TEX_MIPFILTER_NEAREST:
2786 samp->mip_filter = mip_filter_nearest;
2787 break;
2788
2789 case PIPE_TEX_MIPFILTER_LINEAR:
2790 if (key.bits.is_pot &&
2791 sampler->min_img_filter == sampler->mag_img_filter &&
2792 sampler->normalized_coords &&
2793 sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
2794 sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
2795 sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
2796 samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
2797 }
2798 else {
2799 samp->mip_filter = mip_filter_linear;
2800 }
2801
2802 /* Anisotropic filtering extension. */
2803 if (sampler->max_anisotropy > 1) {
2804 samp->mip_filter = mip_filter_linear_aniso;
2805
2806 /* Override min_img_filter:
2807 * min_img_filter needs to be set to NEAREST since we need to access
2808 * each texture pixel as it is and weight it later; using linear
2809 * filters will have incorrect results.
2810 * By setting the filter to NEAREST here, we can avoid calling the
2811 * generic img_filter_2d_nearest in the anisotropic filter function,
2812 * making it possible to use one of the accelerated implementations
2813 */
2814 samp->min_img_filter = get_img_filter(key, PIPE_TEX_FILTER_NEAREST, sampler);
2815
2816 /* on first access create the lookup table containing the filter weights. */
2817 if (!weightLut) {
2818 create_filter_table();
2819 }
2820 }
2821
2822 break;
2823 }
2824
2825 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
2826 samp->compare = sample_compare;
2827 }
2828 else {
2829 /* Skip compare operation by promoting the mip_filter function
2830 * pointer:
2831 */
2832 samp->compare = samp->mip_filter;
2833 }
2834
2835 if (key.bits.target == PIPE_TEXTURE_CUBE) {
2836 samp->sample_target = sample_cube;
2837 }
2838 else {
2839 samp->faces[0] = 0;
2840 samp->faces[1] = 0;
2841 samp->faces[2] = 0;
2842 samp->faces[3] = 0;
2843
2844 /* Skip cube face determination by promoting the compare
2845 * function pointer:
2846 */
2847 samp->sample_target = samp->compare;
2848 }
2849
2850 if (key.bits.swizzle_r != PIPE_SWIZZLE_RED ||
2851 key.bits.swizzle_g != PIPE_SWIZZLE_GREEN ||
2852 key.bits.swizzle_b != PIPE_SWIZZLE_BLUE ||
2853 key.bits.swizzle_a != PIPE_SWIZZLE_ALPHA) {
2854 samp->base.get_samples = sample_swizzle;
2855 }
2856 else {
2857 samp->base.get_samples = samp->sample_target;
2858 }
2859
2860 samp->base.get_dims = sample_get_dims;
2861 samp->base.get_texel = sample_get_texels;
2862 return samp;
2863 }