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