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