1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008 VMware, Inc. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
36 #include "sp_context.h"
38 #include "sp_surface.h"
39 #include "sp_texture.h"
40 #include "sp_tex_sample.h"
41 #include "sp_tile_cache.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_shader_tokens.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
51 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
52 * see 1-pixel bands of improperly weighted linear-filtered textures.
53 * The tests/texwrap.c demo is a good test.
54 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
55 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
57 #define FRAC(f) ((f) - util_ifloor(f))
61 * Linear interpolation macro
64 lerp(float a
, float v0
, float v1
)
66 return v0
+ a
* (v1
- v0
);
71 * Do 2D/biliner interpolation of float values.
72 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
73 * a and b are the horizontal and vertical interpolants.
74 * It's important that this function is inlined when compiled with
75 * optimization! If we find that's not true on some systems, convert
79 lerp_2d(float a
, float b
,
80 float v00
, float v10
, float v01
, float v11
)
82 const float temp0
= lerp(a
, v00
, v10
);
83 const float temp1
= lerp(a
, v01
, v11
);
84 return lerp(b
, temp0
, temp1
);
89 * As above, but 3D interpolation of 8 values.
92 lerp_3d(float a
, float b
, float c
,
93 float v000
, float v100
, float v010
, float v110
,
94 float v001
, float v101
, float v011
, float v111
)
96 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
97 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
98 return lerp(c
, temp0
, temp1
);
104 * If A is a signed integer, A % B doesn't give the right value for A < 0
105 * (in terms of texture repeat). Just casting to unsigned fixes that.
107 #define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
111 * Apply texture coord wrapping mode and return integer texture indexes
112 * for a vector of four texcoords (S or T or P).
113 * \param wrapMode PIPE_TEX_WRAP_x
114 * \param s the incoming texcoords
115 * \param size the texture image size
116 * \param icoord returns the integer texcoords
117 * \return integer texture index
120 nearest_texcoord_4(unsigned wrapMode
, const float s
[4], unsigned size
,
125 case PIPE_TEX_WRAP_REPEAT
:
126 /* s limited to [0,1) */
127 /* i limited to [0,size-1] */
128 for (ch
= 0; ch
< 4; ch
++) {
129 int i
= util_ifloor(s
[ch
] * size
);
130 icoord
[ch
] = REMAINDER(i
, size
);
133 case PIPE_TEX_WRAP_CLAMP
:
134 /* s limited to [0,1] */
135 /* i limited to [0,size-1] */
136 for (ch
= 0; ch
< 4; ch
++) {
139 else if (s
[ch
] >= 1.0F
)
140 icoord
[ch
] = size
- 1;
142 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
145 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
147 /* s limited to [min,max] */
148 /* i limited to [0, size-1] */
149 const float min
= 1.0F
/ (2.0F
* size
);
150 const float max
= 1.0F
- min
;
151 for (ch
= 0; ch
< 4; ch
++) {
154 else if (s
[ch
] > max
)
155 icoord
[ch
] = size
- 1;
157 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
161 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
163 /* s limited to [min,max] */
164 /* i limited to [-1, size] */
165 const float min
= -1.0F
/ (2.0F
* size
);
166 const float max
= 1.0F
- min
;
167 for (ch
= 0; ch
< 4; ch
++) {
170 else if (s
[ch
] >= max
)
173 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
177 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
179 const float min
= 1.0F
/ (2.0F
* size
);
180 const float max
= 1.0F
- min
;
181 for (ch
= 0; ch
< 4; ch
++) {
182 const int flr
= util_ifloor(s
[ch
]);
185 u
= 1.0F
- (s
[ch
] - (float) flr
);
187 u
= s
[ch
] - (float) flr
;
191 icoord
[ch
] = size
- 1;
193 icoord
[ch
] = util_ifloor(u
* size
);
197 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
198 for (ch
= 0; ch
< 4; ch
++) {
199 /* s limited to [0,1] */
200 /* i limited to [0,size-1] */
201 const float u
= fabsf(s
[ch
]);
205 icoord
[ch
] = size
- 1;
207 icoord
[ch
] = util_ifloor(u
* size
);
210 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
212 /* s limited to [min,max] */
213 /* i limited to [0, size-1] */
214 const float min
= 1.0F
/ (2.0F
* size
);
215 const float max
= 1.0F
- min
;
216 for (ch
= 0; ch
< 4; ch
++) {
217 const float u
= fabsf(s
[ch
]);
221 icoord
[ch
] = size
- 1;
223 icoord
[ch
] = util_ifloor(u
* size
);
227 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
229 /* s limited to [min,max] */
230 /* i limited to [0, size-1] */
231 const float min
= -1.0F
/ (2.0F
* size
);
232 const float max
= 1.0F
- min
;
233 for (ch
= 0; ch
< 4; ch
++) {
234 const float u
= fabsf(s
[ch
]);
240 icoord
[ch
] = util_ifloor(u
* size
);
251 * Used to compute texel locations for linear sampling for four texcoords.
252 * \param wrapMode PIPE_TEX_WRAP_x
253 * \param s the texcoords
254 * \param size the texture image size
255 * \param icoord0 returns first texture indexes
256 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
257 * \param w returns blend factor/weight between texture indexes
258 * \param icoord returns the computed integer texture coords
261 linear_texcoord_4(unsigned wrapMode
, const float s
[4], unsigned size
,
262 int icoord0
[4], int icoord1
[4], float w
[4])
267 case PIPE_TEX_WRAP_REPEAT
:
268 for (ch
= 0; ch
< 4; ch
++) {
269 float u
= s
[ch
] * size
- 0.5F
;
270 icoord0
[ch
] = REMAINDER(util_ifloor(u
), size
);
271 icoord1
[ch
] = REMAINDER(icoord0
[ch
] + 1, size
);
275 case PIPE_TEX_WRAP_CLAMP
:
276 for (ch
= 0; ch
< 4; ch
++) {
277 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
279 icoord0
[ch
] = util_ifloor(u
);
280 icoord1
[ch
] = icoord0
[ch
] + 1;
284 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
285 for (ch
= 0; ch
< 4; ch
++) {
286 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
288 icoord0
[ch
] = util_ifloor(u
);
289 icoord1
[ch
] = icoord0
[ch
] + 1;
292 if (icoord1
[ch
] >= (int) size
)
293 icoord1
[ch
] = size
- 1;
297 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
299 const float min
= -1.0F
/ (2.0F
* size
);
300 const float max
= 1.0F
- min
;
301 for (ch
= 0; ch
< 4; ch
++) {
302 float u
= CLAMP(s
[ch
], min
, max
);
304 icoord0
[ch
] = util_ifloor(u
);
305 icoord1
[ch
] = icoord0
[ch
] + 1;
310 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
311 for (ch
= 0; ch
< 4; ch
++) {
312 const int flr
= util_ifloor(s
[ch
]);
315 u
= 1.0F
- (s
[ch
] - (float) flr
);
317 u
= s
[ch
] - (float) flr
;
319 icoord0
[ch
] = util_ifloor(u
);
320 icoord1
[ch
] = icoord0
[ch
] + 1;
323 if (icoord1
[ch
] >= (int) size
)
324 icoord1
[ch
] = size
- 1;
328 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
329 for (ch
= 0; ch
< 4; ch
++) {
330 float u
= fabsf(s
[ch
]);
336 icoord0
[ch
] = util_ifloor(u
);
337 icoord1
[ch
] = icoord0
[ch
] + 1;
341 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
342 for (ch
= 0; ch
< 4; ch
++) {
343 float u
= fabsf(s
[ch
]);
349 icoord0
[ch
] = util_ifloor(u
);
350 icoord1
[ch
] = icoord0
[ch
] + 1;
353 if (icoord1
[ch
] >= (int) size
)
354 icoord1
[ch
] = size
- 1;
358 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
360 const float min
= -1.0F
/ (2.0F
* size
);
361 const float max
= 1.0F
- min
;
362 for (ch
= 0; ch
< 4; ch
++) {
363 float u
= fabsf(s
[ch
]);
371 icoord0
[ch
] = util_ifloor(u
);
372 icoord1
[ch
] = icoord0
[ch
] + 1;
384 * For RECT textures / unnormalized texcoords
385 * Only a subset of wrap modes supported.
388 nearest_texcoord_unnorm_4(unsigned wrapMode
, const float s
[4], unsigned size
,
393 case PIPE_TEX_WRAP_CLAMP
:
394 for (ch
= 0; ch
< 4; ch
++) {
395 int i
= util_ifloor(s
[ch
]);
396 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
399 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
401 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
402 for (ch
= 0; ch
< 4; ch
++) {
403 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
413 * For RECT textures / unnormalized texcoords.
414 * Only a subset of wrap modes supported.
417 linear_texcoord_unnorm_4(unsigned wrapMode
, const float s
[4], unsigned size
,
418 int icoord0
[4], int icoord1
[4], float w
[4])
422 case PIPE_TEX_WRAP_CLAMP
:
423 for (ch
= 0; ch
< 4; ch
++) {
424 /* Not exactly what the spec says, but it matches NVIDIA output */
425 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
426 icoord0
[ch
] = util_ifloor(u
);
427 icoord1
[ch
] = icoord0
[ch
] + 1;
431 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
433 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
434 for (ch
= 0; ch
< 4; ch
++) {
435 float u
= CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
);
437 icoord0
[ch
] = util_ifloor(u
);
438 icoord1
[ch
] = icoord0
[ch
] + 1;
439 if (icoord1
[ch
] > (int) size
- 1)
440 icoord1
[ch
] = size
- 1;
451 choose_cube_face(float rx
, float ry
, float rz
, float *newS
, float *newT
)
455 direction target sc tc ma
456 ---------- ------------------------------- --- --- ---
457 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
458 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
459 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
460 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
461 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
462 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
464 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
468 if (arx
> ary
&& arx
> arz
) {
470 face
= PIPE_TEX_FACE_POS_X
;
476 face
= PIPE_TEX_FACE_NEG_X
;
482 else if (ary
> arx
&& ary
> arz
) {
484 face
= PIPE_TEX_FACE_POS_Y
;
490 face
= PIPE_TEX_FACE_NEG_Y
;
498 face
= PIPE_TEX_FACE_POS_Z
;
504 face
= PIPE_TEX_FACE_NEG_Z
;
511 *newS
= ( sc
/ ma
+ 1.0F
) * 0.5F
;
512 *newT
= ( tc
/ ma
+ 1.0F
) * 0.5F
;
519 * Examine the quad's texture coordinates to compute the partial
520 * derivatives w.r.t X and Y, then compute lambda (level of detail).
522 * This is only done for fragment shaders, not vertex shaders.
525 compute_lambda(struct tgsi_sampler
*tgsi_sampler
,
526 const float s
[QUAD_SIZE
],
527 const float t
[QUAD_SIZE
],
528 const float p
[QUAD_SIZE
],
531 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
532 const struct pipe_texture
*texture
= samp
->texture
;
533 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
536 if (samp
->processor
== TGSI_PROCESSOR_VERTEX
)
539 assert(sampler
->normalized_coords
);
543 float dsdx
= s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
];
544 float dsdy
= s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
];
547 rho
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
550 float dtdx
= t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
];
551 float dtdy
= t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
];
555 max
= MAX2(dtdx
, dtdy
) * texture
->height
[0];
556 rho
= MAX2(rho
, max
);
559 float dpdx
= p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
];
560 float dpdy
= p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
];
564 max
= MAX2(dpdx
, dpdy
) * texture
->depth
[0];
565 rho
= MAX2(rho
, max
);
568 lambda
= util_fast_log2(rho
);
569 lambda
+= lodbias
+ sampler
->lod_bias
;
570 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
577 * Do several things here:
578 * 1. Compute lambda from the texcoords, if needed
579 * 2. Determine if we're minifying or magnifying
580 * 3. If minifying, choose mipmap levels
581 * 4. Return image filter to use within mipmap images
582 * \param level0 Returns first mipmap level to sample from
583 * \param level1 Returns second mipmap level to sample from
584 * \param levelBlend Returns blend factor between levels, in [0,1]
585 * \param imgFilter Returns either the min or mag filter, depending on lambda
588 choose_mipmap_levels(struct tgsi_sampler
*tgsi_sampler
,
589 const float s
[QUAD_SIZE
],
590 const float t
[QUAD_SIZE
],
591 const float p
[QUAD_SIZE
],
593 unsigned *level0
, unsigned *level1
, float *levelBlend
,
596 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
597 const struct pipe_texture
*texture
= samp
->texture
;
598 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
600 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
601 /* no mipmap selection needed */
602 *level0
= *level1
= CLAMP((int) sampler
->min_lod
,
603 0, (int) texture
->last_level
);
605 if (sampler
->min_img_filter
!= sampler
->mag_img_filter
) {
606 /* non-mipmapped texture, but still need to determine if doing
607 * minification or magnification.
609 float lambda
= compute_lambda(tgsi_sampler
, s
, t
, p
, lodbias
);
611 *imgFilter
= sampler
->mag_img_filter
;
614 *imgFilter
= sampler
->min_img_filter
;
618 *imgFilter
= sampler
->mag_img_filter
;
622 float lambda
= compute_lambda(tgsi_sampler
, s
, t
, p
, lodbias
);
624 if (lambda
<= 0.0) { /* XXX threshold depends on the filter */
626 *imgFilter
= sampler
->mag_img_filter
;
627 *level0
= *level1
= 0;
631 *imgFilter
= sampler
->min_img_filter
;
633 /* choose mipmap level(s) and compute the blend factor between them */
634 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
635 /* Nearest mipmap level */
636 const int lvl
= (int) (lambda
+ 0.5);
638 *level1
= CLAMP(lvl
, 0, (int) texture
->last_level
);
641 /* Linear interpolation between mipmap levels */
642 const int lvl
= (int) lambda
;
643 *level0
= CLAMP(lvl
, 0, (int) texture
->last_level
);
644 *level1
= CLAMP(lvl
+ 1, 0, (int) texture
->last_level
);
645 *levelBlend
= FRAC(lambda
); /* blending weight between levels */
653 * Get a texel from a texture, using the texture tile cache.
655 * \param face the cube face in 0..5
656 * \param level the mipmap level
657 * \param x the x coord of texel within 2D image
658 * \param y the y coord of texel within 2D image
659 * \param z which slice of a 3D texture
660 * \param rgba the quad to put the texel/color into
661 * \param j which element of the rgba quad to write to
663 * XXX maybe move this into sp_tile_cache.c and merge with the
664 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
667 get_texel_quad_2d(const struct tgsi_sampler
*tgsi_sampler
,
668 unsigned face
, unsigned level
, int x
, int y
,
671 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
673 const struct softpipe_cached_tile
*tile
674 = sp_get_cached_tile_tex(samp
->cache
,
675 tile_address(x
, y
, 0, face
, level
));
680 out
[0] = &tile
->data
.color
[y
][x
][0];
681 out
[1] = &tile
->data
.color
[y
][x
+1][0];
682 out
[2] = &tile
->data
.color
[y
+1][x
][0];
683 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
686 static INLINE
const float *
687 get_texel_2d_ptr(const struct tgsi_sampler
*tgsi_sampler
,
688 unsigned face
, unsigned level
, int x
, int y
)
690 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
692 const struct softpipe_cached_tile
*tile
693 = sp_get_cached_tile_tex(samp
->cache
,
694 tile_address(x
, y
, 0, face
, level
));
699 return &tile
->data
.color
[y
][x
][0];
704 get_texel_quad_2d_mt(const struct tgsi_sampler
*tgsi_sampler
,
705 unsigned face
, unsigned level
,
712 for (i
= 0; i
< 4; i
++) {
713 unsigned tx
= (i
& 1) ? x1
: x0
;
714 unsigned ty
= (i
>> 1) ? y1
: y0
;
716 out
[i
] = get_texel_2d_ptr( tgsi_sampler
, face
, level
, tx
, ty
);
721 get_texel(const struct tgsi_sampler
*tgsi_sampler
,
722 unsigned face
, unsigned level
, int x
, int y
, int z
,
723 float rgba
[NUM_CHANNELS
][QUAD_SIZE
], unsigned j
)
725 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
726 const struct pipe_texture
*texture
= samp
->texture
;
727 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
729 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
730 y
< 0 || y
>= (int) texture
->height
[level
] ||
731 z
< 0 || z
>= (int) texture
->depth
[level
]) {
732 rgba
[0][j
] = sampler
->border_color
[0];
733 rgba
[1][j
] = sampler
->border_color
[1];
734 rgba
[2][j
] = sampler
->border_color
[2];
735 rgba
[3][j
] = sampler
->border_color
[3];
738 const unsigned tx
= x
% TILE_SIZE
;
739 const unsigned ty
= y
% TILE_SIZE
;
740 const struct softpipe_cached_tile
*tile
;
742 tile
= sp_get_cached_tile_tex(samp
->cache
,
743 tile_address(x
, y
, z
, face
, level
));
745 rgba
[0][j
] = tile
->data
.color
[ty
][tx
][0];
746 rgba
[1][j
] = tile
->data
.color
[ty
][tx
][1];
747 rgba
[2][j
] = tile
->data
.color
[ty
][tx
][2];
748 rgba
[3][j
] = tile
->data
.color
[ty
][tx
][3];
751 debug_printf("Get texel %f %f %f %f from %s\n",
752 rgba
[0][j
], rgba
[1][j
], rgba
[2][j
], rgba
[3][j
],
753 pf_name(texture
->format
));
760 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
761 * When we sampled the depth texture, the depth value was put into all
762 * RGBA channels. We look at the red channel here.
763 * \param rgba quad of (depth) texel values
764 * \param p texture 'P' components for four pixels in quad
765 * \param j which pixel in the quad to test [0..3]
768 shadow_compare(const struct pipe_sampler_state
*sampler
,
769 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
770 const float p
[QUAD_SIZE
],
774 switch (sampler
->compare_func
) {
776 k
= p
[j
] < rgba
[0][j
];
778 case PIPE_FUNC_LEQUAL
:
779 k
= p
[j
] <= rgba
[0][j
];
781 case PIPE_FUNC_GREATER
:
782 k
= p
[j
] > rgba
[0][j
];
784 case PIPE_FUNC_GEQUAL
:
785 k
= p
[j
] >= rgba
[0][j
];
787 case PIPE_FUNC_EQUAL
:
788 k
= p
[j
] == rgba
[0][j
];
790 case PIPE_FUNC_NOTEQUAL
:
791 k
= p
[j
] != rgba
[0][j
];
793 case PIPE_FUNC_ALWAYS
:
796 case PIPE_FUNC_NEVER
:
805 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
806 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = (float) k
;
812 * As above, but do four z/texture comparisons.
815 shadow_compare4(const struct pipe_sampler_state
*sampler
,
816 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
817 const float p
[QUAD_SIZE
])
819 int j
, k0
, k1
, k2
, k3
;
822 /* compare four texcoords vs. four texture samples */
823 switch (sampler
->compare_func
) {
825 k0
= p
[0] < rgba
[0][0];
826 k1
= p
[1] < rgba
[0][1];
827 k2
= p
[2] < rgba
[0][2];
828 k3
= p
[3] < rgba
[0][3];
830 case PIPE_FUNC_LEQUAL
:
831 k0
= p
[0] <= rgba
[0][0];
832 k1
= p
[1] <= rgba
[0][1];
833 k2
= p
[2] <= rgba
[0][2];
834 k3
= p
[3] <= rgba
[0][3];
836 case PIPE_FUNC_GREATER
:
837 k0
= p
[0] > rgba
[0][0];
838 k1
= p
[1] > rgba
[0][1];
839 k2
= p
[2] > rgba
[0][2];
840 k3
= p
[3] > rgba
[0][3];
842 case PIPE_FUNC_GEQUAL
:
843 k0
= p
[0] >= rgba
[0][0];
844 k1
= p
[1] >= rgba
[0][1];
845 k2
= p
[2] >= rgba
[0][2];
846 k3
= p
[3] >= rgba
[0][3];
848 case PIPE_FUNC_EQUAL
:
849 k0
= p
[0] == rgba
[0][0];
850 k1
= p
[1] == rgba
[0][1];
851 k2
= p
[2] == rgba
[0][2];
852 k3
= p
[3] == rgba
[0][3];
854 case PIPE_FUNC_NOTEQUAL
:
855 k0
= p
[0] != rgba
[0][0];
856 k1
= p
[1] != rgba
[0][1];
857 k2
= p
[2] != rgba
[0][2];
858 k3
= p
[3] != rgba
[0][3];
860 case PIPE_FUNC_ALWAYS
:
861 k0
= k1
= k2
= k3
= 1;
863 case PIPE_FUNC_NEVER
:
864 k0
= k1
= k2
= k3
= 0;
867 k0
= k1
= k2
= k3
= 0;
872 /* convert four pass/fail values to an intensity in [0,1] */
873 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
875 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
876 for (j
= 0; j
< 4; j
++) {
877 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
885 sp_get_samples_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
886 const float s
[QUAD_SIZE
],
887 const float t
[QUAD_SIZE
],
888 const float p
[QUAD_SIZE
],
890 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
892 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
894 unsigned level
= samp
->level
;
895 unsigned xpot
= 1 << (samp
->xpot
- level
);
896 unsigned ypot
= 1 << (samp
->ypot
- level
);
897 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
898 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
900 for (j
= 0; j
< QUAD_SIZE
; j
++) {
903 float u
= s
[j
] * xpot
- 0.5F
;
904 float v
= t
[j
] * ypot
- 0.5F
;
906 int uflr
= util_ifloor(u
);
907 int vflr
= util_ifloor(v
);
909 float xw
= u
- (float)uflr
;
910 float yw
= v
- (float)vflr
;
912 int x0
= uflr
& (xpot
- 1);
913 int y0
= vflr
& (ypot
- 1);
918 /* Can we fetch all four at once:
920 if (x0
< xmax
&& y0
< ymax
)
922 get_texel_quad_2d(tgsi_sampler
, 0, level
, x0
, y0
, tx
);
926 unsigned x1
= (x0
+ 1) & (xpot
- 1);
927 unsigned y1
= (y0
+ 1) & (ypot
- 1);
928 get_texel_quad_2d_mt(tgsi_sampler
, 0, level
,
933 /* interpolate R, G, B, A */
934 for (c
= 0; c
< 4; c
++) {
935 rgba
[c
][j
] = lerp_2d(xw
, yw
,
944 sp_get_samples_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
945 const float s
[QUAD_SIZE
],
946 const float t
[QUAD_SIZE
],
947 const float p
[QUAD_SIZE
],
949 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
951 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
953 unsigned level
= samp
->level
;
954 unsigned xpot
= 1 << (samp
->xpot
- level
);
955 unsigned ypot
= 1 << (samp
->ypot
- level
);
957 for (j
= 0; j
< QUAD_SIZE
; j
++) {
960 float u
= s
[j
] * xpot
;
961 float v
= t
[j
] * ypot
;
963 int uflr
= util_ifloor(u
);
964 int vflr
= util_ifloor(v
);
966 int x0
= uflr
& (xpot
- 1);
967 int y0
= vflr
& (ypot
- 1);
969 const float *out
= get_texel_2d_ptr(tgsi_sampler
, 0, level
, x0
, y0
);
971 for (c
= 0; c
< 4; c
++) {
979 sp_get_samples_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
980 const float s
[QUAD_SIZE
],
981 const float t
[QUAD_SIZE
],
982 const float p
[QUAD_SIZE
],
984 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
986 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
988 unsigned level
= samp
->level
;
989 unsigned xpot
= 1 << (samp
->xpot
- level
);
990 unsigned ypot
= 1 << (samp
->ypot
- level
);
992 for (j
= 0; j
< QUAD_SIZE
; j
++) {
995 float u
= s
[j
] * xpot
;
996 float v
= t
[j
] * ypot
;
1001 x0
= util_ifloor(u
);
1004 else if (x0
> xpot
- 1)
1007 y0
= util_ifloor(v
);
1010 else if (y0
> ypot
- 1)
1013 out
= get_texel_2d_ptr(tgsi_sampler
, 0, level
, x0
, y0
);
1015 for (c
= 0; c
< 4; c
++) {
1016 rgba
[c
][j
] = out
[c
];
1023 sp_get_samples_2d_linear_mip_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
1024 const float s
[QUAD_SIZE
],
1025 const float t
[QUAD_SIZE
],
1026 const float p
[QUAD_SIZE
],
1028 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1030 struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1031 const struct pipe_texture
*texture
= samp
->texture
;
1035 lambda
= compute_lambda(tgsi_sampler
, s
, t
, p
, lodbias
);
1036 level0
= (int)lambda
;
1040 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1043 else if (level0
>= texture
->last_level
) {
1044 samp
->level
= texture
->last_level
;
1045 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1049 float levelBlend
= lambda
- level0
;
1054 samp
->level
= level0
;
1055 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1056 s
, t
, p
, 0, rgba0
);
1058 samp
->level
= level0
+1;
1059 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1060 s
, t
, p
, 0, rgba1
);
1062 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1063 for (c
= 0; c
< 4; c
++) {
1064 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1071 * Common code for sampling 1D/2D/cube textures.
1072 * Could probably extend for 3D...
1075 sp_get_samples_2d_common(struct tgsi_sampler
*tgsi_sampler
,
1076 const float s
[QUAD_SIZE
],
1077 const float t
[QUAD_SIZE
],
1078 const float p
[QUAD_SIZE
],
1080 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
1081 const unsigned faces
[4])
1083 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1084 const struct pipe_texture
*texture
= samp
->texture
;
1085 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1086 unsigned level0
, level1
, j
, imgFilter
;
1090 choose_mipmap_levels(tgsi_sampler
, s
, t
, p
,
1092 &level0
, &level1
, &levelBlend
, &imgFilter
);
1094 assert(sampler
->normalized_coords
);
1096 width
= texture
->width
[level0
];
1097 height
= texture
->height
[level0
];
1101 switch (imgFilter
) {
1102 case PIPE_TEX_FILTER_NEAREST
:
1105 nearest_texcoord_4(sampler
->wrap_s
, s
, width
, x
);
1106 nearest_texcoord_4(sampler
->wrap_t
, t
, height
, y
);
1108 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1109 get_texel(tgsi_sampler
, faces
[j
], level0
, x
[j
], y
[j
], 0, rgba
, j
);
1110 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1111 shadow_compare(sampler
, rgba
, p
, j
);
1114 if (level0
!= level1
) {
1115 /* get texels from second mipmap level and blend */
1120 get_texel(tgsi_sampler
, faces
[j
], level1
, x
[j
], y
[j
], 0,
1122 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
1123 shadow_compare(sampler
, rgba2
, p
, j
);
1126 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1127 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1133 case PIPE_TEX_FILTER_LINEAR
:
1134 case PIPE_TEX_FILTER_ANISO
:
1136 int x0
[4], y0
[4], x1
[4], y1
[4];
1137 float xw
[4], yw
[4]; /* weights */
1139 linear_texcoord_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1140 linear_texcoord_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1142 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1143 float tx
[4][4]; /* texels */
1145 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
[j
], y0
[j
], 0, tx
, 0);
1146 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
[j
], y0
[j
], 0, tx
, 1);
1147 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
[j
], y1
[j
], 0, tx
, 2);
1148 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
[j
], y1
[j
], 0, tx
, 3);
1149 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1150 shadow_compare4(sampler
, tx
, p
);
1153 /* interpolate R, G, B, A */
1154 for (c
= 0; c
< 4; c
++) {
1155 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1157 tx
[c
][2], tx
[c
][3]);
1160 if (level0
!= level1
) {
1161 /* get texels from second mipmap level and blend */
1164 /* XXX: This is incorrect -- will often end up with (x0
1165 * == x1 && y0 == y1), meaning that we fetch the same
1166 * texel four times and linearly interpolate between
1167 * identical values. The correct approach would be to
1168 * call linear_texcoord again for the second level.
1174 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
[j
], y0
[j
], 0, tx
, 0);
1175 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
[j
], y0
[j
], 0, tx
, 1);
1176 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
[j
], y1
[j
], 0, tx
, 2);
1177 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
[j
], y1
[j
], 0, tx
, 3);
1178 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
1179 shadow_compare4(sampler
, tx
, p
);
1182 /* interpolate R, G, B, A */
1183 for (c
= 0; c
< 4; c
++) {
1184 rgba2
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1185 tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
1188 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1189 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1202 sp_get_samples_1d(struct tgsi_sampler
*sampler
,
1203 const float s
[QUAD_SIZE
],
1204 const float t
[QUAD_SIZE
],
1205 const float p
[QUAD_SIZE
],
1207 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1209 static const unsigned faces
[4] = {0, 0, 0, 0};
1210 static const float tzero
[4] = {0, 0, 0, 0};
1211 sp_get_samples_2d_common(sampler
, s
, tzero
, NULL
,
1212 lodbias
, rgba
, faces
);
1217 sp_get_samples_2d(struct tgsi_sampler
*sampler
,
1218 const float s
[QUAD_SIZE
],
1219 const float t
[QUAD_SIZE
],
1220 const float p
[QUAD_SIZE
],
1222 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1224 static const unsigned faces
[4] = {0, 0, 0, 0};
1225 sp_get_samples_2d_common(sampler
, s
, t
, p
,
1226 lodbias
, rgba
, faces
);
1231 sp_get_samples_3d(struct tgsi_sampler
*tgsi_sampler
,
1232 const float s
[QUAD_SIZE
],
1233 const float t
[QUAD_SIZE
],
1234 const float p
[QUAD_SIZE
],
1236 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1238 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1239 const struct pipe_texture
*texture
= samp
->texture
;
1240 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1241 /* get/map pipe_surfaces corresponding to 3D tex slices */
1242 unsigned level0
, level1
, j
, imgFilter
;
1243 int width
, height
, depth
;
1245 const uint face
= 0;
1247 choose_mipmap_levels(tgsi_sampler
, s
, t
, p
,
1249 &level0
, &level1
, &levelBlend
, &imgFilter
);
1251 assert(sampler
->normalized_coords
);
1253 width
= texture
->width
[level0
];
1254 height
= texture
->height
[level0
];
1255 depth
= texture
->depth
[level0
];
1261 switch (imgFilter
) {
1262 case PIPE_TEX_FILTER_NEAREST
:
1264 int x
[4], y
[4], z
[4];
1265 nearest_texcoord_4(sampler
->wrap_s
, s
, width
, x
);
1266 nearest_texcoord_4(sampler
->wrap_t
, t
, height
, y
);
1267 nearest_texcoord_4(sampler
->wrap_r
, p
, depth
, z
);
1268 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1269 get_texel(tgsi_sampler
, face
, level0
, x
[j
], y
[j
], z
[j
], rgba
, j
);
1270 if (level0
!= level1
) {
1271 /* get texels from second mipmap level and blend */
1277 get_texel(tgsi_sampler
, face
, level1
, x
[j
], y
[j
], z
[j
], rgba2
, j
);
1278 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1279 rgba
[c
][j
] = lerp(levelBlend
, rgba2
[c
][j
], rgba
[c
][j
]);
1285 case PIPE_TEX_FILTER_LINEAR
:
1286 case PIPE_TEX_FILTER_ANISO
:
1288 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1289 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1290 linear_texcoord_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1291 linear_texcoord_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1292 linear_texcoord_4(sampler
->wrap_r
, p
, depth
, z0
, z1
, zw
);
1294 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1296 float tx0
[4][4], tx1
[4][4];
1297 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], z0
[j
], tx0
, 0);
1298 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], z0
[j
], tx0
, 1);
1299 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], z0
[j
], tx0
, 2);
1300 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], z0
[j
], tx0
, 3);
1301 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], z1
[j
], tx1
, 0);
1302 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], z1
[j
], tx1
, 1);
1303 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], z1
[j
], tx1
, 2);
1304 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], z1
[j
], tx1
, 3);
1306 /* interpolate R, G, B, A */
1307 for (c
= 0; c
< 4; c
++) {
1308 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1309 tx0
[c
][0], tx0
[c
][1],
1310 tx0
[c
][2], tx0
[c
][3],
1311 tx1
[c
][0], tx1
[c
][1],
1312 tx1
[c
][2], tx1
[c
][3]);
1315 if (level0
!= level1
) {
1316 /* get texels from second mipmap level and blend */
1324 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y0
[j
], z0
[j
], tx0
, 0);
1325 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y0
[j
], z0
[j
], tx0
, 1);
1326 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y1
[j
], z0
[j
], tx0
, 2);
1327 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y1
[j
], z0
[j
], tx0
, 3);
1328 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y0
[j
], z1
[j
], tx1
, 0);
1329 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y0
[j
], z1
[j
], tx1
, 1);
1330 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y1
[j
], z1
[j
], tx1
, 2);
1331 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y1
[j
], z1
[j
], tx1
, 3);
1333 /* interpolate R, G, B, A */
1334 for (c
= 0; c
< 4; c
++) {
1335 rgba2
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1336 tx0
[c
][0], tx0
[c
][1],
1337 tx0
[c
][2], tx0
[c
][3],
1338 tx1
[c
][0], tx1
[c
][1],
1339 tx1
[c
][2], tx1
[c
][3]);
1342 /* blend mipmap levels */
1343 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1344 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1357 sp_get_samples_cube(struct tgsi_sampler
*sampler
,
1358 const float s
[QUAD_SIZE
],
1359 const float t
[QUAD_SIZE
],
1360 const float p
[QUAD_SIZE
],
1362 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1364 unsigned faces
[QUAD_SIZE
], j
;
1365 float ssss
[4], tttt
[4];
1366 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1367 faces
[j
] = choose_cube_face(s
[j
], t
[j
], p
[j
], ssss
+ j
, tttt
+ j
);
1369 sp_get_samples_2d_common(sampler
, ssss
, tttt
, NULL
,
1370 lodbias
, rgba
, faces
);
1375 sp_get_samples_rect(struct tgsi_sampler
*tgsi_sampler
,
1376 const float s
[QUAD_SIZE
],
1377 const float t
[QUAD_SIZE
],
1378 const float p
[QUAD_SIZE
],
1380 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1382 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1383 const struct pipe_texture
*texture
= samp
->texture
;
1384 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1385 const uint face
= 0;
1386 unsigned level0
, level1
, j
, imgFilter
;
1390 choose_mipmap_levels(tgsi_sampler
, s
, t
, p
,
1392 &level0
, &level1
, &levelBlend
, &imgFilter
);
1394 /* texture RECTS cannot be mipmapped */
1395 assert(level0
== level1
);
1397 width
= texture
->width
[level0
];
1398 height
= texture
->height
[level0
];
1402 switch (imgFilter
) {
1403 case PIPE_TEX_FILTER_NEAREST
:
1406 nearest_texcoord_unnorm_4(sampler
->wrap_s
, s
, width
, x
);
1407 nearest_texcoord_unnorm_4(sampler
->wrap_t
, t
, height
, y
);
1408 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1409 get_texel(tgsi_sampler
, face
, level0
, x
[j
], y
[j
], 0, rgba
, j
);
1410 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1411 shadow_compare(sampler
, rgba
, p
, j
);
1416 case PIPE_TEX_FILTER_LINEAR
:
1417 case PIPE_TEX_FILTER_ANISO
:
1419 int x0
[4], y0
[4], x1
[4], y1
[4];
1420 float xw
[4], yw
[4]; /* weights */
1421 linear_texcoord_unnorm_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1422 linear_texcoord_unnorm_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1423 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1424 float tx
[4][4]; /* texels */
1426 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], 0, tx
, 0);
1427 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], 0, tx
, 1);
1428 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], 0, tx
, 2);
1429 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], 0, tx
, 3);
1430 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1431 shadow_compare4(sampler
, tx
, p
);
1433 for (c
= 0; c
< 4; c
++) {
1434 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1435 tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
1447 * Error condition handler
1450 sp_get_samples_null(struct tgsi_sampler
*tgsi_sampler
,
1451 const float s
[QUAD_SIZE
],
1452 const float t
[QUAD_SIZE
],
1453 const float p
[QUAD_SIZE
],
1455 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1459 for (i
= 0; i
< 4; i
++)
1460 for (j
= 0; j
< 4; j
++)
1465 * Called via tgsi_sampler::get_samples() when using a sampler for the
1466 * first time. Determine the actual sampler function, link it in and
1470 sp_get_samples(struct tgsi_sampler
*tgsi_sampler
,
1471 const float s
[QUAD_SIZE
],
1472 const float t
[QUAD_SIZE
],
1473 const float p
[QUAD_SIZE
],
1475 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1477 struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1478 const struct pipe_texture
*texture
= samp
->texture
;
1479 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1481 /* Default to the 'undefined' case:
1483 tgsi_sampler
->get_samples
= sp_get_samples_null
;
1486 assert(0); /* is this legal?? */
1490 if (!sampler
->normalized_coords
) {
1491 assert (texture
->target
== PIPE_TEXTURE_2D
);
1492 tgsi_sampler
->get_samples
= sp_get_samples_rect
;
1496 switch (texture
->target
) {
1497 case PIPE_TEXTURE_1D
:
1498 tgsi_sampler
->get_samples
= sp_get_samples_1d
;
1500 case PIPE_TEXTURE_2D
:
1501 tgsi_sampler
->get_samples
= sp_get_samples_2d
;
1503 case PIPE_TEXTURE_3D
:
1504 tgsi_sampler
->get_samples
= sp_get_samples_3d
;
1506 case PIPE_TEXTURE_CUBE
:
1507 tgsi_sampler
->get_samples
= sp_get_samples_cube
;
1514 /* Do this elsewhere:
1516 samp
->xpot
= util_unsigned_logbase2( samp
->texture
->width
[0] );
1517 samp
->ypot
= util_unsigned_logbase2( samp
->texture
->height
[0] );
1519 /* Try to hook in a faster sampler. Ultimately we'll have to
1520 * code-generate these. Luckily most of this looks like it is
1521 * orthogonal state within the sampler.
1523 if (texture
->target
== PIPE_TEXTURE_2D
&&
1524 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1525 sampler
->wrap_s
== sampler
->wrap_t
&&
1526 sampler
->compare_mode
== FALSE
&&
1527 sampler
->normalized_coords
)
1529 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1530 samp
->level
= CLAMP((int) sampler
->min_lod
,
1531 0, (int) texture
->last_level
);
1533 if (sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
) {
1534 switch (sampler
->min_img_filter
) {
1535 case PIPE_TEX_FILTER_NEAREST
:
1536 tgsi_sampler
->get_samples
= sp_get_samples_2d_nearest_repeat_POT
;
1538 case PIPE_TEX_FILTER_LINEAR
:
1539 tgsi_sampler
->get_samples
= sp_get_samples_2d_linear_repeat_POT
;
1545 else if (sampler
->wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
1546 switch (sampler
->min_img_filter
) {
1547 case PIPE_TEX_FILTER_NEAREST
:
1548 tgsi_sampler
->get_samples
= sp_get_samples_2d_nearest_clamp_POT
;
1555 else if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1556 if (sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
) {
1557 switch (sampler
->min_img_filter
) {
1558 case PIPE_TEX_FILTER_LINEAR
:
1559 tgsi_sampler
->get_samples
= sp_get_samples_2d_linear_mip_linear_repeat_POT
;
1568 _debug_printf("target %d/%d min_mip %d/%d min_img %d/%d wrap %d/%d compare %d/%d norm %d/%d\n",
1569 texture
->target
, PIPE_TEXTURE_2D
,
1570 sampler
->min_mip_filter
, PIPE_TEX_MIPFILTER_NONE
,
1571 sampler
->min_img_filter
, sampler
->mag_img_filter
,
1572 sampler
->wrap_s
, sampler
->wrap_t
,
1573 sampler
->compare_mode
, FALSE
,
1574 sampler
->normalized_coords
, TRUE
);
1578 tgsi_sampler
->get_samples( tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);