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