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 "util/u_math.h"
45 #include "util/u_memory.h"
50 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
51 * see 1-pixel bands of improperly weighted linear-filtered textures.
52 * The tests/texwrap.c demo is a good test.
53 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
54 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
56 #define FRAC(f) ((f) - util_ifloor(f))
60 * Linear interpolation macro
63 lerp(float a
, float v0
, float v1
)
65 return v0
+ a
* (v1
- v0
);
70 * Do 2D/biliner interpolation of float values.
71 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
72 * a and b are the horizontal and vertical interpolants.
73 * It's important that this function is inlined when compiled with
74 * optimization! If we find that's not true on some systems, convert
78 lerp_2d(float a
, float b
,
79 float v00
, float v10
, float v01
, float v11
)
81 const float temp0
= lerp(a
, v00
, v10
);
82 const float temp1
= lerp(a
, v01
, v11
);
83 return lerp(b
, temp0
, temp1
);
88 * As above, but 3D interpolation of 8 values.
91 lerp_3d(float a
, float b
, float c
,
92 float v000
, float v100
, float v010
, float v110
,
93 float v001
, float v101
, float v011
, float v111
)
95 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
96 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
97 return lerp(c
, temp0
, temp1
);
103 * If A is a signed integer, A % B doesn't give the right value for A < 0
104 * (in terms of texture repeat). Just casting to unsigned fixes that.
106 #define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
110 * Apply texture coord wrapping mode and return integer texture indexes
111 * for a vector of four texcoords (S or T or P).
112 * \param wrapMode PIPE_TEX_WRAP_x
113 * \param s the incoming texcoords
114 * \param size the texture image size
115 * \param icoord returns the integer texcoords
116 * \return integer texture index
119 nearest_texcoord_4(unsigned wrapMode
, const float s
[4], unsigned size
,
124 case PIPE_TEX_WRAP_REPEAT
:
125 /* s limited to [0,1) */
126 /* i limited to [0,size-1] */
127 for (ch
= 0; ch
< 4; ch
++) {
128 int i
= util_ifloor(s
[ch
] * size
);
129 icoord
[ch
] = REMAINDER(i
, size
);
132 case PIPE_TEX_WRAP_CLAMP
:
133 /* s limited to [0,1] */
134 /* i limited to [0,size-1] */
135 for (ch
= 0; ch
< 4; ch
++) {
138 else if (s
[ch
] >= 1.0F
)
139 icoord
[ch
] = size
- 1;
141 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
144 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
146 /* s limited to [min,max] */
147 /* i limited to [0, size-1] */
148 const float min
= 1.0F
/ (2.0F
* size
);
149 const float max
= 1.0F
- min
;
150 for (ch
= 0; ch
< 4; ch
++) {
153 else if (s
[ch
] > max
)
154 icoord
[ch
] = size
- 1;
156 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
160 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
162 /* s limited to [min,max] */
163 /* i limited to [-1, size] */
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
)
172 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
176 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
178 const float min
= 1.0F
/ (2.0F
* size
);
179 const float max
= 1.0F
- min
;
180 for (ch
= 0; ch
< 4; ch
++) {
181 const int flr
= util_ifloor(s
[ch
]);
184 u
= 1.0F
- (s
[ch
] - (float) flr
);
186 u
= s
[ch
] - (float) flr
;
190 icoord
[ch
] = size
- 1;
192 icoord
[ch
] = util_ifloor(u
* size
);
196 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
197 for (ch
= 0; ch
< 4; ch
++) {
198 /* s limited to [0,1] */
199 /* i limited to [0,size-1] */
200 const float u
= fabsf(s
[ch
]);
204 icoord
[ch
] = size
- 1;
206 icoord
[ch
] = util_ifloor(u
* size
);
209 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
211 /* s limited to [min,max] */
212 /* i limited to [0, size-1] */
213 const float min
= 1.0F
/ (2.0F
* size
);
214 const float max
= 1.0F
- min
;
215 for (ch
= 0; ch
< 4; ch
++) {
216 const float u
= fabsf(s
[ch
]);
220 icoord
[ch
] = size
- 1;
222 icoord
[ch
] = util_ifloor(u
* size
);
226 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
228 /* s limited to [min,max] */
229 /* i limited to [0, size-1] */
230 const float min
= -1.0F
/ (2.0F
* size
);
231 const float max
= 1.0F
- min
;
232 for (ch
= 0; ch
< 4; ch
++) {
233 const float u
= fabsf(s
[ch
]);
239 icoord
[ch
] = util_ifloor(u
* size
);
250 * Used to compute texel locations for linear sampling for four texcoords.
251 * \param wrapMode PIPE_TEX_WRAP_x
252 * \param s the texcoords
253 * \param size the texture image size
254 * \param icoord0 returns first texture indexes
255 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
256 * \param w returns blend factor/weight between texture indexes
257 * \param icoord returns the computed integer texture coords
260 linear_texcoord_4(unsigned wrapMode
, const float s
[4], unsigned size
,
261 int icoord0
[4], int icoord1
[4], float w
[4])
266 case PIPE_TEX_WRAP_REPEAT
:
267 for (ch
= 0; ch
< 4; ch
++) {
268 float u
= s
[ch
] * size
- 0.5F
;
269 icoord0
[ch
] = REMAINDER(util_ifloor(u
), size
);
270 icoord1
[ch
] = REMAINDER(icoord0
[ch
] + 1, size
);
274 case PIPE_TEX_WRAP_CLAMP
:
275 for (ch
= 0; ch
< 4; ch
++) {
276 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
278 icoord0
[ch
] = util_ifloor(u
);
279 icoord1
[ch
] = icoord0
[ch
] + 1;
283 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
284 for (ch
= 0; ch
< 4; ch
++) {
285 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
287 icoord0
[ch
] = util_ifloor(u
);
288 icoord1
[ch
] = icoord0
[ch
] + 1;
291 if (icoord1
[ch
] >= (int) size
)
292 icoord1
[ch
] = size
- 1;
296 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
298 const float min
= -1.0F
/ (2.0F
* size
);
299 const float max
= 1.0F
- min
;
300 for (ch
= 0; ch
< 4; ch
++) {
301 float u
= CLAMP(s
[ch
], min
, max
);
303 icoord0
[ch
] = util_ifloor(u
);
304 icoord1
[ch
] = icoord0
[ch
] + 1;
309 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
310 for (ch
= 0; ch
< 4; ch
++) {
311 const int flr
= util_ifloor(s
[ch
]);
314 u
= 1.0F
- (s
[ch
] - (float) flr
);
316 u
= s
[ch
] - (float) flr
;
318 icoord0
[ch
] = util_ifloor(u
);
319 icoord1
[ch
] = icoord0
[ch
] + 1;
322 if (icoord1
[ch
] >= (int) size
)
323 icoord1
[ch
] = size
- 1;
327 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
328 for (ch
= 0; ch
< 4; ch
++) {
329 float u
= fabsf(s
[ch
]);
335 icoord0
[ch
] = util_ifloor(u
);
336 icoord1
[ch
] = icoord0
[ch
] + 1;
340 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
341 for (ch
= 0; ch
< 4; ch
++) {
342 float u
= fabsf(s
[ch
]);
348 icoord0
[ch
] = util_ifloor(u
);
349 icoord1
[ch
] = icoord0
[ch
] + 1;
352 if (icoord1
[ch
] >= (int) size
)
353 icoord1
[ch
] = size
- 1;
357 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
359 const float min
= -1.0F
/ (2.0F
* size
);
360 const float max
= 1.0F
- min
;
361 for (ch
= 0; ch
< 4; ch
++) {
362 float u
= fabsf(s
[ch
]);
370 icoord0
[ch
] = util_ifloor(u
);
371 icoord1
[ch
] = icoord0
[ch
] + 1;
383 * For RECT textures / unnormalized texcoords
384 * Only a subset of wrap modes supported.
387 nearest_texcoord_unnorm_4(unsigned wrapMode
, const float s
[4], unsigned size
,
392 case PIPE_TEX_WRAP_CLAMP
:
393 for (ch
= 0; ch
< 4; ch
++) {
394 int i
= util_ifloor(s
[ch
]);
395 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
398 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
400 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
401 for (ch
= 0; ch
< 4; ch
++) {
402 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
412 * For RECT textures / unnormalized texcoords.
413 * Only a subset of wrap modes supported.
416 linear_texcoord_unnorm_4(unsigned wrapMode
, const float s
[4], unsigned size
,
417 int icoord0
[4], int icoord1
[4], float w
[4])
421 case PIPE_TEX_WRAP_CLAMP
:
422 for (ch
= 0; ch
< 4; ch
++) {
423 /* Not exactly what the spec says, but it matches NVIDIA output */
424 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
425 icoord0
[ch
] = util_ifloor(u
);
426 icoord1
[ch
] = icoord0
[ch
] + 1;
430 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
432 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
433 for (ch
= 0; ch
< 4; ch
++) {
434 float u
= CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
);
436 icoord0
[ch
] = util_ifloor(u
);
437 icoord1
[ch
] = icoord0
[ch
] + 1;
438 if (icoord1
[ch
] > (int) size
- 1)
439 icoord1
[ch
] = size
- 1;
450 choose_cube_face(float rx
, float ry
, float rz
, float *newS
, float *newT
)
454 direction target sc tc ma
455 ---------- ------------------------------- --- --- ---
456 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
457 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
458 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
459 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
460 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
461 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
463 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
467 if (arx
> ary
&& arx
> arz
) {
469 face
= PIPE_TEX_FACE_POS_X
;
475 face
= PIPE_TEX_FACE_NEG_X
;
481 else if (ary
> arx
&& ary
> arz
) {
483 face
= PIPE_TEX_FACE_POS_Y
;
489 face
= PIPE_TEX_FACE_NEG_Y
;
497 face
= PIPE_TEX_FACE_POS_Z
;
503 face
= PIPE_TEX_FACE_NEG_Z
;
510 *newS
= ( sc
/ ma
+ 1.0F
) * 0.5F
;
511 *newT
= ( tc
/ ma
+ 1.0F
) * 0.5F
;
518 * Examine the quad's texture coordinates to compute the partial
519 * derivatives w.r.t X and Y, then compute lambda (level of detail).
521 * This is only done for fragment shaders, not vertex shaders.
524 compute_lambda(const struct pipe_texture
*tex
,
525 const struct pipe_sampler_state
*sampler
,
526 const float s
[QUAD_SIZE
],
527 const float t
[QUAD_SIZE
],
528 const float p
[QUAD_SIZE
],
533 assert(sampler
->normalized_coords
);
537 float dsdx
= s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
];
538 float dsdy
= s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
];
541 rho
= MAX2(dsdx
, dsdy
) * tex
->width
[0];
544 float dtdx
= t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
];
545 float dtdy
= t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
];
549 max
= MAX2(dtdx
, dtdy
) * tex
->height
[0];
550 rho
= MAX2(rho
, max
);
553 float dpdx
= p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
];
554 float dpdy
= p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
];
558 max
= MAX2(dpdx
, dpdy
) * tex
->depth
[0];
559 rho
= MAX2(rho
, max
);
562 lambda
= util_fast_log2(rho
);
563 lambda
+= lodbias
+ sampler
->lod_bias
;
564 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
571 * Do several things here:
572 * 1. Compute lambda from the texcoords, if needed
573 * 2. Determine if we're minifying or magnifying
574 * 3. If minifying, choose mipmap levels
575 * 4. Return image filter to use within mipmap images
576 * \param level0 Returns first mipmap level to sample from
577 * \param level1 Returns second mipmap level to sample from
578 * \param levelBlend Returns blend factor between levels, in [0,1]
579 * \param imgFilter Returns either the min or mag filter, depending on lambda
582 choose_mipmap_levels(const struct pipe_texture
*texture
,
583 const struct pipe_sampler_state
*sampler
,
584 const float s
[QUAD_SIZE
],
585 const float t
[QUAD_SIZE
],
586 const float p
[QUAD_SIZE
],
587 boolean computeLambda
,
589 unsigned *level0
, unsigned *level1
, float *levelBlend
,
592 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
593 /* no mipmap selection needed */
594 *level0
= *level1
= CLAMP((int) sampler
->min_lod
,
595 0, (int) texture
->last_level
);
597 if (sampler
->min_img_filter
!= sampler
->mag_img_filter
) {
598 /* non-mipmapped texture, but still need to determine if doing
599 * minification or magnification.
601 float lambda
= compute_lambda(texture
, sampler
, s
, t
, p
, lodbias
);
603 *imgFilter
= sampler
->mag_img_filter
;
606 *imgFilter
= sampler
->min_img_filter
;
610 *imgFilter
= sampler
->mag_img_filter
;
617 /* fragment shader */
618 lambda
= compute_lambda(texture
, sampler
, s
, t
, p
, lodbias
);
621 lambda
= lodbias
; /* not really a bias, but absolute LOD */
623 if (lambda
<= 0.0) { /* XXX threshold depends on the filter */
625 *imgFilter
= sampler
->mag_img_filter
;
626 *level0
= *level1
= 0;
630 *imgFilter
= sampler
->min_img_filter
;
632 /* choose mipmap level(s) and compute the blend factor between them */
633 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
634 /* Nearest mipmap level */
635 const int lvl
= (int) (lambda
+ 0.5);
637 *level1
= CLAMP(lvl
, 0, (int) texture
->last_level
);
640 /* Linear interpolation between mipmap levels */
641 const int lvl
= (int) lambda
;
642 *level0
= CLAMP(lvl
, 0, (int) texture
->last_level
);
643 *level1
= CLAMP(lvl
+ 1, 0, (int) texture
->last_level
);
644 *levelBlend
= FRAC(lambda
); /* blending weight between levels */
652 * Get a texel from a texture, using the texture tile cache.
654 * \param face the cube face in 0..5
655 * \param level the mipmap level
656 * \param x the x coord of texel within 2D image
657 * \param y the y coord of texel within 2D image
658 * \param z which slice of a 3D texture
659 * \param rgba the quad to put the texel/color into
660 * \param j which element of the rgba quad to write to
662 * XXX maybe move this into sp_tile_cache.c and merge with the
663 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
666 get_texel_quad_2d(const struct tgsi_sampler
*tgsi_sampler
,
667 unsigned face
, unsigned level
, int x
, int y
,
670 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
672 const struct softpipe_cached_tile
*tile
673 = sp_get_cached_tile_tex(samp
->cache
,
674 tile_address(x
, y
, 0, face
, level
));
679 out
[0] = &tile
->data
.color
[y
][x
][0];
680 out
[1] = &tile
->data
.color
[y
][x
+1][0];
681 out
[2] = &tile
->data
.color
[y
+1][x
][0];
682 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
685 static INLINE
const float *
686 get_texel_2d_ptr(const struct tgsi_sampler
*tgsi_sampler
,
687 unsigned face
, unsigned level
, int x
, int y
)
689 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
691 const struct softpipe_cached_tile
*tile
692 = sp_get_cached_tile_tex(samp
->cache
,
693 tile_address(x
, y
, 0, face
, level
));
698 return &tile
->data
.color
[y
][x
][0];
703 get_texel_quad_2d_mt(const struct tgsi_sampler
*tgsi_sampler
,
704 unsigned face
, unsigned level
,
711 for (i
= 0; i
< 4; i
++) {
712 unsigned tx
= (i
& 1) ? x1
: x0
;
713 unsigned ty
= (i
>> 1) ? y1
: y0
;
715 out
[i
] = get_texel_2d_ptr( tgsi_sampler
, face
, level
, tx
, ty
);
720 get_texel(const struct tgsi_sampler
*tgsi_sampler
,
721 unsigned face
, unsigned level
, int x
, int y
, int z
,
722 float rgba
[NUM_CHANNELS
][QUAD_SIZE
], unsigned j
)
724 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
725 const struct pipe_texture
*texture
= samp
->texture
;
726 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
728 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
729 y
< 0 || y
>= (int) texture
->height
[level
] ||
730 z
< 0 || z
>= (int) texture
->depth
[level
]) {
731 rgba
[0][j
] = sampler
->border_color
[0];
732 rgba
[1][j
] = sampler
->border_color
[1];
733 rgba
[2][j
] = sampler
->border_color
[2];
734 rgba
[3][j
] = sampler
->border_color
[3];
737 const unsigned tx
= x
% TILE_SIZE
;
738 const unsigned ty
= y
% TILE_SIZE
;
739 const struct softpipe_cached_tile
*tile
;
741 tile
= sp_get_cached_tile_tex(samp
->cache
,
742 tile_address(x
, y
, z
, face
, level
));
744 rgba
[0][j
] = tile
->data
.color
[ty
][tx
][0];
745 rgba
[1][j
] = tile
->data
.color
[ty
][tx
][1];
746 rgba
[2][j
] = tile
->data
.color
[ty
][tx
][2];
747 rgba
[3][j
] = tile
->data
.color
[ty
][tx
][3];
750 debug_printf("Get texel %f %f %f %f from %s\n",
751 rgba
[0][j
], rgba
[1][j
], rgba
[2][j
], rgba
[3][j
],
752 pf_name(texture
->format
));
759 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
760 * When we sampled the depth texture, the depth value was put into all
761 * RGBA channels. We look at the red channel here.
762 * \param rgba quad of (depth) texel values
763 * \param p texture 'P' components for four pixels in quad
764 * \param j which pixel in the quad to test [0..3]
767 shadow_compare(const struct pipe_sampler_state
*sampler
,
768 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
769 const float p
[QUAD_SIZE
],
773 switch (sampler
->compare_func
) {
775 k
= p
[j
] < rgba
[0][j
];
777 case PIPE_FUNC_LEQUAL
:
778 k
= p
[j
] <= rgba
[0][j
];
780 case PIPE_FUNC_GREATER
:
781 k
= p
[j
] > rgba
[0][j
];
783 case PIPE_FUNC_GEQUAL
:
784 k
= p
[j
] >= rgba
[0][j
];
786 case PIPE_FUNC_EQUAL
:
787 k
= p
[j
] == rgba
[0][j
];
789 case PIPE_FUNC_NOTEQUAL
:
790 k
= p
[j
] != rgba
[0][j
];
792 case PIPE_FUNC_ALWAYS
:
795 case PIPE_FUNC_NEVER
:
804 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
805 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = (float) k
;
811 * As above, but do four z/texture comparisons.
814 shadow_compare4(const struct pipe_sampler_state
*sampler
,
815 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
816 const float p
[QUAD_SIZE
])
818 int j
, k0
, k1
, k2
, k3
;
821 /* compare four texcoords vs. four texture samples */
822 switch (sampler
->compare_func
) {
824 k0
= p
[0] < rgba
[0][0];
825 k1
= p
[1] < rgba
[0][1];
826 k2
= p
[2] < rgba
[0][2];
827 k3
= p
[3] < rgba
[0][3];
829 case PIPE_FUNC_LEQUAL
:
830 k0
= p
[0] <= rgba
[0][0];
831 k1
= p
[1] <= rgba
[0][1];
832 k2
= p
[2] <= rgba
[0][2];
833 k3
= p
[3] <= rgba
[0][3];
835 case PIPE_FUNC_GREATER
:
836 k0
= p
[0] > rgba
[0][0];
837 k1
= p
[1] > rgba
[0][1];
838 k2
= p
[2] > rgba
[0][2];
839 k3
= p
[3] > rgba
[0][3];
841 case PIPE_FUNC_GEQUAL
:
842 k0
= p
[0] >= rgba
[0][0];
843 k1
= p
[1] >= rgba
[0][1];
844 k2
= p
[2] >= rgba
[0][2];
845 k3
= p
[3] >= rgba
[0][3];
847 case PIPE_FUNC_EQUAL
:
848 k0
= p
[0] == rgba
[0][0];
849 k1
= p
[1] == rgba
[0][1];
850 k2
= p
[2] == rgba
[0][2];
851 k3
= p
[3] == rgba
[0][3];
853 case PIPE_FUNC_NOTEQUAL
:
854 k0
= p
[0] != rgba
[0][0];
855 k1
= p
[1] != rgba
[0][1];
856 k2
= p
[2] != rgba
[0][2];
857 k3
= p
[3] != rgba
[0][3];
859 case PIPE_FUNC_ALWAYS
:
860 k0
= k1
= k2
= k3
= 1;
862 case PIPE_FUNC_NEVER
:
863 k0
= k1
= k2
= k3
= 0;
866 k0
= k1
= k2
= k3
= 0;
871 /* convert four pass/fail values to an intensity in [0,1] */
872 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
874 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
875 for (j
= 0; j
< 4; j
++) {
876 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
884 sp_get_samples_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
885 const float s
[QUAD_SIZE
],
886 const float t
[QUAD_SIZE
],
887 const float p
[QUAD_SIZE
],
889 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
891 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
893 unsigned level
= samp
->level
;
894 unsigned xpot
= 1 << (samp
->xpot
- level
);
895 unsigned ypot
= 1 << (samp
->ypot
- level
);
897 for (j
= 0; j
< QUAD_SIZE
; j
++) {
900 float u
= s
[j
] * xpot
- 0.5F
;
901 float v
= t
[j
] * ypot
- 0.5F
;
903 int uflr
= util_ifloor(u
);
904 int vflr
= util_ifloor(v
);
906 float xw
= u
- (float)uflr
;
907 float yw
= v
- (float)vflr
;
909 int x0
= uflr
& (xpot
- 1);
910 int y0
= vflr
& (ypot
- 1);
915 /* Can we fetch all four at once:
917 if (x0
% TILE_SIZE
!= TILE_SIZE
-1 &&
918 y0
% TILE_SIZE
!= TILE_SIZE
-1)
920 get_texel_quad_2d(tgsi_sampler
, 0, level
, x0
, y0
, tx
);
924 unsigned x1
= (uflr
+ 1) & (xpot
- 1);
925 unsigned y1
= (vflr
+ 1) & (ypot
- 1);
926 get_texel_quad_2d_mt(tgsi_sampler
, 0, level
,
931 /* interpolate R, G, B, A */
932 for (c
= 0; c
< 4; c
++) {
933 rgba
[c
][j
] = lerp_2d(xw
, yw
,
942 sp_get_samples_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
943 const float s
[QUAD_SIZE
],
944 const float t
[QUAD_SIZE
],
945 const float p
[QUAD_SIZE
],
947 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
949 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
951 unsigned level
= samp
->level
;
952 unsigned xpot
= 1 << (samp
->xpot
- level
);
953 unsigned ypot
= 1 << (samp
->ypot
- level
);
955 for (j
= 0; j
< QUAD_SIZE
; j
++) {
958 float u
= s
[j
] * xpot
;
959 float v
= t
[j
] * ypot
;
961 int uflr
= util_ifloor(u
);
962 int vflr
= util_ifloor(v
);
964 int x0
= uflr
& (xpot
- 1);
965 int y0
= vflr
& (ypot
- 1);
967 const float *out
= get_texel_2d_ptr(tgsi_sampler
, 0, level
, x0
, y0
);
969 for (c
= 0; c
< 4; c
++) {
977 sp_get_samples_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
978 const float s
[QUAD_SIZE
],
979 const float t
[QUAD_SIZE
],
980 const float p
[QUAD_SIZE
],
982 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
984 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
986 unsigned level
= samp
->level
;
987 unsigned xpot
= (1<<samp
->xpot
);
988 unsigned ypot
= (1<<samp
->ypot
);
990 for (j
= 0; j
< QUAD_SIZE
; j
++) {
993 float u
= s
[j
] * xpot
;
994 float v
= t
[j
] * ypot
;
1002 else if (x0
> xpot
- 1)
1005 y0
= util_ifloor(v
);
1008 else if (y0
> ypot
- 1)
1011 out
= get_texel_2d_ptr(tgsi_sampler
, 0, level
, x0
, y0
);
1013 for (c
= 0; c
< 4; c
++) {
1014 rgba
[c
][j
] = out
[c
];
1021 sp_get_samples_2d_linear_mip_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
1022 const float s
[QUAD_SIZE
],
1023 const float t
[QUAD_SIZE
],
1024 const float p
[QUAD_SIZE
],
1026 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1028 struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1029 const struct pipe_texture
*texture
= samp
->texture
;
1030 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1034 lambda
= compute_lambda(texture
, sampler
, s
, t
, p
, lodbias
);
1035 level0
= (int)lambda
;
1036 level1
= level0
+ 1;
1040 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1041 s
, t
, p
, lodbias
, rgba
);
1043 else if (level0
>= texture
->last_level
) {
1044 samp
->level
= texture
->last_level
;
1045 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1046 s
, t
, p
, lodbias
, rgba
);
1053 float levelBlend
= lambda
- level0
; /* blending weight between levels */
1055 samp
->level
= level0
;
1056 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1057 s
, t
, p
, lodbias
, rgba0
);
1060 sp_get_samples_2d_linear_repeat_POT( tgsi_sampler
,
1061 s
, t
, p
, lodbias
, rgba1
);
1063 for (c
= 0; c
< 4; c
++)
1064 for (j
= 0; j
< 4; j
++)
1065 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1070 * Common code for sampling 1D/2D/cube textures.
1071 * Could probably extend for 3D...
1074 sp_get_samples_2d_common(const struct tgsi_sampler
*tgsi_sampler
,
1075 const float s
[QUAD_SIZE
],
1076 const float t
[QUAD_SIZE
],
1077 const float p
[QUAD_SIZE
],
1078 boolean computeLambda
,
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(texture
, sampler
, s
, t
, p
, computeLambda
, lodbias
,
1091 &level0
, &level1
, &levelBlend
, &imgFilter
);
1093 assert(sampler
->normalized_coords
);
1095 width
= texture
->width
[level0
];
1096 height
= texture
->height
[level0
];
1100 switch (imgFilter
) {
1101 case PIPE_TEX_FILTER_NEAREST
:
1104 nearest_texcoord_4(sampler
->wrap_s
, s
, width
, x
);
1105 nearest_texcoord_4(sampler
->wrap_t
, t
, height
, y
);
1107 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1108 get_texel(tgsi_sampler
, faces
[j
], level0
, x
[j
], y
[j
], 0, rgba
, j
);
1109 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1110 shadow_compare(sampler
, rgba
, p
, j
);
1113 if (level0
!= level1
) {
1114 /* get texels from second mipmap level and blend */
1119 get_texel(tgsi_sampler
, faces
[j
], level1
, x
[j
], y
[j
], 0,
1121 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
1122 shadow_compare(sampler
, rgba2
, p
, j
);
1125 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1126 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1132 case PIPE_TEX_FILTER_LINEAR
:
1133 case PIPE_TEX_FILTER_ANISO
:
1135 int x0
[4], y0
[4], x1
[4], y1
[4];
1136 float xw
[4], yw
[4]; /* weights */
1138 linear_texcoord_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1139 linear_texcoord_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1141 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1142 float tx
[4][4]; /* texels */
1144 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
[j
], y0
[j
], 0, tx
, 0);
1145 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
[j
], y0
[j
], 0, tx
, 1);
1146 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
[j
], y1
[j
], 0, tx
, 2);
1147 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
[j
], y1
[j
], 0, tx
, 3);
1148 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1149 shadow_compare4(sampler
, tx
, p
);
1152 /* interpolate R, G, B, A */
1153 for (c
= 0; c
< 4; c
++) {
1154 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1156 tx
[c
][2], tx
[c
][3]);
1159 if (level0
!= level1
) {
1160 /* get texels from second mipmap level and blend */
1166 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
[j
], y0
[j
], 0, tx
, 0);
1167 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
[j
], y0
[j
], 0, tx
, 1);
1168 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
[j
], y1
[j
], 0, tx
, 2);
1169 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
[j
], y1
[j
], 0, tx
, 3);
1170 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
1171 shadow_compare4(sampler
, tx
, p
);
1174 /* interpolate R, G, B, A */
1175 for (c
= 0; c
< 4; c
++) {
1176 rgba2
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1177 tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
1180 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1181 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1194 sp_get_samples_1d(const struct tgsi_sampler
*sampler
,
1195 const float s
[QUAD_SIZE
],
1196 const float t
[QUAD_SIZE
],
1197 const float p
[QUAD_SIZE
],
1198 boolean computeLambda
,
1200 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1202 static const unsigned faces
[4] = {0, 0, 0, 0};
1203 static const float tzero
[4] = {0, 0, 0, 0};
1204 sp_get_samples_2d_common(sampler
, s
, tzero
, NULL
,
1205 computeLambda
, lodbias
, rgba
, faces
);
1210 sp_get_samples_2d(const struct tgsi_sampler
*sampler
,
1211 const float s
[QUAD_SIZE
],
1212 const float t
[QUAD_SIZE
],
1213 const float p
[QUAD_SIZE
],
1214 boolean computeLambda
,
1216 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1218 static const unsigned faces
[4] = {0, 0, 0, 0};
1219 sp_get_samples_2d_common(sampler
, s
, t
, p
,
1220 computeLambda
, lodbias
, rgba
, faces
);
1225 sp_get_samples_3d(const struct tgsi_sampler
*tgsi_sampler
,
1226 const float s
[QUAD_SIZE
],
1227 const float t
[QUAD_SIZE
],
1228 const float p
[QUAD_SIZE
],
1229 boolean computeLambda
,
1231 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1233 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1234 const struct pipe_texture
*texture
= samp
->texture
;
1235 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1236 /* get/map pipe_surfaces corresponding to 3D tex slices */
1237 unsigned level0
, level1
, j
, imgFilter
;
1238 int width
, height
, depth
;
1240 const uint face
= 0;
1242 choose_mipmap_levels(texture
, sampler
, s
, t
, p
, computeLambda
, lodbias
,
1243 &level0
, &level1
, &levelBlend
, &imgFilter
);
1245 assert(sampler
->normalized_coords
);
1247 width
= texture
->width
[level0
];
1248 height
= texture
->height
[level0
];
1249 depth
= texture
->depth
[level0
];
1255 switch (imgFilter
) {
1256 case PIPE_TEX_FILTER_NEAREST
:
1258 int x
[4], y
[4], z
[4];
1259 nearest_texcoord_4(sampler
->wrap_s
, s
, width
, x
);
1260 nearest_texcoord_4(sampler
->wrap_t
, t
, height
, y
);
1261 nearest_texcoord_4(sampler
->wrap_r
, p
, depth
, z
);
1262 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1263 get_texel(tgsi_sampler
, face
, level0
, x
[j
], y
[j
], z
[j
], rgba
, j
);
1264 if (level0
!= level1
) {
1265 /* get texels from second mipmap level and blend */
1271 get_texel(tgsi_sampler
, face
, level1
, x
[j
], y
[j
], z
[j
], rgba2
, j
);
1272 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1273 rgba
[c
][j
] = lerp(levelBlend
, rgba2
[c
][j
], rgba
[c
][j
]);
1279 case PIPE_TEX_FILTER_LINEAR
:
1280 case PIPE_TEX_FILTER_ANISO
:
1282 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1283 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1284 linear_texcoord_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1285 linear_texcoord_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1286 linear_texcoord_4(sampler
->wrap_r
, p
, depth
, z0
, z1
, zw
);
1288 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1290 float tx0
[4][4], tx1
[4][4];
1291 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], z0
[j
], tx0
, 0);
1292 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], z0
[j
], tx0
, 1);
1293 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], z0
[j
], tx0
, 2);
1294 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], z0
[j
], tx0
, 3);
1295 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], z1
[j
], tx1
, 0);
1296 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], z1
[j
], tx1
, 1);
1297 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], z1
[j
], tx1
, 2);
1298 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], z1
[j
], tx1
, 3);
1300 /* interpolate R, G, B, A */
1301 for (c
= 0; c
< 4; c
++) {
1302 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1303 tx0
[c
][0], tx0
[c
][1],
1304 tx0
[c
][2], tx0
[c
][3],
1305 tx1
[c
][0], tx1
[c
][1],
1306 tx1
[c
][2], tx1
[c
][3]);
1309 if (level0
!= level1
) {
1310 /* get texels from second mipmap level and blend */
1318 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y0
[j
], z0
[j
], tx0
, 0);
1319 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y0
[j
], z0
[j
], tx0
, 1);
1320 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y1
[j
], z0
[j
], tx0
, 2);
1321 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y1
[j
], z0
[j
], tx0
, 3);
1322 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y0
[j
], z1
[j
], tx1
, 0);
1323 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y0
[j
], z1
[j
], tx1
, 1);
1324 get_texel(tgsi_sampler
, face
, level1
, x0
[j
], y1
[j
], z1
[j
], tx1
, 2);
1325 get_texel(tgsi_sampler
, face
, level1
, x1
[j
], y1
[j
], z1
[j
], tx1
, 3);
1327 /* interpolate R, G, B, A */
1328 for (c
= 0; c
< 4; c
++) {
1329 rgba2
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1330 tx0
[c
][0], tx0
[c
][1],
1331 tx0
[c
][2], tx0
[c
][3],
1332 tx1
[c
][0], tx1
[c
][1],
1333 tx1
[c
][2], tx1
[c
][3]);
1336 /* blend mipmap levels */
1337 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
1338 rgba
[c
][j
] = lerp(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
1351 sp_get_samples_cube(const struct tgsi_sampler
*sampler
,
1352 const float s
[QUAD_SIZE
],
1353 const float t
[QUAD_SIZE
],
1354 const float p
[QUAD_SIZE
],
1355 boolean computeLambda
,
1357 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1359 unsigned faces
[QUAD_SIZE
], j
;
1360 float ssss
[4], tttt
[4];
1361 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1362 faces
[j
] = choose_cube_face(s
[j
], t
[j
], p
[j
], ssss
+ j
, tttt
+ j
);
1364 sp_get_samples_2d_common(sampler
, ssss
, tttt
, NULL
,
1365 computeLambda
, lodbias
, rgba
, faces
);
1370 sp_get_samples_rect(const struct tgsi_sampler
*tgsi_sampler
,
1371 const float s
[QUAD_SIZE
],
1372 const float t
[QUAD_SIZE
],
1373 const float p
[QUAD_SIZE
],
1374 boolean computeLambda
,
1376 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1378 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1379 const struct pipe_texture
*texture
= samp
->texture
;
1380 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1381 const uint face
= 0;
1382 unsigned level0
, level1
, j
, imgFilter
;
1386 choose_mipmap_levels(texture
, sampler
, s
, t
, p
, computeLambda
, lodbias
,
1387 &level0
, &level1
, &levelBlend
, &imgFilter
);
1389 /* texture RECTS cannot be mipmapped */
1390 assert(level0
== level1
);
1392 width
= texture
->width
[level0
];
1393 height
= texture
->height
[level0
];
1397 switch (imgFilter
) {
1398 case PIPE_TEX_FILTER_NEAREST
:
1401 nearest_texcoord_unnorm_4(sampler
->wrap_s
, s
, width
, x
);
1402 nearest_texcoord_unnorm_4(sampler
->wrap_t
, t
, height
, y
);
1403 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1404 get_texel(tgsi_sampler
, face
, level0
, x
[j
], y
[j
], 0, rgba
, j
);
1405 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1406 shadow_compare(sampler
, rgba
, p
, j
);
1411 case PIPE_TEX_FILTER_LINEAR
:
1412 case PIPE_TEX_FILTER_ANISO
:
1414 int x0
[4], y0
[4], x1
[4], y1
[4];
1415 float xw
[4], yw
[4]; /* weights */
1416 linear_texcoord_unnorm_4(sampler
->wrap_s
, s
, width
, x0
, x1
, xw
);
1417 linear_texcoord_unnorm_4(sampler
->wrap_t
, t
, height
, y0
, y1
, yw
);
1418 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1419 float tx
[4][4]; /* texels */
1421 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y0
[j
], 0, tx
, 0);
1422 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y0
[j
], 0, tx
, 1);
1423 get_texel(tgsi_sampler
, face
, level0
, x0
[j
], y1
[j
], 0, tx
, 2);
1424 get_texel(tgsi_sampler
, face
, level0
, x1
[j
], y1
[j
], 0, tx
, 3);
1425 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1426 shadow_compare4(sampler
, tx
, p
);
1428 for (c
= 0; c
< 4; c
++) {
1429 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1430 tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
1442 * Common code for vertex/fragment program texture sampling.
1445 sp_get_samples(struct tgsi_sampler
*tgsi_sampler
,
1446 const float s
[QUAD_SIZE
],
1447 const float t
[QUAD_SIZE
],
1448 const float p
[QUAD_SIZE
],
1449 boolean computeLambda
,
1451 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1453 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1454 const struct pipe_texture
*texture
= samp
->texture
;
1455 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1460 switch (texture
->target
) {
1461 case PIPE_TEXTURE_1D
:
1462 assert(sampler
->normalized_coords
);
1463 sp_get_samples_1d(tgsi_sampler
, s
, t
, p
, computeLambda
, lodbias
, rgba
);
1465 case PIPE_TEXTURE_2D
:
1466 if (sampler
->normalized_coords
)
1467 sp_get_samples_2d(tgsi_sampler
, s
, t
, p
, computeLambda
, lodbias
, rgba
);
1469 sp_get_samples_rect(tgsi_sampler
, s
, t
, p
, computeLambda
, lodbias
, rgba
);
1471 case PIPE_TEXTURE_3D
:
1472 assert(sampler
->normalized_coords
);
1473 sp_get_samples_3d(tgsi_sampler
, s
, t
, p
, computeLambda
, lodbias
, rgba
);
1475 case PIPE_TEXTURE_CUBE
:
1476 assert(sampler
->normalized_coords
);
1477 sp_get_samples_cube(tgsi_sampler
, s
, t
, p
, computeLambda
, lodbias
, rgba
);
1486 printf("Sampled at %f, %f, %f:\n", s
[0], t
[0], p
[0]);
1487 for (i
= 0; i
< 4; i
++) {
1488 printf("Frag %d: %f %f %f %f\n", i
,
1499 sp_get_samples_fallback(struct tgsi_sampler
*tgsi_sampler
,
1500 const float s
[QUAD_SIZE
],
1501 const float t
[QUAD_SIZE
],
1502 const float p
[QUAD_SIZE
],
1504 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1506 sp_get_samples(tgsi_sampler
, s
, t
, p
, TRUE
, lodbias
, rgba
);
1510 * Called via tgsi_sampler::get_samples() when running a fragment shader.
1511 * Get four filtered RGBA values from the sampler's texture.
1514 sp_get_samples_fragment(struct tgsi_sampler
*tgsi_sampler
,
1515 const float s
[QUAD_SIZE
],
1516 const float t
[QUAD_SIZE
],
1517 const float p
[QUAD_SIZE
],
1519 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1521 struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1522 const struct pipe_texture
*texture
= samp
->texture
;
1523 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1525 tgsi_sampler
->get_samples
= sp_get_samples_fallback
;
1527 /* Try to hook in a faster sampler. Ultimately we'll have to
1528 * code-generate these. Luckily most of this looks like it is
1529 * orthogonal state within the sampler.
1531 if (texture
->target
== PIPE_TEXTURE_2D
&&
1532 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1533 sampler
->wrap_s
== sampler
->wrap_t
&&
1534 sampler
->compare_mode
== FALSE
&&
1535 sampler
->normalized_coords
)
1537 samp
->xpot
= util_unsigned_logbase2( samp
->texture
->width
[0] );
1538 samp
->ypot
= util_unsigned_logbase2( samp
->texture
->height
[0] );
1540 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1541 samp
->level
= CLAMP((int) sampler
->min_lod
,
1542 0, (int) texture
->last_level
);
1544 if (sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
) {
1545 switch (sampler
->min_img_filter
) {
1546 case PIPE_TEX_FILTER_NEAREST
:
1547 tgsi_sampler
->get_samples
= sp_get_samples_2d_nearest_repeat_POT
;
1549 case PIPE_TEX_FILTER_LINEAR
:
1550 tgsi_sampler
->get_samples
= sp_get_samples_2d_linear_repeat_POT
;
1556 else if (sampler
->wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
1557 switch (sampler
->min_img_filter
) {
1558 case PIPE_TEX_FILTER_NEAREST
:
1559 tgsi_sampler
->get_samples
= sp_get_samples_2d_nearest_clamp_POT
;
1566 else if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1567 if (sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
) {
1568 switch (sampler
->min_img_filter
) {
1569 case PIPE_TEX_FILTER_LINEAR
:
1570 /* This one not working yet:
1573 tgsi_sampler
->get_samples
= sp_get_samples_2d_linear_mip_linear_repeat_POT
;
1582 _debug_printf("target %d/%d min_mip %d/%d min_img %d/%d wrap %d/%d compare %d/%d norm %d/%d\n",
1583 texture
->target
, PIPE_TEXTURE_2D
,
1584 sampler
->min_mip_filter
, PIPE_TEX_MIPFILTER_NONE
,
1585 sampler
->min_img_filter
, sampler
->mag_img_filter
,
1586 sampler
->wrap_s
, sampler
->wrap_t
,
1587 sampler
->compare_mode
, FALSE
,
1588 sampler
->normalized_coords
, TRUE
);
1591 tgsi_sampler
->get_samples( tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1596 * Called via tgsi_sampler::get_samples() when running a vertex shader.
1597 * Get four filtered RGBA values from the sampler's texture.
1600 sp_get_samples_vertex(struct tgsi_sampler
*tgsi_sampler
,
1601 const float s
[QUAD_SIZE
],
1602 const float t
[QUAD_SIZE
],
1603 const float p
[QUAD_SIZE
],
1605 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1607 sp_get_samples(tgsi_sampler
, s
, t
, p
, FALSE
, lodbias
, rgba
);