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_format.h"
42 #include "util/u_memory.h"
43 #include "sp_quad.h" /* only for #define QUAD_* tokens */
44 #include "sp_tex_sample.h"
45 #include "sp_tex_tile_cache.h"
48 /** Set to one to help debug texture sampling */
53 * Return fractional part of 'f'. Used for computing interpolation weights.
54 * Need to be careful with negative values.
55 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
56 * of improperly weighted linear-filtered textures.
57 * The tests/texwrap.c demo is a good test.
68 * Linear interpolation macro
71 lerp(float a
, float v0
, float v1
)
73 return v0
+ a
* (v1
- v0
);
78 * Do 2D/bilinear interpolation of float values.
79 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
80 * a and b are the horizontal and vertical interpolants.
81 * It's important that this function is inlined when compiled with
82 * optimization! If we find that's not true on some systems, convert
86 lerp_2d(float a
, float b
,
87 float v00
, float v10
, float v01
, float v11
)
89 const float temp0
= lerp(a
, v00
, v10
);
90 const float temp1
= lerp(a
, v01
, v11
);
91 return lerp(b
, temp0
, temp1
);
96 * As above, but 3D interpolation of 8 values.
99 lerp_3d(float a
, float b
, float c
,
100 float v000
, float v100
, float v010
, float v110
,
101 float v001
, float v101
, float v011
, float v111
)
103 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
104 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
105 return lerp(c
, temp0
, temp1
);
111 * Compute coord % size for repeat wrap modes.
112 * Note that if coord is negative, coord % size doesn't give the right
113 * value. To avoid that problem we add a large multiple of the size
114 * (rather than using a conditional).
117 repeat(int coord
, unsigned size
)
119 return (coord
+ size
* 1024) % size
;
124 * Apply texture coord wrapping mode and return integer texture indexes
125 * for a vector of four texcoords (S or T or P).
126 * \param wrapMode PIPE_TEX_WRAP_x
127 * \param s the incoming texcoords
128 * \param size the texture image size
129 * \param icoord returns the integer texcoords
130 * \return integer texture index
133 wrap_nearest_repeat(float s
, unsigned size
, int *icoord
)
135 /* s limited to [0,1) */
136 /* i limited to [0,size-1] */
137 int i
= util_ifloor(s
* size
);
138 *icoord
= repeat(i
, size
);
143 wrap_nearest_clamp(float s
, unsigned size
, int *icoord
)
145 /* s limited to [0,1] */
146 /* i limited to [0,size-1] */
152 *icoord
= util_ifloor(s
* size
);
157 wrap_nearest_clamp_to_edge(float s
, unsigned size
, int *icoord
)
159 /* s limited to [min,max] */
160 /* i limited to [0, size-1] */
161 const float min
= 1.0F
/ (2.0F
* size
);
162 const float max
= 1.0F
- min
;
168 *icoord
= util_ifloor(s
* size
);
173 wrap_nearest_clamp_to_border(float s
, unsigned size
, int *icoord
)
175 /* s limited to [min,max] */
176 /* i limited to [-1, size] */
177 const float min
= -1.0F
/ (2.0F
* size
);
178 const float max
= 1.0F
- min
;
184 *icoord
= util_ifloor(s
* size
);
189 wrap_nearest_mirror_repeat(float s
, unsigned size
, int *icoord
)
191 const float min
= 1.0F
/ (2.0F
* size
);
192 const float max
= 1.0F
- min
;
193 const int flr
= util_ifloor(s
);
202 *icoord
= util_ifloor(u
* size
);
207 wrap_nearest_mirror_clamp(float s
, unsigned size
, int *icoord
)
209 /* s limited to [0,1] */
210 /* i limited to [0,size-1] */
211 const float u
= fabsf(s
);
217 *icoord
= util_ifloor(u
* size
);
222 wrap_nearest_mirror_clamp_to_edge(float s
, unsigned size
, int *icoord
)
224 /* s limited to [min,max] */
225 /* i limited to [0, size-1] */
226 const float min
= 1.0F
/ (2.0F
* size
);
227 const float max
= 1.0F
- min
;
228 const float u
= fabsf(s
);
234 *icoord
= util_ifloor(u
* size
);
239 wrap_nearest_mirror_clamp_to_border(float s
, unsigned size
, int *icoord
)
241 /* s limited to [min,max] */
242 /* i limited to [0, size-1] */
243 const float min
= -1.0F
/ (2.0F
* size
);
244 const float max
= 1.0F
- min
;
245 const float u
= fabsf(s
);
251 *icoord
= util_ifloor(u
* size
);
256 * Used to compute texel locations for linear sampling
257 * \param wrapMode PIPE_TEX_WRAP_x
258 * \param s the texcoord
259 * \param size the texture image size
260 * \param icoord0 returns first texture index
261 * \param icoord1 returns second texture index (usually icoord0 + 1)
262 * \param w returns blend factor/weight between texture indices
263 * \param icoord returns the computed integer texture coord
266 wrap_linear_repeat(float s
, unsigned size
,
267 int *icoord0
, int *icoord1
, float *w
)
269 float u
= s
* size
- 0.5F
;
270 *icoord0
= repeat(util_ifloor(u
), size
);
271 *icoord1
= repeat(*icoord0
+ 1, size
);
277 wrap_linear_clamp(float s
, unsigned size
,
278 int *icoord0
, int *icoord1
, float *w
)
280 float u
= CLAMP(s
, 0.0F
, 1.0F
);
282 *icoord0
= util_ifloor(u
);
283 *icoord1
= *icoord0
+ 1;
289 wrap_linear_clamp_to_edge(float s
, unsigned size
,
290 int *icoord0
, int *icoord1
, float *w
)
292 float u
= CLAMP(s
, 0.0F
, 1.0F
);
294 *icoord0
= util_ifloor(u
);
295 *icoord1
= *icoord0
+ 1;
298 if (*icoord1
>= (int) size
)
305 wrap_linear_clamp_to_border(float s
, unsigned size
,
306 int *icoord0
, int *icoord1
, float *w
)
308 const float min
= -1.0F
/ (2.0F
* size
);
309 const float max
= 1.0F
- min
;
310 float u
= CLAMP(s
, min
, max
);
312 *icoord0
= util_ifloor(u
);
313 *icoord1
= *icoord0
+ 1;
319 wrap_linear_mirror_repeat(float s
, unsigned size
,
320 int *icoord0
, int *icoord1
, float *w
)
322 const int flr
= util_ifloor(s
);
327 *icoord0
= util_ifloor(u
);
328 *icoord1
= *icoord0
+ 1;
331 if (*icoord1
>= (int) size
)
338 wrap_linear_mirror_clamp(float s
, unsigned size
,
339 int *icoord0
, int *icoord1
, float *w
)
347 *icoord0
= util_ifloor(u
);
348 *icoord1
= *icoord0
+ 1;
354 wrap_linear_mirror_clamp_to_edge(float s
, unsigned size
,
355 int *icoord0
, int *icoord1
, float *w
)
363 *icoord0
= util_ifloor(u
);
364 *icoord1
= *icoord0
+ 1;
367 if (*icoord1
>= (int) size
)
374 wrap_linear_mirror_clamp_to_border(float s
, unsigned size
,
375 int *icoord0
, int *icoord1
, float *w
)
377 const float min
= -1.0F
/ (2.0F
* size
);
378 const float max
= 1.0F
- min
;
387 *icoord0
= util_ifloor(u
);
388 *icoord1
= *icoord0
+ 1;
394 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
397 wrap_nearest_unorm_clamp(float s
, unsigned size
, int *icoord
)
399 int i
= util_ifloor(s
);
400 *icoord
= CLAMP(i
, 0, (int) size
-1);
405 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
408 wrap_nearest_unorm_clamp_to_border(float s
, unsigned size
, int *icoord
)
410 *icoord
= util_ifloor( CLAMP(s
, -0.5F
, (float) size
+ 0.5F
) );
415 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
418 wrap_nearest_unorm_clamp_to_edge(float s
, unsigned size
, int *icoord
)
420 *icoord
= util_ifloor( CLAMP(s
, 0.5F
, (float) size
- 0.5F
) );
425 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
428 wrap_linear_unorm_clamp(float s
, unsigned size
,
429 int *icoord0
, int *icoord1
, float *w
)
431 /* Not exactly what the spec says, but it matches NVIDIA output */
432 float u
= CLAMP(s
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
433 *icoord0
= util_ifloor(u
);
434 *icoord1
= *icoord0
+ 1;
440 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
443 wrap_linear_unorm_clamp_to_border(float s
, unsigned size
,
444 int *icoord0
, int *icoord1
, float *w
)
446 float u
= CLAMP(s
, -0.5F
, (float) size
+ 0.5F
);
448 *icoord0
= util_ifloor(u
);
449 *icoord1
= *icoord0
+ 1;
450 if (*icoord1
> (int) size
- 1)
457 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
460 wrap_linear_unorm_clamp_to_edge(float s
, unsigned size
,
461 int *icoord0
, int *icoord1
, float *w
)
463 float u
= CLAMP(s
, +0.5F
, (float) size
- 0.5F
);
465 *icoord0
= util_ifloor(u
);
466 *icoord1
= *icoord0
+ 1;
467 if (*icoord1
> (int) size
- 1)
474 * Do coordinate to array index conversion. For array textures.
477 wrap_array_layer(float coord
, unsigned size
, int *layer
)
479 int c
= util_ifloor(coord
+ 0.5F
);
480 *layer
= CLAMP(c
, 0, size
- 1);
485 * Examine the quad's texture coordinates to compute the partial
486 * derivatives w.r.t X and Y, then compute lambda (level of detail).
489 compute_lambda_1d(const struct sp_sampler_variant
*samp
,
490 const float s
[TGSI_QUAD_SIZE
],
491 const float t
[TGSI_QUAD_SIZE
],
492 const float p
[TGSI_QUAD_SIZE
])
494 const struct pipe_resource
*texture
= samp
->view
->texture
;
495 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
496 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
497 float rho
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
499 return util_fast_log2(rho
);
504 compute_lambda_2d(const struct sp_sampler_variant
*samp
,
505 const float s
[TGSI_QUAD_SIZE
],
506 const float t
[TGSI_QUAD_SIZE
],
507 const float p
[TGSI_QUAD_SIZE
])
509 const struct pipe_resource
*texture
= samp
->view
->texture
;
510 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
511 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
512 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
513 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
514 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
515 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
516 float rho
= MAX2(maxx
, maxy
);
518 return util_fast_log2(rho
);
523 compute_lambda_3d(const struct sp_sampler_variant
*samp
,
524 const float s
[TGSI_QUAD_SIZE
],
525 const float t
[TGSI_QUAD_SIZE
],
526 const float p
[TGSI_QUAD_SIZE
])
528 const struct pipe_resource
*texture
= samp
->view
->texture
;
529 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
530 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
531 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
532 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
533 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
534 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
535 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
536 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
537 float maxz
= MAX2(dpdx
, dpdy
) * u_minify(texture
->depth0
, samp
->view
->u
.tex
.first_level
);
540 rho
= MAX2(maxx
, maxy
);
541 rho
= MAX2(rho
, maxz
);
543 return util_fast_log2(rho
);
548 * Compute lambda for a vertex texture sampler.
549 * Since there aren't derivatives to use, just return 0.
552 compute_lambda_vert(const struct sp_sampler_variant
*samp
,
553 const float s
[TGSI_QUAD_SIZE
],
554 const float t
[TGSI_QUAD_SIZE
],
555 const float p
[TGSI_QUAD_SIZE
])
563 * Get a texel from a texture, using the texture tile cache.
565 * \param addr the template tex address containing cube, z, face info.
566 * \param x the x coord of texel within 2D image
567 * \param y the y coord of texel within 2D image
568 * \param rgba the quad to put the texel/color into
570 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
571 * sp_get_cached_tile_tex() function.
577 static INLINE
const float *
578 get_texel_2d_no_border(const struct sp_sampler_variant
*samp
,
579 union tex_tile_address addr
, int x
, int y
)
581 const struct softpipe_tex_cached_tile
*tile
;
583 addr
.bits
.x
= x
/ TILE_SIZE
;
584 addr
.bits
.y
= y
/ TILE_SIZE
;
588 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
590 return &tile
->data
.color
[y
][x
][0];
594 static INLINE
const float *
595 get_texel_2d(const struct sp_sampler_variant
*samp
,
596 union tex_tile_address addr
, int x
, int y
)
598 const struct pipe_resource
*texture
= samp
->view
->texture
;
599 unsigned level
= addr
.bits
.level
;
601 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
602 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
603 return samp
->sampler
->border_color
.f
;
606 return get_texel_2d_no_border( samp
, addr
, x
, y
);
611 * seamless cubemap neighbour array.
612 * this array is used to find the adjacent face in each of 4 directions,
613 * left, right, up, down. (or -x, +x, -y, +y).
615 static const unsigned face_array
[PIPE_TEX_FACE_MAX
][4] = {
616 /* pos X first then neg X is Z different, Y the same */
617 /* PIPE_TEX_FACE_POS_X,*/
618 { PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
,
619 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
620 /* PIPE_TEX_FACE_NEG_X */
621 { PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
,
622 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
624 /* pos Y first then neg Y is X different, X the same */
625 /* PIPE_TEX_FACE_POS_Y */
626 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
627 PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
},
629 /* PIPE_TEX_FACE_NEG_Y */
630 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
631 PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
},
633 /* pos Z first then neg Y is X different, X the same */
634 /* PIPE_TEX_FACE_POS_Z */
635 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
636 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
638 /* PIPE_TEX_FACE_NEG_Z */
639 { PIPE_TEX_FACE_POS_X
, PIPE_TEX_FACE_NEG_X
,
640 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
}
643 static INLINE
unsigned
644 get_next_face(unsigned face
, int x
, int y
)
648 if (x
== 0 && y
== 0)
659 return face_array
[face
][idx
];
662 static INLINE
const float *
663 get_texel_cube_seamless(const struct sp_sampler_variant
*samp
,
664 union tex_tile_address addr
, int x
, int y
,
667 const struct pipe_resource
*texture
= samp
->view
->texture
;
668 unsigned level
= addr
.bits
.level
;
669 unsigned face
= addr
.bits
.face
;
674 max_x
= (int) u_minify(texture
->width0
, level
);
675 max_y
= (int) u_minify(texture
->height0
, level
);
679 /* the corner case */
680 if ((x
< 0 || x
>= max_x
) &&
681 (y
< 0 || y
>= max_y
)) {
682 const float *c1
, *c2
, *c3
;
683 int fx
= x
< 0 ? 0 : max_x
- 1;
684 int fy
= y
< 0 ? 0 : max_y
- 1;
685 c1
= get_texel_2d_no_border( samp
, addr
, fx
, fy
);
686 addr
.bits
.face
= get_next_face(face
, (x
< 0) ? -1 : 1, 0);
687 c2
= get_texel_2d_no_border( samp
, addr
, (x
< 0) ? max_x
- 1 : 0, fy
);
688 addr
.bits
.face
= get_next_face(face
, 0, (y
< 0) ? -1 : 1);
689 c3
= get_texel_2d_no_border( samp
, addr
, fx
, (y
< 0) ? max_y
- 1 : 0);
690 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
691 corner
[c
] = CLAMP((c1
[c
] + c2
[c
] + c3
[c
]), 0.0F
, 1.0F
) / 3;
695 /* change the face */
698 face
= get_next_face(face
, -1, 0);
699 } else if (x
>= max_x
) {
701 face
= get_next_face(face
, 1, 0);
704 face
= get_next_face(face
, 0, -1);
705 } else if (y
>= max_y
) {
707 face
= get_next_face(face
, 0, 1);
710 addr
.bits
.face
= face
;
711 return get_texel_2d_no_border( samp
, addr
, new_x
, new_y
);
714 /* Gather a quad of adjacent texels within a tile:
717 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant
*samp
,
718 union tex_tile_address addr
,
719 unsigned x
, unsigned y
,
722 const struct softpipe_tex_cached_tile
*tile
;
724 addr
.bits
.x
= x
/ TILE_SIZE
;
725 addr
.bits
.y
= y
/ TILE_SIZE
;
729 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
731 out
[0] = &tile
->data
.color
[y
][x
][0];
732 out
[1] = &tile
->data
.color
[y
][x
+1][0];
733 out
[2] = &tile
->data
.color
[y
+1][x
][0];
734 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
738 /* Gather a quad of potentially non-adjacent texels:
741 get_texel_quad_2d_no_border(const struct sp_sampler_variant
*samp
,
742 union tex_tile_address addr
,
747 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
748 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
749 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
750 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
753 /* Can involve a lot of unnecessary checks for border color:
756 get_texel_quad_2d(const struct sp_sampler_variant
*samp
,
757 union tex_tile_address addr
,
762 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
763 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
764 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
765 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
772 static INLINE
const float *
773 get_texel_3d_no_border(const struct sp_sampler_variant
*samp
,
774 union tex_tile_address addr
, int x
, int y
, int z
)
776 const struct softpipe_tex_cached_tile
*tile
;
778 addr
.bits
.x
= x
/ TILE_SIZE
;
779 addr
.bits
.y
= y
/ TILE_SIZE
;
784 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
786 return &tile
->data
.color
[y
][x
][0];
790 static INLINE
const float *
791 get_texel_3d(const struct sp_sampler_variant
*samp
,
792 union tex_tile_address addr
, int x
, int y
, int z
)
794 const struct pipe_resource
*texture
= samp
->view
->texture
;
795 unsigned level
= addr
.bits
.level
;
797 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
798 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
799 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
800 return samp
->sampler
->border_color
.f
;
803 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
808 /* Get texel pointer for 1D array texture */
809 static INLINE
const float *
810 get_texel_1d_array(const struct sp_sampler_variant
*samp
,
811 union tex_tile_address addr
, int x
, int y
)
813 const struct pipe_resource
*texture
= samp
->view
->texture
;
814 unsigned level
= addr
.bits
.level
;
816 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
817 return samp
->sampler
->border_color
.f
;
820 return get_texel_2d_no_border(samp
, addr
, x
, y
);
825 /* Get texel pointer for 2D array texture */
826 static INLINE
const float *
827 get_texel_2d_array(const struct sp_sampler_variant
*samp
,
828 union tex_tile_address addr
, int x
, int y
, int layer
)
830 const struct pipe_resource
*texture
= samp
->view
->texture
;
831 unsigned level
= addr
.bits
.level
;
833 assert(layer
< (int) texture
->array_size
);
836 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
837 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
838 return samp
->sampler
->border_color
.f
;
841 return get_texel_3d_no_border(samp
, addr
, x
, y
, layer
);
846 /* Get texel pointer for cube array texture */
847 static INLINE
const float *
848 get_texel_cube_array(const struct sp_sampler_variant
*samp
,
849 union tex_tile_address addr
, int x
, int y
, int layer
)
851 const struct pipe_resource
*texture
= samp
->view
->texture
;
852 unsigned level
= addr
.bits
.level
;
854 assert(layer
< (int) texture
->array_size
);
857 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
858 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
859 return samp
->sampler
->border_color
.f
;
862 return get_texel_3d_no_border(samp
, addr
, x
, y
, layer
);
866 * Given the logbase2 of a mipmap's base level size and a mipmap level,
867 * return the size (in texels) of that mipmap level.
868 * For example, if level[0].width = 256 then base_pot will be 8.
869 * If level = 2, then we'll return 64 (the width at level=2).
870 * Return 1 if level > base_pot.
872 static INLINE
unsigned
873 pot_level_size(unsigned base_pot
, unsigned level
)
875 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
880 print_sample(const char *function
, const float *rgba
)
882 debug_printf("%s %g %g %g %g\n",
884 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
889 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
891 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
893 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
894 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
895 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
896 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
899 /* Some image-filter fastpaths:
902 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
908 enum tgsi_sampler_control control
,
911 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
912 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
913 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
914 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
915 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
916 union tex_tile_address addr
;
919 float u
= s
* xpot
- 0.5F
;
920 float v
= t
* ypot
- 0.5F
;
922 int uflr
= util_ifloor(u
);
923 int vflr
= util_ifloor(v
);
925 float xw
= u
- (float)uflr
;
926 float yw
= v
- (float)vflr
;
928 int x0
= uflr
& (xpot
- 1);
929 int y0
= vflr
& (ypot
- 1);
934 addr
.bits
.level
= level
;
936 /* Can we fetch all four at once:
938 if (x0
< xmax
&& y0
< ymax
) {
939 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
942 unsigned x1
= (x0
+ 1) & (xpot
- 1);
943 unsigned y1
= (y0
+ 1) & (ypot
- 1);
944 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
947 /* interpolate R, G, B, A */
948 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++) {
949 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
955 print_sample(__FUNCTION__
, rgba
);
961 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
967 enum tgsi_sampler_control control
,
968 float rgba
[TGSI_QUAD_SIZE
])
970 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
971 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
972 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
974 union tex_tile_address addr
;
980 int uflr
= util_ifloor(u
);
981 int vflr
= util_ifloor(v
);
983 int x0
= uflr
& (xpot
- 1);
984 int y0
= vflr
& (ypot
- 1);
987 addr
.bits
.level
= level
;
989 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
990 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
991 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
994 print_sample(__FUNCTION__
, rgba
);
1000 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
1006 enum tgsi_sampler_control control
,
1007 float rgba
[TGSI_QUAD_SIZE
])
1009 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1010 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
1011 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
1012 union tex_tile_address addr
;
1022 addr
.bits
.level
= level
;
1024 x0
= util_ifloor(u
);
1027 else if (x0
> xpot
- 1)
1030 y0
= util_ifloor(v
);
1033 else if (y0
> ypot
- 1)
1036 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
1037 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1038 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1041 print_sample(__FUNCTION__
, rgba
);
1047 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1053 enum tgsi_sampler_control control
,
1054 float rgba
[TGSI_QUAD_SIZE
])
1056 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1057 const struct pipe_resource
*texture
= samp
->view
->texture
;
1060 union tex_tile_address addr
;
1064 width
= u_minify(texture
->width0
, level
);
1069 addr
.bits
.level
= level
;
1071 samp
->nearest_texcoord_s(s
, width
, &x
);
1073 out
= get_texel_2d(samp
, addr
, x
, 0);
1074 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1075 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1078 print_sample(__FUNCTION__
, rgba
);
1084 img_filter_1d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1090 enum tgsi_sampler_control control
,
1093 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1094 const struct pipe_resource
*texture
= samp
->view
->texture
;
1097 union tex_tile_address addr
;
1101 width
= u_minify(texture
->width0
, level
);
1106 addr
.bits
.level
= level
;
1108 samp
->nearest_texcoord_s(s
, width
, &x
);
1109 wrap_array_layer(t
, texture
->array_size
, &layer
);
1111 out
= get_texel_1d_array(samp
, addr
, x
, layer
);
1112 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1113 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1116 print_sample(__FUNCTION__
, rgba
);
1122 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1128 enum tgsi_sampler_control control
,
1131 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1132 const struct pipe_resource
*texture
= samp
->view
->texture
;
1135 union tex_tile_address addr
;
1139 width
= u_minify(texture
->width0
, level
);
1140 height
= u_minify(texture
->height0
, level
);
1146 addr
.bits
.level
= level
;
1148 samp
->nearest_texcoord_s(s
, width
, &x
);
1149 samp
->nearest_texcoord_t(t
, height
, &y
);
1151 out
= get_texel_2d(samp
, addr
, x
, y
);
1152 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1153 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1156 print_sample(__FUNCTION__
, rgba
);
1162 img_filter_2d_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1168 enum tgsi_sampler_control control
,
1171 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1172 const struct pipe_resource
*texture
= samp
->view
->texture
;
1175 union tex_tile_address addr
;
1179 width
= u_minify(texture
->width0
, level
);
1180 height
= u_minify(texture
->height0
, level
);
1186 addr
.bits
.level
= level
;
1188 samp
->nearest_texcoord_s(s
, width
, &x
);
1189 samp
->nearest_texcoord_t(t
, height
, &y
);
1190 wrap_array_layer(p
, texture
->array_size
, &layer
);
1192 out
= get_texel_2d_array(samp
, addr
, x
, y
, layer
);
1193 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1194 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1197 print_sample(__FUNCTION__
, rgba
);
1202 static INLINE
union tex_tile_address
1203 face(union tex_tile_address addr
, unsigned face
)
1205 addr
.bits
.face
= face
;
1211 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1217 enum tgsi_sampler_control control
,
1220 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1221 const struct pipe_resource
*texture
= samp
->view
->texture
;
1224 union tex_tile_address addr
;
1228 width
= u_minify(texture
->width0
, level
);
1229 height
= u_minify(texture
->height0
, level
);
1235 addr
.bits
.level
= level
;
1238 * If NEAREST filtering is done within a miplevel, always apply wrap
1239 * mode CLAMP_TO_EDGE.
1241 if (samp
->sampler
->seamless_cube_map
) {
1242 wrap_nearest_clamp_to_edge(s
, width
, &x
);
1243 wrap_nearest_clamp_to_edge(t
, height
, &y
);
1245 samp
->nearest_texcoord_s(s
, width
, &x
);
1246 samp
->nearest_texcoord_t(t
, height
, &y
);
1249 out
= get_texel_2d(samp
, face(addr
, face_id
), x
, y
);
1250 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1251 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1254 print_sample(__FUNCTION__
, rgba
);
1259 img_filter_cube_array_nearest(struct tgsi_sampler
*tgsi_sampler
,
1265 enum tgsi_sampler_control control
,
1268 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1269 const struct pipe_resource
*texture
= samp
->view
->texture
;
1272 union tex_tile_address addr
;
1276 width
= u_minify(texture
->width0
, level
);
1277 height
= u_minify(texture
->height0
, level
);
1283 addr
.bits
.level
= level
;
1285 samp
->nearest_texcoord_s(s
, width
, &x
);
1286 samp
->nearest_texcoord_t(t
, height
, &y
);
1287 wrap_array_layer(p
, texture
->array_size
, &layer
);
1289 out
= get_texel_cube_array(samp
, addr
, x
, y
, layer
* 6 + face_id
);
1290 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1291 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1294 print_sample(__FUNCTION__
, rgba
);
1299 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1305 enum tgsi_sampler_control control
,
1308 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1309 const struct pipe_resource
*texture
= samp
->view
->texture
;
1310 int width
, height
, depth
;
1312 union tex_tile_address addr
;
1316 width
= u_minify(texture
->width0
, level
);
1317 height
= u_minify(texture
->height0
, level
);
1318 depth
= u_minify(texture
->depth0
, level
);
1324 samp
->nearest_texcoord_s(s
, width
, &x
);
1325 samp
->nearest_texcoord_t(t
, height
, &y
);
1326 samp
->nearest_texcoord_p(p
, depth
, &z
);
1329 addr
.bits
.level
= level
;
1331 out
= get_texel_3d(samp
, addr
, x
, y
, z
);
1332 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1333 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1338 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1344 enum tgsi_sampler_control control
,
1347 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1348 const struct pipe_resource
*texture
= samp
->view
->texture
;
1351 float xw
; /* weights */
1352 union tex_tile_address addr
;
1353 const float *tx0
, *tx1
;
1356 width
= u_minify(texture
->width0
, level
);
1361 addr
.bits
.level
= level
;
1363 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1365 tx0
= get_texel_2d(samp
, addr
, x0
, 0);
1366 tx1
= get_texel_2d(samp
, addr
, x1
, 0);
1368 /* interpolate R, G, B, A */
1369 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1370 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1375 img_filter_1d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1381 enum tgsi_sampler_control control
,
1384 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1385 const struct pipe_resource
*texture
= samp
->view
->texture
;
1388 float xw
; /* weights */
1389 union tex_tile_address addr
;
1390 const float *tx0
, *tx1
;
1393 width
= u_minify(texture
->width0
, level
);
1398 addr
.bits
.level
= level
;
1400 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1401 wrap_array_layer(t
, texture
->array_size
, &layer
);
1403 tx0
= get_texel_1d_array(samp
, addr
, x0
, layer
);
1404 tx1
= get_texel_1d_array(samp
, addr
, x1
, layer
);
1406 /* interpolate R, G, B, A */
1407 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1408 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1413 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1419 enum tgsi_sampler_control control
,
1422 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1423 const struct pipe_resource
*texture
= samp
->view
->texture
;
1426 float xw
, yw
; /* weights */
1427 union tex_tile_address addr
;
1428 const float *tx0
, *tx1
, *tx2
, *tx3
;
1431 width
= u_minify(texture
->width0
, level
);
1432 height
= u_minify(texture
->height0
, level
);
1438 addr
.bits
.level
= level
;
1440 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1441 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1443 tx0
= get_texel_2d(samp
, addr
, x0
, y0
);
1444 tx1
= get_texel_2d(samp
, addr
, x1
, y0
);
1445 tx2
= get_texel_2d(samp
, addr
, x0
, y1
);
1446 tx3
= get_texel_2d(samp
, addr
, x1
, y1
);
1448 /* interpolate R, G, B, A */
1449 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1450 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1457 img_filter_2d_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1463 enum tgsi_sampler_control control
,
1466 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1467 const struct pipe_resource
*texture
= samp
->view
->texture
;
1469 int x0
, y0
, x1
, y1
, layer
;
1470 float xw
, yw
; /* weights */
1471 union tex_tile_address addr
;
1472 const float *tx0
, *tx1
, *tx2
, *tx3
;
1475 width
= u_minify(texture
->width0
, level
);
1476 height
= u_minify(texture
->height0
, level
);
1482 addr
.bits
.level
= level
;
1484 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1485 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1486 wrap_array_layer(p
, texture
->array_size
, &layer
);
1488 tx0
= get_texel_2d_array(samp
, addr
, x0
, y0
, layer
);
1489 tx1
= get_texel_2d_array(samp
, addr
, x1
, y0
, layer
);
1490 tx2
= get_texel_2d_array(samp
, addr
, x0
, y1
, layer
);
1491 tx3
= get_texel_2d_array(samp
, addr
, x1
, y1
, layer
);
1493 /* interpolate R, G, B, A */
1494 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1495 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1502 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1508 enum tgsi_sampler_control control
,
1511 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1512 const struct pipe_resource
*texture
= samp
->view
->texture
;
1515 float xw
, yw
; /* weights */
1516 union tex_tile_address addr
, addrj
;
1517 const float *tx0
, *tx1
, *tx2
, *tx3
;
1518 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
], corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1521 width
= u_minify(texture
->width0
, level
);
1522 height
= u_minify(texture
->height0
, level
);
1528 addr
.bits
.level
= level
;
1531 * For seamless if LINEAR filtering is done within a miplevel,
1532 * always apply wrap mode CLAMP_TO_BORDER.
1534 if (samp
->sampler
->seamless_cube_map
) {
1535 wrap_linear_clamp_to_border(s
, width
, &x0
, &x1
, &xw
);
1536 wrap_linear_clamp_to_border(t
, height
, &y0
, &y1
, &yw
);
1538 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1539 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1542 addrj
= face(addr
, face_id
);
1544 if (samp
->sampler
->seamless_cube_map
) {
1545 tx0
= get_texel_cube_seamless(samp
, addrj
, x0
, y0
, corner0
);
1546 tx1
= get_texel_cube_seamless(samp
, addrj
, x1
, y0
, corner1
);
1547 tx2
= get_texel_cube_seamless(samp
, addrj
, x0
, y1
, corner2
);
1548 tx3
= get_texel_cube_seamless(samp
, addrj
, x1
, y1
, corner3
);
1550 tx0
= get_texel_2d(samp
, addrj
, x0
, y0
);
1551 tx1
= get_texel_2d(samp
, addrj
, x1
, y0
);
1552 tx2
= get_texel_2d(samp
, addrj
, x0
, y1
);
1553 tx3
= get_texel_2d(samp
, addrj
, x1
, y1
);
1555 /* interpolate R, G, B, A */
1556 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1557 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1564 img_filter_cube_array_linear(struct tgsi_sampler
*tgsi_sampler
,
1570 enum tgsi_sampler_control control
,
1573 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1574 const struct pipe_resource
*texture
= samp
->view
->texture
;
1576 int x0
, y0
, x1
, y1
, layer
;
1577 float xw
, yw
; /* weights */
1578 union tex_tile_address addr
;
1579 const float *tx0
, *tx1
, *tx2
, *tx3
;
1582 width
= u_minify(texture
->width0
, level
);
1583 height
= u_minify(texture
->height0
, level
);
1589 addr
.bits
.level
= level
;
1591 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1592 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1593 wrap_array_layer(p
, texture
->array_size
, &layer
);
1595 tx0
= get_texel_cube_array(samp
, addr
, x0
, y0
, layer
* 6 + face_id
);
1596 tx1
= get_texel_cube_array(samp
, addr
, x1
, y0
, layer
* 6 + face_id
);
1597 tx2
= get_texel_cube_array(samp
, addr
, x0
, y1
, layer
* 6 + face_id
);
1598 tx3
= get_texel_cube_array(samp
, addr
, x1
, y1
, layer
* 6 + face_id
);
1600 /* interpolate R, G, B, A */
1601 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1602 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1608 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1614 enum tgsi_sampler_control control
,
1617 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1618 const struct pipe_resource
*texture
= samp
->view
->texture
;
1619 int width
, height
, depth
;
1620 int x0
, x1
, y0
, y1
, z0
, z1
;
1621 float xw
, yw
, zw
; /* interpolation weights */
1622 union tex_tile_address addr
;
1623 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1626 width
= u_minify(texture
->width0
, level
);
1627 height
= u_minify(texture
->height0
, level
);
1628 depth
= u_minify(texture
->depth0
, level
);
1631 addr
.bits
.level
= level
;
1637 samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1638 samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1639 samp
->linear_texcoord_p(p
, depth
, &z0
, &z1
, &zw
);
1642 tx00
= get_texel_3d(samp
, addr
, x0
, y0
, z0
);
1643 tx01
= get_texel_3d(samp
, addr
, x1
, y0
, z0
);
1644 tx02
= get_texel_3d(samp
, addr
, x0
, y1
, z0
);
1645 tx03
= get_texel_3d(samp
, addr
, x1
, y1
, z0
);
1647 tx10
= get_texel_3d(samp
, addr
, x0
, y0
, z1
);
1648 tx11
= get_texel_3d(samp
, addr
, x1
, y0
, z1
);
1649 tx12
= get_texel_3d(samp
, addr
, x0
, y1
, z1
);
1650 tx13
= get_texel_3d(samp
, addr
, x1
, y1
, z1
);
1652 /* interpolate R, G, B, A */
1653 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1654 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1662 /* Calculate level of detail for every fragment.
1663 * Note that lambda has already been biased by global LOD bias.
1666 compute_lod(const struct pipe_sampler_state
*sampler
,
1667 const float biased_lambda
,
1668 const float lodbias
[TGSI_QUAD_SIZE
],
1669 float lod
[TGSI_QUAD_SIZE
])
1673 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1674 lod
[i
] = biased_lambda
+ lodbias
[i
];
1675 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1681 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1682 const float s
[TGSI_QUAD_SIZE
],
1683 const float t
[TGSI_QUAD_SIZE
],
1684 const float p
[TGSI_QUAD_SIZE
],
1685 const float c0
[TGSI_QUAD_SIZE
],
1686 const float c1
[TGSI_QUAD_SIZE
],
1687 enum tgsi_sampler_control control
,
1688 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1690 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1691 const struct pipe_resource
*texture
= samp
->view
->texture
;
1693 float lod
[TGSI_QUAD_SIZE
];
1695 if (control
== tgsi_sampler_lod_bias
) {
1696 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1697 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1698 compute_lod(samp
->sampler
, lambda
, c1
, lod
);
1700 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1702 assert(control
== tgsi_sampler_lod_explicit
);
1704 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1705 memcpy(lod
, c1
, sizeof(lod
));
1707 memcpy(lod
, c0
, sizeof(lod
));
1711 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1712 int level0
= samp
->view
->u
.tex
.first_level
+ (int)lod
[j
];
1715 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1717 else if (level0
>= texture
->last_level
)
1718 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1721 float levelBlend
= frac(lod
[j
]);
1722 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1725 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][0]);
1726 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
+1, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][1]);
1728 for (c
= 0; c
< 4; c
++) {
1729 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1735 print_sample_4(__FUNCTION__
, rgba
);
1741 * Compute nearest mipmap level from texcoords.
1742 * Then sample the texture level for four elements of a quad.
1743 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1746 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1747 const float s
[TGSI_QUAD_SIZE
],
1748 const float t
[TGSI_QUAD_SIZE
],
1749 const float p
[TGSI_QUAD_SIZE
],
1750 const float c0
[TGSI_QUAD_SIZE
],
1751 const float c1
[TGSI_QUAD_SIZE
],
1752 enum tgsi_sampler_control control
,
1753 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1755 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1756 const struct pipe_resource
*texture
= samp
->view
->texture
;
1757 float lod
[TGSI_QUAD_SIZE
];
1760 if (control
== tgsi_sampler_lod_bias
) {
1761 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1762 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1763 compute_lod(samp
->sampler
, lambda
, c1
, lod
);
1765 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1767 assert(control
== tgsi_sampler_lod_explicit
);
1769 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1770 memcpy(lod
, c1
, sizeof(lod
));
1772 memcpy(lod
, c0
, sizeof(lod
));
1775 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1777 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1779 float level
= samp
->view
->u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
) ;
1780 level
= MIN2(level
, (int)texture
->last_level
);
1781 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1786 print_sample_4(__FUNCTION__
, rgba
);
1792 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1793 const float s
[TGSI_QUAD_SIZE
],
1794 const float t
[TGSI_QUAD_SIZE
],
1795 const float p
[TGSI_QUAD_SIZE
],
1796 const float c0
[TGSI_QUAD_SIZE
],
1797 const float c1
[TGSI_QUAD_SIZE
],
1798 enum tgsi_sampler_control control
,
1799 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1801 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1802 float lod
[TGSI_QUAD_SIZE
];
1805 if (control
== tgsi_sampler_lod_bias
) {
1806 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1807 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1808 compute_lod(samp
->sampler
, lambda
, c1
, lod
);
1810 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1812 assert(control
== tgsi_sampler_lod_explicit
);
1814 if (samp
->key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
1815 memcpy(lod
, c1
, sizeof(lod
));
1817 memcpy(lod
, c0
, sizeof(lod
));
1820 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1822 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1825 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1832 mip_filter_none_no_filter_select(struct tgsi_sampler
*tgsi_sampler
,
1833 const float s
[TGSI_QUAD_SIZE
],
1834 const float t
[TGSI_QUAD_SIZE
],
1835 const float p
[TGSI_QUAD_SIZE
],
1836 const float c0
[TGSI_QUAD_SIZE
],
1837 const float c1
[TGSI_QUAD_SIZE
],
1838 enum tgsi_sampler_control control
,
1839 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1841 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1844 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
1845 samp
->mag_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
1849 /* For anisotropic filtering */
1850 #define WEIGHT_LUT_SIZE 1024
1852 static float *weightLut
= NULL
;
1855 * Creates the look-up table used to speed-up EWA sampling
1858 create_filter_table(void)
1862 weightLut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
1864 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1866 float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
1867 float weight
= (float) exp(-alpha
* r2
);
1868 weightLut
[i
] = weight
;
1875 * Elliptical weighted average (EWA) filter for producing high quality
1876 * anisotropic filtered results.
1877 * Based on the Higher Quality Elliptical Weighted Average Filter
1878 * published by Paul S. Heckbert in his Master's Thesis
1879 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1882 img_filter_2d_ewa(struct tgsi_sampler
*tgsi_sampler
,
1883 const float s
[TGSI_QUAD_SIZE
],
1884 const float t
[TGSI_QUAD_SIZE
],
1885 const float p
[TGSI_QUAD_SIZE
],
1887 enum tgsi_sampler_control control
,
1888 const float dudx
, const float dvdx
,
1889 const float dudy
, const float dvdy
,
1890 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1892 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
1893 const struct pipe_resource
*texture
= samp
->view
->texture
;
1895 // ??? Won't the image filters blow up if level is negative?
1896 unsigned level0
= level
> 0 ? level
: 0;
1897 float scaling
= 1.0 / (1 << level0
);
1898 int width
= u_minify(texture
->width0
, level0
);
1899 int height
= u_minify(texture
->height0
, level0
);
1901 float ux
= dudx
* scaling
;
1902 float vx
= dvdx
* scaling
;
1903 float uy
= dudy
* scaling
;
1904 float vy
= dvdy
* scaling
;
1906 /* compute ellipse coefficients to bound the region:
1907 * A*x*x + B*x*y + C*y*y = F.
1909 float A
= vx
*vx
+vy
*vy
+1;
1910 float B
= -2*(ux
*vx
+uy
*vy
);
1911 float C
= ux
*ux
+uy
*uy
+1;
1912 float F
= A
*C
-B
*B
/4.0;
1914 /* check if it is an ellipse */
1915 /* ASSERT(F > 0.0); */
1917 /* Compute the ellipse's (u,v) bounding box in texture space */
1918 float d
= -B
*B
+4.0*C
*A
;
1919 float box_u
= 2.0 / d
* sqrt(d
*C
*F
); /* box_u -> half of bbox with */
1920 float box_v
= 2.0 / d
* sqrt(A
*d
*F
); /* box_v -> half of bbox height */
1922 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1923 float s_buffer
[TGSI_QUAD_SIZE
];
1924 float t_buffer
[TGSI_QUAD_SIZE
];
1925 float weight_buffer
[TGSI_QUAD_SIZE
];
1926 unsigned buffer_next
;
1928 float den
; /* = 0.0F; */
1930 float U
; /* = u0 - tex_u; */
1933 /* Scale ellipse formula to directly index the Filter Lookup Table.
1934 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1936 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1940 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1942 /* For each quad, the du and dx values are the same and so the ellipse is
1943 * also the same. Note that texel/image access can only be performed using
1944 * a quad, i.e. it is not possible to get the pixel value for a single
1945 * tex coord. In order to have a better performance, the access is buffered
1946 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
1947 * full, then the pixel values are read from the image.
1951 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1952 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
1953 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
1954 * value, q, is less than F, we're inside the ellipse
1956 float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
1957 float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
1959 int u0
= (int) floorf(tex_u
- box_u
);
1960 int u1
= (int) ceilf(tex_u
+ box_u
);
1961 int v0
= (int) floorf(tex_v
- box_v
);
1962 int v1
= (int) ceilf(tex_v
+ box_v
);
1964 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
1968 for (v
= v0
; v
<= v1
; ++v
) {
1969 float V
= v
- tex_v
;
1970 float dq
= A
* (2 * U
+ 1) + B
* V
;
1971 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
1974 for (u
= u0
; u
<= u1
; ++u
) {
1975 /* Note that the ellipse has been pre-scaled so F =
1976 * WEIGHT_LUT_SIZE - 1
1978 if (q
< WEIGHT_LUT_SIZE
) {
1979 /* as a LUT is used, q must never be negative;
1980 * should not happen, though
1982 const int qClamped
= q
>= 0.0F
? q
: 0;
1983 float weight
= weightLut
[qClamped
];
1985 weight_buffer
[buffer_next
] = weight
;
1986 s_buffer
[buffer_next
] = u
/ ((float) width
);
1987 t_buffer
[buffer_next
] = v
/ ((float) height
);
1990 if (buffer_next
== TGSI_QUAD_SIZE
) {
1991 /* 4 texel coords are in the buffer -> read it now */
1993 /* it is assumed that samp->min_img_filter is set to
1994 * img_filter_2d_nearest or one of the
1995 * accelerated img_filter_2d_nearest_XXX functions.
1997 for (jj
= 0; jj
< buffer_next
; jj
++) {
1998 samp
->min_img_filter(tgsi_sampler
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
], level
, samp
->faces
[j
],
1999 tgsi_sampler_lod_bias
, &rgba_temp
[0][jj
]);
2000 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2001 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2002 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2003 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2016 /* if the tex coord buffer contains unread values, we will read
2019 if (buffer_next
> 0) {
2021 /* it is assumed that samp->min_img_filter is set to
2022 * img_filter_2d_nearest or one of the
2023 * accelerated img_filter_2d_nearest_XXX functions.
2025 for (jj
= 0; jj
< buffer_next
; jj
++) {
2026 samp
->min_img_filter(tgsi_sampler
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
], level
, samp
->faces
[j
],
2027 tgsi_sampler_lod_bias
, &rgba_temp
[0][jj
]);
2028 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2029 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2030 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2031 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2036 /* Reaching this place would mean that no pixels intersected
2037 * the ellipse. This should never happen because the filter
2038 * we use always intersects at least one pixel.
2045 /* not enough pixels in resampling, resort to direct interpolation */
2046 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level
, samp
->faces
[j
],
2047 tgsi_sampler_lod_bias
, &rgba_temp
[0][j
]);
2049 num
[0] = rgba_temp
[0][j
];
2050 num
[1] = rgba_temp
[1][j
];
2051 num
[2] = rgba_temp
[2][j
];
2052 num
[3] = rgba_temp
[3][j
];
2055 rgba
[0][j
] = num
[0] / den
;
2056 rgba
[1][j
] = num
[1] / den
;
2057 rgba
[2][j
] = num
[2] / den
;
2058 rgba
[3][j
] = num
[3] / den
;
2064 * Sample 2D texture using an anisotropic filter.
2067 mip_filter_linear_aniso(struct tgsi_sampler
*tgsi_sampler
,
2068 const float s
[TGSI_QUAD_SIZE
],
2069 const float t
[TGSI_QUAD_SIZE
],
2070 const float p
[TGSI_QUAD_SIZE
],
2071 const float c0
[TGSI_QUAD_SIZE
],
2072 const float c1
[TGSI_QUAD_SIZE
],
2073 enum tgsi_sampler_control control
,
2074 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2076 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2077 const struct pipe_resource
*texture
= samp
->view
->texture
;
2080 float lod
[TGSI_QUAD_SIZE
];
2082 float s_to_u
= u_minify(texture
->width0
, samp
->view
->u
.tex
.first_level
);
2083 float t_to_v
= u_minify(texture
->height0
, samp
->view
->u
.tex
.first_level
);
2084 float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2085 float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2086 float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2087 float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2089 if (control
== tgsi_sampler_lod_bias
) {
2090 /* note: instead of working with Px and Py, we will use the
2091 * squared length instead, to avoid sqrt.
2093 float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2094 float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2099 const float maxEccentricity
= samp
->sampler
->max_anisotropy
* samp
->sampler
->max_anisotropy
;
2110 /* if the eccentricity of the ellipse is too big, scale up the shorter
2111 * of the two vectors to limit the maximum amount of work per pixel
2114 if (e
> maxEccentricity
) {
2115 /* float s=e / maxEccentricity;
2119 Pmin2
= Pmax2
/ maxEccentricity
;
2122 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2123 * this since 0.5*log(x) = log(sqrt(x))
2125 lambda
= 0.5F
* util_fast_log2(Pmin2
) + samp
->sampler
->lod_bias
;
2126 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
2129 assert(control
== tgsi_sampler_lod_explicit
);
2131 memcpy(lod
, c0
, sizeof(lod
));
2134 /* XXX: Take into account all lod values.
2137 level0
= samp
->view
->u
.tex
.first_level
+ (int)lambda
;
2139 /* If the ellipse covers the whole image, we can
2140 * simply return the average of the whole image.
2142 if (level0
>= (int) texture
->last_level
) {
2144 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
2145 samp
->min_img_filter(tgsi_sampler
, s
[j
], t
[j
], p
[j
], texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
2148 /* don't bother interpolating between multiple LODs; it doesn't
2149 * seem to be worth the extra running time.
2151 img_filter_2d_ewa(tgsi_sampler
, s
, t
, p
, level0
, tgsi_sampler_lod_bias
,
2152 dudx
, dvdx
, dudy
, dvdy
, rgba
);
2156 print_sample_4(__FUNCTION__
, rgba
);
2162 * Specialized version of mip_filter_linear with hard-wired calls to
2163 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2166 mip_filter_linear_2d_linear_repeat_POT(
2167 struct tgsi_sampler
*tgsi_sampler
,
2168 const float s
[TGSI_QUAD_SIZE
],
2169 const float t
[TGSI_QUAD_SIZE
],
2170 const float p
[TGSI_QUAD_SIZE
],
2171 const float c0
[TGSI_QUAD_SIZE
],
2172 const float c1
[TGSI_QUAD_SIZE
],
2173 enum tgsi_sampler_control control
,
2174 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2176 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2177 const struct pipe_resource
*texture
= samp
->view
->texture
;
2180 float lod
[TGSI_QUAD_SIZE
];
2182 if (control
== tgsi_sampler_lod_bias
) {
2183 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
2184 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
2186 assert(control
== tgsi_sampler_lod_explicit
);
2188 memcpy(lod
, c0
, sizeof(lod
));
2191 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2192 int level0
= samp
->view
->u
.tex
.first_level
+ (int)lod
[j
];
2194 /* Catches both negative and large values of level0:
2196 if ((unsigned)level0
>= texture
->last_level
) {
2198 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->u
.tex
.first_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
2200 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], samp
->view
->texture
->last_level
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgba
[0][j
]);
2204 float levelBlend
= frac(lod
[j
]);
2205 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2208 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][0]);
2209 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
[j
], t
[j
], p
[j
], level0
+1, samp
->faces
[j
], tgsi_sampler_lod_bias
, &rgbax
[0][1]);
2211 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2212 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2217 print_sample_4(__FUNCTION__
, rgba
);
2223 * Do shadow/depth comparisons.
2226 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
2227 const float s
[TGSI_QUAD_SIZE
],
2228 const float t
[TGSI_QUAD_SIZE
],
2229 const float p
[TGSI_QUAD_SIZE
],
2230 const float c0
[TGSI_QUAD_SIZE
],
2231 const float c1
[TGSI_QUAD_SIZE
],
2232 enum tgsi_sampler_control control
,
2233 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2235 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2236 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
2237 int j
, k0
, k1
, k2
, k3
;
2239 float pc0
, pc1
, pc2
, pc3
;
2241 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, c1
, control
, rgba
);
2244 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2245 * for 2D Array texture we need to use the 'c0' (aka Q).
2246 * When we sampled the depth texture, the depth value was put into all
2247 * RGBA channels. We look at the red channel here.
2250 if (samp
->view
->texture
->target
== PIPE_TEXTURE_2D_ARRAY
||
2251 samp
->view
->texture
->target
== PIPE_TEXTURE_CUBE
) {
2252 pc0
= CLAMP(c0
[0], 0.0F
, 1.0F
);
2253 pc1
= CLAMP(c0
[1], 0.0F
, 1.0F
);
2254 pc2
= CLAMP(c0
[2], 0.0F
, 1.0F
);
2255 pc3
= CLAMP(c0
[3], 0.0F
, 1.0F
);
2256 } else if (samp
->view
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2257 pc0
= CLAMP(c1
[0], 0.0F
, 1.0F
);
2258 pc1
= CLAMP(c1
[1], 0.0F
, 1.0F
);
2259 pc2
= CLAMP(c1
[2], 0.0F
, 1.0F
);
2260 pc3
= CLAMP(c1
[3], 0.0F
, 1.0F
);
2262 pc0
= CLAMP(p
[0], 0.0F
, 1.0F
);
2263 pc1
= CLAMP(p
[1], 0.0F
, 1.0F
);
2264 pc2
= CLAMP(p
[2], 0.0F
, 1.0F
);
2265 pc3
= CLAMP(p
[3], 0.0F
, 1.0F
);
2267 /* compare four texcoords vs. four texture samples */
2268 switch (sampler
->compare_func
) {
2269 case PIPE_FUNC_LESS
:
2270 k0
= pc0
< rgba
[0][0];
2271 k1
= pc1
< rgba
[0][1];
2272 k2
= pc2
< rgba
[0][2];
2273 k3
= pc3
< rgba
[0][3];
2275 case PIPE_FUNC_LEQUAL
:
2276 k0
= pc0
<= rgba
[0][0];
2277 k1
= pc1
<= rgba
[0][1];
2278 k2
= pc2
<= rgba
[0][2];
2279 k3
= pc3
<= rgba
[0][3];
2281 case PIPE_FUNC_GREATER
:
2282 k0
= pc0
> rgba
[0][0];
2283 k1
= pc1
> rgba
[0][1];
2284 k2
= pc2
> rgba
[0][2];
2285 k3
= pc3
> rgba
[0][3];
2287 case PIPE_FUNC_GEQUAL
:
2288 k0
= pc0
>= rgba
[0][0];
2289 k1
= pc1
>= rgba
[0][1];
2290 k2
= pc2
>= rgba
[0][2];
2291 k3
= pc3
>= rgba
[0][3];
2293 case PIPE_FUNC_EQUAL
:
2294 k0
= pc0
== rgba
[0][0];
2295 k1
= pc1
== rgba
[0][1];
2296 k2
= pc2
== rgba
[0][2];
2297 k3
= pc3
== rgba
[0][3];
2299 case PIPE_FUNC_NOTEQUAL
:
2300 k0
= pc0
!= rgba
[0][0];
2301 k1
= pc1
!= rgba
[0][1];
2302 k2
= pc2
!= rgba
[0][2];
2303 k3
= pc3
!= rgba
[0][3];
2305 case PIPE_FUNC_ALWAYS
:
2306 k0
= k1
= k2
= k3
= 1;
2308 case PIPE_FUNC_NEVER
:
2309 k0
= k1
= k2
= k3
= 0;
2312 k0
= k1
= k2
= k3
= 0;
2317 if (sampler
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
) {
2318 /* convert four pass/fail values to an intensity in [0,1] */
2319 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
2321 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2322 for (j
= 0; j
< 4; j
++) {
2323 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
2327 for (j
= 0; j
< 4; j
++) {
2338 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
2339 * Put face info into the sampler faces[] array.
2342 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
2343 const float s
[TGSI_QUAD_SIZE
],
2344 const float t
[TGSI_QUAD_SIZE
],
2345 const float p
[TGSI_QUAD_SIZE
],
2346 const float c0
[TGSI_QUAD_SIZE
],
2347 const float c1
[TGSI_QUAD_SIZE
],
2348 enum tgsi_sampler_control control
,
2349 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2351 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2353 float ssss
[4], tttt
[4];
2355 /* Not actually used, but the intermediate steps that do the
2356 * dereferencing don't know it.
2358 static float pppp
[4] = { 0, 0, 0, 0 };
2366 direction target sc tc ma
2367 ---------- ------------------------------- --- --- ---
2368 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2369 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2370 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2371 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2372 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2373 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2376 /* Choose the cube face and compute new s/t coords for the 2D face.
2378 * Use the same cube face for all four pixels in the quad.
2380 * This isn't ideal, but if we want to use a different cube face
2381 * per pixel in the quad, we'd have to also compute the per-face
2382 * LOD here too. That's because the four post-face-selection
2383 * texcoords are no longer related to each other (they're
2384 * per-face!) so we can't use subtraction to compute the partial
2385 * deriviates to compute the LOD. Doing so (near cube edges
2386 * anyway) gives us pretty much random values.
2389 /* use the average of the four pixel's texcoords to choose the face */
2390 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
2391 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
2392 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
2393 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
2395 if (arx
>= ary
&& arx
>= arz
) {
2396 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
2397 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
2398 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2399 const float ima
= -0.5F
/ fabsf(s
[j
]);
2400 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
2401 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2402 samp
->faces
[j
] = face
;
2405 else if (ary
>= arx
&& ary
>= arz
) {
2406 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
2407 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
2408 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2409 const float ima
= -0.5F
/ fabsf(t
[j
]);
2410 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
2411 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
2412 samp
->faces
[j
] = face
;
2416 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
2417 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
2418 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2419 const float ima
= -0.5F
/ fabsf(p
[j
]);
2420 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
2421 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2422 samp
->faces
[j
] = face
;
2427 /* In our little pipeline, the compare stage is next. If compare
2428 * is not active, this will point somewhere deeper into the
2429 * pipeline, eg. to mip_filter or even img_filter.
2431 samp
->compare(tgsi_sampler
, ssss
, tttt
, pppp
, c0
, c1
, control
, rgba
);
2436 do_swizzling(const struct sp_sampler_variant
*samp
,
2437 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2438 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2441 const unsigned swizzle_r
= samp
->key
.bits
.swizzle_r
;
2442 const unsigned swizzle_g
= samp
->key
.bits
.swizzle_g
;
2443 const unsigned swizzle_b
= samp
->key
.bits
.swizzle_b
;
2444 const unsigned swizzle_a
= samp
->key
.bits
.swizzle_a
;
2446 switch (swizzle_r
) {
2447 case PIPE_SWIZZLE_ZERO
:
2448 for (j
= 0; j
< 4; j
++)
2451 case PIPE_SWIZZLE_ONE
:
2452 for (j
= 0; j
< 4; j
++)
2456 assert(swizzle_r
< 4);
2457 for (j
= 0; j
< 4; j
++)
2458 out
[0][j
] = in
[swizzle_r
][j
];
2461 switch (swizzle_g
) {
2462 case PIPE_SWIZZLE_ZERO
:
2463 for (j
= 0; j
< 4; j
++)
2466 case PIPE_SWIZZLE_ONE
:
2467 for (j
= 0; j
< 4; j
++)
2471 assert(swizzle_g
< 4);
2472 for (j
= 0; j
< 4; j
++)
2473 out
[1][j
] = in
[swizzle_g
][j
];
2476 switch (swizzle_b
) {
2477 case PIPE_SWIZZLE_ZERO
:
2478 for (j
= 0; j
< 4; j
++)
2481 case PIPE_SWIZZLE_ONE
:
2482 for (j
= 0; j
< 4; j
++)
2486 assert(swizzle_b
< 4);
2487 for (j
= 0; j
< 4; j
++)
2488 out
[2][j
] = in
[swizzle_b
][j
];
2491 switch (swizzle_a
) {
2492 case PIPE_SWIZZLE_ZERO
:
2493 for (j
= 0; j
< 4; j
++)
2496 case PIPE_SWIZZLE_ONE
:
2497 for (j
= 0; j
< 4; j
++)
2501 assert(swizzle_a
< 4);
2502 for (j
= 0; j
< 4; j
++)
2503 out
[3][j
] = in
[swizzle_a
][j
];
2509 sample_swizzle(struct tgsi_sampler
*tgsi_sampler
,
2510 const float s
[TGSI_QUAD_SIZE
],
2511 const float t
[TGSI_QUAD_SIZE
],
2512 const float p
[TGSI_QUAD_SIZE
],
2513 const float c0
[TGSI_QUAD_SIZE
],
2514 const float c1
[TGSI_QUAD_SIZE
],
2515 enum tgsi_sampler_control control
,
2516 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2518 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2519 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2521 samp
->sample_target(tgsi_sampler
, s
, t
, p
, c0
, c1
, control
, rgba_temp
);
2523 do_swizzling(samp
, rgba_temp
, rgba
);
2527 static wrap_nearest_func
2528 get_nearest_unorm_wrap(unsigned mode
)
2531 case PIPE_TEX_WRAP_CLAMP
:
2532 return wrap_nearest_unorm_clamp
;
2533 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2534 return wrap_nearest_unorm_clamp_to_edge
;
2535 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2536 return wrap_nearest_unorm_clamp_to_border
;
2539 return wrap_nearest_unorm_clamp
;
2544 static wrap_nearest_func
2545 get_nearest_wrap(unsigned mode
)
2548 case PIPE_TEX_WRAP_REPEAT
:
2549 return wrap_nearest_repeat
;
2550 case PIPE_TEX_WRAP_CLAMP
:
2551 return wrap_nearest_clamp
;
2552 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2553 return wrap_nearest_clamp_to_edge
;
2554 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2555 return wrap_nearest_clamp_to_border
;
2556 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2557 return wrap_nearest_mirror_repeat
;
2558 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2559 return wrap_nearest_mirror_clamp
;
2560 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2561 return wrap_nearest_mirror_clamp_to_edge
;
2562 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2563 return wrap_nearest_mirror_clamp_to_border
;
2566 return wrap_nearest_repeat
;
2571 static wrap_linear_func
2572 get_linear_unorm_wrap(unsigned mode
)
2575 case PIPE_TEX_WRAP_CLAMP
:
2576 return wrap_linear_unorm_clamp
;
2577 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2578 return wrap_linear_unorm_clamp_to_edge
;
2579 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2580 return wrap_linear_unorm_clamp_to_border
;
2583 return wrap_linear_unorm_clamp
;
2588 static wrap_linear_func
2589 get_linear_wrap(unsigned mode
)
2592 case PIPE_TEX_WRAP_REPEAT
:
2593 return wrap_linear_repeat
;
2594 case PIPE_TEX_WRAP_CLAMP
:
2595 return wrap_linear_clamp
;
2596 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2597 return wrap_linear_clamp_to_edge
;
2598 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2599 return wrap_linear_clamp_to_border
;
2600 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2601 return wrap_linear_mirror_repeat
;
2602 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2603 return wrap_linear_mirror_clamp
;
2604 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2605 return wrap_linear_mirror_clamp_to_edge
;
2606 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2607 return wrap_linear_mirror_clamp_to_border
;
2610 return wrap_linear_repeat
;
2616 * Is swizzling needed for the given state key?
2619 any_swizzle(union sp_sampler_key key
)
2621 return (key
.bits
.swizzle_r
!= PIPE_SWIZZLE_RED
||
2622 key
.bits
.swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2623 key
.bits
.swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2624 key
.bits
.swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
2628 static compute_lambda_func
2629 get_lambda_func(const union sp_sampler_key key
)
2631 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
2632 return compute_lambda_vert
;
2634 switch (key
.bits
.target
) {
2636 case PIPE_TEXTURE_1D
:
2637 case PIPE_TEXTURE_1D_ARRAY
:
2638 return compute_lambda_1d
;
2639 case PIPE_TEXTURE_2D
:
2640 case PIPE_TEXTURE_2D_ARRAY
:
2641 case PIPE_TEXTURE_RECT
:
2642 case PIPE_TEXTURE_CUBE
:
2643 case PIPE_TEXTURE_CUBE_ARRAY
:
2644 return compute_lambda_2d
;
2645 case PIPE_TEXTURE_3D
:
2646 return compute_lambda_3d
;
2649 return compute_lambda_1d
;
2654 static img_filter_func
2655 get_img_filter(const union sp_sampler_key key
,
2657 const struct pipe_sampler_state
*sampler
)
2659 switch (key
.bits
.target
) {
2661 case PIPE_TEXTURE_1D
:
2662 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2663 return img_filter_1d_nearest
;
2665 return img_filter_1d_linear
;
2667 case PIPE_TEXTURE_1D_ARRAY
:
2668 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2669 return img_filter_1d_array_nearest
;
2671 return img_filter_1d_array_linear
;
2673 case PIPE_TEXTURE_2D
:
2674 case PIPE_TEXTURE_RECT
:
2675 /* Try for fast path:
2677 if (key
.bits
.is_pot
&&
2678 sampler
->wrap_s
== sampler
->wrap_t
&&
2679 sampler
->normalized_coords
)
2681 switch (sampler
->wrap_s
) {
2682 case PIPE_TEX_WRAP_REPEAT
:
2684 case PIPE_TEX_FILTER_NEAREST
:
2685 return img_filter_2d_nearest_repeat_POT
;
2686 case PIPE_TEX_FILTER_LINEAR
:
2687 return img_filter_2d_linear_repeat_POT
;
2692 case PIPE_TEX_WRAP_CLAMP
:
2694 case PIPE_TEX_FILTER_NEAREST
:
2695 return img_filter_2d_nearest_clamp_POT
;
2701 /* Otherwise use default versions:
2703 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2704 return img_filter_2d_nearest
;
2706 return img_filter_2d_linear
;
2708 case PIPE_TEXTURE_2D_ARRAY
:
2709 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2710 return img_filter_2d_array_nearest
;
2712 return img_filter_2d_array_linear
;
2714 case PIPE_TEXTURE_CUBE
:
2715 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2716 return img_filter_cube_nearest
;
2718 return img_filter_cube_linear
;
2720 case PIPE_TEXTURE_CUBE_ARRAY
:
2721 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2722 return img_filter_cube_array_nearest
;
2724 return img_filter_cube_array_linear
;
2726 case PIPE_TEXTURE_3D
:
2727 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2728 return img_filter_3d_nearest
;
2730 return img_filter_3d_linear
;
2734 return img_filter_1d_nearest
;
2740 * Bind the given texture object and texture cache to the sampler variant.
2743 sp_sampler_variant_bind_view( struct sp_sampler_variant
*samp
,
2744 struct softpipe_tex_tile_cache
*tex_cache
,
2745 const struct pipe_sampler_view
*view
)
2747 const struct pipe_resource
*texture
= view
->texture
;
2750 samp
->cache
= tex_cache
;
2751 samp
->xpot
= util_logbase2( texture
->width0
);
2752 samp
->ypot
= util_logbase2( texture
->height0
);
2757 sp_sampler_variant_destroy( struct sp_sampler_variant
*samp
)
2764 sample_get_dims(struct tgsi_sampler
*tgsi_sampler
, int level
,
2767 struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2768 const struct pipe_sampler_view
*view
= samp
->view
;
2769 const struct pipe_resource
*texture
= view
->texture
;
2771 /* undefined according to EXT_gpu_program */
2772 level
+= view
->u
.tex
.first_level
;
2773 if (level
> view
->u
.tex
.last_level
)
2776 dims
[0] = u_minify(texture
->width0
, level
);
2778 switch(texture
->target
) {
2779 case PIPE_TEXTURE_1D_ARRAY
:
2780 dims
[1] = texture
->array_size
;
2782 case PIPE_TEXTURE_1D
:
2784 case PIPE_TEXTURE_2D_ARRAY
:
2785 dims
[2] = texture
->array_size
;
2787 case PIPE_TEXTURE_2D
:
2788 case PIPE_TEXTURE_CUBE
:
2789 case PIPE_TEXTURE_RECT
:
2790 dims
[1] = u_minify(texture
->height0
, level
);
2792 case PIPE_TEXTURE_3D
:
2793 dims
[1] = u_minify(texture
->height0
, level
);
2794 dims
[2] = u_minify(texture
->depth0
, level
);
2796 case PIPE_TEXTURE_CUBE_ARRAY
:
2797 dims
[1] = u_minify(texture
->height0
, level
);
2798 dims
[2] = texture
->array_size
/ 6;
2801 dims
[0] /= util_format_get_blocksize(view
->format
);
2804 assert(!"unexpected texture target in sample_get_dims()");
2810 * This function is only used for getting unfiltered texels via the
2811 * TXF opcode. The GL spec says that out-of-bounds texel fetches
2812 * produce undefined results. Instead of crashing, lets just clamp
2813 * coords to the texture image size.
2816 sample_get_texels(struct tgsi_sampler
*tgsi_sampler
,
2817 const int v_i
[TGSI_QUAD_SIZE
],
2818 const int v_j
[TGSI_QUAD_SIZE
],
2819 const int v_k
[TGSI_QUAD_SIZE
],
2820 const int lod
[TGSI_QUAD_SIZE
],
2821 const int8_t offset
[3],
2822 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2824 const struct sp_sampler_variant
*samp
= sp_sampler_variant(tgsi_sampler
);
2825 union tex_tile_address addr
;
2826 const struct pipe_resource
*texture
= samp
->view
->texture
;
2829 const bool need_swizzle
= any_swizzle(samp
->key
);
2830 int width
, height
, depth
, layers
;
2833 /* TODO write a better test for LOD */
2834 addr
.bits
.level
= lod
[0];
2836 width
= u_minify(texture
->width0
, addr
.bits
.level
);
2837 height
= u_minify(texture
->height0
, addr
.bits
.level
);
2838 depth
= u_minify(texture
->depth0
, addr
.bits
.level
);
2839 layers
= texture
->array_size
;
2841 switch(texture
->target
) {
2843 case PIPE_TEXTURE_1D
:
2844 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2845 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2846 tx
= get_texel_2d(samp
, addr
, x
, 0);
2847 for (c
= 0; c
< 4; c
++) {
2852 case PIPE_TEXTURE_1D_ARRAY
:
2853 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2854 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2855 int y
= CLAMP(v_j
[j
], 0, layers
- 1);
2856 tx
= get_texel_1d_array(samp
, addr
, x
, y
);
2857 for (c
= 0; c
< 4; c
++) {
2862 case PIPE_TEXTURE_2D
:
2863 case PIPE_TEXTURE_RECT
:
2864 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2865 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2866 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2867 tx
= get_texel_2d(samp
, addr
, x
, y
);
2868 for (c
= 0; c
< 4; c
++) {
2873 case PIPE_TEXTURE_2D_ARRAY
:
2874 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2875 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2876 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2877 int layer
= CLAMP(v_k
[j
], 0, layers
- 1);
2878 tx
= get_texel_2d_array(samp
, addr
, x
, y
, layer
);
2879 for (c
= 0; c
< 4; c
++) {
2884 case PIPE_TEXTURE_3D
:
2885 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2886 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2887 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2888 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
2890 tx
= get_texel_3d(samp
, addr
, x
, y
, z
);
2891 for (c
= 0; c
< 4; c
++) {
2896 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
2898 assert(!"Unknown or CUBE texture type in TXF processing\n");
2903 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2904 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
2905 do_swizzling(samp
, rgba_temp
, rgba
);
2911 * Create a sampler variant for a given set of non-orthogonal state.
2913 struct sp_sampler_variant
*
2914 sp_create_sampler_variant( const struct pipe_sampler_state
*sampler
,
2915 const union sp_sampler_key key
)
2917 struct sp_sampler_variant
*samp
= CALLOC_STRUCT(sp_sampler_variant
);
2921 samp
->sampler
= sampler
;
2924 /* Note that (for instance) linear_texcoord_s and
2925 * nearest_texcoord_s may be active at the same time, if the
2926 * sampler min_img_filter differs from its mag_img_filter.
2928 if (sampler
->normalized_coords
) {
2929 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
2930 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
2931 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
2933 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
2934 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
2935 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
2938 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
2939 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
2940 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
2942 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
2943 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
2944 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
2947 samp
->compute_lambda
= get_lambda_func( key
);
2949 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
2950 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
2952 switch (sampler
->min_mip_filter
) {
2953 case PIPE_TEX_MIPFILTER_NONE
:
2954 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
2955 samp
->mip_filter
= mip_filter_none_no_filter_select
;
2957 samp
->mip_filter
= mip_filter_none
;
2960 case PIPE_TEX_MIPFILTER_NEAREST
:
2961 samp
->mip_filter
= mip_filter_nearest
;
2964 case PIPE_TEX_MIPFILTER_LINEAR
:
2965 if (key
.bits
.is_pot
&&
2966 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
2967 sampler
->normalized_coords
&&
2968 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
2969 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
2970 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
) {
2971 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2974 samp
->mip_filter
= mip_filter_linear
;
2977 /* Anisotropic filtering extension. */
2978 if (sampler
->max_anisotropy
> 1) {
2979 samp
->mip_filter
= mip_filter_linear_aniso
;
2981 /* Override min_img_filter:
2982 * min_img_filter needs to be set to NEAREST since we need to access
2983 * each texture pixel as it is and weight it later; using linear
2984 * filters will have incorrect results.
2985 * By setting the filter to NEAREST here, we can avoid calling the
2986 * generic img_filter_2d_nearest in the anisotropic filter function,
2987 * making it possible to use one of the accelerated implementations
2989 samp
->min_img_filter
= get_img_filter(key
, PIPE_TEX_FILTER_NEAREST
, sampler
);
2991 /* on first access create the lookup table containing the filter weights. */
2993 create_filter_table();
3000 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
3001 samp
->compare
= sample_compare
;
3004 /* Skip compare operation by promoting the mip_filter function
3007 samp
->compare
= samp
->mip_filter
;
3010 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
|| key
.bits
.target
== PIPE_TEXTURE_CUBE_ARRAY
) {
3011 samp
->sample_target
= sample_cube
;
3019 /* Skip cube face determination by promoting the compare
3022 samp
->sample_target
= samp
->compare
;
3025 if (any_swizzle(key
)) {
3026 samp
->base
.get_samples
= sample_swizzle
;
3029 samp
->base
.get_samples
= samp
->sample_target
;
3032 samp
->base
.get_dims
= sample_get_dims
;
3033 samp
->base
.get_texel
= sample_get_texels
;