1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008-2010 VMware, Inc. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "util/u_math.h"
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_inlines.h"
44 #include "sp_quad.h" /* only for #define QUAD_* tokens */
45 #include "sp_tex_sample.h"
46 #include "sp_texture.h"
47 #include "sp_tex_tile_cache.h"
50 /** Set to one to help debug texture sampling */
55 * Return fractional part of 'f'. Used for computing interpolation weights.
56 * Need to be careful with negative values.
57 * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
58 * of improperly weighted linear-filtered textures.
59 * The tests/texwrap.c demo is a good test.
70 * Linear interpolation macro
73 lerp(float a
, float v0
, float v1
)
75 return v0
+ a
* (v1
- v0
);
80 * Do 2D/bilinear interpolation of float values.
81 * v00, v10, v01 and v11 are typically four texture samples in a square/box.
82 * a and b are the horizontal and vertical interpolants.
83 * It's important that this function is inlined when compiled with
84 * optimization! If we find that's not true on some systems, convert
88 lerp_2d(float a
, float b
,
89 float v00
, float v10
, float v01
, float v11
)
91 const float temp0
= lerp(a
, v00
, v10
);
92 const float temp1
= lerp(a
, v01
, v11
);
93 return lerp(b
, temp0
, temp1
);
98 * As above, but 3D interpolation of 8 values.
101 lerp_3d(float a
, float b
, float c
,
102 float v000
, float v100
, float v010
, float v110
,
103 float v001
, float v101
, float v011
, float v111
)
105 const float temp0
= lerp_2d(a
, b
, v000
, v100
, v010
, v110
);
106 const float temp1
= lerp_2d(a
, b
, v001
, v101
, v011
, v111
);
107 return lerp(c
, temp0
, temp1
);
113 * Compute coord % size for repeat wrap modes.
114 * Note that if coord is negative, coord % size doesn't give the right
115 * value. To avoid that problem we add a large multiple of the size
116 * (rather than using a conditional).
119 repeat(int coord
, unsigned size
)
121 return (coord
+ size
* 1024) % size
;
126 * Apply texture coord wrapping mode and return integer texture indexes
127 * for a vector of four texcoords (S or T or P).
128 * \param wrapMode PIPE_TEX_WRAP_x
129 * \param s the incoming texcoords
130 * \param size the texture image size
131 * \param icoord returns the integer texcoords
134 wrap_nearest_repeat(float s
, unsigned size
, int *icoord
)
136 /* s limited to [0,1) */
137 /* i limited to [0,size-1] */
138 int i
= util_ifloor(s
* size
);
139 *icoord
= repeat(i
, size
);
144 wrap_nearest_clamp(float s
, unsigned size
, int *icoord
)
146 /* s limited to [0,1] */
147 /* i limited to [0,size-1] */
153 *icoord
= util_ifloor(s
* size
);
158 wrap_nearest_clamp_to_edge(float s
, unsigned size
, int *icoord
)
160 /* s limited to [min,max] */
161 /* i limited to [0, size-1] */
162 const float min
= 1.0F
/ (2.0F
* size
);
163 const float max
= 1.0F
- min
;
169 *icoord
= util_ifloor(s
* size
);
174 wrap_nearest_clamp_to_border(float s
, unsigned size
, int *icoord
)
176 /* s limited to [min,max] */
177 /* i limited to [-1, size] */
178 const float min
= -1.0F
/ (2.0F
* size
);
179 const float max
= 1.0F
- min
;
185 *icoord
= util_ifloor(s
* size
);
190 wrap_nearest_mirror_repeat(float s
, unsigned size
, int *icoord
)
192 const float min
= 1.0F
/ (2.0F
* size
);
193 const float max
= 1.0F
- min
;
194 const int flr
= util_ifloor(s
);
203 *icoord
= util_ifloor(u
* size
);
208 wrap_nearest_mirror_clamp(float s
, unsigned size
, int *icoord
)
210 /* s limited to [0,1] */
211 /* i limited to [0,size-1] */
212 const float u
= fabsf(s
);
218 *icoord
= util_ifloor(u
* size
);
223 wrap_nearest_mirror_clamp_to_edge(float s
, unsigned size
, int *icoord
)
225 /* s limited to [min,max] */
226 /* i limited to [0, size-1] */
227 const float min
= 1.0F
/ (2.0F
* size
);
228 const float max
= 1.0F
- min
;
229 const float u
= fabsf(s
);
235 *icoord
= util_ifloor(u
* size
);
240 wrap_nearest_mirror_clamp_to_border(float s
, unsigned size
, int *icoord
)
242 /* s limited to [min,max] */
243 /* i limited to [0, size-1] */
244 const float min
= -1.0F
/ (2.0F
* size
);
245 const float max
= 1.0F
- min
;
246 const float u
= fabsf(s
);
252 *icoord
= util_ifloor(u
* size
);
257 * Used to compute texel locations for linear sampling
258 * \param wrapMode PIPE_TEX_WRAP_x
259 * \param s the texcoord
260 * \param size the texture image size
261 * \param icoord0 returns first texture index
262 * \param icoord1 returns second texture index (usually icoord0 + 1)
263 * \param w returns blend factor/weight between texture indices
264 * \param icoord returns the computed integer texture coord
267 wrap_linear_repeat(float s
, unsigned size
,
268 int *icoord0
, int *icoord1
, float *w
)
270 float u
= s
* size
- 0.5F
;
271 *icoord0
= repeat(util_ifloor(u
), size
);
272 *icoord1
= repeat(*icoord0
+ 1, size
);
278 wrap_linear_clamp(float s
, unsigned size
,
279 int *icoord0
, int *icoord1
, float *w
)
281 float u
= CLAMP(s
, 0.0F
, 1.0F
);
283 *icoord0
= util_ifloor(u
);
284 *icoord1
= *icoord0
+ 1;
290 wrap_linear_clamp_to_edge(float s
, unsigned size
,
291 int *icoord0
, int *icoord1
, float *w
)
293 float u
= CLAMP(s
, 0.0F
, 1.0F
);
295 *icoord0
= util_ifloor(u
);
296 *icoord1
= *icoord0
+ 1;
299 if (*icoord1
>= (int) size
)
306 wrap_linear_clamp_to_border(float s
, unsigned size
,
307 int *icoord0
, int *icoord1
, float *w
)
309 const float min
= -1.0F
/ (2.0F
* size
);
310 const float max
= 1.0F
- min
;
311 float u
= CLAMP(s
, min
, max
);
313 *icoord0
= util_ifloor(u
);
314 *icoord1
= *icoord0
+ 1;
320 wrap_linear_mirror_repeat(float s
, unsigned size
,
321 int *icoord0
, int *icoord1
, float *w
)
323 const int flr
= util_ifloor(s
);
328 *icoord0
= util_ifloor(u
);
329 *icoord1
= *icoord0
+ 1;
332 if (*icoord1
>= (int) size
)
339 wrap_linear_mirror_clamp(float s
, unsigned size
,
340 int *icoord0
, int *icoord1
, float *w
)
348 *icoord0
= util_ifloor(u
);
349 *icoord1
= *icoord0
+ 1;
355 wrap_linear_mirror_clamp_to_edge(float s
, unsigned size
,
356 int *icoord0
, int *icoord1
, float *w
)
364 *icoord0
= util_ifloor(u
);
365 *icoord1
= *icoord0
+ 1;
368 if (*icoord1
>= (int) size
)
375 wrap_linear_mirror_clamp_to_border(float s
, unsigned size
,
376 int *icoord0
, int *icoord1
, float *w
)
378 const float min
= -1.0F
/ (2.0F
* size
);
379 const float max
= 1.0F
- min
;
388 *icoord0
= util_ifloor(u
);
389 *icoord1
= *icoord0
+ 1;
395 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
398 wrap_nearest_unorm_clamp(float s
, unsigned size
, int *icoord
)
400 int i
= util_ifloor(s
);
401 *icoord
= CLAMP(i
, 0, (int) size
-1);
406 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
409 wrap_nearest_unorm_clamp_to_border(float s
, unsigned size
, int *icoord
)
411 *icoord
= util_ifloor( CLAMP(s
, -0.5F
, (float) size
+ 0.5F
) );
416 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
419 wrap_nearest_unorm_clamp_to_edge(float s
, unsigned size
, int *icoord
)
421 *icoord
= util_ifloor( CLAMP(s
, 0.5F
, (float) size
- 0.5F
) );
426 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
429 wrap_linear_unorm_clamp(float s
, unsigned size
,
430 int *icoord0
, int *icoord1
, float *w
)
432 /* Not exactly what the spec says, but it matches NVIDIA output */
433 float u
= CLAMP(s
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
434 *icoord0
= util_ifloor(u
);
435 *icoord1
= *icoord0
+ 1;
441 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
444 wrap_linear_unorm_clamp_to_border(float s
, unsigned size
,
445 int *icoord0
, int *icoord1
, float *w
)
447 float u
= CLAMP(s
, -0.5F
, (float) size
+ 0.5F
);
449 *icoord0
= util_ifloor(u
);
450 *icoord1
= *icoord0
+ 1;
451 if (*icoord1
> (int) size
- 1)
458 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
461 wrap_linear_unorm_clamp_to_edge(float s
, unsigned size
,
462 int *icoord0
, int *icoord1
, float *w
)
464 float u
= CLAMP(s
, +0.5F
, (float) size
- 0.5F
);
466 *icoord0
= util_ifloor(u
);
467 *icoord1
= *icoord0
+ 1;
468 if (*icoord1
> (int) size
- 1)
475 * Do coordinate to array index conversion. For array textures.
478 wrap_array_layer(float coord
, unsigned size
, int *layer
)
480 int c
= util_ifloor(coord
+ 0.5F
);
481 *layer
= CLAMP(c
, 0, (int) size
- 1);
486 * Examine the quad's texture coordinates to compute the partial
487 * derivatives w.r.t X and Y, then compute lambda (level of detail).
490 compute_lambda_1d(const struct sp_sampler_view
*sview
,
491 const float s
[TGSI_QUAD_SIZE
],
492 const float t
[TGSI_QUAD_SIZE
],
493 const float p
[TGSI_QUAD_SIZE
])
495 const struct pipe_resource
*texture
= sview
->base
.texture
;
496 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
497 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
498 float rho
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
500 return util_fast_log2(rho
);
505 compute_lambda_2d(const struct sp_sampler_view
*sview
,
506 const float s
[TGSI_QUAD_SIZE
],
507 const float t
[TGSI_QUAD_SIZE
],
508 const float p
[TGSI_QUAD_SIZE
])
510 const struct pipe_resource
*texture
= sview
->base
.texture
;
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 dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
514 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
515 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
516 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
517 float rho
= MAX2(maxx
, maxy
);
519 return util_fast_log2(rho
);
524 compute_lambda_3d(const struct sp_sampler_view
*sview
,
525 const float s
[TGSI_QUAD_SIZE
],
526 const float t
[TGSI_QUAD_SIZE
],
527 const float p
[TGSI_QUAD_SIZE
])
529 const struct pipe_resource
*texture
= sview
->base
.texture
;
530 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
531 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
532 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
533 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
534 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
535 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
536 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
537 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
538 float maxz
= MAX2(dpdx
, dpdy
) * u_minify(texture
->depth0
, sview
->base
.u
.tex
.first_level
);
541 rho
= MAX2(maxx
, maxy
);
542 rho
= MAX2(rho
, maxz
);
544 return util_fast_log2(rho
);
549 * Compute lambda for a vertex texture sampler.
550 * Since there aren't derivatives to use, just return 0.
553 compute_lambda_vert(const struct sp_sampler_view
*sview
,
554 const float s
[TGSI_QUAD_SIZE
],
555 const float t
[TGSI_QUAD_SIZE
],
556 const float p
[TGSI_QUAD_SIZE
])
564 * Get a texel from a texture, using the texture tile cache.
566 * \param addr the template tex address containing cube, z, face info.
567 * \param x the x coord of texel within 2D image
568 * \param y the y coord of texel within 2D image
569 * \param rgba the quad to put the texel/color into
571 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
572 * sp_get_cached_tile_tex() function.
578 static INLINE
const float *
579 get_texel_2d_no_border(const struct sp_sampler_view
*sp_sview
,
580 union tex_tile_address addr
, int x
, int y
)
582 const struct softpipe_tex_cached_tile
*tile
;
583 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
584 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
588 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
590 return &tile
->data
.color
[y
][x
][0];
594 static INLINE
const float *
595 get_texel_2d(const struct sp_sampler_view
*sp_sview
,
596 const struct sp_sampler
*sp_samp
,
597 union tex_tile_address addr
, int x
, int y
)
599 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
600 unsigned level
= addr
.bits
.level
;
602 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
603 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
604 return sp_samp
->base
.border_color
.f
;
607 return get_texel_2d_no_border( sp_sview
, addr
, x
, y
);
612 * seamless cubemap neighbour array.
613 * this array is used to find the adjacent face in each of 4 directions,
614 * left, right, up, down. (or -x, +x, -y, +y).
616 static const unsigned face_array
[PIPE_TEX_FACE_MAX
][4] = {
617 /* pos X first then neg X is Z different, Y the same */
618 /* PIPE_TEX_FACE_POS_X,*/
619 { PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
,
620 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
621 /* PIPE_TEX_FACE_NEG_X */
622 { PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
,
623 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
625 /* pos Y first then neg Y is X different, X the same */
626 /* PIPE_TEX_FACE_POS_Y */
627 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
628 PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
},
630 /* PIPE_TEX_FACE_NEG_Y */
631 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
632 PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
},
634 /* pos Z first then neg Y is X different, X the same */
635 /* PIPE_TEX_FACE_POS_Z */
636 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
637 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
},
639 /* PIPE_TEX_FACE_NEG_Z */
640 { PIPE_TEX_FACE_POS_X
, PIPE_TEX_FACE_NEG_X
,
641 PIPE_TEX_FACE_NEG_Y
, PIPE_TEX_FACE_POS_Y
}
644 static INLINE
unsigned
645 get_next_face(unsigned face
, int x
, int y
)
649 if (x
== 0 && y
== 0)
660 return face_array
[face
][idx
];
663 static INLINE
const float *
664 get_texel_cube_seamless(const struct sp_sampler_view
*sp_sview
,
665 union tex_tile_address addr
, int x
, int y
,
668 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
669 unsigned level
= addr
.bits
.level
;
670 unsigned face
= addr
.bits
.face
;
675 max_x
= (int) u_minify(texture
->width0
, level
);
676 max_y
= (int) u_minify(texture
->height0
, level
);
680 /* the corner case */
681 if ((x
< 0 || x
>= max_x
) &&
682 (y
< 0 || y
>= max_y
)) {
683 const float *c1
, *c2
, *c3
;
684 int fx
= x
< 0 ? 0 : max_x
- 1;
685 int fy
= y
< 0 ? 0 : max_y
- 1;
686 c1
= get_texel_2d_no_border( sp_sview
, addr
, fx
, fy
);
687 addr
.bits
.face
= get_next_face(face
, (x
< 0) ? -1 : 1, 0);
688 c2
= get_texel_2d_no_border( sp_sview
, addr
, (x
< 0) ? max_x
- 1 : 0, fy
);
689 addr
.bits
.face
= get_next_face(face
, 0, (y
< 0) ? -1 : 1);
690 c3
= get_texel_2d_no_border( sp_sview
, addr
, fx
, (y
< 0) ? max_y
- 1 : 0);
691 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
692 corner
[c
] = CLAMP((c1
[c
] + c2
[c
] + c3
[c
]), 0.0F
, 1.0F
) / 3;
696 /* change the face */
699 face
= get_next_face(face
, -1, 0);
700 } else if (x
>= max_x
) {
702 face
= get_next_face(face
, 1, 0);
705 face
= get_next_face(face
, 0, -1);
706 } else if (y
>= max_y
) {
708 face
= get_next_face(face
, 0, 1);
711 addr
.bits
.face
= face
;
712 return get_texel_2d_no_border( sp_sview
, addr
, new_x
, new_y
);
715 /* Gather a quad of adjacent texels within a tile:
718 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_view
*sp_sview
,
719 union tex_tile_address addr
,
720 unsigned x
, unsigned y
,
723 const struct softpipe_tex_cached_tile
*tile
;
725 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
726 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
730 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
732 out
[0] = &tile
->data
.color
[y
][x
][0];
733 out
[1] = &tile
->data
.color
[y
][x
+1][0];
734 out
[2] = &tile
->data
.color
[y
+1][x
][0];
735 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
739 /* Gather a quad of potentially non-adjacent texels:
742 get_texel_quad_2d_no_border(const struct sp_sampler_view
*sp_sview
,
743 union tex_tile_address addr
,
748 out
[0] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y0
);
749 out
[1] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y0
);
750 out
[2] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y1
);
751 out
[3] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y1
);
754 /* Can involve a lot of unnecessary checks for border color:
757 get_texel_quad_2d(const struct sp_sampler_view
*sp_sview
,
758 const struct sp_sampler
*sp_samp
,
759 union tex_tile_address addr
,
764 out
[0] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y0
);
765 out
[1] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y0
);
766 out
[3] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y1
);
767 out
[2] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y1
);
774 static INLINE
const float *
775 get_texel_3d_no_border(const struct sp_sampler_view
*sp_sview
,
776 union tex_tile_address addr
, int x
, int y
, int z
)
778 const struct softpipe_tex_cached_tile
*tile
;
780 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
781 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
786 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
788 return &tile
->data
.color
[y
][x
][0];
792 static INLINE
const float *
793 get_texel_3d(const struct sp_sampler_view
*sp_sview
,
794 const struct sp_sampler
*sp_samp
,
795 union tex_tile_address addr
, int x
, int y
, int z
)
797 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
798 unsigned level
= addr
.bits
.level
;
800 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
801 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
802 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
803 return sp_samp
->base
.border_color
.f
;
806 return get_texel_3d_no_border( sp_sview
, addr
, x
, y
, z
);
811 /* Get texel pointer for 1D array texture */
812 static INLINE
const float *
813 get_texel_1d_array(const struct sp_sampler_view
*sp_sview
,
814 const struct sp_sampler
*sp_samp
,
815 union tex_tile_address addr
, int x
, int y
)
817 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
818 unsigned level
= addr
.bits
.level
;
820 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
821 return sp_samp
->base
.border_color
.f
;
824 return get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
829 /* Get texel pointer for 2D array texture */
830 static INLINE
const float *
831 get_texel_2d_array(const struct sp_sampler_view
*sp_sview
,
832 const struct sp_sampler
*sp_samp
,
833 union tex_tile_address addr
, int x
, int y
, int layer
)
835 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
836 unsigned level
= addr
.bits
.level
;
838 assert(layer
< (int) texture
->array_size
);
841 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
842 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
843 return sp_samp
->base
.border_color
.f
;
846 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
851 /* Get texel pointer for cube array texture */
852 static INLINE
const float *
853 get_texel_cube_array(const struct sp_sampler_view
*sp_sview
,
854 const struct sp_sampler
*sp_samp
,
855 union tex_tile_address addr
, int x
, int y
, int layer
)
857 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
858 unsigned level
= addr
.bits
.level
;
860 assert(layer
< (int) texture
->array_size
);
863 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
864 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
865 return sp_samp
->base
.border_color
.f
;
868 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
872 * Given the logbase2 of a mipmap's base level size and a mipmap level,
873 * return the size (in texels) of that mipmap level.
874 * For example, if level[0].width = 256 then base_pot will be 8.
875 * If level = 2, then we'll return 64 (the width at level=2).
876 * Return 1 if level > base_pot.
878 static INLINE
unsigned
879 pot_level_size(unsigned base_pot
, unsigned level
)
881 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
886 print_sample(const char *function
, const float *rgba
)
888 debug_printf("%s %g %g %g %g\n",
890 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
895 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
897 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
899 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
900 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
901 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
902 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
906 /* Some image-filter fastpaths:
909 img_filter_2d_linear_repeat_POT(struct sp_sampler_view
*sp_sview
,
910 struct sp_sampler
*sp_samp
,
918 unsigned xpot
= pot_level_size(sp_sview
->xpot
, level
);
919 unsigned ypot
= pot_level_size(sp_sview
->ypot
, level
);
920 int xmax
= (xpot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */
921 int ymax
= (ypot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */
922 union tex_tile_address addr
;
925 float u
= s
* xpot
- 0.5F
;
926 float v
= t
* ypot
- 0.5F
;
928 int uflr
= util_ifloor(u
);
929 int vflr
= util_ifloor(v
);
931 float xw
= u
- (float)uflr
;
932 float yw
= v
- (float)vflr
;
934 int x0
= uflr
& (xpot
- 1);
935 int y0
= vflr
& (ypot
- 1);
940 addr
.bits
.level
= level
;
942 /* Can we fetch all four at once:
944 if (x0
< xmax
&& y0
< ymax
) {
945 get_texel_quad_2d_no_border_single_tile(sp_sview
, addr
, x0
, y0
, tx
);
948 unsigned x1
= (x0
+ 1) & (xpot
- 1);
949 unsigned y1
= (y0
+ 1) & (ypot
- 1);
950 get_texel_quad_2d_no_border(sp_sview
, addr
, x0
, y0
, x1
, y1
, tx
);
953 /* interpolate R, G, B, A */
954 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++) {
955 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
961 print_sample(__FUNCTION__
, rgba
);
967 img_filter_2d_nearest_repeat_POT(struct sp_sampler_view
*sp_sview
,
968 struct sp_sampler
*sp_samp
,
974 float rgba
[TGSI_QUAD_SIZE
])
976 unsigned xpot
= pot_level_size(sp_sview
->xpot
, level
);
977 unsigned ypot
= pot_level_size(sp_sview
->ypot
, level
);
979 union tex_tile_address addr
;
985 int uflr
= util_ifloor(u
);
986 int vflr
= util_ifloor(v
);
988 int x0
= uflr
& (xpot
- 1);
989 int y0
= vflr
& (ypot
- 1);
992 addr
.bits
.level
= level
;
994 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
995 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
996 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
999 print_sample(__FUNCTION__
, rgba
);
1005 img_filter_2d_nearest_clamp_POT(struct sp_sampler_view
*sp_sview
,
1006 struct sp_sampler
*sp_samp
,
1012 float rgba
[TGSI_QUAD_SIZE
])
1014 unsigned xpot
= pot_level_size(sp_sview
->xpot
, level
);
1015 unsigned ypot
= pot_level_size(sp_sview
->ypot
, level
);
1016 union tex_tile_address addr
;
1026 addr
.bits
.level
= level
;
1028 x0
= util_ifloor(u
);
1031 else if (x0
> (int) xpot
- 1)
1034 y0
= util_ifloor(v
);
1037 else if (y0
> (int) ypot
- 1)
1040 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1041 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1042 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1045 print_sample(__FUNCTION__
, rgba
);
1051 img_filter_1d_nearest(struct sp_sampler_view
*sp_sview
,
1052 struct sp_sampler
*sp_samp
,
1058 float rgba
[TGSI_QUAD_SIZE
])
1060 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1063 union tex_tile_address addr
;
1067 width
= u_minify(texture
->width0
, level
);
1072 addr
.bits
.level
= level
;
1074 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1076 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, 0);
1077 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1078 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1081 print_sample(__FUNCTION__
, rgba
);
1087 img_filter_1d_array_nearest(struct sp_sampler_view
*sp_sview
,
1088 struct sp_sampler
*sp_samp
,
1096 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1099 union tex_tile_address addr
;
1103 width
= u_minify(texture
->width0
, level
);
1108 addr
.bits
.level
= level
;
1110 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1111 wrap_array_layer(t
, texture
->array_size
, &layer
);
1113 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
, layer
);
1114 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1115 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1118 print_sample(__FUNCTION__
, rgba
);
1124 img_filter_2d_nearest(struct sp_sampler_view
*sp_sview
,
1125 struct sp_sampler
*sp_samp
,
1133 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1136 union tex_tile_address addr
;
1140 width
= u_minify(texture
->width0
, level
);
1141 height
= u_minify(texture
->height0
, level
);
1147 addr
.bits
.level
= level
;
1149 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1150 sp_samp
->nearest_texcoord_t(t
, height
, &y
);
1152 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, y
);
1153 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1154 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1157 print_sample(__FUNCTION__
, rgba
);
1163 img_filter_2d_array_nearest(struct sp_sampler_view
*sp_sview
,
1164 struct sp_sampler
*sp_samp
,
1172 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1175 union tex_tile_address addr
;
1179 width
= u_minify(texture
->width0
, level
);
1180 height
= u_minify(texture
->height0
, level
);
1186 addr
.bits
.level
= level
;
1188 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1189 sp_samp
->nearest_texcoord_t(t
, height
, &y
);
1190 wrap_array_layer(p
, texture
->array_size
, &layer
);
1192 out
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x
, y
, layer
);
1193 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1194 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1197 print_sample(__FUNCTION__
, rgba
);
1202 static INLINE
union tex_tile_address
1203 face(union tex_tile_address addr
, unsigned face
)
1205 addr
.bits
.face
= face
;
1211 img_filter_cube_nearest(struct sp_sampler_view
*sp_sview
,
1212 struct sp_sampler
*sp_samp
,
1220 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1223 union tex_tile_address addr
;
1227 width
= u_minify(texture
->width0
, level
);
1228 height
= u_minify(texture
->height0
, level
);
1234 addr
.bits
.level
= level
;
1237 * If NEAREST filtering is done within a miplevel, always apply wrap
1238 * mode CLAMP_TO_EDGE.
1240 if (sp_samp
->base
.seamless_cube_map
) {
1241 wrap_nearest_clamp_to_edge(s
, width
, &x
);
1242 wrap_nearest_clamp_to_edge(t
, height
, &y
);
1244 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1245 sp_samp
->nearest_texcoord_t(t
, height
, &y
);
1248 out
= get_texel_2d(sp_sview
, sp_samp
, face(addr
, face_id
), x
, y
);
1249 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1250 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1253 print_sample(__FUNCTION__
, rgba
);
1258 img_filter_cube_array_nearest(struct sp_sampler_view
*sp_sview
,
1259 struct sp_sampler
*sp_samp
,
1267 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1270 union tex_tile_address addr
;
1274 width
= u_minify(texture
->width0
, level
);
1275 height
= u_minify(texture
->height0
, level
);
1281 addr
.bits
.level
= level
;
1283 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1284 sp_samp
->nearest_texcoord_t(t
, height
, &y
);
1285 wrap_array_layer(p
, texture
->array_size
, &layer
);
1287 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layer
* 6 + face_id
);
1288 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1289 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1292 print_sample(__FUNCTION__
, rgba
);
1297 img_filter_3d_nearest(struct sp_sampler_view
*sp_sview
,
1298 struct sp_sampler
*sp_samp
,
1306 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1307 int width
, height
, depth
;
1309 union tex_tile_address addr
;
1313 width
= u_minify(texture
->width0
, level
);
1314 height
= u_minify(texture
->height0
, level
);
1315 depth
= u_minify(texture
->depth0
, level
);
1321 sp_samp
->nearest_texcoord_s(s
, width
, &x
);
1322 sp_samp
->nearest_texcoord_t(t
, height
, &y
);
1323 sp_samp
->nearest_texcoord_p(p
, depth
, &z
);
1326 addr
.bits
.level
= level
;
1328 out
= get_texel_3d(sp_sview
, sp_samp
, addr
, x
, y
, z
);
1329 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1330 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1335 img_filter_1d_linear(struct sp_sampler_view
*sp_sview
,
1336 struct sp_sampler
*sp_samp
,
1344 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1347 float xw
; /* weights */
1348 union tex_tile_address addr
;
1349 const float *tx0
, *tx1
;
1352 width
= u_minify(texture
->width0
, level
);
1357 addr
.bits
.level
= level
;
1359 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1361 tx0
= get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, 0);
1362 tx1
= get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, 0);
1364 /* interpolate R, G, B, A */
1365 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1366 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1371 img_filter_1d_array_linear(struct sp_sampler_view
*sp_sview
,
1372 struct sp_sampler
*sp_samp
,
1380 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1383 float xw
; /* weights */
1384 union tex_tile_address addr
;
1385 const float *tx0
, *tx1
;
1388 width
= u_minify(texture
->width0
, level
);
1393 addr
.bits
.level
= level
;
1395 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1396 wrap_array_layer(t
, texture
->array_size
, &layer
);
1398 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
, layer
);
1399 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
, layer
);
1401 /* interpolate R, G, B, A */
1402 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1403 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1408 img_filter_2d_linear(struct sp_sampler_view
*sp_sview
,
1409 struct sp_sampler
*sp_samp
,
1417 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1420 float xw
, yw
; /* weights */
1421 union tex_tile_address addr
;
1422 const float *tx0
, *tx1
, *tx2
, *tx3
;
1425 width
= u_minify(texture
->width0
, level
);
1426 height
= u_minify(texture
->height0
, level
);
1432 addr
.bits
.level
= level
;
1434 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1435 sp_samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1437 tx0
= get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y0
);
1438 tx1
= get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y0
);
1439 tx2
= get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y1
);
1440 tx3
= get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y1
);
1442 /* interpolate R, G, B, A */
1443 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1444 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1451 img_filter_2d_array_linear(struct sp_sampler_view
*sp_sview
,
1452 struct sp_sampler
*sp_samp
,
1460 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1462 int x0
, y0
, x1
, y1
, layer
;
1463 float xw
, yw
; /* weights */
1464 union tex_tile_address addr
;
1465 const float *tx0
, *tx1
, *tx2
, *tx3
;
1468 width
= u_minify(texture
->width0
, level
);
1469 height
= u_minify(texture
->height0
, level
);
1475 addr
.bits
.level
= level
;
1477 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1478 sp_samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1479 wrap_array_layer(p
, texture
->array_size
, &layer
);
1481 tx0
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
);
1482 tx1
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
);
1483 tx2
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
);
1484 tx3
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
);
1486 /* interpolate R, G, B, A */
1487 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1488 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1495 img_filter_cube_linear(struct sp_sampler_view
*sp_sview
,
1496 struct sp_sampler
*sp_samp
,
1504 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1507 float xw
, yw
; /* weights */
1508 union tex_tile_address addr
, addrj
;
1509 const float *tx0
, *tx1
, *tx2
, *tx3
;
1510 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1511 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1514 width
= u_minify(texture
->width0
, level
);
1515 height
= u_minify(texture
->height0
, level
);
1521 addr
.bits
.level
= level
;
1524 * For seamless if LINEAR filtering is done within a miplevel,
1525 * always apply wrap mode CLAMP_TO_BORDER.
1527 if (sp_samp
->base
.seamless_cube_map
) {
1528 wrap_linear_clamp_to_border(s
, width
, &x0
, &x1
, &xw
);
1529 wrap_linear_clamp_to_border(t
, height
, &y0
, &y1
, &yw
);
1531 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1532 sp_samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1535 addrj
= face(addr
, face_id
);
1537 if (sp_samp
->base
.seamless_cube_map
) {
1538 tx0
= get_texel_cube_seamless(sp_sview
, addrj
, x0
, y0
, corner0
);
1539 tx1
= get_texel_cube_seamless(sp_sview
, addrj
, x1
, y0
, corner1
);
1540 tx2
= get_texel_cube_seamless(sp_sview
, addrj
, x0
, y1
, corner2
);
1541 tx3
= get_texel_cube_seamless(sp_sview
, addrj
, x1
, y1
, corner3
);
1543 tx0
= get_texel_2d(sp_sview
, sp_samp
, addrj
, x0
, y0
);
1544 tx1
= get_texel_2d(sp_sview
, sp_samp
, addrj
, x1
, y0
);
1545 tx2
= get_texel_2d(sp_sview
, sp_samp
, addrj
, x0
, y1
);
1546 tx3
= get_texel_2d(sp_sview
, sp_samp
, addrj
, x1
, y1
);
1548 /* interpolate R, G, B, A */
1549 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1550 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1557 img_filter_cube_array_linear(struct sp_sampler_view
*sp_sview
,
1558 struct sp_sampler
*sp_samp
,
1566 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1568 int x0
, y0
, x1
, y1
, layer
;
1569 float xw
, yw
; /* weights */
1570 union tex_tile_address addr
;
1571 const float *tx0
, *tx1
, *tx2
, *tx3
;
1574 width
= u_minify(texture
->width0
, level
);
1575 height
= u_minify(texture
->height0
, level
);
1581 addr
.bits
.level
= level
;
1583 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1584 sp_samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1585 wrap_array_layer(p
, texture
->array_size
, &layer
);
1587 tx0
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
* 6 + face_id
);
1588 tx1
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
* 6 + face_id
);
1589 tx2
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
* 6 + face_id
);
1590 tx3
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
* 6 + face_id
);
1592 /* interpolate R, G, B, A */
1593 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1594 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1600 img_filter_3d_linear(struct sp_sampler_view
*sp_sview
,
1601 struct sp_sampler
*sp_samp
,
1609 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1610 int width
, height
, depth
;
1611 int x0
, x1
, y0
, y1
, z0
, z1
;
1612 float xw
, yw
, zw
; /* interpolation weights */
1613 union tex_tile_address addr
;
1614 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1617 width
= u_minify(texture
->width0
, level
);
1618 height
= u_minify(texture
->height0
, level
);
1619 depth
= u_minify(texture
->depth0
, level
);
1622 addr
.bits
.level
= level
;
1628 sp_samp
->linear_texcoord_s(s
, width
, &x0
, &x1
, &xw
);
1629 sp_samp
->linear_texcoord_t(t
, height
, &y0
, &y1
, &yw
);
1630 sp_samp
->linear_texcoord_p(p
, depth
, &z0
, &z1
, &zw
);
1633 tx00
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z0
);
1634 tx01
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z0
);
1635 tx02
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z0
);
1636 tx03
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z0
);
1638 tx10
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z1
);
1639 tx11
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z1
);
1640 tx12
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z1
);
1641 tx13
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z1
);
1643 /* interpolate R, G, B, A */
1644 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1645 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1653 /* Calculate level of detail for every fragment,
1654 * with lambda already computed.
1655 * Note that lambda has already been biased by global LOD bias.
1656 * \param biased_lambda per-quad lambda.
1657 * \param lod_in per-fragment lod_bias or explicit_lod.
1658 * \param lod returns the per-fragment lod.
1661 compute_lod(const struct pipe_sampler_state
*sampler
,
1662 enum tgsi_sampler_control control
,
1663 const float biased_lambda
,
1664 const float lod_in
[TGSI_QUAD_SIZE
],
1665 float lod
[TGSI_QUAD_SIZE
])
1667 float min_lod
= sampler
->min_lod
;
1668 float max_lod
= sampler
->max_lod
;
1672 case tgsi_sampler_lod_none
:
1673 case tgsi_sampler_lod_zero
:
1675 case tgsi_sampler_derivs_explicit
:
1676 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(biased_lambda
, min_lod
, max_lod
);
1678 case tgsi_sampler_lod_bias
:
1679 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1680 lod
[i
] = biased_lambda
+ lod_in
[i
];
1681 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1684 case tgsi_sampler_lod_explicit
:
1685 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1686 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1691 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1696 /* Calculate level of detail for every fragment.
1697 * \param lod_in per-fragment lod_bias or explicit_lod.
1698 * \param lod results per-fragment lod.
1701 compute_lambda_lod(struct sp_sampler_view
*sp_sview
,
1702 struct sp_sampler
*sp_samp
,
1703 const float s
[TGSI_QUAD_SIZE
],
1704 const float t
[TGSI_QUAD_SIZE
],
1705 const float p
[TGSI_QUAD_SIZE
],
1706 const float lod_in
[TGSI_QUAD_SIZE
],
1707 enum tgsi_sampler_control control
,
1708 float lod
[TGSI_QUAD_SIZE
])
1710 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1711 float lod_bias
= sampler
->lod_bias
;
1712 float min_lod
= sampler
->min_lod
;
1713 float max_lod
= sampler
->max_lod
;
1718 case tgsi_sampler_lod_none
:
1720 case tgsi_sampler_derivs_explicit
:
1721 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1722 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(lambda
, min_lod
, max_lod
);
1724 case tgsi_sampler_lod_bias
:
1725 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1726 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1727 lod
[i
] = lambda
+ lod_in
[i
];
1728 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1731 case tgsi_sampler_lod_explicit
:
1732 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1733 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1736 case tgsi_sampler_lod_zero
:
1737 /* this is all static state in the sampler really need clamp here? */
1738 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(lod_bias
, min_lod
, max_lod
);
1742 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1748 mip_filter_linear(struct sp_sampler_view
*sp_sview
,
1749 struct sp_sampler
*sp_samp
,
1750 img_filter_func min_filter
,
1751 img_filter_func mag_filter
,
1752 const float s
[TGSI_QUAD_SIZE
],
1753 const float t
[TGSI_QUAD_SIZE
],
1754 const float p
[TGSI_QUAD_SIZE
],
1755 const float c0
[TGSI_QUAD_SIZE
],
1756 const float lod_in
[TGSI_QUAD_SIZE
],
1757 enum tgsi_sampler_control control
,
1758 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1760 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1762 float lod
[TGSI_QUAD_SIZE
];
1764 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, control
, lod
);
1766 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1767 int level0
= sp_sview
->base
.u
.tex
.first_level
+ (int)lod
[j
];
1770 mag_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1771 sp_sview
->base
.u
.tex
.first_level
,
1772 sp_sview
->faces
[j
], &rgba
[0][j
]);
1774 else if (level0
>= (int) texture
->last_level
)
1775 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], texture
->last_level
,
1776 sp_sview
->faces
[j
], &rgba
[0][j
]);
1779 float levelBlend
= frac(lod
[j
]);
1780 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1783 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], level0
,
1784 sp_sview
->faces
[j
], &rgbax
[0][0]);
1785 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], level0
+1,
1786 sp_sview
->faces
[j
], &rgbax
[0][1]);
1788 for (c
= 0; c
< 4; c
++) {
1789 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1795 print_sample_4(__FUNCTION__
, rgba
);
1801 * Compute nearest mipmap level from texcoords.
1802 * Then sample the texture level for four elements of a quad.
1803 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1806 mip_filter_nearest(struct sp_sampler_view
*sp_sview
,
1807 struct sp_sampler
*sp_samp
,
1808 img_filter_func min_filter
,
1809 img_filter_func mag_filter
,
1810 const float s
[TGSI_QUAD_SIZE
],
1811 const float t
[TGSI_QUAD_SIZE
],
1812 const float p
[TGSI_QUAD_SIZE
],
1813 const float c0
[TGSI_QUAD_SIZE
],
1814 const float lod_in
[TGSI_QUAD_SIZE
],
1815 enum tgsi_sampler_control control
,
1816 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1818 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1819 float lod
[TGSI_QUAD_SIZE
];
1822 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, control
, lod
);
1824 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1826 mag_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1827 sp_sview
->base
.u
.tex
.first_level
,
1828 sp_sview
->faces
[j
], &rgba
[0][j
]);
1830 int level
= sp_sview
->base
.u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
);
1831 level
= MIN2(level
, (int)texture
->last_level
);
1832 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1833 level
, sp_sview
->faces
[j
], &rgba
[0][j
]);
1838 print_sample_4(__FUNCTION__
, rgba
);
1844 mip_filter_none(struct sp_sampler_view
*sp_sview
,
1845 struct sp_sampler
*sp_samp
,
1846 img_filter_func min_filter
,
1847 img_filter_func mag_filter
,
1848 const float s
[TGSI_QUAD_SIZE
],
1849 const float t
[TGSI_QUAD_SIZE
],
1850 const float p
[TGSI_QUAD_SIZE
],
1851 const float c0
[TGSI_QUAD_SIZE
],
1852 const float lod_in
[TGSI_QUAD_SIZE
],
1853 enum tgsi_sampler_control control
,
1854 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1856 float lod
[TGSI_QUAD_SIZE
];
1859 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, control
, lod
);
1861 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1863 mag_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1864 sp_sview
->base
.u
.tex
.first_level
,
1865 sp_sview
->faces
[j
], &rgba
[0][j
]);
1868 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1869 sp_sview
->base
.u
.tex
.first_level
,
1870 sp_sview
->faces
[j
], &rgba
[0][j
]);
1877 mip_filter_none_no_filter_select(struct sp_sampler_view
*sp_sview
,
1878 struct sp_sampler
*sp_samp
,
1879 img_filter_func min_filter
,
1880 img_filter_func mag_filter
,
1881 const float s
[TGSI_QUAD_SIZE
],
1882 const float t
[TGSI_QUAD_SIZE
],
1883 const float p
[TGSI_QUAD_SIZE
],
1884 const float c0
[TGSI_QUAD_SIZE
],
1885 const float lod_in
[TGSI_QUAD_SIZE
],
1886 enum tgsi_sampler_control control
,
1887 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1891 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
1892 mag_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
1893 sp_sview
->base
.u
.tex
.first_level
,
1894 sp_sview
->faces
[j
], &rgba
[0][j
]);
1898 /* For anisotropic filtering */
1899 #define WEIGHT_LUT_SIZE 1024
1901 static float *weightLut
= NULL
;
1904 * Creates the look-up table used to speed-up EWA sampling
1907 create_filter_table(void)
1911 weightLut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
1913 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
1915 float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
1916 float weight
= (float) exp(-alpha
* r2
);
1917 weightLut
[i
] = weight
;
1924 * Elliptical weighted average (EWA) filter for producing high quality
1925 * anisotropic filtered results.
1926 * Based on the Higher Quality Elliptical Weighted Average Filter
1927 * published by Paul S. Heckbert in his Master's Thesis
1928 * "Fundamentals of Texture Mapping and Image Warping" (1989)
1931 img_filter_2d_ewa(struct sp_sampler_view
*sp_sview
,
1932 struct sp_sampler
*sp_samp
,
1933 img_filter_func min_filter
,
1934 img_filter_func mag_filter
,
1935 const float s
[TGSI_QUAD_SIZE
],
1936 const float t
[TGSI_QUAD_SIZE
],
1937 const float p
[TGSI_QUAD_SIZE
],
1939 const float dudx
, const float dvdx
,
1940 const float dudy
, const float dvdy
,
1941 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1943 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1945 // ??? Won't the image filters blow up if level is negative?
1946 unsigned level0
= level
> 0 ? level
: 0;
1947 float scaling
= 1.0f
/ (1 << level0
);
1948 int width
= u_minify(texture
->width0
, level0
);
1949 int height
= u_minify(texture
->height0
, level0
);
1951 float ux
= dudx
* scaling
;
1952 float vx
= dvdx
* scaling
;
1953 float uy
= dudy
* scaling
;
1954 float vy
= dvdy
* scaling
;
1956 /* compute ellipse coefficients to bound the region:
1957 * A*x*x + B*x*y + C*y*y = F.
1959 float A
= vx
*vx
+vy
*vy
+1;
1960 float B
= -2*(ux
*vx
+uy
*vy
);
1961 float C
= ux
*ux
+uy
*uy
+1;
1962 float F
= A
*C
-B
*B
/4.0f
;
1964 /* check if it is an ellipse */
1965 /* ASSERT(F > 0.0); */
1967 /* Compute the ellipse's (u,v) bounding box in texture space */
1968 float d
= -B
*B
+4.0f
*C
*A
;
1969 float box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
1970 float box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
1972 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1973 float s_buffer
[TGSI_QUAD_SIZE
];
1974 float t_buffer
[TGSI_QUAD_SIZE
];
1975 float weight_buffer
[TGSI_QUAD_SIZE
];
1976 unsigned buffer_next
;
1978 float den
; /* = 0.0F; */
1980 float U
; /* = u0 - tex_u; */
1983 /* Scale ellipse formula to directly index the Filter Lookup Table.
1984 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
1986 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
1990 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
1992 /* For each quad, the du and dx values are the same and so the ellipse is
1993 * also the same. Note that texel/image access can only be performed using
1994 * a quad, i.e. it is not possible to get the pixel value for a single
1995 * tex coord. In order to have a better performance, the access is buffered
1996 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
1997 * full, then the pixel values are read from the image.
2001 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2002 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
2003 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
2004 * value, q, is less than F, we're inside the ellipse
2006 float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
2007 float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
2009 int u0
= (int) floorf(tex_u
- box_u
);
2010 int u1
= (int) ceilf(tex_u
+ box_u
);
2011 int v0
= (int) floorf(tex_v
- box_v
);
2012 int v1
= (int) ceilf(tex_v
+ box_v
);
2014 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
2018 for (v
= v0
; v
<= v1
; ++v
) {
2019 float V
= v
- tex_v
;
2020 float dq
= A
* (2 * U
+ 1) + B
* V
;
2021 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
2024 for (u
= u0
; u
<= u1
; ++u
) {
2025 /* Note that the ellipse has been pre-scaled so F =
2026 * WEIGHT_LUT_SIZE - 1
2028 if (q
< WEIGHT_LUT_SIZE
) {
2029 /* as a LUT is used, q must never be negative;
2030 * should not happen, though
2032 const int qClamped
= q
>= 0.0F
? q
: 0;
2033 float weight
= weightLut
[qClamped
];
2035 weight_buffer
[buffer_next
] = weight
;
2036 s_buffer
[buffer_next
] = u
/ ((float) width
);
2037 t_buffer
[buffer_next
] = v
/ ((float) height
);
2040 if (buffer_next
== TGSI_QUAD_SIZE
) {
2041 /* 4 texel coords are in the buffer -> read it now */
2043 /* it is assumed that samp->min_img_filter is set to
2044 * img_filter_2d_nearest or one of the
2045 * accelerated img_filter_2d_nearest_XXX functions.
2047 for (jj
= 0; jj
< buffer_next
; jj
++) {
2048 min_filter(sp_sview
, sp_samp
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
],
2049 level
, sp_sview
->faces
[j
], &rgba_temp
[0][jj
]);
2050 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2051 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2052 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2053 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2066 /* if the tex coord buffer contains unread values, we will read
2069 if (buffer_next
> 0) {
2071 /* it is assumed that samp->min_img_filter is set to
2072 * img_filter_2d_nearest or one of the
2073 * accelerated img_filter_2d_nearest_XXX functions.
2075 for (jj
= 0; jj
< buffer_next
; jj
++) {
2076 min_filter(sp_sview
, sp_samp
, s_buffer
[jj
], t_buffer
[jj
], p
[jj
],
2077 level
, sp_sview
->faces
[j
], &rgba_temp
[0][jj
]);
2078 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2079 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2080 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2081 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2086 /* Reaching this place would mean that no pixels intersected
2087 * the ellipse. This should never happen because the filter
2088 * we use always intersects at least one pixel.
2095 /* not enough pixels in resampling, resort to direct interpolation */
2096 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], level
,
2097 sp_sview
->faces
[j
], &rgba_temp
[0][j
]);
2099 num
[0] = rgba_temp
[0][j
];
2100 num
[1] = rgba_temp
[1][j
];
2101 num
[2] = rgba_temp
[2][j
];
2102 num
[3] = rgba_temp
[3][j
];
2105 rgba
[0][j
] = num
[0] / den
;
2106 rgba
[1][j
] = num
[1] / den
;
2107 rgba
[2][j
] = num
[2] / den
;
2108 rgba
[3][j
] = num
[3] / den
;
2114 * Sample 2D texture using an anisotropic filter.
2117 mip_filter_linear_aniso(struct sp_sampler_view
*sp_sview
,
2118 struct sp_sampler
*sp_samp
,
2119 img_filter_func min_filter
,
2120 img_filter_func mag_filter
,
2121 const float s
[TGSI_QUAD_SIZE
],
2122 const float t
[TGSI_QUAD_SIZE
],
2123 const float p
[TGSI_QUAD_SIZE
],
2124 const float c0
[TGSI_QUAD_SIZE
],
2125 const float lod_in
[TGSI_QUAD_SIZE
],
2126 enum tgsi_sampler_control control
,
2127 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2129 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2132 float lod
[TGSI_QUAD_SIZE
];
2134 float s_to_u
= u_minify(texture
->width0
, sp_sview
->base
.u
.tex
.first_level
);
2135 float t_to_v
= u_minify(texture
->height0
, sp_sview
->base
.u
.tex
.first_level
);
2136 float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2137 float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2138 float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2139 float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2141 if (control
== tgsi_sampler_lod_bias
||
2142 control
== tgsi_sampler_lod_none
||
2144 control
== tgsi_sampler_derivs_explicit
) {
2145 /* note: instead of working with Px and Py, we will use the
2146 * squared length instead, to avoid sqrt.
2148 float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2149 float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2154 const float maxEccentricity
= sp_samp
->base
.max_anisotropy
* sp_samp
->base
.max_anisotropy
;
2165 /* if the eccentricity of the ellipse is too big, scale up the shorter
2166 * of the two vectors to limit the maximum amount of work per pixel
2169 if (e
> maxEccentricity
) {
2170 /* float s=e / maxEccentricity;
2174 Pmin2
= Pmax2
/ maxEccentricity
;
2177 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2178 * this since 0.5*log(x) = log(sqrt(x))
2180 lambda
= 0.5F
* util_fast_log2(Pmin2
) + sp_samp
->base
.lod_bias
;
2181 compute_lod(&sp_samp
->base
, control
, lambda
, lod_in
, lod
);
2184 assert(control
== tgsi_sampler_lod_explicit
||
2185 control
== tgsi_sampler_lod_zero
);
2186 compute_lod(&sp_samp
->base
, control
, sp_samp
->base
.lod_bias
, lod_in
, lod
);
2189 /* XXX: Take into account all lod values.
2192 level0
= sp_sview
->base
.u
.tex
.first_level
+ (int)lambda
;
2194 /* If the ellipse covers the whole image, we can
2195 * simply return the average of the whole image.
2197 if (level0
>= (int) texture
->last_level
) {
2199 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
2200 min_filter(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], texture
->last_level
,
2201 sp_sview
->faces
[j
], &rgba
[0][j
]);
2204 /* don't bother interpolating between multiple LODs; it doesn't
2205 * seem to be worth the extra running time.
2207 img_filter_2d_ewa(sp_sview
, sp_samp
, min_filter
, mag_filter
,
2209 dudx
, dvdx
, dudy
, dvdy
, rgba
);
2213 print_sample_4(__FUNCTION__
, rgba
);
2219 * Specialized version of mip_filter_linear with hard-wired calls to
2220 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2223 mip_filter_linear_2d_linear_repeat_POT(
2224 struct sp_sampler_view
*sp_sview
,
2225 struct sp_sampler
*sp_samp
,
2226 img_filter_func min_filter
,
2227 img_filter_func mag_filter
,
2228 const float s
[TGSI_QUAD_SIZE
],
2229 const float t
[TGSI_QUAD_SIZE
],
2230 const float p
[TGSI_QUAD_SIZE
],
2231 const float c0
[TGSI_QUAD_SIZE
],
2232 const float lod_in
[TGSI_QUAD_SIZE
],
2233 enum tgsi_sampler_control control
,
2234 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2236 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2238 float lod
[TGSI_QUAD_SIZE
];
2240 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, control
, lod
);
2242 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2243 int level0
= sp_sview
->base
.u
.tex
.first_level
+ (int)lod
[j
];
2245 /* Catches both negative and large values of level0:
2247 if ((unsigned)level0
>= texture
->last_level
) {
2249 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
2250 sp_sview
->base
.u
.tex
.first_level
,
2251 sp_sview
->faces
[j
], &rgba
[0][j
]);
2253 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
],
2254 sp_sview
->base
.texture
->last_level
,
2255 sp_sview
->faces
[j
], &rgba
[0][j
]);
2259 float levelBlend
= frac(lod
[j
]);
2260 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2263 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], level0
,
2264 sp_sview
->faces
[j
], &rgbax
[0][0]);
2265 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, s
[j
], t
[j
], p
[j
], level0
+1,
2266 sp_sview
->faces
[j
], &rgbax
[0][1]);
2268 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2269 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2274 print_sample_4(__FUNCTION__
, rgba
);
2280 * Do shadow/depth comparisons.
2283 sample_compare(struct sp_sampler_view
*sp_sview
,
2284 struct sp_sampler
*sp_samp
,
2285 const float s
[TGSI_QUAD_SIZE
],
2286 const float t
[TGSI_QUAD_SIZE
],
2287 const float p
[TGSI_QUAD_SIZE
],
2288 const float c0
[TGSI_QUAD_SIZE
],
2289 const float c1
[TGSI_QUAD_SIZE
],
2290 enum tgsi_sampler_control control
,
2291 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2293 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
2294 int j
, k0
, k1
, k2
, k3
;
2296 float pc0
, pc1
, pc2
, pc3
;
2297 const struct util_format_description
*format_desc
;
2301 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2302 * for 2D Array texture we need to use the 'c0' (aka Q).
2303 * When we sampled the depth texture, the depth value was put into all
2304 * RGBA channels. We look at the red channel here.
2307 if (sp_sview
->base
.texture
->target
== PIPE_TEXTURE_2D_ARRAY
||
2308 sp_sview
->base
.texture
->target
== PIPE_TEXTURE_CUBE
) {
2313 } else if (sp_sview
->base
.texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2325 format_desc
= util_format_description(sp_sview
->base
.format
);
2326 /* not entirely sure we couldn't end up with non-valid swizzle here */
2327 chan_type
= format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
2328 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
2329 UTIL_FORMAT_TYPE_FLOAT
;
2330 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
2332 * clamping is a result of conversion to texture format, hence
2333 * doesn't happen with floats. Technically also should do comparison
2334 * in texture format (quantization!).
2336 pc0
= CLAMP(pc0
, 0.0F
, 1.0F
);
2337 pc1
= CLAMP(pc1
, 0.0F
, 1.0F
);
2338 pc2
= CLAMP(pc2
, 0.0F
, 1.0F
);
2339 pc3
= CLAMP(pc3
, 0.0F
, 1.0F
);
2342 /* compare four texcoords vs. four texture samples */
2343 switch (sampler
->compare_func
) {
2344 case PIPE_FUNC_LESS
:
2345 k0
= pc0
< rgba
[0][0];
2346 k1
= pc1
< rgba
[0][1];
2347 k2
= pc2
< rgba
[0][2];
2348 k3
= pc3
< rgba
[0][3];
2350 case PIPE_FUNC_LEQUAL
:
2351 k0
= pc0
<= rgba
[0][0];
2352 k1
= pc1
<= rgba
[0][1];
2353 k2
= pc2
<= rgba
[0][2];
2354 k3
= pc3
<= rgba
[0][3];
2356 case PIPE_FUNC_GREATER
:
2357 k0
= pc0
> rgba
[0][0];
2358 k1
= pc1
> rgba
[0][1];
2359 k2
= pc2
> rgba
[0][2];
2360 k3
= pc3
> rgba
[0][3];
2362 case PIPE_FUNC_GEQUAL
:
2363 k0
= pc0
>= rgba
[0][0];
2364 k1
= pc1
>= rgba
[0][1];
2365 k2
= pc2
>= rgba
[0][2];
2366 k3
= pc3
>= rgba
[0][3];
2368 case PIPE_FUNC_EQUAL
:
2369 k0
= pc0
== rgba
[0][0];
2370 k1
= pc1
== rgba
[0][1];
2371 k2
= pc2
== rgba
[0][2];
2372 k3
= pc3
== rgba
[0][3];
2374 case PIPE_FUNC_NOTEQUAL
:
2375 k0
= pc0
!= rgba
[0][0];
2376 k1
= pc1
!= rgba
[0][1];
2377 k2
= pc2
!= rgba
[0][2];
2378 k3
= pc3
!= rgba
[0][3];
2380 case PIPE_FUNC_ALWAYS
:
2381 k0
= k1
= k2
= k3
= 1;
2383 case PIPE_FUNC_NEVER
:
2384 k0
= k1
= k2
= k3
= 0;
2387 k0
= k1
= k2
= k3
= 0;
2392 if (sampler
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
) {
2393 /* convert four pass/fail values to an intensity in [0,1] */
2395 * XXX this doesn't actually make much sense.
2396 * We just average the result of four _pixels_ and output the same
2397 * value for all of the four pixels of the quad.
2398 * This really needs to work on the _samples_ i.e. inside the img filter.
2400 val
= 0.25F
* (k0
+ k1
+ k2
+ k3
);
2402 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2403 for (j
= 0; j
< 4; j
++) {
2404 rgba
[0][j
] = rgba
[1][j
] = rgba
[2][j
] = val
;
2408 for (j
= 0; j
< 4; j
++) {
2419 do_swizzling(const struct pipe_sampler_view
*sview
,
2420 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2421 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2424 const unsigned swizzle_r
= sview
->swizzle_r
;
2425 const unsigned swizzle_g
= sview
->swizzle_g
;
2426 const unsigned swizzle_b
= sview
->swizzle_b
;
2427 const unsigned swizzle_a
= sview
->swizzle_a
;
2429 switch (swizzle_r
) {
2430 case PIPE_SWIZZLE_ZERO
:
2431 for (j
= 0; j
< 4; j
++)
2434 case PIPE_SWIZZLE_ONE
:
2435 for (j
= 0; j
< 4; j
++)
2439 assert(swizzle_r
< 4);
2440 for (j
= 0; j
< 4; j
++)
2441 out
[0][j
] = in
[swizzle_r
][j
];
2444 switch (swizzle_g
) {
2445 case PIPE_SWIZZLE_ZERO
:
2446 for (j
= 0; j
< 4; j
++)
2449 case PIPE_SWIZZLE_ONE
:
2450 for (j
= 0; j
< 4; j
++)
2454 assert(swizzle_g
< 4);
2455 for (j
= 0; j
< 4; j
++)
2456 out
[1][j
] = in
[swizzle_g
][j
];
2459 switch (swizzle_b
) {
2460 case PIPE_SWIZZLE_ZERO
:
2461 for (j
= 0; j
< 4; j
++)
2464 case PIPE_SWIZZLE_ONE
:
2465 for (j
= 0; j
< 4; j
++)
2469 assert(swizzle_b
< 4);
2470 for (j
= 0; j
< 4; j
++)
2471 out
[2][j
] = in
[swizzle_b
][j
];
2474 switch (swizzle_a
) {
2475 case PIPE_SWIZZLE_ZERO
:
2476 for (j
= 0; j
< 4; j
++)
2479 case PIPE_SWIZZLE_ONE
:
2480 for (j
= 0; j
< 4; j
++)
2484 assert(swizzle_a
< 4);
2485 for (j
= 0; j
< 4; j
++)
2486 out
[3][j
] = in
[swizzle_a
][j
];
2491 static wrap_nearest_func
2492 get_nearest_unorm_wrap(unsigned mode
)
2495 case PIPE_TEX_WRAP_CLAMP
:
2496 return wrap_nearest_unorm_clamp
;
2497 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2498 return wrap_nearest_unorm_clamp_to_edge
;
2499 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2500 return wrap_nearest_unorm_clamp_to_border
;
2503 return wrap_nearest_unorm_clamp
;
2508 static wrap_nearest_func
2509 get_nearest_wrap(unsigned mode
)
2512 case PIPE_TEX_WRAP_REPEAT
:
2513 return wrap_nearest_repeat
;
2514 case PIPE_TEX_WRAP_CLAMP
:
2515 return wrap_nearest_clamp
;
2516 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2517 return wrap_nearest_clamp_to_edge
;
2518 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2519 return wrap_nearest_clamp_to_border
;
2520 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2521 return wrap_nearest_mirror_repeat
;
2522 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2523 return wrap_nearest_mirror_clamp
;
2524 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2525 return wrap_nearest_mirror_clamp_to_edge
;
2526 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2527 return wrap_nearest_mirror_clamp_to_border
;
2530 return wrap_nearest_repeat
;
2535 static wrap_linear_func
2536 get_linear_unorm_wrap(unsigned mode
)
2539 case PIPE_TEX_WRAP_CLAMP
:
2540 return wrap_linear_unorm_clamp
;
2541 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2542 return wrap_linear_unorm_clamp_to_edge
;
2543 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2544 return wrap_linear_unorm_clamp_to_border
;
2547 return wrap_linear_unorm_clamp
;
2552 static wrap_linear_func
2553 get_linear_wrap(unsigned mode
)
2556 case PIPE_TEX_WRAP_REPEAT
:
2557 return wrap_linear_repeat
;
2558 case PIPE_TEX_WRAP_CLAMP
:
2559 return wrap_linear_clamp
;
2560 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2561 return wrap_linear_clamp_to_edge
;
2562 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2563 return wrap_linear_clamp_to_border
;
2564 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2565 return wrap_linear_mirror_repeat
;
2566 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2567 return wrap_linear_mirror_clamp
;
2568 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2569 return wrap_linear_mirror_clamp_to_edge
;
2570 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2571 return wrap_linear_mirror_clamp_to_border
;
2574 return wrap_linear_repeat
;
2580 * Is swizzling needed for the given state key?
2583 any_swizzle(const struct pipe_sampler_view
*view
)
2585 return (view
->swizzle_r
!= PIPE_SWIZZLE_RED
||
2586 view
->swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2587 view
->swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2588 view
->swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
2592 static img_filter_func
2593 get_img_filter(const struct sp_sampler_view
*sp_sview
,
2594 const struct pipe_sampler_state
*sampler
,
2597 switch (sp_sview
->base
.texture
->target
) {
2599 case PIPE_TEXTURE_1D
:
2600 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2601 return img_filter_1d_nearest
;
2603 return img_filter_1d_linear
;
2605 case PIPE_TEXTURE_1D_ARRAY
:
2606 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2607 return img_filter_1d_array_nearest
;
2609 return img_filter_1d_array_linear
;
2611 case PIPE_TEXTURE_2D
:
2612 case PIPE_TEXTURE_RECT
:
2613 /* Try for fast path:
2615 if (sp_sview
->pot2d
&&
2616 sampler
->wrap_s
== sampler
->wrap_t
&&
2617 sampler
->normalized_coords
)
2619 switch (sampler
->wrap_s
) {
2620 case PIPE_TEX_WRAP_REPEAT
:
2622 case PIPE_TEX_FILTER_NEAREST
:
2623 return img_filter_2d_nearest_repeat_POT
;
2624 case PIPE_TEX_FILTER_LINEAR
:
2625 return img_filter_2d_linear_repeat_POT
;
2630 case PIPE_TEX_WRAP_CLAMP
:
2632 case PIPE_TEX_FILTER_NEAREST
:
2633 return img_filter_2d_nearest_clamp_POT
;
2639 /* Otherwise use default versions:
2641 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2642 return img_filter_2d_nearest
;
2644 return img_filter_2d_linear
;
2646 case PIPE_TEXTURE_2D_ARRAY
:
2647 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2648 return img_filter_2d_array_nearest
;
2650 return img_filter_2d_array_linear
;
2652 case PIPE_TEXTURE_CUBE
:
2653 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2654 return img_filter_cube_nearest
;
2656 return img_filter_cube_linear
;
2658 case PIPE_TEXTURE_CUBE_ARRAY
:
2659 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2660 return img_filter_cube_array_nearest
;
2662 return img_filter_cube_array_linear
;
2664 case PIPE_TEXTURE_3D
:
2665 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2666 return img_filter_3d_nearest
;
2668 return img_filter_3d_linear
;
2672 return img_filter_1d_nearest
;
2678 sample_mip(struct sp_sampler_view
*sp_sview
,
2679 struct sp_sampler
*sp_samp
,
2680 const float s
[TGSI_QUAD_SIZE
],
2681 const float t
[TGSI_QUAD_SIZE
],
2682 const float p
[TGSI_QUAD_SIZE
],
2683 const float c0
[TGSI_QUAD_SIZE
],
2684 const float lod
[TGSI_QUAD_SIZE
],
2685 enum tgsi_sampler_control control
,
2686 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2688 mip_filter_func mip_filter
;
2689 img_filter_func min_img_filter
= NULL
;
2690 img_filter_func mag_img_filter
= NULL
;
2692 if (sp_sview
->pot2d
& sp_samp
->min_mag_equal_repeat_linear
) {
2693 mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2696 mip_filter
= sp_samp
->mip_filter
;
2697 min_img_filter
= get_img_filter(sp_sview
, &sp_samp
->base
, sp_samp
->min_img_filter
);
2698 if (sp_samp
->min_mag_equal
) {
2699 mag_img_filter
= min_img_filter
;
2702 mag_img_filter
= get_img_filter(sp_sview
, &sp_samp
->base
, sp_samp
->base
.mag_img_filter
);
2706 mip_filter(sp_sview
, sp_samp
, min_img_filter
, mag_img_filter
,
2707 s
, t
, p
, c0
, lod
, control
, rgba
);
2709 if (sp_samp
->base
.compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2710 sample_compare(sp_sview
, sp_samp
, s
, t
, p
, c0
, lod
, control
, rgba
);
2713 if (sp_sview
->need_swizzle
) {
2714 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2715 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
2716 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
2723 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
2724 * Put face info into the sampler faces[] array.
2727 sample_cube(struct sp_sampler_view
*sp_sview
,
2728 struct sp_sampler
*sp_samp
,
2729 const float s
[TGSI_QUAD_SIZE
],
2730 const float t
[TGSI_QUAD_SIZE
],
2731 const float p
[TGSI_QUAD_SIZE
],
2732 const float c0
[TGSI_QUAD_SIZE
],
2733 const float c1
[TGSI_QUAD_SIZE
],
2734 enum tgsi_sampler_control control
,
2735 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2738 float ssss
[4], tttt
[4];
2740 /* Not actually used, but the intermediate steps that do the
2741 * dereferencing don't know it.
2743 static float pppp
[4] = { 0, 0, 0, 0 };
2751 direction target sc tc ma
2752 ---------- ------------------------------- --- --- ---
2753 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2754 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2755 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2756 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2757 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2758 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2761 /* Choose the cube face and compute new s/t coords for the 2D face.
2763 * Use the same cube face for all four pixels in the quad.
2765 * This isn't ideal, but if we want to use a different cube face
2766 * per pixel in the quad, we'd have to also compute the per-face
2767 * LOD here too. That's because the four post-face-selection
2768 * texcoords are no longer related to each other (they're
2769 * per-face!) so we can't use subtraction to compute the partial
2770 * deriviates to compute the LOD. Doing so (near cube edges
2771 * anyway) gives us pretty much random values.
2774 /* use the average of the four pixel's texcoords to choose the face */
2775 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
2776 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
2777 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
2778 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
2780 if (arx
>= ary
&& arx
>= arz
) {
2781 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
2782 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
2783 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2784 const float ima
= -0.5F
/ fabsf(s
[j
]);
2785 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
2786 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2787 sp_sview
->faces
[j
] = face
;
2790 else if (ary
>= arx
&& ary
>= arz
) {
2791 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
2792 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
2793 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2794 const float ima
= -0.5F
/ fabsf(t
[j
]);
2795 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
2796 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
2797 sp_sview
->faces
[j
] = face
;
2801 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
2802 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
2803 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2804 const float ima
= -0.5F
/ fabsf(p
[j
]);
2805 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
2806 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
2807 sp_sview
->faces
[j
] = face
;
2812 sample_mip(sp_sview
, sp_samp
, ssss
, tttt
, pppp
, c0
, c1
, control
, rgba
);
2817 sp_get_dims(struct sp_sampler_view
*sp_sview
, int level
,
2820 const struct pipe_sampler_view
*view
= &sp_sview
->base
;
2821 const struct pipe_resource
*texture
= view
->texture
;
2823 /* undefined according to EXT_gpu_program */
2824 level
+= view
->u
.tex
.first_level
;
2825 if (level
> view
->u
.tex
.last_level
)
2828 dims
[0] = u_minify(texture
->width0
, level
);
2830 switch(texture
->target
) {
2831 case PIPE_TEXTURE_1D_ARRAY
:
2832 dims
[1] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
2834 case PIPE_TEXTURE_1D
:
2836 case PIPE_TEXTURE_2D_ARRAY
:
2837 dims
[2] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
2839 case PIPE_TEXTURE_2D
:
2840 case PIPE_TEXTURE_CUBE
:
2841 case PIPE_TEXTURE_RECT
:
2842 dims
[1] = u_minify(texture
->height0
, level
);
2844 case PIPE_TEXTURE_3D
:
2845 dims
[1] = u_minify(texture
->height0
, level
);
2846 dims
[2] = u_minify(texture
->depth0
, level
);
2848 case PIPE_TEXTURE_CUBE_ARRAY
:
2849 dims
[1] = u_minify(texture
->height0
, level
);
2850 dims
[2] = (view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1) / 6;
2853 dims
[0] /= util_format_get_blocksize(view
->format
);
2856 assert(!"unexpected texture target in sp_get_dims()");
2862 * This function is only used for getting unfiltered texels via the
2863 * TXF opcode. The GL spec says that out-of-bounds texel fetches
2864 * produce undefined results. Instead of crashing, lets just clamp
2865 * coords to the texture image size.
2868 sp_get_texels(struct sp_sampler_view
*sp_sview
,
2869 const int v_i
[TGSI_QUAD_SIZE
],
2870 const int v_j
[TGSI_QUAD_SIZE
],
2871 const int v_k
[TGSI_QUAD_SIZE
],
2872 const int lod
[TGSI_QUAD_SIZE
],
2873 const int8_t offset
[3],
2874 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2876 union tex_tile_address addr
;
2877 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2880 int width
, height
, depth
;
2883 /* TODO write a better test for LOD */
2884 addr
.bits
.level
= lod
[0];
2886 width
= u_minify(texture
->width0
, addr
.bits
.level
);
2887 height
= u_minify(texture
->height0
, addr
.bits
.level
);
2888 depth
= u_minify(texture
->depth0
, addr
.bits
.level
);
2890 switch(texture
->target
) {
2892 case PIPE_TEXTURE_1D
:
2893 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2894 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2895 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, 0);
2896 for (c
= 0; c
< 4; c
++) {
2901 case PIPE_TEXTURE_1D_ARRAY
:
2902 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2903 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2904 int y
= CLAMP(v_j
[j
], sp_sview
->base
.u
.tex
.first_layer
, sp_sview
->base
.u
.tex
.last_layer
);
2905 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
2906 for (c
= 0; c
< 4; c
++) {
2911 case PIPE_TEXTURE_2D
:
2912 case PIPE_TEXTURE_RECT
:
2913 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2914 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2915 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2916 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
2917 for (c
= 0; c
< 4; c
++) {
2922 case PIPE_TEXTURE_2D_ARRAY
:
2923 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2924 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2925 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2926 int layer
= CLAMP(v_k
[j
], sp_sview
->base
.u
.tex
.first_layer
, sp_sview
->base
.u
.tex
.last_layer
);
2927 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
2928 for (c
= 0; c
< 4; c
++) {
2933 case PIPE_TEXTURE_3D
:
2934 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2935 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
2936 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
2937 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
2938 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, z
);
2939 for (c
= 0; c
< 4; c
++) {
2944 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
2946 assert(!"Unknown or CUBE texture type in TXF processing\n");
2950 if (sp_sview
->need_swizzle
) {
2951 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2952 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
2953 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
2959 softpipe_create_sampler_state(struct pipe_context
*pipe
,
2960 const struct pipe_sampler_state
*sampler
)
2962 struct sp_sampler
*samp
= CALLOC_STRUCT(sp_sampler
);
2964 samp
->base
= *sampler
;
2966 /* Note that (for instance) linear_texcoord_s and
2967 * nearest_texcoord_s may be active at the same time, if the
2968 * sampler min_img_filter differs from its mag_img_filter.
2970 if (sampler
->normalized_coords
) {
2971 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
2972 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
2973 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
2975 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
2976 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
2977 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
2980 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
2981 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
2982 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
2984 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
2985 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
2986 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
2989 samp
->min_img_filter
= sampler
->min_img_filter
;
2991 switch (sampler
->min_mip_filter
) {
2992 case PIPE_TEX_MIPFILTER_NONE
:
2993 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
2994 samp
->mip_filter
= mip_filter_none_no_filter_select
;
2996 samp
->mip_filter
= mip_filter_none
;
2999 case PIPE_TEX_MIPFILTER_NEAREST
:
3000 samp
->mip_filter
= mip_filter_nearest
;
3003 case PIPE_TEX_MIPFILTER_LINEAR
:
3004 if (sampler
->min_img_filter
== sampler
->mag_img_filter
&&
3005 sampler
->normalized_coords
&&
3006 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
3007 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
3008 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
3009 sampler
->max_anisotropy
<= 1) {
3010 samp
->min_mag_equal_repeat_linear
= TRUE
;
3012 samp
->mip_filter
= mip_filter_linear
;
3014 /* Anisotropic filtering extension. */
3015 if (sampler
->max_anisotropy
> 1) {
3016 samp
->mip_filter
= mip_filter_linear_aniso
;
3018 /* Override min_img_filter:
3019 * min_img_filter needs to be set to NEAREST since we need to access
3020 * each texture pixel as it is and weight it later; using linear
3021 * filters will have incorrect results.
3022 * By setting the filter to NEAREST here, we can avoid calling the
3023 * generic img_filter_2d_nearest in the anisotropic filter function,
3024 * making it possible to use one of the accelerated implementations
3026 samp
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
3028 /* on first access create the lookup table containing the filter weights. */
3030 create_filter_table();
3035 if (samp
->min_img_filter
== sampler
->mag_img_filter
) {
3036 samp
->min_mag_equal
= TRUE
;
3039 return (void *)samp
;
3044 softpipe_get_lambda_func(const struct pipe_sampler_view
*view
, unsigned shader
)
3046 if (shader
!= PIPE_SHADER_FRAGMENT
)
3047 return compute_lambda_vert
;
3049 switch (view
->texture
->target
) {
3051 case PIPE_TEXTURE_1D
:
3052 case PIPE_TEXTURE_1D_ARRAY
:
3053 return compute_lambda_1d
;
3054 case PIPE_TEXTURE_2D
:
3055 case PIPE_TEXTURE_2D_ARRAY
:
3056 case PIPE_TEXTURE_RECT
:
3057 case PIPE_TEXTURE_CUBE
:
3058 case PIPE_TEXTURE_CUBE_ARRAY
:
3059 return compute_lambda_2d
;
3060 case PIPE_TEXTURE_3D
:
3061 return compute_lambda_3d
;
3064 return compute_lambda_1d
;
3069 struct pipe_sampler_view
*
3070 softpipe_create_sampler_view(struct pipe_context
*pipe
,
3071 struct pipe_resource
*resource
,
3072 const struct pipe_sampler_view
*templ
)
3074 struct sp_sampler_view
*sview
= CALLOC_STRUCT(sp_sampler_view
);
3075 struct softpipe_resource
*spr
= (struct softpipe_resource
*)resource
;
3078 struct pipe_sampler_view
*view
= &sview
->base
;
3080 view
->reference
.count
= 1;
3081 view
->texture
= NULL
;
3082 pipe_resource_reference(&view
->texture
, resource
);
3083 view
->context
= pipe
;
3085 if (any_swizzle(view
)) {
3086 sview
->need_swizzle
= TRUE
;
3089 if (resource
->target
== PIPE_TEXTURE_CUBE
||
3090 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3091 sview
->get_samples
= sample_cube
;
3093 sview
->get_samples
= sample_mip
;
3095 sview
->pot2d
= spr
->pot
&&
3096 (resource
->target
== PIPE_TEXTURE_2D
||
3097 resource
->target
== PIPE_TEXTURE_RECT
);
3099 sview
->xpot
= util_logbase2( resource
->width0
);
3100 sview
->ypot
= util_logbase2( resource
->height0
);
3103 return (struct pipe_sampler_view
*) sview
;
3108 sp_tgsi_get_dims(struct tgsi_sampler
*tgsi_sampler
,
3109 const unsigned sview_index
,
3110 int level
, int dims
[4])
3112 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3114 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3115 /* TODO should have defined behavior if no texture is bound. */
3116 sp_get_dims(&sp_samp
->sp_sview
[sview_index
], level
, dims
);
3121 sp_tgsi_get_samples(struct tgsi_sampler
*tgsi_sampler
,
3122 const unsigned sview_index
,
3123 const unsigned sampler_index
,
3124 const float s
[TGSI_QUAD_SIZE
],
3125 const float t
[TGSI_QUAD_SIZE
],
3126 const float p
[TGSI_QUAD_SIZE
],
3127 const float c0
[TGSI_QUAD_SIZE
],
3128 const float lod
[TGSI_QUAD_SIZE
],
3129 float derivs
[3][2][TGSI_QUAD_SIZE
],
3130 const int8_t offset
[3],
3131 enum tgsi_sampler_control control
,
3132 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3134 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3136 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3137 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3138 assert(sp_samp
->sp_sampler
[sampler_index
]);
3139 /* FIXME should have defined behavior if no texture is bound. */
3140 assert(sp_samp
->sp_sview
[sview_index
].get_samples
);
3141 sp_samp
->sp_sview
[sview_index
].get_samples(&sp_samp
->sp_sview
[sview_index
],
3142 sp_samp
->sp_sampler
[sampler_index
],
3143 s
, t
, p
, c0
, lod
, control
, rgba
);
3148 sp_tgsi_get_texel(struct tgsi_sampler
*tgsi_sampler
,
3149 const unsigned sview_index
,
3150 const int i
[TGSI_QUAD_SIZE
],
3151 const int j
[TGSI_QUAD_SIZE
], const int k
[TGSI_QUAD_SIZE
],
3152 const int lod
[TGSI_QUAD_SIZE
], const int8_t offset
[3],
3153 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3155 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3157 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3158 /* FIXME should have defined behavior if no texture is bound. */
3159 assert(sp_samp
->sp_sview
[sview_index
].base
.texture
);
3160 sp_get_texels(&sp_samp
->sp_sview
[sview_index
], i
, j
, k
, lod
, offset
, rgba
);
3164 struct sp_tgsi_sampler
*
3165 sp_create_tgsi_sampler(void)
3167 struct sp_tgsi_sampler
*samp
= CALLOC_STRUCT(sp_tgsi_sampler
);
3171 samp
->base
.get_dims
= sp_tgsi_get_dims
;
3172 samp
->base
.get_samples
= sp_tgsi_get_samples
;
3173 samp
->base
.get_texel
= sp_tgsi_get_texel
;