Merge remote branch 'origin/master' into lp-binning
[mesa.git] / src / gallium / drivers / softpipe / sp_tex_sample.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008-2010 VMware, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * Texture sampling
31 *
32 * Authors:
33 * Brian Paul
34 * Keith Whitwell
35 */
36
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "sp_quad.h" /* only for #define QUAD_* tokens */
43 #include "sp_tex_sample.h"
44 #include "sp_tex_tile_cache.h"
45
46
47
48 /*
49 * Return fractional part of 'f'. Used for computing interpolation weights.
50 * Need to be careful with negative values.
51 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
52 * of improperly weighted linear-filtered textures.
53 * The tests/texwrap.c demo is a good test.
54 */
55 static INLINE float
56 frac(float f)
57 {
58 return f - util_ifloor(f);
59 }
60
61
62
63 /**
64 * Linear interpolation macro
65 */
66 static INLINE float
67 lerp(float a, float v0, float v1)
68 {
69 return v0 + a * (v1 - v0);
70 }
71
72
73 /**
74 * Do 2D/biliner interpolation of float values.
75 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
76 * a and b are the horizontal and vertical interpolants.
77 * It's important that this function is inlined when compiled with
78 * optimization! If we find that's not true on some systems, convert
79 * to a macro.
80 */
81 static INLINE float
82 lerp_2d(float a, float b,
83 float v00, float v10, float v01, float v11)
84 {
85 const float temp0 = lerp(a, v00, v10);
86 const float temp1 = lerp(a, v01, v11);
87 return lerp(b, temp0, temp1);
88 }
89
90
91 /**
92 * As above, but 3D interpolation of 8 values.
93 */
94 static INLINE float
95 lerp_3d(float a, float b, float c,
96 float v000, float v100, float v010, float v110,
97 float v001, float v101, float v011, float v111)
98 {
99 const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);
100 const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);
101 return lerp(c, temp0, temp1);
102 }
103
104
105
106 /**
107 * Compute coord % size for repeat wrap modes.
108 * Note that if coord is a signed integer, coord % size doesn't give
109 * the right value for coord < 0 (in terms of texture repeat). Just
110 * casting to unsigned fixes that.
111 */
112 static INLINE int
113 repeat(int coord, unsigned size)
114 {
115 return (int) ((unsigned) coord % size);
116 }
117
118
119 /**
120 * Apply texture coord wrapping mode and return integer texture indexes
121 * for a vector of four texcoords (S or T or P).
122 * \param wrapMode PIPE_TEX_WRAP_x
123 * \param s the incoming texcoords
124 * \param size the texture image size
125 * \param icoord returns the integer texcoords
126 * \return integer texture index
127 */
128 static void
129 wrap_nearest_repeat(const float s[4], unsigned size, int icoord[4])
130 {
131 uint ch;
132 /* s limited to [0,1) */
133 /* i limited to [0,size-1] */
134 for (ch = 0; ch < 4; ch++) {
135 int i = util_ifloor(s[ch] * size);
136 icoord[ch] = repeat(i, size);
137 }
138 }
139
140
141 static void
142 wrap_nearest_clamp(const float s[4], unsigned size, int icoord[4])
143 {
144 uint ch;
145 /* s limited to [0,1] */
146 /* i limited to [0,size-1] */
147 for (ch = 0; ch < 4; ch++) {
148 if (s[ch] <= 0.0F)
149 icoord[ch] = 0;
150 else if (s[ch] >= 1.0F)
151 icoord[ch] = size - 1;
152 else
153 icoord[ch] = util_ifloor(s[ch] * size);
154 }
155 }
156
157
158 static void
159 wrap_nearest_clamp_to_edge(const float s[4], unsigned size, int icoord[4])
160 {
161 uint ch;
162 /* s limited to [min,max] */
163 /* i limited to [0, size-1] */
164 const float min = 1.0F / (2.0F * size);
165 const float max = 1.0F - min;
166 for (ch = 0; ch < 4; ch++) {
167 if (s[ch] < min)
168 icoord[ch] = 0;
169 else if (s[ch] > max)
170 icoord[ch] = size - 1;
171 else
172 icoord[ch] = util_ifloor(s[ch] * size);
173 }
174 }
175
176
177 static void
178 wrap_nearest_clamp_to_border(const float s[4], unsigned size, int icoord[4])
179 {
180 uint ch;
181 /* s limited to [min,max] */
182 /* i limited to [-1, size] */
183 const float min = -1.0F / (2.0F * size);
184 const float max = 1.0F - min;
185 for (ch = 0; ch < 4; ch++) {
186 if (s[ch] <= min)
187 icoord[ch] = -1;
188 else if (s[ch] >= max)
189 icoord[ch] = size;
190 else
191 icoord[ch] = util_ifloor(s[ch] * size);
192 }
193 }
194
195
196 static void
197 wrap_nearest_mirror_repeat(const float s[4], unsigned size, int icoord[4])
198 {
199 uint ch;
200 const float min = 1.0F / (2.0F * size);
201 const float max = 1.0F - min;
202 for (ch = 0; ch < 4; ch++) {
203 const int flr = util_ifloor(s[ch]);
204 float u;
205 if (flr & 1)
206 u = 1.0F - (s[ch] - (float) flr);
207 else
208 u = s[ch] - (float) flr;
209 if (u < min)
210 icoord[ch] = 0;
211 else if (u > max)
212 icoord[ch] = size - 1;
213 else
214 icoord[ch] = util_ifloor(u * size);
215 }
216 }
217
218
219 static void
220 wrap_nearest_mirror_clamp(const float s[4], unsigned size, int icoord[4])
221 {
222 uint ch;
223 for (ch = 0; ch < 4; ch++) {
224 /* s limited to [0,1] */
225 /* i limited to [0,size-1] */
226 const float u = fabsf(s[ch]);
227 if (u <= 0.0F)
228 icoord[ch] = 0;
229 else if (u >= 1.0F)
230 icoord[ch] = size - 1;
231 else
232 icoord[ch] = util_ifloor(u * size);
233 }
234 }
235
236
237 static void
238 wrap_nearest_mirror_clamp_to_edge(const float s[4], unsigned size,
239 int icoord[4])
240 {
241 uint ch;
242 /* s limited to [min,max] */
243 /* i limited to [0, size-1] */
244 const float min = 1.0F / (2.0F * size);
245 const float max = 1.0F - min;
246 for (ch = 0; ch < 4; ch++) {
247 const float u = fabsf(s[ch]);
248 if (u < min)
249 icoord[ch] = 0;
250 else if (u > max)
251 icoord[ch] = size - 1;
252 else
253 icoord[ch] = util_ifloor(u * size);
254 }
255 }
256
257
258 static void
259 wrap_nearest_mirror_clamp_to_border(const float s[4], unsigned size,
260 int icoord[4])
261 {
262 uint ch;
263 /* s limited to [min,max] */
264 /* i limited to [0, size-1] */
265 const float min = -1.0F / (2.0F * size);
266 const float max = 1.0F - min;
267 for (ch = 0; ch < 4; ch++) {
268 const float u = fabsf(s[ch]);
269 if (u < min)
270 icoord[ch] = -1;
271 else if (u > max)
272 icoord[ch] = size;
273 else
274 icoord[ch] = util_ifloor(u * size);
275 }
276 }
277
278
279 /**
280 * Used to compute texel locations for linear sampling for four texcoords.
281 * \param wrapMode PIPE_TEX_WRAP_x
282 * \param s the texcoords
283 * \param size the texture image size
284 * \param icoord0 returns first texture indexes
285 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
286 * \param w returns blend factor/weight between texture indexes
287 * \param icoord returns the computed integer texture coords
288 */
289 static void
290 wrap_linear_repeat(const float s[4], unsigned size,
291 int icoord0[4], int icoord1[4], float w[4])
292 {
293 uint ch;
294 for (ch = 0; ch < 4; ch++) {
295 float u = s[ch] * size - 0.5F;
296 icoord0[ch] = repeat(util_ifloor(u), size);
297 icoord1[ch] = repeat(icoord0[ch] + 1, size);
298 w[ch] = frac(u);
299 }
300 }
301
302
303 static void
304 wrap_linear_clamp(const float s[4], unsigned size,
305 int icoord0[4], int icoord1[4], float w[4])
306 {
307 uint ch;
308 for (ch = 0; ch < 4; ch++) {
309 float u = CLAMP(s[ch], 0.0F, 1.0F);
310 u = u * size - 0.5f;
311 icoord0[ch] = util_ifloor(u);
312 icoord1[ch] = icoord0[ch] + 1;
313 w[ch] = frac(u);
314 }
315 }
316
317
318 static void
319 wrap_linear_clamp_to_edge(const float s[4], unsigned size,
320 int icoord0[4], int icoord1[4], float w[4])
321 {
322 uint ch;
323 for (ch = 0; ch < 4; ch++) {
324 float u = CLAMP(s[ch], 0.0F, 1.0F);
325 u = u * size - 0.5f;
326 icoord0[ch] = util_ifloor(u);
327 icoord1[ch] = icoord0[ch] + 1;
328 if (icoord0[ch] < 0)
329 icoord0[ch] = 0;
330 if (icoord1[ch] >= (int) size)
331 icoord1[ch] = size - 1;
332 w[ch] = frac(u);
333 }
334 }
335
336
337 static void
338 wrap_linear_clamp_to_border(const float s[4], unsigned size,
339 int icoord0[4], int icoord1[4], float w[4])
340 {
341 const float min = -1.0F / (2.0F * size);
342 const float max = 1.0F - min;
343 uint ch;
344 for (ch = 0; ch < 4; ch++) {
345 float u = CLAMP(s[ch], min, max);
346 u = u * size - 0.5f;
347 icoord0[ch] = util_ifloor(u);
348 icoord1[ch] = icoord0[ch] + 1;
349 w[ch] = frac(u);
350 }
351 }
352
353
354 static void
355 wrap_linear_mirror_repeat(const float s[4], unsigned size,
356 int icoord0[4], int icoord1[4], float w[4])
357 {
358 uint ch;
359 for (ch = 0; ch < 4; ch++) {
360 const int flr = util_ifloor(s[ch]);
361 float u;
362 if (flr & 1)
363 u = 1.0F - (s[ch] - (float) flr);
364 else
365 u = s[ch] - (float) flr;
366 u = u * size - 0.5F;
367 icoord0[ch] = util_ifloor(u);
368 icoord1[ch] = icoord0[ch] + 1;
369 if (icoord0[ch] < 0)
370 icoord0[ch] = 0;
371 if (icoord1[ch] >= (int) size)
372 icoord1[ch] = size - 1;
373 w[ch] = frac(u);
374 }
375 }
376
377
378 static void
379 wrap_linear_mirror_clamp(const float s[4], unsigned size,
380 int icoord0[4], int icoord1[4], float w[4])
381 {
382 uint ch;
383 for (ch = 0; ch < 4; ch++) {
384 float u = fabsf(s[ch]);
385 if (u >= 1.0F)
386 u = (float) size;
387 else
388 u *= size;
389 u -= 0.5F;
390 icoord0[ch] = util_ifloor(u);
391 icoord1[ch] = icoord0[ch] + 1;
392 w[ch] = frac(u);
393 }
394 }
395
396
397 static void
398 wrap_linear_mirror_clamp_to_edge(const float s[4], unsigned size,
399 int icoord0[4], int icoord1[4], float w[4])
400 {
401 uint ch;
402 for (ch = 0; ch < 4; ch++) {
403 float u = fabsf(s[ch]);
404 if (u >= 1.0F)
405 u = (float) size;
406 else
407 u *= size;
408 u -= 0.5F;
409 icoord0[ch] = util_ifloor(u);
410 icoord1[ch] = icoord0[ch] + 1;
411 if (icoord0[ch] < 0)
412 icoord0[ch] = 0;
413 if (icoord1[ch] >= (int) size)
414 icoord1[ch] = size - 1;
415 w[ch] = frac(u);
416 }
417 }
418
419
420 static void
421 wrap_linear_mirror_clamp_to_border(const float s[4], unsigned size,
422 int icoord0[4], int icoord1[4], float w[4])
423 {
424 const float min = -1.0F / (2.0F * size);
425 const float max = 1.0F - min;
426 uint ch;
427 for (ch = 0; ch < 4; ch++) {
428 float u = fabsf(s[ch]);
429 if (u <= min)
430 u = min * size;
431 else if (u >= max)
432 u = max * size;
433 else
434 u *= size;
435 u -= 0.5F;
436 icoord0[ch] = util_ifloor(u);
437 icoord1[ch] = icoord0[ch] + 1;
438 w[ch] = frac(u);
439 }
440 }
441
442
443 /**
444 * For RECT textures / unnormalized texcoords
445 * Only a subset of wrap modes supported.
446 */
447 static void
448 wrap_nearest_unorm_clamp(const float s[4], unsigned size, int icoord[4])
449 {
450 uint ch;
451 for (ch = 0; ch < 4; ch++) {
452 int i = util_ifloor(s[ch]);
453 icoord[ch]= CLAMP(i, 0, (int) size-1);
454 }
455 }
456
457
458 /**
459 * Handles clamp_to_edge and clamp_to_border:
460 */
461 static void
462 wrap_nearest_unorm_clamp_to_border(const float s[4], unsigned size,
463 int icoord[4])
464 {
465 uint ch;
466 for (ch = 0; ch < 4; ch++) {
467 icoord[ch]= util_ifloor( CLAMP(s[ch], 0.5F, (float) size - 0.5F) );
468 }
469 }
470
471
472 /**
473 * For RECT textures / unnormalized texcoords.
474 * Only a subset of wrap modes supported.
475 */
476 static void
477 wrap_linear_unorm_clamp(const float s[4], unsigned size,
478 int icoord0[4], int icoord1[4], float w[4])
479 {
480 uint ch;
481 for (ch = 0; ch < 4; ch++) {
482 /* Not exactly what the spec says, but it matches NVIDIA output */
483 float u = CLAMP(s[ch] - 0.5F, 0.0f, (float) size - 1.0f);
484 icoord0[ch] = util_ifloor(u);
485 icoord1[ch] = icoord0[ch] + 1;
486 w[ch] = frac(u);
487 }
488 }
489
490
491 static void
492 wrap_linear_unorm_clamp_to_border(const float s[4], unsigned size,
493 int icoord0[4], int icoord1[4], float w[4])
494 {
495 uint ch;
496 for (ch = 0; ch < 4; ch++) {
497 float u = CLAMP(s[ch], 0.5F, (float) size - 0.5F);
498 u -= 0.5F;
499 icoord0[ch] = util_ifloor(u);
500 icoord1[ch] = icoord0[ch] + 1;
501 if (icoord1[ch] > (int) size - 1)
502 icoord1[ch] = size - 1;
503 w[ch] = frac(u);
504 }
505 }
506
507
508
509 /**
510 * Examine the quad's texture coordinates to compute the partial
511 * derivatives w.r.t X and Y, then compute lambda (level of detail).
512 */
513 static float
514 compute_lambda_1d(const struct sp_sampler_varient *samp,
515 const float s[QUAD_SIZE],
516 const float t[QUAD_SIZE],
517 const float p[QUAD_SIZE])
518 {
519 const struct pipe_texture *texture = samp->texture;
520 const struct pipe_sampler_state *sampler = samp->sampler;
521 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
522 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
523 float rho = MAX2(dsdx, dsdy) * texture->width0;
524
525 return util_fast_log2(rho);
526 }
527
528
529 static float
530 compute_lambda_2d(const struct sp_sampler_varient *samp,
531 const float s[QUAD_SIZE],
532 const float t[QUAD_SIZE],
533 const float p[QUAD_SIZE])
534 {
535 const struct pipe_texture *texture = samp->texture;
536 const struct pipe_sampler_state *sampler = samp->sampler;
537 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
538 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
539 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
540 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
541 float maxx = MAX2(dsdx, dsdy) * texture->width0;
542 float maxy = MAX2(dtdx, dtdy) * texture->height0;
543 float rho = MAX2(maxx, maxy);
544
545 return util_fast_log2(rho);
546 }
547
548
549 static float
550 compute_lambda_3d(const struct sp_sampler_varient *samp,
551 const float s[QUAD_SIZE],
552 const float t[QUAD_SIZE],
553 const float p[QUAD_SIZE])
554 {
555 const struct pipe_texture *texture = samp->texture;
556 const struct pipe_sampler_state *sampler = samp->sampler;
557 float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
558 float dsdy = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);
559 float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
560 float dtdy = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);
561 float dpdx = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);
562 float dpdy = fabsf(p[QUAD_TOP_LEFT] - p[QUAD_BOTTOM_LEFT]);
563 float maxx = MAX2(dsdx, dsdy) * texture->width0;
564 float maxy = MAX2(dtdx, dtdy) * texture->height0;
565 float maxz = MAX2(dpdx, dpdy) * texture->depth0;
566 float rho;
567
568 rho = MAX2(maxx, maxy);
569 rho = MAX2(rho, maxz);
570
571 return util_fast_log2(rho);
572 }
573
574
575 /**
576 * Compute lambda for a vertex texture sampler.
577 * Since there aren't derivatives to use, just return 0.
578 */
579 static float
580 compute_lambda_vert(const struct sp_sampler_varient *samp,
581 const float s[QUAD_SIZE],
582 const float t[QUAD_SIZE],
583 const float p[QUAD_SIZE])
584 {
585 return 0.0f;
586 }
587
588
589
590 /**
591 * Get a texel from a texture, using the texture tile cache.
592 *
593 * \param addr the template tex address containing cube, z, face info.
594 * \param x the x coord of texel within 2D image
595 * \param y the y coord of texel within 2D image
596 * \param rgba the quad to put the texel/color into
597 *
598 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
599 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
600 */
601
602
603
604
605 static INLINE const float *
606 get_texel_2d_no_border(const struct sp_sampler_varient *samp,
607 union tex_tile_address addr, int x, int y)
608 {
609 const struct softpipe_tex_cached_tile *tile;
610
611 addr.bits.x = x / TILE_SIZE;
612 addr.bits.y = y / TILE_SIZE;
613 y %= TILE_SIZE;
614 x %= TILE_SIZE;
615
616 tile = sp_get_cached_tile_tex(samp->cache, addr);
617
618 return &tile->data.color[y][x][0];
619 }
620
621
622 static INLINE const float *
623 get_texel_2d(const struct sp_sampler_varient *samp,
624 union tex_tile_address addr, int x, int y)
625 {
626 const struct pipe_texture *texture = samp->texture;
627 unsigned level = addr.bits.level;
628
629 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
630 y < 0 || y >= (int) u_minify(texture->height0, level)) {
631 return samp->sampler->border_color;
632 }
633 else {
634 return get_texel_2d_no_border( samp, addr, x, y );
635 }
636 }
637
638
639 /* Gather a quad of adjacent texels within a tile:
640 */
641 static INLINE void
642 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient *samp,
643 union tex_tile_address addr,
644 unsigned x, unsigned y,
645 const float *out[4])
646 {
647 const struct softpipe_tex_cached_tile *tile;
648
649 addr.bits.x = x / TILE_SIZE;
650 addr.bits.y = y / TILE_SIZE;
651 y %= TILE_SIZE;
652 x %= TILE_SIZE;
653
654 tile = sp_get_cached_tile_tex(samp->cache, addr);
655
656 out[0] = &tile->data.color[y ][x ][0];
657 out[1] = &tile->data.color[y ][x+1][0];
658 out[2] = &tile->data.color[y+1][x ][0];
659 out[3] = &tile->data.color[y+1][x+1][0];
660 }
661
662
663 /* Gather a quad of potentially non-adjacent texels:
664 */
665 static INLINE void
666 get_texel_quad_2d_no_border(const struct sp_sampler_varient *samp,
667 union tex_tile_address addr,
668 int x0, int y0,
669 int x1, int y1,
670 const float *out[4])
671 {
672 out[0] = get_texel_2d_no_border( samp, addr, x0, y0 );
673 out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
674 out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
675 out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
676 }
677
678 /* Can involve a lot of unnecessary checks for border color:
679 */
680 static INLINE void
681 get_texel_quad_2d(const struct sp_sampler_varient *samp,
682 union tex_tile_address addr,
683 int x0, int y0,
684 int x1, int y1,
685 const float *out[4])
686 {
687 out[0] = get_texel_2d( samp, addr, x0, y0 );
688 out[1] = get_texel_2d( samp, addr, x1, y0 );
689 out[3] = get_texel_2d( samp, addr, x1, y1 );
690 out[2] = get_texel_2d( samp, addr, x0, y1 );
691 }
692
693
694
695 /* 3d varients:
696 */
697 static INLINE const float *
698 get_texel_3d_no_border(const struct sp_sampler_varient *samp,
699 union tex_tile_address addr, int x, int y, int z)
700 {
701 const struct softpipe_tex_cached_tile *tile;
702
703 addr.bits.x = x / TILE_SIZE;
704 addr.bits.y = y / TILE_SIZE;
705 addr.bits.z = z;
706 y %= TILE_SIZE;
707 x %= TILE_SIZE;
708
709 tile = sp_get_cached_tile_tex(samp->cache, addr);
710
711 return &tile->data.color[y][x][0];
712 }
713
714
715 static INLINE const float *
716 get_texel_3d(const struct sp_sampler_varient *samp,
717 union tex_tile_address addr, int x, int y, int z)
718 {
719 const struct pipe_texture *texture = samp->texture;
720 unsigned level = addr.bits.level;
721
722 if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
723 y < 0 || y >= (int) u_minify(texture->height0, level) ||
724 z < 0 || z >= (int) u_minify(texture->depth0, level)) {
725 return samp->sampler->border_color;
726 }
727 else {
728 return get_texel_3d_no_border( samp, addr, x, y, z );
729 }
730 }
731
732
733 /**
734 * Given the logbase2 of a mipmap's base level size and a mipmap level,
735 * return the size (in texels) of that mipmap level.
736 * For example, if level[0].width = 256 then base_pot will be 8.
737 * If level = 2, then we'll return 64 (the width at level=2).
738 * Return 1 if level > base_pot.
739 */
740 static INLINE unsigned
741 pot_level_size(unsigned base_pot, unsigned level)
742 {
743 return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
744 }
745
746
747 /* Some image-filter fastpaths:
748 */
749 static INLINE void
750 img_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
751 const float s[QUAD_SIZE],
752 const float t[QUAD_SIZE],
753 const float p[QUAD_SIZE],
754 const float c0[QUAD_SIZE],
755 enum tgsi_sampler_control control,
756 float rgba[NUM_CHANNELS][QUAD_SIZE])
757 {
758 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
759 unsigned j;
760 unsigned level = samp->level;
761 unsigned xpot = pot_level_size(samp->xpot, level);
762 unsigned ypot = pot_level_size(samp->ypot, level);
763 unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
764 unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
765 union tex_tile_address addr;
766
767 addr.value = 0;
768 addr.bits.level = samp->level;
769
770 for (j = 0; j < QUAD_SIZE; j++) {
771 int c;
772
773 float u = s[j] * xpot - 0.5F;
774 float v = t[j] * ypot - 0.5F;
775
776 int uflr = util_ifloor(u);
777 int vflr = util_ifloor(v);
778
779 float xw = u - (float)uflr;
780 float yw = v - (float)vflr;
781
782 int x0 = uflr & (xpot - 1);
783 int y0 = vflr & (ypot - 1);
784
785 const float *tx[4];
786
787 /* Can we fetch all four at once:
788 */
789 if (x0 < xmax && y0 < ymax) {
790 get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
791 }
792 else {
793 unsigned x1 = (x0 + 1) & (xpot - 1);
794 unsigned y1 = (y0 + 1) & (ypot - 1);
795 get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
796 }
797
798 /* interpolate R, G, B, A */
799 for (c = 0; c < 4; c++) {
800 rgba[c][j] = lerp_2d(xw, yw,
801 tx[0][c], tx[1][c],
802 tx[2][c], tx[3][c]);
803 }
804 }
805 }
806
807
808 static INLINE void
809 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
810 const float s[QUAD_SIZE],
811 const float t[QUAD_SIZE],
812 const float p[QUAD_SIZE],
813 const float c0[QUAD_SIZE],
814 enum tgsi_sampler_control control,
815 float rgba[NUM_CHANNELS][QUAD_SIZE])
816 {
817 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
818 unsigned j;
819 unsigned level = samp->level;
820 unsigned xpot = pot_level_size(samp->xpot, level);
821 unsigned ypot = pot_level_size(samp->ypot, level);
822 union tex_tile_address addr;
823
824 addr.value = 0;
825 addr.bits.level = samp->level;
826
827 for (j = 0; j < QUAD_SIZE; j++) {
828 int c;
829
830 float u = s[j] * xpot;
831 float v = t[j] * ypot;
832
833 int uflr = util_ifloor(u);
834 int vflr = util_ifloor(v);
835
836 int x0 = uflr & (xpot - 1);
837 int y0 = vflr & (ypot - 1);
838
839 const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
840
841 for (c = 0; c < 4; c++) {
842 rgba[c][j] = out[c];
843 }
844 }
845 }
846
847
848 static INLINE void
849 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
850 const float s[QUAD_SIZE],
851 const float t[QUAD_SIZE],
852 const float p[QUAD_SIZE],
853 const float c0[QUAD_SIZE],
854 enum tgsi_sampler_control control,
855 float rgba[NUM_CHANNELS][QUAD_SIZE])
856 {
857 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
858 unsigned j;
859 unsigned level = samp->level;
860 unsigned xpot = pot_level_size(samp->xpot, level);
861 unsigned ypot = pot_level_size(samp->ypot, level);
862 union tex_tile_address addr;
863
864 addr.value = 0;
865 addr.bits.level = samp->level;
866
867 for (j = 0; j < QUAD_SIZE; j++) {
868 int c;
869
870 float u = s[j] * xpot;
871 float v = t[j] * ypot;
872
873 int x0, y0;
874 const float *out;
875
876 x0 = util_ifloor(u);
877 if (x0 < 0)
878 x0 = 0;
879 else if (x0 > xpot - 1)
880 x0 = xpot - 1;
881
882 y0 = util_ifloor(v);
883 if (y0 < 0)
884 y0 = 0;
885 else if (y0 > ypot - 1)
886 y0 = ypot - 1;
887
888 out = get_texel_2d_no_border(samp, addr, x0, y0);
889
890 for (c = 0; c < 4; c++) {
891 rgba[c][j] = out[c];
892 }
893 }
894 }
895
896
897 static void
898 img_filter_1d_nearest(struct tgsi_sampler *tgsi_sampler,
899 const float s[QUAD_SIZE],
900 const float t[QUAD_SIZE],
901 const float p[QUAD_SIZE],
902 const float c0[QUAD_SIZE],
903 enum tgsi_sampler_control control,
904 float rgba[NUM_CHANNELS][QUAD_SIZE])
905 {
906 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
907 const struct pipe_texture *texture = samp->texture;
908 unsigned level0, j;
909 int width;
910 int x[4];
911 union tex_tile_address addr;
912
913 level0 = samp->level;
914 width = u_minify(texture->width0, level0);
915
916 assert(width > 0);
917
918 addr.value = 0;
919 addr.bits.level = samp->level;
920
921 samp->nearest_texcoord_s(s, width, x);
922
923 for (j = 0; j < QUAD_SIZE; j++) {
924 const float *out = get_texel_2d(samp, addr, x[j], 0);
925 int c;
926 for (c = 0; c < 4; c++) {
927 rgba[c][j] = out[c];
928 }
929 }
930 }
931
932
933 static void
934 img_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
935 const float s[QUAD_SIZE],
936 const float t[QUAD_SIZE],
937 const float p[QUAD_SIZE],
938 const float c0[QUAD_SIZE],
939 enum tgsi_sampler_control control,
940 float rgba[NUM_CHANNELS][QUAD_SIZE])
941 {
942 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
943 const struct pipe_texture *texture = samp->texture;
944 unsigned level0, j;
945 int width, height;
946 int x[4], y[4];
947 union tex_tile_address addr;
948
949
950 level0 = samp->level;
951 width = u_minify(texture->width0, level0);
952 height = u_minify(texture->height0, level0);
953
954 assert(width > 0);
955 assert(height > 0);
956
957 addr.value = 0;
958 addr.bits.level = samp->level;
959
960 samp->nearest_texcoord_s(s, width, x);
961 samp->nearest_texcoord_t(t, height, y);
962
963 for (j = 0; j < QUAD_SIZE; j++) {
964 const float *out = get_texel_2d(samp, addr, x[j], y[j]);
965 int c;
966 for (c = 0; c < 4; c++) {
967 rgba[c][j] = out[c];
968 }
969 }
970 }
971
972
973 static INLINE union tex_tile_address
974 face(union tex_tile_address addr, unsigned face )
975 {
976 addr.bits.face = face;
977 return addr;
978 }
979
980
981 static void
982 img_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
983 const float s[QUAD_SIZE],
984 const float t[QUAD_SIZE],
985 const float p[QUAD_SIZE],
986 const float c0[QUAD_SIZE],
987 enum tgsi_sampler_control control,
988 float rgba[NUM_CHANNELS][QUAD_SIZE])
989 {
990 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
991 const struct pipe_texture *texture = samp->texture;
992 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
993 unsigned level0, j;
994 int width, height;
995 int x[4], y[4];
996 union tex_tile_address addr;
997
998 level0 = samp->level;
999 width = u_minify(texture->width0, level0);
1000 height = u_minify(texture->height0, level0);
1001
1002 assert(width > 0);
1003 assert(height > 0);
1004
1005 addr.value = 0;
1006 addr.bits.level = samp->level;
1007
1008 samp->nearest_texcoord_s(s, width, x);
1009 samp->nearest_texcoord_t(t, height, y);
1010
1011 for (j = 0; j < QUAD_SIZE; j++) {
1012 const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
1013 int c;
1014 for (c = 0; c < 4; c++) {
1015 rgba[c][j] = out[c];
1016 }
1017 }
1018 }
1019
1020
1021 static void
1022 img_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
1023 const float s[QUAD_SIZE],
1024 const float t[QUAD_SIZE],
1025 const float p[QUAD_SIZE],
1026 const float c0[QUAD_SIZE],
1027 enum tgsi_sampler_control control,
1028 float rgba[NUM_CHANNELS][QUAD_SIZE])
1029 {
1030 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1031 const struct pipe_texture *texture = samp->texture;
1032 unsigned level0, j;
1033 int width, height, depth;
1034 int x[4], y[4], z[4];
1035 union tex_tile_address addr;
1036
1037 level0 = samp->level;
1038 width = u_minify(texture->width0, level0);
1039 height = u_minify(texture->height0, level0);
1040 depth = u_minify(texture->depth0, level0);
1041
1042 assert(width > 0);
1043 assert(height > 0);
1044 assert(depth > 0);
1045
1046 samp->nearest_texcoord_s(s, width, x);
1047 samp->nearest_texcoord_t(t, height, y);
1048 samp->nearest_texcoord_p(p, depth, z);
1049
1050 addr.value = 0;
1051 addr.bits.level = samp->level;
1052
1053 for (j = 0; j < QUAD_SIZE; j++) {
1054 const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
1055 int c;
1056 for (c = 0; c < 4; c++) {
1057 rgba[c][j] = out[c];
1058 }
1059 }
1060 }
1061
1062
1063 static void
1064 img_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
1065 const float s[QUAD_SIZE],
1066 const float t[QUAD_SIZE],
1067 const float p[QUAD_SIZE],
1068 const float c0[QUAD_SIZE],
1069 enum tgsi_sampler_control control,
1070 float rgba[NUM_CHANNELS][QUAD_SIZE])
1071 {
1072 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1073 const struct pipe_texture *texture = samp->texture;
1074 unsigned level0, j;
1075 int width;
1076 int x0[4], x1[4];
1077 float xw[4]; /* weights */
1078 union tex_tile_address addr;
1079
1080 level0 = samp->level;
1081 width = u_minify(texture->width0, level0);
1082
1083 assert(width > 0);
1084
1085 addr.value = 0;
1086 addr.bits.level = samp->level;
1087
1088 samp->linear_texcoord_s(s, width, x0, x1, xw);
1089
1090 for (j = 0; j < QUAD_SIZE; j++) {
1091 const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
1092 const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
1093 int c;
1094
1095 /* interpolate R, G, B, A */
1096 for (c = 0; c < 4; c++) {
1097 rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1098 }
1099 }
1100 }
1101
1102
1103 static void
1104 img_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
1105 const float s[QUAD_SIZE],
1106 const float t[QUAD_SIZE],
1107 const float p[QUAD_SIZE],
1108 const float c0[QUAD_SIZE],
1109 enum tgsi_sampler_control control,
1110 float rgba[NUM_CHANNELS][QUAD_SIZE])
1111 {
1112 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1113 const struct pipe_texture *texture = samp->texture;
1114 unsigned level0, j;
1115 int width, height;
1116 int x0[4], y0[4], x1[4], y1[4];
1117 float xw[4], yw[4]; /* weights */
1118 union tex_tile_address addr;
1119
1120 level0 = samp->level;
1121 width = u_minify(texture->width0, level0);
1122 height = u_minify(texture->height0, level0);
1123
1124 assert(width > 0);
1125 assert(height > 0);
1126
1127 addr.value = 0;
1128 addr.bits.level = samp->level;
1129
1130 samp->linear_texcoord_s(s, width, x0, x1, xw);
1131 samp->linear_texcoord_t(t, height, y0, y1, yw);
1132
1133 for (j = 0; j < QUAD_SIZE; j++) {
1134 const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
1135 const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
1136 const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
1137 const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
1138 int c;
1139
1140 /* interpolate R, G, B, A */
1141 for (c = 0; c < 4; c++) {
1142 rgba[c][j] = lerp_2d(xw[j], yw[j],
1143 tx0[c], tx1[c],
1144 tx2[c], tx3[c]);
1145 }
1146 }
1147 }
1148
1149
1150 static void
1151 img_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1152 const float s[QUAD_SIZE],
1153 const float t[QUAD_SIZE],
1154 const float p[QUAD_SIZE],
1155 const float c0[QUAD_SIZE],
1156 enum tgsi_sampler_control control,
1157 float rgba[NUM_CHANNELS][QUAD_SIZE])
1158 {
1159 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1160 const struct pipe_texture *texture = samp->texture;
1161 const unsigned *faces = samp->faces; /* zero when not cube-mapping */
1162 unsigned level0, j;
1163 int width, height;
1164 int x0[4], y0[4], x1[4], y1[4];
1165 float xw[4], yw[4]; /* weights */
1166 union tex_tile_address addr;
1167
1168 level0 = samp->level;
1169 width = u_minify(texture->width0, level0);
1170 height = u_minify(texture->height0, level0);
1171
1172 assert(width > 0);
1173 assert(height > 0);
1174
1175 addr.value = 0;
1176 addr.bits.level = samp->level;
1177
1178 samp->linear_texcoord_s(s, width, x0, x1, xw);
1179 samp->linear_texcoord_t(t, height, y0, y1, yw);
1180
1181 for (j = 0; j < QUAD_SIZE; j++) {
1182 union tex_tile_address addrj = face(addr, faces[j]);
1183 const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
1184 const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
1185 const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
1186 const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
1187 int c;
1188
1189 /* interpolate R, G, B, A */
1190 for (c = 0; c < 4; c++) {
1191 rgba[c][j] = lerp_2d(xw[j], yw[j],
1192 tx0[c], tx1[c],
1193 tx2[c], tx3[c]);
1194 }
1195 }
1196 }
1197
1198
1199 static void
1200 img_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
1201 const float s[QUAD_SIZE],
1202 const float t[QUAD_SIZE],
1203 const float p[QUAD_SIZE],
1204 const float c0[QUAD_SIZE],
1205 enum tgsi_sampler_control control,
1206 float rgba[NUM_CHANNELS][QUAD_SIZE])
1207 {
1208 const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1209 const struct pipe_texture *texture = samp->texture;
1210 unsigned level0, j;
1211 int width, height, depth;
1212 int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
1213 float xw[4], yw[4], zw[4]; /* interpolation weights */
1214 union tex_tile_address addr;
1215
1216 level0 = samp->level;
1217 width = u_minify(texture->width0, level0);
1218 height = u_minify(texture->height0, level0);
1219 depth = u_minify(texture->depth0, level0);
1220
1221 addr.value = 0;
1222 addr.bits.level = level0;
1223
1224 assert(width > 0);
1225 assert(height > 0);
1226 assert(depth > 0);
1227
1228 samp->linear_texcoord_s(s, width, x0, x1, xw);
1229 samp->linear_texcoord_t(t, height, y0, y1, yw);
1230 samp->linear_texcoord_p(p, depth, z0, z1, zw);
1231
1232 for (j = 0; j < QUAD_SIZE; j++) {
1233 int c;
1234
1235 const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
1236 const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
1237 const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
1238 const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
1239
1240 const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
1241 const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
1242 const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
1243 const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
1244
1245 /* interpolate R, G, B, A */
1246 for (c = 0; c < 4; c++) {
1247 rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
1248 tx00[c], tx01[c],
1249 tx02[c], tx03[c],
1250 tx10[c], tx11[c],
1251 tx12[c], tx13[c]);
1252 }
1253 }
1254 }
1255
1256
1257 /* Calculate level of detail for every fragment.
1258 * Note that lambda has already been biased by global LOD bias.
1259 */
1260 static INLINE void
1261 compute_lod(const struct pipe_sampler_state *sampler,
1262 const float biased_lambda,
1263 const float lodbias[QUAD_SIZE],
1264 float lod[QUAD_SIZE])
1265 {
1266 uint i;
1267
1268 for (i = 0; i < QUAD_SIZE; i++) {
1269 lod[i] = biased_lambda + lodbias[i];
1270 lod[i] = CLAMP(lod[i], sampler->min_lod, sampler->max_lod);
1271 }
1272 }
1273
1274
1275 static void
1276 mip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1277 const float s[QUAD_SIZE],
1278 const float t[QUAD_SIZE],
1279 const float p[QUAD_SIZE],
1280 const float c0[QUAD_SIZE],
1281 enum tgsi_sampler_control control,
1282 float rgba[NUM_CHANNELS][QUAD_SIZE])
1283 {
1284 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1285 const struct pipe_texture *texture = samp->texture;
1286 int level0;
1287 float lambda;
1288 float lod[QUAD_SIZE];
1289
1290 if (control == tgsi_sampler_lod_bias) {
1291 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1292 compute_lod(samp->sampler, lambda, c0, lod);
1293 } else {
1294 assert(control == tgsi_sampler_lod_explicit);
1295
1296 memcpy(lod, c0, sizeof(lod));
1297 }
1298
1299 /* XXX: Take into account all lod values.
1300 */
1301 lambda = lod[0];
1302 level0 = (int)lambda;
1303
1304 if (lambda < 0.0) {
1305 samp->level = 0;
1306 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1307 }
1308 else if (level0 >= texture->last_level) {
1309 samp->level = texture->last_level;
1310 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1311 }
1312 else {
1313 float levelBlend = lambda - level0;
1314 float rgba0[4][4];
1315 float rgba1[4][4];
1316 int c,j;
1317
1318 samp->level = level0;
1319 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1320
1321 samp->level = level0+1;
1322 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1323
1324 for (j = 0; j < QUAD_SIZE; j++) {
1325 for (c = 0; c < 4; c++) {
1326 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1327 }
1328 }
1329 }
1330 }
1331
1332
1333 static void
1334 mip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
1335 const float s[QUAD_SIZE],
1336 const float t[QUAD_SIZE],
1337 const float p[QUAD_SIZE],
1338 const float c0[QUAD_SIZE],
1339 enum tgsi_sampler_control control,
1340 float rgba[NUM_CHANNELS][QUAD_SIZE])
1341 {
1342 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1343 const struct pipe_texture *texture = samp->texture;
1344 float lambda;
1345 float lod[QUAD_SIZE];
1346
1347 if (control == tgsi_sampler_lod_bias) {
1348 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1349 compute_lod(samp->sampler, lambda, c0, lod);
1350 } else {
1351 assert(control == tgsi_sampler_lod_explicit);
1352
1353 memcpy(lod, c0, sizeof(lod));
1354 }
1355
1356 /* XXX: Take into account all lod values.
1357 */
1358 lambda = lod[0];
1359
1360 if (lambda < 0.0) {
1361 samp->level = 0;
1362 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1363 }
1364 else {
1365 samp->level = (int)(lambda + 0.5) ;
1366 samp->level = MIN2(samp->level, (int)texture->last_level);
1367 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1368 }
1369
1370 #if 0
1371 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1372 rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
1373 rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
1374 rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
1375 rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
1376 #endif
1377 }
1378
1379
1380 static void
1381 mip_filter_none(struct tgsi_sampler *tgsi_sampler,
1382 const float s[QUAD_SIZE],
1383 const float t[QUAD_SIZE],
1384 const float p[QUAD_SIZE],
1385 const float c0[QUAD_SIZE],
1386 enum tgsi_sampler_control control,
1387 float rgba[NUM_CHANNELS][QUAD_SIZE])
1388 {
1389 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1390 float lambda;
1391 float lod[QUAD_SIZE];
1392
1393 if (control == tgsi_sampler_lod_bias) {
1394 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1395 compute_lod(samp->sampler, lambda, c0, lod);
1396 } else {
1397 assert(control == tgsi_sampler_lod_explicit);
1398
1399 memcpy(lod, c0, sizeof(lod));
1400 }
1401
1402 /* XXX: Take into account all lod values.
1403 */
1404 lambda = lod[0];
1405
1406 if (lambda < 0.0) {
1407 samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1408 }
1409 else {
1410 samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1411 }
1412 }
1413
1414
1415
1416 /**
1417 * Specialized version of mip_filter_linear with hard-wired calls to
1418 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1419 */
1420 static void
1421 mip_filter_linear_2d_linear_repeat_POT(
1422 struct tgsi_sampler *tgsi_sampler,
1423 const float s[QUAD_SIZE],
1424 const float t[QUAD_SIZE],
1425 const float p[QUAD_SIZE],
1426 const float c0[QUAD_SIZE],
1427 enum tgsi_sampler_control control,
1428 float rgba[NUM_CHANNELS][QUAD_SIZE])
1429 {
1430 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1431 const struct pipe_texture *texture = samp->texture;
1432 int level0;
1433 float lambda;
1434 float lod[QUAD_SIZE];
1435
1436 if (control == tgsi_sampler_lod_bias) {
1437 lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
1438 compute_lod(samp->sampler, lambda, c0, lod);
1439 } else {
1440 assert(control == tgsi_sampler_lod_explicit);
1441
1442 memcpy(lod, c0, sizeof(lod));
1443 }
1444
1445 /* XXX: Take into account all lod values.
1446 */
1447 lambda = lod[0];
1448 level0 = (int)lambda;
1449
1450 /* Catches both negative and large values of level0:
1451 */
1452 if ((unsigned)level0 >= texture->last_level) {
1453 if (level0 < 0)
1454 samp->level = 0;
1455 else
1456 samp->level = texture->last_level;
1457
1458 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
1459 }
1460 else {
1461 float levelBlend = lambda - level0;
1462 float rgba0[4][4];
1463 float rgba1[4][4];
1464 int c,j;
1465
1466 samp->level = level0;
1467 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
1468
1469 samp->level = level0+1;
1470 img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
1471
1472 for (j = 0; j < QUAD_SIZE; j++) {
1473 for (c = 0; c < 4; c++) {
1474 rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1475 }
1476 }
1477 }
1478 }
1479
1480
1481
1482 /**
1483 * Do shadow/depth comparisons.
1484 */
1485 static void
1486 sample_compare(struct tgsi_sampler *tgsi_sampler,
1487 const float s[QUAD_SIZE],
1488 const float t[QUAD_SIZE],
1489 const float p[QUAD_SIZE],
1490 const float c0[QUAD_SIZE],
1491 enum tgsi_sampler_control control,
1492 float rgba[NUM_CHANNELS][QUAD_SIZE])
1493 {
1494 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1495 const struct pipe_sampler_state *sampler = samp->sampler;
1496 int j, k0, k1, k2, k3;
1497 float val;
1498
1499 samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
1500
1501 /**
1502 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1503 * When we sampled the depth texture, the depth value was put into all
1504 * RGBA channels. We look at the red channel here.
1505 */
1506
1507 /* compare four texcoords vs. four texture samples */
1508 switch (sampler->compare_func) {
1509 case PIPE_FUNC_LESS:
1510 k0 = p[0] < rgba[0][0];
1511 k1 = p[1] < rgba[0][1];
1512 k2 = p[2] < rgba[0][2];
1513 k3 = p[3] < rgba[0][3];
1514 break;
1515 case PIPE_FUNC_LEQUAL:
1516 k0 = p[0] <= rgba[0][0];
1517 k1 = p[1] <= rgba[0][1];
1518 k2 = p[2] <= rgba[0][2];
1519 k3 = p[3] <= rgba[0][3];
1520 break;
1521 case PIPE_FUNC_GREATER:
1522 k0 = p[0] > rgba[0][0];
1523 k1 = p[1] > rgba[0][1];
1524 k2 = p[2] > rgba[0][2];
1525 k3 = p[3] > rgba[0][3];
1526 break;
1527 case PIPE_FUNC_GEQUAL:
1528 k0 = p[0] >= rgba[0][0];
1529 k1 = p[1] >= rgba[0][1];
1530 k2 = p[2] >= rgba[0][2];
1531 k3 = p[3] >= rgba[0][3];
1532 break;
1533 case PIPE_FUNC_EQUAL:
1534 k0 = p[0] == rgba[0][0];
1535 k1 = p[1] == rgba[0][1];
1536 k2 = p[2] == rgba[0][2];
1537 k3 = p[3] == rgba[0][3];
1538 break;
1539 case PIPE_FUNC_NOTEQUAL:
1540 k0 = p[0] != rgba[0][0];
1541 k1 = p[1] != rgba[0][1];
1542 k2 = p[2] != rgba[0][2];
1543 k3 = p[3] != rgba[0][3];
1544 break;
1545 case PIPE_FUNC_ALWAYS:
1546 k0 = k1 = k2 = k3 = 1;
1547 break;
1548 case PIPE_FUNC_NEVER:
1549 k0 = k1 = k2 = k3 = 0;
1550 break;
1551 default:
1552 k0 = k1 = k2 = k3 = 0;
1553 assert(0);
1554 break;
1555 }
1556
1557 /* convert four pass/fail values to an intensity in [0,1] */
1558 val = 0.25F * (k0 + k1 + k2 + k3);
1559
1560 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1561 for (j = 0; j < 4; j++) {
1562 rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
1563 rgba[3][j] = 1.0F;
1564 }
1565 }
1566
1567
1568 /**
1569 * Compute which cube face is referenced by each texcoord and put that
1570 * info into the sampler faces[] array. Then sample the cube faces
1571 */
1572 static void
1573 sample_cube(struct tgsi_sampler *tgsi_sampler,
1574 const float s[QUAD_SIZE],
1575 const float t[QUAD_SIZE],
1576 const float p[QUAD_SIZE],
1577 const float c0[QUAD_SIZE],
1578 enum tgsi_sampler_control control,
1579 float rgba[NUM_CHANNELS][QUAD_SIZE])
1580 {
1581 struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1582 unsigned j;
1583 float ssss[4], tttt[4];
1584
1585 /*
1586 major axis
1587 direction target sc tc ma
1588 ---------- ------------------------------- --- --- ---
1589 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1590 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1591 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1592 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1593 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1594 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1595 */
1596 for (j = 0; j < QUAD_SIZE; j++) {
1597 float rx = s[j];
1598 float ry = t[j];
1599 float rz = p[j];
1600 const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
1601 unsigned face;
1602 float sc, tc, ma;
1603
1604 if (arx >= ary && arx >= arz) {
1605 if (rx >= 0.0F) {
1606 face = PIPE_TEX_FACE_POS_X;
1607 sc = -rz;
1608 tc = -ry;
1609 ma = arx;
1610 }
1611 else {
1612 face = PIPE_TEX_FACE_NEG_X;
1613 sc = rz;
1614 tc = -ry;
1615 ma = arx;
1616 }
1617 }
1618 else if (ary >= arx && ary >= arz) {
1619 if (ry >= 0.0F) {
1620 face = PIPE_TEX_FACE_POS_Y;
1621 sc = rx;
1622 tc = rz;
1623 ma = ary;
1624 }
1625 else {
1626 face = PIPE_TEX_FACE_NEG_Y;
1627 sc = rx;
1628 tc = -rz;
1629 ma = ary;
1630 }
1631 }
1632 else {
1633 if (rz > 0.0F) {
1634 face = PIPE_TEX_FACE_POS_Z;
1635 sc = rx;
1636 tc = -ry;
1637 ma = arz;
1638 }
1639 else {
1640 face = PIPE_TEX_FACE_NEG_Z;
1641 sc = -rx;
1642 tc = -ry;
1643 ma = arz;
1644 }
1645 }
1646
1647 {
1648 const float ima = 1.0 / ma;
1649 ssss[j] = ( sc * ima + 1.0F ) * 0.5F;
1650 tttt[j] = ( tc * ima + 1.0F ) * 0.5F;
1651 samp->faces[j] = face;
1652 }
1653 }
1654
1655 /* In our little pipeline, the compare stage is next. If compare
1656 * is not active, this will point somewhere deeper into the
1657 * pipeline, eg. to mip_filter or even img_filter.
1658 */
1659 samp->compare(tgsi_sampler, ssss, tttt, NULL, c0, control, rgba);
1660 }
1661
1662
1663
1664 static wrap_nearest_func
1665 get_nearest_unorm_wrap(unsigned mode)
1666 {
1667 switch (mode) {
1668 case PIPE_TEX_WRAP_CLAMP:
1669 return wrap_nearest_unorm_clamp;
1670 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1671 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1672 return wrap_nearest_unorm_clamp_to_border;
1673 default:
1674 assert(0);
1675 return wrap_nearest_unorm_clamp;
1676 }
1677 }
1678
1679
1680 static wrap_nearest_func
1681 get_nearest_wrap(unsigned mode)
1682 {
1683 switch (mode) {
1684 case PIPE_TEX_WRAP_REPEAT:
1685 return wrap_nearest_repeat;
1686 case PIPE_TEX_WRAP_CLAMP:
1687 return wrap_nearest_clamp;
1688 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1689 return wrap_nearest_clamp_to_edge;
1690 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1691 return wrap_nearest_clamp_to_border;
1692 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1693 return wrap_nearest_mirror_repeat;
1694 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1695 return wrap_nearest_mirror_clamp;
1696 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1697 return wrap_nearest_mirror_clamp_to_edge;
1698 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1699 return wrap_nearest_mirror_clamp_to_border;
1700 default:
1701 assert(0);
1702 return wrap_nearest_repeat;
1703 }
1704 }
1705
1706
1707 static wrap_linear_func
1708 get_linear_unorm_wrap(unsigned mode)
1709 {
1710 switch (mode) {
1711 case PIPE_TEX_WRAP_CLAMP:
1712 return wrap_linear_unorm_clamp;
1713 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1714 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1715 return wrap_linear_unorm_clamp_to_border;
1716 default:
1717 assert(0);
1718 return wrap_linear_unorm_clamp;
1719 }
1720 }
1721
1722
1723 static wrap_linear_func
1724 get_linear_wrap(unsigned mode)
1725 {
1726 switch (mode) {
1727 case PIPE_TEX_WRAP_REPEAT:
1728 return wrap_linear_repeat;
1729 case PIPE_TEX_WRAP_CLAMP:
1730 return wrap_linear_clamp;
1731 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1732 return wrap_linear_clamp_to_edge;
1733 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1734 return wrap_linear_clamp_to_border;
1735 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1736 return wrap_linear_mirror_repeat;
1737 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1738 return wrap_linear_mirror_clamp;
1739 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1740 return wrap_linear_mirror_clamp_to_edge;
1741 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1742 return wrap_linear_mirror_clamp_to_border;
1743 default:
1744 assert(0);
1745 return wrap_linear_repeat;
1746 }
1747 }
1748
1749
1750 static compute_lambda_func
1751 get_lambda_func(const union sp_sampler_key key)
1752 {
1753 if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
1754 return compute_lambda_vert;
1755
1756 switch (key.bits.target) {
1757 case PIPE_TEXTURE_1D:
1758 return compute_lambda_1d;
1759 case PIPE_TEXTURE_2D:
1760 case PIPE_TEXTURE_CUBE:
1761 return compute_lambda_2d;
1762 case PIPE_TEXTURE_3D:
1763 return compute_lambda_3d;
1764 default:
1765 assert(0);
1766 return compute_lambda_1d;
1767 }
1768 }
1769
1770
1771 static filter_func
1772 get_img_filter(const union sp_sampler_key key,
1773 unsigned filter,
1774 const struct pipe_sampler_state *sampler)
1775 {
1776 switch (key.bits.target) {
1777 case PIPE_TEXTURE_1D:
1778 if (filter == PIPE_TEX_FILTER_NEAREST)
1779 return img_filter_1d_nearest;
1780 else
1781 return img_filter_1d_linear;
1782 break;
1783 case PIPE_TEXTURE_2D:
1784 /* Try for fast path:
1785 */
1786 if (key.bits.is_pot &&
1787 sampler->wrap_s == sampler->wrap_t &&
1788 sampler->normalized_coords)
1789 {
1790 switch (sampler->wrap_s) {
1791 case PIPE_TEX_WRAP_REPEAT:
1792 switch (filter) {
1793 case PIPE_TEX_FILTER_NEAREST:
1794 return img_filter_2d_nearest_repeat_POT;
1795 case PIPE_TEX_FILTER_LINEAR:
1796 return img_filter_2d_linear_repeat_POT;
1797 default:
1798 break;
1799 }
1800 break;
1801 case PIPE_TEX_WRAP_CLAMP:
1802 switch (filter) {
1803 case PIPE_TEX_FILTER_NEAREST:
1804 return img_filter_2d_nearest_clamp_POT;
1805 default:
1806 break;
1807 }
1808 }
1809 }
1810 /* Otherwise use default versions:
1811 */
1812 if (filter == PIPE_TEX_FILTER_NEAREST)
1813 return img_filter_2d_nearest;
1814 else
1815 return img_filter_2d_linear;
1816 break;
1817 case PIPE_TEXTURE_CUBE:
1818 if (filter == PIPE_TEX_FILTER_NEAREST)
1819 return img_filter_cube_nearest;
1820 else
1821 return img_filter_cube_linear;
1822 break;
1823 case PIPE_TEXTURE_3D:
1824 if (filter == PIPE_TEX_FILTER_NEAREST)
1825 return img_filter_3d_nearest;
1826 else
1827 return img_filter_3d_linear;
1828 break;
1829 default:
1830 assert(0);
1831 return img_filter_1d_nearest;
1832 }
1833 }
1834
1835
1836 /**
1837 * Bind the given texture object and texture cache to the sampler varient.
1838 */
1839 void
1840 sp_sampler_varient_bind_texture( struct sp_sampler_varient *samp,
1841 struct softpipe_tex_tile_cache *tex_cache,
1842 const struct pipe_texture *texture )
1843 {
1844 const struct pipe_sampler_state *sampler = samp->sampler;
1845
1846 samp->texture = texture;
1847 samp->cache = tex_cache;
1848 samp->xpot = util_unsigned_logbase2( texture->width0 );
1849 samp->ypot = util_unsigned_logbase2( texture->height0 );
1850 samp->level = CLAMP((int) sampler->min_lod, 0, (int) texture->last_level);
1851 }
1852
1853
1854 void
1855 sp_sampler_varient_destroy( struct sp_sampler_varient *samp )
1856 {
1857 FREE(samp);
1858 }
1859
1860
1861 /**
1862 * Create a sampler varient for a given set of non-orthogonal state.
1863 */
1864 struct sp_sampler_varient *
1865 sp_create_sampler_varient( const struct pipe_sampler_state *sampler,
1866 const union sp_sampler_key key )
1867 {
1868 struct sp_sampler_varient *samp = CALLOC_STRUCT(sp_sampler_varient);
1869 if (!samp)
1870 return NULL;
1871
1872 samp->sampler = sampler;
1873 samp->key = key;
1874
1875 /* Note that (for instance) linear_texcoord_s and
1876 * nearest_texcoord_s may be active at the same time, if the
1877 * sampler min_img_filter differs from its mag_img_filter.
1878 */
1879 if (sampler->normalized_coords) {
1880 samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
1881 samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
1882 samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
1883
1884 samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
1885 samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
1886 samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
1887 }
1888 else {
1889 samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
1890 samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
1891 samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
1892
1893 samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
1894 samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
1895 samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
1896 }
1897
1898 samp->compute_lambda = get_lambda_func( key );
1899
1900 samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
1901 samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
1902
1903 switch (sampler->min_mip_filter) {
1904 case PIPE_TEX_MIPFILTER_NONE:
1905 if (sampler->min_img_filter == sampler->mag_img_filter)
1906 samp->mip_filter = samp->min_img_filter;
1907 else
1908 samp->mip_filter = mip_filter_none;
1909 break;
1910
1911 case PIPE_TEX_MIPFILTER_NEAREST:
1912 samp->mip_filter = mip_filter_nearest;
1913 break;
1914
1915 case PIPE_TEX_MIPFILTER_LINEAR:
1916 if (key.bits.is_pot &&
1917 sampler->min_img_filter == sampler->mag_img_filter &&
1918 sampler->normalized_coords &&
1919 sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
1920 sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
1921 sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR)
1922 {
1923 samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
1924 }
1925 else
1926 {
1927 samp->mip_filter = mip_filter_linear;
1928 }
1929 break;
1930 }
1931
1932 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
1933 samp->compare = sample_compare;
1934 }
1935 else {
1936 /* Skip compare operation by promoting the mip_filter function
1937 * pointer:
1938 */
1939 samp->compare = samp->mip_filter;
1940 }
1941
1942 if (key.bits.target == PIPE_TEXTURE_CUBE) {
1943 samp->base.get_samples = sample_cube;
1944 }
1945 else {
1946 samp->faces[0] = 0;
1947 samp->faces[1] = 0;
1948 samp->faces[2] = 0;
1949 samp->faces[3] = 0;
1950
1951 /* Skip cube face determination by promoting the compare
1952 * function pointer:
1953 */
1954 samp->base.get_samples = samp->compare;
1955 }
1956
1957 return samp;
1958 }