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;
543 * Do coordinate to array index conversion. For array textures.
546 wrap_array_layer(const float coord
[4], unsigned size
, int layer
[4])
549 for (ch
= 0; ch
< 4; ch
++) {
550 int c
= util_ifloor(coord
[ch
] + 0.5F
);
551 layer
[ch
] = CLAMP(c
, 0, size
- 1);
557 * Examine the quad's texture coordinates to compute the partial
558 * derivatives w.r.t X and Y, then compute lambda (level of detail).
561 compute_lambda_1d(const struct sp_sampler_variant
*samp
,
562 const float s
[QUAD_SIZE
],
563 const float t
[QUAD_SIZE
],
564 const float p
[QUAD_SIZE
])
566 const struct pipe_resource
*texture
= samp
->view
->texture
;
567 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
568 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
569 float rho
= MAX2(dsdx
, dsdy
) * texture
->width0
;
571 return util_fast_log2(rho
);
576 compute_lambda_2d(const struct sp_sampler_variant
*samp
,
577 const float s
[QUAD_SIZE
],
578 const float t
[QUAD_SIZE
],
579 const float p
[QUAD_SIZE
])
581 const struct pipe_resource
*texture
= samp
->view
->texture
;
582 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
583 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
584 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
585 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
586 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
587 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
588 float rho
= MAX2(maxx
, maxy
);
590 return util_fast_log2(rho
);
595 compute_lambda_3d(const struct sp_sampler_variant
*samp
,
596 const float s
[QUAD_SIZE
],
597 const float t
[QUAD_SIZE
],
598 const float p
[QUAD_SIZE
])
600 const struct pipe_resource
*texture
= samp
->view
->texture
;
601 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
602 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
603 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
604 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
605 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
606 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
607 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
608 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
609 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth0
;
612 rho
= MAX2(maxx
, maxy
);
613 rho
= MAX2(rho
, maxz
);
615 return util_fast_log2(rho
);
620 * Compute lambda for a vertex texture sampler.
621 * Since there aren't derivatives to use, just return 0.
624 compute_lambda_vert(const struct sp_sampler_variant
*samp
,
625 const float s
[QUAD_SIZE
],
626 const float t
[QUAD_SIZE
],
627 const float p
[QUAD_SIZE
])
635 * Get a texel from a texture, using the texture tile cache.
637 * \param addr the template tex address containing cube, z, face info.
638 * \param x the x coord of texel within 2D image
639 * \param y the y coord of texel within 2D image
640 * \param rgba the quad to put the texel/color into
642 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
643 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
649 static INLINE
const float *
650 get_texel_2d_no_border(const struct sp_sampler_variant
*samp
,
651 union tex_tile_address addr
, int x
, int y
)
653 const struct softpipe_tex_cached_tile
*tile
;
655 addr
.bits
.x
= x
/ TILE_SIZE
;
656 addr
.bits
.y
= y
/ TILE_SIZE
;
660 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
662 return &tile
->data
.color
[y
][x
][0];
666 static INLINE
const float *
667 get_texel_2d(const struct sp_sampler_variant
*samp
,
668 union tex_tile_address addr
, int x
, int y
)
670 const struct pipe_resource
*texture
= samp
->view
->texture
;
671 unsigned level
= addr
.bits
.level
;
673 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
674 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
675 return samp
->sampler
->border_color
;
678 return get_texel_2d_no_border( samp
, addr
, x
, y
);
683 /* Gather a quad of adjacent texels within a tile:
686 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant
*samp
,
687 union tex_tile_address addr
,
688 unsigned x
, unsigned y
,
691 const struct softpipe_tex_cached_tile
*tile
;
693 addr
.bits
.x
= x
/ TILE_SIZE
;
694 addr
.bits
.y
= y
/ TILE_SIZE
;
698 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
700 out
[0] = &tile
->data
.color
[y
][x
][0];
701 out
[1] = &tile
->data
.color
[y
][x
+1][0];
702 out
[2] = &tile
->data
.color
[y
+1][x
][0];
703 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
707 /* Gather a quad of potentially non-adjacent texels:
710 get_texel_quad_2d_no_border(const struct sp_sampler_variant
*samp
,
711 union tex_tile_address addr
,
716 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
717 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
718 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
719 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
722 /* Can involve a lot of unnecessary checks for border color:
725 get_texel_quad_2d(const struct sp_sampler_variant
*samp
,
726 union tex_tile_address addr
,
731 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
732 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
733 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
734 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
741 static INLINE
const float *
742 get_texel_3d_no_border(const struct sp_sampler_variant
*samp
,
743 union tex_tile_address addr
, int x
, int y
, int z
)
745 const struct softpipe_tex_cached_tile
*tile
;
747 addr
.bits
.x
= x
/ TILE_SIZE
;
748 addr
.bits
.y
= y
/ TILE_SIZE
;
753 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
755 return &tile
->data
.color
[y
][x
][0];
759 static INLINE
const float *
760 get_texel_3d(const struct sp_sampler_variant
*samp
,
761 union tex_tile_address addr
, int x
, int y
, int z
)
763 const struct pipe_resource
*texture
= samp
->view
->texture
;
764 unsigned level
= addr
.bits
.level
;
766 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
767 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
768 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
769 return samp
->sampler
->border_color
;
772 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
777 /* Get texel pointer for 1D array texture */
778 static INLINE
const float *
779 get_texel_1d_array(const struct sp_sampler_variant
*samp
,
780 union tex_tile_address addr
, int x
, int y
)
782 const struct pipe_resource
*texture
= samp
->view
->texture
;
783 unsigned level
= addr
.bits
.level
;
785 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
786 return samp
->sampler
->border_color
;
789 return get_texel_2d_no_border(samp
, addr
, x
, y
);
794 /* Get texel pointer for 2D array texture */
795 static INLINE
const float *
796 get_texel_2d_array(const struct sp_sampler_variant
*samp
,
797 union tex_tile_address addr
, int x
, int y
, int layer
)
799 const struct pipe_resource
*texture
= samp
->view
->texture
;
800 unsigned level
= addr
.bits
.level
;
802 assert(layer
< texture
->array_size
);
804 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
805 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
806 return samp
->sampler
->border_color
;
809 return get_texel_3d_no_border(samp
, addr
, x
, y
, layer
);
815 * Given the logbase2 of a mipmap's base level size and a mipmap level,
816 * return the size (in texels) of that mipmap level.
817 * For example, if level[0].width = 256 then base_pot will be 8.
818 * If level = 2, then we'll return 64 (the width at level=2).
819 * Return 1 if level > base_pot.
821 static INLINE
unsigned
822 pot_level_size(unsigned base_pot
, unsigned level
)
824 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
829 print_sample(const char *function
, float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
831 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
833 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
834 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
835 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
836 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
840 /* Some image-filter fastpaths:
843 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
844 const float s
[QUAD_SIZE
],
845 const float t
[QUAD_SIZE
],
846 const float p
[QUAD_SIZE
],
847 const float c0
[QUAD_SIZE
],
848 enum tgsi_sampler_control control
,
849 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
851 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
853 unsigned level
= samp
->level
;
854 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
855 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
856 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
857 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
858 union tex_tile_address addr
;
861 addr
.bits
.level
= samp
->level
;
863 for (j
= 0; j
< QUAD_SIZE
; j
++) {
866 float u
= s
[j
] * xpot
- 0.5F
;
867 float v
= t
[j
] * ypot
- 0.5F
;
869 int uflr
= util_ifloor(u
);
870 int vflr
= util_ifloor(v
);
872 float xw
= u
- (float)uflr
;
873 float yw
= v
- (float)vflr
;
875 int x0
= uflr
& (xpot
- 1);
876 int y0
= vflr
& (ypot
- 1);
880 /* Can we fetch all four at once:
882 if (x0
< xmax
&& y0
< ymax
) {
883 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
886 unsigned x1
= (x0
+ 1) & (xpot
- 1);
887 unsigned y1
= (y0
+ 1) & (ypot
- 1);
888 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
891 /* interpolate R, G, B, A */
892 for (c
= 0; c
< 4; c
++) {
893 rgba
[c
][j
] = lerp_2d(xw
, yw
,
900 print_sample(__FUNCTION__
, rgba
);
906 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
907 const float s
[QUAD_SIZE
],
908 const float t
[QUAD_SIZE
],
909 const float p
[QUAD_SIZE
],
910 const float c0
[QUAD_SIZE
],
911 enum tgsi_sampler_control control
,
912 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
914 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
916 unsigned level
= samp
->level
;
917 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
918 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
919 union tex_tile_address addr
;
922 addr
.bits
.level
= samp
->level
;
924 for (j
= 0; j
< QUAD_SIZE
; j
++) {
927 float u
= s
[j
] * xpot
;
928 float v
= t
[j
] * ypot
;
930 int uflr
= util_ifloor(u
);
931 int vflr
= util_ifloor(v
);
933 int x0
= uflr
& (xpot
- 1);
934 int y0
= vflr
& (ypot
- 1);
936 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
938 for (c
= 0; c
< 4; c
++) {
944 print_sample(__FUNCTION__
, rgba
);
950 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
951 const float s
[QUAD_SIZE
],
952 const float t
[QUAD_SIZE
],
953 const float p
[QUAD_SIZE
],
954 const float c0
[QUAD_SIZE
],
955 enum tgsi_sampler_control control
,
956 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
958 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
960 unsigned level
= samp
->level
;
961 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
962 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
963 union tex_tile_address addr
;
966 addr
.bits
.level
= samp
->level
;
968 for (j
= 0; j
< QUAD_SIZE
; j
++) {
971 float u
= s
[j
] * xpot
;
972 float v
= t
[j
] * ypot
;
980 else if (x0
> xpot
- 1)
986 else if (y0
> ypot
- 1)
989 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
991 for (c
= 0; c
< 4; c
++) {
997 print_sample(__FUNCTION__
, rgba
);
1003 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1004 const float s
[QUAD_SIZE
],
1005 const float t
[QUAD_SIZE
],
1006 const float p
[QUAD_SIZE
],
1007 const float c0
[QUAD_SIZE
],
1008 enum tgsi_sampler_control control
,
1009 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1011 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1012 const struct pipe_resource
*texture
= samp
->view
->texture
;
1016 union tex_tile_address addr
;
1018 level0
= samp
->level
;
1019 width
= u_minify(texture
->width0
, level0
);
1024 addr
.bits
.level
= samp
->level
;
1026 samp
->nearest_texcoord_s(s
, width
, x
);
1028 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1029 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
1031 for (c
= 0; c
< 4; c
++) {
1032 rgba
[c
][j
] = out
[c
];
1037 print_sample(__FUNCTION__
, rgba
);
1043 img_filter_1d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1044 const float s
[QUAD_SIZE
],
1045 const float t
[QUAD_SIZE
],
1046 const float p
[QUAD_SIZE
],
1047 const float c0
[QUAD_SIZE
],
1048 enum tgsi_sampler_control control
,
1049 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1051 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1052 const struct pipe_resource
*texture
= samp
->view
->texture
;
1056 union tex_tile_address addr
;
1058 level0
= samp
->level
;
1059 width
= u_minify(texture
->width0
, level0
);
1064 addr
.bits
.level
= samp
->level
;
1066 samp
->nearest_texcoord_s(s
, width
, x
);
1067 wrap_array_layer(t
, texture
->array_size
, layer
);
1069 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1070 const float *out
= get_texel_1d_array(samp
, addr
, x
[j
], layer
[j
]);
1072 for (c
= 0; c
< 4; c
++) {
1073 rgba
[c
][j
] = out
[c
];
1078 print_sample(__FUNCTION__
, rgba
);
1084 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1085 const float s
[QUAD_SIZE
],
1086 const float t
[QUAD_SIZE
],
1087 const float p
[QUAD_SIZE
],
1088 const float c0
[QUAD_SIZE
],
1089 enum tgsi_sampler_control control
,
1090 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1092 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1093 const struct pipe_resource
*texture
= samp
->view
->texture
;
1097 union tex_tile_address addr
;
1100 level0
= samp
->level
;
1101 width
= u_minify(texture
->width0
, level0
);
1102 height
= u_minify(texture
->height0
, level0
);
1108 addr
.bits
.level
= samp
->level
;
1110 samp
->nearest_texcoord_s(s
, width
, x
);
1111 samp
->nearest_texcoord_t(t
, height
, y
);
1113 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1114 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
1116 for (c
= 0; c
< 4; c
++) {
1117 rgba
[c
][j
] = out
[c
];
1122 print_sample(__FUNCTION__
, rgba
);
1128 img_filter_2d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1129 const float s
[QUAD_SIZE
],
1130 const float t
[QUAD_SIZE
],
1131 const float p
[QUAD_SIZE
],
1132 const float c0
[QUAD_SIZE
],
1133 enum tgsi_sampler_control control
,
1134 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1136 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1137 const struct pipe_resource
*texture
= samp
->view
->texture
;
1140 int x
[4], y
[4], layer
[4];
1141 union tex_tile_address addr
;
1143 level0
= samp
->level
;
1144 width
= u_minify(texture
->width0
, level0
);
1145 height
= u_minify(texture
->height0
, level0
);
1151 addr
.bits
.level
= samp
->level
;
1153 samp
->nearest_texcoord_s(s
, width
, x
);
1154 samp
->nearest_texcoord_t(t
, height
, y
);
1155 wrap_array_layer(p
, texture
->array_size
, layer
);
1157 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1158 const float *out
= get_texel_2d_array(samp
, addr
, x
[j
], y
[j
], layer
[j
]);
1160 for (c
= 0; c
< 4; c
++) {
1161 rgba
[c
][j
] = out
[c
];
1166 print_sample(__FUNCTION__
, rgba
);
1171 static INLINE
union tex_tile_address
1172 face(union tex_tile_address addr
, unsigned face
)
1174 addr
.bits
.face
= face
;
1180 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1181 const float s
[QUAD_SIZE
],
1182 const float t
[QUAD_SIZE
],
1183 const float p
[QUAD_SIZE
],
1184 const float c0
[QUAD_SIZE
],
1185 enum tgsi_sampler_control control
,
1186 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1188 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1189 const struct pipe_resource
*texture
= samp
->view
->texture
;
1190 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1194 union tex_tile_address addr
;
1196 level0
= samp
->level
;
1197 width
= u_minify(texture
->width0
, level0
);
1198 height
= u_minify(texture
->height0
, level0
);
1204 addr
.bits
.level
= samp
->level
;
1206 samp
->nearest_texcoord_s(s
, width
, x
);
1207 samp
->nearest_texcoord_t(t
, height
, y
);
1209 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1210 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1212 for (c
= 0; c
< 4; c
++) {
1213 rgba
[c
][j
] = out
[c
];
1218 print_sample(__FUNCTION__
, rgba
);
1224 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1225 const float s
[QUAD_SIZE
],
1226 const float t
[QUAD_SIZE
],
1227 const float p
[QUAD_SIZE
],
1228 const float c0
[QUAD_SIZE
],
1229 enum tgsi_sampler_control control
,
1230 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1232 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1233 const struct pipe_resource
*texture
= samp
->view
->texture
;
1235 int width
, height
, depth
;
1236 int x
[4], y
[4], z
[4];
1237 union tex_tile_address addr
;
1239 level0
= samp
->level
;
1240 width
= u_minify(texture
->width0
, level0
);
1241 height
= u_minify(texture
->height0
, level0
);
1242 depth
= u_minify(texture
->depth0
, level0
);
1248 samp
->nearest_texcoord_s(s
, width
, x
);
1249 samp
->nearest_texcoord_t(t
, height
, y
);
1250 samp
->nearest_texcoord_p(p
, depth
, z
);
1253 addr
.bits
.level
= samp
->level
;
1255 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1256 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1258 for (c
= 0; c
< 4; c
++) {
1259 rgba
[c
][j
] = out
[c
];
1266 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1267 const float s
[QUAD_SIZE
],
1268 const float t
[QUAD_SIZE
],
1269 const float p
[QUAD_SIZE
],
1270 const float c0
[QUAD_SIZE
],
1271 enum tgsi_sampler_control control
,
1272 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1274 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1275 const struct pipe_resource
*texture
= samp
->view
->texture
;
1279 float xw
[4]; /* weights */
1280 union tex_tile_address addr
;
1282 level0
= samp
->level
;
1283 width
= u_minify(texture
->width0
, level0
);
1288 addr
.bits
.level
= samp
->level
;
1290 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1292 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1293 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1294 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1297 /* interpolate R, G, B, A */
1298 for (c
= 0; c
< 4; c
++) {
1299 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1306 img_filter_1d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1307 const float s
[QUAD_SIZE
],
1308 const float t
[QUAD_SIZE
],
1309 const float p
[QUAD_SIZE
],
1310 const float c0
[QUAD_SIZE
],
1311 enum tgsi_sampler_control control
,
1312 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1314 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1315 const struct pipe_resource
*texture
= samp
->view
->texture
;
1318 int x0
[4], x1
[4], layer
[4];
1319 float xw
[4]; /* weights */
1320 union tex_tile_address addr
;
1322 level0
= samp
->level
;
1323 width
= u_minify(texture
->width0
, level0
);
1328 addr
.bits
.level
= samp
->level
;
1330 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1331 wrap_array_layer(t
, texture
->array_size
, layer
);
1333 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1334 const float *tx0
= get_texel_1d_array(samp
, addr
, x0
[j
], layer
[j
]);
1335 const float *tx1
= get_texel_1d_array(samp
, addr
, x1
[j
], layer
[j
]);
1338 /* interpolate R, G, B, A */
1339 for (c
= 0; c
< 4; c
++) {
1340 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1347 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1348 const float s
[QUAD_SIZE
],
1349 const float t
[QUAD_SIZE
],
1350 const float p
[QUAD_SIZE
],
1351 const float c0
[QUAD_SIZE
],
1352 enum tgsi_sampler_control control
,
1353 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1355 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1356 const struct pipe_resource
*texture
= samp
->view
->texture
;
1359 int x0
[4], y0
[4], x1
[4], y1
[4];
1360 float xw
[4], yw
[4]; /* weights */
1361 union tex_tile_address addr
;
1363 level0
= samp
->level
;
1364 width
= u_minify(texture
->width0
, level0
);
1365 height
= u_minify(texture
->height0
, level0
);
1371 addr
.bits
.level
= samp
->level
;
1373 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1374 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1376 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1377 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1378 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1379 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1380 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1383 /* interpolate R, G, B, A */
1384 for (c
= 0; c
< 4; c
++) {
1385 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1394 img_filter_2d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1395 const float s
[QUAD_SIZE
],
1396 const float t
[QUAD_SIZE
],
1397 const float p
[QUAD_SIZE
],
1398 const float c0
[QUAD_SIZE
],
1399 enum tgsi_sampler_control control
,
1400 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1402 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1403 const struct pipe_resource
*texture
= samp
->view
->texture
;
1406 int x0
[4], y0
[4], x1
[4], y1
[4], layer
[4];
1407 float xw
[4], yw
[4]; /* weights */
1408 union tex_tile_address addr
;
1410 level0
= samp
->level
;
1411 width
= u_minify(texture
->width0
, level0
);
1412 height
= u_minify(texture
->height0
, level0
);
1418 addr
.bits
.level
= samp
->level
;
1420 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1421 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1422 wrap_array_layer(p
, texture
->array_size
, layer
);
1424 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1425 const float *tx0
= get_texel_2d_array(samp
, addr
, x0
[j
], y0
[j
], layer
[j
]);
1426 const float *tx1
= get_texel_2d_array(samp
, addr
, x1
[j
], y0
[j
], layer
[j
]);
1427 const float *tx2
= get_texel_2d_array(samp
, addr
, x0
[j
], y1
[j
], layer
[j
]);
1428 const float *tx3
= get_texel_2d_array(samp
, addr
, x1
[j
], y1
[j
], layer
[j
]);
1431 /* interpolate R, G, B, A */
1432 for (c
= 0; c
< 4; c
++) {
1433 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1442 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1443 const float s
[QUAD_SIZE
],
1444 const float t
[QUAD_SIZE
],
1445 const float p
[QUAD_SIZE
],
1446 const float c0
[QUAD_SIZE
],
1447 enum tgsi_sampler_control control
,
1448 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1450 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1451 const struct pipe_resource
*texture
= samp
->view
->texture
;
1452 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1455 int x0
[4], y0
[4], x1
[4], y1
[4];
1456 float xw
[4], yw
[4]; /* weights */
1457 union tex_tile_address addr
;
1459 level0
= samp
->level
;
1460 width
= u_minify(texture
->width0
, level0
);
1461 height
= u_minify(texture
->height0
, level0
);
1467 addr
.bits
.level
= samp
->level
;
1469 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1470 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1472 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1473 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1474 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1475 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1476 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1477 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1480 /* interpolate R, G, B, A */
1481 for (c
= 0; c
< 4; c
++) {
1482 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1491 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1492 const float s
[QUAD_SIZE
],
1493 const float t
[QUAD_SIZE
],
1494 const float p
[QUAD_SIZE
],
1495 const float c0
[QUAD_SIZE
],
1496 enum tgsi_sampler_control control
,
1497 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1499 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1500 const struct pipe_resource
*texture
= samp
->view
->texture
;
1502 int width
, height
, depth
;
1503 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1504 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1505 union tex_tile_address addr
;
1507 level0
= samp
->level
;
1508 width
= u_minify(texture
->width0
, level0
);
1509 height
= u_minify(texture
->height0
, level0
);
1510 depth
= u_minify(texture
->depth0
, level0
);
1513 addr
.bits
.level
= level0
;
1519 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1520 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1521 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1523 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1526 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1527 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1528 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1529 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1531 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1532 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1533 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1534 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1536 /* interpolate R, G, B, A */
1537 for (c
= 0; c
< 4; c
++) {
1538 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1548 /* Calculate level of detail for every fragment.
1549 * Note that lambda has already been biased by global LOD bias.
1552 compute_lod(const struct pipe_sampler_state
*sampler
,
1553 const float biased_lambda
,
1554 const float lodbias
[QUAD_SIZE
],
1555 float lod
[QUAD_SIZE
])
1559 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1560 lod
[i
] = biased_lambda
+ lodbias
[i
];
1561 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1567 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1568 const float s
[QUAD_SIZE
],
1569 const float t
[QUAD_SIZE
],
1570 const float p
[QUAD_SIZE
],
1571 const float c0
[QUAD_SIZE
],
1572 enum tgsi_sampler_control control
,
1573 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1575 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1576 const struct pipe_resource
*texture
= samp
->view
->texture
;
1579 float lod
[QUAD_SIZE
];
1581 if (control
== tgsi_sampler_lod_bias
) {
1582 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1583 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1585 assert(control
== tgsi_sampler_lod_explicit
);
1587 memcpy(lod
, c0
, sizeof(lod
));
1590 /* XXX: Take into account all lod values.
1593 level0
= (int)lambda
;
1597 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1599 else if (level0
>= texture
->last_level
) {
1600 samp
->level
= texture
->last_level
;
1601 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1604 float levelBlend
= lambda
- level0
;
1609 samp
->level
= level0
;
1610 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1612 samp
->level
= level0
+1;
1613 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1615 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1616 for (c
= 0; c
< 4; c
++) {
1617 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1623 print_sample(__FUNCTION__
, rgba
);
1629 * Compute nearest mipmap level from texcoords.
1630 * Then sample the texture level for four elements of a quad.
1631 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1634 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1635 const float s
[QUAD_SIZE
],
1636 const float t
[QUAD_SIZE
],
1637 const float p
[QUAD_SIZE
],
1638 const float c0
[QUAD_SIZE
],
1639 enum tgsi_sampler_control control
,
1640 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1642 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1643 const struct pipe_resource
*texture
= samp
->view
->texture
;
1645 float lod
[QUAD_SIZE
];
1647 if (control
== tgsi_sampler_lod_bias
) {
1648 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1649 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1651 assert(control
== tgsi_sampler_lod_explicit
);
1653 memcpy(lod
, c0
, sizeof(lod
));
1656 /* XXX: Take into account all lod values.
1662 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1665 samp
->level
= (int)(lambda
+ 0.5) ;
1666 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1667 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1671 print_sample(__FUNCTION__
, rgba
);
1677 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1678 const float s
[QUAD_SIZE
],
1679 const float t
[QUAD_SIZE
],
1680 const float p
[QUAD_SIZE
],
1681 const float c0
[QUAD_SIZE
],
1682 enum tgsi_sampler_control control
,
1683 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1685 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1687 float lod
[QUAD_SIZE
];
1689 if (control
== tgsi_sampler_lod_bias
) {
1690 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1691 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1693 assert(control
== tgsi_sampler_lod_explicit
);
1695 memcpy(lod
, c0
, sizeof(lod
));
1698 /* XXX: Take into account all lod values.
1703 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1706 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1713 * Specialized version of mip_filter_linear with hard-wired calls to
1714 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1717 mip_filter_linear_2d_linear_repeat_POT(
1718 struct tgsi_sampler
*tgsi_sampler
,
1719 const float s
[QUAD_SIZE
],
1720 const float t
[QUAD_SIZE
],
1721 const float p
[QUAD_SIZE
],
1722 const float c0
[QUAD_SIZE
],
1723 enum tgsi_sampler_control control
,
1724 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1726 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1727 const struct pipe_resource
*texture
= samp
->view
->texture
;
1730 float lod
[QUAD_SIZE
];
1732 if (control
== tgsi_sampler_lod_bias
) {
1733 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1734 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1736 assert(control
== tgsi_sampler_lod_explicit
);
1738 memcpy(lod
, c0
, sizeof(lod
));
1741 /* XXX: Take into account all lod values.
1744 level0
= (int)lambda
;
1746 /* Catches both negative and large values of level0:
1748 if ((unsigned)level0
>= texture
->last_level
) {
1752 samp
->level
= texture
->last_level
;
1754 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1757 float levelBlend
= lambda
- level0
;
1762 samp
->level
= level0
;
1763 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1765 samp
->level
= level0
+1;
1766 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1768 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1769 for (c
= 0; c
< 4; c
++) {
1770 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1776 print_sample(__FUNCTION__
, rgba
);
1783 * Do shadow/depth comparisons.
1786 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1787 const float s
[QUAD_SIZE
],
1788 const float t
[QUAD_SIZE
],
1789 const float p
[QUAD_SIZE
],
1790 const float c0
[QUAD_SIZE
],
1791 enum tgsi_sampler_control control
,
1792 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1794 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1795 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1796 int j
, k0
, k1
, k2
, k3
;
1799 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1802 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1803 * When we sampled the depth texture, the depth value was put into all
1804 * RGBA channels. We look at the red channel here.
1807 /* compare four texcoords vs. four texture samples */
1808 switch (sampler
->compare_func
) {
1809 case PIPE_FUNC_LESS
:
1810 k0
= p
[0] < rgba
[0][0];
1811 k1
= p
[1] < rgba
[0][1];
1812 k2
= p
[2] < rgba
[0][2];
1813 k3
= p
[3] < rgba
[0][3];
1815 case PIPE_FUNC_LEQUAL
:
1816 k0
= p
[0] <= rgba
[0][0];
1817 k1
= p
[1] <= rgba
[0][1];
1818 k2
= p
[2] <= rgba
[0][2];
1819 k3
= p
[3] <= rgba
[0][3];
1821 case PIPE_FUNC_GREATER
:
1822 k0
= p
[0] > rgba
[0][0];
1823 k1
= p
[1] > rgba
[0][1];
1824 k2
= p
[2] > rgba
[0][2];
1825 k3
= p
[3] > rgba
[0][3];
1827 case PIPE_FUNC_GEQUAL
:
1828 k0
= p
[0] >= rgba
[0][0];
1829 k1
= p
[1] >= rgba
[0][1];
1830 k2
= p
[2] >= rgba
[0][2];
1831 k3
= p
[3] >= rgba
[0][3];
1833 case PIPE_FUNC_EQUAL
:
1834 k0
= p
[0] == rgba
[0][0];
1835 k1
= p
[1] == rgba
[0][1];
1836 k2
= p
[2] == rgba
[0][2];
1837 k3
= p
[3] == rgba
[0][3];
1839 case PIPE_FUNC_NOTEQUAL
:
1840 k0
= p
[0] != rgba
[0][0];
1841 k1
= p
[1] != rgba
[0][1];
1842 k2
= p
[2] != rgba
[0][2];
1843 k3
= p
[3] != rgba
[0][3];
1845 case PIPE_FUNC_ALWAYS
:
1846 k0
= k1
= k2
= k3
= 1;
1848 case PIPE_FUNC_NEVER
:
1849 k0
= k1
= k2
= k3
= 0;
1852 k0
= k1
= k2
= k3
= 0;
1857 /* convert four pass/fail values to an intensity in [0,1] */
1858 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1860 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1861 for (j
= 0; j
< 4; j
++) {
1862 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1869 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1870 * Put face info into the sampler faces[] array.
1873 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1874 const float s
[QUAD_SIZE
],
1875 const float t
[QUAD_SIZE
],
1876 const float p
[QUAD_SIZE
],
1877 const float c0
[QUAD_SIZE
],
1878 enum tgsi_sampler_control control
,
1879 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1881 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1883 float ssss
[4], tttt
[4];
1887 direction target sc tc ma
1888 ---------- ------------------------------- --- --- ---
1889 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1890 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1891 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1892 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1893 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1894 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1897 /* Choose the cube face and compute new s/t coords for the 2D face.
1899 * Use the same cube face for all four pixels in the quad.
1901 * This isn't ideal, but if we want to use a different cube face
1902 * per pixel in the quad, we'd have to also compute the per-face
1903 * LOD here too. That's because the four post-face-selection
1904 * texcoords are no longer related to each other (they're
1905 * per-face!) so we can't use subtraction to compute the partial
1906 * deriviates to compute the LOD. Doing so (near cube edges
1907 * anyway) gives us pretty much random values.
1910 /* use the average of the four pixel's texcoords to choose the face */
1911 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1912 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1913 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1914 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1916 if (arx
>= ary
&& arx
>= arz
) {
1917 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
1918 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
1919 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1920 const float ima
= -0.5F
/ fabsf(s
[j
]);
1921 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
1922 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1923 samp
->faces
[j
] = face
;
1926 else if (ary
>= arx
&& ary
>= arz
) {
1927 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
1928 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
1929 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1930 const float ima
= -0.5F
/ fabsf(t
[j
]);
1931 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
1932 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
1933 samp
->faces
[j
] = face
;
1937 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
1938 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
1939 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1940 const float ima
= -0.5 / fabsf(p
[j
]);
1941 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
1942 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1943 samp
->faces
[j
] = face
;
1948 /* In our little pipeline, the compare stage is next. If compare
1949 * is not active, this will point somewhere deeper into the
1950 * pipeline, eg. to mip_filter or even img_filter.
1952 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1957 sample_swizzle(struct tgsi_sampler
*tgsi_sampler
,
1958 const float s
[QUAD_SIZE
],
1959 const float t
[QUAD_SIZE
],
1960 const float p
[QUAD_SIZE
],
1961 const float c0
[QUAD_SIZE
],
1962 enum tgsi_sampler_control control
,
1963 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1965 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1966 float rgba_temp
[NUM_CHANNELS
][QUAD_SIZE
];
1967 const unsigned swizzle_r
= samp
->key
.bits
.swizzle_r
;
1968 const unsigned swizzle_g
= samp
->key
.bits
.swizzle_g
;
1969 const unsigned swizzle_b
= samp
->key
.bits
.swizzle_b
;
1970 const unsigned swizzle_a
= samp
->key
.bits
.swizzle_a
;
1973 samp
->sample_target(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba_temp
);
1975 switch (swizzle_r
) {
1976 case PIPE_SWIZZLE_ZERO
:
1977 for (j
= 0; j
< 4; j
++)
1980 case PIPE_SWIZZLE_ONE
:
1981 for (j
= 0; j
< 4; j
++)
1985 assert(swizzle_r
< 4);
1986 for (j
= 0; j
< 4; j
++)
1987 rgba
[0][j
] = rgba_temp
[swizzle_r
][j
];
1990 switch (swizzle_g
) {
1991 case PIPE_SWIZZLE_ZERO
:
1992 for (j
= 0; j
< 4; j
++)
1995 case PIPE_SWIZZLE_ONE
:
1996 for (j
= 0; j
< 4; j
++)
2000 assert(swizzle_g
< 4);
2001 for (j
= 0; j
< 4; j
++)
2002 rgba
[1][j
] = rgba_temp
[swizzle_g
][j
];
2005 switch (swizzle_b
) {
2006 case PIPE_SWIZZLE_ZERO
:
2007 for (j
= 0; j
< 4; j
++)
2010 case PIPE_SWIZZLE_ONE
:
2011 for (j
= 0; j
< 4; j
++)
2015 assert(swizzle_b
< 4);
2016 for (j
= 0; j
< 4; j
++)
2017 rgba
[2][j
] = rgba_temp
[swizzle_b
][j
];
2020 switch (swizzle_a
) {
2021 case PIPE_SWIZZLE_ZERO
:
2022 for (j
= 0; j
< 4; j
++)
2025 case PIPE_SWIZZLE_ONE
:
2026 for (j
= 0; j
< 4; j
++)
2030 assert(swizzle_a
< 4);
2031 for (j
= 0; j
< 4; j
++)
2032 rgba
[3][j
] = rgba_temp
[swizzle_a
][j
];
2037 static wrap_nearest_func
2038 get_nearest_unorm_wrap(unsigned mode
)
2041 case PIPE_TEX_WRAP_CLAMP
:
2042 return wrap_nearest_unorm_clamp
;
2043 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2044 return wrap_nearest_unorm_clamp_to_edge
;
2045 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2046 return wrap_nearest_unorm_clamp_to_border
;
2049 return wrap_nearest_unorm_clamp
;
2054 static wrap_nearest_func
2055 get_nearest_wrap(unsigned mode
)
2058 case PIPE_TEX_WRAP_REPEAT
:
2059 return wrap_nearest_repeat
;
2060 case PIPE_TEX_WRAP_CLAMP
:
2061 return wrap_nearest_clamp
;
2062 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2063 return wrap_nearest_clamp_to_edge
;
2064 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2065 return wrap_nearest_clamp_to_border
;
2066 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2067 return wrap_nearest_mirror_repeat
;
2068 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2069 return wrap_nearest_mirror_clamp
;
2070 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2071 return wrap_nearest_mirror_clamp_to_edge
;
2072 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2073 return wrap_nearest_mirror_clamp_to_border
;
2076 return wrap_nearest_repeat
;
2081 static wrap_linear_func
2082 get_linear_unorm_wrap(unsigned mode
)
2085 case PIPE_TEX_WRAP_CLAMP
:
2086 return wrap_linear_unorm_clamp
;
2087 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2088 return wrap_linear_unorm_clamp_to_edge
;
2089 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2090 return wrap_linear_unorm_clamp_to_border
;
2093 return wrap_linear_unorm_clamp
;
2098 static wrap_linear_func
2099 get_linear_wrap(unsigned mode
)
2102 case PIPE_TEX_WRAP_REPEAT
:
2103 return wrap_linear_repeat
;
2104 case PIPE_TEX_WRAP_CLAMP
:
2105 return wrap_linear_clamp
;
2106 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2107 return wrap_linear_clamp_to_edge
;
2108 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2109 return wrap_linear_clamp_to_border
;
2110 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2111 return wrap_linear_mirror_repeat
;
2112 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2113 return wrap_linear_mirror_clamp
;
2114 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2115 return wrap_linear_mirror_clamp_to_edge
;
2116 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2117 return wrap_linear_mirror_clamp_to_border
;
2120 return wrap_linear_repeat
;
2125 static compute_lambda_func
2126 get_lambda_func(const union sp_sampler_key key
)
2128 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
2129 return compute_lambda_vert
;
2131 switch (key
.bits
.target
) {
2132 case PIPE_TEXTURE_1D
:
2133 case PIPE_TEXTURE_1D_ARRAY
:
2134 return compute_lambda_1d
;
2135 case PIPE_TEXTURE_2D
:
2136 case PIPE_TEXTURE_2D_ARRAY
:
2137 case PIPE_TEXTURE_RECT
:
2138 case PIPE_TEXTURE_CUBE
:
2139 return compute_lambda_2d
;
2140 case PIPE_TEXTURE_3D
:
2141 return compute_lambda_3d
;
2144 return compute_lambda_1d
;
2150 get_img_filter(const union sp_sampler_key key
,
2152 const struct pipe_sampler_state
*sampler
)
2154 switch (key
.bits
.target
) {
2155 case PIPE_TEXTURE_1D
:
2156 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2157 return img_filter_1d_nearest
;
2159 return img_filter_1d_linear
;
2161 case PIPE_TEXTURE_1D_ARRAY
:
2162 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2163 return img_filter_1d_array_nearest
;
2165 return img_filter_1d_array_linear
;
2167 case PIPE_TEXTURE_2D
:
2168 case PIPE_TEXTURE_RECT
:
2169 /* Try for fast path:
2171 if (key
.bits
.is_pot
&&
2172 sampler
->wrap_s
== sampler
->wrap_t
&&
2173 sampler
->normalized_coords
)
2175 switch (sampler
->wrap_s
) {
2176 case PIPE_TEX_WRAP_REPEAT
:
2178 case PIPE_TEX_FILTER_NEAREST
:
2179 return img_filter_2d_nearest_repeat_POT
;
2180 case PIPE_TEX_FILTER_LINEAR
:
2181 return img_filter_2d_linear_repeat_POT
;
2186 case PIPE_TEX_WRAP_CLAMP
:
2188 case PIPE_TEX_FILTER_NEAREST
:
2189 return img_filter_2d_nearest_clamp_POT
;
2195 /* Otherwise use default versions:
2197 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2198 return img_filter_2d_nearest
;
2200 return img_filter_2d_linear
;
2202 case PIPE_TEXTURE_2D_ARRAY
:
2203 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2204 return img_filter_2d_array_nearest
;
2206 return img_filter_2d_array_linear
;
2208 case PIPE_TEXTURE_CUBE
:
2209 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2210 return img_filter_cube_nearest
;
2212 return img_filter_cube_linear
;
2214 case PIPE_TEXTURE_3D
:
2215 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2216 return img_filter_3d_nearest
;
2218 return img_filter_3d_linear
;
2222 return img_filter_1d_nearest
;
2228 * Bind the given texture object and texture cache to the sampler variant.
2231 sp_sampler_variant_bind_view( struct sp_sampler_variant
*samp
,
2232 struct softpipe_tex_tile_cache
*tex_cache
,
2233 const struct pipe_sampler_view
*view
)
2235 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
2236 const struct pipe_resource
*texture
= view
->texture
;
2239 samp
->cache
= tex_cache
;
2240 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
2241 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
2242 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
2247 sp_sampler_variant_destroy( struct sp_sampler_variant
*samp
)
2254 * Create a sampler variant for a given set of non-orthogonal state.
2256 struct sp_sampler_variant
*
2257 sp_create_sampler_variant( const struct pipe_sampler_state
*sampler
,
2258 const union sp_sampler_key key
)
2260 struct sp_sampler_variant
*samp
= CALLOC_STRUCT(sp_sampler_variant
);
2264 samp
->sampler
= sampler
;
2267 /* Note that (for instance) linear_texcoord_s and
2268 * nearest_texcoord_s may be active at the same time, if the
2269 * sampler min_img_filter differs from its mag_img_filter.
2271 if (sampler
->normalized_coords
) {
2272 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
2273 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
2274 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
2276 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
2277 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
2278 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
2281 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
2282 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
2283 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
2285 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
2286 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
2287 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
2290 samp
->compute_lambda
= get_lambda_func( key
);
2292 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
2293 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
2295 switch (sampler
->min_mip_filter
) {
2296 case PIPE_TEX_MIPFILTER_NONE
:
2297 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
2298 samp
->mip_filter
= samp
->min_img_filter
;
2300 samp
->mip_filter
= mip_filter_none
;
2303 case PIPE_TEX_MIPFILTER_NEAREST
:
2304 samp
->mip_filter
= mip_filter_nearest
;
2307 case PIPE_TEX_MIPFILTER_LINEAR
:
2308 if (key
.bits
.is_pot
&&
2309 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
2310 sampler
->normalized_coords
&&
2311 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
2312 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
2313 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
2315 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2319 samp
->mip_filter
= mip_filter_linear
;
2324 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2325 samp
->compare
= sample_compare
;
2328 /* Skip compare operation by promoting the mip_filter function
2331 samp
->compare
= samp
->mip_filter
;
2334 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
2335 samp
->sample_target
= sample_cube
;
2343 /* Skip cube face determination by promoting the compare
2346 samp
->sample_target
= samp
->compare
;
2349 if (key
.bits
.swizzle_r
!= PIPE_SWIZZLE_RED
||
2350 key
.bits
.swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2351 key
.bits
.swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2352 key
.bits
.swizzle_a
!= PIPE_SWIZZLE_ALPHA
) {
2353 samp
->base
.get_samples
= sample_swizzle
;
2356 samp
->base
.get_samples
= samp
->sample_target
;