1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008-2010 VMware, Inc. All rights reserved.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
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"
47 /** Set to one to help debug texture sampling */
52 * Return fractional part of 'f'. Used for computing interpolation weights.
53 * Need to be careful with negative values.
54 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
55 * of improperly weighted linear-filtered textures.
56 * The tests/texwrap.c demo is a good test.
67 * Linear interpolation macro
70 lerp(float a
, float v0
, float v1
)
72 return v0
+ a
* (v1
- v0
);
77 * Do 2D/bilinear interpolation of float values.
78 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
79 * a and b are the horizontal and vertical interpolants.
80 * It's important that this function is inlined when compiled with
81 * optimization! If we find that's not true on some systems, convert
85 lerp_2d(float a
, float b
,
86 float v00
, float v10
, float v01
, float v11
)
88 const float temp0
= lerp(a
, v00
, v10
);
89 const float temp1
= lerp(a
, v01
, v11
);
90 return lerp(b
, temp0
, temp1
);
95 * As above, but 3D interpolation of 8 values.
98 lerp_3d(float a
, float b
, float c
,
99 float v000
, float v100
, float v010
, float v110
,
100 float v001
, float v101
, float v011
, float v111
)
102 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
103 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
104 return lerp(c
, temp0
, temp1
);
110 * Compute coord % size for repeat wrap modes.
111 * Note that if coord is negative, coord % size doesn't give the right
112 * value. To avoid that problem we add a large multiple of the size
113 * (rather than using a conditional).
116 repeat(int coord
, unsigned size
)
118 return (coord
+ size
* 1024) % size
;
123 * Apply texture coord wrapping mode and return integer texture indexes
124 * for a vector of four texcoords (S or T or P).
125 * \param wrapMode PIPE_TEX_WRAP_x
126 * \param s the incoming texcoords
127 * \param size the texture image size
128 * \param icoord returns the integer texcoords
129 * \return integer texture index
132 wrap_nearest_repeat(float s
, unsigned size
, int *icoord
)
134 /* s limited to [0,1) */
135 /* i limited to [0,size-1] */
136 int i
= util_ifloor(s
* size
);
137 *icoord
= repeat(i
, size
);
142 wrap_nearest_clamp(float s
, unsigned size
, int *icoord
)
144 /* s limited to [0,1] */
145 /* i limited to [0,size-1] */
151 *icoord
= util_ifloor(s
* size
);
156 wrap_nearest_clamp_to_edge(float s
, unsigned size
, int *icoord
)
158 /* s limited to [min,max] */
159 /* i limited to [0, size-1] */
160 const float min
= 1.0F
/ (2.0F
* size
);
161 const float max
= 1.0F
- min
;
167 *icoord
= util_ifloor(s
* size
);
172 wrap_nearest_clamp_to_border(float s
, unsigned size
, int *icoord
)
174 /* s limited to [min,max] */
175 /* i limited to [-1, size] */
176 const float min
= -1.0F
/ (2.0F
* size
);
177 const float max
= 1.0F
- min
;
183 *icoord
= util_ifloor(s
* size
);
188 wrap_nearest_mirror_repeat(float s
, unsigned size
, int *icoord
)
190 const float min
= 1.0F
/ (2.0F
* size
);
191 const float max
= 1.0F
- min
;
192 const int flr
= util_ifloor(s
);
201 *icoord
= util_ifloor(u
* size
);
206 wrap_nearest_mirror_clamp(float s
, unsigned size
, int *icoord
)
208 /* s limited to [0,1] */
209 /* i limited to [0,size-1] */
210 const float u
= fabsf(s
);
216 *icoord
= util_ifloor(u
* size
);
221 wrap_nearest_mirror_clamp_to_edge(float s
, unsigned size
, int *icoord
)
223 /* s limited to [min,max] */
224 /* i limited to [0, size-1] */
225 const float min
= 1.0F
/ (2.0F
* size
);
226 const float max
= 1.0F
- min
;
227 const float u
= fabsf(s
);
233 *icoord
= util_ifloor(u
* size
);
238 wrap_nearest_mirror_clamp_to_border(float s
, unsigned size
, int *icoord
)
240 /* s limited to [min,max] */
241 /* i limited to [0, size-1] */
242 const float min
= -1.0F
/ (2.0F
* size
);
243 const float max
= 1.0F
- min
;
244 const float u
= fabsf(s
);
250 *icoord
= util_ifloor(u
* size
);
255 * Used to compute texel locations for linear sampling
256 * \param wrapMode PIPE_TEX_WRAP_x
257 * \param s the texcoord
258 * \param size the texture image size
259 * \param icoord0 returns first texture index
260 * \param icoord1 returns second texture index (usually icoord0 + 1)
261 * \param w returns blend factor/weight between texture indices
262 * \param icoord returns the computed integer texture coord
265 wrap_linear_repeat(float s
, unsigned size
,
266 int *icoord0
, int *icoord1
, float *w
)
268 float u
= s
* size
- 0.5F
;
269 *icoord0
= repeat(util_ifloor(u
), size
);
270 *icoord1
= repeat(*icoord0
+ 1, size
);
276 wrap_linear_clamp(float s
, unsigned size
,
277 int *icoord0
, int *icoord1
, float *w
)
279 float u
= CLAMP(s
, 0.0F
, 1.0F
);
281 *icoord0
= util_ifloor(u
);
282 *icoord1
= *icoord0
+ 1;
288 wrap_linear_clamp_to_edge(float s
, unsigned size
,
289 int *icoord0
, int *icoord1
, float *w
)
291 float u
= CLAMP(s
, 0.0F
, 1.0F
);
293 *icoord0
= util_ifloor(u
);
294 *icoord1
= *icoord0
+ 1;
297 if (*icoord1
>= (int) size
)
304 wrap_linear_clamp_to_border(float s
, unsigned size
,
305 int *icoord0
, int *icoord1
, float *w
)
307 const float min
= -1.0F
/ (2.0F
* size
);
308 const float max
= 1.0F
- min
;
309 float u
= CLAMP(s
, min
, max
);
311 *icoord0
= util_ifloor(u
);
312 *icoord1
= *icoord0
+ 1;
318 wrap_linear_mirror_repeat(float s
, unsigned size
,
319 int *icoord0
, int *icoord1
, float *w
)
321 const int flr
= util_ifloor(s
);
326 *icoord0
= util_ifloor(u
);
327 *icoord1
= *icoord0
+ 1;
330 if (*icoord1
>= (int) size
)
337 wrap_linear_mirror_clamp(float s
, unsigned size
,
338 int *icoord0
, int *icoord1
, float *w
)
346 *icoord0
= util_ifloor(u
);
347 *icoord1
= *icoord0
+ 1;
353 wrap_linear_mirror_clamp_to_edge(float s
, unsigned size
,
354 int *icoord0
, int *icoord1
, float *w
)
362 *icoord0
= util_ifloor(u
);
363 *icoord1
= *icoord0
+ 1;
366 if (*icoord1
>= (int) size
)
373 wrap_linear_mirror_clamp_to_border(float s
, unsigned size
,
374 int *icoord0
, int *icoord1
, float *w
)
376 const float min
= -1.0F
/ (2.0F
* size
);
377 const float max
= 1.0F
- min
;
386 *icoord0
= util_ifloor(u
);
387 *icoord1
= *icoord0
+ 1;
393 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
396 wrap_nearest_unorm_clamp(float s
, unsigned size
, int *icoord
)
398 int i
= util_ifloor(s
);
399 *icoord
= CLAMP(i
, 0, (int) size
-1);
404 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
407 wrap_nearest_unorm_clamp_to_border(float s
, unsigned size
, int *icoord
)
409 *icoord
= util_ifloor( CLAMP(s
, -0.5F
, (float) size
+ 0.5F
) );
414 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
417 wrap_nearest_unorm_clamp_to_edge(float s
, unsigned size
, int *icoord
)
419 *icoord
= util_ifloor( CLAMP(s
, 0.5F
, (float) size
- 0.5F
) );
424 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
427 wrap_linear_unorm_clamp(float s
, unsigned size
,
428 int *icoord0
, int *icoord1
, float *w
)
430 /* Not exactly what the spec says, but it matches NVIDIA output */
431 float u
= CLAMP(s
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
432 *icoord0
= util_ifloor(u
);
433 *icoord1
= *icoord0
+ 1;
439 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
442 wrap_linear_unorm_clamp_to_border(float s
, unsigned size
,
443 int *icoord0
, int *icoord1
, float *w
)
445 float u
= CLAMP(s
, -0.5F
, (float) size
+ 0.5F
);
447 *icoord0
= util_ifloor(u
);
448 *icoord1
= *icoord0
+ 1;
449 if (*icoord1
> (int) size
- 1)
456 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
459 wrap_linear_unorm_clamp_to_edge(float s
, unsigned size
,
460 int *icoord0
, int *icoord1
, float *w
)
462 float u
= CLAMP(s
, +0.5F
, (float) size
- 0.5F
);
464 *icoord0
= util_ifloor(u
);
465 *icoord1
= *icoord0
+ 1;
466 if (*icoord1
> (int) size
- 1)
473 * Do coordinate to array index conversion. For array textures.
476 wrap_array_layer(float coord
, unsigned size
, int *layer
)
478 int c
= util_ifloor(coord
+ 0.5F
);
479 *layer
= CLAMP(c
, 0, size
- 1);
484 * Examine the quad's texture coordinates to compute the partial
485 * derivatives w.r.t X and Y, then compute lambda (level of detail).
488 compute_lambda_1d(const struct sp_sampler_variant
*samp
,
489 const float s
[TGSI_QUAD_SIZE
],
490 const float t
[TGSI_QUAD_SIZE
],
491 const float p
[TGSI_QUAD_SIZE
])
493 const struct pipe_resource
*texture
= samp
->view
->texture
;
494 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
495 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
496 float rho
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
498 return util_fast_log2(rho
);
503 compute_lambda_2d(const struct sp_sampler_variant
*samp
,
504 const float s
[TGSI_QUAD_SIZE
],
505 const float t
[TGSI_QUAD_SIZE
],
506 const float p
[TGSI_QUAD_SIZE
])
508 const struct pipe_resource
*texture
= samp
->view
->texture
;
509 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
510 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
511 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
512 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
513 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
514 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
515 float rho
= MAX2(maxx
, maxy
);
517 return util_fast_log2(rho
);
522 compute_lambda_3d(const struct sp_sampler_variant
*samp
,
523 const float s
[TGSI_QUAD_SIZE
],
524 const float t
[TGSI_QUAD_SIZE
],
525 const float p
[TGSI_QUAD_SIZE
])
527 const struct pipe_resource
*texture
= samp
->view
->texture
;
528 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
529 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
530 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
531 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
532 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
533 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
534 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
535 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
536 float maxz
= MAX2(dpdx
, dpdy
) * u_minify(texture
->depth0
, samp
->view
->u
.tex
.first_level
);
539 rho
= MAX2(maxx
, maxy
);
540 rho
= MAX2(rho
, maxz
);
542 return util_fast_log2(rho
);
547 * Compute lambda for a vertex texture sampler.
548 * Since there aren't derivatives to use, just return 0.
551 compute_lambda_vert(const struct sp_sampler_variant
*samp
,
552 const float s
[TGSI_QUAD_SIZE
],
553 const float t
[TGSI_QUAD_SIZE
],
554 const float p
[TGSI_QUAD_SIZE
])
562 * Get a texel from a texture, using the texture tile cache.
564 * \param addr the template tex address containing cube, z, face info.
565 * \param x the x coord of texel within 2D image
566 * \param y the y coord of texel within 2D image
567 * \param rgba the quad to put the texel/color into
569 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
570 * sp_get_cached_tile_tex() function.
576 static INLINE
const float *
577 get_texel_2d_no_border(const struct sp_sampler_variant
*samp
,
578 union tex_tile_address addr
, int x
, int y
)
580 const struct softpipe_tex_cached_tile
*tile
;
582 addr
.bits
.x
= x
/ TILE_SIZE
;
583 addr
.bits
.y
= y
/ TILE_SIZE
;
587 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
589 return &tile
->data
.color
[y
][x
][0];
593 static INLINE
const float *
594 get_texel_2d(const struct sp_sampler_variant
*samp
,
595 union tex_tile_address addr
, int x
, int y
)
597 const struct pipe_resource
*texture
= samp
->view
->texture
;
598 unsigned level
= addr
.bits
.level
;
600 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
601 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
602 return samp
->sampler
->border_color
.f
;
605 return get_texel_2d_no_border( samp
, addr
, x
, y
);
610 /* Gather a quad of adjacent texels within a tile:
613 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant
*samp
,
614 union tex_tile_address addr
,
615 unsigned x
, unsigned y
,
618 const struct softpipe_tex_cached_tile
*tile
;
620 addr
.bits
.x
= x
/ TILE_SIZE
;
621 addr
.bits
.y
= y
/ TILE_SIZE
;
625 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
627 out
[0] = &tile
->data
.color
[y
][x
][0];
628 out
[1] = &tile
->data
.color
[y
][x
+1][0];
629 out
[2] = &tile
->data
.color
[y
+1][x
][0];
630 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
634 /* Gather a quad of potentially non-adjacent texels:
637 get_texel_quad_2d_no_border(const struct sp_sampler_variant
*samp
,
638 union tex_tile_address addr
,
643 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
644 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
645 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
646 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
649 /* Can involve a lot of unnecessary checks for border color:
652 get_texel_quad_2d(const struct sp_sampler_variant
*samp
,
653 union tex_tile_address addr
,
658 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
659 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
660 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
661 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
668 static INLINE
const float *
669 get_texel_3d_no_border(const struct sp_sampler_variant
*samp
,
670 union tex_tile_address addr
, int x
, int y
, int z
)
672 const struct softpipe_tex_cached_tile
*tile
;
674 addr
.bits
.x
= x
/ TILE_SIZE
;
675 addr
.bits
.y
= y
/ TILE_SIZE
;
680 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
682 return &tile
->data
.color
[y
][x
][0];
686 static INLINE
const float *
687 get_texel_3d(const struct sp_sampler_variant
*samp
,
688 union tex_tile_address addr
, int x
, int y
, int z
)
690 const struct pipe_resource
*texture
= samp
->view
->texture
;
691 unsigned level
= addr
.bits
.level
;
693 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
694 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
695 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
696 return samp
->sampler
->border_color
.f
;
699 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
704 /* Get texel pointer for 1D array texture */
705 static INLINE
const float *
706 get_texel_1d_array(const struct sp_sampler_variant
*samp
,
707 union tex_tile_address addr
, int x
, int y
)
709 const struct pipe_resource
*texture
= samp
->view
->texture
;
710 unsigned level
= addr
.bits
.level
;
712 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
713 return samp
->sampler
->border_color
.f
;
716 return get_texel_2d_no_border(samp
, addr
, x
, y
);
721 /* Get texel pointer for 2D array texture */
722 static INLINE
const float *
723 get_texel_2d_array(const struct sp_sampler_variant
*samp
,
724 union tex_tile_address addr
, int x
, int y
, int layer
)
726 const struct pipe_resource
*texture
= samp
->view
->texture
;
727 unsigned level
= addr
.bits
.level
;
729 assert(layer
< (int) texture
->array_size
);
732 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
733 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
734 return samp
->sampler
->border_color
.f
;
737 return get_texel_3d_no_border(samp
, addr
, x
, y
, layer
);
743 * Given the logbase2 of a mipmap's base level size and a mipmap level,
744 * return the size (in texels) of that mipmap level.
745 * For example, if level[0].width = 256 then base_pot will be 8.
746 * If level = 2, then we'll return 64 (the width at level=2).
747 * Return 1 if level > base_pot.
749 static INLINE
unsigned
750 pot_level_size(unsigned base_pot
, unsigned level
)
752 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
757 print_sample(const char *function
, const float *rgba
)
759 debug_printf("%s %g %g %g %g\n",
761 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
766 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
768 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
770 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
771 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
772 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
773 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
776 /* Some image-filter fastpaths:
779 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
785 enum tgsi_sampler_control control
,
788 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
789 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
790 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
791 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
792 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
793 union tex_tile_address addr
;
796 float u
= s
* xpot
- 0.5F
;
797 float v
= t
* ypot
- 0.5F
;
799 int uflr
= util_ifloor(u
);
800 int vflr
= util_ifloor(v
);
802 float xw
= u
- (float)uflr
;
803 float yw
= v
- (float)vflr
;
805 int x0
= uflr
& (xpot
- 1);
806 int y0
= vflr
& (ypot
- 1);
811 addr
.bits
.level
= level
;
813 /* Can we fetch all four at once:
815 if (x0
< xmax
&& y0
< ymax
) {
816 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
819 unsigned x1
= (x0
+ 1) & (xpot
- 1);
820 unsigned y1
= (y0
+ 1) & (ypot
- 1);
821 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
824 /* interpolate R, G, B, A */
825 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++) {
826 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
832 print_sample(__FUNCTION__
, rgba
);
838 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
844 enum tgsi_sampler_control control
,
845 float rgba
[TGSI_QUAD_SIZE
])
847 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
848 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
849 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
851 union tex_tile_address addr
;
857 int uflr
= util_ifloor(u
);
858 int vflr
= util_ifloor(v
);
860 int x0
= uflr
& (xpot
- 1);
861 int y0
= vflr
& (ypot
- 1);
864 addr
.bits
.level
= level
;
866 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
867 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
868 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
871 print_sample(__FUNCTION__
, rgba
);
877 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
883 enum tgsi_sampler_control control
,
884 float rgba
[TGSI_QUAD_SIZE
])
886 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
887 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
888 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
889 union tex_tile_address addr
;
899 addr
.bits
.level
= level
;
904 else if (x0
> xpot
- 1)
910 else if (y0
> ypot
- 1)
913 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
914 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
915 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
918 print_sample(__FUNCTION__
, rgba
);
924 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
930 enum tgsi_sampler_control control
,
931 float rgba
[TGSI_QUAD_SIZE
])
933 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
934 const struct pipe_resource
*texture
= samp
->view
->texture
;
937 union tex_tile_address addr
;
941 width
= u_minify(texture
->width0
, level
);
946 addr
.bits
.level
= level
;
948 samp
->nearest_texcoord_s(s
, width
, &x
);
950 out
= get_texel_2d(samp
, addr
, x
, 0);
951 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
952 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
955 print_sample(__FUNCTION__
, rgba
);
961 img_filter_1d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
967 enum tgsi_sampler_control control
,
970 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
971 const struct pipe_resource
*texture
= samp
->view
->texture
;
974 union tex_tile_address addr
;
978 width
= u_minify(texture
->width0
, level
);
983 addr
.bits
.level
= level
;
985 samp
->nearest_texcoord_s(s
, width
, &x
);
986 wrap_array_layer(t
, texture
->array_size
, &layer
);
988 out
= get_texel_1d_array(samp
, addr
, x
, layer
);
989 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
990 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
993 print_sample(__FUNCTION__
, rgba
);
999 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1005 enum tgsi_sampler_control control
,
1008 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1009 const struct pipe_resource
*texture
= samp
->view
->texture
;
1012 union tex_tile_address addr
;
1016 width
= u_minify(texture
->width0
, level
);
1017 height
= u_minify(texture
->height0
, level
);
1023 addr
.bits
.level
= level
;
1025 samp
->nearest_texcoord_s(s
, width
, &x
);
1026 samp
->nearest_texcoord_t(t
, height
, &y
);
1028 out
= get_texel_2d(samp
, addr
, x
, y
);
1029 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1030 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1033 print_sample(__FUNCTION__
, rgba
);
1039 img_filter_2d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1045 enum tgsi_sampler_control control
,
1048 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1049 const struct pipe_resource
*texture
= samp
->view
->texture
;
1052 union tex_tile_address addr
;
1056 width
= u_minify(texture
->width0
, level
);
1057 height
= u_minify(texture
->height0
, level
);
1063 addr
.bits
.level
= level
;
1065 samp
->nearest_texcoord_s(s
, width
, &x
);
1066 samp
->nearest_texcoord_t(t
, height
, &y
);
1067 wrap_array_layer(p
, texture
->array_size
, &layer
);
1069 out
= get_texel_2d_array(samp
, addr
, x
, y
, layer
);
1070 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1071 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1074 print_sample(__FUNCTION__
, rgba
);
1079 static INLINE
union tex_tile_address
1080 face(union tex_tile_address addr
, unsigned face
)
1082 addr
.bits
.face
= face
;
1088 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1094 enum tgsi_sampler_control control
,
1097 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1098 const struct pipe_resource
*texture
= samp
->view
->texture
;
1101 union tex_tile_address addr
;
1105 width
= u_minify(texture
->width0
, level
);
1106 height
= u_minify(texture
->height0
, level
);
1112 addr
.bits
.level
= level
;
1114 samp
->nearest_texcoord_s(s
, width
, &x
);
1115 samp
->nearest_texcoord_t(t
, height
, &y
);
1117 out
= get_texel_2d(samp
, face(addr
, face_id
), x
, y
);
1118 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1119 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1122 print_sample(__FUNCTION__
, rgba
);
1128 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1134 enum tgsi_sampler_control control
,
1137 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1138 const struct pipe_resource
*texture
= samp
->view
->texture
;
1139 int width
, height
, depth
;
1141 union tex_tile_address addr
;
1145 width
= u_minify(texture
->width0
, level
);
1146 height
= u_minify(texture
->height0
, level
);
1147 depth
= u_minify(texture
->depth0
, level
);
1153 samp
->nearest_texcoord_s(s
, width
, &x
);
1154 samp
->nearest_texcoord_t(t
, height
, &y
);
1155 samp
->nearest_texcoord_p(p
, depth
, &z
);
1158 addr
.bits
.level
= level
;
1160 out
= get_texel_3d(samp
, addr
, x
, y
, z
);
1161 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1162 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1167 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1173 enum tgsi_sampler_control control
,
1176 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1177 const struct pipe_resource
*texture
= samp
->view
->texture
;
1180 float xw
; /* weights */
1181 union tex_tile_address addr
;
1182 const float *tx0
, *tx1
;
1185 width
= u_minify(texture
->width0
, level
);
1190 addr
.bits
.level
= level
;
1192 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1194 tx0
= get_texel_2d(samp
, addr
, x0
, 0);
1195 tx1
= get_texel_2d(samp
, addr
, x1
, 0);
1197 /* interpolate R, G, B, A */
1198 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1199 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1204 img_filter_1d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1210 enum tgsi_sampler_control control
,
1213 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1214 const struct pipe_resource
*texture
= samp
->view
->texture
;
1217 float xw
; /* weights */
1218 union tex_tile_address addr
;
1219 const float *tx0
, *tx1
;
1222 width
= u_minify(texture
->width0
, level
);
1227 addr
.bits
.level
= level
;
1229 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1230 wrap_array_layer(t
, texture
->array_size
, &layer
);
1232 tx0
= get_texel_1d_array(samp
, addr
, x0
, layer
);
1233 tx1
= get_texel_1d_array(samp
, addr
, x1
, layer
);
1235 /* interpolate R, G, B, A */
1236 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1237 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1242 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1248 enum tgsi_sampler_control control
,
1251 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1252 const struct pipe_resource
*texture
= samp
->view
->texture
;
1255 float xw
, yw
; /* weights */
1256 union tex_tile_address addr
;
1257 const float *tx0
, *tx1
, *tx2
, *tx3
;
1260 width
= u_minify(texture
->width0
, level
);
1261 height
= u_minify(texture
->height0
, level
);
1267 addr
.bits
.level
= level
;
1269 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1270 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1272 tx0
= get_texel_2d(samp
, addr
, x0
, y0
);
1273 tx1
= get_texel_2d(samp
, addr
, x1
, y0
);
1274 tx2
= get_texel_2d(samp
, addr
, x0
, y1
);
1275 tx3
= get_texel_2d(samp
, addr
, x1
, y1
);
1277 /* interpolate R, G, B, A */
1278 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1279 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1286 img_filter_2d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1292 enum tgsi_sampler_control control
,
1295 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1296 const struct pipe_resource
*texture
= samp
->view
->texture
;
1298 int x0
, y0
, x1
, y1
, layer
;
1299 float xw
, yw
; /* weights */
1300 union tex_tile_address addr
;
1301 const float *tx0
, *tx1
, *tx2
, *tx3
;
1304 width
= u_minify(texture
->width0
, level
);
1305 height
= u_minify(texture
->height0
, level
);
1311 addr
.bits
.level
= level
;
1313 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1314 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1315 wrap_array_layer(p
, texture
->array_size
, &layer
);
1317 tx0
= get_texel_2d_array(samp
, addr
, x0
, y0
, layer
);
1318 tx1
= get_texel_2d_array(samp
, addr
, x1
, y0
, layer
);
1319 tx2
= get_texel_2d_array(samp
, addr
, x0
, y1
, layer
);
1320 tx3
= get_texel_2d_array(samp
, addr
, x1
, y1
, layer
);
1322 /* interpolate R, G, B, A */
1323 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1324 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1331 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1337 enum tgsi_sampler_control control
,
1340 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1341 const struct pipe_resource
*texture
= samp
->view
->texture
;
1344 float xw
, yw
; /* weights */
1345 union tex_tile_address addr
, addrj
;
1346 const float *tx0
, *tx1
, *tx2
, *tx3
;
1349 width
= u_minify(texture
->width0
, level
);
1350 height
= u_minify(texture
->height0
, level
);
1356 addr
.bits
.level
= level
;
1358 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1359 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1361 addrj
= face(addr
, face_id
);
1362 tx0
= get_texel_2d(samp
, addrj
, x0
, y0
);
1363 tx1
= get_texel_2d(samp
, addrj
, x1
, y0
);
1364 tx2
= get_texel_2d(samp
, addrj
, x0
, y1
);
1365 tx3
= get_texel_2d(samp
, addrj
, x1
, y1
);
1367 /* interpolate R, G, B, A */
1368 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1369 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1376 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1382 enum tgsi_sampler_control control
,
1385 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1386 const struct pipe_resource
*texture
= samp
->view
->texture
;
1387 int width
, height
, depth
;
1388 int x0
, x1
, y0
, y1
, z0
, z1
;
1389 float xw
, yw
, zw
; /* interpolation weights */
1390 union tex_tile_address addr
;
1391 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1394 width
= u_minify(texture
->width0
, level
);
1395 height
= u_minify(texture
->height0
, level
);
1396 depth
= u_minify(texture
->depth0
, level
);
1399 addr
.bits
.level
= level
;
1405 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1406 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1407 samp
->linear_texcoord_p(p
, depth
, &z0
, &z1
, &zw
);
1410 tx00
= get_texel_3d(samp
, addr
, x0
, y0
, z0
);
1411 tx01
= get_texel_3d(samp
, addr
, x1
, y0
, z0
);
1412 tx02
= get_texel_3d(samp
, addr
, x0
, y1
, z0
);
1413 tx03
= get_texel_3d(samp
, addr
, x1
, y1
, z0
);
1415 tx10
= get_texel_3d(samp
, addr
, x0
, y0
, z1
);
1416 tx11
= get_texel_3d(samp
, addr
, x1
, y0
, z1
);
1417 tx12
= get_texel_3d(samp
, addr
, x0
, y1
, z1
);
1418 tx13
= get_texel_3d(samp
, addr
, x1
, y1
, z1
);
1420 /* interpolate R, G, B, A */
1421 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1422 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1430 /* Calculate level of detail for every fragment.
1431 * Note that lambda has already been biased by global LOD bias.
1434 compute_lod(const struct pipe_sampler_state
*sampler
,
1435 const float biased_lambda
,
1436 const float lodbias
[TGSI_QUAD_SIZE
],
1437 float lod
[TGSI_QUAD_SIZE
])
1441 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1442 lod
[i
] = biased_lambda
+ lodbias
[i
];
1443 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1449 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1450 const float s
[TGSI_QUAD_SIZE
],
1451 const float t
[TGSI_QUAD_SIZE
],
1452 const float p
[TGSI_QUAD_SIZE
],
1453 const float c0
[TGSI_QUAD_SIZE
],
1454 enum tgsi_sampler_control control
,
1455 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1457 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1458 const struct pipe_resource
*texture
= samp
->view
->texture
;
1460 float lod
[TGSI_QUAD_SIZE
];
1462 if (control
== tgsi_sampler_lod_bias
) {
1463 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1464 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1466 assert(control
== tgsi_sampler_lod_explicit
);
1468 memcpy(lod
, c0
, sizeof(lod
));
1471 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1472 int level0
= samp
->view
->u
.tex
.first_level
+ (int)lod
[j
];
1475 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1477 else if (level0
>= texture
->last_level
)
1478 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1481 float levelBlend
= frac(lod
[j
]);
1482 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1485 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][0]);
1486 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
+1, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][1]);
1488 for (c
= 0; c
< 4; c
++) {
1489 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1495 print_sample_4(__FUNCTION__
, rgba
);
1501 * Compute nearest mipmap level from texcoords.
1502 * Then sample the texture level for four elements of a quad.
1503 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1506 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1507 const float s
[TGSI_QUAD_SIZE
],
1508 const float t
[TGSI_QUAD_SIZE
],
1509 const float p
[TGSI_QUAD_SIZE
],
1510 const float c0
[TGSI_QUAD_SIZE
],
1511 enum tgsi_sampler_control control
,
1512 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1514 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1515 const struct pipe_resource
*texture
= samp
->view
->texture
;
1516 float lod
[TGSI_QUAD_SIZE
];
1519 if (control
== tgsi_sampler_lod_bias
) {
1520 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1521 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1523 assert(control
== tgsi_sampler_lod_explicit
);
1525 memcpy(lod
, c0
, sizeof(lod
));
1528 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1530 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1532 float level
= samp
->view
->u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
) ;
1533 level
= MIN2(level
, (int)texture
->last_level
);
1534 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1539 print_sample_4(__FUNCTION__
, rgba
);
1545 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1546 const float s
[TGSI_QUAD_SIZE
],
1547 const float t
[TGSI_QUAD_SIZE
],
1548 const float p
[TGSI_QUAD_SIZE
],
1549 const float c0
[TGSI_QUAD_SIZE
],
1550 enum tgsi_sampler_control control
,
1551 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1553 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1554 float lod
[TGSI_QUAD_SIZE
];
1557 if (control
== tgsi_sampler_lod_bias
) {
1558 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1559 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1561 assert(control
== tgsi_sampler_lod_explicit
);
1563 memcpy(lod
, c0
, sizeof(lod
));
1566 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1568 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1571 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1578 mip_filter_none_no_filter_select(struct tgsi_sampler
*tgsi_sampler
,
1579 const float s
[TGSI_QUAD_SIZE
],
1580 const float t
[TGSI_QUAD_SIZE
],
1581 const float p
[TGSI_QUAD_SIZE
],
1582 const float c0
[TGSI_QUAD_SIZE
],
1583 enum tgsi_sampler_control control
,
1584 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1586 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1589 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
1590 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1594 /* For anisotropic filtering */
1595 #define WEIGHT_LUT_SIZE 1024
1597 static float *weightLut
= NULL
;
1600 * Creates the look-up table used to speed-up EWA sampling
1603 create_filter_table(void)
1607 weightLut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
1609 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1611 float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
1612 float weight
= (float) exp(-alpha
* r2
);
1613 weightLut
[i
] = weight
;
1620 * Elliptical weighted average (EWA) filter for producing high quality
1621 * anisotropic filtered results.
1622 * Based on the Higher Quality Elliptical Weighted Average Filter
1623 * published by Paul S. Heckbert in his Master's Thesis
1624 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1627 img_filter_2d_ewa(struct tgsi_sampler
*tgsi_sampler
,
1628 const float s
[TGSI_QUAD_SIZE
],
1629 const float t
[TGSI_QUAD_SIZE
],
1630 const float p
[TGSI_QUAD_SIZE
],
1632 enum tgsi_sampler_control control
,
1633 const float dudx
, const float dvdx
,
1634 const float dudy
, const float dvdy
,
1635 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1637 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1638 const struct pipe_resource
*texture
= samp
->view
->texture
;
1640 // ??? Won't the image filters blow up if level is negative?
1641 unsigned level0
= level
> 0 ? level
: 0;
1642 float scaling
= 1.0 / (1 << level0
);
1643 int width
= u_minify(texture
->width0
, level0
);
1644 int height
= u_minify(texture
->height0
, level0
);
1646 float ux
= dudx
* scaling
;
1647 float vx
= dvdx
* scaling
;
1648 float uy
= dudy
* scaling
;
1649 float vy
= dvdy
* scaling
;
1651 /* compute ellipse coefficients to bound the region:
1652 * A*x*x + B*x*y + C*y*y = F.
1654 float A
= vx
*vx
+vy
*vy
+1;
1655 float B
= -2*(ux
*vx
+uy
*vy
);
1656 float C
= ux
*ux
+uy
*uy
+1;
1657 float F
= A
*C
-B
*B
/4.0;
1659 /* check if it is an ellipse */
1660 /* ASSERT(F > 0.0); */
1662 /* Compute the ellipse's (u,v) bounding box in texture space */
1663 float d
= -B
*B
+4.0*C
*A
;
1664 float box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1665 float box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1667 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1668 float s_buffer
[TGSI_QUAD_SIZE
];
1669 float t_buffer
[TGSI_QUAD_SIZE
];
1670 float weight_buffer
[TGSI_QUAD_SIZE
];
1671 unsigned buffer_next
;
1673 float den
; /* = 0.0F; */
1675 float U
; /* = u0 - tex_u; */
1678 /* Scale ellipse formula to directly index the Filter Lookup Table.
1679 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1681 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1685 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1687 /* For each quad, the du and dx values are the same and so the ellipse is
1688 * also the same. Note that texel/image access can only be performed using
1689 * a quad, i.e. it is not possible to get the pixel value for a single
1690 * tex coord. In order to have a better performance, the access is buffered
1691 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
1692 * full, then the pixel values are read from the image.
1696 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1697 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1698 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1699 * value, q, is less than F, we're inside the ellipse
1701 float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
1702 float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
1704 int u0
= (int) floorf(tex_u
- box_u
);
1705 int u1
= (int) ceilf(tex_u
+ box_u
);
1706 int v0
= (int) floorf(tex_v
- box_v
);
1707 int v1
= (int) ceilf(tex_v
+ box_v
);
1709 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1713 for (v
= v0
; v
<= v1
; ++v
) {
1714 float V
= v
- tex_v
;
1715 float dq
= A
* (2 * U
+ 1) + B
* V
;
1716 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1719 for (u
= u0
; u
<= u1
; ++u
) {
1720 /* Note that the ellipse has been pre-scaled so F =
1721 * WEIGHT_LUT_SIZE - 1
1723 if (q
< WEIGHT_LUT_SIZE
) {
1724 /* as a LUT is used, q must never be negative;
1725 * should not happen, though
1727 const int qClamped
= q
>= 0.0F
? q
: 0;
1728 float weight
= weightLut
[qClamped
];
1730 weight_buffer
[buffer_next
] = weight
;
1731 s_buffer
[buffer_next
] = u
/ ((float) width
);
1732 t_buffer
[buffer_next
] = v
/ ((float) height
);
1735 if (buffer_next
== TGSI_QUAD_SIZE
) {
1736 /* 4 texel coords are in the buffer -> read it now */
1738 /* it is assumed that samp->min_img_filter is set to
1739 * img_filter_2d_nearest or one of the
1740 * accelerated img_filter_2d_nearest_XXX functions.
1742 for (jj
= 0; jj
< buffer_next
; jj
++) {
1743 samp
->min_img_filter(tgsi_sampler
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
], level
, samp
->faces
[j
],
1744 tgsi_sampler_lod_bias
, &rgba_temp
[0][jj
]);
1745 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
1746 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
1747 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
1748 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
1761 /* if the tex coord buffer contains unread values, we will read
1764 if (buffer_next
> 0) {
1766 /* it is assumed that samp->min_img_filter is set to
1767 * img_filter_2d_nearest or one of the
1768 * accelerated img_filter_2d_nearest_XXX functions.
1770 for (jj
= 0; jj
< buffer_next
; jj
++) {
1771 samp
->min_img_filter(tgsi_sampler
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
], level
, samp
->faces
[j
],
1772 tgsi_sampler_lod_bias
, &rgba_temp
[0][jj
]);
1773 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
1774 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
1775 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
1776 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
1781 /* Reaching this place would mean that no pixels intersected
1782 * the ellipse. This should never happen because the filter
1783 * we use always intersects at least one pixel.
1790 /* not enough pixels in resampling, resort to direct interpolation */
1791 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level
, samp
->faces
[j
],
1792 tgsi_sampler_lod_bias
, &rgba_temp
[0][j
]);
1794 num
[0] = rgba_temp
[0][j
];
1795 num
[1] = rgba_temp
[1][j
];
1796 num
[2] = rgba_temp
[2][j
];
1797 num
[3] = rgba_temp
[3][j
];
1800 rgba
[0][j
] = num
[0] / den
;
1801 rgba
[1][j
] = num
[1] / den
;
1802 rgba
[2][j
] = num
[2] / den
;
1803 rgba
[3][j
] = num
[3] / den
;
1809 * Sample 2D texture using an anisotropic filter.
1812 mip_filter_linear_aniso(struct tgsi_sampler
*tgsi_sampler
,
1813 const float s
[TGSI_QUAD_SIZE
],
1814 const float t
[TGSI_QUAD_SIZE
],
1815 const float p
[TGSI_QUAD_SIZE
],
1816 const float c0
[TGSI_QUAD_SIZE
],
1817 enum tgsi_sampler_control control
,
1818 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1820 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1821 const struct pipe_resource
*texture
= samp
->view
->texture
;
1824 float lod
[TGSI_QUAD_SIZE
];
1826 float s_to_u
= u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
1827 float t_to_v
= u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
1828 float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
1829 float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
1830 float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
1831 float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
1833 if (control
== tgsi_sampler_lod_bias
) {
1834 /* note: instead of working with Px and Py, we will use the
1835 * squared length instead, to avoid sqrt.
1837 float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
1838 float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
1843 const float maxEccentricity
= samp
->sampler
->max_anisotropy
* samp
->sampler
->max_anisotropy
;
1854 /* if the eccentricity of the ellipse is too big, scale up the shorter
1855 * of the two vectors to limit the maximum amount of work per pixel
1858 if (e
> maxEccentricity
) {
1859 /* float s=e / maxEccentricity;
1863 Pmin2
= Pmax2
/ maxEccentricity
;
1866 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
1867 * this since 0.5*log(x) = log(sqrt(x))
1869 lambda
= 0.5F
* util_fast_log2(Pmin2
) + samp
->sampler
->lod_bias
;
1870 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1873 assert(control
== tgsi_sampler_lod_explicit
);
1875 memcpy(lod
, c0
, sizeof(lod
));
1878 /* XXX: Take into account all lod values.
1881 level0
= samp
->view
->u
.tex
.first_level
+ (int)lambda
;
1883 /* If the ellipse covers the whole image, we can
1884 * simply return the average of the whole image.
1886 if (level0
>= (int) texture
->last_level
) {
1888 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
1889 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1892 /* don't bother interpolating between multiple LODs; it doesn't
1893 * seem to be worth the extra running time.
1895 img_filter_2d_ewa(tgsi_sampler
, s
, t
, p
, level0
, tgsi_sampler_lod_bias
,
1896 dudx
, dvdx
, dudy
, dvdy
, rgba
);
1900 print_sample_4(__FUNCTION__
, rgba
);
1906 * Specialized version of mip_filter_linear with hard-wired calls to
1907 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1910 mip_filter_linear_2d_linear_repeat_POT(
1911 struct tgsi_sampler
*tgsi_sampler
,
1912 const float s
[TGSI_QUAD_SIZE
],
1913 const float t
[TGSI_QUAD_SIZE
],
1914 const float p
[TGSI_QUAD_SIZE
],
1915 const float c0
[TGSI_QUAD_SIZE
],
1916 enum tgsi_sampler_control control
,
1917 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1919 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1920 const struct pipe_resource
*texture
= samp
->view
->texture
;
1923 float lod
[TGSI_QUAD_SIZE
];
1925 if (control
== tgsi_sampler_lod_bias
) {
1926 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1927 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1929 assert(control
== tgsi_sampler_lod_explicit
);
1931 memcpy(lod
, c0
, sizeof(lod
));
1934 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1935 int level0
= samp
->view
->u
.tex
.first_level
+ (int)lod
[j
];
1937 /* Catches both negative and large values of level0:
1939 if ((unsigned)level0
>= texture
->last_level
) {
1941 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1943 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1947 float levelBlend
= frac(lod
[j
]);
1948 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1951 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][0]);
1952 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
+1, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][1]);
1954 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1955 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1960 print_sample_4(__FUNCTION__
, rgba
);
1966 * Do shadow/depth comparisons.
1969 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1970 const float s
[TGSI_QUAD_SIZE
],
1971 const float t
[TGSI_QUAD_SIZE
],
1972 const float p
[TGSI_QUAD_SIZE
],
1973 const float c0
[TGSI_QUAD_SIZE
],
1974 enum tgsi_sampler_control control
,
1975 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1977 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1978 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1979 int j
, k0
, k1
, k2
, k3
;
1981 float pc0
, pc1
, pc2
, pc3
;
1983 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1986 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1987 * for 2D Array texture we need to use the 'c0' (aka Q).
1988 * When we sampled the depth texture, the depth value was put into all
1989 * RGBA channels. We look at the red channel here.
1992 if (samp
->view
->texture
->target
== PIPE_TEXTURE_2D_ARRAY
||
1993 samp
->view
->texture
->target
== PIPE_TEXTURE_CUBE
) {
1994 pc0
= CLAMP(c0
[0], 0.0F
, 1.0F
);
1995 pc1
= CLAMP(c0
[1], 0.0F
, 1.0F
);
1996 pc2
= CLAMP(c0
[2], 0.0F
, 1.0F
);
1997 pc3
= CLAMP(c0
[3], 0.0F
, 1.0F
);
1999 pc0
= CLAMP(p
[0], 0.0F
, 1.0F
);
2000 pc1
= CLAMP(p
[1], 0.0F
, 1.0F
);
2001 pc2
= CLAMP(p
[2], 0.0F
, 1.0F
);
2002 pc3
= CLAMP(p
[3], 0.0F
, 1.0F
);
2004 /* compare four texcoords vs. four texture samples */
2005 switch (sampler
->compare_func
) {
2006 case PIPE_FUNC_LESS
:
2007 k0
= pc0
< rgba
[0][0];
2008 k1
= pc1
< rgba
[0][1];
2009 k2
= pc2
< rgba
[0][2];
2010 k3
= pc3
< rgba
[0][3];
2012 case PIPE_FUNC_LEQUAL
:
2013 k0
= pc0
<= rgba
[0][0];
2014 k1
= pc1
<= rgba
[0][1];
2015 k2
= pc2
<= rgba
[0][2];
2016 k3
= pc3
<= rgba
[0][3];
2018 case PIPE_FUNC_GREATER
:
2019 k0
= pc0
> rgba
[0][0];
2020 k1
= pc1
> rgba
[0][1];
2021 k2
= pc2
> rgba
[0][2];
2022 k3
= pc3
> rgba
[0][3];
2024 case PIPE_FUNC_GEQUAL
:
2025 k0
= pc0
>= rgba
[0][0];
2026 k1
= pc1
>= rgba
[0][1];
2027 k2
= pc2
>= rgba
[0][2];
2028 k3
= pc3
>= rgba
[0][3];
2030 case PIPE_FUNC_EQUAL
:
2031 k0
= pc0
== rgba
[0][0];
2032 k1
= pc1
== rgba
[0][1];
2033 k2
= pc2
== rgba
[0][2];
2034 k3
= pc3
== rgba
[0][3];
2036 case PIPE_FUNC_NOTEQUAL
:
2037 k0
= pc0
!= rgba
[0][0];
2038 k1
= pc1
!= rgba
[0][1];
2039 k2
= pc2
!= rgba
[0][2];
2040 k3
= pc3
!= rgba
[0][3];
2042 case PIPE_FUNC_ALWAYS
:
2043 k0
= k1
= k2
= k3
= 1;
2045 case PIPE_FUNC_NEVER
:
2046 k0
= k1
= k2
= k3
= 0;
2049 k0
= k1
= k2
= k3
= 0;
2054 if (sampler
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
) {
2055 /* convert four pass/fail values to an intensity in [0,1] */
2056 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
2058 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2059 for (j
= 0; j
< 4; j
++) {
2060 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
2064 for (j
= 0; j
< 4; j
++) {
2075 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
2076 * Put face info into the sampler faces[] array.
2079 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
2080 const float s
[TGSI_QUAD_SIZE
],
2081 const float t
[TGSI_QUAD_SIZE
],
2082 const float p
[TGSI_QUAD_SIZE
],
2083 const float c0
[TGSI_QUAD_SIZE
],
2084 enum tgsi_sampler_control control
,
2085 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2087 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2089 float ssss
[4], tttt
[4];
2091 /* Not actually used, but the intermediate steps that do the
2092 * dereferencing don't know it.
2094 static const float pppp
[4] = { 0, 0, 0, 0 };
2098 direction target sc tc ma
2099 ---------- ------------------------------- --- --- ---
2100 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2101 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2102 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2103 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2104 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2105 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2108 /* Choose the cube face and compute new s/t coords for the 2D face.
2110 * Use the same cube face for all four pixels in the quad.
2112 * This isn't ideal, but if we want to use a different cube face
2113 * per pixel in the quad, we'd have to also compute the per-face
2114 * LOD here too. That's because the four post-face-selection
2115 * texcoords are no longer related to each other (they're
2116 * per-face!) so we can't use subtraction to compute the partial
2117 * deriviates to compute the LOD. Doing so (near cube edges
2118 * anyway) gives us pretty much random values.
2121 /* use the average of the four pixel's texcoords to choose the face */
2122 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
2123 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
2124 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
2125 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
2127 if (arx
>= ary
&& arx
>= arz
) {
2128 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
2129 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
2130 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2131 const float ima
= -0.5F
/ fabsf(s
[j
]);
2132 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
2133 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2134 samp
->faces
[j
] = face
;
2137 else if (ary
>= arx
&& ary
>= arz
) {
2138 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
2139 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
2140 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2141 const float ima
= -0.5F
/ fabsf(t
[j
]);
2142 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
2143 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
2144 samp
->faces
[j
] = face
;
2148 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
2149 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
2150 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2151 const float ima
= -0.5F
/ fabsf(p
[j
]);
2152 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
2153 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2154 samp
->faces
[j
] = face
;
2159 /* In our little pipeline, the compare stage is next. If compare
2160 * is not active, this will point somewhere deeper into the
2161 * pipeline, eg. to mip_filter or even img_filter.
2163 samp
->compare(tgsi_sampler
, ssss
, tttt
, pppp
, c0
, control
, rgba
);
2168 do_swizzling(const struct sp_sampler_variant
*samp
,
2169 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2170 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2173 const unsigned swizzle_r
= samp
->key
.bits
.swizzle_r
;
2174 const unsigned swizzle_g
= samp
->key
.bits
.swizzle_g
;
2175 const unsigned swizzle_b
= samp
->key
.bits
.swizzle_b
;
2176 const unsigned swizzle_a
= samp
->key
.bits
.swizzle_a
;
2178 switch (swizzle_r
) {
2179 case PIPE_SWIZZLE_ZERO
:
2180 for (j
= 0; j
< 4; j
++)
2183 case PIPE_SWIZZLE_ONE
:
2184 for (j
= 0; j
< 4; j
++)
2188 assert(swizzle_r
< 4);
2189 for (j
= 0; j
< 4; j
++)
2190 out
[0][j
] = in
[swizzle_r
][j
];
2193 switch (swizzle_g
) {
2194 case PIPE_SWIZZLE_ZERO
:
2195 for (j
= 0; j
< 4; j
++)
2198 case PIPE_SWIZZLE_ONE
:
2199 for (j
= 0; j
< 4; j
++)
2203 assert(swizzle_g
< 4);
2204 for (j
= 0; j
< 4; j
++)
2205 out
[1][j
] = in
[swizzle_g
][j
];
2208 switch (swizzle_b
) {
2209 case PIPE_SWIZZLE_ZERO
:
2210 for (j
= 0; j
< 4; j
++)
2213 case PIPE_SWIZZLE_ONE
:
2214 for (j
= 0; j
< 4; j
++)
2218 assert(swizzle_b
< 4);
2219 for (j
= 0; j
< 4; j
++)
2220 out
[2][j
] = in
[swizzle_b
][j
];
2223 switch (swizzle_a
) {
2224 case PIPE_SWIZZLE_ZERO
:
2225 for (j
= 0; j
< 4; j
++)
2228 case PIPE_SWIZZLE_ONE
:
2229 for (j
= 0; j
< 4; j
++)
2233 assert(swizzle_a
< 4);
2234 for (j
= 0; j
< 4; j
++)
2235 out
[3][j
] = in
[swizzle_a
][j
];
2241 sample_swizzle(struct tgsi_sampler
*tgsi_sampler
,
2242 const float s
[TGSI_QUAD_SIZE
],
2243 const float t
[TGSI_QUAD_SIZE
],
2244 const float p
[TGSI_QUAD_SIZE
],
2245 const float c0
[TGSI_QUAD_SIZE
],
2246 enum tgsi_sampler_control control
,
2247 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2249 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2250 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2252 samp
->sample_target(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba_temp
);
2254 do_swizzling(samp
, rgba_temp
, rgba
);
2258 static wrap_nearest_func
2259 get_nearest_unorm_wrap(unsigned mode
)
2262 case PIPE_TEX_WRAP_CLAMP
:
2263 return wrap_nearest_unorm_clamp
;
2264 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2265 return wrap_nearest_unorm_clamp_to_edge
;
2266 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2267 return wrap_nearest_unorm_clamp_to_border
;
2270 return wrap_nearest_unorm_clamp
;
2275 static wrap_nearest_func
2276 get_nearest_wrap(unsigned mode
)
2279 case PIPE_TEX_WRAP_REPEAT
:
2280 return wrap_nearest_repeat
;
2281 case PIPE_TEX_WRAP_CLAMP
:
2282 return wrap_nearest_clamp
;
2283 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2284 return wrap_nearest_clamp_to_edge
;
2285 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2286 return wrap_nearest_clamp_to_border
;
2287 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2288 return wrap_nearest_mirror_repeat
;
2289 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2290 return wrap_nearest_mirror_clamp
;
2291 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2292 return wrap_nearest_mirror_clamp_to_edge
;
2293 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2294 return wrap_nearest_mirror_clamp_to_border
;
2297 return wrap_nearest_repeat
;
2302 static wrap_linear_func
2303 get_linear_unorm_wrap(unsigned mode
)
2306 case PIPE_TEX_WRAP_CLAMP
:
2307 return wrap_linear_unorm_clamp
;
2308 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2309 return wrap_linear_unorm_clamp_to_edge
;
2310 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2311 return wrap_linear_unorm_clamp_to_border
;
2314 return wrap_linear_unorm_clamp
;
2319 static wrap_linear_func
2320 get_linear_wrap(unsigned mode
)
2323 case PIPE_TEX_WRAP_REPEAT
:
2324 return wrap_linear_repeat
;
2325 case PIPE_TEX_WRAP_CLAMP
:
2326 return wrap_linear_clamp
;
2327 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2328 return wrap_linear_clamp_to_edge
;
2329 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2330 return wrap_linear_clamp_to_border
;
2331 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2332 return wrap_linear_mirror_repeat
;
2333 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2334 return wrap_linear_mirror_clamp
;
2335 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2336 return wrap_linear_mirror_clamp_to_edge
;
2337 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2338 return wrap_linear_mirror_clamp_to_border
;
2341 return wrap_linear_repeat
;
2347 * Is swizzling needed for the given state key?
2350 any_swizzle(union sp_sampler_key key
)
2352 return (key
.bits
.swizzle_r
!= PIPE_SWIZZLE_RED
||
2353 key
.bits
.swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2354 key
.bits
.swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2355 key
.bits
.swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
2359 static compute_lambda_func
2360 get_lambda_func(const union sp_sampler_key key
)
2362 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
2363 return compute_lambda_vert
;
2365 switch (key
.bits
.target
) {
2366 case PIPE_TEXTURE_1D
:
2367 case PIPE_TEXTURE_1D_ARRAY
:
2368 return compute_lambda_1d
;
2369 case PIPE_TEXTURE_2D
:
2370 case PIPE_TEXTURE_2D_ARRAY
:
2371 case PIPE_TEXTURE_RECT
:
2372 case PIPE_TEXTURE_CUBE
:
2373 return compute_lambda_2d
;
2374 case PIPE_TEXTURE_3D
:
2375 return compute_lambda_3d
;
2378 return compute_lambda_1d
;
2383 static img_filter_func
2384 get_img_filter(const union sp_sampler_key key
,
2386 const struct pipe_sampler_state
*sampler
)
2388 switch (key
.bits
.target
) {
2389 case PIPE_TEXTURE_1D
:
2390 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2391 return img_filter_1d_nearest
;
2393 return img_filter_1d_linear
;
2395 case PIPE_TEXTURE_1D_ARRAY
:
2396 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2397 return img_filter_1d_array_nearest
;
2399 return img_filter_1d_array_linear
;
2401 case PIPE_TEXTURE_2D
:
2402 case PIPE_TEXTURE_RECT
:
2403 /* Try for fast path:
2405 if (key
.bits
.is_pot
&&
2406 sampler
->wrap_s
== sampler
->wrap_t
&&
2407 sampler
->normalized_coords
)
2409 switch (sampler
->wrap_s
) {
2410 case PIPE_TEX_WRAP_REPEAT
:
2412 case PIPE_TEX_FILTER_NEAREST
:
2413 return img_filter_2d_nearest_repeat_POT
;
2414 case PIPE_TEX_FILTER_LINEAR
:
2415 return img_filter_2d_linear_repeat_POT
;
2420 case PIPE_TEX_WRAP_CLAMP
:
2422 case PIPE_TEX_FILTER_NEAREST
:
2423 return img_filter_2d_nearest_clamp_POT
;
2429 /* Otherwise use default versions:
2431 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2432 return img_filter_2d_nearest
;
2434 return img_filter_2d_linear
;
2436 case PIPE_TEXTURE_2D_ARRAY
:
2437 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2438 return img_filter_2d_array_nearest
;
2440 return img_filter_2d_array_linear
;
2442 case PIPE_TEXTURE_CUBE
:
2443 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2444 return img_filter_cube_nearest
;
2446 return img_filter_cube_linear
;
2448 case PIPE_TEXTURE_3D
:
2449 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2450 return img_filter_3d_nearest
;
2452 return img_filter_3d_linear
;
2456 return img_filter_1d_nearest
;
2462 * Bind the given texture object and texture cache to the sampler variant.
2465 sp_sampler_variant_bind_view( struct sp_sampler_variant
*samp
,
2466 struct softpipe_tex_tile_cache
*tex_cache
,
2467 const struct pipe_sampler_view
*view
)
2469 const struct pipe_resource
*texture
= view
->texture
;
2472 samp
->cache
= tex_cache
;
2473 samp
->xpot
= util_logbase2( texture
->width0
);
2474 samp
->ypot
= util_logbase2( texture
->height0
);
2479 sp_sampler_variant_destroy( struct sp_sampler_variant
*samp
)
2486 sample_get_dims(struct tgsi_sampler
*tgsi_sampler
, int level
,
2489 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2490 const struct pipe_sampler_view
*view
= samp
->view
;
2491 const struct pipe_resource
*texture
= view
->texture
;
2493 /* undefined according to EXT_gpu_program */
2494 level
+= view
->u
.tex
.first_level
;
2495 if (level
> view
->u
.tex
.last_level
)
2498 dims
[0] = u_minify(texture
->width0
, level
);
2500 switch(texture
->target
) {
2501 case PIPE_TEXTURE_1D_ARRAY
:
2502 dims
[1] = texture
->array_size
;
2504 case PIPE_TEXTURE_1D
:
2507 case PIPE_TEXTURE_2D_ARRAY
:
2508 dims
[2] = texture
->array_size
;
2510 case PIPE_TEXTURE_2D
:
2511 case PIPE_TEXTURE_CUBE
:
2512 case PIPE_TEXTURE_RECT
:
2513 dims
[1] = u_minify(texture
->height0
, level
);
2515 case PIPE_TEXTURE_3D
:
2516 dims
[1] = u_minify(texture
->height0
, level
);
2517 dims
[2] = u_minify(texture
->depth0
, level
);
2520 assert(!"unexpected texture target in sample_get_dims()");
2526 * This function is only used for getting unfiltered texels via the
2527 * TXF opcode. The GL spec says that out-of-bounds texel fetches
2528 * produce undefined results. Instead of crashing, lets just clamp
2529 * coords to the texture image size.
2532 sample_get_texels(struct tgsi_sampler
*tgsi_sampler
,
2533 const int v_i
[TGSI_QUAD_SIZE
],
2534 const int v_j
[TGSI_QUAD_SIZE
],
2535 const int v_k
[TGSI_QUAD_SIZE
],
2536 const int lod
[TGSI_QUAD_SIZE
],
2537 const int8_t offset
[3],
2538 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2540 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2541 union tex_tile_address addr
;
2542 const struct pipe_resource
*texture
= samp
->view
->texture
;
2545 const bool need_swizzle
= any_swizzle(samp
->key
);
2546 int width
, height
, depth
, layers
;
2549 /* TODO write a better test for LOD */
2550 addr
.bits
.level
= lod
[0];
2552 width
= u_minify(texture
->width0
, addr
.bits
.level
);
2553 height
= u_minify(texture
->height0
, addr
.bits
.level
);
2554 depth
= u_minify(texture
->depth0
, addr
.bits
.level
);
2555 layers
= texture
->array_size
;
2557 switch(texture
->target
) {
2558 case PIPE_TEXTURE_1D
:
2559 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2560 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2561 tx
= get_texel_2d(samp
, addr
, x
, 0);
2562 for (c
= 0; c
< 4; c
++) {
2567 case PIPE_TEXTURE_1D_ARRAY
:
2568 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2569 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2570 int y
= CLAMP(v_j
[j
], 0, layers
- 1);
2571 tx
= get_texel_1d_array(samp
, addr
, x
, y
);
2572 for (c
= 0; c
< 4; c
++) {
2577 case PIPE_TEXTURE_2D
:
2578 case PIPE_TEXTURE_RECT
:
2579 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2580 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2581 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2582 tx
= get_texel_2d(samp
, addr
, x
, y
);
2583 for (c
= 0; c
< 4; c
++) {
2588 case PIPE_TEXTURE_2D_ARRAY
:
2589 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2590 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2591 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2592 int layer
= CLAMP(v_k
[j
], 0, layers
- 1);
2593 tx
= get_texel_2d_array(samp
, addr
, x
, y
, layer
);
2594 for (c
= 0; c
< 4; c
++) {
2599 case PIPE_TEXTURE_3D
:
2600 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2601 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2602 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2603 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
2605 tx
= get_texel_3d(samp
, addr
, x
, y
, z
);
2606 for (c
= 0; c
< 4; c
++) {
2611 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
2613 assert(!"Unknown or CUBE texture type in TXF processing\n");
2618 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2619 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
2620 do_swizzling(samp
, rgba_temp
, rgba
);
2626 * Create a sampler variant for a given set of non-orthogonal state.
2628 struct sp_sampler_variant
*
2629 sp_create_sampler_variant( const struct pipe_sampler_state
*sampler
,
2630 const union sp_sampler_key key
)
2632 struct sp_sampler_variant
*samp
= CALLOC_STRUCT(sp_sampler_variant
);
2636 samp
->sampler
= sampler
;
2639 /* Note that (for instance) linear_texcoord_s and
2640 * nearest_texcoord_s may be active at the same time, if the
2641 * sampler min_img_filter differs from its mag_img_filter.
2643 if (sampler
->normalized_coords
) {
2644 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
2645 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
2646 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
2648 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
2649 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
2650 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
2653 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
2654 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
2655 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
2657 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
2658 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
2659 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
2662 samp
->compute_lambda
= get_lambda_func( key
);
2664 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
2665 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
2667 switch (sampler
->min_mip_filter
) {
2668 case PIPE_TEX_MIPFILTER_NONE
:
2669 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
2670 samp
->mip_filter
= mip_filter_none_no_filter_select
;
2672 samp
->mip_filter
= mip_filter_none
;
2675 case PIPE_TEX_MIPFILTER_NEAREST
:
2676 samp
->mip_filter
= mip_filter_nearest
;
2679 case PIPE_TEX_MIPFILTER_LINEAR
:
2680 if (key
.bits
.is_pot
&&
2681 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
2682 sampler
->normalized_coords
&&
2683 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
2684 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
2685 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
) {
2686 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2689 samp
->mip_filter
= mip_filter_linear
;
2692 /* Anisotropic filtering extension. */
2693 if (sampler
->max_anisotropy
> 1) {
2694 samp
->mip_filter
= mip_filter_linear_aniso
;
2696 /* Override min_img_filter:
2697 * min_img_filter needs to be set to NEAREST since we need to access
2698 * each texture pixel as it is and weight it later; using linear
2699 * filters will have incorrect results.
2700 * By setting the filter to NEAREST here, we can avoid calling the
2701 * generic img_filter_2d_nearest in the anisotropic filter function,
2702 * making it possible to use one of the accelerated implementations
2704 samp
->min_img_filter
= get_img_filter(key
, PIPE_TEX_FILTER_NEAREST
, sampler
);
2706 /* on first access create the lookup table containing the filter weights. */
2708 create_filter_table();
2715 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2716 samp
->compare
= sample_compare
;
2719 /* Skip compare operation by promoting the mip_filter function
2722 samp
->compare
= samp
->mip_filter
;
2725 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
2726 samp
->sample_target
= sample_cube
;
2734 /* Skip cube face determination by promoting the compare
2737 samp
->sample_target
= samp
->compare
;
2740 if (any_swizzle(key
)) {
2741 samp
->base
.get_samples
= sample_swizzle
;
2744 samp
->base
.get_samples
= samp
->sample_target
;
2747 samp
->base
.get_dims
= sample_get_dims
;
2748 samp
->base
.get_texel
= sample_get_texels
;