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 **************************************************************************/
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "sp_quad.h" /* only for #define QUAD_* tokens */
43 #include "sp_tex_sample.h"
44 #include "sp_tex_tile_cache.h"
49 * 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
62 lerp(float a
, float v0
, float v1
)
64 return v0
+ a
* (v1
- v0
);
69 * Do 2D/biliner interpolation of float values.
70 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
71 * a and b are the horizontal and vertical interpolants.
72 * It's important that this function is inlined when compiled with
73 * optimization! If we find that's not true on some systems, convert
77 lerp_2d(float a
, float b
,
78 float v00
, float v10
, float v01
, float v11
)
80 const float temp0
= lerp(a
, v00
, v10
);
81 const float temp1
= lerp(a
, v01
, v11
);
82 return lerp(b
, temp0
, temp1
);
87 * As above, but 3D interpolation of 8 values.
90 lerp_3d(float a
, float b
, float c
,
91 float v000
, float v100
, float v010
, float v110
,
92 float v001
, float v101
, float v011
, float v111
)
94 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
95 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
96 return lerp(c
, temp0
, temp1
);
102 * If A is a signed integer, A % B doesn't give the right value for A < 0
103 * (in terms of texture repeat). Just casting to unsigned fixes that.
105 #define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
109 * Apply texture coord wrapping mode and return integer texture indexes
110 * for a vector of four texcoords (S or T or P).
111 * \param wrapMode PIPE_TEX_WRAP_x
112 * \param s the incoming texcoords
113 * \param size the texture image size
114 * \param icoord returns the integer texcoords
115 * \return integer texture index
118 wrap_nearest_repeat(const float s
[4], unsigned size
, int icoord
[4])
121 /* s limited to [0,1) */
122 /* i limited to [0,size-1] */
123 for (ch
= 0; ch
< 4; ch
++) {
124 int i
= util_ifloor(s
[ch
] * size
);
125 icoord
[ch
] = REMAINDER(i
, size
);
131 wrap_nearest_clamp(const float s
[4], unsigned size
, int icoord
[4])
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
);
148 wrap_nearest_clamp_to_edge(const float s
[4], unsigned size
, int icoord
[4])
151 /* s limited to [min,max] */
152 /* i limited to [0, size-1] */
153 const float min
= 1.0F
/ (2.0F
* size
);
154 const float max
= 1.0F
- min
;
155 for (ch
= 0; ch
< 4; ch
++) {
158 else if (s
[ch
] > max
)
159 icoord
[ch
] = size
- 1;
161 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
167 wrap_nearest_clamp_to_border(const float s
[4], unsigned size
, int icoord
[4])
170 /* s limited to [min,max] */
171 /* i limited to [-1, size] */
172 const float min
= -1.0F
/ (2.0F
* size
);
173 const float max
= 1.0F
- min
;
174 for (ch
= 0; ch
< 4; ch
++) {
177 else if (s
[ch
] >= max
)
180 icoord
[ch
] = util_ifloor(s
[ch
] * size
);
186 wrap_nearest_mirror_repeat(const float s
[4], unsigned size
, int icoord
[4])
189 const float min
= 1.0F
/ (2.0F
* size
);
190 const float max
= 1.0F
- min
;
191 for (ch
= 0; ch
< 4; ch
++) {
192 const int flr
= util_ifloor(s
[ch
]);
195 u
= 1.0F
- (s
[ch
] - (float) flr
);
197 u
= s
[ch
] - (float) flr
;
201 icoord
[ch
] = size
- 1;
203 icoord
[ch
] = util_ifloor(u
* size
);
209 wrap_nearest_mirror_clamp(const float s
[4], unsigned size
, int icoord
[4])
212 for (ch
= 0; ch
< 4; ch
++) {
213 /* s limited to [0,1] */
214 /* i limited to [0,size-1] */
215 const float u
= fabsf(s
[ch
]);
219 icoord
[ch
] = size
- 1;
221 icoord
[ch
] = util_ifloor(u
* size
);
227 wrap_nearest_mirror_clamp_to_edge(const float s
[4], unsigned size
,
231 /* s limited to [min,max] */
232 /* i limited to [0, size-1] */
233 const float min
= 1.0F
/ (2.0F
* size
);
234 const float max
= 1.0F
- min
;
235 for (ch
= 0; ch
< 4; ch
++) {
236 const float u
= fabsf(s
[ch
]);
240 icoord
[ch
] = size
- 1;
242 icoord
[ch
] = util_ifloor(u
* size
);
248 wrap_nearest_mirror_clamp_to_border(const float s
[4], unsigned size
,
252 /* s limited to [min,max] */
253 /* i limited to [0, size-1] */
254 const float min
= -1.0F
/ (2.0F
* size
);
255 const float max
= 1.0F
- min
;
256 for (ch
= 0; ch
< 4; ch
++) {
257 const float u
= fabsf(s
[ch
]);
263 icoord
[ch
] = util_ifloor(u
* size
);
269 * Used to compute texel locations for linear sampling for four texcoords.
270 * \param wrapMode PIPE_TEX_WRAP_x
271 * \param s the texcoords
272 * \param size the texture image size
273 * \param icoord0 returns first texture indexes
274 * \param icoord1 returns second texture indexes (usually icoord0 + 1)
275 * \param w returns blend factor/weight between texture indexes
276 * \param icoord returns the computed integer texture coords
279 wrap_linear_repeat(const float s
[4], unsigned size
,
280 int icoord0
[4], int icoord1
[4], float w
[4])
283 for (ch
= 0; ch
< 4; ch
++) {
284 float u
= s
[ch
] * size
- 0.5F
;
285 icoord0
[ch
] = REMAINDER(util_ifloor(u
), size
);
286 icoord1
[ch
] = REMAINDER(icoord0
[ch
] + 1, size
);
293 wrap_linear_clamp(const float s
[4], unsigned size
,
294 int icoord0
[4], int icoord1
[4], float w
[4])
297 for (ch
= 0; ch
< 4; ch
++) {
298 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
300 icoord0
[ch
] = util_ifloor(u
);
301 icoord1
[ch
] = icoord0
[ch
] + 1;
308 wrap_linear_clamp_to_edge(const float s
[4], unsigned size
,
309 int icoord0
[4], int icoord1
[4], float w
[4])
312 for (ch
= 0; ch
< 4; ch
++) {
313 float u
= CLAMP(s
[ch
], 0.0F
, 1.0F
);
315 icoord0
[ch
] = util_ifloor(u
);
316 icoord1
[ch
] = icoord0
[ch
] + 1;
319 if (icoord1
[ch
] >= (int) size
)
320 icoord1
[ch
] = size
- 1;
327 wrap_linear_clamp_to_border(const float s
[4], unsigned size
,
328 int icoord0
[4], int icoord1
[4], float w
[4])
330 const float min
= -1.0F
/ (2.0F
* size
);
331 const float max
= 1.0F
- min
;
333 for (ch
= 0; ch
< 4; ch
++) {
334 float u
= CLAMP(s
[ch
], min
, max
);
336 icoord0
[ch
] = util_ifloor(u
);
337 icoord1
[ch
] = icoord0
[ch
] + 1;
344 wrap_linear_mirror_repeat(const float s
[4], unsigned size
,
345 int icoord0
[4], int icoord1
[4], float w
[4])
348 for (ch
= 0; ch
< 4; ch
++) {
349 const int flr
= util_ifloor(s
[ch
]);
352 u
= 1.0F
- (s
[ch
] - (float) flr
);
354 u
= s
[ch
] - (float) flr
;
356 icoord0
[ch
] = util_ifloor(u
);
357 icoord1
[ch
] = icoord0
[ch
] + 1;
360 if (icoord1
[ch
] >= (int) size
)
361 icoord1
[ch
] = size
- 1;
368 wrap_linear_mirror_clamp(const float s
[4], unsigned size
,
369 int icoord0
[4], int icoord1
[4], float w
[4])
372 for (ch
= 0; ch
< 4; ch
++) {
373 float u
= fabsf(s
[ch
]);
379 icoord0
[ch
] = util_ifloor(u
);
380 icoord1
[ch
] = icoord0
[ch
] + 1;
387 wrap_linear_mirror_clamp_to_edge(const float s
[4], unsigned size
,
388 int icoord0
[4], int icoord1
[4], float w
[4])
391 for (ch
= 0; ch
< 4; ch
++) {
392 float u
= fabsf(s
[ch
]);
398 icoord0
[ch
] = util_ifloor(u
);
399 icoord1
[ch
] = icoord0
[ch
] + 1;
402 if (icoord1
[ch
] >= (int) size
)
403 icoord1
[ch
] = size
- 1;
410 wrap_linear_mirror_clamp_to_border(const float s
[4], unsigned size
,
411 int icoord0
[4], int icoord1
[4], float w
[4])
413 const float min
= -1.0F
/ (2.0F
* size
);
414 const float max
= 1.0F
- min
;
416 for (ch
= 0; ch
< 4; ch
++) {
417 float u
= fabsf(s
[ch
]);
425 icoord0
[ch
] = util_ifloor(u
);
426 icoord1
[ch
] = icoord0
[ch
] + 1;
433 * For RECT textures / unnormalized texcoords
434 * Only a subset of wrap modes supported.
437 wrap_nearest_unorm_clamp(const float s
[4], unsigned size
, int icoord
[4])
440 for (ch
= 0; ch
< 4; ch
++) {
441 int i
= util_ifloor(s
[ch
]);
442 icoord
[ch
]= CLAMP(i
, 0, (int) size
-1);
448 * Handles clamp_to_edge and clamp_to_border:
451 wrap_nearest_unorm_clamp_to_border(const float s
[4], unsigned size
,
455 for (ch
= 0; ch
< 4; ch
++) {
456 icoord
[ch
]= util_ifloor( CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
) );
462 * For RECT textures / unnormalized texcoords.
463 * Only a subset of wrap modes supported.
466 wrap_linear_unorm_clamp(const float s
[4], unsigned size
,
467 int icoord0
[4], int icoord1
[4], float w
[4])
470 for (ch
= 0; ch
< 4; ch
++) {
471 /* Not exactly what the spec says, but it matches NVIDIA output */
472 float u
= CLAMP(s
[ch
] - 0.5F
, 0.0f
, (float) size
- 1.0f
);
473 icoord0
[ch
] = util_ifloor(u
);
474 icoord1
[ch
] = icoord0
[ch
] + 1;
481 wrap_linear_unorm_clamp_to_border(const float s
[4], unsigned size
,
482 int icoord0
[4], int icoord1
[4], float w
[4])
485 for (ch
= 0; ch
< 4; ch
++) {
486 float u
= CLAMP(s
[ch
], 0.5F
, (float) size
- 0.5F
);
488 icoord0
[ch
] = util_ifloor(u
);
489 icoord1
[ch
] = icoord0
[ch
] + 1;
490 if (icoord1
[ch
] > (int) size
- 1)
491 icoord1
[ch
] = size
- 1;
499 * Examine the quad's texture coordinates to compute the partial
500 * derivatives w.r.t X and Y, then compute lambda (level of detail).
503 compute_lambda_1d(const struct sp_sampler_varient
*samp
,
504 const float s
[QUAD_SIZE
],
505 const float t
[QUAD_SIZE
],
506 const float p
[QUAD_SIZE
],
509 const struct pipe_texture
*texture
= samp
->texture
;
510 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
511 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
512 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
513 float rho
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
516 lambda
= util_fast_log2(rho
);
517 lambda
+= lodbias
+ sampler
->lod_bias
;
518 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
525 compute_lambda_2d(const struct sp_sampler_varient
*samp
,
526 const float s
[QUAD_SIZE
],
527 const float t
[QUAD_SIZE
],
528 const float p
[QUAD_SIZE
],
531 const struct pipe_texture
*texture
= samp
->texture
;
532 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
533 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
534 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
535 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
536 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
537 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
538 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height
[0];
539 float rho
= MAX2(maxx
, maxy
);
542 lambda
= util_fast_log2(rho
);
543 lambda
+= lodbias
+ sampler
->lod_bias
;
544 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
551 compute_lambda_3d(const struct sp_sampler_varient
*samp
,
552 const float s
[QUAD_SIZE
],
553 const float t
[QUAD_SIZE
],
554 const float p
[QUAD_SIZE
],
557 const struct pipe_texture
*texture
= samp
->texture
;
558 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
559 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
560 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
561 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
562 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
563 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
564 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
565 float maxx
= MAX2(dsdx
, dsdy
) * texture
->width
[0];
566 float maxy
= MAX2(dtdx
, dtdy
) * texture
->height
[0];
567 float maxz
= MAX2(dpdx
, dpdy
) * texture
->depth
[0];
570 rho
= MAX2(maxx
, maxy
);
571 rho
= MAX2(rho
, maxz
);
573 lambda
= util_fast_log2(rho
);
574 lambda
+= lodbias
+ sampler
->lod_bias
;
575 lambda
= CLAMP(lambda
, sampler
->min_lod
, sampler
->max_lod
);
582 * Compute lambda for a vertex texture sampler.
583 * Since there aren't derivatives to use, just return the LOD bias.
586 compute_lambda_vert(const struct sp_sampler_varient
*samp
,
587 const float s
[QUAD_SIZE
],
588 const float t
[QUAD_SIZE
],
589 const float p
[QUAD_SIZE
],
598 * Get a texel from a texture, using the texture tile cache.
600 * \param addr the template tex address containing cube, z, face info.
601 * \param x the x coord of texel within 2D image
602 * \param y the y coord of texel within 2D image
603 * \param rgba the quad to put the texel/color into
605 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
606 * sp_get_cached_tile_tex() function. Also, get 4 texels instead of 1...
612 static INLINE
const float *
613 get_texel_2d_no_border(const struct sp_sampler_varient
*samp
,
614 union tex_tile_address addr
, int x
, int y
)
616 const struct softpipe_tex_cached_tile
*tile
;
618 addr
.bits
.x
= x
/ TILE_SIZE
;
619 addr
.bits
.y
= y
/ TILE_SIZE
;
623 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
625 return &tile
->data
.color
[y
][x
][0];
629 static INLINE
const float *
630 get_texel_2d(const struct sp_sampler_varient
*samp
,
631 union tex_tile_address addr
, int x
, int y
)
633 const struct pipe_texture
*texture
= samp
->texture
;
634 unsigned level
= addr
.bits
.level
;
636 if (x
< 0 || x
>= (int) texture
->width
[level
] ||
637 y
< 0 || y
>= (int) texture
->height
[level
]) {
638 return samp
->sampler
->border_color
;
641 return get_texel_2d_no_border( samp
, addr
, x
, y
);
646 /* Gather a quad of adjacent texels within a tile:
649 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient
*samp
,
650 union tex_tile_address addr
,
651 unsigned x
, unsigned y
,
654 const struct softpipe_tex_cached_tile
*tile
;
656 addr
.bits
.x
= x
/ TILE_SIZE
;
657 addr
.bits
.y
= y
/ TILE_SIZE
;
661 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
663 out
[0] = &tile
->data
.color
[y
][x
][0];
664 out
[1] = &tile
->data
.color
[y
][x
+1][0];
665 out
[2] = &tile
->data
.color
[y
+1][x
][0];
666 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
670 /* Gather a quad of potentially non-adjacent texels:
673 get_texel_quad_2d_no_border(const struct sp_sampler_varient
*samp
,
674 union tex_tile_address addr
,
679 out
[0] = get_texel_2d_no_border( samp
, addr
, x0
, y0
);
680 out
[1] = get_texel_2d_no_border( samp
, addr
, x1
, y0
);
681 out
[2] = get_texel_2d_no_border( samp
, addr
, x0
, y1
);
682 out
[3] = get_texel_2d_no_border( samp
, addr
, x1
, y1
);
685 /* Can involve a lot of unnecessary checks for border color:
688 get_texel_quad_2d(const struct sp_sampler_varient
*samp
,
689 union tex_tile_address addr
,
694 out
[0] = get_texel_2d( samp
, addr
, x0
, y0
);
695 out
[1] = get_texel_2d( samp
, addr
, x1
, y0
);
696 out
[3] = get_texel_2d( samp
, addr
, x1
, y1
);
697 out
[2] = get_texel_2d( samp
, addr
, x0
, y1
);
704 static INLINE
const float *
705 get_texel_3d_no_border(const struct sp_sampler_varient
*samp
,
706 union tex_tile_address addr
, int x
, int y
, int z
)
708 const struct softpipe_tex_cached_tile
*tile
;
710 addr
.bits
.x
= x
/ TILE_SIZE
;
711 addr
.bits
.y
= y
/ TILE_SIZE
;
716 tile
= sp_get_cached_tile_tex(samp
->cache
, addr
);
718 return &tile
->data
.color
[y
][x
][0];
722 static INLINE
const float *
723 get_texel_3d(const struct sp_sampler_varient
*samp
,
724 union tex_tile_address addr
, int x
, int y
, int z
)
726 const struct pipe_texture
*texture
= samp
->texture
;
727 unsigned level
= addr
.bits
.level
;
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 return samp
->sampler
->border_color
;
735 return get_texel_3d_no_border( samp
, addr
, x
, y
, z
);
741 * Given the logbase2 of a mipmap's base level size and a mipmap level,
742 * return the size (in texels) of that mipmap level.
743 * For example, if level[0].width = 256 then base_pot will be 8.
744 * If level = 2, then we'll return 64 (the width at level=2).
745 * Return 1 if level > base_pot.
747 static INLINE
unsigned
748 pot_level_size(unsigned base_pot
, unsigned level
)
750 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
754 /* Some image-filter fastpaths:
757 img_filter_2d_linear_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
758 const float s
[QUAD_SIZE
],
759 const float t
[QUAD_SIZE
],
760 const float p
[QUAD_SIZE
],
762 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
764 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
766 unsigned level
= samp
->level
;
767 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
768 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
769 unsigned xmax
= (xpot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, xpot) - 1; */
770 unsigned ymax
= (ypot
- 1) & (TILE_SIZE
- 1); /* MIN2(TILE_SIZE, ypot) - 1; */
771 union tex_tile_address addr
;
774 addr
.bits
.level
= samp
->level
;
776 for (j
= 0; j
< QUAD_SIZE
; j
++) {
779 float u
= s
[j
] * xpot
- 0.5F
;
780 float v
= t
[j
] * ypot
- 0.5F
;
782 int uflr
= util_ifloor(u
);
783 int vflr
= util_ifloor(v
);
785 float xw
= u
- (float)uflr
;
786 float yw
= v
- (float)vflr
;
788 int x0
= uflr
& (xpot
- 1);
789 int y0
= vflr
& (ypot
- 1);
793 /* Can we fetch all four at once:
795 if (x0
< xmax
&& y0
< ymax
) {
796 get_texel_quad_2d_no_border_single_tile(samp
, addr
, x0
, y0
, tx
);
799 unsigned x1
= (x0
+ 1) & (xpot
- 1);
800 unsigned y1
= (y0
+ 1) & (ypot
- 1);
801 get_texel_quad_2d_no_border(samp
, addr
, x0
, y0
, x1
, y1
, tx
);
804 /* interpolate R, G, B, A */
805 for (c
= 0; c
< 4; c
++) {
806 rgba
[c
][j
] = lerp_2d(xw
, yw
,
815 img_filter_2d_nearest_repeat_POT(struct tgsi_sampler
*tgsi_sampler
,
816 const float s
[QUAD_SIZE
],
817 const float t
[QUAD_SIZE
],
818 const float p
[QUAD_SIZE
],
820 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
822 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
824 unsigned level
= samp
->level
;
825 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
826 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
827 union tex_tile_address addr
;
830 addr
.bits
.level
= samp
->level
;
832 for (j
= 0; j
< QUAD_SIZE
; j
++) {
835 float u
= s
[j
] * xpot
;
836 float v
= t
[j
] * ypot
;
838 int uflr
= util_ifloor(u
);
839 int vflr
= util_ifloor(v
);
841 int x0
= uflr
& (xpot
- 1);
842 int y0
= vflr
& (ypot
- 1);
844 const float *out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
846 for (c
= 0; c
< 4; c
++) {
854 img_filter_2d_nearest_clamp_POT(struct tgsi_sampler
*tgsi_sampler
,
855 const float s
[QUAD_SIZE
],
856 const float t
[QUAD_SIZE
],
857 const float p
[QUAD_SIZE
],
859 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
861 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
863 unsigned level
= samp
->level
;
864 unsigned xpot
= pot_level_size(samp
->xpot
, level
);
865 unsigned ypot
= pot_level_size(samp
->ypot
, level
);
866 union tex_tile_address addr
;
869 addr
.bits
.level
= samp
->level
;
871 for (j
= 0; j
< QUAD_SIZE
; j
++) {
874 float u
= s
[j
] * xpot
;
875 float v
= t
[j
] * ypot
;
883 else if (x0
> xpot
- 1)
889 else if (y0
> ypot
- 1)
892 out
= get_texel_2d_no_border(samp
, addr
, x0
, y0
);
894 for (c
= 0; c
< 4; c
++) {
902 img_filter_1d_nearest(struct tgsi_sampler
*tgsi_sampler
,
903 const float s
[QUAD_SIZE
],
904 const float t
[QUAD_SIZE
],
905 const float p
[QUAD_SIZE
],
907 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
909 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
910 const struct pipe_texture
*texture
= samp
->texture
;
914 union tex_tile_address addr
;
916 level0
= samp
->level
;
917 width
= texture
->width
[level0
];
922 addr
.bits
.level
= samp
->level
;
924 samp
->nearest_texcoord_s(s
, width
, x
);
926 for (j
= 0; j
< QUAD_SIZE
; j
++) {
927 const float *out
= get_texel_2d(samp
, addr
, x
[j
], 0);
929 for (c
= 0; c
< 4; c
++) {
937 img_filter_2d_nearest(struct tgsi_sampler
*tgsi_sampler
,
938 const float s
[QUAD_SIZE
],
939 const float t
[QUAD_SIZE
],
940 const float p
[QUAD_SIZE
],
942 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
944 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
945 const struct pipe_texture
*texture
= samp
->texture
;
949 union tex_tile_address addr
;
952 level0
= samp
->level
;
953 width
= texture
->width
[level0
];
954 height
= texture
->height
[level0
];
960 addr
.bits
.level
= samp
->level
;
962 samp
->nearest_texcoord_s(s
, width
, x
);
963 samp
->nearest_texcoord_t(t
, height
, y
);
965 for (j
= 0; j
< QUAD_SIZE
; j
++) {
966 const float *out
= get_texel_2d(samp
, addr
, x
[j
], y
[j
]);
968 for (c
= 0; c
< 4; c
++) {
975 static inline union tex_tile_address
976 face(union tex_tile_address addr
, unsigned face
)
978 addr
.bits
.face
= face
;
984 img_filter_cube_nearest(struct tgsi_sampler
*tgsi_sampler
,
985 const float s
[QUAD_SIZE
],
986 const float t
[QUAD_SIZE
],
987 const float p
[QUAD_SIZE
],
989 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
991 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
992 const struct pipe_texture
*texture
= samp
->texture
;
993 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
997 union tex_tile_address addr
;
999 level0
= samp
->level
;
1000 width
= texture
->width
[level0
];
1001 height
= texture
->height
[level0
];
1007 addr
.bits
.level
= samp
->level
;
1009 samp
->nearest_texcoord_s(s
, width
, x
);
1010 samp
->nearest_texcoord_t(t
, height
, y
);
1012 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1013 const float *out
= get_texel_2d(samp
, face(addr
, faces
[j
]), x
[j
], y
[j
]);
1015 for (c
= 0; c
< 4; c
++) {
1016 rgba
[c
][j
] = out
[c
];
1023 img_filter_3d_nearest(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 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1031 const struct pipe_texture
*texture
= samp
->texture
;
1033 int width
, height
, depth
;
1034 int x
[4], y
[4], z
[4];
1035 union tex_tile_address addr
;
1037 level0
= samp
->level
;
1038 width
= texture
->width
[level0
];
1039 height
= texture
->height
[level0
];
1040 depth
= texture
->depth
[level0
];
1046 samp
->nearest_texcoord_s(s
, width
, x
);
1047 samp
->nearest_texcoord_t(t
, height
, y
);
1048 samp
->nearest_texcoord_p(p
, depth
, z
);
1051 addr
.bits
.level
= samp
->level
;
1053 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1054 const float *out
= get_texel_3d(samp
, addr
, x
[j
], y
[j
], z
[j
]);
1056 for (c
= 0; c
< 4; c
++) {
1057 rgba
[c
][j
] = out
[c
];
1064 img_filter_1d_linear(struct tgsi_sampler
*tgsi_sampler
,
1065 const float s
[QUAD_SIZE
],
1066 const float t
[QUAD_SIZE
],
1067 const float p
[QUAD_SIZE
],
1069 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1071 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1072 const struct pipe_texture
*texture
= samp
->texture
;
1076 float xw
[4]; /* weights */
1077 union tex_tile_address addr
;
1079 level0
= samp
->level
;
1080 width
= texture
->width
[level0
];
1085 addr
.bits
.level
= samp
->level
;
1087 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1089 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1090 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], 0);
1091 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], 0);
1094 /* interpolate R, G, B, A */
1095 for (c
= 0; c
< 4; c
++) {
1096 rgba
[c
][j
] = lerp(xw
[j
], tx0
[c
], tx1
[c
]);
1103 img_filter_2d_linear(struct tgsi_sampler
*tgsi_sampler
,
1104 const float s
[QUAD_SIZE
],
1105 const float t
[QUAD_SIZE
],
1106 const float p
[QUAD_SIZE
],
1108 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1110 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1111 const struct pipe_texture
*texture
= samp
->texture
;
1114 int x0
[4], y0
[4], x1
[4], y1
[4];
1115 float xw
[4], yw
[4]; /* weights */
1116 union tex_tile_address addr
;
1118 level0
= samp
->level
;
1119 width
= texture
->width
[level0
];
1120 height
= texture
->height
[level0
];
1126 addr
.bits
.level
= samp
->level
;
1128 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1129 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1131 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1132 const float *tx0
= get_texel_2d(samp
, addr
, x0
[j
], y0
[j
]);
1133 const float *tx1
= get_texel_2d(samp
, addr
, x1
[j
], y0
[j
]);
1134 const float *tx2
= get_texel_2d(samp
, addr
, x0
[j
], y1
[j
]);
1135 const float *tx3
= get_texel_2d(samp
, addr
, x1
[j
], y1
[j
]);
1138 /* interpolate R, G, B, A */
1139 for (c
= 0; c
< 4; c
++) {
1140 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1149 img_filter_cube_linear(struct tgsi_sampler
*tgsi_sampler
,
1150 const float s
[QUAD_SIZE
],
1151 const float t
[QUAD_SIZE
],
1152 const float p
[QUAD_SIZE
],
1154 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1156 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1157 const struct pipe_texture
*texture
= samp
->texture
;
1158 const unsigned *faces
= samp
->faces
; /* zero when not cube-mapping */
1161 int x0
[4], y0
[4], x1
[4], y1
[4];
1162 float xw
[4], yw
[4]; /* weights */
1163 union tex_tile_address addr
;
1165 level0
= samp
->level
;
1166 width
= texture
->width
[level0
];
1167 height
= texture
->height
[level0
];
1173 addr
.bits
.level
= samp
->level
;
1175 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1176 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1178 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1179 union tex_tile_address addrj
= face(addr
, faces
[j
]);
1180 const float *tx0
= get_texel_2d(samp
, addrj
, x0
[j
], y0
[j
]);
1181 const float *tx1
= get_texel_2d(samp
, addrj
, x1
[j
], y0
[j
]);
1182 const float *tx2
= get_texel_2d(samp
, addrj
, x0
[j
], y1
[j
]);
1183 const float *tx3
= get_texel_2d(samp
, addrj
, x1
[j
], y1
[j
]);
1186 /* interpolate R, G, B, A */
1187 for (c
= 0; c
< 4; c
++) {
1188 rgba
[c
][j
] = lerp_2d(xw
[j
], yw
[j
],
1197 img_filter_3d_linear(struct tgsi_sampler
*tgsi_sampler
,
1198 const float s
[QUAD_SIZE
],
1199 const float t
[QUAD_SIZE
],
1200 const float p
[QUAD_SIZE
],
1202 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1204 const struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1205 const struct pipe_texture
*texture
= samp
->texture
;
1207 int width
, height
, depth
;
1208 int x0
[4], x1
[4], y0
[4], y1
[4], z0
[4], z1
[4];
1209 float xw
[4], yw
[4], zw
[4]; /* interpolation weights */
1210 union tex_tile_address addr
;
1212 level0
= samp
->level
;
1213 width
= texture
->width
[level0
];
1214 height
= texture
->height
[level0
];
1215 depth
= texture
->depth
[level0
];
1218 addr
.bits
.level
= level0
;
1224 samp
->linear_texcoord_s(s
, width
, x0
, x1
, xw
);
1225 samp
->linear_texcoord_t(t
, height
, y0
, y1
, yw
);
1226 samp
->linear_texcoord_p(p
, depth
, z0
, z1
, zw
);
1228 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1231 const float *tx00
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z0
[j
]);
1232 const float *tx01
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z0
[j
]);
1233 const float *tx02
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z0
[j
]);
1234 const float *tx03
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z0
[j
]);
1236 const float *tx10
= get_texel_3d(samp
, addr
, x0
[j
], y0
[j
], z1
[j
]);
1237 const float *tx11
= get_texel_3d(samp
, addr
, x1
[j
], y0
[j
], z1
[j
]);
1238 const float *tx12
= get_texel_3d(samp
, addr
, x0
[j
], y1
[j
], z1
[j
]);
1239 const float *tx13
= get_texel_3d(samp
, addr
, x1
[j
], y1
[j
], z1
[j
]);
1241 /* interpolate R, G, B, A */
1242 for (c
= 0; c
< 4; c
++) {
1243 rgba
[c
][j
] = lerp_3d(xw
[j
], yw
[j
], zw
[j
],
1254 mip_filter_linear(struct tgsi_sampler
*tgsi_sampler
,
1255 const float s
[QUAD_SIZE
],
1256 const float t
[QUAD_SIZE
],
1257 const float p
[QUAD_SIZE
],
1259 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1261 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1262 const struct pipe_texture
*texture
= samp
->texture
;
1266 lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1267 level0
= (int)lambda
;
1271 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1273 else if (level0
>= texture
->last_level
) {
1274 samp
->level
= texture
->last_level
;
1275 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1278 float levelBlend
= lambda
- level0
;
1283 samp
->level
= level0
;
1284 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba0
);
1286 samp
->level
= level0
+1;
1287 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba1
);
1289 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1290 for (c
= 0; c
< 4; c
++) {
1291 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1299 mip_filter_nearest(struct tgsi_sampler
*tgsi_sampler
,
1300 const float s
[QUAD_SIZE
],
1301 const float t
[QUAD_SIZE
],
1302 const float p
[QUAD_SIZE
],
1304 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1306 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1307 const struct pipe_texture
*texture
= samp
->texture
;
1310 lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1314 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1317 samp
->level
= (int)(lambda
+ 0.5) ;
1318 samp
->level
= MIN2(samp
->level
, (int)texture
->last_level
);
1319 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1323 printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1324 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1325 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1326 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1327 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1333 mip_filter_none(struct tgsi_sampler
*tgsi_sampler
,
1334 const float s
[QUAD_SIZE
],
1335 const float t
[QUAD_SIZE
],
1336 const float p
[QUAD_SIZE
],
1338 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1340 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1341 float lambda
= samp
->compute_lambda(samp
, s
, t
, p
, lodbias
);
1344 samp
->mag_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1347 samp
->min_img_filter( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1354 * Specialized version of mip_filter_linear with hard-wired calls to
1355 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1358 mip_filter_linear_2d_linear_repeat_POT(
1359 struct tgsi_sampler
*tgsi_sampler
,
1360 const float s
[QUAD_SIZE
],
1361 const float t
[QUAD_SIZE
],
1362 const float p
[QUAD_SIZE
],
1364 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1366 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1367 const struct pipe_texture
*texture
= samp
->texture
;
1371 lambda
= compute_lambda_2d(samp
, s
, t
, p
, lodbias
);
1372 level0
= (int)lambda
;
1374 /* Catches both negative and large values of level0:
1376 if ((unsigned)level0
>= texture
->last_level
) {
1380 samp
->level
= texture
->last_level
;
1382 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba
);
1385 float levelBlend
= lambda
- level0
;
1390 samp
->level
= level0
;
1391 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba0
);
1393 samp
->level
= level0
+1;
1394 img_filter_2d_linear_repeat_POT( tgsi_sampler
, s
, t
, p
, 0, rgba1
);
1396 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1397 for (c
= 0; c
< 4; c
++) {
1398 rgba
[c
][j
] = lerp(levelBlend
, rgba0
[c
][j
], rgba1
[c
][j
]);
1407 * Do shadow/depth comparisons.
1410 sample_compare(struct tgsi_sampler
*tgsi_sampler
,
1411 const float s
[QUAD_SIZE
],
1412 const float t
[QUAD_SIZE
],
1413 const float p
[QUAD_SIZE
],
1415 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1417 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1418 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1419 int j
, k0
, k1
, k2
, k3
;
1422 samp
->mip_filter( tgsi_sampler
, s
, t
, p
, lodbias
, rgba
);
1425 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
1426 * When we sampled the depth texture, the depth value was put into all
1427 * RGBA channels. We look at the red channel here.
1430 /* compare four texcoords vs. four texture samples */
1431 switch (sampler
->compare_func
) {
1432 case PIPE_FUNC_LESS
:
1433 k0
= p
[0] < rgba
[0][0];
1434 k1
= p
[1] < rgba
[0][1];
1435 k2
= p
[2] < rgba
[0][2];
1436 k3
= p
[3] < rgba
[0][3];
1438 case PIPE_FUNC_LEQUAL
:
1439 k0
= p
[0] <= rgba
[0][0];
1440 k1
= p
[1] <= rgba
[0][1];
1441 k2
= p
[2] <= rgba
[0][2];
1442 k3
= p
[3] <= rgba
[0][3];
1444 case PIPE_FUNC_GREATER
:
1445 k0
= p
[0] > rgba
[0][0];
1446 k1
= p
[1] > rgba
[0][1];
1447 k2
= p
[2] > rgba
[0][2];
1448 k3
= p
[3] > rgba
[0][3];
1450 case PIPE_FUNC_GEQUAL
:
1451 k0
= p
[0] >= rgba
[0][0];
1452 k1
= p
[1] >= rgba
[0][1];
1453 k2
= p
[2] >= rgba
[0][2];
1454 k3
= p
[3] >= rgba
[0][3];
1456 case PIPE_FUNC_EQUAL
:
1457 k0
= p
[0] == rgba
[0][0];
1458 k1
= p
[1] == rgba
[0][1];
1459 k2
= p
[2] == rgba
[0][2];
1460 k3
= p
[3] == rgba
[0][3];
1462 case PIPE_FUNC_NOTEQUAL
:
1463 k0
= p
[0] != rgba
[0][0];
1464 k1
= p
[1] != rgba
[0][1];
1465 k2
= p
[2] != rgba
[0][2];
1466 k3
= p
[3] != rgba
[0][3];
1468 case PIPE_FUNC_ALWAYS
:
1469 k0
= k1
= k2
= k3
= 1;
1471 case PIPE_FUNC_NEVER
:
1472 k0
= k1
= k2
= k3
= 0;
1475 k0
= k1
= k2
= k3
= 0;
1480 /* convert four pass/fail values to an intensity in [0,1] */
1481 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
1483 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1484 for (j
= 0; j
< 4; j
++) {
1485 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
1492 * Compute which cube face is referenced by each texcoord and put that
1493 * info into the sampler faces[] array. Then sample the cube faces
1496 sample_cube(struct tgsi_sampler
*tgsi_sampler
,
1497 const float s
[QUAD_SIZE
],
1498 const float t
[QUAD_SIZE
],
1499 const float p
[QUAD_SIZE
],
1501 float rgba
[NUM_CHANNELS
][QUAD_SIZE
])
1503 struct sp_sampler_varient
*samp
= sp_sampler_varient(tgsi_sampler
);
1505 float ssss
[4], tttt
[4];
1509 direction target sc tc ma
1510 ---------- ------------------------------- --- --- ---
1511 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
1512 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
1513 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
1514 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
1515 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
1516 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
1518 for (j
= 0; j
< QUAD_SIZE
; j
++) {
1522 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
1526 if (arx
>= ary
&& arx
>= arz
) {
1528 face
= PIPE_TEX_FACE_POS_X
;
1534 face
= PIPE_TEX_FACE_NEG_X
;
1540 else if (ary
>= arx
&& ary
>= arz
) {
1542 face
= PIPE_TEX_FACE_POS_Y
;
1548 face
= PIPE_TEX_FACE_NEG_Y
;
1556 face
= PIPE_TEX_FACE_POS_Z
;
1562 face
= PIPE_TEX_FACE_NEG_Z
;
1570 const float ima
= 1.0 / ma
;
1571 ssss
[j
] = ( sc
* ima
+ 1.0F
) * 0.5F
;
1572 tttt
[j
] = ( tc
* ima
+ 1.0F
) * 0.5F
;
1573 samp
->faces
[j
] = face
;
1577 /* In our little pipeline, the compare stage is next. If compare
1578 * is not active, this will point somewhere deeper into the
1579 * pipeline, eg. to mip_filter or even img_filter.
1581 samp
->compare(tgsi_sampler
, ssss
, tttt
, NULL
, lodbias
, rgba
);
1586 static wrap_nearest_func
1587 get_nearest_unorm_wrap(unsigned mode
)
1590 case PIPE_TEX_WRAP_CLAMP
:
1591 return wrap_nearest_unorm_clamp
;
1592 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1593 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1594 return wrap_nearest_unorm_clamp_to_border
;
1597 return wrap_nearest_unorm_clamp
;
1602 static wrap_nearest_func
1603 get_nearest_wrap(unsigned mode
)
1606 case PIPE_TEX_WRAP_REPEAT
:
1607 return wrap_nearest_repeat
;
1608 case PIPE_TEX_WRAP_CLAMP
:
1609 return wrap_nearest_clamp
;
1610 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1611 return wrap_nearest_clamp_to_edge
;
1612 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1613 return wrap_nearest_clamp_to_border
;
1614 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1615 return wrap_nearest_mirror_repeat
;
1616 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1617 return wrap_nearest_mirror_clamp
;
1618 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1619 return wrap_nearest_mirror_clamp_to_edge
;
1620 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1621 return wrap_nearest_mirror_clamp_to_border
;
1624 return wrap_nearest_repeat
;
1629 static wrap_linear_func
1630 get_linear_unorm_wrap(unsigned mode
)
1633 case PIPE_TEX_WRAP_CLAMP
:
1634 return wrap_linear_unorm_clamp
;
1635 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1636 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1637 return wrap_linear_unorm_clamp_to_border
;
1640 return wrap_linear_unorm_clamp
;
1645 static wrap_linear_func
1646 get_linear_wrap(unsigned mode
)
1649 case PIPE_TEX_WRAP_REPEAT
:
1650 return wrap_linear_repeat
;
1651 case PIPE_TEX_WRAP_CLAMP
:
1652 return wrap_linear_clamp
;
1653 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1654 return wrap_linear_clamp_to_edge
;
1655 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1656 return wrap_linear_clamp_to_border
;
1657 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1658 return wrap_linear_mirror_repeat
;
1659 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1660 return wrap_linear_mirror_clamp
;
1661 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1662 return wrap_linear_mirror_clamp_to_edge
;
1663 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1664 return wrap_linear_mirror_clamp_to_border
;
1667 return wrap_linear_repeat
;
1672 static compute_lambda_func
1673 get_lambda_func(const union sp_sampler_key key
)
1675 if (key
.bits
.processor
== TGSI_PROCESSOR_VERTEX
)
1676 return compute_lambda_vert
;
1678 switch (key
.bits
.target
) {
1679 case PIPE_TEXTURE_1D
:
1680 return compute_lambda_1d
;
1681 case PIPE_TEXTURE_2D
:
1682 case PIPE_TEXTURE_CUBE
:
1683 return compute_lambda_2d
;
1684 case PIPE_TEXTURE_3D
:
1685 return compute_lambda_3d
;
1688 return compute_lambda_1d
;
1694 get_img_filter(const union sp_sampler_key key
,
1696 const struct pipe_sampler_state
*sampler
)
1698 switch (key
.bits
.target
) {
1699 case PIPE_TEXTURE_1D
:
1700 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1701 return img_filter_1d_nearest
;
1703 return img_filter_1d_linear
;
1705 case PIPE_TEXTURE_2D
:
1706 /* Try for fast path:
1708 if (key
.bits
.is_pot
&&
1709 sampler
->wrap_s
== sampler
->wrap_t
&&
1710 sampler
->normalized_coords
)
1712 switch (sampler
->wrap_s
) {
1713 case PIPE_TEX_WRAP_REPEAT
:
1715 case PIPE_TEX_FILTER_NEAREST
:
1716 return img_filter_2d_nearest_repeat_POT
;
1717 case PIPE_TEX_FILTER_LINEAR
:
1718 return img_filter_2d_linear_repeat_POT
;
1723 case PIPE_TEX_WRAP_CLAMP
:
1725 case PIPE_TEX_FILTER_NEAREST
:
1726 return img_filter_2d_nearest_clamp_POT
;
1732 /* Otherwise use default versions:
1734 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1735 return img_filter_2d_nearest
;
1737 return img_filter_2d_linear
;
1739 case PIPE_TEXTURE_CUBE
:
1740 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1741 return img_filter_cube_nearest
;
1743 return img_filter_cube_linear
;
1745 case PIPE_TEXTURE_3D
:
1746 if (filter
== PIPE_TEX_FILTER_NEAREST
)
1747 return img_filter_3d_nearest
;
1749 return img_filter_3d_linear
;
1753 return img_filter_1d_nearest
;
1759 * Bind the given texture object and texture cache to the sampler varient.
1762 sp_sampler_varient_bind_texture( struct sp_sampler_varient
*samp
,
1763 struct softpipe_tex_tile_cache
*tex_cache
,
1764 const struct pipe_texture
*texture
)
1766 const struct pipe_sampler_state
*sampler
= samp
->sampler
;
1768 samp
->texture
= texture
;
1769 samp
->cache
= tex_cache
;
1770 samp
->xpot
= util_unsigned_logbase2( texture
->width
[0] );
1771 samp
->ypot
= util_unsigned_logbase2( texture
->height
[0] );
1772 samp
->level
= CLAMP((int) sampler
->min_lod
, 0, (int) texture
->last_level
);
1777 sp_sampler_varient_destroy( struct sp_sampler_varient
*samp
)
1784 * Create a sampler varient for a given set of non-orthogonal state.
1786 struct sp_sampler_varient
*
1787 sp_create_sampler_varient( const struct pipe_sampler_state
*sampler
,
1788 const union sp_sampler_key key
)
1790 struct sp_sampler_varient
*samp
= CALLOC_STRUCT(sp_sampler_varient
);
1794 samp
->sampler
= sampler
;
1797 /* Note that (for instance) linear_texcoord_s and
1798 * nearest_texcoord_s may be active at the same time, if the
1799 * sampler min_img_filter differs from its mag_img_filter.
1801 if (sampler
->normalized_coords
) {
1802 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
1803 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
1804 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
1806 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
1807 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
1808 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
1811 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
1812 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
1813 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
1815 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
1816 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
1817 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
1820 samp
->compute_lambda
= get_lambda_func( key
);
1822 samp
->min_img_filter
= get_img_filter(key
, sampler
->min_img_filter
, sampler
);
1823 samp
->mag_img_filter
= get_img_filter(key
, sampler
->mag_img_filter
, sampler
);
1825 switch (sampler
->min_mip_filter
) {
1826 case PIPE_TEX_MIPFILTER_NONE
:
1827 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
1828 samp
->mip_filter
= samp
->min_img_filter
;
1830 samp
->mip_filter
= mip_filter_none
;
1833 case PIPE_TEX_MIPFILTER_NEAREST
:
1834 samp
->mip_filter
= mip_filter_nearest
;
1837 case PIPE_TEX_MIPFILTER_LINEAR
:
1838 if (key
.bits
.is_pot
&&
1839 sampler
->min_img_filter
== sampler
->mag_img_filter
&&
1840 sampler
->normalized_coords
&&
1841 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
1842 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
1843 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
)
1845 samp
->mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
1849 samp
->mip_filter
= mip_filter_linear
;
1854 if (sampler
->compare_mode
!= FALSE
) {
1855 samp
->compare
= sample_compare
;
1858 /* Skip compare operation by promoting the mip_filter function
1861 samp
->compare
= samp
->mip_filter
;
1864 if (key
.bits
.target
== PIPE_TEXTURE_CUBE
) {
1865 samp
->base
.get_samples
= sample_cube
;
1873 /* Skip cube face determination by promoting the compare
1876 samp
->base
.get_samples
= samp
->compare
;