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_varient
*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_varient
*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_varient
*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_varient
*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_varient
*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_varient
*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 sp_tex_tile_cache_border_color(samp
->cache
,
663 samp
->sampler
->border_color
);
666 return get_texel_2d_no_border( samp
, addr
, x
, y
);
671 /* Gather a quad of adjacent texels within a tile:
674 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
675 union tex_tile_address addr
,
676 unsigned x
, unsigned y
,
679 const struct softpipe_tex_cached_tile
*tile
;
681 addr
.bits
.x
= x
/ TILE_SIZE
;
682 addr
.bits
.y
= y
/ TILE_SIZE
;
686 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
688 out
[0] = &tile
->data
.color
[y
][x
][0];
689 out
[1] = &tile
->data
.color
[y
][x
+1][0];
690 out
[2] = &tile
->data
.color
[y
+1][x
][0];
691 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
695 /* Gather a quad of potentially non-adjacent texels:
698 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
699 union tex_tile_address addr
,
704 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
705 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
706 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
707 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
710 /* Can involve a lot of unnecessary checks for border color:
713 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
714 union tex_tile_address addr
,
719 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
720 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
721 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
722 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
729 static INLINE
const float *
730 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
731 union tex_tile_address addr
, int x
, int y
, int z
)
733 const struct softpipe_tex_cached_tile
*tile
;
735 addr
.bits
.x
= x
/ TILE_SIZE
;
736 addr
.bits
.y
= y
/ TILE_SIZE
;
741 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
743 return &tile
->data
.color
[y
][x
][0];
747 static INLINE
const float *
748 get_texel_3d(const struct sp_sampler_varient
*samp
,
749 union tex_tile_address addr
, int x
, int y
, int z
)
751 const struct pipe_resource
*texture
= samp
->texture
;
752 unsigned level
= addr
.bits
.level
;
754 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
755 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
756 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
757 return sp_tex_tile_cache_border_color(samp
->cache
,
758 samp
->sampler
->border_color
);
761 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
767 * Given the logbase2 of a mipmap's base level size and a mipmap level,
768 * return the size (in texels) of that mipmap level.
769 * For example, if level[0].width = 256 then base_pot will be 8.
770 * If level = 2, then we'll return 64 (the width at level=2).
771 * Return 1 if level > base_pot.
773 static INLINE
unsigned
774 pot_level_size(unsigned base_pot
, unsigned level
)
776 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
781 print_sample(const char *function
, float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
783 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
785 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
786 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
787 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
788 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
792 /* Some image-filter fastpaths:
795 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
796 const float s
[QUAD_SIZE
],
797 const float t
[QUAD_SIZE
],
798 const float p
[QUAD_SIZE
],
799 const float c0
[QUAD_SIZE
],
800 enum tgsi_sampler_control control
,
801 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
803 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
805 unsigned level
= samp
->level
;
806 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
807 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
808 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
809 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
810 union tex_tile_address addr
;
813 addr
.bits
.level
= samp
->level
;
815 for (j
= 0; j
< QUAD_SIZE
; j
++) {
818 float u
= s
[j
] * xpot
- 0.5F
;
819 float v
= t
[j
] * ypot
- 0.5F
;
821 int uflr
= util_ifloor(u
);
822 int vflr
= util_ifloor(v
);
824 float xw
= u
- (float)uflr
;
825 float yw
= v
- (float)vflr
;
827 int x0
= uflr
& (xpot
- 1);
828 int y0
= vflr
& (ypot
- 1);
832 /* Can we fetch all four at once:
834 if (x0
< xmax
&& y0
< ymax
) {
835 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
838 unsigned x1
= (x0
+ 1) & (xpot
- 1);
839 unsigned y1
= (y0
+ 1) & (ypot
- 1);
840 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
843 /* interpolate R, G, B, A */
844 for (c
= 0; c
< 4; c
++) {
845 rgba
[c
][j
] = lerp_2d(xw
, yw
,
852 print_sample(__FUNCTION__
, rgba
);
858 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
859 const float s
[QUAD_SIZE
],
860 const float t
[QUAD_SIZE
],
861 const float p
[QUAD_SIZE
],
862 const float c0
[QUAD_SIZE
],
863 enum tgsi_sampler_control control
,
864 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
866 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
868 unsigned level
= samp
->level
;
869 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
870 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
871 union tex_tile_address addr
;
874 addr
.bits
.level
= samp
->level
;
876 for (j
= 0; j
< QUAD_SIZE
; j
++) {
879 float u
= s
[j
] * xpot
;
880 float v
= t
[j
] * ypot
;
882 int uflr
= util_ifloor(u
);
883 int vflr
= util_ifloor(v
);
885 int x0
= uflr
& (xpot
- 1);
886 int y0
= vflr
& (ypot
- 1);
888 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
890 for (c
= 0; c
< 4; c
++) {
896 print_sample(__FUNCTION__
, rgba
);
902 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
903 const float s
[QUAD_SIZE
],
904 const float t
[QUAD_SIZE
],
905 const float p
[QUAD_SIZE
],
906 const float c0
[QUAD_SIZE
],
907 enum tgsi_sampler_control control
,
908 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
910 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
912 unsigned level
= samp
->level
;
913 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
914 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
915 union tex_tile_address addr
;
918 addr
.bits
.level
= samp
->level
;
920 for (j
= 0; j
< QUAD_SIZE
; j
++) {
923 float u
= s
[j
] * xpot
;
924 float v
= t
[j
] * ypot
;
932 else if (x0
> xpot
- 1)
938 else if (y0
> ypot
- 1)
941 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
943 for (c
= 0; c
< 4; c
++) {
949 print_sample(__FUNCTION__
, rgba
);
955 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
956 const float s
[QUAD_SIZE
],
957 const float t
[QUAD_SIZE
],
958 const float p
[QUAD_SIZE
],
959 const float c0
[QUAD_SIZE
],
960 enum tgsi_sampler_control control
,
961 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
963 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
964 const struct pipe_resource
*texture
= samp
->texture
;
968 union tex_tile_address addr
;
970 level0
= samp
->level
;
971 width
= u_minify(texture
->width0
, level0
);
976 addr
.bits
.level
= samp
->level
;
978 samp
->nearest_texcoord_s(s
, width
, x
);
980 for (j
= 0; j
< QUAD_SIZE
; j
++) {
981 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
983 for (c
= 0; c
< 4; c
++) {
989 print_sample(__FUNCTION__
, rgba
);
995 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
996 const float s
[QUAD_SIZE
],
997 const float t
[QUAD_SIZE
],
998 const float p
[QUAD_SIZE
],
999 const float c0
[QUAD_SIZE
],
1000 enum tgsi_sampler_control control
,
1001 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1003 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1004 const struct pipe_resource
*texture
= samp
->texture
;
1008 union tex_tile_address addr
;
1011 level0
= samp
->level
;
1012 width
= u_minify(texture
->width0
, level0
);
1013 height
= u_minify(texture
->height0
, level0
);
1019 addr
.bits
.level
= samp
->level
;
1021 samp
->nearest_texcoord_s(s
, width
, x
);
1022 samp
->nearest_texcoord_t(t
, height
, y
);
1024 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1025 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
1027 for (c
= 0; c
< 4; c
++) {
1028 rgba
[c
][j
] = out
[c
];
1033 print_sample(__FUNCTION__
, rgba
);
1038 static INLINE
union tex_tile_address
1039 face(union tex_tile_address addr
, unsigned face
)
1041 addr
.bits
.face
= face
;
1047 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1048 const float s
[QUAD_SIZE
],
1049 const float t
[QUAD_SIZE
],
1050 const float p
[QUAD_SIZE
],
1051 const float c0
[QUAD_SIZE
],
1052 enum tgsi_sampler_control control
,
1053 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1055 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1056 const struct pipe_resource
*texture
= samp
->texture
;
1057 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1061 union tex_tile_address addr
;
1063 level0
= samp
->level
;
1064 width
= u_minify(texture
->width0
, level0
);
1065 height
= u_minify(texture
->height0
, level0
);
1071 addr
.bits
.level
= samp
->level
;
1073 samp
->nearest_texcoord_s(s
, width
, x
);
1074 samp
->nearest_texcoord_t(t
, height
, y
);
1076 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1077 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1079 for (c
= 0; c
< 4; c
++) {
1080 rgba
[c
][j
] = out
[c
];
1085 print_sample(__FUNCTION__
, rgba
);
1091 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1092 const float s
[QUAD_SIZE
],
1093 const float t
[QUAD_SIZE
],
1094 const float p
[QUAD_SIZE
],
1095 const float c0
[QUAD_SIZE
],
1096 enum tgsi_sampler_control control
,
1097 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1099 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1100 const struct pipe_resource
*texture
= samp
->texture
;
1102 int width
, height
, depth
;
1103 int x
[4], y
[4], z
[4];
1104 union tex_tile_address addr
;
1106 level0
= samp
->level
;
1107 width
= u_minify(texture
->width0
, level0
);
1108 height
= u_minify(texture
->height0
, level0
);
1109 depth
= u_minify(texture
->depth0
, level0
);
1115 samp
->nearest_texcoord_s(s
, width
, x
);
1116 samp
->nearest_texcoord_t(t
, height
, y
);
1117 samp
->nearest_texcoord_p(p
, depth
, z
);
1120 addr
.bits
.level
= samp
->level
;
1122 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1123 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1125 for (c
= 0; c
< 4; c
++) {
1126 rgba
[c
][j
] = out
[c
];
1133 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1134 const float s
[QUAD_SIZE
],
1135 const float t
[QUAD_SIZE
],
1136 const float p
[QUAD_SIZE
],
1137 const float c0
[QUAD_SIZE
],
1138 enum tgsi_sampler_control control
,
1139 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1141 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1142 const struct pipe_resource
*texture
= samp
->texture
;
1146 float xw
[4]; /* weights */
1147 union tex_tile_address addr
;
1149 level0
= samp
->level
;
1150 width
= u_minify(texture
->width0
, level0
);
1155 addr
.bits
.level
= samp
->level
;
1157 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1159 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1160 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1161 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1164 /* interpolate R, G, B, A */
1165 for (c
= 0; c
< 4; c
++) {
1166 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1173 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1174 const float s
[QUAD_SIZE
],
1175 const float t
[QUAD_SIZE
],
1176 const float p
[QUAD_SIZE
],
1177 const float c0
[QUAD_SIZE
],
1178 enum tgsi_sampler_control control
,
1179 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1181 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1182 const struct pipe_resource
*texture
= samp
->texture
;
1185 int x0
[4], y0
[4], x1
[4], y1
[4];
1186 float xw
[4], yw
[4]; /* weights */
1187 union tex_tile_address addr
;
1189 level0
= samp
->level
;
1190 width
= u_minify(texture
->width0
, level0
);
1191 height
= u_minify(texture
->height0
, level0
);
1197 addr
.bits
.level
= samp
->level
;
1199 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1200 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1202 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1203 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1204 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1205 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1206 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1209 /* interpolate R, G, B, A */
1210 for (c
= 0; c
< 4; c
++) {
1211 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1220 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1221 const float s
[QUAD_SIZE
],
1222 const float t
[QUAD_SIZE
],
1223 const float p
[QUAD_SIZE
],
1224 const float c0
[QUAD_SIZE
],
1225 enum tgsi_sampler_control control
,
1226 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1228 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1229 const struct pipe_resource
*texture
= samp
->texture
;
1230 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1233 int x0
[4], y0
[4], x1
[4], y1
[4];
1234 float xw
[4], yw
[4]; /* weights */
1235 union tex_tile_address addr
;
1237 level0
= samp
->level
;
1238 width
= u_minify(texture
->width0
, level0
);
1239 height
= u_minify(texture
->height0
, level0
);
1245 addr
.bits
.level
= samp
->level
;
1247 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1248 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1250 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1251 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1252 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1253 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1254 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1255 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1258 /* interpolate R, G, B, A */
1259 for (c
= 0; c
< 4; c
++) {
1260 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1269 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1270 const float s
[QUAD_SIZE
],
1271 const float t
[QUAD_SIZE
],
1272 const float p
[QUAD_SIZE
],
1273 const float c0
[QUAD_SIZE
],
1274 enum tgsi_sampler_control control
,
1275 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1277 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1278 const struct pipe_resource
*texture
= samp
->texture
;
1280 int width
, height
, depth
;
1281 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1282 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1283 union tex_tile_address addr
;
1285 level0
= samp
->level
;
1286 width
= u_minify(texture
->width0
, level0
);
1287 height
= u_minify(texture
->height0
, level0
);
1288 depth
= u_minify(texture
->depth0
, level0
);
1291 addr
.bits
.level
= level0
;
1297 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1298 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1299 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1301 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1304 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1305 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1306 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1307 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1309 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1310 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1311 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1312 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1314 /* interpolate R, G, B, A */
1315 for (c
= 0; c
< 4; c
++) {
1316 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1326 /* Calculate level of detail for every fragment.
1327 * Note that lambda has already been biased by global LOD bias.
1330 compute_lod(const struct pipe_sampler_state
*sampler
,
1331 const float biased_lambda
,
1332 const float lodbias
[QUAD_SIZE
],
1333 float lod
[QUAD_SIZE
])
1337 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1338 lod
[i
] = biased_lambda
+ lodbias
[i
];
1339 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1345 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1346 const float s
[QUAD_SIZE
],
1347 const float t
[QUAD_SIZE
],
1348 const float p
[QUAD_SIZE
],
1349 const float c0
[QUAD_SIZE
],
1350 enum tgsi_sampler_control control
,
1351 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1353 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1354 const struct pipe_resource
*texture
= samp
->texture
;
1357 float lod
[QUAD_SIZE
];
1359 if (control
== tgsi_sampler_lod_bias
) {
1360 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1361 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1363 assert(control
== tgsi_sampler_lod_explicit
);
1365 memcpy(lod
, c0
, sizeof(lod
));
1368 /* XXX: Take into account all lod values.
1371 level0
= (int)lambda
;
1375 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1377 else if (level0
>= texture
->last_level
) {
1378 samp
->level
= texture
->last_level
;
1379 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1382 float levelBlend
= lambda
- level0
;
1387 samp
->level
= level0
;
1388 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1390 samp
->level
= level0
+1;
1391 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1393 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1394 for (c
= 0; c
< 4; c
++) {
1395 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1401 print_sample(__FUNCTION__
, rgba
);
1407 * Compute nearest mipmap level from texcoords.
1408 * Then sample the texture level for four elements of a quad.
1409 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1412 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1413 const float s
[QUAD_SIZE
],
1414 const float t
[QUAD_SIZE
],
1415 const float p
[QUAD_SIZE
],
1416 const float c0
[QUAD_SIZE
],
1417 enum tgsi_sampler_control control
,
1418 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1420 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1421 const struct pipe_resource
*texture
= samp
->texture
;
1423 float lod
[QUAD_SIZE
];
1425 if (control
== tgsi_sampler_lod_bias
) {
1426 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1427 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1429 assert(control
== tgsi_sampler_lod_explicit
);
1431 memcpy(lod
, c0
, sizeof(lod
));
1434 /* XXX: Take into account all lod values.
1440 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1443 samp
->level
= (int)(lambda
+ 0.5) ;
1444 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1445 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1449 print_sample(__FUNCTION__
, rgba
);
1455 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1456 const float s
[QUAD_SIZE
],
1457 const float t
[QUAD_SIZE
],
1458 const float p
[QUAD_SIZE
],
1459 const float c0
[QUAD_SIZE
],
1460 enum tgsi_sampler_control control
,
1461 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1463 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1465 float lod
[QUAD_SIZE
];
1467 if (control
== tgsi_sampler_lod_bias
) {
1468 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1469 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1471 assert(control
== tgsi_sampler_lod_explicit
);
1473 memcpy(lod
, c0
, sizeof(lod
));
1476 /* XXX: Take into account all lod values.
1481 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1484 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1491 * Specialized version of mip_filter_linear with hard-wired calls to
1492 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1495 mip_filter_linear_2d_linear_repeat_POT(
1496 struct tgsi_sampler
*tgsi_sampler
,
1497 const float s
[QUAD_SIZE
],
1498 const float t
[QUAD_SIZE
],
1499 const float p
[QUAD_SIZE
],
1500 const float c0
[QUAD_SIZE
],
1501 enum tgsi_sampler_control control
,
1502 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1504 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1505 const struct pipe_resource
*texture
= samp
->texture
;
1508 float lod
[QUAD_SIZE
];
1510 if (control
== tgsi_sampler_lod_bias
) {
1511 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1512 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1514 assert(control
== tgsi_sampler_lod_explicit
);
1516 memcpy(lod
, c0
, sizeof(lod
));
1519 /* XXX: Take into account all lod values.
1522 level0
= (int)lambda
;
1524 /* Catches both negative and large values of level0:
1526 if ((unsigned)level0
>= texture
->last_level
) {
1530 samp
->level
= texture
->last_level
;
1532 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1535 float levelBlend
= lambda
- level0
;
1540 samp
->level
= level0
;
1541 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1543 samp
->level
= level0
+1;
1544 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1546 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1547 for (c
= 0; c
< 4; c
++) {
1548 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1554 print_sample(__FUNCTION__
, rgba
);
1561 * Do shadow/depth comparisons.
1564 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1565 const float s
[QUAD_SIZE
],
1566 const float t
[QUAD_SIZE
],
1567 const float p
[QUAD_SIZE
],
1568 const float c0
[QUAD_SIZE
],
1569 enum tgsi_sampler_control control
,
1570 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1572 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1573 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1574 int j
, k0
, k1
, k2
, k3
;
1577 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1580 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1581 * When we sampled the depth texture, the depth value was put into all
1582 * RGBA channels. We look at the red channel here.
1585 /* compare four texcoords vs. four texture samples */
1586 switch (sampler
->compare_func
) {
1587 case PIPE_FUNC_LESS
:
1588 k0
= p
[0] < rgba
[0][0];
1589 k1
= p
[1] < rgba
[0][1];
1590 k2
= p
[2] < rgba
[0][2];
1591 k3
= p
[3] < rgba
[0][3];
1593 case PIPE_FUNC_LEQUAL
:
1594 k0
= p
[0] <= rgba
[0][0];
1595 k1
= p
[1] <= rgba
[0][1];
1596 k2
= p
[2] <= rgba
[0][2];
1597 k3
= p
[3] <= rgba
[0][3];
1599 case PIPE_FUNC_GREATER
:
1600 k0
= p
[0] > rgba
[0][0];
1601 k1
= p
[1] > rgba
[0][1];
1602 k2
= p
[2] > rgba
[0][2];
1603 k3
= p
[3] > rgba
[0][3];
1605 case PIPE_FUNC_GEQUAL
:
1606 k0
= p
[0] >= rgba
[0][0];
1607 k1
= p
[1] >= rgba
[0][1];
1608 k2
= p
[2] >= rgba
[0][2];
1609 k3
= p
[3] >= rgba
[0][3];
1611 case PIPE_FUNC_EQUAL
:
1612 k0
= p
[0] == rgba
[0][0];
1613 k1
= p
[1] == rgba
[0][1];
1614 k2
= p
[2] == rgba
[0][2];
1615 k3
= p
[3] == rgba
[0][3];
1617 case PIPE_FUNC_NOTEQUAL
:
1618 k0
= p
[0] != rgba
[0][0];
1619 k1
= p
[1] != rgba
[0][1];
1620 k2
= p
[2] != rgba
[0][2];
1621 k3
= p
[3] != rgba
[0][3];
1623 case PIPE_FUNC_ALWAYS
:
1624 k0
= k1
= k2
= k3
= 1;
1626 case PIPE_FUNC_NEVER
:
1627 k0
= k1
= k2
= k3
= 0;
1630 k0
= k1
= k2
= k3
= 0;
1635 /* convert four pass/fail values to an intensity in [0,1] */
1636 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1638 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1639 for (j
= 0; j
< 4; j
++) {
1640 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1647 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1648 * Put face info into the sampler faces[] array.
1651 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1652 const float s
[QUAD_SIZE
],
1653 const float t
[QUAD_SIZE
],
1654 const float p
[QUAD_SIZE
],
1655 const float c0
[QUAD_SIZE
],
1656 enum tgsi_sampler_control control
,
1657 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1659 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1661 float ssss
[4], tttt
[4];
1665 direction target sc tc ma
1666 ---------- ------------------------------- --- --- ---
1667 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1668 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1669 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1670 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1671 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1672 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1675 /* Choose the cube face and compute new s/t coords for the 2D face.
1677 * Use the same cube face for all four pixels in the quad.
1679 * This isn't ideal, but if we want to use a different cube face
1680 * per pixel in the quad, we'd have to also compute the per-face
1681 * LOD here too. That's because the four post-face-selection
1682 * texcoords are no longer related to each other (they're
1683 * per-face!) so we can't use subtraction to compute the partial
1684 * deriviates to compute the LOD. Doing so (near cube edges
1685 * anyway) gives us pretty much random values.
1688 /* use the average of the four pixel's texcoords to choose the face */
1689 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1690 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1691 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1692 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1694 if (arx
>= ary
&& arx
>= arz
) {
1695 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
1696 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
1697 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1698 const float ima
= -0.5F
/ fabsf(s
[j
]);
1699 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
1700 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1701 samp
->faces
[j
] = face
;
1704 else if (ary
>= arx
&& ary
>= arz
) {
1705 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
1706 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
1707 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1708 const float ima
= -0.5F
/ fabsf(t
[j
]);
1709 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
1710 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
1711 samp
->faces
[j
] = face
;
1715 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
1716 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
1717 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1718 const float ima
= -0.5 / fabsf(p
[j
]);
1719 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
1720 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1721 samp
->faces
[j
] = face
;
1726 /* In our little pipeline, the compare stage is next. If compare
1727 * is not active, this will point somewhere deeper into the
1728 * pipeline, eg. to mip_filter or even img_filter.
1730 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1735 static wrap_nearest_func
1736 get_nearest_unorm_wrap(unsigned mode
)
1739 case PIPE_TEX_WRAP_CLAMP
:
1740 return wrap_nearest_unorm_clamp
;
1741 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1742 return wrap_nearest_unorm_clamp_to_edge
;
1743 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1744 return wrap_nearest_unorm_clamp_to_border
;
1747 return wrap_nearest_unorm_clamp
;
1752 static wrap_nearest_func
1753 get_nearest_wrap(unsigned mode
)
1756 case PIPE_TEX_WRAP_REPEAT
:
1757 return wrap_nearest_repeat
;
1758 case PIPE_TEX_WRAP_CLAMP
:
1759 return wrap_nearest_clamp
;
1760 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1761 return wrap_nearest_clamp_to_edge
;
1762 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1763 return wrap_nearest_clamp_to_border
;
1764 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1765 return wrap_nearest_mirror_repeat
;
1766 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1767 return wrap_nearest_mirror_clamp
;
1768 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1769 return wrap_nearest_mirror_clamp_to_edge
;
1770 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1771 return wrap_nearest_mirror_clamp_to_border
;
1774 return wrap_nearest_repeat
;
1779 static wrap_linear_func
1780 get_linear_unorm_wrap(unsigned mode
)
1783 case PIPE_TEX_WRAP_CLAMP
:
1784 return wrap_linear_unorm_clamp
;
1785 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1786 return wrap_linear_unorm_clamp_to_edge
;
1787 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1788 return wrap_linear_unorm_clamp_to_border
;
1791 return wrap_linear_unorm_clamp
;
1796 static wrap_linear_func
1797 get_linear_wrap(unsigned mode
)
1800 case PIPE_TEX_WRAP_REPEAT
:
1801 return wrap_linear_repeat
;
1802 case PIPE_TEX_WRAP_CLAMP
:
1803 return wrap_linear_clamp
;
1804 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1805 return wrap_linear_clamp_to_edge
;
1806 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1807 return wrap_linear_clamp_to_border
;
1808 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1809 return wrap_linear_mirror_repeat
;
1810 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1811 return wrap_linear_mirror_clamp
;
1812 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1813 return wrap_linear_mirror_clamp_to_edge
;
1814 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1815 return wrap_linear_mirror_clamp_to_border
;
1818 return wrap_linear_repeat
;
1823 static compute_lambda_func
1824 get_lambda_func(const union sp_sampler_key key
)
1826 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1827 return compute_lambda_vert
;
1829 switch (key
.bits
.target
) {
1830 case PIPE_TEXTURE_1D
:
1831 return compute_lambda_1d
;
1832 case PIPE_TEXTURE_2D
:
1833 case PIPE_TEXTURE_RECT
:
1834 case PIPE_TEXTURE_CUBE
:
1835 return compute_lambda_2d
;
1836 case PIPE_TEXTURE_3D
:
1837 return compute_lambda_3d
;
1840 return compute_lambda_1d
;
1846 get_img_filter(const union sp_sampler_key key
,
1848 const struct pipe_sampler_state
*sampler
)
1850 switch (key
.bits
.target
) {
1851 case PIPE_TEXTURE_1D
:
1852 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1853 return img_filter_1d_nearest
;
1855 return img_filter_1d_linear
;
1857 case PIPE_TEXTURE_2D
:
1858 case PIPE_TEXTURE_RECT
:
1859 /* Try for fast path:
1861 if (key
.bits
.is_pot
&&
1862 sampler
->wrap_s
== sampler
->wrap_t
&&
1863 sampler
->normalized_coords
)
1865 switch (sampler
->wrap_s
) {
1866 case PIPE_TEX_WRAP_REPEAT
:
1868 case PIPE_TEX_FILTER_NEAREST
:
1869 return img_filter_2d_nearest_repeat_POT
;
1870 case PIPE_TEX_FILTER_LINEAR
:
1871 return img_filter_2d_linear_repeat_POT
;
1876 case PIPE_TEX_WRAP_CLAMP
:
1878 case PIPE_TEX_FILTER_NEAREST
:
1879 return img_filter_2d_nearest_clamp_POT
;
1885 /* Otherwise use default versions:
1887 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1888 return img_filter_2d_nearest
;
1890 return img_filter_2d_linear
;
1892 case PIPE_TEXTURE_CUBE
:
1893 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1894 return img_filter_cube_nearest
;
1896 return img_filter_cube_linear
;
1898 case PIPE_TEXTURE_3D
:
1899 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1900 return img_filter_3d_nearest
;
1902 return img_filter_3d_linear
;
1906 return img_filter_1d_nearest
;
1912 * Bind the given texture object and texture cache to the sampler varient.
1915 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1916 struct softpipe_tex_tile_cache
*tex_cache
,
1917 const struct pipe_resource
*texture
)
1919 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1921 samp
->texture
= texture
;
1922 samp
->cache
= tex_cache
;
1923 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
1924 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
1925 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1930 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1937 * Create a sampler varient for a given set of non-orthogonal state.
1939 struct sp_sampler_varient
*
1940 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1941 const union sp_sampler_key key
)
1943 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1947 samp
->sampler
= sampler
;
1950 /* Note that (for instance) linear_texcoord_s and
1951 * nearest_texcoord_s may be active at the same time, if the
1952 * sampler min_img_filter differs from its mag_img_filter.
1954 if (sampler
->normalized_coords
) {
1955 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1956 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1957 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1959 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1960 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1961 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1964 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1965 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1966 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1968 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1969 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1970 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1973 samp
->compute_lambda
= get_lambda_func( key
);
1975 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1976 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1978 switch (sampler
->min_mip_filter
) {
1979 case PIPE_TEX_MIPFILTER_NONE
:
1980 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1981 samp
->mip_filter
= samp
->min_img_filter
;
1983 samp
->mip_filter
= mip_filter_none
;
1986 case PIPE_TEX_MIPFILTER_NEAREST
:
1987 samp
->mip_filter
= mip_filter_nearest
;
1990 case PIPE_TEX_MIPFILTER_LINEAR
:
1991 if (key
.bits
.is_pot
&&
1992 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1993 sampler
->normalized_coords
&&
1994 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1995 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1996 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1998 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2002 samp
->mip_filter
= mip_filter_linear
;
2007 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2008 samp
->compare
= sample_compare
;
2011 /* Skip compare operation by promoting the mip_filter function
2014 samp
->compare
= samp
->mip_filter
;
2017 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
2018 samp
->base
.get_samples
= sample_cube
;
2026 /* Skip cube face determination by promoting the compare
2029 samp
->base
.get_samples
= samp
->compare
;