1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008-2010 VMware, Inc. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "sp_quad.h" /* only for #define QUAD_* tokens */
43 #include "sp_tex_sample.h"
44 #include "sp_tex_tile_cache.h"
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.
64 * Linear interpolation macro
67 lerp(float a
, float v0
, float v1
)
69 return v0
+ a
* (v1
- v0
);
74 * Do 2D/bilinear 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 negative, coord % size doesn't give the right
109 * value. To avoid that problem we add a large multiple of the size
110 * (rather than using a conditional).
113 repeat(int coord
, unsigned size
)
115 return (coord
+ size
* 1024) % 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
]);
204 float u
= frac(s
[ch
]);
210 icoord
[ch
] = size
- 1;
212 icoord
[ch
] = util_ifloor(u
* size
);
218 wrap_nearest_mirror_clamp(const float s
[4], unsigned size
, int icoord
[4])
221 for (ch
= 0; ch
< 4; ch
++) {
222 /* s limited to [0,1] */
223 /* i limited to [0,size-1] */
224 const float u
= fabsf(s
[ch
]);
228 icoord
[ch
] = size
- 1;
230 icoord
[ch
] = util_ifloor(u
* size
);
236 wrap_nearest_mirror_clamp_to_edge(const float s
[4], unsigned size
,
240 /* s limited to [min,max] */
241 /* i limited to [0, size-1] */
242 const float min
= 1.0F
/ (2.0F
* size
);
243 const float max
= 1.0F
- min
;
244 for (ch
= 0; ch
< 4; ch
++) {
245 const float u
= fabsf(s
[ch
]);
249 icoord
[ch
] = size
- 1;
251 icoord
[ch
] = util_ifloor(u
* size
);
257 wrap_nearest_mirror_clamp_to_border(const float s
[4], unsigned size
,
261 /* s limited to [min,max] */
262 /* i limited to [0, size-1] */
263 const float min
= -1.0F
/ (2.0F
* size
);
264 const float max
= 1.0F
- min
;
265 for (ch
= 0; ch
< 4; ch
++) {
266 const float u
= fabsf(s
[ch
]);
272 icoord
[ch
] = util_ifloor(u
* size
);
278 * Used to compute texel locations for linear sampling for four texcoords.
279 * \param wrapMode PIPE_TEX_WRAP_x
280 * \param s the texcoords
281 * \param size the texture image size
282 * \param icoord0 returns first texture indexes
283 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
284 * \param w returns blend factor/weight between texture indexes
285 * \param icoord returns the computed integer texture coords
288 wrap_linear_repeat(const float s
[4], unsigned size
,
289 int icoord0
[4], int icoord1
[4], float w
[4])
292 for (ch
= 0; ch
< 4; ch
++) {
293 float u
= s
[ch
] * size
- 0.5F
;
294 icoord0
[ch
] = repeat(util_ifloor(u
), size
);
295 icoord1
[ch
] = repeat(icoord0
[ch
] + 1, size
);
302 wrap_linear_clamp(const float s
[4], unsigned size
,
303 int icoord0
[4], int icoord1
[4], float w
[4])
306 for (ch
= 0; ch
< 4; ch
++) {
307 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
309 icoord0
[ch
] = util_ifloor(u
);
310 icoord1
[ch
] = icoord0
[ch
] + 1;
317 wrap_linear_clamp_to_edge(const float s
[4], unsigned size
,
318 int icoord0
[4], int icoord1
[4], float w
[4])
321 for (ch
= 0; ch
< 4; ch
++) {
322 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
324 icoord0
[ch
] = util_ifloor(u
);
325 icoord1
[ch
] = icoord0
[ch
] + 1;
328 if (icoord1
[ch
] >= (int) size
)
329 icoord1
[ch
] = size
- 1;
336 wrap_linear_clamp_to_border(const float s
[4], unsigned size
,
337 int icoord0
[4], int icoord1
[4], float w
[4])
339 const float min
= -1.0F
/ (2.0F
* size
);
340 const float max
= 1.0F
- min
;
342 for (ch
= 0; ch
< 4; ch
++) {
343 float u
= CLAMP(s
[ch
], min
, max
);
345 icoord0
[ch
] = util_ifloor(u
);
346 icoord1
[ch
] = icoord0
[ch
] + 1;
353 wrap_linear_mirror_repeat(const float s
[4], unsigned size
,
354 int icoord0
[4], int icoord1
[4], float w
[4])
357 for (ch
= 0; ch
< 4; ch
++) {
358 const int flr
= util_ifloor(s
[ch
]);
359 float u
= frac(s
[ch
]);
363 icoord0
[ch
] = util_ifloor(u
);
364 icoord1
[ch
] = icoord0
[ch
] + 1;
367 if (icoord1
[ch
] >= (int) size
)
368 icoord1
[ch
] = size
- 1;
375 wrap_linear_mirror_clamp(const float s
[4], unsigned size
,
376 int icoord0
[4], int icoord1
[4], float w
[4])
379 for (ch
= 0; ch
< 4; ch
++) {
380 float u
= fabsf(s
[ch
]);
386 icoord0
[ch
] = util_ifloor(u
);
387 icoord1
[ch
] = icoord0
[ch
] + 1;
394 wrap_linear_mirror_clamp_to_edge(const float s
[4], unsigned size
,
395 int icoord0
[4], int icoord1
[4], float w
[4])
398 for (ch
= 0; ch
< 4; ch
++) {
399 float u
= fabsf(s
[ch
]);
405 icoord0
[ch
] = util_ifloor(u
);
406 icoord1
[ch
] = icoord0
[ch
] + 1;
409 if (icoord1
[ch
] >= (int) size
)
410 icoord1
[ch
] = size
- 1;
417 wrap_linear_mirror_clamp_to_border(const float s
[4], unsigned size
,
418 int icoord0
[4], int icoord1
[4], float w
[4])
420 const float min
= -1.0F
/ (2.0F
* size
);
421 const float max
= 1.0F
- min
;
423 for (ch
= 0; ch
< 4; ch
++) {
424 float u
= fabsf(s
[ch
]);
432 icoord0
[ch
] = util_ifloor(u
);
433 icoord1
[ch
] = icoord0
[ch
] + 1;
440 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
443 wrap_nearest_unorm_clamp(const float s
[4], unsigned size
, int icoord
[4])
446 for (ch
= 0; ch
< 4; ch
++) {
447 int i
= util_ifloor(s
[ch
]);
448 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
454 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
457 wrap_nearest_unorm_clamp_to_border(const float s
[4], unsigned size
,
461 for (ch
= 0; ch
< 4; ch
++) {
462 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], -0.5F
, (float) size
+ 0.5F
) );
468 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
471 wrap_nearest_unorm_clamp_to_edge(const float s
[4], unsigned size
,
475 for (ch
= 0; ch
< 4; ch
++) {
476 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
482 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
485 wrap_linear_unorm_clamp(const float s
[4], unsigned size
,
486 int icoord0
[4], int icoord1
[4], float w
[4])
489 for (ch
= 0; ch
< 4; ch
++) {
490 /* Not exactly what the spec says, but it matches NVIDIA output */
491 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
492 icoord0
[ch
] = util_ifloor(u
);
493 icoord1
[ch
] = icoord0
[ch
] + 1;
500 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
503 wrap_linear_unorm_clamp_to_border(const float s
[4], unsigned size
,
504 int icoord0
[4], int icoord1
[4], float w
[4])
507 for (ch
= 0; ch
< 4; ch
++) {
508 float u
= CLAMP(s
[ch
], -0.5F
, (float) size
+ 0.5F
);
510 icoord0
[ch
] = util_ifloor(u
);
511 icoord1
[ch
] = icoord0
[ch
] + 1;
512 if (icoord1
[ch
] > (int) size
- 1)
513 icoord1
[ch
] = size
- 1;
520 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
523 wrap_linear_unorm_clamp_to_edge(const float s
[4], unsigned size
,
524 int icoord0
[4], int icoord1
[4], float w
[4])
527 for (ch
= 0; ch
< 4; ch
++) {
528 float u
= CLAMP(s
[ch
], +0.5F
, (float) size
- 0.5F
);
530 icoord0
[ch
] = util_ifloor(u
);
531 icoord1
[ch
] = icoord0
[ch
] + 1;
532 if (icoord1
[ch
] > (int) size
- 1)
533 icoord1
[ch
] = size
- 1;
541 * Examine the quad's texture coordinates to compute the partial
542 * derivatives w.r.t X and Y, then compute lambda (level of detail).
545 compute_lambda_1d(const struct sp_sampler_varient
*samp
,
546 const float s
[QUAD_SIZE
],
547 const float t
[QUAD_SIZE
],
548 const float p
[QUAD_SIZE
])
550 const struct pipe_resource
*texture
= samp
->texture
;
551 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
552 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
553 float rho
= MAX2(dsdx
, dsdy
) * texture
->width0
;
555 return util_fast_log2(rho
);
560 compute_lambda_2d(const struct sp_sampler_varient
*samp
,
561 const float s
[QUAD_SIZE
],
562 const float t
[QUAD_SIZE
],
563 const float p
[QUAD_SIZE
])
565 const struct pipe_resource
*texture
= samp
->texture
;
566 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
567 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
568 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
569 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
570 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
571 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
572 float rho
= MAX2(maxx
, maxy
);
574 return util_fast_log2(rho
);
579 compute_lambda_3d(const struct sp_sampler_varient
*samp
,
580 const float s
[QUAD_SIZE
],
581 const float t
[QUAD_SIZE
],
582 const float p
[QUAD_SIZE
])
584 const struct pipe_resource
*texture
= samp
->texture
;
585 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
586 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
587 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
588 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
589 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
590 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
591 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
592 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
593 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth0
;
596 rho
= MAX2(maxx
, maxy
);
597 rho
= MAX2(rho
, maxz
);
599 return util_fast_log2(rho
);
604 * Compute lambda for a vertex texture sampler.
605 * Since there aren't derivatives to use, just return 0.
608 compute_lambda_vert(const struct sp_sampler_varient
*samp
,
609 const float s
[QUAD_SIZE
],
610 const float t
[QUAD_SIZE
],
611 const float p
[QUAD_SIZE
])
619 * Get a texel from a texture, using the texture tile cache.
621 * \param addr the template tex address containing cube, z, face info.
622 * \param x the x coord of texel within 2D image
623 * \param y the y coord of texel within 2D image
624 * \param rgba the quad to put the texel/color into
626 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
627 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
633 static INLINE
const float *
634 get_texel_2d_no_border(const struct sp_sampler_varient
*samp
,
635 union tex_tile_address addr
, int x
, int y
)
637 const struct softpipe_tex_cached_tile
*tile
;
639 addr
.bits
.x
= x
/ TILE_SIZE
;
640 addr
.bits
.y
= y
/ TILE_SIZE
;
644 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
646 return &tile
->data
.color
[y
][x
][0];
650 static INLINE
const float *
651 get_texel_2d(const struct sp_sampler_varient
*samp
,
652 union tex_tile_address addr
, int x
, int y
)
654 const struct pipe_resource
*texture
= samp
->texture
;
655 unsigned level
= addr
.bits
.level
;
657 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
658 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
659 return sp_tex_tile_cache_border_color(samp
->cache
,
660 samp
->sampler
->border_color
);
663 return get_texel_2d_no_border( samp
, addr
, x
, y
);
668 /* Gather a quad of adjacent texels within a tile:
671 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
672 union tex_tile_address addr
,
673 unsigned x
, unsigned y
,
676 const struct softpipe_tex_cached_tile
*tile
;
678 addr
.bits
.x
= x
/ TILE_SIZE
;
679 addr
.bits
.y
= y
/ TILE_SIZE
;
683 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
685 out
[0] = &tile
->data
.color
[y
][x
][0];
686 out
[1] = &tile
->data
.color
[y
][x
+1][0];
687 out
[2] = &tile
->data
.color
[y
+1][x
][0];
688 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
692 /* Gather a quad of potentially non-adjacent texels:
695 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
696 union tex_tile_address addr
,
701 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
702 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
703 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
704 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
707 /* Can involve a lot of unnecessary checks for border color:
710 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
711 union tex_tile_address addr
,
716 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
717 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
718 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
719 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
726 static INLINE
const float *
727 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
728 union tex_tile_address addr
, int x
, int y
, int z
)
730 const struct softpipe_tex_cached_tile
*tile
;
732 addr
.bits
.x
= x
/ TILE_SIZE
;
733 addr
.bits
.y
= y
/ TILE_SIZE
;
738 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
740 return &tile
->data
.color
[y
][x
][0];
744 static INLINE
const float *
745 get_texel_3d(const struct sp_sampler_varient
*samp
,
746 union tex_tile_address addr
, int x
, int y
, int z
)
748 const struct pipe_resource
*texture
= samp
->texture
;
749 unsigned level
= addr
.bits
.level
;
751 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
752 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
753 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
754 return sp_tex_tile_cache_border_color(samp
->cache
,
755 samp
->sampler
->border_color
);
758 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
764 * Given the logbase2 of a mipmap's base level size and a mipmap level,
765 * return the size (in texels) of that mipmap level.
766 * For example, if level[0].width = 256 then base_pot will be 8.
767 * If level = 2, then we'll return 64 (the width at level=2).
768 * Return 1 if level > base_pot.
770 static INLINE
unsigned
771 pot_level_size(unsigned base_pot
, unsigned level
)
773 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
777 /* Some image-filter fastpaths:
780 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
781 const float s
[QUAD_SIZE
],
782 const float t
[QUAD_SIZE
],
783 const float p
[QUAD_SIZE
],
784 const float c0
[QUAD_SIZE
],
785 enum tgsi_sampler_control control
,
786 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
788 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
790 unsigned level
= samp
->level
;
791 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
792 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
793 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
794 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
795 union tex_tile_address addr
;
798 addr
.bits
.level
= samp
->level
;
800 for (j
= 0; j
< QUAD_SIZE
; j
++) {
803 float u
= s
[j
] * xpot
- 0.5F
;
804 float v
= t
[j
] * ypot
- 0.5F
;
806 int uflr
= util_ifloor(u
);
807 int vflr
= util_ifloor(v
);
809 float xw
= u
- (float)uflr
;
810 float yw
= v
- (float)vflr
;
812 int x0
= uflr
& (xpot
- 1);
813 int y0
= vflr
& (ypot
- 1);
817 /* Can we fetch all four at once:
819 if (x0
< xmax
&& y0
< ymax
) {
820 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
823 unsigned x1
= (x0
+ 1) & (xpot
- 1);
824 unsigned y1
= (y0
+ 1) & (ypot
- 1);
825 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
828 /* interpolate R, G, B, A */
829 for (c
= 0; c
< 4; c
++) {
830 rgba
[c
][j
] = lerp_2d(xw
, yw
,
839 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
840 const float s
[QUAD_SIZE
],
841 const float t
[QUAD_SIZE
],
842 const float p
[QUAD_SIZE
],
843 const float c0
[QUAD_SIZE
],
844 enum tgsi_sampler_control control
,
845 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
847 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
849 unsigned level
= samp
->level
;
850 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
851 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
852 union tex_tile_address addr
;
855 addr
.bits
.level
= samp
->level
;
857 for (j
= 0; j
< QUAD_SIZE
; j
++) {
860 float u
= s
[j
] * xpot
;
861 float v
= t
[j
] * ypot
;
863 int uflr
= util_ifloor(u
);
864 int vflr
= util_ifloor(v
);
866 int x0
= uflr
& (xpot
- 1);
867 int y0
= vflr
& (ypot
- 1);
869 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
871 for (c
= 0; c
< 4; c
++) {
879 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
880 const float s
[QUAD_SIZE
],
881 const float t
[QUAD_SIZE
],
882 const float p
[QUAD_SIZE
],
883 const float c0
[QUAD_SIZE
],
884 enum tgsi_sampler_control control
,
885 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
887 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
889 unsigned level
= samp
->level
;
890 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
891 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
892 union tex_tile_address addr
;
895 addr
.bits
.level
= samp
->level
;
897 for (j
= 0; j
< QUAD_SIZE
; j
++) {
900 float u
= s
[j
] * xpot
;
901 float v
= t
[j
] * ypot
;
909 else if (x0
> xpot
- 1)
915 else if (y0
> ypot
- 1)
918 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
920 for (c
= 0; c
< 4; c
++) {
928 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
929 const float s
[QUAD_SIZE
],
930 const float t
[QUAD_SIZE
],
931 const float p
[QUAD_SIZE
],
932 const float c0
[QUAD_SIZE
],
933 enum tgsi_sampler_control control
,
934 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
936 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
937 const struct pipe_resource
*texture
= samp
->texture
;
941 union tex_tile_address addr
;
943 level0
= samp
->level
;
944 width
= u_minify(texture
->width0
, level0
);
949 addr
.bits
.level
= samp
->level
;
951 samp
->nearest_texcoord_s(s
, width
, x
);
953 for (j
= 0; j
< QUAD_SIZE
; j
++) {
954 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
956 for (c
= 0; c
< 4; c
++) {
964 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
965 const float s
[QUAD_SIZE
],
966 const float t
[QUAD_SIZE
],
967 const float p
[QUAD_SIZE
],
968 const float c0
[QUAD_SIZE
],
969 enum tgsi_sampler_control control
,
970 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
972 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
973 const struct pipe_resource
*texture
= samp
->texture
;
977 union tex_tile_address addr
;
980 level0
= samp
->level
;
981 width
= u_minify(texture
->width0
, level0
);
982 height
= u_minify(texture
->height0
, level0
);
988 addr
.bits
.level
= samp
->level
;
990 samp
->nearest_texcoord_s(s
, width
, x
);
991 samp
->nearest_texcoord_t(t
, height
, y
);
993 for (j
= 0; j
< QUAD_SIZE
; j
++) {
994 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
996 for (c
= 0; c
< 4; c
++) {
1003 static INLINE
union tex_tile_address
1004 face(union tex_tile_address addr
, unsigned face
)
1006 addr
.bits
.face
= face
;
1012 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1013 const float s
[QUAD_SIZE
],
1014 const float t
[QUAD_SIZE
],
1015 const float p
[QUAD_SIZE
],
1016 const float c0
[QUAD_SIZE
],
1017 enum tgsi_sampler_control control
,
1018 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1020 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1021 const struct pipe_resource
*texture
= samp
->texture
;
1022 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1026 union tex_tile_address addr
;
1028 level0
= samp
->level
;
1029 width
= u_minify(texture
->width0
, level0
);
1030 height
= u_minify(texture
->height0
, level0
);
1036 addr
.bits
.level
= samp
->level
;
1038 samp
->nearest_texcoord_s(s
, width
, x
);
1039 samp
->nearest_texcoord_t(t
, height
, y
);
1041 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1042 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1044 for (c
= 0; c
< 4; c
++) {
1045 rgba
[c
][j
] = out
[c
];
1052 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1053 const float s
[QUAD_SIZE
],
1054 const float t
[QUAD_SIZE
],
1055 const float p
[QUAD_SIZE
],
1056 const float c0
[QUAD_SIZE
],
1057 enum tgsi_sampler_control control
,
1058 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1060 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1061 const struct pipe_resource
*texture
= samp
->texture
;
1063 int width
, height
, depth
;
1064 int x
[4], y
[4], z
[4];
1065 union tex_tile_address addr
;
1067 level0
= samp
->level
;
1068 width
= u_minify(texture
->width0
, level0
);
1069 height
= u_minify(texture
->height0
, level0
);
1070 depth
= u_minify(texture
->depth0
, level0
);
1076 samp
->nearest_texcoord_s(s
, width
, x
);
1077 samp
->nearest_texcoord_t(t
, height
, y
);
1078 samp
->nearest_texcoord_p(p
, depth
, z
);
1081 addr
.bits
.level
= samp
->level
;
1083 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1084 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1086 for (c
= 0; c
< 4; c
++) {
1087 rgba
[c
][j
] = out
[c
];
1094 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1095 const float s
[QUAD_SIZE
],
1096 const float t
[QUAD_SIZE
],
1097 const float p
[QUAD_SIZE
],
1098 const float c0
[QUAD_SIZE
],
1099 enum tgsi_sampler_control control
,
1100 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1102 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1103 const struct pipe_resource
*texture
= samp
->texture
;
1107 float xw
[4]; /* weights */
1108 union tex_tile_address addr
;
1110 level0
= samp
->level
;
1111 width
= u_minify(texture
->width0
, level0
);
1116 addr
.bits
.level
= samp
->level
;
1118 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1120 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1121 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1122 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1125 /* interpolate R, G, B, A */
1126 for (c
= 0; c
< 4; c
++) {
1127 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1134 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1135 const float s
[QUAD_SIZE
],
1136 const float t
[QUAD_SIZE
],
1137 const float p
[QUAD_SIZE
],
1138 const float c0
[QUAD_SIZE
],
1139 enum tgsi_sampler_control control
,
1140 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1142 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1143 const struct pipe_resource
*texture
= samp
->texture
;
1146 int x0
[4], y0
[4], x1
[4], y1
[4];
1147 float xw
[4], yw
[4]; /* weights */
1148 union tex_tile_address addr
;
1150 level0
= samp
->level
;
1151 width
= u_minify(texture
->width0
, level0
);
1152 height
= u_minify(texture
->height0
, level0
);
1158 addr
.bits
.level
= samp
->level
;
1160 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1161 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1163 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1164 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1165 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1166 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1167 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1170 /* interpolate R, G, B, A */
1171 for (c
= 0; c
< 4; c
++) {
1172 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1181 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1182 const float s
[QUAD_SIZE
],
1183 const float t
[QUAD_SIZE
],
1184 const float p
[QUAD_SIZE
],
1185 const float c0
[QUAD_SIZE
],
1186 enum tgsi_sampler_control control
,
1187 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1189 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1190 const struct pipe_resource
*texture
= samp
->texture
;
1191 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1194 int x0
[4], y0
[4], x1
[4], y1
[4];
1195 float xw
[4], yw
[4]; /* weights */
1196 union tex_tile_address addr
;
1198 level0
= samp
->level
;
1199 width
= u_minify(texture
->width0
, level0
);
1200 height
= u_minify(texture
->height0
, level0
);
1206 addr
.bits
.level
= samp
->level
;
1208 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1209 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1211 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1212 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1213 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1214 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1215 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1216 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1219 /* interpolate R, G, B, A */
1220 for (c
= 0; c
< 4; c
++) {
1221 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1230 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1231 const float s
[QUAD_SIZE
],
1232 const float t
[QUAD_SIZE
],
1233 const float p
[QUAD_SIZE
],
1234 const float c0
[QUAD_SIZE
],
1235 enum tgsi_sampler_control control
,
1236 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1238 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1239 const struct pipe_resource
*texture
= samp
->texture
;
1241 int width
, height
, depth
;
1242 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1243 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1244 union tex_tile_address addr
;
1246 level0
= samp
->level
;
1247 width
= u_minify(texture
->width0
, level0
);
1248 height
= u_minify(texture
->height0
, level0
);
1249 depth
= u_minify(texture
->depth0
, level0
);
1252 addr
.bits
.level
= level0
;
1258 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1259 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1260 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1262 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1265 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1266 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1267 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1268 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1270 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1271 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1272 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1273 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1275 /* interpolate R, G, B, A */
1276 for (c
= 0; c
< 4; c
++) {
1277 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1287 /* Calculate level of detail for every fragment.
1288 * Note that lambda has already been biased by global LOD bias.
1291 compute_lod(const struct pipe_sampler_state
*sampler
,
1292 const float biased_lambda
,
1293 const float lodbias
[QUAD_SIZE
],
1294 float lod
[QUAD_SIZE
])
1298 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1299 lod
[i
] = biased_lambda
+ lodbias
[i
];
1300 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1306 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1307 const float s
[QUAD_SIZE
],
1308 const float t
[QUAD_SIZE
],
1309 const float p
[QUAD_SIZE
],
1310 const float c0
[QUAD_SIZE
],
1311 enum tgsi_sampler_control control
,
1312 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1314 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1315 const struct pipe_resource
*texture
= samp
->texture
;
1318 float lod
[QUAD_SIZE
];
1320 if (control
== tgsi_sampler_lod_bias
) {
1321 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1322 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1324 assert(control
== tgsi_sampler_lod_explicit
);
1326 memcpy(lod
, c0
, sizeof(lod
));
1329 /* XXX: Take into account all lod values.
1332 level0
= (int)lambda
;
1336 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1338 else if (level0
>= texture
->last_level
) {
1339 samp
->level
= texture
->last_level
;
1340 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1343 float levelBlend
= lambda
- level0
;
1348 samp
->level
= level0
;
1349 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1351 samp
->level
= level0
+1;
1352 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1354 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1355 for (c
= 0; c
< 4; c
++) {
1356 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1364 * Compute nearest mipmap level from texcoords.
1365 * Then sample the texture level for four elements of a quad.
1366 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1369 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1370 const float s
[QUAD_SIZE
],
1371 const float t
[QUAD_SIZE
],
1372 const float p
[QUAD_SIZE
],
1373 const float c0
[QUAD_SIZE
],
1374 enum tgsi_sampler_control control
,
1375 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1377 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1378 const struct pipe_resource
*texture
= samp
->texture
;
1380 float lod
[QUAD_SIZE
];
1382 if (control
== tgsi_sampler_lod_bias
) {
1383 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1384 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1386 assert(control
== tgsi_sampler_lod_explicit
);
1388 memcpy(lod
, c0
, sizeof(lod
));
1391 /* XXX: Take into account all lod values.
1397 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1400 samp
->level
= (int)(lambda
+ 0.5) ;
1401 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1402 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1406 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1407 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1408 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1409 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1410 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1416 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1417 const float s
[QUAD_SIZE
],
1418 const float t
[QUAD_SIZE
],
1419 const float p
[QUAD_SIZE
],
1420 const float c0
[QUAD_SIZE
],
1421 enum tgsi_sampler_control control
,
1422 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1424 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1426 float lod
[QUAD_SIZE
];
1428 if (control
== tgsi_sampler_lod_bias
) {
1429 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1430 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1432 assert(control
== tgsi_sampler_lod_explicit
);
1434 memcpy(lod
, c0
, sizeof(lod
));
1437 /* XXX: Take into account all lod values.
1442 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1445 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1452 * Specialized version of mip_filter_linear with hard-wired calls to
1453 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1456 mip_filter_linear_2d_linear_repeat_POT(
1457 struct tgsi_sampler
*tgsi_sampler
,
1458 const float s
[QUAD_SIZE
],
1459 const float t
[QUAD_SIZE
],
1460 const float p
[QUAD_SIZE
],
1461 const float c0
[QUAD_SIZE
],
1462 enum tgsi_sampler_control control
,
1463 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1465 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1466 const struct pipe_resource
*texture
= samp
->texture
;
1469 float lod
[QUAD_SIZE
];
1471 if (control
== tgsi_sampler_lod_bias
) {
1472 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1473 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1475 assert(control
== tgsi_sampler_lod_explicit
);
1477 memcpy(lod
, c0
, sizeof(lod
));
1480 /* XXX: Take into account all lod values.
1483 level0
= (int)lambda
;
1485 /* Catches both negative and large values of level0:
1487 if ((unsigned)level0
>= texture
->last_level
) {
1491 samp
->level
= texture
->last_level
;
1493 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1496 float levelBlend
= lambda
- level0
;
1501 samp
->level
= level0
;
1502 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1504 samp
->level
= level0
+1;
1505 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1507 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1508 for (c
= 0; c
< 4; c
++) {
1509 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1518 * Do shadow/depth comparisons.
1521 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1522 const float s
[QUAD_SIZE
],
1523 const float t
[QUAD_SIZE
],
1524 const float p
[QUAD_SIZE
],
1525 const float c0
[QUAD_SIZE
],
1526 enum tgsi_sampler_control control
,
1527 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1529 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1530 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1531 int j
, k0
, k1
, k2
, k3
;
1534 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1537 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1538 * When we sampled the depth texture, the depth value was put into all
1539 * RGBA channels. We look at the red channel here.
1542 /* compare four texcoords vs. four texture samples */
1543 switch (sampler
->compare_func
) {
1544 case PIPE_FUNC_LESS
:
1545 k0
= p
[0] < rgba
[0][0];
1546 k1
= p
[1] < rgba
[0][1];
1547 k2
= p
[2] < rgba
[0][2];
1548 k3
= p
[3] < rgba
[0][3];
1550 case PIPE_FUNC_LEQUAL
:
1551 k0
= p
[0] <= rgba
[0][0];
1552 k1
= p
[1] <= rgba
[0][1];
1553 k2
= p
[2] <= rgba
[0][2];
1554 k3
= p
[3] <= rgba
[0][3];
1556 case PIPE_FUNC_GREATER
:
1557 k0
= p
[0] > rgba
[0][0];
1558 k1
= p
[1] > rgba
[0][1];
1559 k2
= p
[2] > rgba
[0][2];
1560 k3
= p
[3] > rgba
[0][3];
1562 case PIPE_FUNC_GEQUAL
:
1563 k0
= p
[0] >= rgba
[0][0];
1564 k1
= p
[1] >= rgba
[0][1];
1565 k2
= p
[2] >= rgba
[0][2];
1566 k3
= p
[3] >= rgba
[0][3];
1568 case PIPE_FUNC_EQUAL
:
1569 k0
= p
[0] == rgba
[0][0];
1570 k1
= p
[1] == rgba
[0][1];
1571 k2
= p
[2] == rgba
[0][2];
1572 k3
= p
[3] == rgba
[0][3];
1574 case PIPE_FUNC_NOTEQUAL
:
1575 k0
= p
[0] != rgba
[0][0];
1576 k1
= p
[1] != rgba
[0][1];
1577 k2
= p
[2] != rgba
[0][2];
1578 k3
= p
[3] != rgba
[0][3];
1580 case PIPE_FUNC_ALWAYS
:
1581 k0
= k1
= k2
= k3
= 1;
1583 case PIPE_FUNC_NEVER
:
1584 k0
= k1
= k2
= k3
= 0;
1587 k0
= k1
= k2
= k3
= 0;
1592 /* convert four pass/fail values to an intensity in [0,1] */
1593 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1595 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1596 for (j
= 0; j
< 4; j
++) {
1597 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1604 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1605 * Put face info into the sampler faces[] array.
1608 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1609 const float s
[QUAD_SIZE
],
1610 const float t
[QUAD_SIZE
],
1611 const float p
[QUAD_SIZE
],
1612 const float c0
[QUAD_SIZE
],
1613 enum tgsi_sampler_control control
,
1614 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1616 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1618 float ssss
[4], tttt
[4];
1622 direction target sc tc ma
1623 ---------- ------------------------------- --- --- ---
1624 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1625 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1626 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1627 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1628 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1629 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1632 /* Choose the cube face and compute new s/t coords for the 2D face.
1634 * Use the same cube face for all four pixels in the quad.
1636 * This isn't ideal, but if we want to use a different cube face
1637 * per pixel in the quad, we'd have to also compute the per-face
1638 * LOD here too. That's because the four post-face-selection
1639 * texcoords are no longer related to each other (they're
1640 * per-face!) so we can't use subtraction to compute the partial
1641 * deriviates to compute the LOD. Doing so (near cube edges
1642 * anyway) gives us pretty much random values.
1645 /* use the average of the four pixel's texcoords to choose the face */
1646 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1647 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1648 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1649 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1651 if (arx
>= ary
&& arx
>= arz
) {
1652 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
1653 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
1654 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1655 const float ima
= -0.5F
/ fabsf(s
[j
]);
1656 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
1657 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1658 samp
->faces
[j
] = face
;
1661 else if (ary
>= arx
&& ary
>= arz
) {
1662 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
1663 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
1664 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1665 const float ima
= -0.5F
/ fabsf(t
[j
]);
1666 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
1667 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
1668 samp
->faces
[j
] = face
;
1672 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
1673 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
1674 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1675 const float ima
= -0.5 / fabsf(p
[j
]);
1676 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
1677 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1678 samp
->faces
[j
] = face
;
1683 /* In our little pipeline, the compare stage is next. If compare
1684 * is not active, this will point somewhere deeper into the
1685 * pipeline, eg. to mip_filter or even img_filter.
1687 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1692 static wrap_nearest_func
1693 get_nearest_unorm_wrap(unsigned mode
)
1696 case PIPE_TEX_WRAP_CLAMP
:
1697 return wrap_nearest_unorm_clamp
;
1698 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1699 return wrap_nearest_unorm_clamp_to_edge
;
1700 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1701 return wrap_nearest_unorm_clamp_to_border
;
1704 return wrap_nearest_unorm_clamp
;
1709 static wrap_nearest_func
1710 get_nearest_wrap(unsigned mode
)
1713 case PIPE_TEX_WRAP_REPEAT
:
1714 return wrap_nearest_repeat
;
1715 case PIPE_TEX_WRAP_CLAMP
:
1716 return wrap_nearest_clamp
;
1717 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1718 return wrap_nearest_clamp_to_edge
;
1719 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1720 return wrap_nearest_clamp_to_border
;
1721 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1722 return wrap_nearest_mirror_repeat
;
1723 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1724 return wrap_nearest_mirror_clamp
;
1725 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1726 return wrap_nearest_mirror_clamp_to_edge
;
1727 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1728 return wrap_nearest_mirror_clamp_to_border
;
1731 return wrap_nearest_repeat
;
1736 static wrap_linear_func
1737 get_linear_unorm_wrap(unsigned mode
)
1740 case PIPE_TEX_WRAP_CLAMP
:
1741 return wrap_linear_unorm_clamp
;
1742 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1743 return wrap_linear_unorm_clamp_to_edge
;
1744 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1745 return wrap_linear_unorm_clamp_to_border
;
1748 return wrap_linear_unorm_clamp
;
1753 static wrap_linear_func
1754 get_linear_wrap(unsigned mode
)
1757 case PIPE_TEX_WRAP_REPEAT
:
1758 return wrap_linear_repeat
;
1759 case PIPE_TEX_WRAP_CLAMP
:
1760 return wrap_linear_clamp
;
1761 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1762 return wrap_linear_clamp_to_edge
;
1763 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1764 return wrap_linear_clamp_to_border
;
1765 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1766 return wrap_linear_mirror_repeat
;
1767 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1768 return wrap_linear_mirror_clamp
;
1769 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1770 return wrap_linear_mirror_clamp_to_edge
;
1771 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1772 return wrap_linear_mirror_clamp_to_border
;
1775 return wrap_linear_repeat
;
1780 static compute_lambda_func
1781 get_lambda_func(const union sp_sampler_key key
)
1783 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1784 return compute_lambda_vert
;
1786 switch (key
.bits
.target
) {
1787 case PIPE_TEXTURE_1D
:
1788 return compute_lambda_1d
;
1789 case PIPE_TEXTURE_2D
:
1790 case PIPE_TEXTURE_RECT
:
1791 case PIPE_TEXTURE_CUBE
:
1792 return compute_lambda_2d
;
1793 case PIPE_TEXTURE_3D
:
1794 return compute_lambda_3d
;
1797 return compute_lambda_1d
;
1803 get_img_filter(const union sp_sampler_key key
,
1805 const struct pipe_sampler_state
*sampler
)
1807 switch (key
.bits
.target
) {
1808 case PIPE_TEXTURE_1D
:
1809 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1810 return img_filter_1d_nearest
;
1812 return img_filter_1d_linear
;
1814 case PIPE_TEXTURE_2D
:
1815 case PIPE_TEXTURE_RECT
:
1816 /* Try for fast path:
1818 if (key
.bits
.is_pot
&&
1819 sampler
->wrap_s
== sampler
->wrap_t
&&
1820 sampler
->normalized_coords
)
1822 switch (sampler
->wrap_s
) {
1823 case PIPE_TEX_WRAP_REPEAT
:
1825 case PIPE_TEX_FILTER_NEAREST
:
1826 return img_filter_2d_nearest_repeat_POT
;
1827 case PIPE_TEX_FILTER_LINEAR
:
1828 return img_filter_2d_linear_repeat_POT
;
1833 case PIPE_TEX_WRAP_CLAMP
:
1835 case PIPE_TEX_FILTER_NEAREST
:
1836 return img_filter_2d_nearest_clamp_POT
;
1842 /* Otherwise use default versions:
1844 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1845 return img_filter_2d_nearest
;
1847 return img_filter_2d_linear
;
1849 case PIPE_TEXTURE_CUBE
:
1850 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1851 return img_filter_cube_nearest
;
1853 return img_filter_cube_linear
;
1855 case PIPE_TEXTURE_3D
:
1856 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1857 return img_filter_3d_nearest
;
1859 return img_filter_3d_linear
;
1863 return img_filter_1d_nearest
;
1869 * Bind the given texture object and texture cache to the sampler varient.
1872 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1873 struct softpipe_tex_tile_cache
*tex_cache
,
1874 const struct pipe_resource
*texture
)
1876 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1878 samp
->texture
= texture
;
1879 samp
->cache
= tex_cache
;
1880 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
1881 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
1882 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1887 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1894 * Create a sampler varient for a given set of non-orthogonal state.
1896 struct sp_sampler_varient
*
1897 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1898 const union sp_sampler_key key
)
1900 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1904 samp
->sampler
= sampler
;
1907 /* Note that (for instance) linear_texcoord_s and
1908 * nearest_texcoord_s may be active at the same time, if the
1909 * sampler min_img_filter differs from its mag_img_filter.
1911 if (sampler
->normalized_coords
) {
1912 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1913 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1914 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1916 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1917 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1918 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1921 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1922 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1923 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1925 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1926 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1927 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1930 samp
->compute_lambda
= get_lambda_func( key
);
1932 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1933 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1935 switch (sampler
->min_mip_filter
) {
1936 case PIPE_TEX_MIPFILTER_NONE
:
1937 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1938 samp
->mip_filter
= samp
->min_img_filter
;
1940 samp
->mip_filter
= mip_filter_none
;
1943 case PIPE_TEX_MIPFILTER_NEAREST
:
1944 samp
->mip_filter
= mip_filter_nearest
;
1947 case PIPE_TEX_MIPFILTER_LINEAR
:
1948 if (key
.bits
.is_pot
&&
1949 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1950 sampler
->normalized_coords
&&
1951 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1952 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1953 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1955 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
1959 samp
->mip_filter
= mip_filter_linear
;
1964 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
1965 samp
->compare
= sample_compare
;
1968 /* Skip compare operation by promoting the mip_filter function
1971 samp
->compare
= samp
->mip_filter
;
1974 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
1975 samp
->base
.get_samples
= sample_cube
;
1983 /* Skip cube face determination by promoting the compare
1986 samp
->base
.get_samples
= samp
->compare
;