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