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