1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
35 #include "sp_context.h"
36 #include "sp_headers.h"
37 #include "sp_surface.h"
38 #include "sp_texture.h"
39 #include "sp_tex_sample.h"
40 #include "sp_tile_cache.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "tgsi/tgsi_exec.h"
44 #include "util/u_math.h"
45 #include "util/u_memory.h"
49 * Note, the FRAC macro has to work perfectly. Otherwise you'll sometimes
50 * see 1-pixel bands of improperly weighted linear-filtered textures.
51 * The tests/texwrap.c demo is a good test.
52 * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
53 * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
55 #define FRAC(f) ((f) - util_ifloor(f))
59 * Linear interpolation macro
61 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
65 * Do 2D/biliner interpolation of float values.
66 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
67 * a and b are the horizontal and vertical interpolants.
68 * It's important that this function is inlined when compiled with
69 * optimization! If we find that's not true on some systems, convert
73 lerp_2d(float a
, float b
,
74 float v00
, float v10
, float v01
, float v11
)
76 const float temp0
= LERP(a
, v00
, v10
);
77 const float temp1
= LERP(a
, v01
, v11
);
78 return LERP(b
, temp0
, temp1
);
83 * If A is a signed integer, A % B doesn't give the right value for A < 0
84 * (in terms of texture repeat). Just casting to unsigned fixes that.
86 #define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
90 * Apply texture coord wrapping mode and return integer texture index.
91 * \param wrapMode PIPE_TEX_WRAP_x
92 * \param s the texcoord
93 * \param size the texture image size
94 * \return integer texture index
97 nearest_texcoord(unsigned wrapMode
, float s
, unsigned size
)
101 case PIPE_TEX_WRAP_REPEAT
:
102 /* s limited to [0,1) */
103 /* i limited to [0,size-1] */
104 i
= util_ifloor(s
* size
);
105 i
= REMAINDER(i
, size
);
107 case PIPE_TEX_WRAP_CLAMP
:
108 /* s limited to [0,1] */
109 /* i limited to [0,size-1] */
115 i
= util_ifloor(s
* size
);
117 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
119 /* s limited to [min,max] */
120 /* i limited to [0, size-1] */
121 const float min
= 1.0F
/ (2.0F
* size
);
122 const float max
= 1.0F
- min
;
128 i
= util_ifloor(s
* size
);
131 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
133 /* s limited to [min,max] */
134 /* i limited to [-1, size] */
135 const float min
= -1.0F
/ (2.0F
* size
);
136 const float max
= 1.0F
- min
;
142 i
= util_ifloor(s
* size
);
145 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
147 const float min
= 1.0F
/ (2.0F
* size
);
148 const float max
= 1.0F
- min
;
149 const int flr
= util_ifloor(s
);
152 u
= 1.0F
- (s
- (float) flr
);
160 i
= util_ifloor(u
* size
);
163 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
165 /* s limited to [0,1] */
166 /* i limited to [0,size-1] */
167 const float u
= fabsf(s
);
173 i
= util_ifloor(u
* size
);
176 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
178 /* s limited to [min,max] */
179 /* i limited to [0, size-1] */
180 const float min
= 1.0F
/ (2.0F
* size
);
181 const float max
= 1.0F
- min
;
182 const float u
= fabsf(s
);
188 i
= util_ifloor(u
* size
);
191 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
193 /* s limited to [min,max] */
194 /* i limited to [0, size-1] */
195 const float min
= -1.0F
/ (2.0F
* size
);
196 const float max
= 1.0F
- min
;
197 const float u
= fabsf(s
);
203 i
= util_ifloor(u
* size
);
214 * Used to compute texel locations for linear sampling.
215 * \param wrapMode PIPE_TEX_WRAP_x
216 * \param s the texcoord
217 * \param size the texture image size
218 * \param i0 returns first texture index
219 * \param i1 returns second texture index (usually *i0 + 1)
220 * \param a returns blend factor/weight between texture indexes
223 linear_texcoord(unsigned wrapMode
, float s
, unsigned size
,
224 int *i0
, int *i1
, float *a
)
228 case PIPE_TEX_WRAP_REPEAT
:
230 *i0
= REMAINDER(util_ifloor(u
), size
);
231 *i1
= REMAINDER(*i0
+ 1, size
);
233 case PIPE_TEX_WRAP_CLAMP
:
241 *i0
= util_ifloor(u
);
244 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
252 *i0
= util_ifloor(u
);
256 if (*i1
>= (int) size
)
259 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
261 const float min
= -1.0F
/ (2.0F
* size
);
262 const float max
= 1.0F
- min
;
270 *i0
= util_ifloor(u
);
274 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
276 const int flr
= util_ifloor(s
);
278 u
= 1.0F
- (s
- (float) flr
);
281 u
= (u
* size
) - 0.5F
;
282 *i0
= util_ifloor(u
);
286 if (*i1
>= (int) size
)
290 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
297 *i0
= util_ifloor(u
);
300 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
307 *i0
= util_ifloor(u
);
311 if (*i1
>= (int) size
)
314 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
316 const float min
= -1.0F
/ (2.0F
* size
);
317 const float max
= 1.0F
- min
;
326 *i0
= util_ifloor(u
);
338 * For RECT textures / unnormalized texcoords
339 * Only a subset of wrap modes supported.
342 nearest_texcoord_unnorm(unsigned wrapMode
, float s
, unsigned size
)
346 case PIPE_TEX_WRAP_CLAMP
:
348 return CLAMP(i
, 0, (int) size
-1);
349 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
351 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
352 return util_ifloor( CLAMP(s
, 0.5F
, (float) size
- 0.5F
) );
361 * For RECT textures / unnormalized texcoords.
362 * Only a subset of wrap modes supported.
365 linear_texcoord_unnorm(unsigned wrapMode
, float s
, unsigned size
,
366 int *i0
, int *i1
, float *a
)
369 case PIPE_TEX_WRAP_CLAMP
:
370 /* Not exactly what the spec says, but it matches NVIDIA output */
371 s
= CLAMP(s
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
372 *i0
= util_ifloor(s
);
375 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
377 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
378 s
= CLAMP(s
, 0.5F
, (float) size
- 0.5F
);
380 *i0
= util_ifloor(s
);
382 if (*i1
> (int) size
- 1)
393 choose_cube_face(float rx
, float ry
, float rz
, float *newS
, float *newT
)
397 direction target sc tc ma
398 ---------- ------------------------------- --- --- ---
399 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
400 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
401 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
402 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
403 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
404 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
406 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
410 if (arx
> ary
&& arx
> arz
) {
412 face
= PIPE_TEX_FACE_POS_X
;
418 face
= PIPE_TEX_FACE_NEG_X
;
424 else if (ary
> arx
&& ary
> arz
) {
426 face
= PIPE_TEX_FACE_POS_Y
;
432 face
= PIPE_TEX_FACE_NEG_Y
;
440 face
= PIPE_TEX_FACE_POS_Z
;
446 face
= PIPE_TEX_FACE_NEG_Z
;
453 *newS
= ( sc
/ ma
+ 1.0F
) * 0.5F
;
454 *newT
= ( tc
/ ma
+ 1.0F
) * 0.5F
;
461 * Examine the quad's texture coordinates to compute the partial
462 * derivatives w.r.t X and Y, then compute lambda (level of detail).
464 * This is only done for fragment shaders, not vertex shaders.
467 compute_lambda(const struct pipe_texture
*tex
,
468 const struct pipe_sampler_state
*sampler
,
469 const float s
[QUAD_SIZE
],
470 const float t
[QUAD_SIZE
],
471 const float p
[QUAD_SIZE
],
476 assert(sampler
->normalized_coords
);
480 float dsdx
= s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
];
481 float dsdy
= s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
];
484 rho
= MAX2(dsdx
, dsdy
) * tex
->width
[0];
487 float dtdx
= t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
];
488 float dtdy
= t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
];
492 max
= MAX2(dtdx
, dtdy
) * tex
->height
[0];
493 rho
= MAX2(rho
, max
);
496 float dpdx
= p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
];
497 float dpdy
= p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
];
501 max
= MAX2(dpdx
, dpdy
) * tex
->depth
[0];
502 rho
= MAX2(rho
, max
);
505 lambda
= util_fast_log2(rho
);
506 lambda
+= lodbias
+ sampler
->lod_bias
;
507 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
514 * Do several things here:
515 * 1. Compute lambda from the texcoords, if needed
516 * 2. Determine if we're minifying or magnifying
517 * 3. If minifying, choose mipmap levels
518 * 4. Return image filter to use within mipmap images
521 choose_mipmap_levels(const struct pipe_texture
*texture
,
522 const struct pipe_sampler_state
*sampler
,
523 const float s
[QUAD_SIZE
],
524 const float t
[QUAD_SIZE
],
525 const float p
[QUAD_SIZE
],
527 unsigned *level0
, unsigned *level1
, float *levelBlend
,
531 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
532 /* no mipmap selection needed */
533 *level0
= *level1
= CLAMP((int) sampler
->min_lod
,
534 0, (int) texture
->last_level
);
536 if (sampler
->min_img_filter
!= sampler
->mag_img_filter
) {
537 /* non-mipmapped texture, but still need to determine if doing
538 * minification or magnification.
540 float lambda
= compute_lambda(texture
, sampler
, s
, t
, p
, lodbias
);
542 *imgFilter
= sampler
->mag_img_filter
;
545 *imgFilter
= sampler
->min_img_filter
;
549 *imgFilter
= sampler
->mag_img_filter
;
556 /* fragment shader */
557 lambda
= compute_lambda(texture
, sampler
, s
, t
, p
, lodbias
);
560 lambda
= lodbias
; /* not really a bias, but absolute LOD */
562 if (lambda
<= 0.0) { /* XXX threshold depends on the filter */
564 *imgFilter
= sampler
->mag_img_filter
;
565 *level0
= *level1
= 0;
569 *imgFilter
= sampler
->min_img_filter
;
571 /* choose mipmap level(s) and compute the blend factor between them */
572 if (sampler
->min_mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
573 /* Nearest mipmap level */
574 const int lvl
= (int) (lambda
+ 0.5);
576 *level1
= CLAMP(lvl
, 0, (int) texture
->last_level
);
579 /* Linear interpolation between mipmap levels */
580 const int lvl
= (int) lambda
;
581 *level0
= CLAMP(lvl
, 0, (int) texture
->last_level
);
582 *level1
= CLAMP(lvl
+ 1, 0, (int) texture
->last_level
);
583 *levelBlend
= FRAC(lambda
); /* blending weight between levels */
591 * Get a texel from a texture, using the texture tile cache.
592 * Called by the TGSI interpreter.
594 * \param face the cube face in 0..5
595 * \param level the mipmap level
596 * \param x the x coord of texel within 2D image
597 * \param y the y coord of texel within 2D image
598 * \param z which slice of a 3D texture
599 * \param rgba the quad to put the texel/color into
600 * \param j which element of the rgba quad to write to
602 * XXX maybe move this into sp_tile_cache.c and merge with the
603 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
606 get_texel(struct tgsi_sampler
*tgsi_sampler
,
607 unsigned face
, unsigned level
, int x
, int y
, int z
,
608 float rgba
[NUM_CHANNELS
][QUAD_SIZE
], unsigned j
)
610 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
611 const struct softpipe_context
*sp
= samp
->sp
;
612 const uint unit
= samp
->unit
;
613 const struct pipe_texture
*texture
= sp
->texture
[unit
];
614 const struct pipe_sampler_state
*sampler
= sp
->sampler
[unit
];
616 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
617 y
< 0 || y
>= (int) texture
->height
[level
] ||
618 z
< 0 || z
>= (int) texture
->depth
[level
]) {
619 rgba
[0][j
] = sampler
->border_color
[0];
620 rgba
[1][j
] = sampler
->border_color
[1];
621 rgba
[2][j
] = sampler
->border_color
[2];
622 rgba
[3][j
] = sampler
->border_color
[3];
625 const int tx
= x
% TILE_SIZE
;
626 const int ty
= y
% TILE_SIZE
;
627 const struct softpipe_cached_tile
*tile
628 = sp_get_cached_tile_tex(samp
->sp
, samp
->cache
,
629 x
, y
, z
, face
, level
);
630 rgba
[0][j
] = tile
->data
.color
[ty
][tx
][0];
631 rgba
[1][j
] = tile
->data
.color
[ty
][tx
][1];
632 rgba
[2][j
] = tile
->data
.color
[ty
][tx
][2];
633 rgba
[3][j
] = tile
->data
.color
[ty
][tx
][3];
636 debug_printf("Get texel %f %f %f %f from %s\n",
637 rgba
[0][j
], rgba
[1][j
], rgba
[2][j
], rgba
[3][j
],
638 pf_name(texture
->format
));
645 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
646 * When we sampled the depth texture, the depth value was put into all
647 * RGBA channels. We look at the red channel here.
650 shadow_compare(uint compare_func
,
651 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
652 const float p
[QUAD_SIZE
],
656 switch (compare_func
) {
658 k
= p
[j
] < rgba
[0][j
];
660 case PIPE_FUNC_LEQUAL
:
661 k
= p
[j
] <= rgba
[0][j
];
663 case PIPE_FUNC_GREATER
:
664 k
= p
[j
] > rgba
[0][j
];
666 case PIPE_FUNC_GEQUAL
:
667 k
= p
[j
] >= rgba
[0][j
];
669 case PIPE_FUNC_EQUAL
:
670 k
= p
[j
] == rgba
[0][j
];
672 case PIPE_FUNC_NOTEQUAL
:
673 k
= p
[j
] != rgba
[0][j
];
675 case PIPE_FUNC_ALWAYS
:
678 case PIPE_FUNC_NEVER
:
687 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = (float) k
;
692 * Common code for sampling 1D/2D/cube textures.
693 * Could probably extend for 3D...
696 sp_get_samples_2d_common(struct tgsi_sampler
*tgsi_sampler
,
697 const float s
[QUAD_SIZE
],
698 const float t
[QUAD_SIZE
],
699 const float p
[QUAD_SIZE
],
701 float rgba
[NUM_CHANNELS
][QUAD_SIZE
],
702 const unsigned faces
[4])
704 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
705 const struct softpipe_context
*sp
= samp
->sp
;
706 const uint unit
= samp
->unit
;
707 const struct pipe_texture
*texture
= sp
->texture
[unit
];
708 const struct pipe_sampler_state
*sampler
= sp
->sampler
[unit
];
709 const uint compare_func
= sampler
->compare_func
;
710 unsigned level0
, level1
, j
, imgFilter
;
714 choose_mipmap_levels(texture
, sampler
, s
, t
, p
, lodbias
,
715 &level0
, &level1
, &levelBlend
, &imgFilter
);
717 assert(sampler
->normalized_coords
);
719 width
= texture
->width
[level0
];
720 height
= texture
->height
[level0
];
725 case PIPE_TEX_FILTER_NEAREST
:
726 for (j
= 0; j
< QUAD_SIZE
; j
++) {
727 int x
= nearest_texcoord(sampler
->wrap_s
, s
[j
], width
);
728 int y
= nearest_texcoord(sampler
->wrap_t
, t
[j
], height
);
729 get_texel(tgsi_sampler
, faces
[j
], level0
, x
, y
, 0, rgba
, j
);
730 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
731 shadow_compare(compare_func
, rgba
, p
, j
);
734 if (level0
!= level1
) {
735 /* get texels from second mipmap level and blend */
740 get_texel(tgsi_sampler
, faces
[j
], level1
, x
, y
, 0, rgba2
, j
);
741 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
742 shadow_compare(compare_func
, rgba2
, p
, j
);
745 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
746 rgba
[c
][j
] = LERP(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
751 case PIPE_TEX_FILTER_LINEAR
:
752 case PIPE_TEX_FILTER_ANISO
:
753 for (j
= 0; j
< QUAD_SIZE
; j
++) {
754 float tx
[4][4], a
, b
;
755 int x0
, y0
, x1
, y1
, c
;
756 linear_texcoord(sampler
->wrap_s
, s
[j
], width
, &x0
, &x1
, &a
);
757 linear_texcoord(sampler
->wrap_t
, t
[j
], height
, &y0
, &y1
, &b
);
758 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
, y0
, 0, tx
, 0);
759 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
, y0
, 0, tx
, 1);
760 get_texel(tgsi_sampler
, faces
[j
], level0
, x0
, y1
, 0, tx
, 2);
761 get_texel(tgsi_sampler
, faces
[j
], level0
, x1
, y1
, 0, tx
, 3);
762 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
763 shadow_compare(compare_func
, tx
, p
, 0);
764 shadow_compare(compare_func
, tx
, p
, 1);
765 shadow_compare(compare_func
, tx
, p
, 2);
766 shadow_compare(compare_func
, tx
, p
, 3);
769 for (c
= 0; c
< 4; c
++) {
770 rgba
[c
][j
] = lerp_2d(a
, b
, tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
773 if (level0
!= level1
) {
774 /* get texels from second mipmap level and blend */
780 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
, y0
, 0, tx
, 0);
781 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
, y0
, 0, tx
, 1);
782 get_texel(tgsi_sampler
, faces
[j
], level1
, x0
, y1
, 0, tx
, 2);
783 get_texel(tgsi_sampler
, faces
[j
], level1
, x1
, y1
, 0, tx
, 3);
784 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
){
785 shadow_compare(compare_func
, tx
, p
, 0);
786 shadow_compare(compare_func
, tx
, p
, 1);
787 shadow_compare(compare_func
, tx
, p
, 2);
788 shadow_compare(compare_func
, tx
, p
, 3);
791 for (c
= 0; c
< 4; c
++) {
792 rgba2
[c
][j
] = lerp_2d(a
, b
,
793 tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
796 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
797 rgba
[c
][j
] = LERP(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
809 sp_get_samples_1d(struct tgsi_sampler
*sampler
,
810 const float s
[QUAD_SIZE
],
811 const float t
[QUAD_SIZE
],
812 const float p
[QUAD_SIZE
],
814 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
816 static const unsigned faces
[4] = {0, 0, 0, 0};
817 static const float tzero
[4] = {0, 0, 0, 0};
818 sp_get_samples_2d_common(sampler
, s
, tzero
, NULL
, lodbias
, rgba
, faces
);
823 sp_get_samples_2d(struct tgsi_sampler
*sampler
,
824 const float s
[QUAD_SIZE
],
825 const float t
[QUAD_SIZE
],
826 const float p
[QUAD_SIZE
],
828 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
830 static const unsigned faces
[4] = {0, 0, 0, 0};
831 sp_get_samples_2d_common(sampler
, s
, t
, p
, lodbias
, rgba
, faces
);
836 sp_get_samples_3d(struct tgsi_sampler
*tgsi_sampler
,
837 const float s
[QUAD_SIZE
],
838 const float t
[QUAD_SIZE
],
839 const float p
[QUAD_SIZE
],
841 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
843 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
844 const struct softpipe_context
*sp
= samp
->sp
;
845 const uint unit
= samp
->unit
;
846 const struct pipe_texture
*texture
= sp
->texture
[unit
];
847 const struct pipe_sampler_state
*sampler
= sp
->sampler
[unit
];
848 /* get/map pipe_surfaces corresponding to 3D tex slices */
849 unsigned level0
, level1
, j
, imgFilter
;
850 int width
, height
, depth
;
854 choose_mipmap_levels(texture
, sampler
, s
, t
, p
, lodbias
,
855 &level0
, &level1
, &levelBlend
, &imgFilter
);
857 assert(sampler
->normalized_coords
);
859 width
= texture
->width
[level0
];
860 height
= texture
->height
[level0
];
861 depth
= texture
->depth
[level0
];
868 case PIPE_TEX_FILTER_NEAREST
:
869 for (j
= 0; j
< QUAD_SIZE
; j
++) {
870 int x
= nearest_texcoord(sampler
->wrap_s
, s
[j
], width
);
871 int y
= nearest_texcoord(sampler
->wrap_t
, t
[j
], height
);
872 int z
= nearest_texcoord(sampler
->wrap_r
, p
[j
], depth
);
873 get_texel(tgsi_sampler
, face
, level0
, x
, y
, z
, rgba
, j
);
875 if (level0
!= level1
) {
876 /* get texels from second mipmap level and blend */
882 get_texel(tgsi_sampler
, face
, level1
, x
, y
, z
, rgba2
, j
);
883 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
884 rgba
[c
][j
] = LERP(levelBlend
, rgba2
[c
][j
], rgba
[c
][j
]);
889 case PIPE_TEX_FILTER_LINEAR
:
890 case PIPE_TEX_FILTER_ANISO
:
891 for (j
= 0; j
< QUAD_SIZE
; j
++) {
892 float texel0
[4][4], texel1
[4][4];
893 float xw
, yw
, zw
; /* interpolation weights */
894 int x0
, x1
, y0
, y1
, z0
, z1
, c
;
895 linear_texcoord(sampler
->wrap_s
, s
[j
], width
, &x0
, &x1
, &xw
);
896 linear_texcoord(sampler
->wrap_t
, t
[j
], height
, &y0
, &y1
, &yw
);
897 linear_texcoord(sampler
->wrap_r
, p
[j
], depth
, &z0
, &z1
, &zw
);
898 get_texel(tgsi_sampler
, face
, level0
, x0
, y0
, z0
, texel0
, 0);
899 get_texel(tgsi_sampler
, face
, level0
, x1
, y0
, z0
, texel0
, 1);
900 get_texel(tgsi_sampler
, face
, level0
, x0
, y1
, z0
, texel0
, 2);
901 get_texel(tgsi_sampler
, face
, level0
, x1
, y1
, z0
, texel0
, 3);
902 get_texel(tgsi_sampler
, face
, level0
, x0
, y0
, z1
, texel1
, 0);
903 get_texel(tgsi_sampler
, face
, level0
, x1
, y0
, z1
, texel1
, 1);
904 get_texel(tgsi_sampler
, face
, level0
, x0
, y1
, z1
, texel1
, 2);
905 get_texel(tgsi_sampler
, face
, level0
, x1
, y1
, z1
, texel1
, 3);
908 for (c
= 0; c
< 4; c
++) {
909 float ctemp0
[4][4], ctemp1
[4][4];
910 ctemp0
[c
][j
] = lerp_2d(xw
, yw
,
911 texel0
[c
][0], texel0
[c
][1],
912 texel0
[c
][2], texel0
[c
][3]);
913 ctemp1
[c
][j
] = lerp_2d(xw
, yw
,
914 texel1
[c
][0], texel1
[c
][1],
915 texel1
[c
][2], texel1
[c
][3]);
916 rgba
[c
][j
] = LERP(zw
, ctemp0
[c
][j
], ctemp1
[c
][j
]);
919 if (level0
!= level1
) {
920 /* get texels from second mipmap level and blend */
928 get_texel(tgsi_sampler
, face
, level1
, x0
, y0
, z0
, texel0
, 0);
929 get_texel(tgsi_sampler
, face
, level1
, x1
, y0
, z0
, texel0
, 1);
930 get_texel(tgsi_sampler
, face
, level1
, x0
, y1
, z0
, texel0
, 2);
931 get_texel(tgsi_sampler
, face
, level1
, x1
, y1
, z0
, texel0
, 3);
932 get_texel(tgsi_sampler
, face
, level1
, x0
, y0
, z1
, texel1
, 0);
933 get_texel(tgsi_sampler
, face
, level1
, x1
, y0
, z1
, texel1
, 1);
934 get_texel(tgsi_sampler
, face
, level1
, x0
, y1
, z1
, texel1
, 2);
935 get_texel(tgsi_sampler
, face
, level1
, x1
, y1
, z1
, texel1
, 3);
938 for (c
= 0; c
< 4; c
++) {
939 float ctemp0
[4][4], ctemp1
[4][4];
940 ctemp0
[c
][j
] = lerp_2d(xw
, yw
,
941 texel0
[c
][0], texel0
[c
][1],
942 texel0
[c
][2], texel0
[c
][3]);
943 ctemp1
[c
][j
] = lerp_2d(xw
, yw
,
944 texel1
[c
][0], texel1
[c
][1],
945 texel1
[c
][2], texel1
[c
][3]);
946 rgba2
[c
][j
] = LERP(zw
, ctemp0
[c
][j
], ctemp1
[c
][j
]);
949 /* blend mipmap levels */
950 for (c
= 0; c
< NUM_CHANNELS
; c
++) {
951 rgba
[c
][j
] = LERP(levelBlend
, rgba
[c
][j
], rgba2
[c
][j
]);
963 sp_get_samples_cube(struct tgsi_sampler
*sampler
,
964 const float s
[QUAD_SIZE
],
965 const float t
[QUAD_SIZE
],
966 const float p
[QUAD_SIZE
],
968 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
970 unsigned faces
[QUAD_SIZE
], j
;
971 float ssss
[4], tttt
[4];
972 for (j
= 0; j
< QUAD_SIZE
; j
++) {
973 faces
[j
] = choose_cube_face(s
[j
], t
[j
], p
[j
], ssss
+ j
, tttt
+ j
);
975 sp_get_samples_2d_common(sampler
, ssss
, tttt
, NULL
, lodbias
, rgba
, faces
);
980 sp_get_samples_rect(struct tgsi_sampler
*tgsi_sampler
,
981 const float s
[QUAD_SIZE
],
982 const float t
[QUAD_SIZE
],
983 const float p
[QUAD_SIZE
],
985 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
987 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
988 const struct softpipe_context
*sp
= samp
->sp
;
989 const uint unit
= samp
->unit
;
990 const struct pipe_texture
*texture
= sp
->texture
[unit
];
991 const struct pipe_sampler_state
*sampler
= sp
->sampler
[unit
];
992 //sp_get_samples_2d_common(sampler, s, t, p, lodbias, rgba, faces);
993 static const uint face
= 0;
994 const uint compare_func
= sampler
->compare_func
;
995 unsigned level0
, level1
, j
, imgFilter
;
999 choose_mipmap_levels(texture
, sampler
, s
, t
, p
, lodbias
,
1000 &level0
, &level1
, &levelBlend
, &imgFilter
);
1002 /* texture RECTS cannot be mipmapped */
1003 assert(level0
== level1
);
1005 width
= texture
->width
[level0
];
1006 height
= texture
->height
[level0
];
1010 switch (imgFilter
) {
1011 case PIPE_TEX_FILTER_NEAREST
:
1012 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1013 int x
= nearest_texcoord_unnorm(sampler
->wrap_s
, s
[j
], width
);
1014 int y
= nearest_texcoord_unnorm(sampler
->wrap_t
, t
[j
], height
);
1015 get_texel(tgsi_sampler
, face
, level0
, x
, y
, 0, rgba
, j
);
1016 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1017 shadow_compare(compare_func
, rgba
, p
, j
);
1021 case PIPE_TEX_FILTER_LINEAR
:
1022 case PIPE_TEX_FILTER_ANISO
:
1023 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1024 float tx
[4][4], a
, b
;
1025 int x0
, y0
, x1
, y1
, c
;
1026 linear_texcoord_unnorm(sampler
->wrap_s
, s
[j
], width
, &x0
, &x1
, &a
);
1027 linear_texcoord_unnorm(sampler
->wrap_t
, t
[j
], height
, &y0
, &y1
, &b
);
1028 get_texel(tgsi_sampler
, face
, level0
, x0
, y0
, 0, tx
, 0);
1029 get_texel(tgsi_sampler
, face
, level0
, x1
, y0
, 0, tx
, 1);
1030 get_texel(tgsi_sampler
, face
, level0
, x0
, y1
, 0, tx
, 2);
1031 get_texel(tgsi_sampler
, face
, level0
, x1
, y1
, 0, tx
, 3);
1032 if (sampler
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
1033 shadow_compare(compare_func
, tx
, p
, 0);
1034 shadow_compare(compare_func
, tx
, p
, 1);
1035 shadow_compare(compare_func
, tx
, p
, 2);
1036 shadow_compare(compare_func
, tx
, p
, 3);
1039 for (c
= 0; c
< 4; c
++) {
1040 rgba
[c
][j
] = lerp_2d(a
, b
, tx
[c
][0], tx
[c
][1], tx
[c
][2], tx
[c
][3]);
1053 * Called via tgsi_sampler::get_samples()
1054 * Use the sampler's state setting to get a filtered RGBA value
1055 * from the sampler's texture.
1057 * XXX we can implement many versions of this function, each
1058 * tightly coded for a specific combination of sampler state
1059 * (nearest + repeat), (bilinear mipmap + clamp), etc.
1061 * The update_samplers() function in st_atom_sampler.c could create
1062 * a new tgsi_sampler object for each state combo it finds....
1065 sp_get_samples(struct tgsi_sampler
*tgsi_sampler
,
1066 const float s
[QUAD_SIZE
],
1067 const float t
[QUAD_SIZE
],
1068 const float p
[QUAD_SIZE
],
1070 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1072 const struct sp_shader_sampler
*samp
= sp_shader_sampler(tgsi_sampler
);
1073 const struct softpipe_context
*sp
= samp
->sp
;
1074 const uint unit
= samp
->unit
;
1075 const struct pipe_texture
*texture
= sp
->texture
[unit
];
1076 const struct pipe_sampler_state
*sampler
= sp
->sampler
[unit
];
1081 switch (texture
->target
) {
1082 case PIPE_TEXTURE_1D
:
1083 assert(sampler
->normalized_coords
);
1084 sp_get_samples_1d(tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1086 case PIPE_TEXTURE_2D
:
1087 if (sampler
->normalized_coords
)
1088 sp_get_samples_2d(tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1090 sp_get_samples_rect(tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1092 case PIPE_TEXTURE_3D
:
1093 assert(sampler
->normalized_coords
);
1094 sp_get_samples_3d(tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1096 case PIPE_TEXTURE_CUBE
:
1097 assert(sampler
->normalized_coords
);
1098 sp_get_samples_cube(tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1107 printf("Sampled at %f, %f, %f:\n", s
[0], t
[0], p
[0]);
1108 for (i
= 0; i
< 4; i
++) {
1109 printf("Frag %d: %f %f %f %f\n", i
,