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