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(const float s
[4], unsigned size
, int icoord
[4])
135 /* s limited to [0,1) */
136 /* i limited to [0,size-1] */
137 for (ch
= 0; ch
< 4; ch
++) {
138 int i
= util_ifloor(s
[ch
] * size
);
139 icoord
[ch
] = repeat(i
, size
);
145 wrap_nearest_clamp(const float s
[4], unsigned size
, int icoord
[4])
148 /* s limited to [0,1] */
149 /* i limited to [0,size-1] */
150 for (ch
= 0; ch
< 4; ch
++) {
153 else if (s
[ch
] >= 1.0F
)
154 icoord
[ch
] = size
- 1;
156 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
162 wrap_nearest_clamp_to_edge(const float s
[4], unsigned size
, int icoord
[4])
165 /* s limited to [min,max] */
166 /* i limited to [0, size-1] */
167 const float min
= 1.0F
/ (2.0F
* size
);
168 const float max
= 1.0F
- min
;
169 for (ch
= 0; ch
< 4; ch
++) {
172 else if (s
[ch
] > max
)
173 icoord
[ch
] = size
- 1;
175 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
181 wrap_nearest_clamp_to_border(const float s
[4], unsigned size
, int icoord
[4])
184 /* s limited to [min,max] */
185 /* i limited to [-1, size] */
186 const float min
= -1.0F
/ (2.0F
* size
);
187 const float max
= 1.0F
- min
;
188 for (ch
= 0; ch
< 4; ch
++) {
191 else if (s
[ch
] >= max
)
194 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
200 wrap_nearest_mirror_repeat(const float s
[4], unsigned size
, int icoord
[4])
203 const float min
= 1.0F
/ (2.0F
* size
);
204 const float max
= 1.0F
- min
;
205 for (ch
= 0; ch
< 4; ch
++) {
206 const int flr
= util_ifloor(s
[ch
]);
207 float u
= frac(s
[ch
]);
213 icoord
[ch
] = size
- 1;
215 icoord
[ch
] = util_ifloor(u
* size
);
221 wrap_nearest_mirror_clamp(const float s
[4], unsigned size
, int icoord
[4])
224 for (ch
= 0; ch
< 4; ch
++) {
225 /* s limited to [0,1] */
226 /* i limited to [0,size-1] */
227 const float u
= fabsf(s
[ch
]);
231 icoord
[ch
] = size
- 1;
233 icoord
[ch
] = util_ifloor(u
* size
);
239 wrap_nearest_mirror_clamp_to_edge(const float s
[4], unsigned size
,
243 /* s limited to [min,max] */
244 /* i limited to [0, size-1] */
245 const float min
= 1.0F
/ (2.0F
* size
);
246 const float max
= 1.0F
- min
;
247 for (ch
= 0; ch
< 4; ch
++) {
248 const float u
= fabsf(s
[ch
]);
252 icoord
[ch
] = size
- 1;
254 icoord
[ch
] = util_ifloor(u
* size
);
260 wrap_nearest_mirror_clamp_to_border(const float s
[4], unsigned size
,
264 /* s limited to [min,max] */
265 /* i limited to [0, size-1] */
266 const float min
= -1.0F
/ (2.0F
* size
);
267 const float max
= 1.0F
- min
;
268 for (ch
= 0; ch
< 4; ch
++) {
269 const float u
= fabsf(s
[ch
]);
275 icoord
[ch
] = util_ifloor(u
* size
);
281 * Used to compute texel locations for linear sampling for four texcoords.
282 * \param wrapMode PIPE_TEX_WRAP_x
283 * \param s the texcoords
284 * \param size the texture image size
285 * \param icoord0 returns first texture indexes
286 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
287 * \param w returns blend factor/weight between texture indexes
288 * \param icoord returns the computed integer texture coords
291 wrap_linear_repeat(const float s
[4], unsigned size
,
292 int icoord0
[4], int icoord1
[4], float w
[4])
295 for (ch
= 0; ch
< 4; ch
++) {
296 float u
= s
[ch
] * size
- 0.5F
;
297 icoord0
[ch
] = repeat(util_ifloor(u
), size
);
298 icoord1
[ch
] = repeat(icoord0
[ch
] + 1, size
);
305 wrap_linear_clamp(const float s
[4], unsigned size
,
306 int icoord0
[4], int icoord1
[4], float w
[4])
309 for (ch
= 0; ch
< 4; ch
++) {
310 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
312 icoord0
[ch
] = util_ifloor(u
);
313 icoord1
[ch
] = icoord0
[ch
] + 1;
320 wrap_linear_clamp_to_edge(const float s
[4], unsigned size
,
321 int icoord0
[4], int icoord1
[4], float w
[4])
324 for (ch
= 0; ch
< 4; ch
++) {
325 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
327 icoord0
[ch
] = util_ifloor(u
);
328 icoord1
[ch
] = icoord0
[ch
] + 1;
331 if (icoord1
[ch
] >= (int) size
)
332 icoord1
[ch
] = size
- 1;
339 wrap_linear_clamp_to_border(const float s
[4], unsigned size
,
340 int icoord0
[4], int icoord1
[4], float w
[4])
342 const float min
= -1.0F
/ (2.0F
* size
);
343 const float max
= 1.0F
- min
;
345 for (ch
= 0; ch
< 4; ch
++) {
346 float u
= CLAMP(s
[ch
], min
, max
);
348 icoord0
[ch
] = util_ifloor(u
);
349 icoord1
[ch
] = icoord0
[ch
] + 1;
356 wrap_linear_mirror_repeat(const float s
[4], unsigned size
,
357 int icoord0
[4], int icoord1
[4], float w
[4])
360 for (ch
= 0; ch
< 4; ch
++) {
361 const int flr
= util_ifloor(s
[ch
]);
362 float u
= frac(s
[ch
]);
366 icoord0
[ch
] = util_ifloor(u
);
367 icoord1
[ch
] = icoord0
[ch
] + 1;
370 if (icoord1
[ch
] >= (int) size
)
371 icoord1
[ch
] = size
- 1;
378 wrap_linear_mirror_clamp(const float s
[4], unsigned size
,
379 int icoord0
[4], int icoord1
[4], float w
[4])
382 for (ch
= 0; ch
< 4; ch
++) {
383 float u
= fabsf(s
[ch
]);
389 icoord0
[ch
] = util_ifloor(u
);
390 icoord1
[ch
] = icoord0
[ch
] + 1;
397 wrap_linear_mirror_clamp_to_edge(const float s
[4], unsigned size
,
398 int icoord0
[4], int icoord1
[4], float w
[4])
401 for (ch
= 0; ch
< 4; ch
++) {
402 float u
= fabsf(s
[ch
]);
408 icoord0
[ch
] = util_ifloor(u
);
409 icoord1
[ch
] = icoord0
[ch
] + 1;
412 if (icoord1
[ch
] >= (int) size
)
413 icoord1
[ch
] = size
- 1;
420 wrap_linear_mirror_clamp_to_border(const float s
[4], unsigned size
,
421 int icoord0
[4], int icoord1
[4], float w
[4])
423 const float min
= -1.0F
/ (2.0F
* size
);
424 const float max
= 1.0F
- min
;
426 for (ch
= 0; ch
< 4; ch
++) {
427 float u
= fabsf(s
[ch
]);
435 icoord0
[ch
] = util_ifloor(u
);
436 icoord1
[ch
] = icoord0
[ch
] + 1;
443 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
446 wrap_nearest_unorm_clamp(const float s
[4], unsigned size
, int icoord
[4])
449 for (ch
= 0; ch
< 4; ch
++) {
450 int i
= util_ifloor(s
[ch
]);
451 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
457 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
460 wrap_nearest_unorm_clamp_to_border(const float s
[4], unsigned size
,
464 for (ch
= 0; ch
< 4; ch
++) {
465 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], -0.5F
, (float) size
+ 0.5F
) );
471 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
474 wrap_nearest_unorm_clamp_to_edge(const float s
[4], unsigned size
,
478 for (ch
= 0; ch
< 4; ch
++) {
479 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
485 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
488 wrap_linear_unorm_clamp(const float s
[4], unsigned size
,
489 int icoord0
[4], int icoord1
[4], float w
[4])
492 for (ch
= 0; ch
< 4; ch
++) {
493 /* Not exactly what the spec says, but it matches NVIDIA output */
494 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
495 icoord0
[ch
] = util_ifloor(u
);
496 icoord1
[ch
] = icoord0
[ch
] + 1;
503 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
506 wrap_linear_unorm_clamp_to_border(const float s
[4], unsigned size
,
507 int icoord0
[4], int icoord1
[4], float w
[4])
510 for (ch
= 0; ch
< 4; ch
++) {
511 float u
= CLAMP(s
[ch
], -0.5F
, (float) size
+ 0.5F
);
513 icoord0
[ch
] = util_ifloor(u
);
514 icoord1
[ch
] = icoord0
[ch
] + 1;
515 if (icoord1
[ch
] > (int) size
- 1)
516 icoord1
[ch
] = size
- 1;
523 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
526 wrap_linear_unorm_clamp_to_edge(const float s
[4], unsigned size
,
527 int icoord0
[4], int icoord1
[4], float w
[4])
530 for (ch
= 0; ch
< 4; ch
++) {
531 float u
= CLAMP(s
[ch
], +0.5F
, (float) size
- 0.5F
);
533 icoord0
[ch
] = util_ifloor(u
);
534 icoord1
[ch
] = icoord0
[ch
] + 1;
535 if (icoord1
[ch
] > (int) size
- 1)
536 icoord1
[ch
] = size
- 1;
544 * Examine the quad's texture coordinates to compute the partial
545 * derivatives w.r.t X and Y, then compute lambda (level of detail).
548 compute_lambda_1d(const struct sp_sampler_variant
*samp
,
549 const float s
[QUAD_SIZE
],
550 const float t
[QUAD_SIZE
],
551 const float p
[QUAD_SIZE
])
553 const struct pipe_resource
*texture
= samp
->texture
;
554 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
555 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
556 float rho
= MAX2(dsdx
, dsdy
) * texture
->width0
;
558 return util_fast_log2(rho
);
563 compute_lambda_2d(const struct sp_sampler_variant
*samp
,
564 const float s
[QUAD_SIZE
],
565 const float t
[QUAD_SIZE
],
566 const float p
[QUAD_SIZE
])
568 const struct pipe_resource
*texture
= samp
->texture
;
569 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
570 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
571 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
572 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
573 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
574 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
575 float rho
= MAX2(maxx
, maxy
);
577 return util_fast_log2(rho
);
582 compute_lambda_3d(const struct sp_sampler_variant
*samp
,
583 const float s
[QUAD_SIZE
],
584 const float t
[QUAD_SIZE
],
585 const float p
[QUAD_SIZE
])
587 const struct pipe_resource
*texture
= samp
->texture
;
588 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
589 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
590 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
591 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
592 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
593 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
594 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
595 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
596 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth0
;
599 rho
= MAX2(maxx
, maxy
);
600 rho
= MAX2(rho
, maxz
);
602 return util_fast_log2(rho
);
607 * Compute lambda for a vertex texture sampler.
608 * Since there aren't derivatives to use, just return 0.
611 compute_lambda_vert(const struct sp_sampler_variant
*samp
,
612 const float s
[QUAD_SIZE
],
613 const float t
[QUAD_SIZE
],
614 const float p
[QUAD_SIZE
])
622 * Get a texel from a texture, using the texture tile cache.
624 * \param addr the template tex address containing cube, z, face info.
625 * \param x the x coord of texel within 2D image
626 * \param y the y coord of texel within 2D image
627 * \param rgba the quad to put the texel/color into
629 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
630 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
636 static INLINE
const float *
637 get_texel_2d_no_border(const struct sp_sampler_variant
*samp
,
638 union tex_tile_address addr
, int x
, int y
)
640 const struct softpipe_tex_cached_tile
*tile
;
642 addr
.bits
.x
= x
/ TILE_SIZE
;
643 addr
.bits
.y
= y
/ TILE_SIZE
;
647 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
649 return &tile
->data
.color
[y
][x
][0];
653 static INLINE
const float *
654 get_texel_2d(const struct sp_sampler_variant
*samp
,
655 union tex_tile_address addr
, int x
, int y
)
657 const struct pipe_resource
*texture
= samp
->texture
;
658 unsigned level
= addr
.bits
.level
;
660 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
661 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
662 return samp
->sampler
->border_color
;
665 return get_texel_2d_no_border( samp
, addr
, x
, y
);
670 /* Gather a quad of adjacent texels within a tile:
673 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant
*samp
,
674 union tex_tile_address addr
,
675 unsigned x
, unsigned y
,
678 const struct softpipe_tex_cached_tile
*tile
;
680 addr
.bits
.x
= x
/ TILE_SIZE
;
681 addr
.bits
.y
= y
/ TILE_SIZE
;
685 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
687 out
[0] = &tile
->data
.color
[y
][x
][0];
688 out
[1] = &tile
->data
.color
[y
][x
+1][0];
689 out
[2] = &tile
->data
.color
[y
+1][x
][0];
690 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
694 /* Gather a quad of potentially non-adjacent texels:
697 get_texel_quad_2d_no_border(const struct sp_sampler_variant
*samp
,
698 union tex_tile_address addr
,
703 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
704 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
705 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
706 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
709 /* Can involve a lot of unnecessary checks for border color:
712 get_texel_quad_2d(const struct sp_sampler_variant
*samp
,
713 union tex_tile_address addr
,
718 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
719 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
720 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
721 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
728 static INLINE
const float *
729 get_texel_3d_no_border(const struct sp_sampler_variant
*samp
,
730 union tex_tile_address addr
, int x
, int y
, int z
)
732 const struct softpipe_tex_cached_tile
*tile
;
734 addr
.bits
.x
= x
/ TILE_SIZE
;
735 addr
.bits
.y
= y
/ TILE_SIZE
;
740 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
742 return &tile
->data
.color
[y
][x
][0];
746 static INLINE
const float *
747 get_texel_3d(const struct sp_sampler_variant
*samp
,
748 union tex_tile_address addr
, int x
, int y
, int z
)
750 const struct pipe_resource
*texture
= samp
->texture
;
751 unsigned level
= addr
.bits
.level
;
753 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
754 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
755 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
756 return samp
->sampler
->border_color
;
759 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
765 * Given the logbase2 of a mipmap's base level size and a mipmap level,
766 * return the size (in texels) of that mipmap level.
767 * For example, if level[0].width = 256 then base_pot will be 8.
768 * If level = 2, then we'll return 64 (the width at level=2).
769 * Return 1 if level > base_pot.
771 static INLINE
unsigned
772 pot_level_size(unsigned base_pot
, unsigned level
)
774 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
779 print_sample(const char *function
, float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
781 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
783 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
784 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
785 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
786 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
790 /* Some image-filter fastpaths:
793 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
794 const float s
[QUAD_SIZE
],
795 const float t
[QUAD_SIZE
],
796 const float p
[QUAD_SIZE
],
797 const float c0
[QUAD_SIZE
],
798 enum tgsi_sampler_control control
,
799 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
801 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
803 unsigned level
= samp
->level
;
804 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
805 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
806 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
807 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
808 union tex_tile_address addr
;
811 addr
.bits
.level
= samp
->level
;
813 for (j
= 0; j
< QUAD_SIZE
; j
++) {
816 float u
= s
[j
] * xpot
- 0.5F
;
817 float v
= t
[j
] * ypot
- 0.5F
;
819 int uflr
= util_ifloor(u
);
820 int vflr
= util_ifloor(v
);
822 float xw
= u
- (float)uflr
;
823 float yw
= v
- (float)vflr
;
825 int x0
= uflr
& (xpot
- 1);
826 int y0
= vflr
& (ypot
- 1);
830 /* Can we fetch all four at once:
832 if (x0
< xmax
&& y0
< ymax
) {
833 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
836 unsigned x1
= (x0
+ 1) & (xpot
- 1);
837 unsigned y1
= (y0
+ 1) & (ypot
- 1);
838 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
841 /* interpolate R, G, B, A */
842 for (c
= 0; c
< 4; c
++) {
843 rgba
[c
][j
] = lerp_2d(xw
, yw
,
850 print_sample(__FUNCTION__
, rgba
);
856 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
857 const float s
[QUAD_SIZE
],
858 const float t
[QUAD_SIZE
],
859 const float p
[QUAD_SIZE
],
860 const float c0
[QUAD_SIZE
],
861 enum tgsi_sampler_control control
,
862 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
864 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
866 unsigned level
= samp
->level
;
867 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
868 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
869 union tex_tile_address addr
;
872 addr
.bits
.level
= samp
->level
;
874 for (j
= 0; j
< QUAD_SIZE
; j
++) {
877 float u
= s
[j
] * xpot
;
878 float v
= t
[j
] * ypot
;
880 int uflr
= util_ifloor(u
);
881 int vflr
= util_ifloor(v
);
883 int x0
= uflr
& (xpot
- 1);
884 int y0
= vflr
& (ypot
- 1);
886 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
888 for (c
= 0; c
< 4; c
++) {
894 print_sample(__FUNCTION__
, rgba
);
900 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
901 const float s
[QUAD_SIZE
],
902 const float t
[QUAD_SIZE
],
903 const float p
[QUAD_SIZE
],
904 const float c0
[QUAD_SIZE
],
905 enum tgsi_sampler_control control
,
906 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
908 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
910 unsigned level
= samp
->level
;
911 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
912 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
913 union tex_tile_address addr
;
916 addr
.bits
.level
= samp
->level
;
918 for (j
= 0; j
< QUAD_SIZE
; j
++) {
921 float u
= s
[j
] * xpot
;
922 float v
= t
[j
] * ypot
;
930 else if (x0
> xpot
- 1)
936 else if (y0
> ypot
- 1)
939 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
941 for (c
= 0; c
< 4; c
++) {
947 print_sample(__FUNCTION__
, rgba
);
953 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
954 const float s
[QUAD_SIZE
],
955 const float t
[QUAD_SIZE
],
956 const float p
[QUAD_SIZE
],
957 const float c0
[QUAD_SIZE
],
958 enum tgsi_sampler_control control
,
959 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
961 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
962 const struct pipe_resource
*texture
= samp
->texture
;
966 union tex_tile_address addr
;
968 level0
= samp
->level
;
969 width
= u_minify(texture
->width0
, level0
);
974 addr
.bits
.level
= samp
->level
;
976 samp
->nearest_texcoord_s(s
, width
, x
);
978 for (j
= 0; j
< QUAD_SIZE
; j
++) {
979 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
981 for (c
= 0; c
< 4; c
++) {
987 print_sample(__FUNCTION__
, rgba
);
993 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
994 const float s
[QUAD_SIZE
],
995 const float t
[QUAD_SIZE
],
996 const float p
[QUAD_SIZE
],
997 const float c0
[QUAD_SIZE
],
998 enum tgsi_sampler_control control
,
999 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1001 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1002 const struct pipe_resource
*texture
= samp
->texture
;
1006 union tex_tile_address addr
;
1009 level0
= samp
->level
;
1010 width
= u_minify(texture
->width0
, level0
);
1011 height
= u_minify(texture
->height0
, level0
);
1017 addr
.bits
.level
= samp
->level
;
1019 samp
->nearest_texcoord_s(s
, width
, x
);
1020 samp
->nearest_texcoord_t(t
, height
, y
);
1022 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1023 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
1025 for (c
= 0; c
< 4; c
++) {
1026 rgba
[c
][j
] = out
[c
];
1031 print_sample(__FUNCTION__
, rgba
);
1036 static INLINE
union tex_tile_address
1037 face(union tex_tile_address addr
, unsigned face
)
1039 addr
.bits
.face
= face
;
1045 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1046 const float s
[QUAD_SIZE
],
1047 const float t
[QUAD_SIZE
],
1048 const float p
[QUAD_SIZE
],
1049 const float c0
[QUAD_SIZE
],
1050 enum tgsi_sampler_control control
,
1051 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1053 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1054 const struct pipe_resource
*texture
= samp
->texture
;
1055 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1059 union tex_tile_address addr
;
1061 level0
= samp
->level
;
1062 width
= u_minify(texture
->width0
, level0
);
1063 height
= u_minify(texture
->height0
, level0
);
1069 addr
.bits
.level
= samp
->level
;
1071 samp
->nearest_texcoord_s(s
, width
, x
);
1072 samp
->nearest_texcoord_t(t
, height
, y
);
1074 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1075 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1077 for (c
= 0; c
< 4; c
++) {
1078 rgba
[c
][j
] = out
[c
];
1083 print_sample(__FUNCTION__
, rgba
);
1089 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1090 const float s
[QUAD_SIZE
],
1091 const float t
[QUAD_SIZE
],
1092 const float p
[QUAD_SIZE
],
1093 const float c0
[QUAD_SIZE
],
1094 enum tgsi_sampler_control control
,
1095 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1097 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1098 const struct pipe_resource
*texture
= samp
->texture
;
1100 int width
, height
, depth
;
1101 int x
[4], y
[4], z
[4];
1102 union tex_tile_address addr
;
1104 level0
= samp
->level
;
1105 width
= u_minify(texture
->width0
, level0
);
1106 height
= u_minify(texture
->height0
, level0
);
1107 depth
= u_minify(texture
->depth0
, level0
);
1113 samp
->nearest_texcoord_s(s
, width
, x
);
1114 samp
->nearest_texcoord_t(t
, height
, y
);
1115 samp
->nearest_texcoord_p(p
, depth
, z
);
1118 addr
.bits
.level
= samp
->level
;
1120 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1121 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1123 for (c
= 0; c
< 4; c
++) {
1124 rgba
[c
][j
] = out
[c
];
1131 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1132 const float s
[QUAD_SIZE
],
1133 const float t
[QUAD_SIZE
],
1134 const float p
[QUAD_SIZE
],
1135 const float c0
[QUAD_SIZE
],
1136 enum tgsi_sampler_control control
,
1137 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1139 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1140 const struct pipe_resource
*texture
= samp
->texture
;
1144 float xw
[4]; /* weights */
1145 union tex_tile_address addr
;
1147 level0
= samp
->level
;
1148 width
= u_minify(texture
->width0
, level0
);
1153 addr
.bits
.level
= samp
->level
;
1155 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1157 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1158 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1159 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1162 /* interpolate R, G, B, A */
1163 for (c
= 0; c
< 4; c
++) {
1164 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1171 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1172 const float s
[QUAD_SIZE
],
1173 const float t
[QUAD_SIZE
],
1174 const float p
[QUAD_SIZE
],
1175 const float c0
[QUAD_SIZE
],
1176 enum tgsi_sampler_control control
,
1177 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1179 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1180 const struct pipe_resource
*texture
= samp
->texture
;
1183 int x0
[4], y0
[4], x1
[4], y1
[4];
1184 float xw
[4], yw
[4]; /* weights */
1185 union tex_tile_address addr
;
1187 level0
= samp
->level
;
1188 width
= u_minify(texture
->width0
, level0
);
1189 height
= u_minify(texture
->height0
, level0
);
1195 addr
.bits
.level
= samp
->level
;
1197 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1198 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1200 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1201 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1202 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1203 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1204 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1207 /* interpolate R, G, B, A */
1208 for (c
= 0; c
< 4; c
++) {
1209 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1218 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1219 const float s
[QUAD_SIZE
],
1220 const float t
[QUAD_SIZE
],
1221 const float p
[QUAD_SIZE
],
1222 const float c0
[QUAD_SIZE
],
1223 enum tgsi_sampler_control control
,
1224 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1226 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1227 const struct pipe_resource
*texture
= samp
->texture
;
1228 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1231 int x0
[4], y0
[4], x1
[4], y1
[4];
1232 float xw
[4], yw
[4]; /* weights */
1233 union tex_tile_address addr
;
1235 level0
= samp
->level
;
1236 width
= u_minify(texture
->width0
, level0
);
1237 height
= u_minify(texture
->height0
, level0
);
1243 addr
.bits
.level
= samp
->level
;
1245 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1246 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1248 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1249 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1250 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1251 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1252 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1253 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1256 /* interpolate R, G, B, A */
1257 for (c
= 0; c
< 4; c
++) {
1258 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1267 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1268 const float s
[QUAD_SIZE
],
1269 const float t
[QUAD_SIZE
],
1270 const float p
[QUAD_SIZE
],
1271 const float c0
[QUAD_SIZE
],
1272 enum tgsi_sampler_control control
,
1273 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1275 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1276 const struct pipe_resource
*texture
= samp
->texture
;
1278 int width
, height
, depth
;
1279 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1280 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1281 union tex_tile_address addr
;
1283 level0
= samp
->level
;
1284 width
= u_minify(texture
->width0
, level0
);
1285 height
= u_minify(texture
->height0
, level0
);
1286 depth
= u_minify(texture
->depth0
, level0
);
1289 addr
.bits
.level
= level0
;
1295 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1296 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1297 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1299 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1302 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1303 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1304 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1305 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1307 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1308 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1309 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1310 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1312 /* interpolate R, G, B, A */
1313 for (c
= 0; c
< 4; c
++) {
1314 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1324 /* Calculate level of detail for every fragment.
1325 * Note that lambda has already been biased by global LOD bias.
1328 compute_lod(const struct pipe_sampler_state
*sampler
,
1329 const float biased_lambda
,
1330 const float lodbias
[QUAD_SIZE
],
1331 float lod
[QUAD_SIZE
])
1335 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1336 lod
[i
] = biased_lambda
+ lodbias
[i
];
1337 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1343 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1344 const float s
[QUAD_SIZE
],
1345 const float t
[QUAD_SIZE
],
1346 const float p
[QUAD_SIZE
],
1347 const float c0
[QUAD_SIZE
],
1348 enum tgsi_sampler_control control
,
1349 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1351 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1352 const struct pipe_resource
*texture
= samp
->texture
;
1355 float lod
[QUAD_SIZE
];
1357 if (control
== tgsi_sampler_lod_bias
) {
1358 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1359 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1361 assert(control
== tgsi_sampler_lod_explicit
);
1363 memcpy(lod
, c0
, sizeof(lod
));
1366 /* XXX: Take into account all lod values.
1369 level0
= (int)lambda
;
1373 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1375 else if (level0
>= texture
->last_level
) {
1376 samp
->level
= texture
->last_level
;
1377 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1380 float levelBlend
= lambda
- level0
;
1385 samp
->level
= level0
;
1386 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1388 samp
->level
= level0
+1;
1389 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1391 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1392 for (c
= 0; c
< 4; c
++) {
1393 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1399 print_sample(__FUNCTION__
, rgba
);
1405 * Compute nearest mipmap level from texcoords.
1406 * Then sample the texture level for four elements of a quad.
1407 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1410 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1411 const float s
[QUAD_SIZE
],
1412 const float t
[QUAD_SIZE
],
1413 const float p
[QUAD_SIZE
],
1414 const float c0
[QUAD_SIZE
],
1415 enum tgsi_sampler_control control
,
1416 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1418 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1419 const struct pipe_resource
*texture
= samp
->texture
;
1421 float lod
[QUAD_SIZE
];
1423 if (control
== tgsi_sampler_lod_bias
) {
1424 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1425 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1427 assert(control
== tgsi_sampler_lod_explicit
);
1429 memcpy(lod
, c0
, sizeof(lod
));
1432 /* XXX: Take into account all lod values.
1438 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1441 samp
->level
= (int)(lambda
+ 0.5) ;
1442 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1443 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1447 print_sample(__FUNCTION__
, rgba
);
1453 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1454 const float s
[QUAD_SIZE
],
1455 const float t
[QUAD_SIZE
],
1456 const float p
[QUAD_SIZE
],
1457 const float c0
[QUAD_SIZE
],
1458 enum tgsi_sampler_control control
,
1459 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1461 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1463 float lod
[QUAD_SIZE
];
1465 if (control
== tgsi_sampler_lod_bias
) {
1466 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1467 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1469 assert(control
== tgsi_sampler_lod_explicit
);
1471 memcpy(lod
, c0
, sizeof(lod
));
1474 /* XXX: Take into account all lod values.
1479 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1482 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1489 * Specialized version of mip_filter_linear with hard-wired calls to
1490 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1493 mip_filter_linear_2d_linear_repeat_POT(
1494 struct tgsi_sampler
*tgsi_sampler
,
1495 const float s
[QUAD_SIZE
],
1496 const float t
[QUAD_SIZE
],
1497 const float p
[QUAD_SIZE
],
1498 const float c0
[QUAD_SIZE
],
1499 enum tgsi_sampler_control control
,
1500 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1502 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1503 const struct pipe_resource
*texture
= samp
->texture
;
1506 float lod
[QUAD_SIZE
];
1508 if (control
== tgsi_sampler_lod_bias
) {
1509 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1510 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1512 assert(control
== tgsi_sampler_lod_explicit
);
1514 memcpy(lod
, c0
, sizeof(lod
));
1517 /* XXX: Take into account all lod values.
1520 level0
= (int)lambda
;
1522 /* Catches both negative and large values of level0:
1524 if ((unsigned)level0
>= texture
->last_level
) {
1528 samp
->level
= texture
->last_level
;
1530 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1533 float levelBlend
= lambda
- level0
;
1538 samp
->level
= level0
;
1539 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1541 samp
->level
= level0
+1;
1542 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1544 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1545 for (c
= 0; c
< 4; c
++) {
1546 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1552 print_sample(__FUNCTION__
, rgba
);
1559 * Do shadow/depth comparisons.
1562 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1563 const float s
[QUAD_SIZE
],
1564 const float t
[QUAD_SIZE
],
1565 const float p
[QUAD_SIZE
],
1566 const float c0
[QUAD_SIZE
],
1567 enum tgsi_sampler_control control
,
1568 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1570 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1571 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1572 int j
, k0
, k1
, k2
, k3
;
1575 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1578 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1579 * When we sampled the depth texture, the depth value was put into all
1580 * RGBA channels. We look at the red channel here.
1583 /* compare four texcoords vs. four texture samples */
1584 switch (sampler
->compare_func
) {
1585 case PIPE_FUNC_LESS
:
1586 k0
= p
[0] < rgba
[0][0];
1587 k1
= p
[1] < rgba
[0][1];
1588 k2
= p
[2] < rgba
[0][2];
1589 k3
= p
[3] < rgba
[0][3];
1591 case PIPE_FUNC_LEQUAL
:
1592 k0
= p
[0] <= rgba
[0][0];
1593 k1
= p
[1] <= rgba
[0][1];
1594 k2
= p
[2] <= rgba
[0][2];
1595 k3
= p
[3] <= rgba
[0][3];
1597 case PIPE_FUNC_GREATER
:
1598 k0
= p
[0] > rgba
[0][0];
1599 k1
= p
[1] > rgba
[0][1];
1600 k2
= p
[2] > rgba
[0][2];
1601 k3
= p
[3] > rgba
[0][3];
1603 case PIPE_FUNC_GEQUAL
:
1604 k0
= p
[0] >= rgba
[0][0];
1605 k1
= p
[1] >= rgba
[0][1];
1606 k2
= p
[2] >= rgba
[0][2];
1607 k3
= p
[3] >= rgba
[0][3];
1609 case PIPE_FUNC_EQUAL
:
1610 k0
= p
[0] == rgba
[0][0];
1611 k1
= p
[1] == rgba
[0][1];
1612 k2
= p
[2] == rgba
[0][2];
1613 k3
= p
[3] == rgba
[0][3];
1615 case PIPE_FUNC_NOTEQUAL
:
1616 k0
= p
[0] != rgba
[0][0];
1617 k1
= p
[1] != rgba
[0][1];
1618 k2
= p
[2] != rgba
[0][2];
1619 k3
= p
[3] != rgba
[0][3];
1621 case PIPE_FUNC_ALWAYS
:
1622 k0
= k1
= k2
= k3
= 1;
1624 case PIPE_FUNC_NEVER
:
1625 k0
= k1
= k2
= k3
= 0;
1628 k0
= k1
= k2
= k3
= 0;
1633 /* convert four pass/fail values to an intensity in [0,1] */
1634 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1636 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1637 for (j
= 0; j
< 4; j
++) {
1638 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1645 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1646 * Put face info into the sampler faces[] array.
1649 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1650 const float s
[QUAD_SIZE
],
1651 const float t
[QUAD_SIZE
],
1652 const float p
[QUAD_SIZE
],
1653 const float c0
[QUAD_SIZE
],
1654 enum tgsi_sampler_control control
,
1655 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1657 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1659 float ssss
[4], tttt
[4];
1663 direction target sc tc ma
1664 ---------- ------------------------------- --- --- ---
1665 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1666 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1667 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1668 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1669 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1670 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1673 /* Choose the cube face and compute new s/t coords for the 2D face.
1675 * Use the same cube face for all four pixels in the quad.
1677 * This isn't ideal, but if we want to use a different cube face
1678 * per pixel in the quad, we'd have to also compute the per-face
1679 * LOD here too. That's because the four post-face-selection
1680 * texcoords are no longer related to each other (they're
1681 * per-face!) so we can't use subtraction to compute the partial
1682 * deriviates to compute the LOD. Doing so (near cube edges
1683 * anyway) gives us pretty much random values.
1686 /* use the average of the four pixel's texcoords to choose the face */
1687 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1688 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1689 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1690 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1692 if (arx
>= ary
&& arx
>= arz
) {
1693 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
1694 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
1695 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1696 const float ima
= -0.5F
/ fabsf(s
[j
]);
1697 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
1698 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1699 samp
->faces
[j
] = face
;
1702 else if (ary
>= arx
&& ary
>= arz
) {
1703 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
1704 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
1705 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1706 const float ima
= -0.5F
/ fabsf(t
[j
]);
1707 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
1708 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
1709 samp
->faces
[j
] = face
;
1713 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
1714 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
1715 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1716 const float ima
= -0.5 / fabsf(p
[j
]);
1717 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
1718 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1719 samp
->faces
[j
] = face
;
1724 /* In our little pipeline, the compare stage is next. If compare
1725 * is not active, this will point somewhere deeper into the
1726 * pipeline, eg. to mip_filter or even img_filter.
1728 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1733 sample_swizzle(struct tgsi_sampler
*tgsi_sampler
,
1734 const float s
[QUAD_SIZE
],
1735 const float t
[QUAD_SIZE
],
1736 const float p
[QUAD_SIZE
],
1737 const float c0
[QUAD_SIZE
],
1738 enum tgsi_sampler_control control
,
1739 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1741 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1742 float rgba_temp
[NUM_CHANNELS
][QUAD_SIZE
];
1743 const unsigned swizzle_r
= samp
->key
.bits
.swizzle_r
;
1744 const unsigned swizzle_g
= samp
->key
.bits
.swizzle_g
;
1745 const unsigned swizzle_b
= samp
->key
.bits
.swizzle_b
;
1746 const unsigned swizzle_a
= samp
->key
.bits
.swizzle_a
;
1749 samp
->sample_target(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba_temp
);
1751 switch (swizzle_r
) {
1752 case PIPE_SWIZZLE_ZERO
:
1753 for (j
= 0; j
< 4; j
++)
1756 case PIPE_SWIZZLE_ONE
:
1757 for (j
= 0; j
< 4; j
++)
1761 assert(swizzle_r
< 4);
1762 for (j
= 0; j
< 4; j
++)
1763 rgba
[0][j
] = rgba_temp
[swizzle_r
][j
];
1766 switch (swizzle_g
) {
1767 case PIPE_SWIZZLE_ZERO
:
1768 for (j
= 0; j
< 4; j
++)
1771 case PIPE_SWIZZLE_ONE
:
1772 for (j
= 0; j
< 4; j
++)
1776 assert(swizzle_g
< 4);
1777 for (j
= 0; j
< 4; j
++)
1778 rgba
[1][j
] = rgba_temp
[swizzle_g
][j
];
1781 switch (swizzle_b
) {
1782 case PIPE_SWIZZLE_ZERO
:
1783 for (j
= 0; j
< 4; j
++)
1786 case PIPE_SWIZZLE_ONE
:
1787 for (j
= 0; j
< 4; j
++)
1791 assert(swizzle_b
< 4);
1792 for (j
= 0; j
< 4; j
++)
1793 rgba
[2][j
] = rgba_temp
[swizzle_b
][j
];
1796 switch (swizzle_a
) {
1797 case PIPE_SWIZZLE_ZERO
:
1798 for (j
= 0; j
< 4; j
++)
1801 case PIPE_SWIZZLE_ONE
:
1802 for (j
= 0; j
< 4; j
++)
1806 assert(swizzle_a
< 4);
1807 for (j
= 0; j
< 4; j
++)
1808 rgba
[3][j
] = rgba_temp
[swizzle_a
][j
];
1813 static wrap_nearest_func
1814 get_nearest_unorm_wrap(unsigned mode
)
1817 case PIPE_TEX_WRAP_CLAMP
:
1818 return wrap_nearest_unorm_clamp
;
1819 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1820 return wrap_nearest_unorm_clamp_to_edge
;
1821 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1822 return wrap_nearest_unorm_clamp_to_border
;
1825 return wrap_nearest_unorm_clamp
;
1830 static wrap_nearest_func
1831 get_nearest_wrap(unsigned mode
)
1834 case PIPE_TEX_WRAP_REPEAT
:
1835 return wrap_nearest_repeat
;
1836 case PIPE_TEX_WRAP_CLAMP
:
1837 return wrap_nearest_clamp
;
1838 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1839 return wrap_nearest_clamp_to_edge
;
1840 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1841 return wrap_nearest_clamp_to_border
;
1842 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1843 return wrap_nearest_mirror_repeat
;
1844 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1845 return wrap_nearest_mirror_clamp
;
1846 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1847 return wrap_nearest_mirror_clamp_to_edge
;
1848 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1849 return wrap_nearest_mirror_clamp_to_border
;
1852 return wrap_nearest_repeat
;
1857 static wrap_linear_func
1858 get_linear_unorm_wrap(unsigned mode
)
1861 case PIPE_TEX_WRAP_CLAMP
:
1862 return wrap_linear_unorm_clamp
;
1863 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1864 return wrap_linear_unorm_clamp_to_edge
;
1865 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1866 return wrap_linear_unorm_clamp_to_border
;
1869 return wrap_linear_unorm_clamp
;
1874 static wrap_linear_func
1875 get_linear_wrap(unsigned mode
)
1878 case PIPE_TEX_WRAP_REPEAT
:
1879 return wrap_linear_repeat
;
1880 case PIPE_TEX_WRAP_CLAMP
:
1881 return wrap_linear_clamp
;
1882 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1883 return wrap_linear_clamp_to_edge
;
1884 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1885 return wrap_linear_clamp_to_border
;
1886 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1887 return wrap_linear_mirror_repeat
;
1888 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1889 return wrap_linear_mirror_clamp
;
1890 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1891 return wrap_linear_mirror_clamp_to_edge
;
1892 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1893 return wrap_linear_mirror_clamp_to_border
;
1896 return wrap_linear_repeat
;
1901 static compute_lambda_func
1902 get_lambda_func(const union sp_sampler_key key
)
1904 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1905 return compute_lambda_vert
;
1907 switch (key
.bits
.target
) {
1908 case PIPE_TEXTURE_1D
:
1909 return compute_lambda_1d
;
1910 case PIPE_TEXTURE_2D
:
1911 case PIPE_TEXTURE_RECT
:
1912 case PIPE_TEXTURE_CUBE
:
1913 return compute_lambda_2d
;
1914 case PIPE_TEXTURE_3D
:
1915 return compute_lambda_3d
;
1918 return compute_lambda_1d
;
1924 get_img_filter(const union sp_sampler_key key
,
1926 const struct pipe_sampler_state
*sampler
)
1928 switch (key
.bits
.target
) {
1929 case PIPE_TEXTURE_1D
:
1930 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1931 return img_filter_1d_nearest
;
1933 return img_filter_1d_linear
;
1935 case PIPE_TEXTURE_2D
:
1936 case PIPE_TEXTURE_RECT
:
1937 /* Try for fast path:
1939 if (key
.bits
.is_pot
&&
1940 sampler
->wrap_s
== sampler
->wrap_t
&&
1941 sampler
->normalized_coords
)
1943 switch (sampler
->wrap_s
) {
1944 case PIPE_TEX_WRAP_REPEAT
:
1946 case PIPE_TEX_FILTER_NEAREST
:
1947 return img_filter_2d_nearest_repeat_POT
;
1948 case PIPE_TEX_FILTER_LINEAR
:
1949 return img_filter_2d_linear_repeat_POT
;
1954 case PIPE_TEX_WRAP_CLAMP
:
1956 case PIPE_TEX_FILTER_NEAREST
:
1957 return img_filter_2d_nearest_clamp_POT
;
1963 /* Otherwise use default versions:
1965 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1966 return img_filter_2d_nearest
;
1968 return img_filter_2d_linear
;
1970 case PIPE_TEXTURE_CUBE
:
1971 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1972 return img_filter_cube_nearest
;
1974 return img_filter_cube_linear
;
1976 case PIPE_TEXTURE_3D
:
1977 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1978 return img_filter_3d_nearest
;
1980 return img_filter_3d_linear
;
1984 return img_filter_1d_nearest
;
1990 * Bind the given texture object and texture cache to the sampler variant.
1993 sp_sampler_variant_bind_texture( struct sp_sampler_variant
*samp
,
1994 struct softpipe_tex_tile_cache
*tex_cache
,
1995 const struct pipe_resource
*texture
)
1997 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1999 samp
->texture
= texture
;
2000 samp
->cache
= tex_cache
;
2001 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
2002 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
2003 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
2008 sp_sampler_variant_destroy( struct sp_sampler_variant
*samp
)
2015 * Create a sampler variant for a given set of non-orthogonal state.
2017 struct sp_sampler_variant
*
2018 sp_create_sampler_variant( const struct pipe_sampler_state
*sampler
,
2019 const union sp_sampler_key key
)
2021 struct sp_sampler_variant
*samp
= CALLOC_STRUCT(sp_sampler_variant
);
2025 samp
->sampler
= sampler
;
2028 /* Note that (for instance) linear_texcoord_s and
2029 * nearest_texcoord_s may be active at the same time, if the
2030 * sampler min_img_filter differs from its mag_img_filter.
2032 if (sampler
->normalized_coords
) {
2033 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
2034 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
2035 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
2037 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
2038 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
2039 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
2042 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
2043 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
2044 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
2046 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
2047 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
2048 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
2051 samp
->compute_lambda
= get_lambda_func( key
);
2053 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
2054 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
2056 switch (sampler
->min_mip_filter
) {
2057 case PIPE_TEX_MIPFILTER_NONE
:
2058 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
2059 samp
->mip_filter
= samp
->min_img_filter
;
2061 samp
->mip_filter
= mip_filter_none
;
2064 case PIPE_TEX_MIPFILTER_NEAREST
:
2065 samp
->mip_filter
= mip_filter_nearest
;
2068 case PIPE_TEX_MIPFILTER_LINEAR
:
2069 if (key
.bits
.is_pot
&&
2070 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
2071 sampler
->normalized_coords
&&
2072 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
2073 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
2074 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
2076 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2080 samp
->mip_filter
= mip_filter_linear
;
2085 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2086 samp
->compare
= sample_compare
;
2089 /* Skip compare operation by promoting the mip_filter function
2092 samp
->compare
= samp
->mip_filter
;
2095 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
2096 samp
->sample_target
= sample_cube
;
2104 /* Skip cube face determination by promoting the compare
2107 samp
->sample_target
= samp
->compare
;
2110 if (key
.bits
.swizzle_r
!= PIPE_SWIZZLE_RED
||
2111 key
.bits
.swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2112 key
.bits
.swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2113 key
.bits
.swizzle_a
!= PIPE_SWIZZLE_ALPHA
) {
2114 samp
->base
.get_samples
= sample_swizzle
;
2117 samp
->base
.get_samples
= samp
->sample_target
;