1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008 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"
49 * Return fractional part of 'f'. Used for computing interpolation weights.
50 * Need to be careful with negative values.
51 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
52 * of improperly weighted linear-filtered textures.
53 * The tests/texwrap.c demo is a good test.
58 return f
- util_ifloor(f
);
64 * Linear interpolation macro
67 lerp(float a
, float v0
, float v1
)
69 return v0
+ a
* (v1
- v0
);
74 * Do 2D/biliner interpolation of float values.
75 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
76 * a and b are the horizontal and vertical interpolants.
77 * It's important that this function is inlined when compiled with
78 * optimization! If we find that's not true on some systems, convert
82 lerp_2d(float a
, float b
,
83 float v00
, float v10
, float v01
, float v11
)
85 const float temp0
= lerp(a
, v00
, v10
);
86 const float temp1
= lerp(a
, v01
, v11
);
87 return lerp(b
, temp0
, temp1
);
92 * As above, but 3D interpolation of 8 values.
95 lerp_3d(float a
, float b
, float c
,
96 float v000
, float v100
, float v010
, float v110
,
97 float v001
, float v101
, float v011
, float v111
)
99 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
100 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
101 return lerp(c
, temp0
, temp1
);
107 * Compute coord % size for repeat wrap modes.
108 * Note that if coord is a signed integer, coord % size doesn't give
109 * the right value for coord < 0 (in terms of texture repeat). Just
110 * casting to unsigned fixes that.
113 repeat(int coord
, unsigned size
)
115 return (int) ((unsigned) coord
% size
);
120 * Apply texture coord wrapping mode and return integer texture indexes
121 * for a vector of four texcoords (S or T or P).
122 * \param wrapMode PIPE_TEX_WRAP_x
123 * \param s the incoming texcoords
124 * \param size the texture image size
125 * \param icoord returns the integer texcoords
126 * \return integer texture index
129 wrap_nearest_repeat(const float s
[4], unsigned size
, int icoord
[4])
132 /* s limited to [0,1) */
133 /* i limited to [0,size-1] */
134 for (ch
= 0; ch
< 4; ch
++) {
135 int i
= util_ifloor(s
[ch
] * size
);
136 icoord
[ch
] = repeat(i
, size
);
142 wrap_nearest_clamp(const float s
[4], unsigned size
, int icoord
[4])
145 /* s limited to [0,1] */
146 /* i limited to [0,size-1] */
147 for (ch
= 0; ch
< 4; ch
++) {
150 else if (s
[ch
] >= 1.0F
)
151 icoord
[ch
] = size
- 1;
153 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
159 wrap_nearest_clamp_to_edge(const float s
[4], unsigned size
, int icoord
[4])
162 /* s limited to [min,max] */
163 /* i limited to [0, size-1] */
164 const float min
= 1.0F
/ (2.0F
* size
);
165 const float max
= 1.0F
- min
;
166 for (ch
= 0; ch
< 4; ch
++) {
169 else if (s
[ch
] > max
)
170 icoord
[ch
] = size
- 1;
172 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
178 wrap_nearest_clamp_to_border(const float s
[4], unsigned size
, int icoord
[4])
181 /* s limited to [min,max] */
182 /* i limited to [-1, size] */
183 const float min
= -1.0F
/ (2.0F
* size
);
184 const float max
= 1.0F
- min
;
185 for (ch
= 0; ch
< 4; ch
++) {
188 else if (s
[ch
] >= max
)
191 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
197 wrap_nearest_mirror_repeat(const float s
[4], unsigned size
, int icoord
[4])
200 const float min
= 1.0F
/ (2.0F
* size
);
201 const float max
= 1.0F
- min
;
202 for (ch
= 0; ch
< 4; ch
++) {
203 const int flr
= util_ifloor(s
[ch
]);
206 u
= 1.0F
- (s
[ch
] - (float) flr
);
208 u
= s
[ch
] - (float) flr
;
212 icoord
[ch
] = size
- 1;
214 icoord
[ch
] = util_ifloor(u
* size
);
220 wrap_nearest_mirror_clamp(const float s
[4], unsigned size
, int icoord
[4])
223 for (ch
= 0; ch
< 4; ch
++) {
224 /* s limited to [0,1] */
225 /* i limited to [0,size-1] */
226 const float u
= fabsf(s
[ch
]);
230 icoord
[ch
] = size
- 1;
232 icoord
[ch
] = util_ifloor(u
* size
);
238 wrap_nearest_mirror_clamp_to_edge(const float s
[4], unsigned size
,
242 /* s limited to [min,max] */
243 /* i limited to [0, size-1] */
244 const float min
= 1.0F
/ (2.0F
* size
);
245 const float max
= 1.0F
- min
;
246 for (ch
= 0; ch
< 4; ch
++) {
247 const float u
= fabsf(s
[ch
]);
251 icoord
[ch
] = size
- 1;
253 icoord
[ch
] = util_ifloor(u
* size
);
259 wrap_nearest_mirror_clamp_to_border(const float s
[4], unsigned size
,
263 /* s limited to [min,max] */
264 /* i limited to [0, size-1] */
265 const float min
= -1.0F
/ (2.0F
* size
);
266 const float max
= 1.0F
- min
;
267 for (ch
= 0; ch
< 4; ch
++) {
268 const float u
= fabsf(s
[ch
]);
274 icoord
[ch
] = util_ifloor(u
* size
);
280 * Used to compute texel locations for linear sampling for four texcoords.
281 * \param wrapMode PIPE_TEX_WRAP_x
282 * \param s the texcoords
283 * \param size the texture image size
284 * \param icoord0 returns first texture indexes
285 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
286 * \param w returns blend factor/weight between texture indexes
287 * \param icoord returns the computed integer texture coords
290 wrap_linear_repeat(const float s
[4], unsigned size
,
291 int icoord0
[4], int icoord1
[4], float w
[4])
294 for (ch
= 0; ch
< 4; ch
++) {
295 float u
= s
[ch
] * size
- 0.5F
;
296 icoord0
[ch
] = repeat(util_ifloor(u
), size
);
297 icoord1
[ch
] = repeat(icoord0
[ch
] + 1, size
);
304 wrap_linear_clamp(const float s
[4], unsigned size
,
305 int icoord0
[4], int icoord1
[4], float w
[4])
308 for (ch
= 0; ch
< 4; ch
++) {
309 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
311 icoord0
[ch
] = util_ifloor(u
);
312 icoord1
[ch
] = icoord0
[ch
] + 1;
319 wrap_linear_clamp_to_edge(const float s
[4], unsigned size
,
320 int icoord0
[4], int icoord1
[4], float w
[4])
323 for (ch
= 0; ch
< 4; ch
++) {
324 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
326 icoord0
[ch
] = util_ifloor(u
);
327 icoord1
[ch
] = icoord0
[ch
] + 1;
330 if (icoord1
[ch
] >= (int) size
)
331 icoord1
[ch
] = size
- 1;
338 wrap_linear_clamp_to_border(const float s
[4], unsigned size
,
339 int icoord0
[4], int icoord1
[4], float w
[4])
341 const float min
= -1.0F
/ (2.0F
* size
);
342 const float max
= 1.0F
- min
;
344 for (ch
= 0; ch
< 4; ch
++) {
345 float u
= CLAMP(s
[ch
], min
, max
);
347 icoord0
[ch
] = util_ifloor(u
);
348 icoord1
[ch
] = icoord0
[ch
] + 1;
355 wrap_linear_mirror_repeat(const float s
[4], unsigned size
,
356 int icoord0
[4], int icoord1
[4], float w
[4])
359 for (ch
= 0; ch
< 4; ch
++) {
360 const int flr
= util_ifloor(s
[ch
]);
363 u
= 1.0F
- (s
[ch
] - (float) flr
);
365 u
= s
[ch
] - (float) flr
;
367 icoord0
[ch
] = util_ifloor(u
);
368 icoord1
[ch
] = icoord0
[ch
] + 1;
371 if (icoord1
[ch
] >= (int) size
)
372 icoord1
[ch
] = size
- 1;
379 wrap_linear_mirror_clamp(const float s
[4], unsigned size
,
380 int icoord0
[4], int icoord1
[4], float w
[4])
383 for (ch
= 0; ch
< 4; ch
++) {
384 float u
= fabsf(s
[ch
]);
390 icoord0
[ch
] = util_ifloor(u
);
391 icoord1
[ch
] = icoord0
[ch
] + 1;
398 wrap_linear_mirror_clamp_to_edge(const float s
[4], unsigned size
,
399 int icoord0
[4], int icoord1
[4], float w
[4])
402 for (ch
= 0; ch
< 4; ch
++) {
403 float u
= fabsf(s
[ch
]);
409 icoord0
[ch
] = util_ifloor(u
);
410 icoord1
[ch
] = icoord0
[ch
] + 1;
413 if (icoord1
[ch
] >= (int) size
)
414 icoord1
[ch
] = size
- 1;
421 wrap_linear_mirror_clamp_to_border(const float s
[4], unsigned size
,
422 int icoord0
[4], int icoord1
[4], float w
[4])
424 const float min
= -1.0F
/ (2.0F
* size
);
425 const float max
= 1.0F
- min
;
427 for (ch
= 0; ch
< 4; ch
++) {
428 float u
= fabsf(s
[ch
]);
436 icoord0
[ch
] = util_ifloor(u
);
437 icoord1
[ch
] = icoord0
[ch
] + 1;
444 * For RECT textures / unnormalized texcoords
445 * Only a subset of wrap modes supported.
448 wrap_nearest_unorm_clamp(const float s
[4], unsigned size
, int icoord
[4])
451 for (ch
= 0; ch
< 4; ch
++) {
452 int i
= util_ifloor(s
[ch
]);
453 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
459 * Handles clamp_to_edge and clamp_to_border:
462 wrap_nearest_unorm_clamp_to_border(const float s
[4], unsigned size
,
466 for (ch
= 0; ch
< 4; ch
++) {
467 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
473 * For RECT textures / unnormalized texcoords.
474 * Only a subset of wrap modes supported.
477 wrap_linear_unorm_clamp(const float s
[4], unsigned size
,
478 int icoord0
[4], int icoord1
[4], float w
[4])
481 for (ch
= 0; ch
< 4; ch
++) {
482 /* Not exactly what the spec says, but it matches NVIDIA output */
483 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
484 icoord0
[ch
] = util_ifloor(u
);
485 icoord1
[ch
] = icoord0
[ch
] + 1;
492 wrap_linear_unorm_clamp_to_border(const float s
[4], unsigned size
,
493 int icoord0
[4], int icoord1
[4], float w
[4])
496 for (ch
= 0; ch
< 4; ch
++) {
497 float u
= CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
);
499 icoord0
[ch
] = util_ifloor(u
);
500 icoord1
[ch
] = icoord0
[ch
] + 1;
501 if (icoord1
[ch
] > (int) size
- 1)
502 icoord1
[ch
] = size
- 1;
510 * Examine the quad's texture coordinates to compute the partial
511 * derivatives w.r.t X and Y, then compute lambda (level of detail).
514 compute_lambda_1d(const struct sp_sampler_varient
*samp
,
515 const float s
[QUAD_SIZE
],
516 const float t
[QUAD_SIZE
],
517 const float p
[QUAD_SIZE
],
520 const struct pipe_texture
*texture
= samp
->texture
;
521 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
522 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
523 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
524 float rho
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
527 lambda
= util_fast_log2(rho
);
528 lambda
+= lodbias
+ sampler
->lod_bias
;
529 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
536 compute_lambda_2d(const struct sp_sampler_varient
*samp
,
537 const float s
[QUAD_SIZE
],
538 const float t
[QUAD_SIZE
],
539 const float p
[QUAD_SIZE
],
542 const struct pipe_texture
*texture
= samp
->texture
;
543 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
544 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
545 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
546 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
547 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
548 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
549 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height
[0];
550 float rho
= MAX2(maxx
, maxy
);
553 lambda
= util_fast_log2(rho
);
554 lambda
+= lodbias
+ sampler
->lod_bias
;
555 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
562 compute_lambda_3d(const struct sp_sampler_varient
*samp
,
563 const float s
[QUAD_SIZE
],
564 const float t
[QUAD_SIZE
],
565 const float p
[QUAD_SIZE
],
568 const struct pipe_texture
*texture
= samp
->texture
;
569 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
570 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
571 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
572 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
573 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
574 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
575 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
576 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
577 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height
[0];
578 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth
[0];
581 rho
= MAX2(maxx
, maxy
);
582 rho
= MAX2(rho
, maxz
);
584 lambda
= util_fast_log2(rho
);
585 lambda
+= lodbias
+ sampler
->lod_bias
;
586 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
593 * Compute lambda for a vertex texture sampler.
594 * Since there aren't derivatives to use, just return the LOD bias.
597 compute_lambda_vert(const struct sp_sampler_varient
*samp
,
598 const float s
[QUAD_SIZE
],
599 const float t
[QUAD_SIZE
],
600 const float p
[QUAD_SIZE
],
609 * Get a texel from a texture, using the texture tile cache.
611 * \param addr the template tex address containing cube, z, face info.
612 * \param x the x coord of texel within 2D image
613 * \param y the y coord of texel within 2D image
614 * \param rgba the quad to put the texel/color into
616 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
617 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
623 static INLINE
const float *
624 get_texel_2d_no_border(const struct sp_sampler_varient
*samp
,
625 union tex_tile_address addr
, int x
, int y
)
627 const struct softpipe_tex_cached_tile
*tile
;
629 addr
.bits
.x
= x
/ TILE_SIZE
;
630 addr
.bits
.y
= y
/ TILE_SIZE
;
634 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
636 return &tile
->data
.color
[y
][x
][0];
640 static INLINE
const float *
641 get_texel_2d(const struct sp_sampler_varient
*samp
,
642 union tex_tile_address addr
, int x
, int y
)
644 const struct pipe_texture
*texture
= samp
->texture
;
645 unsigned level
= addr
.bits
.level
;
647 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
648 y
< 0 || y
>= (int) texture
->height
[level
]) {
649 return samp
->sampler
->border_color
;
652 return get_texel_2d_no_border( samp
, addr
, x
, y
);
657 /* Gather a quad of adjacent texels within a tile:
660 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
661 union tex_tile_address addr
,
662 unsigned x
, unsigned y
,
665 const struct softpipe_tex_cached_tile
*tile
;
667 addr
.bits
.x
= x
/ TILE_SIZE
;
668 addr
.bits
.y
= y
/ TILE_SIZE
;
672 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
674 out
[0] = &tile
->data
.color
[y
][x
][0];
675 out
[1] = &tile
->data
.color
[y
][x
+1][0];
676 out
[2] = &tile
->data
.color
[y
+1][x
][0];
677 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
681 /* Gather a quad of potentially non-adjacent texels:
684 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
685 union tex_tile_address addr
,
690 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
691 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
692 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
693 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
696 /* Can involve a lot of unnecessary checks for border color:
699 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
700 union tex_tile_address addr
,
705 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
706 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
707 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
708 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
715 static INLINE
const float *
716 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
717 union tex_tile_address addr
, int x
, int y
, int z
)
719 const struct softpipe_tex_cached_tile
*tile
;
721 addr
.bits
.x
= x
/ TILE_SIZE
;
722 addr
.bits
.y
= y
/ TILE_SIZE
;
727 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
729 return &tile
->data
.color
[y
][x
][0];
733 static INLINE
const float *
734 get_texel_3d(const struct sp_sampler_varient
*samp
,
735 union tex_tile_address addr
, int x
, int y
, int z
)
737 const struct pipe_texture
*texture
= samp
->texture
;
738 unsigned level
= addr
.bits
.level
;
740 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
741 y
< 0 || y
>= (int) texture
->height
[level
] ||
742 z
< 0 || z
>= (int) texture
->depth
[level
]) {
743 return samp
->sampler
->border_color
;
746 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
752 * Given the logbase2 of a mipmap's base level size and a mipmap level,
753 * return the size (in texels) of that mipmap level.
754 * For example, if level[0].width = 256 then base_pot will be 8.
755 * If level = 2, then we'll return 64 (the width at level=2).
756 * Return 1 if level > base_pot.
758 static INLINE
unsigned
759 pot_level_size(unsigned base_pot
, unsigned level
)
761 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
765 /* Some image-filter fastpaths:
768 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
769 const float s
[QUAD_SIZE
],
770 const float t
[QUAD_SIZE
],
771 const float p
[QUAD_SIZE
],
773 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
775 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
777 unsigned level
= samp
->level
;
778 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
779 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
780 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
781 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
782 union tex_tile_address addr
;
785 addr
.bits
.level
= samp
->level
;
787 for (j
= 0; j
< QUAD_SIZE
; j
++) {
790 float u
= s
[j
] * xpot
- 0.5F
;
791 float v
= t
[j
] * ypot
- 0.5F
;
793 int uflr
= util_ifloor(u
);
794 int vflr
= util_ifloor(v
);
796 float xw
= u
- (float)uflr
;
797 float yw
= v
- (float)vflr
;
799 int x0
= uflr
& (xpot
- 1);
800 int y0
= vflr
& (ypot
- 1);
804 /* Can we fetch all four at once:
806 if (x0
< xmax
&& y0
< ymax
) {
807 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
810 unsigned x1
= (x0
+ 1) & (xpot
- 1);
811 unsigned y1
= (y0
+ 1) & (ypot
- 1);
812 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
815 /* interpolate R, G, B, A */
816 for (c
= 0; c
< 4; c
++) {
817 rgba
[c
][j
] = lerp_2d(xw
, yw
,
826 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
827 const float s
[QUAD_SIZE
],
828 const float t
[QUAD_SIZE
],
829 const float p
[QUAD_SIZE
],
831 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
833 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
835 unsigned level
= samp
->level
;
836 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
837 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
838 union tex_tile_address addr
;
841 addr
.bits
.level
= samp
->level
;
843 for (j
= 0; j
< QUAD_SIZE
; j
++) {
846 float u
= s
[j
] * xpot
;
847 float v
= t
[j
] * ypot
;
849 int uflr
= util_ifloor(u
);
850 int vflr
= util_ifloor(v
);
852 int x0
= uflr
& (xpot
- 1);
853 int y0
= vflr
& (ypot
- 1);
855 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
857 for (c
= 0; c
< 4; c
++) {
865 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
866 const float s
[QUAD_SIZE
],
867 const float t
[QUAD_SIZE
],
868 const float p
[QUAD_SIZE
],
870 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
872 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
874 unsigned level
= samp
->level
;
875 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
876 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
877 union tex_tile_address addr
;
880 addr
.bits
.level
= samp
->level
;
882 for (j
= 0; j
< QUAD_SIZE
; j
++) {
885 float u
= s
[j
] * xpot
;
886 float v
= t
[j
] * ypot
;
894 else if (x0
> xpot
- 1)
900 else if (y0
> ypot
- 1)
903 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
905 for (c
= 0; c
< 4; c
++) {
913 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
914 const float s
[QUAD_SIZE
],
915 const float t
[QUAD_SIZE
],
916 const float p
[QUAD_SIZE
],
918 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
920 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
921 const struct pipe_texture
*texture
= samp
->texture
;
925 union tex_tile_address addr
;
927 level0
= samp
->level
;
928 width
= texture
->width
[level0
];
933 addr
.bits
.level
= samp
->level
;
935 samp
->nearest_texcoord_s(s
, width
, x
);
937 for (j
= 0; j
< QUAD_SIZE
; j
++) {
938 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
940 for (c
= 0; c
< 4; c
++) {
948 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
949 const float s
[QUAD_SIZE
],
950 const float t
[QUAD_SIZE
],
951 const float p
[QUAD_SIZE
],
953 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
955 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
956 const struct pipe_texture
*texture
= samp
->texture
;
960 union tex_tile_address addr
;
963 level0
= samp
->level
;
964 width
= texture
->width
[level0
];
965 height
= texture
->height
[level0
];
971 addr
.bits
.level
= samp
->level
;
973 samp
->nearest_texcoord_s(s
, width
, x
);
974 samp
->nearest_texcoord_t(t
, height
, y
);
976 for (j
= 0; j
< QUAD_SIZE
; j
++) {
977 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
979 for (c
= 0; c
< 4; c
++) {
986 static INLINE
union tex_tile_address
987 face(union tex_tile_address addr
, unsigned face
)
989 addr
.bits
.face
= face
;
995 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
996 const float s
[QUAD_SIZE
],
997 const float t
[QUAD_SIZE
],
998 const float p
[QUAD_SIZE
],
1000 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1002 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1003 const struct pipe_texture
*texture
= samp
->texture
;
1004 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1008 union tex_tile_address addr
;
1010 level0
= samp
->level
;
1011 width
= texture
->width
[level0
];
1012 height
= texture
->height
[level0
];
1018 addr
.bits
.level
= samp
->level
;
1020 samp
->nearest_texcoord_s(s
, width
, x
);
1021 samp
->nearest_texcoord_t(t
, height
, y
);
1023 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1024 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1026 for (c
= 0; c
< 4; c
++) {
1027 rgba
[c
][j
] = out
[c
];
1034 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1035 const float s
[QUAD_SIZE
],
1036 const float t
[QUAD_SIZE
],
1037 const float p
[QUAD_SIZE
],
1039 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1041 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1042 const struct pipe_texture
*texture
= samp
->texture
;
1044 int width
, height
, depth
;
1045 int x
[4], y
[4], z
[4];
1046 union tex_tile_address addr
;
1048 level0
= samp
->level
;
1049 width
= texture
->width
[level0
];
1050 height
= texture
->height
[level0
];
1051 depth
= texture
->depth
[level0
];
1057 samp
->nearest_texcoord_s(s
, width
, x
);
1058 samp
->nearest_texcoord_t(t
, height
, y
);
1059 samp
->nearest_texcoord_p(p
, depth
, z
);
1062 addr
.bits
.level
= samp
->level
;
1064 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1065 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1067 for (c
= 0; c
< 4; c
++) {
1068 rgba
[c
][j
] = out
[c
];
1075 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1076 const float s
[QUAD_SIZE
],
1077 const float t
[QUAD_SIZE
],
1078 const float p
[QUAD_SIZE
],
1080 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1082 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1083 const struct pipe_texture
*texture
= samp
->texture
;
1087 float xw
[4]; /* weights */
1088 union tex_tile_address addr
;
1090 level0
= samp
->level
;
1091 width
= texture
->width
[level0
];
1096 addr
.bits
.level
= samp
->level
;
1098 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1100 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1101 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1102 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1105 /* interpolate R, G, B, A */
1106 for (c
= 0; c
< 4; c
++) {
1107 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1114 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1115 const float s
[QUAD_SIZE
],
1116 const float t
[QUAD_SIZE
],
1117 const float p
[QUAD_SIZE
],
1119 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1121 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1122 const struct pipe_texture
*texture
= samp
->texture
;
1125 int x0
[4], y0
[4], x1
[4], y1
[4];
1126 float xw
[4], yw
[4]; /* weights */
1127 union tex_tile_address addr
;
1129 level0
= samp
->level
;
1130 width
= texture
->width
[level0
];
1131 height
= texture
->height
[level0
];
1137 addr
.bits
.level
= samp
->level
;
1139 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1140 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1142 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1143 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1144 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1145 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1146 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1149 /* interpolate R, G, B, A */
1150 for (c
= 0; c
< 4; c
++) {
1151 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1160 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1161 const float s
[QUAD_SIZE
],
1162 const float t
[QUAD_SIZE
],
1163 const float p
[QUAD_SIZE
],
1165 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1167 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1168 const struct pipe_texture
*texture
= samp
->texture
;
1169 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1172 int x0
[4], y0
[4], x1
[4], y1
[4];
1173 float xw
[4], yw
[4]; /* weights */
1174 union tex_tile_address addr
;
1176 level0
= samp
->level
;
1177 width
= texture
->width
[level0
];
1178 height
= texture
->height
[level0
];
1184 addr
.bits
.level
= samp
->level
;
1186 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1187 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1189 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1190 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1191 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1192 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1193 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1194 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1197 /* interpolate R, G, B, A */
1198 for (c
= 0; c
< 4; c
++) {
1199 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1208 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1209 const float s
[QUAD_SIZE
],
1210 const float t
[QUAD_SIZE
],
1211 const float p
[QUAD_SIZE
],
1213 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1215 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1216 const struct pipe_texture
*texture
= samp
->texture
;
1218 int width
, height
, depth
;
1219 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1220 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1221 union tex_tile_address addr
;
1223 level0
= samp
->level
;
1224 width
= texture
->width
[level0
];
1225 height
= texture
->height
[level0
];
1226 depth
= texture
->depth
[level0
];
1229 addr
.bits
.level
= level0
;
1235 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1236 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1237 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1239 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1242 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1243 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1244 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1245 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1247 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1248 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1249 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1250 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1252 /* interpolate R, G, B, A */
1253 for (c
= 0; c
< 4; c
++) {
1254 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1265 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1266 const float s
[QUAD_SIZE
],
1267 const float t
[QUAD_SIZE
],
1268 const float p
[QUAD_SIZE
],
1270 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1272 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1273 const struct pipe_texture
*texture
= samp
->texture
;
1277 lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1278 level0
= (int)lambda
;
1282 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1284 else if (level0
>= texture
->last_level
) {
1285 samp
->level
= texture
->last_level
;
1286 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1289 float levelBlend
= lambda
- level0
;
1294 samp
->level
= level0
;
1295 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba0
);
1297 samp
->level
= level0
+1;
1298 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba1
);
1300 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1301 for (c
= 0; c
< 4; c
++) {
1302 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1310 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1311 const float s
[QUAD_SIZE
],
1312 const float t
[QUAD_SIZE
],
1313 const float p
[QUAD_SIZE
],
1315 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1317 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1318 const struct pipe_texture
*texture
= samp
->texture
;
1321 lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1325 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1328 samp
->level
= (int)(lambda
+ 0.5) ;
1329 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1330 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1334 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1335 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1336 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1337 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1338 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1344 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1345 const float s
[QUAD_SIZE
],
1346 const float t
[QUAD_SIZE
],
1347 const float p
[QUAD_SIZE
],
1349 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1351 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1352 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1355 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1358 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1365 * Specialized version of mip_filter_linear with hard-wired calls to
1366 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1369 mip_filter_linear_2d_linear_repeat_POT(
1370 struct tgsi_sampler
*tgsi_sampler
,
1371 const float s
[QUAD_SIZE
],
1372 const float t
[QUAD_SIZE
],
1373 const float p
[QUAD_SIZE
],
1375 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1377 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1378 const struct pipe_texture
*texture
= samp
->texture
;
1382 lambda
= compute_lambda_2d(samp
, s
, t
, p
, lodbias
);
1383 level0
= (int)lambda
;
1385 /* Catches both negative and large values of level0:
1387 if ((unsigned)level0
>= texture
->last_level
) {
1391 samp
->level
= texture
->last_level
;
1393 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1396 float levelBlend
= lambda
- level0
;
1401 samp
->level
= level0
;
1402 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba0
);
1404 samp
->level
= level0
+1;
1405 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba1
);
1407 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1408 for (c
= 0; c
< 4; c
++) {
1409 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1418 * Do shadow/depth comparisons.
1421 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1422 const float s
[QUAD_SIZE
],
1423 const float t
[QUAD_SIZE
],
1424 const float p
[QUAD_SIZE
],
1426 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1428 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1429 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1430 int j
, k0
, k1
, k2
, k3
;
1433 samp
->mip_filter( tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1436 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1437 * When we sampled the depth texture, the depth value was put into all
1438 * RGBA channels. We look at the red channel here.
1441 /* compare four texcoords vs. four texture samples */
1442 switch (sampler
->compare_func
) {
1443 case PIPE_FUNC_LESS
:
1444 k0
= p
[0] < rgba
[0][0];
1445 k1
= p
[1] < rgba
[0][1];
1446 k2
= p
[2] < rgba
[0][2];
1447 k3
= p
[3] < rgba
[0][3];
1449 case PIPE_FUNC_LEQUAL
:
1450 k0
= p
[0] <= rgba
[0][0];
1451 k1
= p
[1] <= rgba
[0][1];
1452 k2
= p
[2] <= rgba
[0][2];
1453 k3
= p
[3] <= rgba
[0][3];
1455 case PIPE_FUNC_GREATER
:
1456 k0
= p
[0] > rgba
[0][0];
1457 k1
= p
[1] > rgba
[0][1];
1458 k2
= p
[2] > rgba
[0][2];
1459 k3
= p
[3] > rgba
[0][3];
1461 case PIPE_FUNC_GEQUAL
:
1462 k0
= p
[0] >= rgba
[0][0];
1463 k1
= p
[1] >= rgba
[0][1];
1464 k2
= p
[2] >= rgba
[0][2];
1465 k3
= p
[3] >= rgba
[0][3];
1467 case PIPE_FUNC_EQUAL
:
1468 k0
= p
[0] == rgba
[0][0];
1469 k1
= p
[1] == rgba
[0][1];
1470 k2
= p
[2] == rgba
[0][2];
1471 k3
= p
[3] == rgba
[0][3];
1473 case PIPE_FUNC_NOTEQUAL
:
1474 k0
= p
[0] != rgba
[0][0];
1475 k1
= p
[1] != rgba
[0][1];
1476 k2
= p
[2] != rgba
[0][2];
1477 k3
= p
[3] != rgba
[0][3];
1479 case PIPE_FUNC_ALWAYS
:
1480 k0
= k1
= k2
= k3
= 1;
1482 case PIPE_FUNC_NEVER
:
1483 k0
= k1
= k2
= k3
= 0;
1486 k0
= k1
= k2
= k3
= 0;
1491 /* convert four pass/fail values to an intensity in [0,1] */
1492 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1494 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1495 for (j
= 0; j
< 4; j
++) {
1496 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1503 * Compute which cube face is referenced by each texcoord and put that
1504 * info into the sampler faces[] array. Then sample the cube faces
1507 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1508 const float s
[QUAD_SIZE
],
1509 const float t
[QUAD_SIZE
],
1510 const float p
[QUAD_SIZE
],
1512 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1514 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1516 float ssss
[4], tttt
[4];
1520 direction target sc tc ma
1521 ---------- ------------------------------- --- --- ---
1522 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1523 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1524 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1525 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1526 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1527 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1529 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1533 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1537 if (arx
>= ary
&& arx
>= arz
) {
1539 face
= PIPE_TEX_FACE_POS_X
;
1545 face
= PIPE_TEX_FACE_NEG_X
;
1551 else if (ary
>= arx
&& ary
>= arz
) {
1553 face
= PIPE_TEX_FACE_POS_Y
;
1559 face
= PIPE_TEX_FACE_NEG_Y
;
1567 face
= PIPE_TEX_FACE_POS_Z
;
1573 face
= PIPE_TEX_FACE_NEG_Z
;
1581 const float ima
= 1.0 / ma
;
1582 ssss
[j
] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1583 tttt
[j
] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1584 samp
->faces
[j
] = face
;
1588 /* In our little pipeline, the compare stage is next. If compare
1589 * is not active, this will point somewhere deeper into the
1590 * pipeline, eg. to mip_filter or even img_filter.
1592 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, lodbias
, rgba
);
1597 static wrap_nearest_func
1598 get_nearest_unorm_wrap(unsigned mode
)
1601 case PIPE_TEX_WRAP_CLAMP
:
1602 return wrap_nearest_unorm_clamp
;
1603 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1604 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1605 return wrap_nearest_unorm_clamp_to_border
;
1608 return wrap_nearest_unorm_clamp
;
1613 static wrap_nearest_func
1614 get_nearest_wrap(unsigned mode
)
1617 case PIPE_TEX_WRAP_REPEAT
:
1618 return wrap_nearest_repeat
;
1619 case PIPE_TEX_WRAP_CLAMP
:
1620 return wrap_nearest_clamp
;
1621 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1622 return wrap_nearest_clamp_to_edge
;
1623 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1624 return wrap_nearest_clamp_to_border
;
1625 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1626 return wrap_nearest_mirror_repeat
;
1627 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1628 return wrap_nearest_mirror_clamp
;
1629 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1630 return wrap_nearest_mirror_clamp_to_edge
;
1631 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1632 return wrap_nearest_mirror_clamp_to_border
;
1635 return wrap_nearest_repeat
;
1640 static wrap_linear_func
1641 get_linear_unorm_wrap(unsigned mode
)
1644 case PIPE_TEX_WRAP_CLAMP
:
1645 return wrap_linear_unorm_clamp
;
1646 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1647 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1648 return wrap_linear_unorm_clamp_to_border
;
1651 return wrap_linear_unorm_clamp
;
1656 static wrap_linear_func
1657 get_linear_wrap(unsigned mode
)
1660 case PIPE_TEX_WRAP_REPEAT
:
1661 return wrap_linear_repeat
;
1662 case PIPE_TEX_WRAP_CLAMP
:
1663 return wrap_linear_clamp
;
1664 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1665 return wrap_linear_clamp_to_edge
;
1666 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1667 return wrap_linear_clamp_to_border
;
1668 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1669 return wrap_linear_mirror_repeat
;
1670 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1671 return wrap_linear_mirror_clamp
;
1672 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1673 return wrap_linear_mirror_clamp_to_edge
;
1674 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1675 return wrap_linear_mirror_clamp_to_border
;
1678 return wrap_linear_repeat
;
1683 static compute_lambda_func
1684 get_lambda_func(const union sp_sampler_key key
)
1686 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1687 return compute_lambda_vert
;
1689 switch (key
.bits
.target
) {
1690 case PIPE_TEXTURE_1D
:
1691 return compute_lambda_1d
;
1692 case PIPE_TEXTURE_2D
:
1693 case PIPE_TEXTURE_CUBE
:
1694 return compute_lambda_2d
;
1695 case PIPE_TEXTURE_3D
:
1696 return compute_lambda_3d
;
1699 return compute_lambda_1d
;
1705 get_img_filter(const union sp_sampler_key key
,
1707 const struct pipe_sampler_state
*sampler
)
1709 switch (key
.bits
.target
) {
1710 case PIPE_TEXTURE_1D
:
1711 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1712 return img_filter_1d_nearest
;
1714 return img_filter_1d_linear
;
1716 case PIPE_TEXTURE_2D
:
1717 /* Try for fast path:
1719 if (key
.bits
.is_pot
&&
1720 sampler
->wrap_s
== sampler
->wrap_t
&&
1721 sampler
->normalized_coords
)
1723 switch (sampler
->wrap_s
) {
1724 case PIPE_TEX_WRAP_REPEAT
:
1726 case PIPE_TEX_FILTER_NEAREST
:
1727 return img_filter_2d_nearest_repeat_POT
;
1728 case PIPE_TEX_FILTER_LINEAR
:
1729 return img_filter_2d_linear_repeat_POT
;
1734 case PIPE_TEX_WRAP_CLAMP
:
1736 case PIPE_TEX_FILTER_NEAREST
:
1737 return img_filter_2d_nearest_clamp_POT
;
1743 /* Otherwise use default versions:
1745 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1746 return img_filter_2d_nearest
;
1748 return img_filter_2d_linear
;
1750 case PIPE_TEXTURE_CUBE
:
1751 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1752 return img_filter_cube_nearest
;
1754 return img_filter_cube_linear
;
1756 case PIPE_TEXTURE_3D
:
1757 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1758 return img_filter_3d_nearest
;
1760 return img_filter_3d_linear
;
1764 return img_filter_1d_nearest
;
1770 * Bind the given texture object and texture cache to the sampler varient.
1773 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1774 struct softpipe_tex_tile_cache
*tex_cache
,
1775 const struct pipe_texture
*texture
)
1777 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1779 samp
->texture
= texture
;
1780 samp
->cache
= tex_cache
;
1781 samp
->xpot
= util_unsigned_logbase2( texture
->width
[0] );
1782 samp
->ypot
= util_unsigned_logbase2( texture
->height
[0] );
1783 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1788 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1795 * Create a sampler varient for a given set of non-orthogonal state.
1797 struct sp_sampler_varient
*
1798 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1799 const union sp_sampler_key key
)
1801 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1805 samp
->sampler
= sampler
;
1808 /* Note that (for instance) linear_texcoord_s and
1809 * nearest_texcoord_s may be active at the same time, if the
1810 * sampler min_img_filter differs from its mag_img_filter.
1812 if (sampler
->normalized_coords
) {
1813 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1814 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1815 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1817 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1818 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1819 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1822 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1823 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1824 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1826 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1827 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1828 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1831 samp
->compute_lambda
= get_lambda_func( key
);
1833 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1834 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1836 switch (sampler
->min_mip_filter
) {
1837 case PIPE_TEX_MIPFILTER_NONE
:
1838 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1839 samp
->mip_filter
= samp
->min_img_filter
;
1841 samp
->mip_filter
= mip_filter_none
;
1844 case PIPE_TEX_MIPFILTER_NEAREST
:
1845 samp
->mip_filter
= mip_filter_nearest
;
1848 case PIPE_TEX_MIPFILTER_LINEAR
:
1849 if (key
.bits
.is_pot
&&
1850 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1851 sampler
->normalized_coords
&&
1852 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1853 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1854 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1856 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
1860 samp
->mip_filter
= mip_filter_linear
;
1865 if (sampler
->compare_mode
!= FALSE
) {
1866 samp
->compare
= sample_compare
;
1869 /* Skip compare operation by promoting the mip_filter function
1872 samp
->compare
= samp
->mip_filter
;
1875 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
1876 samp
->base
.get_samples
= sample_cube
;
1884 /* Skip cube face determination by promoting the compare
1887 samp
->base
.get_samples
= samp
->compare
;