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 * 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 samp
->sampler
->border_color
;
662 return get_texel_2d_no_border( samp
, addr
, x
, y
);
667 /* Gather a quad of adjacent texels within a tile:
670 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
671 union tex_tile_address addr
,
672 unsigned x
, unsigned y
,
675 const struct softpipe_tex_cached_tile
*tile
;
677 addr
.bits
.x
= x
/ TILE_SIZE
;
678 addr
.bits
.y
= y
/ TILE_SIZE
;
682 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
684 out
[0] = &tile
->data
.color
[y
][x
][0];
685 out
[1] = &tile
->data
.color
[y
][x
+1][0];
686 out
[2] = &tile
->data
.color
[y
+1][x
][0];
687 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
691 /* Gather a quad of potentially non-adjacent texels:
694 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
695 union tex_tile_address addr
,
700 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
701 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
702 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
703 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
706 /* Can involve a lot of unnecessary checks for border color:
709 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
710 union tex_tile_address addr
,
715 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
716 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
717 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
718 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
725 static INLINE
const float *
726 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
727 union tex_tile_address addr
, int x
, int y
, int z
)
729 const struct softpipe_tex_cached_tile
*tile
;
731 addr
.bits
.x
= x
/ TILE_SIZE
;
732 addr
.bits
.y
= y
/ TILE_SIZE
;
737 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
739 return &tile
->data
.color
[y
][x
][0];
743 static INLINE
const float *
744 get_texel_3d(const struct sp_sampler_varient
*samp
,
745 union tex_tile_address addr
, int x
, int y
, int z
)
747 const struct pipe_resource
*texture
= samp
->texture
;
748 unsigned level
= addr
.bits
.level
;
750 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
751 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
752 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
753 return samp
->sampler
->border_color
;
756 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
762 * Given the logbase2 of a mipmap's base level size and a mipmap level,
763 * return the size (in texels) of that mipmap level.
764 * For example, if level[0].width = 256 then base_pot will be 8.
765 * If level = 2, then we'll return 64 (the width at level=2).
766 * Return 1 if level > base_pot.
768 static INLINE
unsigned
769 pot_level_size(unsigned base_pot
, unsigned level
)
771 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
775 /* Some image-filter fastpaths:
778 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
779 const float s
[QUAD_SIZE
],
780 const float t
[QUAD_SIZE
],
781 const float p
[QUAD_SIZE
],
782 const float c0
[QUAD_SIZE
],
783 enum tgsi_sampler_control control
,
784 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
786 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
788 unsigned level
= samp
->level
;
789 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
790 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
791 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
792 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
793 union tex_tile_address addr
;
796 addr
.bits
.level
= samp
->level
;
798 for (j
= 0; j
< QUAD_SIZE
; j
++) {
801 float u
= s
[j
] * xpot
- 0.5F
;
802 float v
= t
[j
] * ypot
- 0.5F
;
804 int uflr
= util_ifloor(u
);
805 int vflr
= util_ifloor(v
);
807 float xw
= u
- (float)uflr
;
808 float yw
= v
- (float)vflr
;
810 int x0
= uflr
& (xpot
- 1);
811 int y0
= vflr
& (ypot
- 1);
815 /* Can we fetch all four at once:
817 if (x0
< xmax
&& y0
< ymax
) {
818 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
821 unsigned x1
= (x0
+ 1) & (xpot
- 1);
822 unsigned y1
= (y0
+ 1) & (ypot
- 1);
823 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
826 /* interpolate R, G, B, A */
827 for (c
= 0; c
< 4; c
++) {
828 rgba
[c
][j
] = lerp_2d(xw
, yw
,
837 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
838 const float s
[QUAD_SIZE
],
839 const float t
[QUAD_SIZE
],
840 const float p
[QUAD_SIZE
],
841 const float c0
[QUAD_SIZE
],
842 enum tgsi_sampler_control control
,
843 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
845 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
847 unsigned level
= samp
->level
;
848 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
849 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
850 union tex_tile_address addr
;
853 addr
.bits
.level
= samp
->level
;
855 for (j
= 0; j
< QUAD_SIZE
; j
++) {
858 float u
= s
[j
] * xpot
;
859 float v
= t
[j
] * ypot
;
861 int uflr
= util_ifloor(u
);
862 int vflr
= util_ifloor(v
);
864 int x0
= uflr
& (xpot
- 1);
865 int y0
= vflr
& (ypot
- 1);
867 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
869 for (c
= 0; c
< 4; c
++) {
877 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
878 const float s
[QUAD_SIZE
],
879 const float t
[QUAD_SIZE
],
880 const float p
[QUAD_SIZE
],
881 const float c0
[QUAD_SIZE
],
882 enum tgsi_sampler_control control
,
883 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
885 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
887 unsigned level
= samp
->level
;
888 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
889 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
890 union tex_tile_address addr
;
893 addr
.bits
.level
= samp
->level
;
895 for (j
= 0; j
< QUAD_SIZE
; j
++) {
898 float u
= s
[j
] * xpot
;
899 float v
= t
[j
] * ypot
;
907 else if (x0
> xpot
- 1)
913 else if (y0
> ypot
- 1)
916 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
918 for (c
= 0; c
< 4; c
++) {
926 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
927 const float s
[QUAD_SIZE
],
928 const float t
[QUAD_SIZE
],
929 const float p
[QUAD_SIZE
],
930 const float c0
[QUAD_SIZE
],
931 enum tgsi_sampler_control control
,
932 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
934 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
935 const struct pipe_resource
*texture
= samp
->texture
;
939 union tex_tile_address addr
;
941 level0
= samp
->level
;
942 width
= u_minify(texture
->width0
, level0
);
947 addr
.bits
.level
= samp
->level
;
949 samp
->nearest_texcoord_s(s
, width
, x
);
951 for (j
= 0; j
< QUAD_SIZE
; j
++) {
952 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
954 for (c
= 0; c
< 4; c
++) {
962 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
963 const float s
[QUAD_SIZE
],
964 const float t
[QUAD_SIZE
],
965 const float p
[QUAD_SIZE
],
966 const float c0
[QUAD_SIZE
],
967 enum tgsi_sampler_control control
,
968 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
970 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
971 const struct pipe_resource
*texture
= samp
->texture
;
975 union tex_tile_address addr
;
978 level0
= samp
->level
;
979 width
= u_minify(texture
->width0
, level0
);
980 height
= u_minify(texture
->height0
, level0
);
986 addr
.bits
.level
= samp
->level
;
988 samp
->nearest_texcoord_s(s
, width
, x
);
989 samp
->nearest_texcoord_t(t
, height
, y
);
991 for (j
= 0; j
< QUAD_SIZE
; j
++) {
992 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
994 for (c
= 0; c
< 4; c
++) {
1001 static INLINE
union tex_tile_address
1002 face(union tex_tile_address addr
, unsigned face
)
1004 addr
.bits
.face
= face
;
1010 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
1011 const float s
[QUAD_SIZE
],
1012 const float t
[QUAD_SIZE
],
1013 const float p
[QUAD_SIZE
],
1014 const float c0
[QUAD_SIZE
],
1015 enum tgsi_sampler_control control
,
1016 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1018 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1019 const struct pipe_resource
*texture
= samp
->texture
;
1020 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1024 union tex_tile_address addr
;
1026 level0
= samp
->level
;
1027 width
= u_minify(texture
->width0
, level0
);
1028 height
= u_minify(texture
->height0
, level0
);
1034 addr
.bits
.level
= samp
->level
;
1036 samp
->nearest_texcoord_s(s
, width
, x
);
1037 samp
->nearest_texcoord_t(t
, height
, y
);
1039 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1040 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1042 for (c
= 0; c
< 4; c
++) {
1043 rgba
[c
][j
] = out
[c
];
1050 img_filter_3d_nearest(struct tgsi_sampler
*tgsi_sampler
,
1051 const float s
[QUAD_SIZE
],
1052 const float t
[QUAD_SIZE
],
1053 const float p
[QUAD_SIZE
],
1054 const float c0
[QUAD_SIZE
],
1055 enum tgsi_sampler_control control
,
1056 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1058 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1059 const struct pipe_resource
*texture
= samp
->texture
;
1061 int width
, height
, depth
;
1062 int x
[4], y
[4], z
[4];
1063 union tex_tile_address addr
;
1065 level0
= samp
->level
;
1066 width
= u_minify(texture
->width0
, level0
);
1067 height
= u_minify(texture
->height0
, level0
);
1068 depth
= u_minify(texture
->depth0
, level0
);
1074 samp
->nearest_texcoord_s(s
, width
, x
);
1075 samp
->nearest_texcoord_t(t
, height
, y
);
1076 samp
->nearest_texcoord_p(p
, depth
, z
);
1079 addr
.bits
.level
= samp
->level
;
1081 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1082 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1084 for (c
= 0; c
< 4; c
++) {
1085 rgba
[c
][j
] = out
[c
];
1092 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1093 const float s
[QUAD_SIZE
],
1094 const float t
[QUAD_SIZE
],
1095 const float p
[QUAD_SIZE
],
1096 const float c0
[QUAD_SIZE
],
1097 enum tgsi_sampler_control control
,
1098 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1100 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1101 const struct pipe_resource
*texture
= samp
->texture
;
1105 float xw
[4]; /* weights */
1106 union tex_tile_address addr
;
1108 level0
= samp
->level
;
1109 width
= u_minify(texture
->width0
, level0
);
1114 addr
.bits
.level
= samp
->level
;
1116 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1118 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1119 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1120 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1123 /* interpolate R, G, B, A */
1124 for (c
= 0; c
< 4; c
++) {
1125 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1132 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1133 const float s
[QUAD_SIZE
],
1134 const float t
[QUAD_SIZE
],
1135 const float p
[QUAD_SIZE
],
1136 const float c0
[QUAD_SIZE
],
1137 enum tgsi_sampler_control control
,
1138 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1140 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1141 const struct pipe_resource
*texture
= samp
->texture
;
1144 int x0
[4], y0
[4], x1
[4], y1
[4];
1145 float xw
[4], yw
[4]; /* weights */
1146 union tex_tile_address addr
;
1148 level0
= samp
->level
;
1149 width
= u_minify(texture
->width0
, level0
);
1150 height
= u_minify(texture
->height0
, level0
);
1156 addr
.bits
.level
= samp
->level
;
1158 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1159 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1161 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1162 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1163 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1164 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1165 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1168 /* interpolate R, G, B, A */
1169 for (c
= 0; c
< 4; c
++) {
1170 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1179 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1180 const float s
[QUAD_SIZE
],
1181 const float t
[QUAD_SIZE
],
1182 const float p
[QUAD_SIZE
],
1183 const float c0
[QUAD_SIZE
],
1184 enum tgsi_sampler_control control
,
1185 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1187 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1188 const struct pipe_resource
*texture
= samp
->texture
;
1189 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1192 int x0
[4], y0
[4], x1
[4], y1
[4];
1193 float xw
[4], yw
[4]; /* weights */
1194 union tex_tile_address addr
;
1196 level0
= samp
->level
;
1197 width
= u_minify(texture
->width0
, level0
);
1198 height
= u_minify(texture
->height0
, level0
);
1204 addr
.bits
.level
= samp
->level
;
1206 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1207 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1209 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1210 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1211 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1212 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1213 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1214 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1217 /* interpolate R, G, B, A */
1218 for (c
= 0; c
< 4; c
++) {
1219 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1228 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1229 const float s
[QUAD_SIZE
],
1230 const float t
[QUAD_SIZE
],
1231 const float p
[QUAD_SIZE
],
1232 const float c0
[QUAD_SIZE
],
1233 enum tgsi_sampler_control control
,
1234 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1236 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1237 const struct pipe_resource
*texture
= samp
->texture
;
1239 int width
, height
, depth
;
1240 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1241 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1242 union tex_tile_address addr
;
1244 level0
= samp
->level
;
1245 width
= u_minify(texture
->width0
, level0
);
1246 height
= u_minify(texture
->height0
, level0
);
1247 depth
= u_minify(texture
->depth0
, level0
);
1250 addr
.bits
.level
= level0
;
1256 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1257 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1258 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1260 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1263 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1264 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1265 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1266 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1268 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1269 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1270 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1271 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1273 /* interpolate R, G, B, A */
1274 for (c
= 0; c
< 4; c
++) {
1275 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1285 /* Calculate level of detail for every fragment.
1286 * Note that lambda has already been biased by global LOD bias.
1289 compute_lod(const struct pipe_sampler_state
*sampler
,
1290 const float biased_lambda
,
1291 const float lodbias
[QUAD_SIZE
],
1292 float lod
[QUAD_SIZE
])
1296 for (i
= 0; i
< QUAD_SIZE
; i
++) {
1297 lod
[i
] = biased_lambda
+ lodbias
[i
];
1298 lod
[i
] = CLAMP(lod
[i
], sampler
->min_lod
, sampler
->max_lod
);
1304 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1305 const float s
[QUAD_SIZE
],
1306 const float t
[QUAD_SIZE
],
1307 const float p
[QUAD_SIZE
],
1308 const float c0
[QUAD_SIZE
],
1309 enum tgsi_sampler_control control
,
1310 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1312 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1313 const struct pipe_resource
*texture
= samp
->texture
;
1316 float lod
[QUAD_SIZE
];
1318 if (control
== tgsi_sampler_lod_bias
) {
1319 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1320 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1322 assert(control
== tgsi_sampler_lod_explicit
);
1324 memcpy(lod
, c0
, sizeof(lod
));
1327 /* XXX: Take into account all lod values.
1330 level0
= (int)lambda
;
1334 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1336 else if (level0
>= texture
->last_level
) {
1337 samp
->level
= texture
->last_level
;
1338 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1341 float levelBlend
= lambda
- level0
;
1346 samp
->level
= level0
;
1347 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1349 samp
->level
= level0
+1;
1350 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1352 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1353 for (c
= 0; c
< 4; c
++) {
1354 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1362 * Compute nearest mipmap level from texcoords.
1363 * Then sample the texture level for four elements of a quad.
1364 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1367 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1368 const float s
[QUAD_SIZE
],
1369 const float t
[QUAD_SIZE
],
1370 const float p
[QUAD_SIZE
],
1371 const float c0
[QUAD_SIZE
],
1372 enum tgsi_sampler_control control
,
1373 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1375 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1376 const struct pipe_resource
*texture
= samp
->texture
;
1378 float lod
[QUAD_SIZE
];
1380 if (control
== tgsi_sampler_lod_bias
) {
1381 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1382 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1384 assert(control
== tgsi_sampler_lod_explicit
);
1386 memcpy(lod
, c0
, sizeof(lod
));
1389 /* XXX: Take into account all lod values.
1395 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1398 samp
->level
= (int)(lambda
+ 0.5) ;
1399 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1400 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1404 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1405 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1406 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1407 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1408 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1414 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1415 const float s
[QUAD_SIZE
],
1416 const float t
[QUAD_SIZE
],
1417 const float p
[QUAD_SIZE
],
1418 const float c0
[QUAD_SIZE
],
1419 enum tgsi_sampler_control control
,
1420 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1422 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1424 float lod
[QUAD_SIZE
];
1426 if (control
== tgsi_sampler_lod_bias
) {
1427 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1428 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1430 assert(control
== tgsi_sampler_lod_explicit
);
1432 memcpy(lod
, c0
, sizeof(lod
));
1435 /* XXX: Take into account all lod values.
1440 samp
->mag_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1443 samp
->min_img_filter(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1450 * Specialized version of mip_filter_linear with hard-wired calls to
1451 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1454 mip_filter_linear_2d_linear_repeat_POT(
1455 struct tgsi_sampler
*tgsi_sampler
,
1456 const float s
[QUAD_SIZE
],
1457 const float t
[QUAD_SIZE
],
1458 const float p
[QUAD_SIZE
],
1459 const float c0
[QUAD_SIZE
],
1460 enum tgsi_sampler_control control
,
1461 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1463 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1464 const struct pipe_resource
*texture
= samp
->texture
;
1467 float lod
[QUAD_SIZE
];
1469 if (control
== tgsi_sampler_lod_bias
) {
1470 lambda
= samp
->compute_lambda(samp
, s
, t
, p
) + samp
->sampler
->lod_bias
;
1471 compute_lod(samp
->sampler
, lambda
, c0
, lod
);
1473 assert(control
== tgsi_sampler_lod_explicit
);
1475 memcpy(lod
, c0
, sizeof(lod
));
1478 /* XXX: Take into account all lod values.
1481 level0
= (int)lambda
;
1483 /* Catches both negative and large values of level0:
1485 if ((unsigned)level0
>= texture
->last_level
) {
1489 samp
->level
= texture
->last_level
;
1491 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba
);
1494 float levelBlend
= lambda
- level0
;
1499 samp
->level
= level0
;
1500 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba0
);
1502 samp
->level
= level0
+1;
1503 img_filter_2d_linear_repeat_POT(tgsi_sampler
, s
, t
, p
, NULL
, tgsi_sampler_lod_bias
, rgba1
);
1505 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1506 for (c
= 0; c
< 4; c
++) {
1507 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1516 * Do shadow/depth comparisons.
1519 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1520 const float s
[QUAD_SIZE
],
1521 const float t
[QUAD_SIZE
],
1522 const float p
[QUAD_SIZE
],
1523 const float c0
[QUAD_SIZE
],
1524 enum tgsi_sampler_control control
,
1525 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1527 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1528 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1529 int j
, k0
, k1
, k2
, k3
;
1532 samp
->mip_filter(tgsi_sampler
, s
, t
, p
, c0
, control
, rgba
);
1535 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1536 * When we sampled the depth texture, the depth value was put into all
1537 * RGBA channels. We look at the red channel here.
1540 /* compare four texcoords vs. four texture samples */
1541 switch (sampler
->compare_func
) {
1542 case PIPE_FUNC_LESS
:
1543 k0
= p
[0] < rgba
[0][0];
1544 k1
= p
[1] < rgba
[0][1];
1545 k2
= p
[2] < rgba
[0][2];
1546 k3
= p
[3] < rgba
[0][3];
1548 case PIPE_FUNC_LEQUAL
:
1549 k0
= p
[0] <= rgba
[0][0];
1550 k1
= p
[1] <= rgba
[0][1];
1551 k2
= p
[2] <= rgba
[0][2];
1552 k3
= p
[3] <= rgba
[0][3];
1554 case PIPE_FUNC_GREATER
:
1555 k0
= p
[0] > rgba
[0][0];
1556 k1
= p
[1] > rgba
[0][1];
1557 k2
= p
[2] > rgba
[0][2];
1558 k3
= p
[3] > rgba
[0][3];
1560 case PIPE_FUNC_GEQUAL
:
1561 k0
= p
[0] >= rgba
[0][0];
1562 k1
= p
[1] >= rgba
[0][1];
1563 k2
= p
[2] >= rgba
[0][2];
1564 k3
= p
[3] >= rgba
[0][3];
1566 case PIPE_FUNC_EQUAL
:
1567 k0
= p
[0] == rgba
[0][0];
1568 k1
= p
[1] == rgba
[0][1];
1569 k2
= p
[2] == rgba
[0][2];
1570 k3
= p
[3] == rgba
[0][3];
1572 case PIPE_FUNC_NOTEQUAL
:
1573 k0
= p
[0] != rgba
[0][0];
1574 k1
= p
[1] != rgba
[0][1];
1575 k2
= p
[2] != rgba
[0][2];
1576 k3
= p
[3] != rgba
[0][3];
1578 case PIPE_FUNC_ALWAYS
:
1579 k0
= k1
= k2
= k3
= 1;
1581 case PIPE_FUNC_NEVER
:
1582 k0
= k1
= k2
= k3
= 0;
1585 k0
= k1
= k2
= k3
= 0;
1590 /* convert four pass/fail values to an intensity in [0,1] */
1591 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1593 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1594 for (j
= 0; j
< 4; j
++) {
1595 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1602 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
1603 * Put face info into the sampler faces[] array.
1606 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1607 const float s
[QUAD_SIZE
],
1608 const float t
[QUAD_SIZE
],
1609 const float p
[QUAD_SIZE
],
1610 const float c0
[QUAD_SIZE
],
1611 enum tgsi_sampler_control control
,
1612 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1614 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1616 float ssss
[4], tttt
[4];
1620 direction target sc tc ma
1621 ---------- ------------------------------- --- --- ---
1622 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1623 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1624 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1625 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1626 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1627 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1630 /* Choose the cube face and compute new s/t coords for the 2D face.
1632 * Use the same cube face for all four pixels in the quad.
1634 * This isn't ideal, but if we want to use a different cube face
1635 * per pixel in the quad, we'd have to also compute the per-face
1636 * LOD here too. That's because the four post-face-selection
1637 * texcoords are no longer related to each other (they're
1638 * per-face!) so we can't use subtraction to compute the partial
1639 * deriviates to compute the LOD. Doing so (near cube edges
1640 * anyway) gives us pretty much random values.
1643 /* use the average of the four pixel's texcoords to choose the face */
1644 const float rx
= 0.25 * (s
[0] + s
[1] + s
[2] + s
[3]);
1645 const float ry
= 0.25 * (t
[0] + t
[1] + t
[2] + t
[3]);
1646 const float rz
= 0.25 * (p
[0] + p
[1] + p
[2] + p
[3]);
1647 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1649 if (arx
>= ary
&& arx
>= arz
) {
1650 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
1651 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
1652 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1653 const float ima
= -0.5F
/ fabsf(s
[j
]);
1654 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
1655 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1656 samp
->faces
[j
] = face
;
1659 else if (ary
>= arx
&& ary
>= arz
) {
1660 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
1661 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
1662 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1663 const float ima
= -0.5F
/ fabsf(t
[j
]);
1664 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
1665 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
1666 samp
->faces
[j
] = face
;
1670 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
1671 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
1672 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1673 const float ima
= -0.5 / fabsf(p
[j
]);
1674 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
1675 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
1676 samp
->faces
[j
] = face
;
1681 /* In our little pipeline, the compare stage is next. If compare
1682 * is not active, this will point somewhere deeper into the
1683 * pipeline, eg. to mip_filter or even img_filter.
1685 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, c0
, control
, rgba
);
1690 static wrap_nearest_func
1691 get_nearest_unorm_wrap(unsigned mode
)
1694 case PIPE_TEX_WRAP_CLAMP
:
1695 return wrap_nearest_unorm_clamp
;
1696 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1697 return wrap_nearest_unorm_clamp_to_edge
;
1698 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1699 return wrap_nearest_unorm_clamp_to_border
;
1702 return wrap_nearest_unorm_clamp
;
1707 static wrap_nearest_func
1708 get_nearest_wrap(unsigned mode
)
1711 case PIPE_TEX_WRAP_REPEAT
:
1712 return wrap_nearest_repeat
;
1713 case PIPE_TEX_WRAP_CLAMP
:
1714 return wrap_nearest_clamp
;
1715 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1716 return wrap_nearest_clamp_to_edge
;
1717 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1718 return wrap_nearest_clamp_to_border
;
1719 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1720 return wrap_nearest_mirror_repeat
;
1721 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1722 return wrap_nearest_mirror_clamp
;
1723 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1724 return wrap_nearest_mirror_clamp_to_edge
;
1725 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1726 return wrap_nearest_mirror_clamp_to_border
;
1729 return wrap_nearest_repeat
;
1734 static wrap_linear_func
1735 get_linear_unorm_wrap(unsigned mode
)
1738 case PIPE_TEX_WRAP_CLAMP
:
1739 return wrap_linear_unorm_clamp
;
1740 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1741 return wrap_linear_unorm_clamp_to_edge
;
1742 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1743 return wrap_linear_unorm_clamp_to_border
;
1746 return wrap_linear_unorm_clamp
;
1751 static wrap_linear_func
1752 get_linear_wrap(unsigned mode
)
1755 case PIPE_TEX_WRAP_REPEAT
:
1756 return wrap_linear_repeat
;
1757 case PIPE_TEX_WRAP_CLAMP
:
1758 return wrap_linear_clamp
;
1759 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1760 return wrap_linear_clamp_to_edge
;
1761 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1762 return wrap_linear_clamp_to_border
;
1763 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1764 return wrap_linear_mirror_repeat
;
1765 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1766 return wrap_linear_mirror_clamp
;
1767 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1768 return wrap_linear_mirror_clamp_to_edge
;
1769 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1770 return wrap_linear_mirror_clamp_to_border
;
1773 return wrap_linear_repeat
;
1778 static compute_lambda_func
1779 get_lambda_func(const union sp_sampler_key key
)
1781 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1782 return compute_lambda_vert
;
1784 switch (key
.bits
.target
) {
1785 case PIPE_TEXTURE_1D
:
1786 return compute_lambda_1d
;
1787 case PIPE_TEXTURE_2D
:
1788 case PIPE_TEXTURE_CUBE
:
1789 return compute_lambda_2d
;
1790 case PIPE_TEXTURE_3D
:
1791 return compute_lambda_3d
;
1794 return compute_lambda_1d
;
1800 get_img_filter(const union sp_sampler_key key
,
1802 const struct pipe_sampler_state
*sampler
)
1804 switch (key
.bits
.target
) {
1805 case PIPE_TEXTURE_1D
:
1806 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1807 return img_filter_1d_nearest
;
1809 return img_filter_1d_linear
;
1811 case PIPE_TEXTURE_2D
:
1812 /* Try for fast path:
1814 if (key
.bits
.is_pot
&&
1815 sampler
->wrap_s
== sampler
->wrap_t
&&
1816 sampler
->normalized_coords
)
1818 switch (sampler
->wrap_s
) {
1819 case PIPE_TEX_WRAP_REPEAT
:
1821 case PIPE_TEX_FILTER_NEAREST
:
1822 return img_filter_2d_nearest_repeat_POT
;
1823 case PIPE_TEX_FILTER_LINEAR
:
1824 return img_filter_2d_linear_repeat_POT
;
1829 case PIPE_TEX_WRAP_CLAMP
:
1831 case PIPE_TEX_FILTER_NEAREST
:
1832 return img_filter_2d_nearest_clamp_POT
;
1838 /* Otherwise use default versions:
1840 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1841 return img_filter_2d_nearest
;
1843 return img_filter_2d_linear
;
1845 case PIPE_TEXTURE_CUBE
:
1846 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1847 return img_filter_cube_nearest
;
1849 return img_filter_cube_linear
;
1851 case PIPE_TEXTURE_3D
:
1852 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1853 return img_filter_3d_nearest
;
1855 return img_filter_3d_linear
;
1859 return img_filter_1d_nearest
;
1865 * Bind the given texture object and texture cache to the sampler varient.
1868 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1869 struct softpipe_tex_tile_cache
*tex_cache
,
1870 const struct pipe_resource
*texture
)
1872 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1874 samp
->texture
= texture
;
1875 samp
->cache
= tex_cache
;
1876 samp
->xpot
= util_unsigned_logbase2( texture
->width0
);
1877 samp
->ypot
= util_unsigned_logbase2( texture
->height0
);
1878 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1883 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1890 * Create a sampler varient for a given set of non-orthogonal state.
1892 struct sp_sampler_varient
*
1893 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1894 const union sp_sampler_key key
)
1896 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1900 samp
->sampler
= sampler
;
1903 /* Note that (for instance) linear_texcoord_s and
1904 * nearest_texcoord_s may be active at the same time, if the
1905 * sampler min_img_filter differs from its mag_img_filter.
1907 if (sampler
->normalized_coords
) {
1908 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1909 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1910 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1912 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1913 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1914 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1917 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1918 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1919 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1921 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1922 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1923 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1926 samp
->compute_lambda
= get_lambda_func( key
);
1928 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1929 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1931 switch (sampler
->min_mip_filter
) {
1932 case PIPE_TEX_MIPFILTER_NONE
:
1933 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1934 samp
->mip_filter
= samp
->min_img_filter
;
1936 samp
->mip_filter
= mip_filter_none
;
1939 case PIPE_TEX_MIPFILTER_NEAREST
:
1940 samp
->mip_filter
= mip_filter_nearest
;
1943 case PIPE_TEX_MIPFILTER_LINEAR
:
1944 if (key
.bits
.is_pot
&&
1945 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1946 sampler
->normalized_coords
&&
1947 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1948 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1949 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1951 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
1955 samp
->mip_filter
= mip_filter_linear
;
1960 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
1961 samp
->compare
= sample_compare
;
1964 /* Skip compare operation by promoting the mip_filter function
1967 samp
->compare
= samp
->mip_filter
;
1970 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
1971 samp
->base
.get_samples
= sample_cube
;
1979 /* Skip cube face determination by promoting the compare
1982 samp
->base
.get_samples
= samp
->compare
;