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/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
]);
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 * For RECT textures / unnormalized texcoords
441 * Only a subset of wrap modes supported.
444 wrap_nearest_unorm_clamp(const float s
[4], unsigned size
, int icoord
[4])
447 for (ch
= 0; ch
< 4; ch
++) {
448 int i
= util_ifloor(s
[ch
]);
449 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
455 * Handles clamp_to_edge and clamp_to_border:
458 wrap_nearest_unorm_clamp_to_border(const float s
[4], unsigned size
,
462 for (ch
= 0; ch
< 4; ch
++) {
463 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
469 * For RECT textures / unnormalized texcoords.
470 * Only a subset of wrap modes supported.
473 wrap_linear_unorm_clamp(const float s
[4], unsigned size
,
474 int icoord0
[4], int icoord1
[4], float w
[4])
477 for (ch
= 0; ch
< 4; ch
++) {
478 /* Not exactly what the spec says, but it matches NVIDIA output */
479 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
480 icoord0
[ch
] = util_ifloor(u
);
481 icoord1
[ch
] = icoord0
[ch
] + 1;
488 wrap_linear_unorm_clamp_to_border(const float s
[4], unsigned size
,
489 int icoord0
[4], int icoord1
[4], float w
[4])
492 for (ch
= 0; ch
< 4; ch
++) {
493 float u
= CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
);
495 icoord0
[ch
] = util_ifloor(u
);
496 icoord1
[ch
] = icoord0
[ch
] + 1;
497 if (icoord1
[ch
] > (int) size
- 1)
498 icoord1
[ch
] = size
- 1;
506 * Examine the quad's texture coordinates to compute the partial
507 * derivatives w.r.t X and Y, then compute lambda (level of detail).
510 compute_lambda_1d(const struct sp_sampler_varient
*samp
,
511 const float s
[QUAD_SIZE
],
512 const float t
[QUAD_SIZE
],
513 const float p
[QUAD_SIZE
])
515 const struct pipe_texture
*texture
= samp
->texture
;
516 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
517 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
518 float rho
= MAX2(dsdx
, dsdy
) * texture
->width0
;
520 return util_fast_log2(rho
);
525 compute_lambda_2d(const struct sp_sampler_varient
*samp
,
526 const float s
[QUAD_SIZE
],
527 const float t
[QUAD_SIZE
],
528 const float p
[QUAD_SIZE
])
530 const struct pipe_texture
*texture
= samp
->texture
;
531 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
532 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
533 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
534 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
535 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
536 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
537 float rho
= MAX2(maxx
, maxy
);
539 return util_fast_log2(rho
);
544 compute_lambda_3d(const struct sp_sampler_varient
*samp
,
545 const float s
[QUAD_SIZE
],
546 const float t
[QUAD_SIZE
],
547 const float p
[QUAD_SIZE
])
549 const struct pipe_texture
*texture
= samp
->texture
;
550 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
551 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
552 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
553 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
554 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
555 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
556 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width0
;
557 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height0
;
558 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth0
;
561 rho
= MAX2(maxx
, maxy
);
562 rho
= MAX2(rho
, maxz
);
564 return util_fast_log2(rho
);
569 * Compute lambda for a vertex texture sampler.
570 * Since there aren't derivatives to use, just return 0.
573 compute_lambda_vert(const struct sp_sampler_varient
*samp
,
574 const float s
[QUAD_SIZE
],
575 const float t
[QUAD_SIZE
],
576 const float p
[QUAD_SIZE
])
584 * Get a texel from a texture, using the texture tile cache.
586 * \param addr the template tex address containing cube, z, face info.
587 * \param x the x coord of texel within 2D image
588 * \param y the y coord of texel within 2D image
589 * \param rgba the quad to put the texel/color into
591 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
592 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
598 static INLINE
const float *
599 get_texel_2d_no_border(const struct sp_sampler_varient
*samp
,
600 union tex_tile_address addr
, int x
, int y
)
602 const struct softpipe_tex_cached_tile
*tile
;
604 addr
.bits
.x
= x
/ TILE_SIZE
;
605 addr
.bits
.y
= y
/ TILE_SIZE
;
609 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
611 return &tile
->data
.color
[y
][x
][0];
615 static INLINE
const float *
616 get_texel_2d(const struct sp_sampler_varient
*samp
,
617 union tex_tile_address addr
, int x
, int y
)
619 const struct pipe_texture
*texture
= samp
->texture
;
620 unsigned level
= addr
.bits
.level
;
622 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
623 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
624 return samp
->sampler
->border_color
;
627 return get_texel_2d_no_border( samp
, addr
, x
, y
);
632 /* Gather a quad of adjacent texels within a tile:
635 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
636 union tex_tile_address addr
,
637 unsigned x
, unsigned y
,
640 const struct softpipe_tex_cached_tile
*tile
;
642 addr
.bits
.x
= x
/ TILE_SIZE
;
643 addr
.bits
.y
= y
/ TILE_SIZE
;
647 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
649 out
[0] = &tile
->data
.color
[y
][x
][0];
650 out
[1] = &tile
->data
.color
[y
][x
+1][0];
651 out
[2] = &tile
->data
.color
[y
+1][x
][0];
652 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
656 /* Gather a quad of potentially non-adjacent texels:
659 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
660 union tex_tile_address addr
,
665 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
666 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
667 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
668 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
671 /* Can involve a lot of unnecessary checks for border color:
674 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
675 union tex_tile_address addr
,
680 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
681 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
682 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
683 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
690 static INLINE
const float *
691 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
692 union tex_tile_address addr
, int x
, int y
, int z
)
694 const struct softpipe_tex_cached_tile
*tile
;
696 addr
.bits
.x
= x
/ TILE_SIZE
;
697 addr
.bits
.y
= y
/ TILE_SIZE
;
702 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
704 return &tile
->data
.color
[y
][x
][0];
708 static INLINE
const float *
709 get_texel_3d(const struct sp_sampler_varient
*samp
,
710 union tex_tile_address addr
, int x
, int y
, int z
)
712 const struct pipe_texture
*texture
= samp
->texture
;
713 unsigned level
= addr
.bits
.level
;
715 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
716 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
717 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
718 return samp
->sampler
->border_color
;
721 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
727 * Given the logbase2 of a mipmap's base level size and a mipmap level,
728 * return the size (in texels) of that mipmap level.
729 * For example, if level[0].width = 256 then base_pot will be 8.
730 * If level = 2, then we'll return 64 (the width at level=2).
731 * Return 1 if level > base_pot.
733 static INLINE
unsigned
734 pot_level_size(unsigned base_pot
, unsigned level
)
736 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
740 /* Some image-filter fastpaths:
743 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
744 const float s
[QUAD_SIZE
],
745 const float t
[QUAD_SIZE
],
746 const float p
[QUAD_SIZE
],
747 const float c0
[QUAD_SIZE
],
748 enum tgsi_sampler_control control
,
749 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
751 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
753 unsigned level
= samp
->level
;
754 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
755 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
756 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
757 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
758 union tex_tile_address addr
;
761 addr
.bits
.level
= samp
->level
;
763 for (j
= 0; j
< QUAD_SIZE
; j
++) {
766 float u
= s
[j
] * xpot
- 0.5F
;
767 float v
= t
[j
] * ypot
- 0.5F
;
769 int uflr
= util_ifloor(u
);
770 int vflr
= util_ifloor(v
);
772 float xw
= u
- (float)uflr
;
773 float yw
= v
- (float)vflr
;
775 int x0
= uflr
& (xpot
- 1);
776 int y0
= vflr
& (ypot
- 1);
780 /* Can we fetch all four at once:
782 if (x0
< xmax
&& y0
< ymax
) {
783 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
786 unsigned x1
= (x0
+ 1) & (xpot
- 1);
787 unsigned y1
= (y0
+ 1) & (ypot
- 1);
788 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
791 /* interpolate R, G, B, A */
792 for (c
= 0; c
< 4; c
++) {
793 rgba
[c
][j
] = lerp_2d(xw
, yw
,
802 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
803 const float s
[QUAD_SIZE
],
804 const float t
[QUAD_SIZE
],
805 const float p
[QUAD_SIZE
],
806 const float c0
[QUAD_SIZE
],
807 enum tgsi_sampler_control control
,
808 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
810 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
812 unsigned level
= samp
->level
;
813 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
814 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
815 union tex_tile_address addr
;
818 addr
.bits
.level
= samp
->level
;
820 for (j
= 0; j
< QUAD_SIZE
; j
++) {
823 float u
= s
[j
] * xpot
;
824 float v
= t
[j
] * ypot
;
826 int uflr
= util_ifloor(u
);
827 int vflr
= util_ifloor(v
);
829 int x0
= uflr
& (xpot
- 1);
830 int y0
= vflr
& (ypot
- 1);
832 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
834 for (c
= 0; c
< 4; c
++) {
842 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
843 const float s
[QUAD_SIZE
],
844 const float t
[QUAD_SIZE
],
845 const float p
[QUAD_SIZE
],
846 const float c0
[QUAD_SIZE
],
847 enum tgsi_sampler_control control
,
848 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
850 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
852 unsigned level
= samp
->level
;
853 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
854 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
855 union tex_tile_address addr
;
858 addr
.bits
.level
= samp
->level
;
860 for (j
= 0; j
< QUAD_SIZE
; j
++) {
863 float u
= s
[j
] * xpot
;
864 float v
= t
[j
] * ypot
;
872 else if (x0
> xpot
- 1)
878 else if (y0
> ypot
- 1)
881 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
883 for (c
= 0; c
< 4; c
++) {
891 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
892 const float s
[QUAD_SIZE
],
893 const float t
[QUAD_SIZE
],
894 const float p
[QUAD_SIZE
],
895 const float c0
[QUAD_SIZE
],
896 enum tgsi_sampler_control control
,
897 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
899 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
900 const struct pipe_texture
*texture
= samp
->texture
;
904 union tex_tile_address addr
;
906 level0
= samp
->level
;
907 width
= u_minify(texture
->width0
, level0
);
912 addr
.bits
.level
= samp
->level
;
914 samp
->nearest_texcoord_s(s
, width
, x
);
916 for (j
= 0; j
< QUAD_SIZE
; j
++) {
917 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
919 for (c
= 0; c
< 4; c
++) {
927 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
928 const float s
[QUAD_SIZE
],
929 const float t
[QUAD_SIZE
],
930 const float p
[QUAD_SIZE
],
931 const float c0
[QUAD_SIZE
],
932 enum tgsi_sampler_control control
,
933 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
935 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
936 const struct pipe_texture
*texture
= samp
->texture
;
940 union tex_tile_address addr
;
943 level0
= samp
->level
;
944 width
= u_minify(texture
->width0
, level0
);
945 height
= u_minify(texture
->height0
, level0
);
951 addr
.bits
.level
= samp
->level
;
953 samp
->nearest_texcoord_s(s
, width
, x
);
954 samp
->nearest_texcoord_t(t
, height
, y
);
956 for (j
= 0; j
< QUAD_SIZE
; j
++) {
957 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
959 for (c
= 0; c
< 4; c
++) {
966 static INLINE
union tex_tile_address
967 face(union tex_tile_address addr
, unsigned face
)
969 addr
.bits
.face
= face
;
975 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
976 const float s
[QUAD_SIZE
],
977 const float t
[QUAD_SIZE
],
978 const float p
[QUAD_SIZE
],
979 const float c0
[QUAD_SIZE
],
980 enum tgsi_sampler_control control
,
981 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
983 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
984 const struct pipe_texture
*texture
= samp
->texture
;
985 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
989 union tex_tile_address addr
;
991 level0
= samp
->level
;
992 width
= u_minify(texture
->width0
, level0
);
993 height
= u_minify(texture
->height0
, level0
);
999 addr
.bits
.level
= samp
->level
;
1001 samp
->nearest_texcoord_s(s
, width
, x
);
1002 samp
->nearest_texcoord_t(t
, height
, y
);
1004 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1005 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1007 for (c
= 0; c
< 4; c
++) {
1008 rgba
[c
][j
] = out
[c
];
1015 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1016 const float s
[QUAD_SIZE
],
1017 const float t
[QUAD_SIZE
],
1018 const float p
[QUAD_SIZE
],
1019 const float c0
[QUAD_SIZE
],
1020 enum tgsi_sampler_control control
,
1021 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1023 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1024 const struct pipe_texture
*texture
= samp
->texture
;
1026 int width
, height
, depth
;
1027 int x
[4], y
[4], z
[4];
1028 union tex_tile_address addr
;
1030 level0
= samp
->level
;
1031 width
= u_minify(texture
->width0
, level0
);
1032 height
= u_minify(texture
->height0
, level0
);
1033 depth
= u_minify(texture
->depth0
, level0
);
1039 samp
->nearest_texcoord_s(s
, width
, x
);
1040 samp
->nearest_texcoord_t(t
, height
, y
);
1041 samp
->nearest_texcoord_p(p
, depth
, z
);
1044 addr
.bits
.level
= samp
->level
;
1046 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1047 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1049 for (c
= 0; c
< 4; c
++) {
1050 rgba
[c
][j
] = out
[c
];
1057 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1058 const float s
[QUAD_SIZE
],
1059 const float t
[QUAD_SIZE
],
1060 const float p
[QUAD_SIZE
],
1061 const float c0
[QUAD_SIZE
],
1062 enum tgsi_sampler_control control
,
1063 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1065 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1066 const struct pipe_texture
*texture
= samp
->texture
;
1070 float xw
[4]; /* weights */
1071 union tex_tile_address addr
;
1073 level0
= samp
->level
;
1074 width
= u_minify(texture
->width0
, level0
);
1079 addr
.bits
.level
= samp
->level
;
1081 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1083 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1084 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1085 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1088 /* interpolate R, G, B, A */
1089 for (c
= 0; c
< 4; c
++) {
1090 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1097 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1098 const float s
[QUAD_SIZE
],
1099 const float t
[QUAD_SIZE
],
1100 const float p
[QUAD_SIZE
],
1101 const float c0
[QUAD_SIZE
],
1102 enum tgsi_sampler_control control
,
1103 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1105 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1106 const struct pipe_texture
*texture
= samp
->texture
;
1109 int x0
[4], y0
[4], x1
[4], y1
[4];
1110 float xw
[4], yw
[4]; /* weights */
1111 union tex_tile_address addr
;
1113 level0
= samp
->level
;
1114 width
= u_minify(texture
->width0
, level0
);
1115 height
= u_minify(texture
->height0
, level0
);
1121 addr
.bits
.level
= samp
->level
;
1123 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1124 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1126 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1127 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1128 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1129 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1130 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1133 /* interpolate R, G, B, A */
1134 for (c
= 0; c
< 4; c
++) {
1135 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1144 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1145 const float s
[QUAD_SIZE
],
1146 const float t
[QUAD_SIZE
],
1147 const float p
[QUAD_SIZE
],
1148 const float c0
[QUAD_SIZE
],
1149 enum tgsi_sampler_control control
,
1150 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1152 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1153 const struct pipe_texture
*texture
= samp
->texture
;
1154 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1157 int x0
[4], y0
[4], x1
[4], y1
[4];
1158 float xw
[4], yw
[4]; /* weights */
1159 union tex_tile_address addr
;
1161 level0
= samp
->level
;
1162 width
= u_minify(texture
->width0
, level0
);
1163 height
= u_minify(texture
->height0
, level0
);
1169 addr
.bits
.level
= samp
->level
;
1171 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1172 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1174 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1175 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1176 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1177 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1178 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1179 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1182 /* interpolate R, G, B, A */
1183 for (c
= 0; c
< 4; c
++) {
1184 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1193 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1194 const float s
[QUAD_SIZE
],
1195 const float t
[QUAD_SIZE
],
1196 const float p
[QUAD_SIZE
],
1197 const float c0
[QUAD_SIZE
],
1198 enum tgsi_sampler_control control
,
1199 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1201 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1202 const struct pipe_texture
*texture
= samp
->texture
;
1204 int width
, height
, depth
;
1205 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1206 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1207 union tex_tile_address addr
;
1209 level0
= samp
->level
;
1210 width
= u_minify(texture
->width0
, level0
);
1211 height
= u_minify(texture
->height0
, level0
);
1212 depth
= u_minify(texture
->depth0
, level0
);
1215 addr
.bits
.level
= level0
;
1221 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1222 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1223 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1225 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1228 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1229 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1230 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1231 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1233 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1234 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1235 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1236 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1238 /* interpolate R, G, B, A */
1239 for (c
= 0; c
< 4; c
++) {
1240 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1250 /* Calculate level of detail for every fragment.
1251 * Note that lambda has already been biased by global LOD bias.
1254 compute_lod(const struct pipe_sampler_state
*sampler
,
1255 const float biased_lambda
,
1256 const float lodbias
[QUAD_SIZE
],
1257 float lod
[QUAD_SIZE
])
1261 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1262 lod
[i
] = biased_lambda
+ lodbias
[i
];
1263 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1269 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1270 const float s
[QUAD_SIZE
],
1271 const float t
[QUAD_SIZE
],
1272 const float p
[QUAD_SIZE
],
1273 const float c0
[QUAD_SIZE
],
1274 enum tgsi_sampler_control control
,
1275 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1277 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1278 const struct pipe_texture
*texture
= samp
->texture
;
1281 float lod
[QUAD_SIZE
];
1283 if (control
== tgsi_sampler_lod_bias
) {
1284 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1285 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1287 assert(control
== tgsi_sampler_lod_explicit
);
1289 memcpy(lod
, c0
, sizeof(lod
));
1292 /* XXX: Take into account all lod values.
1295 level0
= (int)lambda
;
1299 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1301 else if (level0
>= texture
->last_level
) {
1302 samp
->level
= texture
->last_level
;
1303 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1306 float levelBlend
= lambda
- level0
;
1311 samp
->level
= level0
;
1312 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1314 samp
->level
= level0
+1;
1315 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1317 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1318 for (c
= 0; c
< 4; c
++) {
1319 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1327 * Compute nearest mipmap level from texcoords.
1328 * Then sample the texture level for four elements of a quad.
1329 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1332 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1333 const float s
[QUAD_SIZE
],
1334 const float t
[QUAD_SIZE
],
1335 const float p
[QUAD_SIZE
],
1336 const float c0
[QUAD_SIZE
],
1337 enum tgsi_sampler_control control
,
1338 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1340 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1341 const struct pipe_texture
*texture
= samp
->texture
;
1343 float lod
[QUAD_SIZE
];
1345 if (control
== tgsi_sampler_lod_bias
) {
1346 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1347 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1349 assert(control
== tgsi_sampler_lod_explicit
);
1351 memcpy(lod
, c0
, sizeof(lod
));
1354 /* XXX: Take into account all lod values.
1360 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1363 samp
->level
= (int)(lambda
+ 0.5) ;
1364 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1365 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1369 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1370 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1371 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1372 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1373 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1379 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1380 const float s
[QUAD_SIZE
],
1381 const float t
[QUAD_SIZE
],
1382 const float p
[QUAD_SIZE
],
1383 const float c0
[QUAD_SIZE
],
1384 enum tgsi_sampler_control control
,
1385 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1387 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1389 float lod
[QUAD_SIZE
];
1391 if (control
== tgsi_sampler_lod_bias
) {
1392 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1393 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1395 assert(control
== tgsi_sampler_lod_explicit
);
1397 memcpy(lod
, c0
, sizeof(lod
));
1400 /* XXX: Take into account all lod values.
1405 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1408 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1415 * Specialized version of mip_filter_linear with hard-wired calls to
1416 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1419 mip_filter_linear_2d_linear_repeat_POT(
1420 struct tgsi_sampler
*tgsi_sampler
,
1421 const float s
[QUAD_SIZE
],
1422 const float t
[QUAD_SIZE
],
1423 const float p
[QUAD_SIZE
],
1424 const float c0
[QUAD_SIZE
],
1425 enum tgsi_sampler_control control
,
1426 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1428 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1429 const struct pipe_texture
*texture
= samp
->texture
;
1432 float lod
[QUAD_SIZE
];
1434 if (control
== tgsi_sampler_lod_bias
) {
1435 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1436 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1438 assert(control
== tgsi_sampler_lod_explicit
);
1440 memcpy(lod
, c0
, sizeof(lod
));
1443 /* XXX: Take into account all lod values.
1446 level0
= (int)lambda
;
1448 /* Catches both negative and large values of level0:
1450 if ((unsigned)level0
>= texture
->last_level
) {
1454 samp
->level
= texture
->last_level
;
1456 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1459 float levelBlend
= lambda
- level0
;
1464 samp
->level
= level0
;
1465 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1467 samp
->level
= level0
+1;
1468 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1470 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1471 for (c
= 0; c
< 4; c
++) {
1472 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1481 * Do shadow/depth comparisons.
1484 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1485 const float s
[QUAD_SIZE
],
1486 const float t
[QUAD_SIZE
],
1487 const float p
[QUAD_SIZE
],
1488 const float c0
[QUAD_SIZE
],
1489 enum tgsi_sampler_control control
,
1490 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1492 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1493 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1494 int j
, k0
, k1
, k2
, k3
;
1497 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1500 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1501 * When we sampled the depth texture, the depth value was put into all
1502 * RGBA channels. We look at the red channel here.
1505 /* compare four texcoords vs. four texture samples */
1506 switch (sampler
->compare_func
) {
1507 case PIPE_FUNC_LESS
:
1508 k0
= p
[0] < rgba
[0][0];
1509 k1
= p
[1] < rgba
[0][1];
1510 k2
= p
[2] < rgba
[0][2];
1511 k3
= p
[3] < rgba
[0][3];
1513 case PIPE_FUNC_LEQUAL
:
1514 k0
= p
[0] <= rgba
[0][0];
1515 k1
= p
[1] <= rgba
[0][1];
1516 k2
= p
[2] <= rgba
[0][2];
1517 k3
= p
[3] <= rgba
[0][3];
1519 case PIPE_FUNC_GREATER
:
1520 k0
= p
[0] > rgba
[0][0];
1521 k1
= p
[1] > rgba
[0][1];
1522 k2
= p
[2] > rgba
[0][2];
1523 k3
= p
[3] > rgba
[0][3];
1525 case PIPE_FUNC_GEQUAL
:
1526 k0
= p
[0] >= rgba
[0][0];
1527 k1
= p
[1] >= rgba
[0][1];
1528 k2
= p
[2] >= rgba
[0][2];
1529 k3
= p
[3] >= rgba
[0][3];
1531 case PIPE_FUNC_EQUAL
:
1532 k0
= p
[0] == rgba
[0][0];
1533 k1
= p
[1] == rgba
[0][1];
1534 k2
= p
[2] == rgba
[0][2];
1535 k3
= p
[3] == rgba
[0][3];
1537 case PIPE_FUNC_NOTEQUAL
:
1538 k0
= p
[0] != rgba
[0][0];
1539 k1
= p
[1] != rgba
[0][1];
1540 k2
= p
[2] != rgba
[0][2];
1541 k3
= p
[3] != rgba
[0][3];
1543 case PIPE_FUNC_ALWAYS
:
1544 k0
= k1
= k2
= k3
= 1;
1546 case PIPE_FUNC_NEVER
:
1547 k0
= k1
= k2
= k3
= 0;
1550 k0
= k1
= k2
= k3
= 0;
1555 /* convert four pass/fail values to an intensity in [0,1] */
1556 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1558 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1559 for (j
= 0; j
< 4; j
++) {
1560 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1567 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1568 * Put face info into the sampler faces[] array.
1571 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1572 const float s
[QUAD_SIZE
],
1573 const float t
[QUAD_SIZE
],
1574 const float p
[QUAD_SIZE
],
1575 const float c0
[QUAD_SIZE
],
1576 enum tgsi_sampler_control control
,
1577 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1579 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1581 float ssss
[4], tttt
[4];
1586 direction target sc tc ma
1587 ---------- ------------------------------- --- --- ---
1588 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1589 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1590 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1591 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1592 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1593 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1596 /* First choose the cube face.
1597 * Use the same cube face for all four pixels in the quad.
1599 * This isn't ideal, but if we want to use a different cube face
1600 * per pixel in the quad, we'd have to also compute the per-face
1601 * LOD here too. That's because the four post-face-selection
1602 * texcoords are no longer related to each other (they're
1603 * per-face!) so we can't use subtraction to compute the partial
1604 * deriviates to compute the LOD. Doing so (near cube edges
1605 * anyway) gives us pretty much random values.
1608 /* use the average of the four pixel's texcoords to choose the face */
1609 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1610 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1611 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1612 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1614 if (arx
>= ary
&& arx
>= arz
) {
1616 face
= PIPE_TEX_FACE_POS_X
;
1619 face
= PIPE_TEX_FACE_NEG_X
;
1622 else if (ary
>= arx
&& ary
>= arz
) {
1624 face
= PIPE_TEX_FACE_POS_Y
;
1627 face
= PIPE_TEX_FACE_NEG_Y
;
1632 face
= PIPE_TEX_FACE_POS_Z
;
1635 face
= PIPE_TEX_FACE_NEG_Z
;
1640 /* Now compute the 2D _face_ texture coords from the
1641 * 3D _cube_ texture coords.
1643 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1644 const float rx
= s
[j
], ry
= t
[j
], rz
= p
[j
];
1645 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1649 case PIPE_TEX_FACE_POS_X
:
1654 case PIPE_TEX_FACE_NEG_X
:
1659 case PIPE_TEX_FACE_POS_Y
:
1664 case PIPE_TEX_FACE_NEG_Y
:
1669 case PIPE_TEX_FACE_POS_Z
:
1674 case PIPE_TEX_FACE_NEG_Z
:
1680 assert(0 && "bad cube face");
1687 const float ima
= 1.0 / ma
;
1688 ssss
[j
] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1689 tttt
[j
] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1690 samp
->faces
[j
] = face
;
1694 /* In our little pipeline, the compare stage is next. If compare
1695 * is not active, this will point somewhere deeper into the
1696 * pipeline, eg. to mip_filter or even img_filter.
1698 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1703 static wrap_nearest_func
1704 get_nearest_unorm_wrap(unsigned mode
)
1707 case PIPE_TEX_WRAP_CLAMP
:
1708 return wrap_nearest_unorm_clamp
;
1709 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1710 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1711 return wrap_nearest_unorm_clamp_to_border
;
1714 return wrap_nearest_unorm_clamp
;
1719 static wrap_nearest_func
1720 get_nearest_wrap(unsigned mode
)
1723 case PIPE_TEX_WRAP_REPEAT
:
1724 return wrap_nearest_repeat
;
1725 case PIPE_TEX_WRAP_CLAMP
:
1726 return wrap_nearest_clamp
;
1727 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1728 return wrap_nearest_clamp_to_edge
;
1729 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1730 return wrap_nearest_clamp_to_border
;
1731 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1732 return wrap_nearest_mirror_repeat
;
1733 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1734 return wrap_nearest_mirror_clamp
;
1735 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1736 return wrap_nearest_mirror_clamp_to_edge
;
1737 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1738 return wrap_nearest_mirror_clamp_to_border
;
1741 return wrap_nearest_repeat
;
1746 static wrap_linear_func
1747 get_linear_unorm_wrap(unsigned mode
)
1750 case PIPE_TEX_WRAP_CLAMP
:
1751 return wrap_linear_unorm_clamp
;
1752 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1753 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1754 return wrap_linear_unorm_clamp_to_border
;
1757 return wrap_linear_unorm_clamp
;
1762 static wrap_linear_func
1763 get_linear_wrap(unsigned mode
)
1766 case PIPE_TEX_WRAP_REPEAT
:
1767 return wrap_linear_repeat
;
1768 case PIPE_TEX_WRAP_CLAMP
:
1769 return wrap_linear_clamp
;
1770 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1771 return wrap_linear_clamp_to_edge
;
1772 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1773 return wrap_linear_clamp_to_border
;
1774 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1775 return wrap_linear_mirror_repeat
;
1776 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1777 return wrap_linear_mirror_clamp
;
1778 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1779 return wrap_linear_mirror_clamp_to_edge
;
1780 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1781 return wrap_linear_mirror_clamp_to_border
;
1784 return wrap_linear_repeat
;
1789 static compute_lambda_func
1790 get_lambda_func(const union sp_sampler_key key
)
1792 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1793 return compute_lambda_vert
;
1795 switch (key
.bits
.target
) {
1796 case PIPE_TEXTURE_1D
:
1797 return compute_lambda_1d
;
1798 case PIPE_TEXTURE_2D
:
1799 case PIPE_TEXTURE_CUBE
:
1800 return compute_lambda_2d
;
1801 case PIPE_TEXTURE_3D
:
1802 return compute_lambda_3d
;
1805 return compute_lambda_1d
;
1811 get_img_filter(const union sp_sampler_key key
,
1813 const struct pipe_sampler_state
*sampler
)
1815 switch (key
.bits
.target
) {
1816 case PIPE_TEXTURE_1D
:
1817 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1818 return img_filter_1d_nearest
;
1820 return img_filter_1d_linear
;
1822 case PIPE_TEXTURE_2D
:
1823 /* Try for fast path:
1825 if (key
.bits
.is_pot
&&
1826 sampler
->wrap_s
== sampler
->wrap_t
&&
1827 sampler
->normalized_coords
)
1829 switch (sampler
->wrap_s
) {
1830 case PIPE_TEX_WRAP_REPEAT
:
1832 case PIPE_TEX_FILTER_NEAREST
:
1833 return img_filter_2d_nearest_repeat_POT
;
1834 case PIPE_TEX_FILTER_LINEAR
:
1835 return img_filter_2d_linear_repeat_POT
;
1840 case PIPE_TEX_WRAP_CLAMP
:
1842 case PIPE_TEX_FILTER_NEAREST
:
1843 return img_filter_2d_nearest_clamp_POT
;
1849 /* Otherwise use default versions:
1851 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1852 return img_filter_2d_nearest
;
1854 return img_filter_2d_linear
;
1856 case PIPE_TEXTURE_CUBE
:
1857 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1858 return img_filter_cube_nearest
;
1860 return img_filter_cube_linear
;
1862 case PIPE_TEXTURE_3D
:
1863 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1864 return img_filter_3d_nearest
;
1866 return img_filter_3d_linear
;
1870 return img_filter_1d_nearest
;
1876 * Bind the given texture object and texture cache to the sampler varient.
1879 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1880 struct softpipe_tex_tile_cache
*tex_cache
,
1881 const struct pipe_texture
*texture
)
1883 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1885 samp
->texture
= texture
;
1886 samp
->cache
= tex_cache
;
1887 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
1888 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
1889 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1894 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1901 * Create a sampler varient for a given set of non-orthogonal state.
1903 struct sp_sampler_varient
*
1904 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1905 const union sp_sampler_key key
)
1907 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1911 samp
->sampler
= sampler
;
1914 /* Note that (for instance) linear_texcoord_s and
1915 * nearest_texcoord_s may be active at the same time, if the
1916 * sampler min_img_filter differs from its mag_img_filter.
1918 if (sampler
->normalized_coords
) {
1919 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1920 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1921 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1923 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1924 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1925 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1928 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1929 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1930 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1932 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1933 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1934 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1937 samp
->compute_lambda
= get_lambda_func( key
);
1939 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1940 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1942 switch (sampler
->min_mip_filter
) {
1943 case PIPE_TEX_MIPFILTER_NONE
:
1944 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1945 samp
->mip_filter
= samp
->min_img_filter
;
1947 samp
->mip_filter
= mip_filter_none
;
1950 case PIPE_TEX_MIPFILTER_NEAREST
:
1951 samp
->mip_filter
= mip_filter_nearest
;
1954 case PIPE_TEX_MIPFILTER_LINEAR
:
1955 if (key
.bits
.is_pot
&&
1956 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1957 sampler
->normalized_coords
&&
1958 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1959 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1960 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1962 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
1966 samp
->mip_filter
= mip_filter_linear
;
1971 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
1972 samp
->compare
= sample_compare
;
1975 /* Skip compare operation by promoting the mip_filter function
1978 samp
->compare
= samp
->mip_filter
;
1981 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
1982 samp
->base
.get_samples
= sample_cube
;
1990 /* Skip cube face determination by promoting the compare
1993 samp
->base
.get_samples
= samp
->compare
;